goat3d

annotate libs/vmath/vector.h @ 43:1dcbe87b6a5d

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