dbf-halloween2015
diff libs/vmath/vector.h @ 1:c3f5c32cb210
barfed all the libraries in the source tree to make porting easier
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 01 Nov 2015 00:36:56 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/vmath/vector.h Sun Nov 01 00:36:56 2015 +0200 1.3 @@ -0,0 +1,298 @@ 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 +#ifndef VMATH_VECTOR_H_ 1.23 +#define VMATH_VECTOR_H_ 1.24 + 1.25 +#include <stdio.h> 1.26 +#include "vmath_types.h" 1.27 + 1.28 +#ifdef __cplusplus 1.29 +extern "C" { 1.30 +#endif /* __cplusplus */ 1.31 + 1.32 +/* C 2D vector functions */ 1.33 +static inline vec2_t v2_cons(scalar_t x, scalar_t y); 1.34 +static inline void v2_print(FILE *fp, vec2_t v); 1.35 + 1.36 +static inline vec2_t v2_add(vec2_t v1, vec2_t v2); 1.37 +static inline vec2_t v2_sub(vec2_t v1, vec2_t v2); 1.38 +static inline vec2_t v2_scale(vec2_t v, scalar_t s); 1.39 +static inline scalar_t v2_dot(vec2_t v1, vec2_t v2); 1.40 +static inline scalar_t v2_length(vec2_t v); 1.41 +static inline scalar_t v2_length_sq(vec2_t v); 1.42 +static inline vec2_t v2_normalize(vec2_t v); 1.43 + 1.44 +static inline vec2_t v2_lerp(vec2_t v1, vec2_t v2, scalar_t t); 1.45 + 1.46 +/* C 3D vector functions */ 1.47 +static inline vec3_t v3_cons(scalar_t x, scalar_t y, scalar_t z); 1.48 +static inline void v3_print(FILE *fp, vec3_t v); 1.49 + 1.50 +static inline vec3_t v3_add(vec3_t v1, vec3_t v2); 1.51 +static inline vec3_t v3_sub(vec3_t v1, vec3_t v2); 1.52 +static inline vec3_t v3_neg(vec3_t v); 1.53 +static inline vec3_t v3_mul(vec3_t v1, vec3_t v2); 1.54 +static inline vec3_t v3_scale(vec3_t v1, scalar_t s); 1.55 +static inline scalar_t v3_dot(vec3_t v1, vec3_t v2); 1.56 +static inline vec3_t v3_cross(vec3_t v1, vec3_t v2); 1.57 +static inline scalar_t v3_length(vec3_t v); 1.58 +static inline scalar_t v3_length_sq(vec3_t v); 1.59 +static inline vec3_t v3_normalize(vec3_t v); 1.60 +static inline vec3_t v3_transform(vec3_t v, mat4_t m); 1.61 + 1.62 +static inline vec3_t v3_rotate(vec3_t v, scalar_t x, scalar_t y, scalar_t z); 1.63 +static inline vec3_t v3_rotate_axis(vec3_t v, scalar_t angle, scalar_t x, scalar_t y, scalar_t z); 1.64 +static inline vec3_t v3_rotate_quat(vec3_t v, quat_t q); 1.65 + 1.66 +static inline vec3_t v3_reflect(vec3_t v, vec3_t n); 1.67 + 1.68 +static inline vec3_t v3_lerp(vec3_t v1, vec3_t v2, scalar_t t); 1.69 + 1.70 +/* C 4D vector functions */ 1.71 +static inline vec4_t v4_cons(scalar_t x, scalar_t y, scalar_t z, scalar_t w); 1.72 +static inline void v4_print(FILE *fp, vec4_t v); 1.73 + 1.74 +static inline vec4_t v4_add(vec4_t v1, vec4_t v2); 1.75 +static inline vec4_t v4_sub(vec4_t v1, vec4_t v2); 1.76 +static inline vec4_t v4_neg(vec4_t v); 1.77 +static inline vec4_t v4_mul(vec4_t v1, vec4_t v2); 1.78 +static inline vec4_t v4_scale(vec4_t v, scalar_t s); 1.79 +static inline scalar_t v4_dot(vec4_t v1, vec4_t v2); 1.80 +static inline scalar_t v4_length(vec4_t v); 1.81 +static inline scalar_t v4_length_sq(vec4_t v); 1.82 +static inline vec4_t v4_normalize(vec4_t v); 1.83 +static inline vec4_t v4_transform(vec4_t v, mat4_t m); 1.84 + 1.85 +#ifdef __cplusplus 1.86 +} /* extern "C" */ 1.87 + 1.88 +/* when included from C++ source files, also define the vector classes */ 1.89 +#include <iostream> 1.90 + 1.91 +/** 2D Vector */ 1.92 +class Vector2 { 1.93 +public: 1.94 + scalar_t x, y; 1.95 + 1.96 + Vector2(scalar_t x = 0.0, scalar_t y = 0.0); 1.97 + Vector2(const vec2_t &vec); 1.98 + Vector2(const Vector3 &vec); 1.99 + Vector2(const Vector4 &vec); 1.100 + 1.101 + inline scalar_t &operator [](int elem); 1.102 + inline const scalar_t &operator [](int elem) const; 1.103 + 1.104 + inline scalar_t length() const; 1.105 + inline scalar_t length_sq() const; 1.106 + void normalize(); 1.107 + Vector2 normalized() const; 1.108 + 1.109 + void transform(const Matrix3x3 &mat); 1.110 + Vector2 transformed(const Matrix3x3 &mat) const; 1.111 + 1.112 + void rotate(scalar_t angle); 1.113 + Vector2 rotated(scalar_t angle) const; 1.114 + 1.115 + Vector2 reflection(const Vector2 &normal) const; 1.116 + Vector2 refraction(const Vector2 &normal, scalar_t src_ior, scalar_t dst_ior) const; 1.117 +}; 1.118 + 1.119 +/* unary operations */ 1.120 +inline Vector2 operator -(const Vector2 &vec); 1.121 + 1.122 +/* binary vector (op) vector operations */ 1.123 +inline scalar_t dot_product(const Vector2 &v1, const Vector2 &v2); 1.124 + 1.125 +inline Vector2 operator +(const Vector2 &v1, const Vector2 &v2); 1.126 +inline Vector2 operator -(const Vector2 &v1, const Vector2 &v2); 1.127 +inline Vector2 operator *(const Vector2 &v1, const Vector2 &v2); 1.128 +inline Vector2 operator /(const Vector2 &v1, const Vector2 &v2); 1.129 +inline bool operator ==(const Vector2 &v1, const Vector2 &v2); 1.130 + 1.131 +inline void operator +=(Vector2 &v1, const Vector2 &v2); 1.132 +inline void operator -=(Vector2 &v1, const Vector2 &v2); 1.133 +inline void operator *=(Vector2 &v1, const Vector2 &v2); 1.134 +inline void operator /=(Vector2 &v1, const Vector2 &v2); 1.135 + 1.136 +/* binary vector (op) scalar and scalar (op) vector operations */ 1.137 +inline Vector2 operator +(const Vector2 &vec, scalar_t scalar); 1.138 +inline Vector2 operator +(scalar_t scalar, const Vector2 &vec); 1.139 +inline Vector2 operator -(const Vector2 &vec, scalar_t scalar); 1.140 +inline Vector2 operator *(const Vector2 &vec, scalar_t scalar); 1.141 +inline Vector2 operator *(scalar_t scalar, const Vector2 &vec); 1.142 +inline Vector2 operator /(const Vector2 &vec, scalar_t scalar); 1.143 + 1.144 +inline void operator +=(Vector2 &vec, scalar_t scalar); 1.145 +inline void operator -=(Vector2 &vec, scalar_t scalar); 1.146 +inline void operator *=(Vector2 &vec, scalar_t scalar); 1.147 +inline void operator /=(Vector2 &vec, scalar_t scalar); 1.148 + 1.149 +std::ostream &operator <<(std::ostream &out, const Vector2 &vec); 1.150 + 1.151 +inline Vector2 lerp(const Vector2 &a, const Vector2 &b, scalar_t t); 1.152 +inline Vector2 catmull_rom_spline(const Vector2 &v0, const Vector2 &v1, 1.153 + const Vector2 &v2, const Vector2 &v3, scalar_t t); 1.154 +inline Vector2 bspline(const Vector2 &v0, const Vector2 &v1, 1.155 + const Vector2 &v2, const Vector2 &v3, scalar_t t); 1.156 + 1.157 +/* 3D Vector */ 1.158 +class Vector3 { 1.159 +public: 1.160 + scalar_t x, y, z; 1.161 + 1.162 + Vector3(scalar_t x = 0.0, scalar_t y = 0.0, scalar_t z = 0.0); 1.163 + Vector3(const vec3_t &vec); 1.164 + Vector3(const Vector2 &vec); 1.165 + Vector3(const Vector4 &vec); 1.166 + Vector3(const SphVector &sph); 1.167 + 1.168 + Vector3 &operator =(const SphVector &sph); 1.169 + 1.170 + inline scalar_t &operator [](int elem); 1.171 + inline const scalar_t &operator [](int elem) const; 1.172 + 1.173 + inline scalar_t length() const; 1.174 + inline scalar_t length_sq() const; 1.175 + void normalize(); 1.176 + Vector3 normalized() const; 1.177 + 1.178 + void transform(const Matrix3x3 &mat); 1.179 + Vector3 transformed(const Matrix3x3 &mat) const; 1.180 + void transform(const Matrix4x4 &mat); 1.181 + Vector3 transformed(const Matrix4x4 &mat) const; 1.182 + void transform(const Quaternion &quat); 1.183 + Vector3 transformed(const Quaternion &quat) const; 1.184 + 1.185 + void rotate(const Vector3 &euler); 1.186 + Vector3 rotated(const Vector3 &euler) const; 1.187 + 1.188 + Vector3 reflection(const Vector3 &normal) const; 1.189 + Vector3 refraction(const Vector3 &normal, scalar_t src_ior, scalar_t dst_ior) const; 1.190 + Vector3 refraction(const Vector3 &normal, scalar_t ior) const; 1.191 +}; 1.192 + 1.193 +/* unary operations */ 1.194 +inline Vector3 operator -(const Vector3 &vec); 1.195 + 1.196 +/* binary vector (op) vector operations */ 1.197 +inline scalar_t dot_product(const Vector3 &v1, const Vector3 &v2); 1.198 +inline Vector3 cross_product(const Vector3 &v1, const Vector3 &v2); 1.199 + 1.200 +inline Vector3 operator +(const Vector3 &v1, const Vector3 &v2); 1.201 +inline Vector3 operator -(const Vector3 &v1, const Vector3 &v2); 1.202 +inline Vector3 operator *(const Vector3 &v1, const Vector3 &v2); 1.203 +inline Vector3 operator /(const Vector3 &v1, const Vector3 &v2); 1.204 +inline bool operator ==(const Vector3 &v1, const Vector3 &v2); 1.205 + 1.206 +inline void operator +=(Vector3 &v1, const Vector3 &v2); 1.207 +inline void operator -=(Vector3 &v1, const Vector3 &v2); 1.208 +inline void operator *=(Vector3 &v1, const Vector3 &v2); 1.209 +inline void operator /=(Vector3 &v1, const Vector3 &v2); 1.210 + 1.211 +/* binary vector (op) scalar and scalar (op) vector operations */ 1.212 +inline Vector3 operator +(const Vector3 &vec, scalar_t scalar); 1.213 +inline Vector3 operator +(scalar_t scalar, const Vector3 &vec); 1.214 +inline Vector3 operator -(const Vector3 &vec, scalar_t scalar); 1.215 +inline Vector3 operator *(const Vector3 &vec, scalar_t scalar); 1.216 +inline Vector3 operator *(scalar_t scalar, const Vector3 &vec); 1.217 +inline Vector3 operator /(const Vector3 &vec, scalar_t scalar); 1.218 + 1.219 +inline void operator +=(Vector3 &vec, scalar_t scalar); 1.220 +inline void operator -=(Vector3 &vec, scalar_t scalar); 1.221 +inline void operator *=(Vector3 &vec, scalar_t scalar); 1.222 +inline void operator /=(Vector3 &vec, scalar_t scalar); 1.223 + 1.224 +std::ostream &operator <<(std::ostream &out, const Vector3 &vec); 1.225 + 1.226 +inline Vector3 lerp(const Vector3 &a, const Vector3 &b, scalar_t t); 1.227 +inline Vector3 catmull_rom_spline(const Vector3 &v0, const Vector3 &v1, 1.228 + const Vector3 &v2, const Vector3 &v3, scalar_t t); 1.229 +inline Vector3 bspline(const Vector3 &v0, const Vector3 &v1, 1.230 + const Vector3 &v2, const Vector3 &v3, scalar_t t); 1.231 + 1.232 +/* 4D Vector */ 1.233 +class Vector4 { 1.234 +public: 1.235 + scalar_t x, y, z, w; 1.236 + 1.237 + Vector4(scalar_t x = 0.0, scalar_t y = 0.0, scalar_t z = 0.0, scalar_t w = 0.0); 1.238 + Vector4(const vec4_t &vec); 1.239 + Vector4(const Vector2 &vec); 1.240 + Vector4(const Vector3 &vec); 1.241 + 1.242 + inline scalar_t &operator [](int elem); 1.243 + inline const scalar_t &operator [](int elem) const; 1.244 + 1.245 + inline scalar_t length() const; 1.246 + inline scalar_t length_sq() const; 1.247 + void normalize(); 1.248 + Vector4 normalized() const; 1.249 + 1.250 + void transform(const Matrix4x4 &mat); 1.251 + Vector4 transformed(const Matrix4x4 &mat) const; 1.252 + 1.253 + Vector4 reflection(const Vector4 &normal) const; 1.254 + Vector4 refraction(const Vector4 &normal, scalar_t src_ior, scalar_t dst_ior) const; 1.255 +}; 1.256 + 1.257 + 1.258 +/* unary operations */ 1.259 +inline Vector4 operator -(const Vector4 &vec); 1.260 + 1.261 +/* binary vector (op) vector operations */ 1.262 +inline scalar_t dot_product(const Vector4 &v1, const Vector4 &v2); 1.263 +inline Vector4 cross_product(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3); 1.264 + 1.265 +inline Vector4 operator +(const Vector4 &v1, const Vector4 &v2); 1.266 +inline Vector4 operator -(const Vector4 &v1, const Vector4 &v2); 1.267 +inline Vector4 operator *(const Vector4 &v1, const Vector4 &v2); 1.268 +inline Vector4 operator /(const Vector4 &v1, const Vector4 &v2); 1.269 +inline bool operator ==(const Vector4 &v1, const Vector4 &v2); 1.270 + 1.271 +inline void operator +=(Vector4 &v1, const Vector4 &v2); 1.272 +inline void operator -=(Vector4 &v1, const Vector4 &v2); 1.273 +inline void operator *=(Vector4 &v1, const Vector4 &v2); 1.274 +inline void operator /=(Vector4 &v1, const Vector4 &v2); 1.275 + 1.276 +/* binary vector (op) scalar and scalar (op) vector operations */ 1.277 +inline Vector4 operator +(const Vector4 &vec, scalar_t scalar); 1.278 +inline Vector4 operator +(scalar_t scalar, const Vector4 &vec); 1.279 +inline Vector4 operator -(const Vector4 &vec, scalar_t scalar); 1.280 +inline Vector4 operator *(const Vector4 &vec, scalar_t scalar); 1.281 +inline Vector4 operator *(scalar_t scalar, const Vector4 &vec); 1.282 +inline Vector4 operator /(const Vector4 &vec, scalar_t scalar); 1.283 + 1.284 +inline void operator +=(Vector4 &vec, scalar_t scalar); 1.285 +inline void operator -=(Vector4 &vec, scalar_t scalar); 1.286 +inline void operator *=(Vector4 &vec, scalar_t scalar); 1.287 +inline void operator /=(Vector4 &vec, scalar_t scalar); 1.288 + 1.289 +std::ostream &operator <<(std::ostream &out, const Vector4 &vec); 1.290 + 1.291 +inline Vector4 lerp(const Vector4 &v0, const Vector4 &v1, scalar_t t); 1.292 +inline Vector4 catmull_rom_spline(const Vector4 &v0, const Vector4 &v1, 1.293 + const Vector4 &v2, const Vector4 &v3, scalar_t t); 1.294 +inline Vector4 bspline(const Vector4 &v0, const Vector4 &v1, 1.295 + const Vector4 &v2, const Vector4 &v3, scalar_t t); 1.296 + 1.297 +#endif /* __cplusplus */ 1.298 + 1.299 +#include "vector.inl" 1.300 + 1.301 +#endif /* VMATH_VECTOR_H_ */