]>
Dogcows Code - chaz/rasterize/blob - vec.h
48635238202c5de1a437161028112bf1085352c1
3 * CS5600 University of Utah
5 * mcgarvey@eng.utah.edu
15 * A simple vector class.
24 typedef struct vec vec_t
;
27 * Initialize a vector with four components.
30 void vec_init(vec_t
* v
, scal_t x
, scal_t y
, scal_t z
, scal_t w
)
40 * Create a new vector with four components.
43 vec_t
vec_new2(scal_t x
, scal_t y
, scal_t z
, scal_t w
)
46 vec_init(&v
, x
, y
, z
, w
);
51 * Create a new vector with three components. The fourth component is
55 vec_t
vec_new(scal_t x
, scal_t y
, scal_t z
)
57 return vec_new2(x
, y
, z
, S(1.0));
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))
64 #define VEC_ZERO_FREE vec_new2(S(0.0), S(0.0), S(0.0), S(0.0))
68 * Print the vector to stdout.
71 void vec_print(vec_t v
)
73 #if (SCALAR_SIZE == 8)
74 const char* fmt
= "[ %9.5lf %9.5lf %9.5lf %9.5lf ]";
76 const char* fmt
= "[ %9.5f %9.5f %9.5f %9.5f ]";
78 printf(fmt
, v
.x
, v
.y
, v
.z
, v
.w
);
83 * Calculate the magnitude of the vector, squared.
86 scal_t
vec_length2(vec_t v
)
88 return v
.x
* v
.x
+ v
.y
* v
.y
+ v
.z
* v
.z
;
92 * Calculate the magnitude of the vector.
95 scal_t
vec_length(vec_t v
)
97 return scal_sqrt(vec_length2(v
));
102 * Determine whether or not two vectors are exactly equal.
105 bool vec_isequal(vec_t a
, vec_t b
)
107 return (a
.x
== b
.x
&& a
.y
== b
.y
&& a
.z
== b
.z
&& a
.w
== b
.w
);
111 * Determine whether or not two vectors are mostly equal.
114 bool vec_isequal2(vec_t a
, vec_t b
, scal_t epsilon
)
116 return scal_isequal2(a
.x
, b
.x
, epsilon
) &&
117 scal_isequal2(a
.y
, b
.y
, epsilon
) &&
118 scal_isequal2(a
.z
, b
.z
, epsilon
) &&
119 scal_isequal2(a
.w
, b
.w
, epsilon
);
123 * Determine if one vector is "less than" another, for purposes of sorting.
126 int vec_compare(vec_t a
, vec_t b
)
128 if (vec_isequal(a
, b
)) {
131 return vec_length2(a
) < vec_length2(b
) ? -1 : 1;
136 * Scale the vector with a scalar value.
139 vec_t
vec_scale(vec_t v
, scal_t s
)
148 * Add two vectors together.
151 vec_t
vec_add(vec_t a
, vec_t b
)
160 * Add three vectors together.
163 vec_t
vec_add2(vec_t a
, vec_t b
, vec_t c
)
165 return vec_add(vec_add(a
, b
), c
);
169 * Subtract a vector from another vector.
172 vec_t
vec_sub(vec_t a
, vec_t b
)
184 vec_t
vec_neg(vec_t v
)
194 * Get a normalized (unit length) vector.
197 vec_t
vec_normalize(vec_t v
)
199 scal_t l
= vec_length(v
);
203 return vec_scale(v
, S(1.0) / l
);
208 * Get the dot product of two vectors, ignoring the last component.
211 scal_t
vec_dot(vec_t a
, vec_t b
)
213 return a
.x
* b
.x
+ a
.y
* b
.y
+ a
.z
* b
.z
;
217 * Get the dot product of two vectors.
220 scal_t
vec_dot2(vec_t a
, vec_t b
)
222 return a
.x
* b
.x
+ a
.y
* b
.y
+ a
.z
* b
.z
+ a
.w
* b
.w
;
227 * Get the cross product of two vectors.
230 vec_t
vec_cross(vec_t a
, vec_t b
)
232 return vec_new(a
.y
* b
.z
- a
.z
* b
.y
,
233 a
.z
* b
.x
- a
.x
* b
.z
,
234 a
.x
* b
.y
- a
.y
* b
.x
);
239 * Perform a homogeneous divide.
242 vec_t
vec_homodiv(vec_t v
)
253 * Interpolate smoothly between three vectors with barycentric coordinates.
256 vec_t
vec_interp(vec_t v1
, vec_t v2
, vec_t v3
, scal_t b
[3])
258 return vec_new(v1
.x
* b
[0] + v2
.x
* b
[1] + v3
.x
* b
[2],
259 v1
.y
* b
[0] + v2
.y
* b
[1] + v3
.y
* b
[2],
260 v1
.z
* b
[0] + v2
.z
* b
[1] + v3
.z
* b
[2]);
This page took 0.041749 seconds and 3 git commands to generate.