istereo

annotate libs/vmath/vector.inl @ 29:fb4c9641059f

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