istereo
view libs/vmath/vector.inl @ 33:33e9eedad390
ah.. blunder
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Fri, 09 Sep 2011 00:03:37 +0300 |
parents | |
children | ff055bff6a15 |
line source
1 #include <math.h>
3 #ifdef __cplusplus
4 extern "C" {
5 #endif /* __cplusplus */
7 /* C 2D vector functions */
8 static inline vec2_t v2_cons(scalar_t x, scalar_t y)
9 {
10 vec2_t v;
11 v.x = x;
12 v.y = y;
13 return v;
14 }
16 static inline void v2_print(FILE *fp, vec2_t v)
17 {
18 fprintf(fp, "[ %.4f %.4f ]", v.x, v.y);
19 }
21 static inline vec2_t v2_add(vec2_t v1, vec2_t v2)
22 {
23 vec2_t res;
24 res.x = v1.x + v2.x;
25 res.y = v1.y + v2.y;
26 return res;
27 }
29 static inline vec2_t v2_sub(vec2_t v1, vec2_t v2)
30 {
31 vec2_t res;
32 res.x = v1.x - v2.x;
33 res.y = v1.y - v2.y;
34 return res;
35 }
37 static inline vec2_t v2_scale(vec2_t v, scalar_t s)
38 {
39 vec2_t res;
40 res.x = v.x * s;
41 res.y = v.y * s;
42 return res;
43 }
45 static inline scalar_t v2_dot(vec2_t v1, vec2_t v2)
46 {
47 return v1.x * v2.x + v1.y * v2.y;
48 }
50 static inline scalar_t v2_length(vec2_t v)
51 {
52 return sqrt(v.x * v.x + v.y * v.y);
53 }
55 static inline scalar_t v2_length_sq(vec2_t v)
56 {
57 return v.x * v.x + v.y * v.y;
58 }
60 static inline vec2_t v2_normalize(vec2_t v)
61 {
62 scalar_t len = (scalar_t)sqrt(v.x * v.x + v.y * v.y);
63 v.x /= len;
64 v.y /= len;
65 return v;
66 }
68 static inline vec2_t v2_lerp(vec2_t v1, vec2_t v2, scalar_t t)
69 {
70 vec2_t res;
71 res.x = v1.x + (v2.x - v1.x) * t;
72 res.y = v1.y + (v2.y - v1.y) * t;
73 return res;
74 }
77 /* C 3D vector functions */
78 static inline vec3_t v3_cons(scalar_t x, scalar_t y, scalar_t z)
79 {
80 vec3_t v;
81 v.x = x;
82 v.y = y;
83 v.z = z;
84 return v;
85 }
87 static inline void v3_print(FILE *fp, vec3_t v)
88 {
89 fprintf(fp, "[ %.4f %.4f %.4f ]", v.x, v.y, v.z);
90 }
92 static inline vec3_t v3_add(vec3_t v1, vec3_t v2)
93 {
94 v1.x += v2.x;
95 v1.y += v2.y;
96 v1.z += v2.z;
97 return v1;
98 }
100 static inline vec3_t v3_sub(vec3_t v1, vec3_t v2)
101 {
102 v1.x -= v2.x;
103 v1.y -= v2.y;
104 v1.z -= v2.z;
105 return v1;
106 }
108 static inline vec3_t v3_neg(vec3_t v)
109 {
110 v.x = -v.x;
111 v.y = -v.y;
112 v.z = -v.z;
113 return v;
114 }
116 static inline vec3_t v3_mul(vec3_t v1, vec3_t v2)
117 {
118 v1.x *= v2.x;
119 v1.y *= v2.y;
120 v1.z *= v2.z;
121 return v1;
122 }
124 static inline vec3_t v3_scale(vec3_t v1, scalar_t s)
125 {
126 v1.x *= s;
127 v1.y *= s;
128 v1.z *= s;
129 return v1;
130 }
132 static inline scalar_t v3_dot(vec3_t v1, vec3_t v2)
133 {
134 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
135 }
137 static inline vec3_t v3_cross(vec3_t v1, vec3_t v2)
138 {
139 vec3_t v;
140 v.x = v1.y * v2.z - v1.z * v2.y;
141 v.y = v1.z * v2.x - v1.x * v2.z;
142 v.z = v1.x * v2.y - v1.y * v2.x;
143 return v;
144 }
146 static inline scalar_t v3_length(vec3_t v)
147 {
148 return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
149 }
151 static inline scalar_t v3_length_sq(vec3_t v)
152 {
153 return v.x * v.x + v.y * v.y + v.z * v.z;
154 }
156 static inline vec3_t v3_normalize(vec3_t v)
157 {
158 scalar_t len = sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
159 v.x /= len;
160 v.y /= len;
161 v.z /= len;
162 return v;
163 }
165 static inline vec3_t v3_transform(vec3_t v, mat4_t m)
166 {
167 vec3_t res;
168 res.x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3];
169 res.y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3];
170 res.z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3];
171 return res;
172 }
174 static inline vec3_t v3_rotate(vec3_t v, scalar_t x, scalar_t y, scalar_t z)
175 {
176 void m4_rotate(mat4_t, scalar_t, scalar_t, scalar_t);
178 mat4_t m = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
179 m4_rotate(m, x, y, z);
180 return v3_transform(v, m);
181 }
183 static inline vec3_t v3_rotate_axis(vec3_t v, scalar_t angle, scalar_t x, scalar_t y, scalar_t z)
184 {
185 void m4_rotate_axis(mat4_t, scalar_t, scalar_t, scalar_t, scalar_t);
187 mat4_t m = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
188 m4_rotate_axis(m, angle, x, y, z);
189 return v3_transform(v, m);
190 }
192 static inline vec3_t v3_rotate_quat(vec3_t v, quat_t q)
193 {
194 quat_t quat_rotate_quat(quat_t, quat_t);
196 quat_t vq = v4_cons(v.x, v.y, v.z, 0.0);
197 quat_t res = quat_rotate_quat(vq, q);
198 return v3_cons(res.x, res.y, res.z);
199 }
201 static inline vec3_t v3_reflect(vec3_t v, vec3_t n)
202 {
203 scalar_t dot = v3_dot(v, n);
204 return v3_sub(v3_scale(n, dot * 2.0), v);
205 }
207 static inline vec3_t v3_lerp(vec3_t v1, vec3_t v2, scalar_t t)
208 {
209 v1.x += (v2.x - v1.x) * t;
210 v1.y += (v2.y - v1.y) * t;
211 v1.z += (v2.z - v1.z) * t;
212 return v1;
213 }
215 /* C 4D vector functions */
216 static inline vec4_t v4_cons(scalar_t x, scalar_t y, scalar_t z, scalar_t w)
217 {
218 vec4_t v;
219 v.x = x;
220 v.y = y;
221 v.z = z;
222 v.w = w;
223 return v;
224 }
226 static inline void v4_print(FILE *fp, vec4_t v)
227 {
228 fprintf(fp, "[ %.4f %.4f %.4f %.4f ]", v.x, v.y, v.z, v.w);
229 }
231 static inline vec4_t v4_add(vec4_t v1, vec4_t v2)
232 {
233 v1.x += v2.x;
234 v1.y += v2.y;
235 v1.z += v2.z;
236 v1.w += v2.w;
237 return v1;
238 }
240 static inline vec4_t v4_sub(vec4_t v1, vec4_t v2)
241 {
242 v1.x -= v2.x;
243 v1.y -= v2.y;
244 v1.z -= v2.z;
245 v1.w -= v2.w;
246 return v1;
247 }
249 static inline vec4_t v4_neg(vec4_t v)
250 {
251 v.x = -v.x;
252 v.y = -v.y;
253 v.z = -v.z;
254 v.w = -v.w;
255 return v;
256 }
258 static inline vec4_t v4_mul(vec4_t v1, vec4_t v2)
259 {
260 v1.x *= v2.x;
261 v1.y *= v2.y;
262 v1.z *= v2.z;
263 v1.w *= v2.w;
264 return v1;
265 }
267 static inline vec4_t v4_scale(vec4_t v, scalar_t s)
268 {
269 v.x *= s;
270 v.y *= s;
271 v.z *= s;
272 v.w *= s;
273 return v;
274 }
276 static inline scalar_t v4_dot(vec4_t v1, vec4_t v2)
277 {
278 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
279 }
281 static inline scalar_t v4_length(vec4_t v)
282 {
283 return sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
284 }
286 static inline scalar_t v4_length_sq(vec4_t v)
287 {
288 return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
289 }
291 static inline vec4_t v4_normalize(vec4_t v)
292 {
293 scalar_t len = sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
294 v.x /= len;
295 v.y /= len;
296 v.z /= len;
297 v.w /= len;
298 return v;
299 }
301 static inline vec4_t v4_transform(vec4_t v, mat4_t m)
302 {
303 vec4_t res;
304 res.x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w;
305 res.y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w;
306 res.z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w;
307 res.w = m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w;
308 return res;
309 }
311 #ifdef __cplusplus
312 } /* extern "C" */
315 /* --------------- C++ part -------------- */
317 inline scalar_t &Vector2::operator [](int elem) {
318 return elem ? y : x;
319 }
321 inline const scalar_t &Vector2::operator [](int elem) const {
322 return elem ? y : x;
323 }
325 inline Vector2 operator -(const Vector2 &vec) {
326 return Vector2(-vec.x, -vec.y);
327 }
329 inline scalar_t dot_product(const Vector2 &v1, const Vector2 &v2) {
330 return v1.x * v2.x + v1.y * v2.y;
331 }
333 inline Vector2 operator +(const Vector2 &v1, const Vector2 &v2) {
334 return Vector2(v1.x + v2.x, v1.y + v2.y);
335 }
337 inline Vector2 operator -(const Vector2 &v1, const Vector2 &v2) {
338 return Vector2(v1.x - v2.x, v1.y - v2.y);
339 }
341 inline Vector2 operator *(const Vector2 &v1, const Vector2 &v2) {
342 return Vector2(v1.x * v2.x, v1.y * v2.y);
343 }
345 inline Vector2 operator /(const Vector2 &v1, const Vector2 &v2) {
346 return Vector2(v1.x / v2.x, v1.y / v2.y);
347 }
349 inline bool operator ==(const Vector2 &v1, const Vector2 &v2) {
350 return (fabs(v1.x - v2.x) < XSMALL_NUMBER) && (fabs(v1.y - v2.x) < XSMALL_NUMBER);
351 }
353 inline void operator +=(Vector2 &v1, const Vector2 &v2) {
354 v1.x += v2.x;
355 v1.y += v2.y;
356 }
358 inline void operator -=(Vector2 &v1, const Vector2 &v2) {
359 v1.x -= v2.x;
360 v1.y -= v2.y;
361 }
363 inline void operator *=(Vector2 &v1, const Vector2 &v2) {
364 v1.x *= v2.x;
365 v1.y *= v2.y;
366 }
368 inline void operator /=(Vector2 &v1, const Vector2 &v2) {
369 v1.x /= v2.x;
370 v1.y /= v2.y;
371 }
373 inline Vector2 operator +(const Vector2 &vec, scalar_t scalar) {
374 return Vector2(vec.x + scalar, vec.y + scalar);
375 }
377 inline Vector2 operator +(scalar_t scalar, const Vector2 &vec) {
378 return Vector2(vec.x + scalar, vec.y + scalar);
379 }
381 inline Vector2 operator -(scalar_t scalar, const Vector2 &vec) {
382 return Vector2(vec.x - scalar, vec.y - scalar);
383 }
385 inline Vector2 operator *(const Vector2 &vec, scalar_t scalar) {
386 return Vector2(vec.x * scalar, vec.y * scalar);
387 }
389 inline Vector2 operator *(scalar_t scalar, const Vector2 &vec) {
390 return Vector2(vec.x * scalar, vec.y * scalar);
391 }
393 inline Vector2 operator /(const Vector2 &vec, scalar_t scalar) {
394 return Vector2(vec.x / scalar, vec.y / scalar);
395 }
397 inline void operator +=(Vector2 &vec, scalar_t scalar) {
398 vec.x += scalar;
399 vec.y += scalar;
400 }
402 inline void operator -=(Vector2 &vec, scalar_t scalar) {
403 vec.x -= scalar;
404 vec.y -= scalar;
405 }
407 inline void operator *=(Vector2 &vec, scalar_t scalar) {
408 vec.x *= scalar;
409 vec.y *= scalar;
410 }
412 inline void operator /=(Vector2 &vec, scalar_t scalar) {
413 vec.x /= scalar;
414 vec.y /= scalar;
415 }
417 inline scalar_t Vector2::length() const {
418 return sqrt(x*x + y*y);
419 }
421 inline scalar_t Vector2::length_sq() const {
422 return x*x + y*y;
423 }
425 inline Vector2 lerp(const Vector2 &a, const Vector2 &b, scalar_t t)
426 {
427 return a + (b - a) * t;
428 }
430 inline Vector2 catmull_rom_spline(const Vector2 &v0, const Vector2 &v1,
431 const Vector2 &v2, const Vector2 &v3, scalar_t t)
432 {
433 scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t);
434 scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t);
435 scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t);
436 return Vector2(x, y);
437 }
440 /* ------------- Vector3 -------------- */
442 inline scalar_t &Vector3::operator [](int elem) {
443 return elem ? (elem == 1 ? y : z) : x;
444 }
446 inline const scalar_t &Vector3::operator [](int elem) const {
447 return elem ? (elem == 1 ? y : z) : x;
448 }
450 /* unary operations */
451 inline Vector3 operator -(const Vector3 &vec) {
452 return Vector3(-vec.x, -vec.y, -vec.z);
453 }
455 /* binary vector (op) vector operations */
456 inline scalar_t dot_product(const Vector3 &v1, const Vector3 &v2) {
457 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
458 }
460 inline Vector3 cross_product(const Vector3 &v1, const Vector3 &v2) {
461 return Vector3(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x);
462 }
465 inline Vector3 operator +(const Vector3 &v1, const Vector3 &v2) {
466 return Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
467 }
469 inline Vector3 operator -(const Vector3 &v1, const Vector3 &v2) {
470 return Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
471 }
473 inline Vector3 operator *(const Vector3 &v1, const Vector3 &v2) {
474 return Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
475 }
477 inline Vector3 operator /(const Vector3 &v1, const Vector3 &v2) {
478 return Vector3(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z);
479 }
481 inline bool operator ==(const Vector3 &v1, const Vector3 &v2) {
482 return (fabs(v1.x - v2.x) < XSMALL_NUMBER) && (fabs(v1.y - v2.y) < XSMALL_NUMBER) && (fabs(v1.z - v2.z) < XSMALL_NUMBER);
483 }
485 inline void operator +=(Vector3 &v1, const Vector3 &v2) {
486 v1.x += v2.x;
487 v1.y += v2.y;
488 v1.z += v2.z;
489 }
491 inline void operator -=(Vector3 &v1, const Vector3 &v2) {
492 v1.x -= v2.x;
493 v1.y -= v2.y;
494 v1.z -= v2.z;
495 }
497 inline void operator *=(Vector3 &v1, const Vector3 &v2) {
498 v1.x *= v2.x;
499 v1.y *= v2.y;
500 v1.z *= v2.z;
501 }
503 inline void operator /=(Vector3 &v1, const Vector3 &v2) {
504 v1.x /= v2.x;
505 v1.y /= v2.y;
506 v1.z /= v2.z;
507 }
508 /* binary vector (op) scalar and scalar (op) vector operations */
509 inline Vector3 operator +(const Vector3 &vec, scalar_t scalar) {
510 return Vector3(vec.x + scalar, vec.y + scalar, vec.z + scalar);
511 }
513 inline Vector3 operator +(scalar_t scalar, const Vector3 &vec) {
514 return Vector3(vec.x + scalar, vec.y + scalar, vec.z + scalar);
515 }
517 inline Vector3 operator -(const Vector3 &vec, scalar_t scalar) {
518 return Vector3(vec.x - scalar, vec.y - scalar, vec.z - scalar);
519 }
521 inline Vector3 operator *(const Vector3 &vec, scalar_t scalar) {
522 return Vector3(vec.x * scalar, vec.y * scalar, vec.z * scalar);
523 }
525 inline Vector3 operator *(scalar_t scalar, const Vector3 &vec) {
526 return Vector3(vec.x * scalar, vec.y * scalar, vec.z * scalar);
527 }
529 inline Vector3 operator /(const Vector3 &vec, scalar_t scalar) {
530 return Vector3(vec.x / scalar, vec.y / scalar, vec.z / scalar);
531 }
533 inline void operator +=(Vector3 &vec, scalar_t scalar) {
534 vec.x += scalar;
535 vec.y += scalar;
536 vec.z += scalar;
537 }
539 inline void operator -=(Vector3 &vec, scalar_t scalar) {
540 vec.x -= scalar;
541 vec.y -= scalar;
542 vec.z -= scalar;
543 }
545 inline void operator *=(Vector3 &vec, scalar_t scalar) {
546 vec.x *= scalar;
547 vec.y *= scalar;
548 vec.z *= scalar;
549 }
551 inline void operator /=(Vector3 &vec, scalar_t scalar) {
552 vec.x /= scalar;
553 vec.y /= scalar;
554 vec.z /= scalar;
555 }
557 inline scalar_t Vector3::length() const {
558 return sqrt(x*x + y*y + z*z);
559 }
560 inline scalar_t Vector3::length_sq() const {
561 return x*x + y*y + z*z;
562 }
564 inline Vector3 lerp(const Vector3 &a, const Vector3 &b, scalar_t t) {
565 return a + (b - a) * t;
566 }
568 inline Vector3 catmull_rom_spline(const Vector3 &v0, const Vector3 &v1,
569 const Vector3 &v2, const Vector3 &v3, scalar_t t)
570 {
571 scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t);
572 scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t);
573 scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t);
574 scalar_t z = spline(v0.z, v1.z, v2.z, v3.z, t);
575 return Vector3(x, y, z);
576 }
578 /* ----------- Vector4 ----------------- */
580 inline scalar_t &Vector4::operator [](int elem) {
581 return elem ? (elem == 1 ? y : (elem == 2 ? z : w)) : x;
582 }
584 inline const scalar_t &Vector4::operator [](int elem) const {
585 return elem ? (elem == 1 ? y : (elem == 2 ? z : w)) : x;
586 }
588 inline Vector4 operator -(const Vector4 &vec) {
589 return Vector4(-vec.x, -vec.y, -vec.z, -vec.w);
590 }
592 inline scalar_t dot_product(const Vector4 &v1, const Vector4 &v2) {
593 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
594 }
596 inline Vector4 cross_product(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3) {
597 scalar_t a, b, c, d, e, f; /* Intermediate Values */
598 Vector4 result;
600 /* Calculate intermediate values. */
601 a = (v2.x * v3.y) - (v2.y * v3.x);
602 b = (v2.x * v3.z) - (v2.z * v3.x);
603 c = (v2.x * v3.w) - (v2.w * v3.x);
604 d = (v2.y * v3.z) - (v2.z * v3.y);
605 e = (v2.y * v3.w) - (v2.w * v3.y);
606 f = (v2.z * v3.w) - (v2.w * v3.z);
608 /* Calculate the result-vector components. */
609 result.x = (v1.y * f) - (v1.z * e) + (v1.w * d);
610 result.y = - (v1.x * f) + (v1.z * c) - (v1.w * b);
611 result.z = (v1.x * e) - (v1.y * c) + (v1.w * a);
612 result.w = - (v1.x * d) + (v1.y * b) - (v1.z * a);
613 return result;
614 }
616 inline Vector4 operator +(const Vector4 &v1, const Vector4 &v2) {
617 return Vector4(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w);
618 }
620 inline Vector4 operator -(const Vector4 &v1, const Vector4 &v2) {
621 return Vector4(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w);
622 }
624 inline Vector4 operator *(const Vector4 &v1, const Vector4 &v2) {
625 return Vector4(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w);
626 }
628 inline Vector4 operator /(const Vector4 &v1, const Vector4 &v2) {
629 return Vector4(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.w / v2.w);
630 }
632 inline bool operator ==(const Vector4 &v1, const Vector4 &v2) {
633 return (fabs(v1.x - v2.x) < XSMALL_NUMBER) &&
634 (fabs(v1.y - v2.y) < XSMALL_NUMBER) &&
635 (fabs(v1.z - v2.z) < XSMALL_NUMBER) &&
636 (fabs(v1.w - v2.w) < XSMALL_NUMBER);
637 }
639 inline void operator +=(Vector4 &v1, const Vector4 &v2) {
640 v1.x += v2.x;
641 v1.y += v2.y;
642 v1.z += v2.z;
643 v1.w += v2.w;
644 }
646 inline void operator -=(Vector4 &v1, const Vector4 &v2) {
647 v1.x -= v2.x;
648 v1.y -= v2.y;
649 v1.z -= v2.z;
650 v1.w -= v2.w;
651 }
653 inline void operator *=(Vector4 &v1, const Vector4 &v2) {
654 v1.x *= v2.x;
655 v1.y *= v2.y;
656 v1.z *= v2.z;
657 v1.w *= v2.w;
658 }
660 inline void operator /=(Vector4 &v1, const Vector4 &v2) {
661 v1.x /= v2.x;
662 v1.y /= v2.y;
663 v1.z /= v2.z;
664 v1.w /= v2.w;
665 }
667 /* binary vector (op) scalar and scalar (op) vector operations */
668 inline Vector4 operator +(const Vector4 &vec, scalar_t scalar) {
669 return Vector4(vec.x + scalar, vec.y + scalar, vec.z + scalar, vec.w + scalar);
670 }
672 inline Vector4 operator +(scalar_t scalar, const Vector4 &vec) {
673 return Vector4(vec.x + scalar, vec.y + scalar, vec.z + scalar, vec.w + scalar);
674 }
676 inline Vector4 operator -(const Vector4 &vec, scalar_t scalar) {
677 return Vector4(vec.x - scalar, vec.y - scalar, vec.z - scalar, vec.w - scalar);
678 }
680 inline Vector4 operator *(const Vector4 &vec, scalar_t scalar) {
681 return Vector4(vec.x * scalar, vec.y * scalar, vec.z * scalar, vec.w * scalar);
682 }
684 inline Vector4 operator *(scalar_t scalar, const Vector4 &vec) {
685 return Vector4(vec.x * scalar, vec.y * scalar, vec.z * scalar, vec.w * scalar);
686 }
688 inline Vector4 operator /(const Vector4 &vec, scalar_t scalar) {
689 return Vector4(vec.x / scalar, vec.y / scalar, vec.z / scalar, vec.w / scalar);
690 }
692 inline void operator +=(Vector4 &vec, scalar_t scalar) {
693 vec.x += scalar;
694 vec.y += scalar;
695 vec.z += scalar;
696 vec.w += scalar;
697 }
699 inline void operator -=(Vector4 &vec, scalar_t scalar) {
700 vec.x -= scalar;
701 vec.y -= scalar;
702 vec.z -= scalar;
703 vec.w -= scalar;
704 }
706 inline void operator *=(Vector4 &vec, scalar_t scalar) {
707 vec.x *= scalar;
708 vec.y *= scalar;
709 vec.z *= scalar;
710 vec.w *= scalar;
711 }
713 inline void operator /=(Vector4 &vec, scalar_t scalar) {
714 vec.x /= scalar;
715 vec.y /= scalar;
716 vec.z /= scalar;
717 vec.w /= scalar;
718 }
720 inline scalar_t Vector4::length() const {
721 return sqrt(x*x + y*y + z*z + w*w);
722 }
723 inline scalar_t Vector4::length_sq() const {
724 return x*x + y*y + z*z + w*w;
725 }
727 inline Vector4 lerp(const Vector4 &v0, const Vector4 &v1, scalar_t t)
728 {
729 return v0 + (v1 - v0) * t;
730 }
732 inline Vector4 catmull_rom_spline(const Vector4 &v0, const Vector4 &v1,
733 const Vector4 &v2, const Vector4 &v3, scalar_t t)
734 {
735 scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t);
736 scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t);
737 scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t);
738 scalar_t z = spline(v0.z, v1.z, v2.z, v3.z, t);
739 scalar_t w = spline(v0.w, v1.w, v2.w, v3.w, t);
740 return Vector4(x, y, z, w);
741 }
743 #endif /* __cplusplus */