]>
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))
67 * Print the vector to stdout.
70 void vec_print(vec_t v
)
72 #if (SCALAR_SIZE == 8)
73 const char* fmt
= "[ %9.5lf %9.5lf %9.5lf %9.5lf ]";
75 const char* fmt
= "[ %9.5f %9.5f %9.5f %9.5f ]";
77 printf(fmt
, v
.x
, v
.y
, v
.z
, v
.w
);
82 * Calculate the magnitude of the vector, squared.
85 scal_t
vec_length2(vec_t v
)
87 return v
.x
* v
.x
+ v
.y
* v
.y
+ v
.z
* v
.z
;
91 * Calculate the magnitude of the vector.
94 scal_t
vec_length(vec_t v
)
96 return scal_sqrt(vec_length2(v
));
101 * Determine whether or not two vectors are exactly equal.
104 bool vec_isequal(vec_t a
, vec_t b
)
106 return (a
.x
== b
.x
&& a
.y
== b
.y
&& a
.z
== b
.z
&& a
.w
== b
.w
);
110 * Determine whether or not two vectors are mostly equal.
113 bool vec_isequal2(vec_t a
, vec_t b
, scal_t epsilon
)
115 return scal_isequal2(a
.x
, b
.x
, epsilon
) &&
116 scal_isequal2(a
.y
, b
.y
, epsilon
) &&
117 scal_isequal2(a
.z
, b
.z
, epsilon
) &&
118 scal_isequal2(a
.w
, b
.w
, epsilon
);
122 * Determine if one vector is "less than" another, for purposes of sorting.
125 int vec_compare(vec_t a
, vec_t b
)
127 if (vec_isequal(a
, b
)) {
130 return vec_length2(a
) < vec_length2(b
) ? -1 : 1;
135 * Scale the vector with a scalar value.
138 vec_t
vec_scale(vec_t v
, scal_t s
)
147 * Add two vectors together.
150 vec_t
vec_add(vec_t a
, vec_t b
)
159 * Subtract a vector from another vector.
162 vec_t
vec_sub(vec_t a
, vec_t b
)
174 vec_t
vec_neg(vec_t v
)
184 * Get a normalized (unit length) vector.
187 vec_t
vec_normalize(vec_t v
)
189 scal_t l
= vec_length(v
);
193 return vec_scale(v
, S(1.0) / l
);
198 * Get the dot product of two vectors, ignoring the last component.
201 scal_t
vec_dot(vec_t a
, vec_t b
)
203 return a
.x
* b
.x
+ a
.y
* b
.y
+ a
.z
* b
.z
;
207 * Get the dot product of two vectors.
210 scal_t
vec_dot2(vec_t a
, vec_t b
)
212 return a
.x
* b
.x
+ a
.y
* b
.y
+ a
.z
* b
.z
+ a
.w
* b
.w
;
217 * Get the cross product of two vectors.
220 vec_t
vec_cross(vec_t a
, vec_t b
)
222 return vec_new(a
.y
* b
.z
- a
.z
* b
.y
,
223 a
.z
* b
.x
- a
.x
* b
.z
,
224 a
.x
* b
.y
- a
.y
* b
.x
);
229 * Perform a homogeneous divide.
232 vec_t
vec_homodiv(vec_t v
)
This page took 0.043017 seconds and 5 git commands to generate.