3dphotoshoot

annotate libs/vmath/vector.inl @ 27:3d082c566b53

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