3dphotoshoot

annotate libs/vmath/vector.h @ 10:c71c477521ca

converting to GLES2 and C++
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 31 May 2015 00:40:26 +0300
parents
children
rev   line source
nuclear@10 1 /*
nuclear@10 2 libvmath - a vector math library
nuclear@10 3 Copyright (C) 2004-2015 John Tsiombikas <nuclear@member.fsf.org>
nuclear@10 4
nuclear@10 5 This program is free software: you can redistribute it and/or modify
nuclear@10 6 it under the terms of the GNU Lesser General Public License as published
nuclear@10 7 by the Free Software Foundation, either version 3 of the License, or
nuclear@10 8 (at your option) any later version.
nuclear@10 9
nuclear@10 10 This program is distributed in the hope that it will be useful,
nuclear@10 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@10 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@10 13 GNU Lesser General Public License for more details.
nuclear@10 14
nuclear@10 15 You should have received a copy of the GNU Lesser General Public License
nuclear@10 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@10 17 */
nuclear@10 18
nuclear@10 19 #ifndef VMATH_VECTOR_H_
nuclear@10 20 #define VMATH_VECTOR_H_
nuclear@10 21
nuclear@10 22 #include <stdio.h>
nuclear@10 23 #include "vmath_types.h"
nuclear@10 24
nuclear@10 25 #ifdef __cplusplus
nuclear@10 26 extern "C" {
nuclear@10 27 #endif /* __cplusplus */
nuclear@10 28
nuclear@10 29 /* C 2D vector functions */
nuclear@10 30 static inline vec2_t v2_cons(scalar_t x, scalar_t y);
nuclear@10 31 static inline void v2_print(FILE *fp, vec2_t v);
nuclear@10 32
nuclear@10 33 static inline vec2_t v2_add(vec2_t v1, vec2_t v2);
nuclear@10 34 static inline vec2_t v2_sub(vec2_t v1, vec2_t v2);
nuclear@10 35 static inline vec2_t v2_scale(vec2_t v, scalar_t s);
nuclear@10 36 static inline scalar_t v2_dot(vec2_t v1, vec2_t v2);
nuclear@10 37 static inline scalar_t v2_length(vec2_t v);
nuclear@10 38 static inline scalar_t v2_length_sq(vec2_t v);
nuclear@10 39 static inline vec2_t v2_normalize(vec2_t v);
nuclear@10 40
nuclear@10 41 static inline vec2_t v2_lerp(vec2_t v1, vec2_t v2, scalar_t t);
nuclear@10 42
nuclear@10 43 /* C 3D vector functions */
nuclear@10 44 static inline vec3_t v3_cons(scalar_t x, scalar_t y, scalar_t z);
nuclear@10 45 static inline void v3_print(FILE *fp, vec3_t v);
nuclear@10 46
nuclear@10 47 static inline vec3_t v3_add(vec3_t v1, vec3_t v2);
nuclear@10 48 static inline vec3_t v3_sub(vec3_t v1, vec3_t v2);
nuclear@10 49 static inline vec3_t v3_neg(vec3_t v);
nuclear@10 50 static inline vec3_t v3_mul(vec3_t v1, vec3_t v2);
nuclear@10 51 static inline vec3_t v3_scale(vec3_t v1, scalar_t s);
nuclear@10 52 static inline scalar_t v3_dot(vec3_t v1, vec3_t v2);
nuclear@10 53 static inline vec3_t v3_cross(vec3_t v1, vec3_t v2);
nuclear@10 54 static inline scalar_t v3_length(vec3_t v);
nuclear@10 55 static inline scalar_t v3_length_sq(vec3_t v);
nuclear@10 56 static inline vec3_t v3_normalize(vec3_t v);
nuclear@10 57 static inline vec3_t v3_transform(vec3_t v, mat4_t m);
nuclear@10 58
nuclear@10 59 static inline vec3_t v3_rotate(vec3_t v, scalar_t x, scalar_t y, scalar_t z);
nuclear@10 60 static inline vec3_t v3_rotate_axis(vec3_t v, scalar_t angle, scalar_t x, scalar_t y, scalar_t z);
nuclear@10 61 static inline vec3_t v3_rotate_quat(vec3_t v, quat_t q);
nuclear@10 62
nuclear@10 63 static inline vec3_t v3_reflect(vec3_t v, vec3_t n);
nuclear@10 64
nuclear@10 65 static inline vec3_t v3_lerp(vec3_t v1, vec3_t v2, scalar_t t);
nuclear@10 66
nuclear@10 67 /* C 4D vector functions */
nuclear@10 68 static inline vec4_t v4_cons(scalar_t x, scalar_t y, scalar_t z, scalar_t w);
nuclear@10 69 static inline void v4_print(FILE *fp, vec4_t v);
nuclear@10 70
nuclear@10 71 static inline vec4_t v4_add(vec4_t v1, vec4_t v2);
nuclear@10 72 static inline vec4_t v4_sub(vec4_t v1, vec4_t v2);
nuclear@10 73 static inline vec4_t v4_neg(vec4_t v);
nuclear@10 74 static inline vec4_t v4_mul(vec4_t v1, vec4_t v2);
nuclear@10 75 static inline vec4_t v4_scale(vec4_t v, scalar_t s);
nuclear@10 76 static inline scalar_t v4_dot(vec4_t v1, vec4_t v2);
nuclear@10 77 static inline scalar_t v4_length(vec4_t v);
nuclear@10 78 static inline scalar_t v4_length_sq(vec4_t v);
nuclear@10 79 static inline vec4_t v4_normalize(vec4_t v);
nuclear@10 80 static inline vec4_t v4_transform(vec4_t v, mat4_t m);
nuclear@10 81
nuclear@10 82 #ifdef __cplusplus
nuclear@10 83 } /* extern "C" */
nuclear@10 84
nuclear@10 85 /* when included from C++ source files, also define the vector classes */
nuclear@10 86
nuclear@10 87 /** 2D Vector */
nuclear@10 88 class Vector2 {
nuclear@10 89 public:
nuclear@10 90 scalar_t x, y;
nuclear@10 91
nuclear@10 92 Vector2(scalar_t x = 0.0, scalar_t y = 0.0);
nuclear@10 93 Vector2(const vec2_t &vec);
nuclear@10 94 Vector2(const Vector3 &vec);
nuclear@10 95 Vector2(const Vector4 &vec);
nuclear@10 96
nuclear@10 97 inline scalar_t &operator [](int elem);
nuclear@10 98 inline const scalar_t &operator [](int elem) const;
nuclear@10 99
nuclear@10 100 inline scalar_t length() const;
nuclear@10 101 inline scalar_t length_sq() const;
nuclear@10 102 void normalize();
nuclear@10 103 Vector2 normalized() const;
nuclear@10 104
nuclear@10 105 void transform(const Matrix3x3 &mat);
nuclear@10 106 Vector2 transformed(const Matrix3x3 &mat) const;
nuclear@10 107
nuclear@10 108 void rotate(scalar_t angle);
nuclear@10 109 Vector2 rotated(scalar_t angle) const;
nuclear@10 110
nuclear@10 111 Vector2 reflection(const Vector2 &normal) const;
nuclear@10 112 Vector2 refraction(const Vector2 &normal, scalar_t src_ior, scalar_t dst_ior) const;
nuclear@10 113 };
nuclear@10 114
nuclear@10 115 /* unary operations */
nuclear@10 116 inline Vector2 operator -(const Vector2 &vec);
nuclear@10 117
nuclear@10 118 /* binary vector (op) vector operations */
nuclear@10 119 inline scalar_t dot_product(const Vector2 &v1, const Vector2 &v2);
nuclear@10 120
nuclear@10 121 inline Vector2 operator +(const Vector2 &v1, const Vector2 &v2);
nuclear@10 122 inline Vector2 operator -(const Vector2 &v1, const Vector2 &v2);
nuclear@10 123 inline Vector2 operator *(const Vector2 &v1, const Vector2 &v2);
nuclear@10 124 inline Vector2 operator /(const Vector2 &v1, const Vector2 &v2);
nuclear@10 125 inline bool operator ==(const Vector2 &v1, const Vector2 &v2);
nuclear@10 126
nuclear@10 127 inline void operator +=(Vector2 &v1, const Vector2 &v2);
nuclear@10 128 inline void operator -=(Vector2 &v1, const Vector2 &v2);
nuclear@10 129 inline void operator *=(Vector2 &v1, const Vector2 &v2);
nuclear@10 130 inline void operator /=(Vector2 &v1, const Vector2 &v2);
nuclear@10 131
nuclear@10 132 /* binary vector (op) scalar and scalar (op) vector operations */
nuclear@10 133 inline Vector2 operator +(const Vector2 &vec, scalar_t scalar);
nuclear@10 134 inline Vector2 operator +(scalar_t scalar, const Vector2 &vec);
nuclear@10 135 inline Vector2 operator -(const Vector2 &vec, scalar_t scalar);
nuclear@10 136 inline Vector2 operator *(const Vector2 &vec, scalar_t scalar);
nuclear@10 137 inline Vector2 operator *(scalar_t scalar, const Vector2 &vec);
nuclear@10 138 inline Vector2 operator /(const Vector2 &vec, scalar_t scalar);
nuclear@10 139
nuclear@10 140 inline void operator +=(Vector2 &vec, scalar_t scalar);
nuclear@10 141 inline void operator -=(Vector2 &vec, scalar_t scalar);
nuclear@10 142 inline void operator *=(Vector2 &vec, scalar_t scalar);
nuclear@10 143 inline void operator /=(Vector2 &vec, scalar_t scalar);
nuclear@10 144
nuclear@10 145
nuclear@10 146 inline Vector2 lerp(const Vector2 &a, const Vector2 &b, scalar_t t);
nuclear@10 147 inline Vector2 catmull_rom_spline(const Vector2 &v0, const Vector2 &v1,
nuclear@10 148 const Vector2 &v2, const Vector2 &v3, scalar_t t);
nuclear@10 149 inline Vector2 bspline(const Vector2 &v0, const Vector2 &v1,
nuclear@10 150 const Vector2 &v2, const Vector2 &v3, scalar_t t);
nuclear@10 151
nuclear@10 152 /* 3D Vector */
nuclear@10 153 class Vector3 {
nuclear@10 154 public:
nuclear@10 155 scalar_t x, y, z;
nuclear@10 156
nuclear@10 157 Vector3(scalar_t x = 0.0, scalar_t y = 0.0, scalar_t z = 0.0);
nuclear@10 158 Vector3(const vec3_t &vec);
nuclear@10 159 Vector3(const Vector2 &vec);
nuclear@10 160 Vector3(const Vector4 &vec);
nuclear@10 161 Vector3(const SphVector &sph);
nuclear@10 162
nuclear@10 163 Vector3 &operator =(const SphVector &sph);
nuclear@10 164
nuclear@10 165 inline scalar_t &operator [](int elem);
nuclear@10 166 inline const scalar_t &operator [](int elem) const;
nuclear@10 167
nuclear@10 168 inline scalar_t length() const;
nuclear@10 169 inline scalar_t length_sq() const;
nuclear@10 170 void normalize();
nuclear@10 171 Vector3 normalized() const;
nuclear@10 172
nuclear@10 173 void transform(const Matrix3x3 &mat);
nuclear@10 174 Vector3 transformed(const Matrix3x3 &mat) const;
nuclear@10 175 void transform(const Matrix4x4 &mat);
nuclear@10 176 Vector3 transformed(const Matrix4x4 &mat) const;
nuclear@10 177 void transform(const Quaternion &quat);
nuclear@10 178 Vector3 transformed(const Quaternion &quat) const;
nuclear@10 179
nuclear@10 180 void rotate(const Vector3 &euler);
nuclear@10 181 Vector3 rotated(const Vector3 &euler) const;
nuclear@10 182
nuclear@10 183 Vector3 reflection(const Vector3 &normal) const;
nuclear@10 184 Vector3 refraction(const Vector3 &normal, scalar_t src_ior, scalar_t dst_ior) const;
nuclear@10 185 Vector3 refraction(const Vector3 &normal, scalar_t ior) const;
nuclear@10 186 };
nuclear@10 187
nuclear@10 188 /* unary operations */
nuclear@10 189 inline Vector3 operator -(const Vector3 &vec);
nuclear@10 190
nuclear@10 191 /* binary vector (op) vector operations */
nuclear@10 192 inline scalar_t dot_product(const Vector3 &v1, const Vector3 &v2);
nuclear@10 193 inline Vector3 cross_product(const Vector3 &v1, const Vector3 &v2);
nuclear@10 194
nuclear@10 195 inline Vector3 operator +(const Vector3 &v1, const Vector3 &v2);
nuclear@10 196 inline Vector3 operator -(const Vector3 &v1, const Vector3 &v2);
nuclear@10 197 inline Vector3 operator *(const Vector3 &v1, const Vector3 &v2);
nuclear@10 198 inline Vector3 operator /(const Vector3 &v1, const Vector3 &v2);
nuclear@10 199 inline bool operator ==(const Vector3 &v1, const Vector3 &v2);
nuclear@10 200
nuclear@10 201 inline void operator +=(Vector3 &v1, const Vector3 &v2);
nuclear@10 202 inline void operator -=(Vector3 &v1, const Vector3 &v2);
nuclear@10 203 inline void operator *=(Vector3 &v1, const Vector3 &v2);
nuclear@10 204 inline void operator /=(Vector3 &v1, const Vector3 &v2);
nuclear@10 205
nuclear@10 206 /* binary vector (op) scalar and scalar (op) vector operations */
nuclear@10 207 inline Vector3 operator +(const Vector3 &vec, scalar_t scalar);
nuclear@10 208 inline Vector3 operator +(scalar_t scalar, const Vector3 &vec);
nuclear@10 209 inline Vector3 operator -(const Vector3 &vec, scalar_t scalar);
nuclear@10 210 inline Vector3 operator *(const Vector3 &vec, scalar_t scalar);
nuclear@10 211 inline Vector3 operator *(scalar_t scalar, const Vector3 &vec);
nuclear@10 212 inline Vector3 operator /(const Vector3 &vec, scalar_t scalar);
nuclear@10 213
nuclear@10 214 inline void operator +=(Vector3 &vec, scalar_t scalar);
nuclear@10 215 inline void operator -=(Vector3 &vec, scalar_t scalar);
nuclear@10 216 inline void operator *=(Vector3 &vec, scalar_t scalar);
nuclear@10 217 inline void operator /=(Vector3 &vec, scalar_t scalar);
nuclear@10 218
nuclear@10 219
nuclear@10 220 inline Vector3 lerp(const Vector3 &a, const Vector3 &b, scalar_t t);
nuclear@10 221 inline Vector3 catmull_rom_spline(const Vector3 &v0, const Vector3 &v1,
nuclear@10 222 const Vector3 &v2, const Vector3 &v3, scalar_t t);
nuclear@10 223 inline Vector3 bspline(const Vector3 &v0, const Vector3 &v1,
nuclear@10 224 const Vector3 &v2, const Vector3 &v3, scalar_t t);
nuclear@10 225
nuclear@10 226 /* 4D Vector */
nuclear@10 227 class Vector4 {
nuclear@10 228 public:
nuclear@10 229 scalar_t x, y, z, w;
nuclear@10 230
nuclear@10 231 Vector4(scalar_t x = 0.0, scalar_t y = 0.0, scalar_t z = 0.0, scalar_t w = 0.0);
nuclear@10 232 Vector4(const vec4_t &vec);
nuclear@10 233 Vector4(const Vector2 &vec);
nuclear@10 234 Vector4(const Vector3 &vec);
nuclear@10 235
nuclear@10 236 inline scalar_t &operator [](int elem);
nuclear@10 237 inline const scalar_t &operator [](int elem) const;
nuclear@10 238
nuclear@10 239 inline scalar_t length() const;
nuclear@10 240 inline scalar_t length_sq() const;
nuclear@10 241 void normalize();
nuclear@10 242 Vector4 normalized() const;
nuclear@10 243
nuclear@10 244 void transform(const Matrix4x4 &mat);
nuclear@10 245 Vector4 transformed(const Matrix4x4 &mat) const;
nuclear@10 246
nuclear@10 247 Vector4 reflection(const Vector4 &normal) const;
nuclear@10 248 Vector4 refraction(const Vector4 &normal, scalar_t src_ior, scalar_t dst_ior) const;
nuclear@10 249 };
nuclear@10 250
nuclear@10 251
nuclear@10 252 /* unary operations */
nuclear@10 253 inline Vector4 operator -(const Vector4 &vec);
nuclear@10 254
nuclear@10 255 /* binary vector (op) vector operations */
nuclear@10 256 inline scalar_t dot_product(const Vector4 &v1, const Vector4 &v2);
nuclear@10 257 inline Vector4 cross_product(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3);
nuclear@10 258
nuclear@10 259 inline Vector4 operator +(const Vector4 &v1, const Vector4 &v2);
nuclear@10 260 inline Vector4 operator -(const Vector4 &v1, const Vector4 &v2);
nuclear@10 261 inline Vector4 operator *(const Vector4 &v1, const Vector4 &v2);
nuclear@10 262 inline Vector4 operator /(const Vector4 &v1, const Vector4 &v2);
nuclear@10 263 inline bool operator ==(const Vector4 &v1, const Vector4 &v2);
nuclear@10 264
nuclear@10 265 inline void operator +=(Vector4 &v1, const Vector4 &v2);
nuclear@10 266 inline void operator -=(Vector4 &v1, const Vector4 &v2);
nuclear@10 267 inline void operator *=(Vector4 &v1, const Vector4 &v2);
nuclear@10 268 inline void operator /=(Vector4 &v1, const Vector4 &v2);
nuclear@10 269
nuclear@10 270 /* binary vector (op) scalar and scalar (op) vector operations */
nuclear@10 271 inline Vector4 operator +(const Vector4 &vec, scalar_t scalar);
nuclear@10 272 inline Vector4 operator +(scalar_t scalar, const Vector4 &vec);
nuclear@10 273 inline Vector4 operator -(const Vector4 &vec, scalar_t scalar);
nuclear@10 274 inline Vector4 operator *(const Vector4 &vec, scalar_t scalar);
nuclear@10 275 inline Vector4 operator *(scalar_t scalar, const Vector4 &vec);
nuclear@10 276 inline Vector4 operator /(const Vector4 &vec, scalar_t scalar);
nuclear@10 277
nuclear@10 278 inline void operator +=(Vector4 &vec, scalar_t scalar);
nuclear@10 279 inline void operator -=(Vector4 &vec, scalar_t scalar);
nuclear@10 280 inline void operator *=(Vector4 &vec, scalar_t scalar);
nuclear@10 281 inline void operator /=(Vector4 &vec, scalar_t scalar);
nuclear@10 282
nuclear@10 283
nuclear@10 284 inline Vector4 lerp(const Vector4 &v0, const Vector4 &v1, scalar_t t);
nuclear@10 285 inline Vector4 catmull_rom_spline(const Vector4 &v0, const Vector4 &v1,
nuclear@10 286 const Vector4 &v2, const Vector4 &v3, scalar_t t);
nuclear@10 287 inline Vector4 bspline(const Vector4 &v0, const Vector4 &v1,
nuclear@10 288 const Vector4 &v2, const Vector4 &v3, scalar_t t);
nuclear@10 289
nuclear@10 290 #endif /* __cplusplus */
nuclear@10 291
nuclear@10 292 #include "vector.inl"
nuclear@10 293
nuclear@10 294 #endif /* VMATH_VECTOR_H_ */