/* * CS5600 University of Utah * Charles McGarvey * mcgarvey@eng.utah.edu */ #ifndef __TRI_H__ #define __TRI_H__ #include "mat.h" #include "vert.h" /* * A triangle is a polygon of three vertices. */ struct tri { vert_t a; vert_t b; vert_t c; }; typedef struct tri tri_t; /* * Initialize a triangle. */ __fast__ void tri_init(tri_t* t, vert_t a, vert_t b, vert_t c) { t->a = a; t->b = b; t->c = c; } /* * Create a new triangle. */ __fast__ tri_t tri_new(vert_t a, vert_t b, vert_t c) { tri_t t; tri_init(&t, a, b, c); return t; } #define TRI_ZERO tri_new(VERT_ZERO, VERT_ZERO, VERT_ZERO) /* * Create a new triangle on the heap. */ __fast__ tri_t* tri_alloc(vert_t a, vert_t b, vert_t c) { tri_t* t = (tri_t*)mem_alloc(sizeof(tri_t)); tri_init(t, a, b, c); return t; } /* * Apply a transformation matrix to alter the triangle geometry. */ __fast__ tri_t tri_transform(tri_t t, mat_t m) { t.a.v = mat_apply(m, t.a.v); t.b.v = mat_apply(m, t.b.v); t.c.v = mat_apply(m, t.c.v); return t; } /* * Get the barycentric coordinates of a vector against a triangle. The * returned coordinates will be a linear combination, but they may not * actually be barycentric coordinates. Use the function vec_is_barycentric * to check if they really are barycentric coordinates, meaning the point * vector v is inside the triangle, ignoring the Z components. */ vec_t tri_barycentric(const tri_t* t, vec_t v); #endif // __TRI_H__