istereo

annotate libs/vmath/vector.inl @ 39:ff055bff6a15

copyright statements and stuff
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sun, 11 Sep 2011 09:03:18 +0300
parents fb4c9641059f
children
rev   line source
nuclear@39 1 /*
nuclear@39 2 libvmath - a vector math library
nuclear@39 3 Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
nuclear@39 4
nuclear@39 5 This program is free software: you can redistribute it and/or modify
nuclear@39 6 it under the terms of the GNU Lesser General Public License as published
nuclear@39 7 by the Free Software Foundation, either version 3 of the License, or
nuclear@39 8 (at your option) any later version.
nuclear@39 9
nuclear@39 10 This program is distributed in the hope that it will be useful,
nuclear@39 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@39 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@39 13 GNU Lesser General Public License for more details.
nuclear@39 14
nuclear@39 15 You should have received a copy of the GNU Lesser General Public License
nuclear@39 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@39 17 */
nuclear@39 18
nuclear@29 19 #include <math.h>
nuclear@29 20
nuclear@29 21 #ifdef __cplusplus
nuclear@29 22 extern "C" {
nuclear@29 23 #endif /* __cplusplus */
nuclear@29 24
nuclear@29 25 /* C 2D vector functions */
nuclear@29 26 static inline vec2_t v2_cons(scalar_t x, scalar_t y)
nuclear@29 27 {
nuclear@29 28 vec2_t v;
nuclear@29 29 v.x = x;
nuclear@29 30 v.y = y;
nuclear@29 31 return v;
nuclear@29 32 }
nuclear@29 33
nuclear@29 34 static inline void v2_print(FILE *fp, vec2_t v)
nuclear@29 35 {
nuclear@29 36 fprintf(fp, "[ %.4f %.4f ]", v.x, v.y);
nuclear@29 37 }
nuclear@29 38
nuclear@29 39 static inline vec2_t v2_add(vec2_t v1, vec2_t v2)
nuclear@29 40 {
nuclear@29 41 vec2_t res;
nuclear@29 42 res.x = v1.x + v2.x;
nuclear@29 43 res.y = v1.y + v2.y;
nuclear@29 44 return res;
nuclear@29 45 }
nuclear@29 46
nuclear@29 47 static inline vec2_t v2_sub(vec2_t v1, vec2_t v2)
nuclear@29 48 {
nuclear@29 49 vec2_t res;
nuclear@29 50 res.x = v1.x - v2.x;
nuclear@29 51 res.y = v1.y - v2.y;
nuclear@29 52 return res;
nuclear@29 53 }
nuclear@29 54
nuclear@29 55 static inline vec2_t v2_scale(vec2_t v, scalar_t s)
nuclear@29 56 {
nuclear@29 57 vec2_t res;
nuclear@29 58 res.x = v.x * s;
nuclear@29 59 res.y = v.y * s;
nuclear@29 60 return res;
nuclear@29 61 }
nuclear@29 62
nuclear@29 63 static inline scalar_t v2_dot(vec2_t v1, vec2_t v2)
nuclear@29 64 {
nuclear@29 65 return v1.x * v2.x + v1.y * v2.y;
nuclear@29 66 }
nuclear@29 67
nuclear@29 68 static inline scalar_t v2_length(vec2_t v)
nuclear@29 69 {
nuclear@29 70 return sqrt(v.x * v.x + v.y * v.y);
nuclear@29 71 }
nuclear@29 72
nuclear@29 73 static inline scalar_t v2_length_sq(vec2_t v)
nuclear@29 74 {
nuclear@29 75 return v.x * v.x + v.y * v.y;
nuclear@29 76 }
nuclear@29 77
nuclear@29 78 static inline vec2_t v2_normalize(vec2_t v)
nuclear@29 79 {
nuclear@29 80 scalar_t len = (scalar_t)sqrt(v.x * v.x + v.y * v.y);
nuclear@29 81 v.x /= len;
nuclear@29 82 v.y /= len;
nuclear@29 83 return v;
nuclear@29 84 }
nuclear@29 85
nuclear@29 86 static inline vec2_t v2_lerp(vec2_t v1, vec2_t v2, scalar_t t)
nuclear@29 87 {
nuclear@29 88 vec2_t res;
nuclear@29 89 res.x = v1.x + (v2.x - v1.x) * t;
nuclear@29 90 res.y = v1.y + (v2.y - v1.y) * t;
nuclear@29 91 return res;
nuclear@29 92 }
nuclear@29 93
nuclear@29 94
nuclear@29 95 /* C 3D vector functions */
nuclear@29 96 static inline vec3_t v3_cons(scalar_t x, scalar_t y, scalar_t z)
nuclear@29 97 {
nuclear@29 98 vec3_t v;
nuclear@29 99 v.x = x;
nuclear@29 100 v.y = y;
nuclear@29 101 v.z = z;
nuclear@29 102 return v;
nuclear@29 103 }
nuclear@29 104
nuclear@29 105 static inline void v3_print(FILE *fp, vec3_t v)
nuclear@29 106 {
nuclear@29 107 fprintf(fp, "[ %.4f %.4f %.4f ]", v.x, v.y, v.z);
nuclear@29 108 }
nuclear@29 109
nuclear@29 110 static inline vec3_t v3_add(vec3_t v1, vec3_t v2)
nuclear@29 111 {
nuclear@29 112 v1.x += v2.x;
nuclear@29 113 v1.y += v2.y;
nuclear@29 114 v1.z += v2.z;
nuclear@29 115 return v1;
nuclear@29 116 }
nuclear@29 117
nuclear@29 118 static inline vec3_t v3_sub(vec3_t v1, vec3_t v2)
nuclear@29 119 {
nuclear@29 120 v1.x -= v2.x;
nuclear@29 121 v1.y -= v2.y;
nuclear@29 122 v1.z -= v2.z;
nuclear@29 123 return v1;
nuclear@29 124 }
nuclear@29 125
nuclear@29 126 static inline vec3_t v3_neg(vec3_t v)
nuclear@29 127 {
nuclear@29 128 v.x = -v.x;
nuclear@29 129 v.y = -v.y;
nuclear@29 130 v.z = -v.z;
nuclear@29 131 return v;
nuclear@29 132 }
nuclear@29 133
nuclear@29 134 static inline vec3_t v3_mul(vec3_t v1, vec3_t v2)
nuclear@29 135 {
nuclear@29 136 v1.x *= v2.x;
nuclear@29 137 v1.y *= v2.y;
nuclear@29 138 v1.z *= v2.z;
nuclear@29 139 return v1;
nuclear@29 140 }
nuclear@29 141
nuclear@29 142 static inline vec3_t v3_scale(vec3_t v1, scalar_t s)
nuclear@29 143 {
nuclear@29 144 v1.x *= s;
nuclear@29 145 v1.y *= s;
nuclear@29 146 v1.z *= s;
nuclear@29 147 return v1;
nuclear@29 148 }
nuclear@29 149
nuclear@29 150 static inline scalar_t v3_dot(vec3_t v1, vec3_t v2)
nuclear@29 151 {
nuclear@29 152 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
nuclear@29 153 }
nuclear@29 154
nuclear@29 155 static inline vec3_t v3_cross(vec3_t v1, vec3_t v2)
nuclear@29 156 {
nuclear@29 157 vec3_t v;
nuclear@29 158 v.x = v1.y * v2.z - v1.z * v2.y;
nuclear@29 159 v.y = v1.z * v2.x - v1.x * v2.z;
nuclear@29 160 v.z = v1.x * v2.y - v1.y * v2.x;
nuclear@29 161 return v;
nuclear@29 162 }
nuclear@29 163
nuclear@29 164 static inline scalar_t v3_length(vec3_t v)
nuclear@29 165 {
nuclear@29 166 return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
nuclear@29 167 }
nuclear@29 168
nuclear@29 169 static inline scalar_t v3_length_sq(vec3_t v)
nuclear@29 170 {
nuclear@29 171 return v.x * v.x + v.y * v.y + v.z * v.z;
nuclear@29 172 }
nuclear@29 173
nuclear@29 174 static inline vec3_t v3_normalize(vec3_t v)
nuclear@29 175 {
nuclear@29 176 scalar_t len = sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
nuclear@29 177 v.x /= len;
nuclear@29 178 v.y /= len;
nuclear@29 179 v.z /= len;
nuclear@29 180 return v;
nuclear@29 181 }
nuclear@29 182
nuclear@29 183 static inline vec3_t v3_transform(vec3_t v, mat4_t m)
nuclear@29 184 {
nuclear@29 185 vec3_t res;
nuclear@29 186 res.x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3];
nuclear@29 187 res.y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3];
nuclear@29 188 res.z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3];
nuclear@29 189 return res;
nuclear@29 190 }
nuclear@29 191
nuclear@29 192 static inline vec3_t v3_rotate(vec3_t v, scalar_t x, scalar_t y, scalar_t z)
nuclear@29 193 {
nuclear@29 194 void m4_rotate(mat4_t, scalar_t, scalar_t, scalar_t);
nuclear@29 195
nuclear@29 196 mat4_t m = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
nuclear@29 197 m4_rotate(m, x, y, z);
nuclear@29 198 return v3_transform(v, m);
nuclear@29 199 }
nuclear@29 200
nuclear@29 201 static inline vec3_t v3_rotate_axis(vec3_t v, scalar_t angle, scalar_t x, scalar_t y, scalar_t z)
nuclear@29 202 {
nuclear@29 203 void m4_rotate_axis(mat4_t, scalar_t, scalar_t, scalar_t, scalar_t);
nuclear@29 204
nuclear@29 205 mat4_t m = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
nuclear@29 206 m4_rotate_axis(m, angle, x, y, z);
nuclear@29 207 return v3_transform(v, m);
nuclear@29 208 }
nuclear@29 209
nuclear@29 210 static inline vec3_t v3_rotate_quat(vec3_t v, quat_t q)
nuclear@29 211 {
nuclear@29 212 quat_t quat_rotate_quat(quat_t, quat_t);
nuclear@29 213
nuclear@29 214 quat_t vq = v4_cons(v.x, v.y, v.z, 0.0);
nuclear@29 215 quat_t res = quat_rotate_quat(vq, q);
nuclear@29 216 return v3_cons(res.x, res.y, res.z);
nuclear@29 217 }
nuclear@29 218
nuclear@29 219 static inline vec3_t v3_reflect(vec3_t v, vec3_t n)
nuclear@29 220 {
nuclear@29 221 scalar_t dot = v3_dot(v, n);
nuclear@29 222 return v3_sub(v3_scale(n, dot * 2.0), v);
nuclear@29 223 }
nuclear@29 224
nuclear@29 225 static inline vec3_t v3_lerp(vec3_t v1, vec3_t v2, scalar_t t)
nuclear@29 226 {
nuclear@29 227 v1.x += (v2.x - v1.x) * t;
nuclear@29 228 v1.y += (v2.y - v1.y) * t;
nuclear@29 229 v1.z += (v2.z - v1.z) * t;
nuclear@29 230 return v1;
nuclear@29 231 }
nuclear@29 232
nuclear@29 233 /* C 4D vector functions */
nuclear@29 234 static inline vec4_t v4_cons(scalar_t x, scalar_t y, scalar_t z, scalar_t w)
nuclear@29 235 {
nuclear@29 236 vec4_t v;
nuclear@29 237 v.x = x;
nuclear@29 238 v.y = y;
nuclear@29 239 v.z = z;
nuclear@29 240 v.w = w;
nuclear@29 241 return v;
nuclear@29 242 }
nuclear@29 243
nuclear@29 244 static inline void v4_print(FILE *fp, vec4_t v)
nuclear@29 245 {
nuclear@29 246 fprintf(fp, "[ %.4f %.4f %.4f %.4f ]", v.x, v.y, v.z, v.w);
nuclear@29 247 }
nuclear@29 248
nuclear@29 249 static inline vec4_t v4_add(vec4_t v1, vec4_t v2)
nuclear@29 250 {
nuclear@29 251 v1.x += v2.x;
nuclear@29 252 v1.y += v2.y;
nuclear@29 253 v1.z += v2.z;
nuclear@29 254 v1.w += v2.w;
nuclear@29 255 return v1;
nuclear@29 256 }
nuclear@29 257
nuclear@29 258 static inline vec4_t v4_sub(vec4_t v1, vec4_t v2)
nuclear@29 259 {
nuclear@29 260 v1.x -= v2.x;
nuclear@29 261 v1.y -= v2.y;
nuclear@29 262 v1.z -= v2.z;
nuclear@29 263 v1.w -= v2.w;
nuclear@29 264 return v1;
nuclear@29 265 }
nuclear@29 266
nuclear@29 267 static inline vec4_t v4_neg(vec4_t v)
nuclear@29 268 {
nuclear@29 269 v.x = -v.x;
nuclear@29 270 v.y = -v.y;
nuclear@29 271 v.z = -v.z;
nuclear@29 272 v.w = -v.w;
nuclear@29 273 return v;
nuclear@29 274 }
nuclear@29 275
nuclear@29 276 static inline vec4_t v4_mul(vec4_t v1, vec4_t v2)
nuclear@29 277 {
nuclear@29 278 v1.x *= v2.x;
nuclear@29 279 v1.y *= v2.y;
nuclear@29 280 v1.z *= v2.z;
nuclear@29 281 v1.w *= v2.w;
nuclear@29 282 return v1;
nuclear@29 283 }
nuclear@29 284
nuclear@29 285 static inline vec4_t v4_scale(vec4_t v, scalar_t s)
nuclear@29 286 {
nuclear@29 287 v.x *= s;
nuclear@29 288 v.y *= s;
nuclear@29 289 v.z *= s;
nuclear@29 290 v.w *= s;
nuclear@29 291 return v;
nuclear@29 292 }
nuclear@29 293
nuclear@29 294 static inline scalar_t v4_dot(vec4_t v1, vec4_t v2)
nuclear@29 295 {
nuclear@29 296 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
nuclear@29 297 }
nuclear@29 298
nuclear@29 299 static inline scalar_t v4_length(vec4_t v)
nuclear@29 300 {
nuclear@29 301 return sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
nuclear@29 302 }
nuclear@29 303
nuclear@29 304 static inline scalar_t v4_length_sq(vec4_t v)
nuclear@29 305 {
nuclear@29 306 return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
nuclear@29 307 }
nuclear@29 308
nuclear@29 309 static inline vec4_t v4_normalize(vec4_t v)
nuclear@29 310 {
nuclear@29 311 scalar_t len = sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
nuclear@29 312 v.x /= len;
nuclear@29 313 v.y /= len;
nuclear@29 314 v.z /= len;
nuclear@29 315 v.w /= len;
nuclear@29 316 return v;
nuclear@29 317 }
nuclear@29 318
nuclear@29 319 static inline vec4_t v4_transform(vec4_t v, mat4_t m)
nuclear@29 320 {
nuclear@29 321 vec4_t res;
nuclear@29 322 res.x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w;
nuclear@29 323 res.y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w;
nuclear@29 324 res.z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w;
nuclear@29 325 res.w = m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w;
nuclear@29 326 return res;
nuclear@29 327 }
nuclear@29 328
nuclear@29 329 #ifdef __cplusplus
nuclear@29 330 } /* extern "C" */
nuclear@29 331
nuclear@29 332
nuclear@29 333 /* --------------- C++ part -------------- */
nuclear@29 334
nuclear@29 335 inline scalar_t &Vector2::operator [](int elem) {
nuclear@29 336 return elem ? y : x;
nuclear@29 337 }
nuclear@29 338
nuclear@29 339 inline const scalar_t &Vector2::operator [](int elem) const {
nuclear@29 340 return elem ? y : x;
nuclear@29 341 }
nuclear@29 342
nuclear@29 343 inline Vector2 operator -(const Vector2 &vec) {
nuclear@29 344 return Vector2(-vec.x, -vec.y);
nuclear@29 345 }
nuclear@29 346
nuclear@29 347 inline scalar_t dot_product(const Vector2 &v1, const Vector2 &v2) {
nuclear@29 348 return v1.x * v2.x + v1.y * v2.y;
nuclear@29 349 }
nuclear@29 350
nuclear@29 351 inline Vector2 operator +(const Vector2 &v1, const Vector2 &v2) {
nuclear@29 352 return Vector2(v1.x + v2.x, v1.y + v2.y);
nuclear@29 353 }
nuclear@29 354
nuclear@29 355 inline Vector2 operator -(const Vector2 &v1, const Vector2 &v2) {
nuclear@29 356 return Vector2(v1.x - v2.x, v1.y - v2.y);
nuclear@29 357 }
nuclear@29 358
nuclear@29 359 inline Vector2 operator *(const Vector2 &v1, const Vector2 &v2) {
nuclear@29 360 return Vector2(v1.x * v2.x, v1.y * v2.y);
nuclear@29 361 }
nuclear@29 362
nuclear@29 363 inline Vector2 operator /(const Vector2 &v1, const Vector2 &v2) {
nuclear@29 364 return Vector2(v1.x / v2.x, v1.y / v2.y);
nuclear@29 365 }
nuclear@29 366
nuclear@29 367 inline bool operator ==(const Vector2 &v1, const Vector2 &v2) {
nuclear@29 368 return (fabs(v1.x - v2.x) < XSMALL_NUMBER) && (fabs(v1.y - v2.x) < XSMALL_NUMBER);
nuclear@29 369 }
nuclear@29 370
nuclear@29 371 inline void operator +=(Vector2 &v1, const Vector2 &v2) {
nuclear@29 372 v1.x += v2.x;
nuclear@29 373 v1.y += v2.y;
nuclear@29 374 }
nuclear@29 375
nuclear@29 376 inline void operator -=(Vector2 &v1, const Vector2 &v2) {
nuclear@29 377 v1.x -= v2.x;
nuclear@29 378 v1.y -= v2.y;
nuclear@29 379 }
nuclear@29 380
nuclear@29 381 inline void operator *=(Vector2 &v1, const Vector2 &v2) {
nuclear@29 382 v1.x *= v2.x;
nuclear@29 383 v1.y *= v2.y;
nuclear@29 384 }
nuclear@29 385
nuclear@29 386 inline void operator /=(Vector2 &v1, const Vector2 &v2) {
nuclear@29 387 v1.x /= v2.x;
nuclear@29 388 v1.y /= v2.y;
nuclear@29 389 }
nuclear@29 390
nuclear@29 391 inline Vector2 operator +(const Vector2 &vec, scalar_t scalar) {
nuclear@29 392 return Vector2(vec.x + scalar, vec.y + scalar);
nuclear@29 393 }
nuclear@29 394
nuclear@29 395 inline Vector2 operator +(scalar_t scalar, const Vector2 &vec) {
nuclear@29 396 return Vector2(vec.x + scalar, vec.y + scalar);
nuclear@29 397 }
nuclear@29 398
nuclear@29 399 inline Vector2 operator -(scalar_t scalar, const Vector2 &vec) {
nuclear@29 400 return Vector2(vec.x - scalar, vec.y - scalar);
nuclear@29 401 }
nuclear@29 402
nuclear@29 403 inline Vector2 operator *(const Vector2 &vec, scalar_t scalar) {
nuclear@29 404 return Vector2(vec.x * scalar, vec.y * scalar);
nuclear@29 405 }
nuclear@29 406
nuclear@29 407 inline Vector2 operator *(scalar_t scalar, const Vector2 &vec) {
nuclear@29 408 return Vector2(vec.x * scalar, vec.y * scalar);
nuclear@29 409 }
nuclear@29 410
nuclear@29 411 inline Vector2 operator /(const Vector2 &vec, scalar_t scalar) {
nuclear@29 412 return Vector2(vec.x / scalar, vec.y / scalar);
nuclear@29 413 }
nuclear@29 414
nuclear@29 415 inline void operator +=(Vector2 &vec, scalar_t scalar) {
nuclear@29 416 vec.x += scalar;
nuclear@29 417 vec.y += scalar;
nuclear@29 418 }
nuclear@29 419
nuclear@29 420 inline void operator -=(Vector2 &vec, scalar_t scalar) {
nuclear@29 421 vec.x -= scalar;
nuclear@29 422 vec.y -= scalar;
nuclear@29 423 }
nuclear@29 424
nuclear@29 425 inline void operator *=(Vector2 &vec, scalar_t scalar) {
nuclear@29 426 vec.x *= scalar;
nuclear@29 427 vec.y *= scalar;
nuclear@29 428 }
nuclear@29 429
nuclear@29 430 inline void operator /=(Vector2 &vec, scalar_t scalar) {
nuclear@29 431 vec.x /= scalar;
nuclear@29 432 vec.y /= scalar;
nuclear@29 433 }
nuclear@29 434
nuclear@29 435 inline scalar_t Vector2::length() const {
nuclear@29 436 return sqrt(x*x + y*y);
nuclear@29 437 }
nuclear@29 438
nuclear@29 439 inline scalar_t Vector2::length_sq() const {
nuclear@29 440 return x*x + y*y;
nuclear@29 441 }
nuclear@29 442
nuclear@29 443 inline Vector2 lerp(const Vector2 &a, const Vector2 &b, scalar_t t)
nuclear@29 444 {
nuclear@29 445 return a + (b - a) * t;
nuclear@29 446 }
nuclear@29 447
nuclear@29 448 inline Vector2 catmull_rom_spline(const Vector2 &v0, const Vector2 &v1,
nuclear@29 449 const Vector2 &v2, const Vector2 &v3, scalar_t t)
nuclear@29 450 {
nuclear@29 451 scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t);
nuclear@29 452 scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t);
nuclear@29 453 scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t);
nuclear@29 454 return Vector2(x, y);
nuclear@29 455 }
nuclear@29 456
nuclear@29 457
nuclear@29 458 /* ------------- Vector3 -------------- */
nuclear@29 459
nuclear@29 460 inline scalar_t &Vector3::operator [](int elem) {
nuclear@29 461 return elem ? (elem == 1 ? y : z) : x;
nuclear@29 462 }
nuclear@29 463
nuclear@29 464 inline const scalar_t &Vector3::operator [](int elem) const {
nuclear@29 465 return elem ? (elem == 1 ? y : z) : x;
nuclear@29 466 }
nuclear@29 467
nuclear@29 468 /* unary operations */
nuclear@29 469 inline Vector3 operator -(const Vector3 &vec) {
nuclear@29 470 return Vector3(-vec.x, -vec.y, -vec.z);
nuclear@29 471 }
nuclear@29 472
nuclear@29 473 /* binary vector (op) vector operations */
nuclear@29 474 inline scalar_t dot_product(const Vector3 &v1, const Vector3 &v2) {
nuclear@29 475 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
nuclear@29 476 }
nuclear@29 477
nuclear@29 478 inline Vector3 cross_product(const Vector3 &v1, const Vector3 &v2) {
nuclear@29 479 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);
nuclear@29 480 }
nuclear@29 481
nuclear@29 482
nuclear@29 483 inline Vector3 operator +(const Vector3 &v1, const Vector3 &v2) {
nuclear@29 484 return Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
nuclear@29 485 }
nuclear@29 486
nuclear@29 487 inline Vector3 operator -(const Vector3 &v1, const Vector3 &v2) {
nuclear@29 488 return Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
nuclear@29 489 }
nuclear@29 490
nuclear@29 491 inline Vector3 operator *(const Vector3 &v1, const Vector3 &v2) {
nuclear@29 492 return Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
nuclear@29 493 }
nuclear@29 494
nuclear@29 495 inline Vector3 operator /(const Vector3 &v1, const Vector3 &v2) {
nuclear@29 496 return Vector3(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z);
nuclear@29 497 }
nuclear@29 498
nuclear@29 499 inline bool operator ==(const Vector3 &v1, const Vector3 &v2) {
nuclear@29 500 return (fabs(v1.x - v2.x) < XSMALL_NUMBER) && (fabs(v1.y - v2.y) < XSMALL_NUMBER) && (fabs(v1.z - v2.z) < XSMALL_NUMBER);
nuclear@29 501 }
nuclear@29 502
nuclear@29 503 inline void operator +=(Vector3 &v1, const Vector3 &v2) {
nuclear@29 504 v1.x += v2.x;
nuclear@29 505 v1.y += v2.y;
nuclear@29 506 v1.z += v2.z;
nuclear@29 507 }
nuclear@29 508
nuclear@29 509 inline void operator -=(Vector3 &v1, const Vector3 &v2) {
nuclear@29 510 v1.x -= v2.x;
nuclear@29 511 v1.y -= v2.y;
nuclear@29 512 v1.z -= v2.z;
nuclear@29 513 }
nuclear@29 514
nuclear@29 515 inline void operator *=(Vector3 &v1, const Vector3 &v2) {
nuclear@29 516 v1.x *= v2.x;
nuclear@29 517 v1.y *= v2.y;
nuclear@29 518 v1.z *= v2.z;
nuclear@29 519 }
nuclear@29 520
nuclear@29 521 inline void operator /=(Vector3 &v1, const Vector3 &v2) {
nuclear@29 522 v1.x /= v2.x;
nuclear@29 523 v1.y /= v2.y;
nuclear@29 524 v1.z /= v2.z;
nuclear@29 525 }
nuclear@29 526 /* binary vector (op) scalar and scalar (op) vector operations */
nuclear@29 527 inline Vector3 operator +(const Vector3 &vec, scalar_t scalar) {
nuclear@29 528 return Vector3(vec.x + scalar, vec.y + scalar, vec.z + scalar);
nuclear@29 529 }
nuclear@29 530
nuclear@29 531 inline Vector3 operator +(scalar_t scalar, const Vector3 &vec) {
nuclear@29 532 return Vector3(vec.x + scalar, vec.y + scalar, vec.z + scalar);
nuclear@29 533 }
nuclear@29 534
nuclear@29 535 inline Vector3 operator -(const Vector3 &vec, scalar_t scalar) {
nuclear@29 536 return Vector3(vec.x - scalar, vec.y - scalar, vec.z - scalar);
nuclear@29 537 }
nuclear@29 538
nuclear@29 539 inline Vector3 operator *(const Vector3 &vec, scalar_t scalar) {
nuclear@29 540 return Vector3(vec.x * scalar, vec.y * scalar, vec.z * scalar);
nuclear@29 541 }
nuclear@29 542
nuclear@29 543 inline Vector3 operator *(scalar_t scalar, const Vector3 &vec) {
nuclear@29 544 return Vector3(vec.x * scalar, vec.y * scalar, vec.z * scalar);
nuclear@29 545 }
nuclear@29 546
nuclear@29 547 inline Vector3 operator /(const Vector3 &vec, scalar_t scalar) {
nuclear@29 548 return Vector3(vec.x / scalar, vec.y / scalar, vec.z / scalar);
nuclear@29 549 }
nuclear@29 550
nuclear@29 551 inline void operator +=(Vector3 &vec, scalar_t scalar) {
nuclear@29 552 vec.x += scalar;
nuclear@29 553 vec.y += scalar;
nuclear@29 554 vec.z += scalar;
nuclear@29 555 }
nuclear@29 556
nuclear@29 557 inline void operator -=(Vector3 &vec, scalar_t scalar) {
nuclear@29 558 vec.x -= scalar;
nuclear@29 559 vec.y -= scalar;
nuclear@29 560 vec.z -= scalar;
nuclear@29 561 }
nuclear@29 562
nuclear@29 563 inline void operator *=(Vector3 &vec, scalar_t scalar) {
nuclear@29 564 vec.x *= scalar;
nuclear@29 565 vec.y *= scalar;
nuclear@29 566 vec.z *= scalar;
nuclear@29 567 }
nuclear@29 568
nuclear@29 569 inline void operator /=(Vector3 &vec, scalar_t scalar) {
nuclear@29 570 vec.x /= scalar;
nuclear@29 571 vec.y /= scalar;
nuclear@29 572 vec.z /= scalar;
nuclear@29 573 }
nuclear@29 574
nuclear@29 575 inline scalar_t Vector3::length() const {
nuclear@29 576 return sqrt(x*x + y*y + z*z);
nuclear@29 577 }
nuclear@29 578 inline scalar_t Vector3::length_sq() const {
nuclear@29 579 return x*x + y*y + z*z;
nuclear@29 580 }
nuclear@29 581
nuclear@29 582 inline Vector3 lerp(const Vector3 &a, const Vector3 &b, scalar_t t) {
nuclear@29 583 return a + (b - a) * t;
nuclear@29 584 }
nuclear@29 585
nuclear@29 586 inline Vector3 catmull_rom_spline(const Vector3 &v0, const Vector3 &v1,
nuclear@29 587 const Vector3 &v2, const Vector3 &v3, scalar_t t)
nuclear@29 588 {
nuclear@29 589 scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t);
nuclear@29 590 scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t);
nuclear@29 591 scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t);
nuclear@29 592 scalar_t z = spline(v0.z, v1.z, v2.z, v3.z, t);
nuclear@29 593 return Vector3(x, y, z);
nuclear@29 594 }
nuclear@29 595
nuclear@29 596 /* ----------- Vector4 ----------------- */
nuclear@29 597
nuclear@29 598 inline scalar_t &Vector4::operator [](int elem) {
nuclear@29 599 return elem ? (elem == 1 ? y : (elem == 2 ? z : w)) : x;
nuclear@29 600 }
nuclear@29 601
nuclear@29 602 inline const scalar_t &Vector4::operator [](int elem) const {
nuclear@29 603 return elem ? (elem == 1 ? y : (elem == 2 ? z : w)) : x;
nuclear@29 604 }
nuclear@29 605
nuclear@29 606 inline Vector4 operator -(const Vector4 &vec) {
nuclear@29 607 return Vector4(-vec.x, -vec.y, -vec.z, -vec.w);
nuclear@29 608 }
nuclear@29 609
nuclear@29 610 inline scalar_t dot_product(const Vector4 &v1, const Vector4 &v2) {
nuclear@29 611 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
nuclear@29 612 }
nuclear@29 613
nuclear@29 614 inline Vector4 cross_product(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3) {
nuclear@29 615 scalar_t a, b, c, d, e, f; /* Intermediate Values */
nuclear@29 616 Vector4 result;
nuclear@29 617
nuclear@29 618 /* Calculate intermediate values. */
nuclear@29 619 a = (v2.x * v3.y) - (v2.y * v3.x);
nuclear@29 620 b = (v2.x * v3.z) - (v2.z * v3.x);
nuclear@29 621 c = (v2.x * v3.w) - (v2.w * v3.x);
nuclear@29 622 d = (v2.y * v3.z) - (v2.z * v3.y);
nuclear@29 623 e = (v2.y * v3.w) - (v2.w * v3.y);
nuclear@29 624 f = (v2.z * v3.w) - (v2.w * v3.z);
nuclear@29 625
nuclear@29 626 /* Calculate the result-vector components. */
nuclear@29 627 result.x = (v1.y * f) - (v1.z * e) + (v1.w * d);
nuclear@29 628 result.y = - (v1.x * f) + (v1.z * c) - (v1.w * b);
nuclear@29 629 result.z = (v1.x * e) - (v1.y * c) + (v1.w * a);
nuclear@29 630 result.w = - (v1.x * d) + (v1.y * b) - (v1.z * a);
nuclear@29 631 return result;
nuclear@29 632 }
nuclear@29 633
nuclear@29 634 inline Vector4 operator +(const Vector4 &v1, const Vector4 &v2) {
nuclear@29 635 return Vector4(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w);
nuclear@29 636 }
nuclear@29 637
nuclear@29 638 inline Vector4 operator -(const Vector4 &v1, const Vector4 &v2) {
nuclear@29 639 return Vector4(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w);
nuclear@29 640 }
nuclear@29 641
nuclear@29 642 inline Vector4 operator *(const Vector4 &v1, const Vector4 &v2) {
nuclear@29 643 return Vector4(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w);
nuclear@29 644 }
nuclear@29 645
nuclear@29 646 inline Vector4 operator /(const Vector4 &v1, const Vector4 &v2) {
nuclear@29 647 return Vector4(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.w / v2.w);
nuclear@29 648 }
nuclear@29 649
nuclear@29 650 inline bool operator ==(const Vector4 &v1, const Vector4 &v2) {
nuclear@29 651 return (fabs(v1.x - v2.x) < XSMALL_NUMBER) &&
nuclear@29 652 (fabs(v1.y - v2.y) < XSMALL_NUMBER) &&
nuclear@29 653 (fabs(v1.z - v2.z) < XSMALL_NUMBER) &&
nuclear@29 654 (fabs(v1.w - v2.w) < XSMALL_NUMBER);
nuclear@29 655 }
nuclear@29 656
nuclear@29 657 inline void operator +=(Vector4 &v1, const Vector4 &v2) {
nuclear@29 658 v1.x += v2.x;
nuclear@29 659 v1.y += v2.y;
nuclear@29 660 v1.z += v2.z;
nuclear@29 661 v1.w += v2.w;
nuclear@29 662 }
nuclear@29 663
nuclear@29 664 inline void operator -=(Vector4 &v1, const Vector4 &v2) {
nuclear@29 665 v1.x -= v2.x;
nuclear@29 666 v1.y -= v2.y;
nuclear@29 667 v1.z -= v2.z;
nuclear@29 668 v1.w -= v2.w;
nuclear@29 669 }
nuclear@29 670
nuclear@29 671 inline void operator *=(Vector4 &v1, const Vector4 &v2) {
nuclear@29 672 v1.x *= v2.x;
nuclear@29 673 v1.y *= v2.y;
nuclear@29 674 v1.z *= v2.z;
nuclear@29 675 v1.w *= v2.w;
nuclear@29 676 }
nuclear@29 677
nuclear@29 678 inline void operator /=(Vector4 &v1, const Vector4 &v2) {
nuclear@29 679 v1.x /= v2.x;
nuclear@29 680 v1.y /= v2.y;
nuclear@29 681 v1.z /= v2.z;
nuclear@29 682 v1.w /= v2.w;
nuclear@29 683 }
nuclear@29 684
nuclear@29 685 /* binary vector (op) scalar and scalar (op) vector operations */
nuclear@29 686 inline Vector4 operator +(const Vector4 &vec, scalar_t scalar) {
nuclear@29 687 return Vector4(vec.x + scalar, vec.y + scalar, vec.z + scalar, vec.w + scalar);
nuclear@29 688 }
nuclear@29 689
nuclear@29 690 inline Vector4 operator +(scalar_t scalar, const Vector4 &vec) {
nuclear@29 691 return Vector4(vec.x + scalar, vec.y + scalar, vec.z + scalar, vec.w + scalar);
nuclear@29 692 }
nuclear@29 693
nuclear@29 694 inline Vector4 operator -(const Vector4 &vec, scalar_t scalar) {
nuclear@29 695 return Vector4(vec.x - scalar, vec.y - scalar, vec.z - scalar, vec.w - scalar);
nuclear@29 696 }
nuclear@29 697
nuclear@29 698 inline Vector4 operator *(const Vector4 &vec, scalar_t scalar) {
nuclear@29 699 return Vector4(vec.x * scalar, vec.y * scalar, vec.z * scalar, vec.w * scalar);
nuclear@29 700 }
nuclear@29 701
nuclear@29 702 inline Vector4 operator *(scalar_t scalar, const Vector4 &vec) {
nuclear@29 703 return Vector4(vec.x * scalar, vec.y * scalar, vec.z * scalar, vec.w * scalar);
nuclear@29 704 }
nuclear@29 705
nuclear@29 706 inline Vector4 operator /(const Vector4 &vec, scalar_t scalar) {
nuclear@29 707 return Vector4(vec.x / scalar, vec.y / scalar, vec.z / scalar, vec.w / scalar);
nuclear@29 708 }
nuclear@29 709
nuclear@29 710 inline void operator +=(Vector4 &vec, scalar_t scalar) {
nuclear@29 711 vec.x += scalar;
nuclear@29 712 vec.y += scalar;
nuclear@29 713 vec.z += scalar;
nuclear@29 714 vec.w += scalar;
nuclear@29 715 }
nuclear@29 716
nuclear@29 717 inline void operator -=(Vector4 &vec, scalar_t scalar) {
nuclear@29 718 vec.x -= scalar;
nuclear@29 719 vec.y -= scalar;
nuclear@29 720 vec.z -= scalar;
nuclear@29 721 vec.w -= scalar;
nuclear@29 722 }
nuclear@29 723
nuclear@29 724 inline void operator *=(Vector4 &vec, scalar_t scalar) {
nuclear@29 725 vec.x *= scalar;
nuclear@29 726 vec.y *= scalar;
nuclear@29 727 vec.z *= scalar;
nuclear@29 728 vec.w *= scalar;
nuclear@29 729 }
nuclear@29 730
nuclear@29 731 inline void operator /=(Vector4 &vec, scalar_t scalar) {
nuclear@29 732 vec.x /= scalar;
nuclear@29 733 vec.y /= scalar;
nuclear@29 734 vec.z /= scalar;
nuclear@29 735 vec.w /= scalar;
nuclear@29 736 }
nuclear@29 737
nuclear@29 738 inline scalar_t Vector4::length() const {
nuclear@29 739 return sqrt(x*x + y*y + z*z + w*w);
nuclear@29 740 }
nuclear@29 741 inline scalar_t Vector4::length_sq() const {
nuclear@29 742 return x*x + y*y + z*z + w*w;
nuclear@29 743 }
nuclear@29 744
nuclear@29 745 inline Vector4 lerp(const Vector4 &v0, const Vector4 &v1, scalar_t t)
nuclear@29 746 {
nuclear@29 747 return v0 + (v1 - v0) * t;
nuclear@29 748 }
nuclear@29 749
nuclear@29 750 inline Vector4 catmull_rom_spline(const Vector4 &v0, const Vector4 &v1,
nuclear@29 751 const Vector4 &v2, const Vector4 &v3, scalar_t t)
nuclear@29 752 {
nuclear@29 753 scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t);
nuclear@29 754 scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t);
nuclear@29 755 scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t);
nuclear@29 756 scalar_t z = spline(v0.z, v1.z, v2.z, v3.z, t);
nuclear@29 757 scalar_t w = spline(v0.w, v1.w, v2.w, v3.w, t);
nuclear@29 758 return Vector4(x, y, z, w);
nuclear@29 759 }
nuclear@29 760
nuclear@29 761 #endif /* __cplusplus */