2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
8 #ifndef __TRI_H__
9 #define __TRI_H__
11 #include "mat.h"
12 #include "vert.h"
15 /*
16 * A triangle is a polygon of three vertices.
17 */
18 struct tri
19 {
20 vert_t a;
21 vert_t b;
22 vert_t c;
23 };
24 typedef struct tri tri_t;
26 /*
27 * Initialize a triangle.
28 */
29 __fast__
30 void tri_init(tri_t* t, vert_t a, vert_t b, vert_t c)
31 {
32 t->a = a;
33 t->b = b;
34 t->c = c;
35 }
38 /*
39 * Create a new triangle.
40 */
41 __fast__
42 tri_t tri_new(vert_t a, vert_t b, vert_t c)
43 {
44 tri_t t;
45 tri_init(&t, a, b, c);
46 return t;
47 }
49 #define TRI_ZERO tri_new(VERT_ZERO, VERT_ZERO, VERT_ZERO)
52 /*
53 * Create a new triangle on the heap.
54 */
55 __fast__
56 tri_t* tri_alloc(vert_t a, vert_t b, vert_t c)
57 {
58 tri_t* t = (tri_t*)mem_alloc(sizeof(tri_t));
59 tri_init(t, a, b, c);
60 return t;
61 }
64 /*
65 * Apply a transformation matrix to alter the triangle geometry.
66 */
67 __fast__
68 tri_t tri_transform(tri_t t, mat_t m)
69 {
70 t.a.v = mat_apply(m, t.a.v);
71 t.b.v = mat_apply(m, t.b.v);
72 t.c.v = mat_apply(m, t.c.v);
73 return t;
74 }
77 /*
78 * Get the barycentric coordinates of a vector against a triangle. The
79 * returned coordinates will be a linear combination, but they may not
80 * actually be barycentric coordinates. Use the function vec_is_barycentric
81 * to check if they really are barycentric coordinates, meaning the point
82 * vector v is inside the triangle, ignoring the Z components.
83 */
84 vec_t tri_barycentric(const tri_t* t, vec_t v);
87 #endif // __TRI_H__