nuclear@39: /* nuclear@39: libvmath - a vector math library nuclear@39: Copyright (C) 2004-2011 John Tsiombikas nuclear@39: nuclear@39: This program is free software: you can redistribute it and/or modify nuclear@39: it under the terms of the GNU Lesser General Public License as published nuclear@39: by the Free Software Foundation, either version 3 of the License, or nuclear@39: (at your option) any later version. nuclear@39: nuclear@39: This program is distributed in the hope that it will be useful, nuclear@39: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@39: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@39: GNU Lesser General Public License for more details. nuclear@39: nuclear@39: You should have received a copy of the GNU Lesser General Public License nuclear@39: along with this program. If not, see . nuclear@39: */ nuclear@39: nuclear@29: #include nuclear@29: nuclear@29: #ifdef __cplusplus nuclear@29: extern "C" { nuclear@29: #endif /* __cplusplus */ nuclear@29: nuclear@29: /* C 2D vector functions */ nuclear@29: static inline vec2_t v2_cons(scalar_t x, scalar_t y) nuclear@29: { nuclear@29: vec2_t v; nuclear@29: v.x = x; nuclear@29: v.y = y; nuclear@29: return v; nuclear@29: } nuclear@29: nuclear@29: static inline void v2_print(FILE *fp, vec2_t v) nuclear@29: { nuclear@29: fprintf(fp, "[ %.4f %.4f ]", v.x, v.y); nuclear@29: } nuclear@29: nuclear@29: static inline vec2_t v2_add(vec2_t v1, vec2_t v2) nuclear@29: { nuclear@29: vec2_t res; nuclear@29: res.x = v1.x + v2.x; nuclear@29: res.y = v1.y + v2.y; nuclear@29: return res; nuclear@29: } nuclear@29: nuclear@29: static inline vec2_t v2_sub(vec2_t v1, vec2_t v2) nuclear@29: { nuclear@29: vec2_t res; nuclear@29: res.x = v1.x - v2.x; nuclear@29: res.y = v1.y - v2.y; nuclear@29: return res; nuclear@29: } nuclear@29: nuclear@29: static inline vec2_t v2_scale(vec2_t v, scalar_t s) nuclear@29: { nuclear@29: vec2_t res; nuclear@29: res.x = v.x * s; nuclear@29: res.y = v.y * s; nuclear@29: return res; nuclear@29: } nuclear@29: nuclear@29: static inline scalar_t v2_dot(vec2_t v1, vec2_t v2) nuclear@29: { nuclear@29: return v1.x * v2.x + v1.y * v2.y; nuclear@29: } nuclear@29: nuclear@29: static inline scalar_t v2_length(vec2_t v) nuclear@29: { nuclear@29: return sqrt(v.x * v.x + v.y * v.y); nuclear@29: } nuclear@29: nuclear@29: static inline scalar_t v2_length_sq(vec2_t v) nuclear@29: { nuclear@29: return v.x * v.x + v.y * v.y; nuclear@29: } nuclear@29: nuclear@29: static inline vec2_t v2_normalize(vec2_t v) nuclear@29: { nuclear@29: scalar_t len = (scalar_t)sqrt(v.x * v.x + v.y * v.y); nuclear@29: v.x /= len; nuclear@29: v.y /= len; nuclear@29: return v; nuclear@29: } nuclear@29: nuclear@29: static inline vec2_t v2_lerp(vec2_t v1, vec2_t v2, scalar_t t) nuclear@29: { nuclear@29: vec2_t res; nuclear@29: res.x = v1.x + (v2.x - v1.x) * t; nuclear@29: res.y = v1.y + (v2.y - v1.y) * t; nuclear@29: return res; nuclear@29: } nuclear@29: nuclear@29: nuclear@29: /* C 3D vector functions */ nuclear@29: static inline vec3_t v3_cons(scalar_t x, scalar_t y, scalar_t z) nuclear@29: { nuclear@29: vec3_t v; nuclear@29: v.x = x; nuclear@29: v.y = y; nuclear@29: v.z = z; nuclear@29: return v; nuclear@29: } nuclear@29: nuclear@29: static inline void v3_print(FILE *fp, vec3_t v) nuclear@29: { nuclear@29: fprintf(fp, "[ %.4f %.4f %.4f ]", v.x, v.y, v.z); nuclear@29: } nuclear@29: nuclear@29: static inline vec3_t v3_add(vec3_t v1, vec3_t v2) nuclear@29: { nuclear@29: v1.x += v2.x; nuclear@29: v1.y += v2.y; nuclear@29: v1.z += v2.z; nuclear@29: return v1; nuclear@29: } nuclear@29: nuclear@29: static inline vec3_t v3_sub(vec3_t v1, vec3_t v2) nuclear@29: { nuclear@29: v1.x -= v2.x; nuclear@29: v1.y -= v2.y; nuclear@29: v1.z -= v2.z; nuclear@29: return v1; nuclear@29: } nuclear@29: nuclear@29: static inline vec3_t v3_neg(vec3_t v) nuclear@29: { nuclear@29: v.x = -v.x; nuclear@29: v.y = -v.y; nuclear@29: v.z = -v.z; nuclear@29: return v; nuclear@29: } nuclear@29: nuclear@29: static inline vec3_t v3_mul(vec3_t v1, vec3_t v2) nuclear@29: { nuclear@29: v1.x *= v2.x; nuclear@29: v1.y *= v2.y; nuclear@29: v1.z *= v2.z; nuclear@29: return v1; nuclear@29: } nuclear@29: nuclear@29: static inline vec3_t v3_scale(vec3_t v1, scalar_t s) nuclear@29: { nuclear@29: v1.x *= s; nuclear@29: v1.y *= s; nuclear@29: v1.z *= s; nuclear@29: return v1; nuclear@29: } nuclear@29: nuclear@29: static inline scalar_t v3_dot(vec3_t v1, vec3_t v2) nuclear@29: { nuclear@29: return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; nuclear@29: } nuclear@29: nuclear@29: static inline vec3_t v3_cross(vec3_t v1, vec3_t v2) nuclear@29: { nuclear@29: vec3_t v; nuclear@29: v.x = v1.y * v2.z - v1.z * v2.y; nuclear@29: v.y = v1.z * v2.x - v1.x * v2.z; nuclear@29: v.z = v1.x * v2.y - v1.y * v2.x; nuclear@29: return v; nuclear@29: } nuclear@29: nuclear@29: static inline scalar_t v3_length(vec3_t v) nuclear@29: { nuclear@29: return sqrt(v.x * v.x + v.y * v.y + v.z * v.z); nuclear@29: } nuclear@29: nuclear@29: static inline scalar_t v3_length_sq(vec3_t v) nuclear@29: { nuclear@29: return v.x * v.x + v.y * v.y + v.z * v.z; nuclear@29: } nuclear@29: nuclear@29: static inline vec3_t v3_normalize(vec3_t v) nuclear@29: { nuclear@29: scalar_t len = sqrt(v.x * v.x + v.y * v.y + v.z * v.z); nuclear@29: v.x /= len; nuclear@29: v.y /= len; nuclear@29: v.z /= len; nuclear@29: return v; nuclear@29: } nuclear@29: nuclear@29: static inline vec3_t v3_transform(vec3_t v, mat4_t m) nuclear@29: { nuclear@29: vec3_t res; nuclear@29: res.x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3]; nuclear@29: res.y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3]; nuclear@29: res.z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3]; nuclear@29: return res; nuclear@29: } nuclear@29: nuclear@29: static inline vec3_t v3_rotate(vec3_t v, scalar_t x, scalar_t y, scalar_t z) nuclear@29: { nuclear@29: void m4_rotate(mat4_t, scalar_t, scalar_t, scalar_t); nuclear@29: nuclear@29: mat4_t m = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; nuclear@29: m4_rotate(m, x, y, z); nuclear@29: return v3_transform(v, m); nuclear@29: } nuclear@29: nuclear@29: static inline vec3_t v3_rotate_axis(vec3_t v, scalar_t angle, scalar_t x, scalar_t y, scalar_t z) nuclear@29: { nuclear@29: void m4_rotate_axis(mat4_t, scalar_t, scalar_t, scalar_t, scalar_t); nuclear@29: nuclear@29: mat4_t m = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; nuclear@29: m4_rotate_axis(m, angle, x, y, z); nuclear@29: return v3_transform(v, m); nuclear@29: } nuclear@29: nuclear@29: static inline vec3_t v3_rotate_quat(vec3_t v, quat_t q) nuclear@29: { nuclear@29: quat_t quat_rotate_quat(quat_t, quat_t); nuclear@29: nuclear@29: quat_t vq = v4_cons(v.x, v.y, v.z, 0.0); nuclear@29: quat_t res = quat_rotate_quat(vq, q); nuclear@29: return v3_cons(res.x, res.y, res.z); nuclear@29: } nuclear@29: nuclear@29: static inline vec3_t v3_reflect(vec3_t v, vec3_t n) nuclear@29: { nuclear@29: scalar_t dot = v3_dot(v, n); nuclear@29: return v3_sub(v3_scale(n, dot * 2.0), v); nuclear@29: } nuclear@29: nuclear@29: static inline vec3_t v3_lerp(vec3_t v1, vec3_t v2, scalar_t t) nuclear@29: { nuclear@29: v1.x += (v2.x - v1.x) * t; nuclear@29: v1.y += (v2.y - v1.y) * t; nuclear@29: v1.z += (v2.z - v1.z) * t; nuclear@29: return v1; nuclear@29: } nuclear@29: nuclear@29: /* C 4D vector functions */ nuclear@29: static inline vec4_t v4_cons(scalar_t x, scalar_t y, scalar_t z, scalar_t w) nuclear@29: { nuclear@29: vec4_t v; nuclear@29: v.x = x; nuclear@29: v.y = y; nuclear@29: v.z = z; nuclear@29: v.w = w; nuclear@29: return v; nuclear@29: } nuclear@29: nuclear@29: static inline void v4_print(FILE *fp, vec4_t v) nuclear@29: { nuclear@29: fprintf(fp, "[ %.4f %.4f %.4f %.4f ]", v.x, v.y, v.z, v.w); nuclear@29: } nuclear@29: nuclear@29: static inline vec4_t v4_add(vec4_t v1, vec4_t v2) nuclear@29: { nuclear@29: v1.x += v2.x; nuclear@29: v1.y += v2.y; nuclear@29: v1.z += v2.z; nuclear@29: v1.w += v2.w; nuclear@29: return v1; nuclear@29: } nuclear@29: nuclear@29: static inline vec4_t v4_sub(vec4_t v1, vec4_t v2) nuclear@29: { nuclear@29: v1.x -= v2.x; nuclear@29: v1.y -= v2.y; nuclear@29: v1.z -= v2.z; nuclear@29: v1.w -= v2.w; nuclear@29: return v1; nuclear@29: } nuclear@29: nuclear@29: static inline vec4_t v4_neg(vec4_t v) nuclear@29: { nuclear@29: v.x = -v.x; nuclear@29: v.y = -v.y; nuclear@29: v.z = -v.z; nuclear@29: v.w = -v.w; nuclear@29: return v; nuclear@29: } nuclear@29: nuclear@29: static inline vec4_t v4_mul(vec4_t v1, vec4_t v2) nuclear@29: { nuclear@29: v1.x *= v2.x; nuclear@29: v1.y *= v2.y; nuclear@29: v1.z *= v2.z; nuclear@29: v1.w *= v2.w; nuclear@29: return v1; nuclear@29: } nuclear@29: nuclear@29: static inline vec4_t v4_scale(vec4_t v, scalar_t s) nuclear@29: { nuclear@29: v.x *= s; nuclear@29: v.y *= s; nuclear@29: v.z *= s; nuclear@29: v.w *= s; nuclear@29: return v; nuclear@29: } nuclear@29: nuclear@29: static inline scalar_t v4_dot(vec4_t v1, vec4_t v2) nuclear@29: { nuclear@29: return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w; nuclear@29: } nuclear@29: nuclear@29: static inline scalar_t v4_length(vec4_t v) nuclear@29: { nuclear@29: return sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); nuclear@29: } nuclear@29: nuclear@29: static inline scalar_t v4_length_sq(vec4_t v) nuclear@29: { nuclear@29: return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w; nuclear@29: } nuclear@29: nuclear@29: static inline vec4_t v4_normalize(vec4_t v) nuclear@29: { nuclear@29: scalar_t len = sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); nuclear@29: v.x /= len; nuclear@29: v.y /= len; nuclear@29: v.z /= len; nuclear@29: v.w /= len; nuclear@29: return v; nuclear@29: } nuclear@29: nuclear@29: static inline vec4_t v4_transform(vec4_t v, mat4_t m) nuclear@29: { nuclear@29: vec4_t res; nuclear@29: res.x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w; nuclear@29: res.y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w; nuclear@29: res.z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w; nuclear@29: res.w = m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w; nuclear@29: return res; nuclear@29: } nuclear@29: nuclear@29: #ifdef __cplusplus nuclear@29: } /* extern "C" */ nuclear@29: nuclear@29: nuclear@29: /* --------------- C++ part -------------- */ nuclear@29: nuclear@29: inline scalar_t &Vector2::operator [](int elem) { nuclear@29: return elem ? y : x; nuclear@29: } nuclear@29: nuclear@29: inline const scalar_t &Vector2::operator [](int elem) const { nuclear@29: return elem ? y : x; nuclear@29: } nuclear@29: nuclear@29: inline Vector2 operator -(const Vector2 &vec) { nuclear@29: return Vector2(-vec.x, -vec.y); nuclear@29: } nuclear@29: nuclear@29: inline scalar_t dot_product(const Vector2 &v1, const Vector2 &v2) { nuclear@29: return v1.x * v2.x + v1.y * v2.y; nuclear@29: } nuclear@29: nuclear@29: inline Vector2 operator +(const Vector2 &v1, const Vector2 &v2) { nuclear@29: return Vector2(v1.x + v2.x, v1.y + v2.y); nuclear@29: } nuclear@29: nuclear@29: inline Vector2 operator -(const Vector2 &v1, const Vector2 &v2) { nuclear@29: return Vector2(v1.x - v2.x, v1.y - v2.y); nuclear@29: } nuclear@29: nuclear@29: inline Vector2 operator *(const Vector2 &v1, const Vector2 &v2) { nuclear@29: return Vector2(v1.x * v2.x, v1.y * v2.y); nuclear@29: } nuclear@29: nuclear@29: inline Vector2 operator /(const Vector2 &v1, const Vector2 &v2) { nuclear@29: return Vector2(v1.x / v2.x, v1.y / v2.y); nuclear@29: } nuclear@29: nuclear@29: inline bool operator ==(const Vector2 &v1, const Vector2 &v2) { nuclear@29: return (fabs(v1.x - v2.x) < XSMALL_NUMBER) && (fabs(v1.y - v2.x) < XSMALL_NUMBER); nuclear@29: } nuclear@29: nuclear@29: inline void operator +=(Vector2 &v1, const Vector2 &v2) { nuclear@29: v1.x += v2.x; nuclear@29: v1.y += v2.y; nuclear@29: } nuclear@29: nuclear@29: inline void operator -=(Vector2 &v1, const Vector2 &v2) { nuclear@29: v1.x -= v2.x; nuclear@29: v1.y -= v2.y; nuclear@29: } nuclear@29: nuclear@29: inline void operator *=(Vector2 &v1, const Vector2 &v2) { nuclear@29: v1.x *= v2.x; nuclear@29: v1.y *= v2.y; nuclear@29: } nuclear@29: nuclear@29: inline void operator /=(Vector2 &v1, const Vector2 &v2) { nuclear@29: v1.x /= v2.x; nuclear@29: v1.y /= v2.y; nuclear@29: } nuclear@29: nuclear@29: inline Vector2 operator +(const Vector2 &vec, scalar_t scalar) { nuclear@29: return Vector2(vec.x + scalar, vec.y + scalar); nuclear@29: } nuclear@29: nuclear@29: inline Vector2 operator +(scalar_t scalar, const Vector2 &vec) { nuclear@29: return Vector2(vec.x + scalar, vec.y + scalar); nuclear@29: } nuclear@29: nuclear@29: inline Vector2 operator -(scalar_t scalar, const Vector2 &vec) { nuclear@29: return Vector2(vec.x - scalar, vec.y - scalar); nuclear@29: } nuclear@29: nuclear@29: inline Vector2 operator *(const Vector2 &vec, scalar_t scalar) { nuclear@29: return Vector2(vec.x * scalar, vec.y * scalar); nuclear@29: } nuclear@29: nuclear@29: inline Vector2 operator *(scalar_t scalar, const Vector2 &vec) { nuclear@29: return Vector2(vec.x * scalar, vec.y * scalar); nuclear@29: } nuclear@29: nuclear@29: inline Vector2 operator /(const Vector2 &vec, scalar_t scalar) { nuclear@29: return Vector2(vec.x / scalar, vec.y / scalar); nuclear@29: } nuclear@29: nuclear@29: inline void operator +=(Vector2 &vec, scalar_t scalar) { nuclear@29: vec.x += scalar; nuclear@29: vec.y += scalar; nuclear@29: } nuclear@29: nuclear@29: inline void operator -=(Vector2 &vec, scalar_t scalar) { nuclear@29: vec.x -= scalar; nuclear@29: vec.y -= scalar; nuclear@29: } nuclear@29: nuclear@29: inline void operator *=(Vector2 &vec, scalar_t scalar) { nuclear@29: vec.x *= scalar; nuclear@29: vec.y *= scalar; nuclear@29: } nuclear@29: nuclear@29: inline void operator /=(Vector2 &vec, scalar_t scalar) { nuclear@29: vec.x /= scalar; nuclear@29: vec.y /= scalar; nuclear@29: } nuclear@29: nuclear@29: inline scalar_t Vector2::length() const { nuclear@29: return sqrt(x*x + y*y); nuclear@29: } nuclear@29: nuclear@29: inline scalar_t Vector2::length_sq() const { nuclear@29: return x*x + y*y; nuclear@29: } nuclear@29: nuclear@29: inline Vector2 lerp(const Vector2 &a, const Vector2 &b, scalar_t t) nuclear@29: { nuclear@29: return a + (b - a) * t; nuclear@29: } nuclear@29: nuclear@29: inline Vector2 catmull_rom_spline(const Vector2 &v0, const Vector2 &v1, nuclear@29: const Vector2 &v2, const Vector2 &v3, scalar_t t) nuclear@29: { nuclear@29: scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t); nuclear@29: scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t); nuclear@29: scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t); nuclear@29: return Vector2(x, y); nuclear@29: } nuclear@29: nuclear@29: nuclear@29: /* ------------- Vector3 -------------- */ nuclear@29: nuclear@29: inline scalar_t &Vector3::operator [](int elem) { nuclear@29: return elem ? (elem == 1 ? y : z) : x; nuclear@29: } nuclear@29: nuclear@29: inline const scalar_t &Vector3::operator [](int elem) const { nuclear@29: return elem ? (elem == 1 ? y : z) : x; nuclear@29: } nuclear@29: nuclear@29: /* unary operations */ nuclear@29: inline Vector3 operator -(const Vector3 &vec) { nuclear@29: return Vector3(-vec.x, -vec.y, -vec.z); nuclear@29: } nuclear@29: nuclear@29: /* binary vector (op) vector operations */ nuclear@29: inline scalar_t dot_product(const Vector3 &v1, const Vector3 &v2) { nuclear@29: return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; nuclear@29: } nuclear@29: nuclear@29: inline Vector3 cross_product(const Vector3 &v1, const Vector3 &v2) { nuclear@29: 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: } nuclear@29: nuclear@29: nuclear@29: inline Vector3 operator +(const Vector3 &v1, const Vector3 &v2) { nuclear@29: return Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z); nuclear@29: } nuclear@29: nuclear@29: inline Vector3 operator -(const Vector3 &v1, const Vector3 &v2) { nuclear@29: return Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z); nuclear@29: } nuclear@29: nuclear@29: inline Vector3 operator *(const Vector3 &v1, const Vector3 &v2) { nuclear@29: return Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z); nuclear@29: } nuclear@29: nuclear@29: inline Vector3 operator /(const Vector3 &v1, const Vector3 &v2) { nuclear@29: return Vector3(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z); nuclear@29: } nuclear@29: nuclear@29: inline bool operator ==(const Vector3 &v1, const Vector3 &v2) { nuclear@29: return (fabs(v1.x - v2.x) < XSMALL_NUMBER) && (fabs(v1.y - v2.y) < XSMALL_NUMBER) && (fabs(v1.z - v2.z) < XSMALL_NUMBER); nuclear@29: } nuclear@29: nuclear@29: inline void operator +=(Vector3 &v1, const Vector3 &v2) { nuclear@29: v1.x += v2.x; nuclear@29: v1.y += v2.y; nuclear@29: v1.z += v2.z; nuclear@29: } nuclear@29: nuclear@29: inline void operator -=(Vector3 &v1, const Vector3 &v2) { nuclear@29: v1.x -= v2.x; nuclear@29: v1.y -= v2.y; nuclear@29: v1.z -= v2.z; nuclear@29: } nuclear@29: nuclear@29: inline void operator *=(Vector3 &v1, const Vector3 &v2) { nuclear@29: v1.x *= v2.x; nuclear@29: v1.y *= v2.y; nuclear@29: v1.z *= v2.z; nuclear@29: } nuclear@29: nuclear@29: inline void operator /=(Vector3 &v1, const Vector3 &v2) { nuclear@29: v1.x /= v2.x; nuclear@29: v1.y /= v2.y; nuclear@29: v1.z /= v2.z; nuclear@29: } nuclear@29: /* binary vector (op) scalar and scalar (op) vector operations */ nuclear@29: inline Vector3 operator +(const Vector3 &vec, scalar_t scalar) { nuclear@29: return Vector3(vec.x + scalar, vec.y + scalar, vec.z + scalar); nuclear@29: } nuclear@29: nuclear@29: inline Vector3 operator +(scalar_t scalar, const Vector3 &vec) { nuclear@29: return Vector3(vec.x + scalar, vec.y + scalar, vec.z + scalar); nuclear@29: } nuclear@29: nuclear@29: inline Vector3 operator -(const Vector3 &vec, scalar_t scalar) { nuclear@29: return Vector3(vec.x - scalar, vec.y - scalar, vec.z - scalar); nuclear@29: } nuclear@29: nuclear@29: inline Vector3 operator *(const Vector3 &vec, scalar_t scalar) { nuclear@29: return Vector3(vec.x * scalar, vec.y * scalar, vec.z * scalar); nuclear@29: } nuclear@29: nuclear@29: inline Vector3 operator *(scalar_t scalar, const Vector3 &vec) { nuclear@29: return Vector3(vec.x * scalar, vec.y * scalar, vec.z * scalar); nuclear@29: } nuclear@29: nuclear@29: inline Vector3 operator /(const Vector3 &vec, scalar_t scalar) { nuclear@29: return Vector3(vec.x / scalar, vec.y / scalar, vec.z / scalar); nuclear@29: } nuclear@29: nuclear@29: inline void operator +=(Vector3 &vec, scalar_t scalar) { nuclear@29: vec.x += scalar; nuclear@29: vec.y += scalar; nuclear@29: vec.z += scalar; nuclear@29: } nuclear@29: nuclear@29: inline void operator -=(Vector3 &vec, scalar_t scalar) { nuclear@29: vec.x -= scalar; nuclear@29: vec.y -= scalar; nuclear@29: vec.z -= scalar; nuclear@29: } nuclear@29: nuclear@29: inline void operator *=(Vector3 &vec, scalar_t scalar) { nuclear@29: vec.x *= scalar; nuclear@29: vec.y *= scalar; nuclear@29: vec.z *= scalar; nuclear@29: } nuclear@29: nuclear@29: inline void operator /=(Vector3 &vec, scalar_t scalar) { nuclear@29: vec.x /= scalar; nuclear@29: vec.y /= scalar; nuclear@29: vec.z /= scalar; nuclear@29: } nuclear@29: nuclear@29: inline scalar_t Vector3::length() const { nuclear@29: return sqrt(x*x + y*y + z*z); nuclear@29: } nuclear@29: inline scalar_t Vector3::length_sq() const { nuclear@29: return x*x + y*y + z*z; nuclear@29: } nuclear@29: nuclear@29: inline Vector3 lerp(const Vector3 &a, const Vector3 &b, scalar_t t) { nuclear@29: return a + (b - a) * t; nuclear@29: } nuclear@29: nuclear@29: inline Vector3 catmull_rom_spline(const Vector3 &v0, const Vector3 &v1, nuclear@29: const Vector3 &v2, const Vector3 &v3, scalar_t t) nuclear@29: { nuclear@29: scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t); nuclear@29: scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t); nuclear@29: scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t); nuclear@29: scalar_t z = spline(v0.z, v1.z, v2.z, v3.z, t); nuclear@29: return Vector3(x, y, z); nuclear@29: } nuclear@29: nuclear@29: /* ----------- Vector4 ----------------- */ nuclear@29: nuclear@29: inline scalar_t &Vector4::operator [](int elem) { nuclear@29: return elem ? (elem == 1 ? y : (elem == 2 ? z : w)) : x; nuclear@29: } nuclear@29: nuclear@29: inline const scalar_t &Vector4::operator [](int elem) const { nuclear@29: return elem ? (elem == 1 ? y : (elem == 2 ? z : w)) : x; nuclear@29: } nuclear@29: nuclear@29: inline Vector4 operator -(const Vector4 &vec) { nuclear@29: return Vector4(-vec.x, -vec.y, -vec.z, -vec.w); nuclear@29: } nuclear@29: nuclear@29: inline scalar_t dot_product(const Vector4 &v1, const Vector4 &v2) { nuclear@29: return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w; nuclear@29: } nuclear@29: nuclear@29: inline Vector4 cross_product(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3) { nuclear@29: scalar_t a, b, c, d, e, f; /* Intermediate Values */ nuclear@29: Vector4 result; nuclear@29: nuclear@29: /* Calculate intermediate values. */ nuclear@29: a = (v2.x * v3.y) - (v2.y * v3.x); nuclear@29: b = (v2.x * v3.z) - (v2.z * v3.x); nuclear@29: c = (v2.x * v3.w) - (v2.w * v3.x); nuclear@29: d = (v2.y * v3.z) - (v2.z * v3.y); nuclear@29: e = (v2.y * v3.w) - (v2.w * v3.y); nuclear@29: f = (v2.z * v3.w) - (v2.w * v3.z); nuclear@29: nuclear@29: /* Calculate the result-vector components. */ nuclear@29: result.x = (v1.y * f) - (v1.z * e) + (v1.w * d); nuclear@29: result.y = - (v1.x * f) + (v1.z * c) - (v1.w * b); nuclear@29: result.z = (v1.x * e) - (v1.y * c) + (v1.w * a); nuclear@29: result.w = - (v1.x * d) + (v1.y * b) - (v1.z * a); nuclear@29: return result; nuclear@29: } nuclear@29: nuclear@29: inline Vector4 operator +(const Vector4 &v1, const Vector4 &v2) { nuclear@29: return Vector4(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w); nuclear@29: } nuclear@29: nuclear@29: inline Vector4 operator -(const Vector4 &v1, const Vector4 &v2) { nuclear@29: return Vector4(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w); nuclear@29: } nuclear@29: nuclear@29: inline Vector4 operator *(const Vector4 &v1, const Vector4 &v2) { nuclear@29: return Vector4(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w); nuclear@29: } nuclear@29: nuclear@29: inline Vector4 operator /(const Vector4 &v1, const Vector4 &v2) { nuclear@29: return Vector4(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.w / v2.w); nuclear@29: } nuclear@29: nuclear@29: inline bool operator ==(const Vector4 &v1, const Vector4 &v2) { nuclear@29: return (fabs(v1.x - v2.x) < XSMALL_NUMBER) && nuclear@29: (fabs(v1.y - v2.y) < XSMALL_NUMBER) && nuclear@29: (fabs(v1.z - v2.z) < XSMALL_NUMBER) && nuclear@29: (fabs(v1.w - v2.w) < XSMALL_NUMBER); nuclear@29: } nuclear@29: nuclear@29: inline void operator +=(Vector4 &v1, const Vector4 &v2) { nuclear@29: v1.x += v2.x; nuclear@29: v1.y += v2.y; nuclear@29: v1.z += v2.z; nuclear@29: v1.w += v2.w; nuclear@29: } nuclear@29: nuclear@29: inline void operator -=(Vector4 &v1, const Vector4 &v2) { nuclear@29: v1.x -= v2.x; nuclear@29: v1.y -= v2.y; nuclear@29: v1.z -= v2.z; nuclear@29: v1.w -= v2.w; nuclear@29: } nuclear@29: nuclear@29: inline void operator *=(Vector4 &v1, const Vector4 &v2) { nuclear@29: v1.x *= v2.x; nuclear@29: v1.y *= v2.y; nuclear@29: v1.z *= v2.z; nuclear@29: v1.w *= v2.w; nuclear@29: } nuclear@29: nuclear@29: inline void operator /=(Vector4 &v1, const Vector4 &v2) { nuclear@29: v1.x /= v2.x; nuclear@29: v1.y /= v2.y; nuclear@29: v1.z /= v2.z; nuclear@29: v1.w /= v2.w; nuclear@29: } nuclear@29: nuclear@29: /* binary vector (op) scalar and scalar (op) vector operations */ nuclear@29: inline Vector4 operator +(const Vector4 &vec, scalar_t scalar) { nuclear@29: return Vector4(vec.x + scalar, vec.y + scalar, vec.z + scalar, vec.w + scalar); nuclear@29: } nuclear@29: nuclear@29: inline Vector4 operator +(scalar_t scalar, const Vector4 &vec) { nuclear@29: return Vector4(vec.x + scalar, vec.y + scalar, vec.z + scalar, vec.w + scalar); nuclear@29: } nuclear@29: nuclear@29: inline Vector4 operator -(const Vector4 &vec, scalar_t scalar) { nuclear@29: return Vector4(vec.x - scalar, vec.y - scalar, vec.z - scalar, vec.w - scalar); nuclear@29: } nuclear@29: nuclear@29: inline Vector4 operator *(const Vector4 &vec, scalar_t scalar) { nuclear@29: return Vector4(vec.x * scalar, vec.y * scalar, vec.z * scalar, vec.w * scalar); nuclear@29: } nuclear@29: nuclear@29: inline Vector4 operator *(scalar_t scalar, const Vector4 &vec) { nuclear@29: return Vector4(vec.x * scalar, vec.y * scalar, vec.z * scalar, vec.w * scalar); nuclear@29: } nuclear@29: nuclear@29: inline Vector4 operator /(const Vector4 &vec, scalar_t scalar) { nuclear@29: return Vector4(vec.x / scalar, vec.y / scalar, vec.z / scalar, vec.w / scalar); nuclear@29: } nuclear@29: nuclear@29: inline void operator +=(Vector4 &vec, scalar_t scalar) { nuclear@29: vec.x += scalar; nuclear@29: vec.y += scalar; nuclear@29: vec.z += scalar; nuclear@29: vec.w += scalar; nuclear@29: } nuclear@29: nuclear@29: inline void operator -=(Vector4 &vec, scalar_t scalar) { nuclear@29: vec.x -= scalar; nuclear@29: vec.y -= scalar; nuclear@29: vec.z -= scalar; nuclear@29: vec.w -= scalar; nuclear@29: } nuclear@29: nuclear@29: inline void operator *=(Vector4 &vec, scalar_t scalar) { nuclear@29: vec.x *= scalar; nuclear@29: vec.y *= scalar; nuclear@29: vec.z *= scalar; nuclear@29: vec.w *= scalar; nuclear@29: } nuclear@29: nuclear@29: inline void operator /=(Vector4 &vec, scalar_t scalar) { nuclear@29: vec.x /= scalar; nuclear@29: vec.y /= scalar; nuclear@29: vec.z /= scalar; nuclear@29: vec.w /= scalar; nuclear@29: } nuclear@29: nuclear@29: inline scalar_t Vector4::length() const { nuclear@29: return sqrt(x*x + y*y + z*z + w*w); nuclear@29: } nuclear@29: inline scalar_t Vector4::length_sq() const { nuclear@29: return x*x + y*y + z*z + w*w; nuclear@29: } nuclear@29: nuclear@29: inline Vector4 lerp(const Vector4 &v0, const Vector4 &v1, scalar_t t) nuclear@29: { nuclear@29: return v0 + (v1 - v0) * t; nuclear@29: } nuclear@29: nuclear@29: inline Vector4 catmull_rom_spline(const Vector4 &v0, const Vector4 &v1, nuclear@29: const Vector4 &v2, const Vector4 &v3, scalar_t t) nuclear@29: { nuclear@29: scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t); nuclear@29: scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t); nuclear@29: scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t); nuclear@29: scalar_t z = spline(v0.z, v1.z, v2.z, v3.z, t); nuclear@29: scalar_t w = spline(v0.w, v1.w, v2.w, v3.w, t); nuclear@29: return Vector4(x, y, z, w); nuclear@29: } nuclear@29: nuclear@29: #endif /* __cplusplus */