istereo
diff 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 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/vmath/vector.inl Thu Sep 08 08:31:15 2011 +0300 1.3 @@ -0,0 +1,743 @@ 1.4 +#include <math.h> 1.5 + 1.6 +#ifdef __cplusplus 1.7 +extern "C" { 1.8 +#endif /* __cplusplus */ 1.9 + 1.10 +/* C 2D vector functions */ 1.11 +static inline vec2_t v2_cons(scalar_t x, scalar_t y) 1.12 +{ 1.13 + vec2_t v; 1.14 + v.x = x; 1.15 + v.y = y; 1.16 + return v; 1.17 +} 1.18 + 1.19 +static inline void v2_print(FILE *fp, vec2_t v) 1.20 +{ 1.21 + fprintf(fp, "[ %.4f %.4f ]", v.x, v.y); 1.22 +} 1.23 + 1.24 +static inline vec2_t v2_add(vec2_t v1, vec2_t v2) 1.25 +{ 1.26 + vec2_t res; 1.27 + res.x = v1.x + v2.x; 1.28 + res.y = v1.y + v2.y; 1.29 + return res; 1.30 +} 1.31 + 1.32 +static inline vec2_t v2_sub(vec2_t v1, vec2_t v2) 1.33 +{ 1.34 + vec2_t res; 1.35 + res.x = v1.x - v2.x; 1.36 + res.y = v1.y - v2.y; 1.37 + return res; 1.38 +} 1.39 + 1.40 +static inline vec2_t v2_scale(vec2_t v, scalar_t s) 1.41 +{ 1.42 + vec2_t res; 1.43 + res.x = v.x * s; 1.44 + res.y = v.y * s; 1.45 + return res; 1.46 +} 1.47 + 1.48 +static inline scalar_t v2_dot(vec2_t v1, vec2_t v2) 1.49 +{ 1.50 + return v1.x * v2.x + v1.y * v2.y; 1.51 +} 1.52 + 1.53 +static inline scalar_t v2_length(vec2_t v) 1.54 +{ 1.55 + return sqrt(v.x * v.x + v.y * v.y); 1.56 +} 1.57 + 1.58 +static inline scalar_t v2_length_sq(vec2_t v) 1.59 +{ 1.60 + return v.x * v.x + v.y * v.y; 1.61 +} 1.62 + 1.63 +static inline vec2_t v2_normalize(vec2_t v) 1.64 +{ 1.65 + scalar_t len = (scalar_t)sqrt(v.x * v.x + v.y * v.y); 1.66 + v.x /= len; 1.67 + v.y /= len; 1.68 + return v; 1.69 +} 1.70 + 1.71 +static inline vec2_t v2_lerp(vec2_t v1, vec2_t v2, scalar_t t) 1.72 +{ 1.73 + vec2_t res; 1.74 + res.x = v1.x + (v2.x - v1.x) * t; 1.75 + res.y = v1.y + (v2.y - v1.y) * t; 1.76 + return res; 1.77 +} 1.78 + 1.79 + 1.80 +/* C 3D vector functions */ 1.81 +static inline vec3_t v3_cons(scalar_t x, scalar_t y, scalar_t z) 1.82 +{ 1.83 + vec3_t v; 1.84 + v.x = x; 1.85 + v.y = y; 1.86 + v.z = z; 1.87 + return v; 1.88 +} 1.89 + 1.90 +static inline void v3_print(FILE *fp, vec3_t v) 1.91 +{ 1.92 + fprintf(fp, "[ %.4f %.4f %.4f ]", v.x, v.y, v.z); 1.93 +} 1.94 + 1.95 +static inline vec3_t v3_add(vec3_t v1, vec3_t v2) 1.96 +{ 1.97 + v1.x += v2.x; 1.98 + v1.y += v2.y; 1.99 + v1.z += v2.z; 1.100 + return v1; 1.101 +} 1.102 + 1.103 +static inline vec3_t v3_sub(vec3_t v1, vec3_t v2) 1.104 +{ 1.105 + v1.x -= v2.x; 1.106 + v1.y -= v2.y; 1.107 + v1.z -= v2.z; 1.108 + return v1; 1.109 +} 1.110 + 1.111 +static inline vec3_t v3_neg(vec3_t v) 1.112 +{ 1.113 + v.x = -v.x; 1.114 + v.y = -v.y; 1.115 + v.z = -v.z; 1.116 + return v; 1.117 +} 1.118 + 1.119 +static inline vec3_t v3_mul(vec3_t v1, vec3_t v2) 1.120 +{ 1.121 + v1.x *= v2.x; 1.122 + v1.y *= v2.y; 1.123 + v1.z *= v2.z; 1.124 + return v1; 1.125 +} 1.126 + 1.127 +static inline vec3_t v3_scale(vec3_t v1, scalar_t s) 1.128 +{ 1.129 + v1.x *= s; 1.130 + v1.y *= s; 1.131 + v1.z *= s; 1.132 + return v1; 1.133 +} 1.134 + 1.135 +static inline scalar_t v3_dot(vec3_t v1, vec3_t v2) 1.136 +{ 1.137 + return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; 1.138 +} 1.139 + 1.140 +static inline vec3_t v3_cross(vec3_t v1, vec3_t v2) 1.141 +{ 1.142 + vec3_t v; 1.143 + v.x = v1.y * v2.z - v1.z * v2.y; 1.144 + v.y = v1.z * v2.x - v1.x * v2.z; 1.145 + v.z = v1.x * v2.y - v1.y * v2.x; 1.146 + return v; 1.147 +} 1.148 + 1.149 +static inline scalar_t v3_length(vec3_t v) 1.150 +{ 1.151 + return sqrt(v.x * v.x + v.y * v.y + v.z * v.z); 1.152 +} 1.153 + 1.154 +static inline scalar_t v3_length_sq(vec3_t v) 1.155 +{ 1.156 + return v.x * v.x + v.y * v.y + v.z * v.z; 1.157 +} 1.158 + 1.159 +static inline vec3_t v3_normalize(vec3_t v) 1.160 +{ 1.161 + scalar_t len = sqrt(v.x * v.x + v.y * v.y + v.z * v.z); 1.162 + v.x /= len; 1.163 + v.y /= len; 1.164 + v.z /= len; 1.165 + return v; 1.166 +} 1.167 + 1.168 +static inline vec3_t v3_transform(vec3_t v, mat4_t m) 1.169 +{ 1.170 + vec3_t res; 1.171 + res.x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3]; 1.172 + res.y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3]; 1.173 + res.z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3]; 1.174 + return res; 1.175 +} 1.176 + 1.177 +static inline vec3_t v3_rotate(vec3_t v, scalar_t x, scalar_t y, scalar_t z) 1.178 +{ 1.179 + void m4_rotate(mat4_t, scalar_t, scalar_t, scalar_t); 1.180 + 1.181 + mat4_t m = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; 1.182 + m4_rotate(m, x, y, z); 1.183 + return v3_transform(v, m); 1.184 +} 1.185 + 1.186 +static inline vec3_t v3_rotate_axis(vec3_t v, scalar_t angle, scalar_t x, scalar_t y, scalar_t z) 1.187 +{ 1.188 + void m4_rotate_axis(mat4_t, scalar_t, scalar_t, scalar_t, scalar_t); 1.189 + 1.190 + mat4_t m = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; 1.191 + m4_rotate_axis(m, angle, x, y, z); 1.192 + return v3_transform(v, m); 1.193 +} 1.194 + 1.195 +static inline vec3_t v3_rotate_quat(vec3_t v, quat_t q) 1.196 +{ 1.197 + quat_t quat_rotate_quat(quat_t, quat_t); 1.198 + 1.199 + quat_t vq = v4_cons(v.x, v.y, v.z, 0.0); 1.200 + quat_t res = quat_rotate_quat(vq, q); 1.201 + return v3_cons(res.x, res.y, res.z); 1.202 +} 1.203 + 1.204 +static inline vec3_t v3_reflect(vec3_t v, vec3_t n) 1.205 +{ 1.206 + scalar_t dot = v3_dot(v, n); 1.207 + return v3_sub(v3_scale(n, dot * 2.0), v); 1.208 +} 1.209 + 1.210 +static inline vec3_t v3_lerp(vec3_t v1, vec3_t v2, scalar_t t) 1.211 +{ 1.212 + v1.x += (v2.x - v1.x) * t; 1.213 + v1.y += (v2.y - v1.y) * t; 1.214 + v1.z += (v2.z - v1.z) * t; 1.215 + return v1; 1.216 +} 1.217 + 1.218 +/* C 4D vector functions */ 1.219 +static inline vec4_t v4_cons(scalar_t x, scalar_t y, scalar_t z, scalar_t w) 1.220 +{ 1.221 + vec4_t v; 1.222 + v.x = x; 1.223 + v.y = y; 1.224 + v.z = z; 1.225 + v.w = w; 1.226 + return v; 1.227 +} 1.228 + 1.229 +static inline void v4_print(FILE *fp, vec4_t v) 1.230 +{ 1.231 + fprintf(fp, "[ %.4f %.4f %.4f %.4f ]", v.x, v.y, v.z, v.w); 1.232 +} 1.233 + 1.234 +static inline vec4_t v4_add(vec4_t v1, vec4_t v2) 1.235 +{ 1.236 + v1.x += v2.x; 1.237 + v1.y += v2.y; 1.238 + v1.z += v2.z; 1.239 + v1.w += v2.w; 1.240 + return v1; 1.241 +} 1.242 + 1.243 +static inline vec4_t v4_sub(vec4_t v1, vec4_t v2) 1.244 +{ 1.245 + v1.x -= v2.x; 1.246 + v1.y -= v2.y; 1.247 + v1.z -= v2.z; 1.248 + v1.w -= v2.w; 1.249 + return v1; 1.250 +} 1.251 + 1.252 +static inline vec4_t v4_neg(vec4_t v) 1.253 +{ 1.254 + v.x = -v.x; 1.255 + v.y = -v.y; 1.256 + v.z = -v.z; 1.257 + v.w = -v.w; 1.258 + return v; 1.259 +} 1.260 + 1.261 +static inline vec4_t v4_mul(vec4_t v1, vec4_t v2) 1.262 +{ 1.263 + v1.x *= v2.x; 1.264 + v1.y *= v2.y; 1.265 + v1.z *= v2.z; 1.266 + v1.w *= v2.w; 1.267 + return v1; 1.268 +} 1.269 + 1.270 +static inline vec4_t v4_scale(vec4_t v, scalar_t s) 1.271 +{ 1.272 + v.x *= s; 1.273 + v.y *= s; 1.274 + v.z *= s; 1.275 + v.w *= s; 1.276 + return v; 1.277 +} 1.278 + 1.279 +static inline scalar_t v4_dot(vec4_t v1, vec4_t v2) 1.280 +{ 1.281 + return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w; 1.282 +} 1.283 + 1.284 +static inline scalar_t v4_length(vec4_t v) 1.285 +{ 1.286 + return sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); 1.287 +} 1.288 + 1.289 +static inline scalar_t v4_length_sq(vec4_t v) 1.290 +{ 1.291 + return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w; 1.292 +} 1.293 + 1.294 +static inline vec4_t v4_normalize(vec4_t v) 1.295 +{ 1.296 + scalar_t len = sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); 1.297 + v.x /= len; 1.298 + v.y /= len; 1.299 + v.z /= len; 1.300 + v.w /= len; 1.301 + return v; 1.302 +} 1.303 + 1.304 +static inline vec4_t v4_transform(vec4_t v, mat4_t m) 1.305 +{ 1.306 + vec4_t res; 1.307 + res.x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w; 1.308 + res.y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w; 1.309 + res.z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w; 1.310 + res.w = m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w; 1.311 + return res; 1.312 +} 1.313 + 1.314 +#ifdef __cplusplus 1.315 +} /* extern "C" */ 1.316 + 1.317 + 1.318 +/* --------------- C++ part -------------- */ 1.319 + 1.320 +inline scalar_t &Vector2::operator [](int elem) { 1.321 + return elem ? y : x; 1.322 +} 1.323 + 1.324 +inline const scalar_t &Vector2::operator [](int elem) const { 1.325 + return elem ? y : x; 1.326 +} 1.327 + 1.328 +inline Vector2 operator -(const Vector2 &vec) { 1.329 + return Vector2(-vec.x, -vec.y); 1.330 +} 1.331 + 1.332 +inline scalar_t dot_product(const Vector2 &v1, const Vector2 &v2) { 1.333 + return v1.x * v2.x + v1.y * v2.y; 1.334 +} 1.335 + 1.336 +inline Vector2 operator +(const Vector2 &v1, const Vector2 &v2) { 1.337 + return Vector2(v1.x + v2.x, v1.y + v2.y); 1.338 +} 1.339 + 1.340 +inline Vector2 operator -(const Vector2 &v1, const Vector2 &v2) { 1.341 + return Vector2(v1.x - v2.x, v1.y - v2.y); 1.342 +} 1.343 + 1.344 +inline Vector2 operator *(const Vector2 &v1, const Vector2 &v2) { 1.345 + return Vector2(v1.x * v2.x, v1.y * v2.y); 1.346 +} 1.347 + 1.348 +inline Vector2 operator /(const Vector2 &v1, const Vector2 &v2) { 1.349 + return Vector2(v1.x / v2.x, v1.y / v2.y); 1.350 +} 1.351 + 1.352 +inline bool operator ==(const Vector2 &v1, const Vector2 &v2) { 1.353 + return (fabs(v1.x - v2.x) < XSMALL_NUMBER) && (fabs(v1.y - v2.x) < XSMALL_NUMBER); 1.354 +} 1.355 + 1.356 +inline void operator +=(Vector2 &v1, const Vector2 &v2) { 1.357 + v1.x += v2.x; 1.358 + v1.y += v2.y; 1.359 +} 1.360 + 1.361 +inline void operator -=(Vector2 &v1, const Vector2 &v2) { 1.362 + v1.x -= v2.x; 1.363 + v1.y -= v2.y; 1.364 +} 1.365 + 1.366 +inline void operator *=(Vector2 &v1, const Vector2 &v2) { 1.367 + v1.x *= v2.x; 1.368 + v1.y *= v2.y; 1.369 +} 1.370 + 1.371 +inline void operator /=(Vector2 &v1, const Vector2 &v2) { 1.372 + v1.x /= v2.x; 1.373 + v1.y /= v2.y; 1.374 +} 1.375 + 1.376 +inline Vector2 operator +(const Vector2 &vec, scalar_t scalar) { 1.377 + return Vector2(vec.x + scalar, vec.y + scalar); 1.378 +} 1.379 + 1.380 +inline Vector2 operator +(scalar_t scalar, const Vector2 &vec) { 1.381 + return Vector2(vec.x + scalar, vec.y + scalar); 1.382 +} 1.383 + 1.384 +inline Vector2 operator -(scalar_t scalar, const Vector2 &vec) { 1.385 + return Vector2(vec.x - scalar, vec.y - scalar); 1.386 +} 1.387 + 1.388 +inline Vector2 operator *(const Vector2 &vec, scalar_t scalar) { 1.389 + return Vector2(vec.x * scalar, vec.y * scalar); 1.390 +} 1.391 + 1.392 +inline Vector2 operator *(scalar_t scalar, const Vector2 &vec) { 1.393 + return Vector2(vec.x * scalar, vec.y * scalar); 1.394 +} 1.395 + 1.396 +inline Vector2 operator /(const Vector2 &vec, scalar_t scalar) { 1.397 + return Vector2(vec.x / scalar, vec.y / scalar); 1.398 +} 1.399 + 1.400 +inline void operator +=(Vector2 &vec, scalar_t scalar) { 1.401 + vec.x += scalar; 1.402 + vec.y += scalar; 1.403 +} 1.404 + 1.405 +inline void operator -=(Vector2 &vec, scalar_t scalar) { 1.406 + vec.x -= scalar; 1.407 + vec.y -= scalar; 1.408 +} 1.409 + 1.410 +inline void operator *=(Vector2 &vec, scalar_t scalar) { 1.411 + vec.x *= scalar; 1.412 + vec.y *= scalar; 1.413 +} 1.414 + 1.415 +inline void operator /=(Vector2 &vec, scalar_t scalar) { 1.416 + vec.x /= scalar; 1.417 + vec.y /= scalar; 1.418 +} 1.419 + 1.420 +inline scalar_t Vector2::length() const { 1.421 + return sqrt(x*x + y*y); 1.422 +} 1.423 + 1.424 +inline scalar_t Vector2::length_sq() const { 1.425 + return x*x + y*y; 1.426 +} 1.427 + 1.428 +inline Vector2 lerp(const Vector2 &a, const Vector2 &b, scalar_t t) 1.429 +{ 1.430 + return a + (b - a) * t; 1.431 +} 1.432 + 1.433 +inline Vector2 catmull_rom_spline(const Vector2 &v0, const Vector2 &v1, 1.434 + const Vector2 &v2, const Vector2 &v3, scalar_t t) 1.435 +{ 1.436 + scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t); 1.437 + scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t); 1.438 + scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t); 1.439 + return Vector2(x, y); 1.440 +} 1.441 + 1.442 + 1.443 +/* ------------- Vector3 -------------- */ 1.444 + 1.445 +inline scalar_t &Vector3::operator [](int elem) { 1.446 + return elem ? (elem == 1 ? y : z) : x; 1.447 +} 1.448 + 1.449 +inline const scalar_t &Vector3::operator [](int elem) const { 1.450 + return elem ? (elem == 1 ? y : z) : x; 1.451 +} 1.452 + 1.453 +/* unary operations */ 1.454 +inline Vector3 operator -(const Vector3 &vec) { 1.455 + return Vector3(-vec.x, -vec.y, -vec.z); 1.456 +} 1.457 + 1.458 +/* binary vector (op) vector operations */ 1.459 +inline scalar_t dot_product(const Vector3 &v1, const Vector3 &v2) { 1.460 + return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; 1.461 +} 1.462 + 1.463 +inline Vector3 cross_product(const Vector3 &v1, const Vector3 &v2) { 1.464 + 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); 1.465 +} 1.466 + 1.467 + 1.468 +inline Vector3 operator +(const Vector3 &v1, const Vector3 &v2) { 1.469 + return Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z); 1.470 +} 1.471 + 1.472 +inline Vector3 operator -(const Vector3 &v1, const Vector3 &v2) { 1.473 + return Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z); 1.474 +} 1.475 + 1.476 +inline Vector3 operator *(const Vector3 &v1, const Vector3 &v2) { 1.477 + return Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z); 1.478 +} 1.479 + 1.480 +inline Vector3 operator /(const Vector3 &v1, const Vector3 &v2) { 1.481 + return Vector3(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z); 1.482 +} 1.483 + 1.484 +inline bool operator ==(const Vector3 &v1, const Vector3 &v2) { 1.485 + return (fabs(v1.x - v2.x) < XSMALL_NUMBER) && (fabs(v1.y - v2.y) < XSMALL_NUMBER) && (fabs(v1.z - v2.z) < XSMALL_NUMBER); 1.486 +} 1.487 + 1.488 +inline void operator +=(Vector3 &v1, const Vector3 &v2) { 1.489 + v1.x += v2.x; 1.490 + v1.y += v2.y; 1.491 + v1.z += v2.z; 1.492 +} 1.493 + 1.494 +inline void operator -=(Vector3 &v1, const Vector3 &v2) { 1.495 + v1.x -= v2.x; 1.496 + v1.y -= v2.y; 1.497 + v1.z -= v2.z; 1.498 +} 1.499 + 1.500 +inline void operator *=(Vector3 &v1, const Vector3 &v2) { 1.501 + v1.x *= v2.x; 1.502 + v1.y *= v2.y; 1.503 + v1.z *= v2.z; 1.504 +} 1.505 + 1.506 +inline void operator /=(Vector3 &v1, const Vector3 &v2) { 1.507 + v1.x /= v2.x; 1.508 + v1.y /= v2.y; 1.509 + v1.z /= v2.z; 1.510 +} 1.511 +/* binary vector (op) scalar and scalar (op) vector operations */ 1.512 +inline Vector3 operator +(const Vector3 &vec, scalar_t scalar) { 1.513 + return Vector3(vec.x + scalar, vec.y + scalar, vec.z + scalar); 1.514 +} 1.515 + 1.516 +inline Vector3 operator +(scalar_t scalar, const Vector3 &vec) { 1.517 + return Vector3(vec.x + scalar, vec.y + scalar, vec.z + scalar); 1.518 +} 1.519 + 1.520 +inline Vector3 operator -(const Vector3 &vec, scalar_t scalar) { 1.521 + return Vector3(vec.x - scalar, vec.y - scalar, vec.z - scalar); 1.522 +} 1.523 + 1.524 +inline Vector3 operator *(const Vector3 &vec, scalar_t scalar) { 1.525 + return Vector3(vec.x * scalar, vec.y * scalar, vec.z * scalar); 1.526 +} 1.527 + 1.528 +inline Vector3 operator *(scalar_t scalar, const Vector3 &vec) { 1.529 + return Vector3(vec.x * scalar, vec.y * scalar, vec.z * scalar); 1.530 +} 1.531 + 1.532 +inline Vector3 operator /(const Vector3 &vec, scalar_t scalar) { 1.533 + return Vector3(vec.x / scalar, vec.y / scalar, vec.z / scalar); 1.534 +} 1.535 + 1.536 +inline void operator +=(Vector3 &vec, scalar_t scalar) { 1.537 + vec.x += scalar; 1.538 + vec.y += scalar; 1.539 + vec.z += scalar; 1.540 +} 1.541 + 1.542 +inline void operator -=(Vector3 &vec, scalar_t scalar) { 1.543 + vec.x -= scalar; 1.544 + vec.y -= scalar; 1.545 + vec.z -= scalar; 1.546 +} 1.547 + 1.548 +inline void operator *=(Vector3 &vec, scalar_t scalar) { 1.549 + vec.x *= scalar; 1.550 + vec.y *= scalar; 1.551 + vec.z *= scalar; 1.552 +} 1.553 + 1.554 +inline void operator /=(Vector3 &vec, scalar_t scalar) { 1.555 + vec.x /= scalar; 1.556 + vec.y /= scalar; 1.557 + vec.z /= scalar; 1.558 +} 1.559 + 1.560 +inline scalar_t Vector3::length() const { 1.561 + return sqrt(x*x + y*y + z*z); 1.562 +} 1.563 +inline scalar_t Vector3::length_sq() const { 1.564 + return x*x + y*y + z*z; 1.565 +} 1.566 + 1.567 +inline Vector3 lerp(const Vector3 &a, const Vector3 &b, scalar_t t) { 1.568 + return a + (b - a) * t; 1.569 +} 1.570 + 1.571 +inline Vector3 catmull_rom_spline(const Vector3 &v0, const Vector3 &v1, 1.572 + const Vector3 &v2, const Vector3 &v3, scalar_t t) 1.573 +{ 1.574 + scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t); 1.575 + scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t); 1.576 + scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t); 1.577 + scalar_t z = spline(v0.z, v1.z, v2.z, v3.z, t); 1.578 + return Vector3(x, y, z); 1.579 +} 1.580 + 1.581 +/* ----------- Vector4 ----------------- */ 1.582 + 1.583 +inline scalar_t &Vector4::operator [](int elem) { 1.584 + return elem ? (elem == 1 ? y : (elem == 2 ? z : w)) : x; 1.585 +} 1.586 + 1.587 +inline const scalar_t &Vector4::operator [](int elem) const { 1.588 + return elem ? (elem == 1 ? y : (elem == 2 ? z : w)) : x; 1.589 +} 1.590 + 1.591 +inline Vector4 operator -(const Vector4 &vec) { 1.592 + return Vector4(-vec.x, -vec.y, -vec.z, -vec.w); 1.593 +} 1.594 + 1.595 +inline scalar_t dot_product(const Vector4 &v1, const Vector4 &v2) { 1.596 + return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w; 1.597 +} 1.598 + 1.599 +inline Vector4 cross_product(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3) { 1.600 + scalar_t a, b, c, d, e, f; /* Intermediate Values */ 1.601 + Vector4 result; 1.602 + 1.603 + /* Calculate intermediate values. */ 1.604 + a = (v2.x * v3.y) - (v2.y * v3.x); 1.605 + b = (v2.x * v3.z) - (v2.z * v3.x); 1.606 + c = (v2.x * v3.w) - (v2.w * v3.x); 1.607 + d = (v2.y * v3.z) - (v2.z * v3.y); 1.608 + e = (v2.y * v3.w) - (v2.w * v3.y); 1.609 + f = (v2.z * v3.w) - (v2.w * v3.z); 1.610 + 1.611 + /* Calculate the result-vector components. */ 1.612 + result.x = (v1.y * f) - (v1.z * e) + (v1.w * d); 1.613 + result.y = - (v1.x * f) + (v1.z * c) - (v1.w * b); 1.614 + result.z = (v1.x * e) - (v1.y * c) + (v1.w * a); 1.615 + result.w = - (v1.x * d) + (v1.y * b) - (v1.z * a); 1.616 + return result; 1.617 +} 1.618 + 1.619 +inline Vector4 operator +(const Vector4 &v1, const Vector4 &v2) { 1.620 + return Vector4(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w); 1.621 +} 1.622 + 1.623 +inline Vector4 operator -(const Vector4 &v1, const Vector4 &v2) { 1.624 + return Vector4(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w); 1.625 +} 1.626 + 1.627 +inline Vector4 operator *(const Vector4 &v1, const Vector4 &v2) { 1.628 + return Vector4(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w); 1.629 +} 1.630 + 1.631 +inline Vector4 operator /(const Vector4 &v1, const Vector4 &v2) { 1.632 + return Vector4(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.w / v2.w); 1.633 +} 1.634 + 1.635 +inline bool operator ==(const Vector4 &v1, const Vector4 &v2) { 1.636 + return (fabs(v1.x - v2.x) < XSMALL_NUMBER) && 1.637 + (fabs(v1.y - v2.y) < XSMALL_NUMBER) && 1.638 + (fabs(v1.z - v2.z) < XSMALL_NUMBER) && 1.639 + (fabs(v1.w - v2.w) < XSMALL_NUMBER); 1.640 +} 1.641 + 1.642 +inline void operator +=(Vector4 &v1, const Vector4 &v2) { 1.643 + v1.x += v2.x; 1.644 + v1.y += v2.y; 1.645 + v1.z += v2.z; 1.646 + v1.w += v2.w; 1.647 +} 1.648 + 1.649 +inline void operator -=(Vector4 &v1, const Vector4 &v2) { 1.650 + v1.x -= v2.x; 1.651 + v1.y -= v2.y; 1.652 + v1.z -= v2.z; 1.653 + v1.w -= v2.w; 1.654 +} 1.655 + 1.656 +inline void operator *=(Vector4 &v1, const Vector4 &v2) { 1.657 + v1.x *= v2.x; 1.658 + v1.y *= v2.y; 1.659 + v1.z *= v2.z; 1.660 + v1.w *= v2.w; 1.661 +} 1.662 + 1.663 +inline void operator /=(Vector4 &v1, const Vector4 &v2) { 1.664 + v1.x /= v2.x; 1.665 + v1.y /= v2.y; 1.666 + v1.z /= v2.z; 1.667 + v1.w /= v2.w; 1.668 +} 1.669 + 1.670 +/* binary vector (op) scalar and scalar (op) vector operations */ 1.671 +inline Vector4 operator +(const Vector4 &vec, scalar_t scalar) { 1.672 + return Vector4(vec.x + scalar, vec.y + scalar, vec.z + scalar, vec.w + scalar); 1.673 +} 1.674 + 1.675 +inline Vector4 operator +(scalar_t scalar, const Vector4 &vec) { 1.676 + return Vector4(vec.x + scalar, vec.y + scalar, vec.z + scalar, vec.w + scalar); 1.677 +} 1.678 + 1.679 +inline Vector4 operator -(const Vector4 &vec, scalar_t scalar) { 1.680 + return Vector4(vec.x - scalar, vec.y - scalar, vec.z - scalar, vec.w - scalar); 1.681 +} 1.682 + 1.683 +inline Vector4 operator *(const Vector4 &vec, scalar_t scalar) { 1.684 + return Vector4(vec.x * scalar, vec.y * scalar, vec.z * scalar, vec.w * scalar); 1.685 +} 1.686 + 1.687 +inline Vector4 operator *(scalar_t scalar, const Vector4 &vec) { 1.688 + return Vector4(vec.x * scalar, vec.y * scalar, vec.z * scalar, vec.w * scalar); 1.689 +} 1.690 + 1.691 +inline Vector4 operator /(const Vector4 &vec, scalar_t scalar) { 1.692 + return Vector4(vec.x / scalar, vec.y / scalar, vec.z / scalar, vec.w / scalar); 1.693 +} 1.694 + 1.695 +inline void operator +=(Vector4 &vec, scalar_t scalar) { 1.696 + vec.x += scalar; 1.697 + vec.y += scalar; 1.698 + vec.z += scalar; 1.699 + vec.w += scalar; 1.700 +} 1.701 + 1.702 +inline void operator -=(Vector4 &vec, scalar_t scalar) { 1.703 + vec.x -= scalar; 1.704 + vec.y -= scalar; 1.705 + vec.z -= scalar; 1.706 + vec.w -= scalar; 1.707 +} 1.708 + 1.709 +inline void operator *=(Vector4 &vec, scalar_t scalar) { 1.710 + vec.x *= scalar; 1.711 + vec.y *= scalar; 1.712 + vec.z *= scalar; 1.713 + vec.w *= scalar; 1.714 +} 1.715 + 1.716 +inline void operator /=(Vector4 &vec, scalar_t scalar) { 1.717 + vec.x /= scalar; 1.718 + vec.y /= scalar; 1.719 + vec.z /= scalar; 1.720 + vec.w /= scalar; 1.721 +} 1.722 + 1.723 +inline scalar_t Vector4::length() const { 1.724 + return sqrt(x*x + y*y + z*z + w*w); 1.725 +} 1.726 +inline scalar_t Vector4::length_sq() const { 1.727 + return x*x + y*y + z*z + w*w; 1.728 +} 1.729 + 1.730 +inline Vector4 lerp(const Vector4 &v0, const Vector4 &v1, scalar_t t) 1.731 +{ 1.732 + return v0 + (v1 - v0) * t; 1.733 +} 1.734 + 1.735 +inline Vector4 catmull_rom_spline(const Vector4 &v0, const Vector4 &v1, 1.736 + const Vector4 &v2, const Vector4 &v3, scalar_t t) 1.737 +{ 1.738 + scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t); 1.739 + scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t); 1.740 + scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t); 1.741 + scalar_t z = spline(v0.z, v1.z, v2.z, v3.z, t); 1.742 + scalar_t w = spline(v0.w, v1.w, v2.w, v3.w, t); 1.743 + return Vector4(x, y, z, w); 1.744 +} 1.745 + 1.746 +#endif /* __cplusplus */