gpuray_glsl

annotate vmath/vector.inl @ 0:f234630e38ff

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