]>
Dogcows Code - chaz/rasterize/blob - vec.h
2c1269dccc88eb567e1962ecbcda2d8e5d082f56
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 int vec_compare(vec_t a
, vec_t b
)
112 if (vec_isequal(a
, b
)) {
115 return vec_length2(a
) < vec_length2(b
) ? -1 : 1;
120 * Scale the vector with a scalar value.
123 vec_t
vec_scale(vec_t v
, scal_t s
)
132 * Add two vectors together.
135 vec_t
vec_add(vec_t a
, vec_t b
)
144 * Subtract a vector from another vector.
147 vec_t
vec_sub(vec_t a
, vec_t b
)
159 vec_t
vec_neg(vec_t v
)
169 * Get a normalized (unit length) vector.
172 vec_t
vec_normalize(vec_t v
)
174 scal_t l
= vec_length(v
);
178 return vec_scale(v
, S(1.0) / l
);
183 * Get the dot product of two vectors, ignoring the last component.
186 scal_t
vec_dot(vec_t a
, vec_t b
)
188 return a
.x
* b
.x
+ a
.y
* b
.y
+ a
.z
* b
.z
;
192 * Get the dot product of two vectors.
195 scal_t
vec_dot2(vec_t a
, vec_t b
)
197 return a
.x
* b
.x
+ a
.y
* b
.y
+ a
.z
* b
.z
+ a
.w
* b
.w
;
202 * Get the cross product of two vectors.
205 vec_t
vec_cross(vec_t a
, vec_t b
)
207 return vec_new(a
.y
* b
.z
- a
.z
* b
.y
,
208 a
.z
* b
.x
- a
.x
* b
.z
,
209 a
.x
* b
.y
- a
.y
* b
.x
);
214 * Perform a homogeneous divide.
217 vec_t
vec_homodiv(vec_t v
)
This page took 0.044535 seconds and 4 git commands to generate.