]>
Dogcows Code - chaz/rasterize/blob - vec.h
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 * Get a pointer to the vector data.
71 const scal_t
* vec_data(const vec_t
* v
)
78 * Print the vector to stdout.
81 void vec_print(vec_t v
)
83 #if (SCALAR_SIZE == 8)
84 const char* fmt
= "[ %9.5lf %9.5lf %9.5lf %9.5lf ]";
86 const char* fmt
= "[ %9.5f %9.5f %9.5f %9.5f ]";
88 printf(fmt
, v
.x
, v
.y
, v
.z
, v
.w
);
93 * Calculate the magnitude of the vector, squared.
96 scal_t
vec_length2(vec_t v
)
98 return v
.x
* v
.x
+ v
.y
* v
.y
+ v
.z
* v
.z
;
102 * Calculate the magnitude of the vector.
105 scal_t
vec_length(vec_t v
)
107 return scal_sqrt(vec_length2(v
));
112 * Determine whether or not two vectors are exactly equal.
115 bool vec_isequal(vec_t a
, vec_t b
)
117 return (a
.x
== b
.x
&& a
.y
== b
.y
&& a
.z
== b
.z
&& a
.w
== b
.w
);
121 * Determine whether or not two vectors are mostly equal.
124 bool vec_isequal2(vec_t a
, vec_t b
, scal_t epsilon
)
126 return scal_isequal2(a
.x
, b
.x
, epsilon
) &&
127 scal_isequal2(a
.y
, b
.y
, epsilon
) &&
128 scal_isequal2(a
.z
, b
.z
, epsilon
) &&
129 scal_isequal2(a
.w
, b
.w
, epsilon
);
133 * Determine if one vector is "less than" another, for purposes of sorting.
136 int vec_compare(vec_t a
, vec_t b
)
138 if (vec_isequal(a
, b
)) {
141 return vec_length2(a
) < vec_length2(b
) ? -1 : 1;
146 * Scale the vector with a scalar value.
149 vec_t
vec_scale(vec_t v
, scal_t s
)
158 * Add two vectors together.
161 vec_t
vec_add(vec_t a
, vec_t b
)
170 * Add three vectors together.
173 vec_t
vec_add2(vec_t a
, vec_t b
, vec_t c
)
175 return vec_add(vec_add(a
, b
), c
);
179 * Subtract a vector from another vector.
182 vec_t
vec_sub(vec_t a
, vec_t b
)
194 vec_t
vec_neg(vec_t v
)
204 * Get a normalized (unit length) vector.
207 vec_t
vec_normalize(vec_t v
)
209 scal_t l
= vec_length(v
);
213 return vec_scale(v
, S(1.0) / l
);
218 * Get the dot product of two vectors, ignoring the last component.
221 scal_t
vec_dot(vec_t a
, vec_t b
)
223 return a
.x
* b
.x
+ a
.y
* b
.y
+ a
.z
* b
.z
;
227 * Get the dot product of two vectors.
230 scal_t
vec_dot2(vec_t a
, vec_t b
)
232 return a
.x
* b
.x
+ a
.y
* b
.y
+ a
.z
* b
.z
+ a
.w
* b
.w
;
237 * Get the cross product of two vectors.
240 vec_t
vec_cross(vec_t a
, vec_t b
)
242 return vec_new(a
.y
* b
.z
- a
.z
* b
.y
,
243 a
.z
* b
.x
- a
.x
* b
.z
,
244 a
.x
* b
.y
- a
.y
* b
.x
);
249 * Perform a homogeneous divide.
252 vec_t
vec_homodiv(vec_t v
)
263 * Interpolate smoothly between three vectors with barycentric coordinates.
266 vec_t
vec_interp(vec_t v1
, vec_t v2
, vec_t v3
, scal_t b
[3])
268 return vec_new(v1
.x
* b
[0] + v2
.x
* b
[1] + v3
.x
* b
[2],
269 v1
.y
* b
[0] + v2
.y
* b
[1] + v3
.y
* b
[2],
270 v1
.z
* b
[0] + v2
.z
* b
[1] + v3
.z
* b
[2]);
274 * Interpolate smoothly between three texture coordinates with barycentric
275 * coordinates and perspective correction.
278 vec_t
vec_tinterp(vec_t v1
, vec_t v2
, vec_t v3
, scal_t b
[3])
280 scal_t denom
= (S(1.0) / v1
.w
) * b
[0]
281 + (S(1.0) / v2
.w
) * b
[1]
282 + (S(1.0) / v3
.w
) * b
[2];
283 return vec_new(scal_clamp(((v1
.x
/ v1
.w
) * b
[0]
284 + (v2
.x
/ v2
.w
) * b
[1]
285 + (v3
.x
/ v3
.w
) * b
[2]) / denom
, S(0.0), S(1.0)),
286 scal_clamp(((v1
.y
/ v1
.w
) * b
[0]
287 + (v2
.y
/ v2
.w
) * b
[1]
288 + (v3
.y
/ v3
.w
) * b
[2]) / denom
, S(0.0), S(1.0)),
This page took 0.047356 seconds and 4 git commands to generate.