dbf-halloween2015

view libs/vmath/vector.h @ 3:c37fe5d8a4ed

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