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