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