gpuray_glsl

diff vmath/vector.inl @ 0:f234630e38ff

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