goat3d

annotate libs/vmath/vector.inl @ 43:1dcbe87b6a5d

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