2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
8 #ifndef __VEC_H__
9 #define __VEC_H__
11 #include "common.h"
14 /*
15 * A simple vector class.
16 */
17 struct vec
18 {
19 scal_t x;
20 scal_t y;
21 scal_t z;
22 scal_t w;
23 };
24 typedef struct vec vec_t;
26 /*
27 * Initialize a vector with four components.
28 */
29 __fast__
30 void vec_init(vec_t* v, scal_t x, scal_t y, scal_t z, scal_t w)
31 {
32 v->x = x;
33 v->y = y;
34 v->z = z;
35 v->w = w;
36 }
39 /*
40 * Create a new vector with four components.
41 */
42 __fast__
43 vec_t vec_new2(scal_t x, scal_t y, scal_t z, scal_t w)
44 {
45 vec_t v;
46 vec_init(&v, x, y, z, w);
47 return v;
48 }
50 /*
51 * Create a new vector with three components. The fourth component is
52 * initialized to one.
53 */
54 __fast__
55 vec_t vec_new(scal_t x, scal_t y, scal_t z)
56 {
57 return vec_new2(x, y, z, S(1.0));
58 }
60 #define VEC_ZERO vec_new(S(0.0), S(0.0), S(0.0))
61 #define VEC_ORTHO_X vec_new(S(1.0), S(0.0), S(0.0))
62 #define VEC_ORTHO_Y vec_new(S(0.0), S(1.0), S(0.0))
63 #define VEC_ORTHO_Z vec_new(S(0.0), S(0.0), S(1.0))
66 /*
67 * Scale the vector with a scalar value.
68 */
69 __fast__
70 vec_t vec_scale(vec_t v, scal_t s)
71 {
72 v.x *= s;
73 v.y *= s;
74 v.z *= s;
75 return v;
76 }
78 /*
79 * Add two vectors together.
80 */
81 __fast__
82 vec_t vec_add(vec_t a, vec_t b)
83 {
84 a.x += b.x;
85 a.y += b.y;
86 a.z += b.z;
87 return a;
88 }
90 /*
91 * Subtract a vector from another vector.
92 */
93 __fast__
94 vec_t vec_sub(vec_t a, vec_t b)
95 {
96 a.x -= b.x;
97 a.y -= b.y;
98 a.z -= b.z;
99 return a;
100 }
103 /*
104 * Get the dot product of two vectors.
105 */
106 __fast__
107 scal_t vec_dot(vec_t a, vec_t b)
108 {
109 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
110 }
112 /*
113 * Get the dot product of two vectors, ignoring the last component.
114 */
115 __fast__
116 scal_t vec_dot3(vec_t a, vec_t b)
117 {
118 return a.x * b.x + a.y * b.y + a.z * b.z;
119 }
122 /*
123 * Check whether the values of the first three components could actually be
124 * barycentric coordinates. Note: The necessary condition of each component
125 * adding up to one is assumed and not checked.
126 */
127 __fast__
128 bool vec_is_barycentric(vec_t v)
129 {
130 /*
131 * XXX: I'm fudging the bounds a little because horizontal edges (relative
132 * to the screen) are otherwise sometimes really jagged. This probably
133 * isn't the best solution.
134 */
135 if (S(-0.000001) <= v.x && v.x <= S(1.000001) &&
136 S(-0.000001) <= v.y && v.y <= S(1.000001) &&
137 S(-0.000001) <= v.z && v.z <= S(1.000001)) {
138 return true;
139 }
140 return false;
141 }
143 #endif // __VEC_H__
This page took 0.045148 seconds and 5 git commands to generate.