Skip to content
Newer
Older
100644 463 lines (354 sloc) 10.2 KB
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
1 // MathGP Library
2 // Copyright (c) 2012 Borislav Stanimirov
f8838f6 @iboB * added a gcc code blocks project for the test
authored
3 //
4 // See the LICENSE.txt file, included in this
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
5 // distribution for details about the copyright
192b3f3 @iboB * added descriptions to the headers
authored
6
7 // vector.h - definition of the vector classes
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
8 #pragma once
9
f8838f6 @iboB * added a gcc code blocks project for the test
authored
10 #include "constants.h"
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
11 #include "ntuple.h"
71dad21 @iboB Quaternion almost complete. More ntuple functions
authored
12 #include "fwd.h"
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
13
4b0cb7e @iboB * Removed old yamgp references
authored
14 namespace mathgp
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
15 {
16 namespace _internal
17 {
18
19 template <size_t _n, typename _type, typename _this_type>
20 class vectornt : public ntuple<_n, _type, _this_type>
21 {
f8838f6 @iboB * added a gcc code blocks project for the test
authored
22 //typedef ntuple<_n, _type, _this_type> parent;
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
23 public:
f8838f6 @iboB * added a gcc code blocks project for the test
authored
24 static const size_t dimension = _n;
408fba9 @iboB more code dumped
authored
25
2ad4984 @iboB * more tests
authored
26 _type length_sq() const
408fba9 @iboB more code dumped
authored
27 {
2ad4984 @iboB * more tests
authored
28 _type result = 0;
408fba9 @iboB more code dumped
authored
29
30 MATHGP_FOR_VALUES(i)
f8838f6 @iboB * added a gcc code blocks project for the test
authored
31 result += sq(this->at(i));
408fba9 @iboB more code dumped
authored
32
33 return result;
34 }
35
2ad4984 @iboB * more tests
authored
36 _type length() const
408fba9 @iboB more code dumped
authored
37 {
38 return std::sqrt(length_sq());
39 }
40
ba2dbbe @iboB Fixed swizzle compilation errors
authored
41 _this_type& normalize()
408fba9 @iboB more code dumped
authored
42 {
3d8e441 @iboB Added assertions to the library (and the ability to disable them in d…
authored
43 MATHGP_ASSERT3(!::mathgp::close(length(), _type(0)), "normalizing a zero-length vector");
ba2dbbe @iboB Fixed swizzle compilation errors
authored
44 return this->as_this_type() /= length();
408fba9 @iboB more code dumped
authored
45 }
a39a85f @iboB Added basic transformation functionality
authored
46
7434fd2 @iboB More small functions
authored
47 bool is_normalized() const
48 {
49 return ::mathgp::close(length(), _type(1));
50 }
51
ba2dbbe @iboB Fixed swizzle compilation errors
authored
52 _this_type& homogenous_normalize()
a39a85f @iboB Added basic transformation functionality
authored
53 {
54 static_assert(dimension > 1, "you need at least two homogenous coordinates");
3d8e441 @iboB Added assertions to the library (and the ability to disable them in d…
authored
55 MATHGP_ASSERT3(!::mathgp::close(this->back(), _type(0)), "homogenous normalize with a zero homogenous coordinate");
a39a85f @iboB Added basic transformation functionality
authored
56
ba2dbbe @iboB Fixed swizzle compilation errors
authored
57 return this->as_this_type() /= this->back();
a39a85f @iboB Added basic transformation functionality
authored
58 }
7434fd2 @iboB More small functions
authored
59
60 _this_type get_reflection(const vectornt<_n, _type, _this_type>& normal) const
61 {
62 MATHGP_ASSERT3(normal.is_normalized(), "reflecting with a non-normalized normal");
63 return *this - _type(2) * dot(*this, normal) * normal;
64 }
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
65 };
66
f8838f6 @iboB * added a gcc code blocks project for the test
authored
67 template <size_t _n, typename _type, typename _this_type>
68 _type dot(const vectornt<_n, _type, _this_type>& a, const vectornt<_n, _type, _this_type>& b)
408fba9 @iboB more code dumped
authored
69 {
70 _type result = 0;
71
72 MATHGP_FOR_VALUES(i)
73 result += a.at(i) * b.at(i);
74
75 return result;
76 }
77
78 template <size_t _dim, typename _type, typename _this_type>
79 bool orthogonal(const vectornt<_dim, _type, _this_type>& a, const vectornt<_dim, _type, _this_type>& b)
80 {
71dad21 @iboB Quaternion almost complete. More ntuple functions
authored
81 return close(dot(a, b), 0);
408fba9 @iboB more code dumped
authored
82 }
83
71dad21 @iboB Quaternion almost complete. More ntuple functions
authored
84 template <size_t _dim, typename _type, typename _this_type>
85 bool colinear(const vectornt<_dim, _type, _this_type>& a, const vectornt<_dim, _type, _this_type>& b)
86 {
87 // there are many ways to do this, but to save sqrt calls let's try this
88 vectornt<_dim, _type, _this_type> adb = div(a, b);
89
90 return close(adb.x(), adb.y()) && close(adb.x(), adb.z());
91 }
92
93
f8838f6 @iboB * added a gcc code blocks project for the test
authored
94 template <size_t _n, typename _type, typename _this_type>
95 _this_type normalized(const vectornt<_n, _type, _this_type>& a)
408fba9 @iboB more code dumped
authored
96 {
97 _type length = a.length();
98
3d8e441 @iboB Added assertions to the library (and the ability to disable them in d…
authored
99 MATHGP_ASSERT3(!::mathgp::close(length, _type(0)), "normalizing a zero-length vector");
100
408fba9 @iboB more code dumped
authored
101 _this_type ret;
102
103 MATHGP_EACH_OF(ret) = a.at(i) / length;
104
105 return ret;
106 }
107
7434fd2 @iboB More small functions
authored
108 template <size_t _dim, typename _type, typename _this_type>
109 _type distance_sq(const vectornt<_dim, _type, _this_type>& a, const vectornt<_dim, _type, _this_type>& b)
110 {
111 return (b-a).length_sq();
112 }
113
114 template <size_t _dim, typename _type, typename _this_type>
115 _type distance(const vectornt<_dim, _type, _this_type>& a, const vectornt<_dim, _type, _this_type>& b)
116 {
117 return (b-a).length();
118 }
119
120
4b0cb7e @iboB * Removed old yamgp references
authored
121 #if !defined(MATHGP_DISABLE_SWIZZLE)
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
122
123 template <typename _type>
124 class wrap_ref
125 {
126 public:
127 operator _type&()
128 {
129 return *ref;
130 }
131
132 operator const _type&() const
133 {
134 return *ref;
135 }
136
137 _type* ref;
138 };
139
140
141 template <size_t _n, typename _type, typename _this_type>
142 class swizzle_vectornt : public vectornt<_n, wrap_ref<_type>, _this_type>
143 {
144 public:
f8838f6 @iboB * added a gcc code blocks project for the test
authored
145
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
146 operator _this_type() const
147 {
148 _this_type ret;
149
f8838f6 @iboB * added a gcc code blocks project for the test
authored
150 MATHGP_EACH_OF(ret) = this->at(i);
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
151
152 return ret;
153 }
154
155 // intentionally void
156 // it breaks a=b=c, but gets rid of unwanted temporary objects
f8838f6 @iboB * added a gcc code blocks project for the test
authored
157 void operator=(const vectornt<_n, _type, _this_type>& b)
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
158 {
f8838f6 @iboB * added a gcc code blocks project for the test
authored
159 for(size_t i=0; i<_n; ++i)
160 *this->at(i).ref = b.at(i);
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
161 }
162
163 private:
164 ////////////////////////////////////////////////////////
165 // unsafe operators to be hidden
166 };
167
4b0cb7e @iboB * Removed old yamgp references
authored
168 #endif // #if !defined(MATHGP_DISABLE_SWIZZLE)
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
169
4b0cb7e @iboB * Removed old yamgp references
authored
170 } // namespace _internal
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
171
172 template <typename _type>
173 class vector1t : public _internal::vectornt<1, _type, vector1t<_type>>
174 {
175 public:
176 ////////////////////////////////////////////////////////
177 // named constructors
178
2ad4984 @iboB * more tests
authored
179 static vector1t coord(_type x)
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
180 {
181 vector1t ret;
182
183 ret.x() = x;
184
185 return ret;
186 }
187
4b0cb7e @iboB * Removed old yamgp references
authored
188 #include "vector1_swizzle.inl"
189
190 #if !defined(MATHGP_DISABLE_SWIZZLE)
191 # include "vector1_swizzle_declare.inl" // generated file
192 #endif
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
193
194 };
195
196 template <typename _type>
408fba9 @iboB more code dumped
authored
197 vector1t<_type> v(const _type& x)
198 {
199 return vector1t<_type>::coord(x);
200 }
201
202 template <typename _type>
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
203 class vector2t : public _internal::vectornt<2, _type, vector2t<_type>>
204 {
205 public:
206 ////////////////////////////////////////////////////////
207 // named constructors
208
2ad4984 @iboB * more tests
authored
209 static vector2t coord(_type x, _type y)
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
210 {
211 vector2t ret;
212
213 ret.x() = x;
214 ret.y() = y;
215
216 return ret;
217 }
218
408fba9 @iboB more code dumped
authored
219 ////////////////////////////////////////////////////////
220 // functions
221
222 vector2t get_orthogonal() const
223 {
3d8e441 @iboB Added assertions to the library (and the ability to disable them in d…
authored
224 MATHGP_ASSERT3(!close(*this, vector2t::zero()), "finding an orthogonal of a zero vector");
f8838f6 @iboB * added a gcc code blocks project for the test
authored
225 return vector2t::coord(-y(), x());
408fba9 @iboB more code dumped
authored
226 }
227
4b0cb7e @iboB * Removed old yamgp references
authored
228 #include "vector1_swizzle.inl"
229 #include "vector2_swizzle.inl"
230
231 #if !defined(MATHGP_DISABLE_SWIZZLE)
232 # include "vector2_swizzle_declare.inl" // generated file
233 #endif
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
234
235 };
236
237 template <typename _type>
408fba9 @iboB more code dumped
authored
238 vector2t<_type> v(const _type& x, const _type& y)
239 {
240 return vector2t<_type>::coord(x, y);
241 }
242
243 template <typename _type>
244 _type cross_magnitude(const vector2t<_type>& a, const vector2t<_type>& b)
245 {
246 return a.x*b.y - a.y*b.x;
247 }
248
249 template <typename _type>
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
250 class vector3t : public _internal::vectornt<3, _type, vector3t<_type>>
251 {
252 public:
253 ////////////////////////////////////////////////////////
254 // named constructors
255
2ad4984 @iboB * more tests
authored
256 static vector3t coord(_type x, _type y, _type z)
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
257 {
258 vector3t ret;
259
260 ret.x() = x;
261 ret.y() = y;
262 ret.z() = z;
263
264 return ret;
265 }
266
408fba9 @iboB more code dumped
authored
267 ////////////////////////////////////////////////////////
268 // functions
e3c3577 @iboB Added an accidentally removed default argument
authored
269 vector3t& change_handedness()
270 {
271 flip_sign(z());
272 return *this;
273 }
408fba9 @iboB more code dumped
authored
274
275 vector3t get_orthogonal() const
276 {
277 size_t non_zeros = 0;
278 size_t non_zero_index = 0;
279
280 for (size_t i=0; i<3; ++i)
281 {
f8838f6 @iboB * added a gcc code blocks project for the test
authored
282 if (std::abs(this->at(i)) > constants<_type>::EPSILON())
408fba9 @iboB more code dumped
authored
283 {
284 ++non_zeros;
285 non_zero_index = i;
286 }
287 }
288
3d8e441 @iboB Added assertions to the library (and the ability to disable them in d…
authored
289 MATHGP_ASSERT3(non_zeros, "finding an orthogonal of a zero vector");
290
408fba9 @iboB more code dumped
authored
291 if (non_zeros >= 2)
292 {
f8838f6 @iboB * added a gcc code blocks project for the test
authored
293 return vector3t::coord(y() * z() / 2, x() * z() / 2, -x() * y());
408fba9 @iboB more code dumped
authored
294 }
295 else
296 {
297 vector3t ret = vector3t::zero();
298
f8838f6 @iboB * added a gcc code blocks project for the test
authored
299 ret.at((non_zero_index + 1) % 3) = this->at(non_zero_index);
408fba9 @iboB more code dumped
authored
300 return ret;
301 }
302 }
303
4b0cb7e @iboB * Removed old yamgp references
authored
304 #include "vector1_swizzle.inl"
305 #include "vector2_swizzle.inl"
306 #include "vector3_swizzle.inl"
307
308 #if !defined(MATHGP_DISABLE_SWIZZLE)
309 # include "vector3_swizzle_declare.inl" // generated file
310 #endif
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
311
312 };
313
4b0cb7e @iboB * Removed old yamgp references
authored
314 template <typename _type>
408fba9 @iboB more code dumped
authored
315 vector3t<_type> v(const _type& x, const _type& y, const _type& z)
316 {
317 return vector3t<_type>::coord(x, y, z);
318 }
319
320 template <typename _type>
321 vector3t<_type> cross(const vector3t<_type>& a, const vector3t<_type>& b)
322 {
3d8e441 @iboB Added assertions to the library (and the ability to disable them in d…
authored
323 MATHGP_ASSERT3(!close(a, vector3t<_type>::zero()), "cross product with a zero vector");
324 MATHGP_ASSERT3(!close(b, vector3t<_type>::zero()), "cross product with a zero vector");
325
408fba9 @iboB more code dumped
authored
326 return vector3t<_type>::coord(
327 a.y()*b.z() - a.z()*b.y(),
328 a.z()*b.x() - a.x()*b.z(),
329 a.x()*b.y() - a.y()*b.x()
330 );
331 }
332
333 template <typename _type>
4b0cb7e @iboB * Removed old yamgp references
authored
334 class vector4t : public _internal::vectornt<4, _type, vector4t<_type>>
335 {
336 public:
337 ////////////////////////////////////////////////////////
338 // named constructors
339
2ad4984 @iboB * more tests
authored
340 static vector4t coord(_type x, _type y, _type z, _type w)
4b0cb7e @iboB * Removed old yamgp references
authored
341 {
342 vector4t ret;
343
344 ret.x() = x;
345 ret.y() = y;
346 ret.z() = z;
347 ret.w() = w;
348
349 return ret;
350 }
351
a39a85f @iboB Added basic transformation functionality
authored
352 static vector4t coord(const vector3t<_type>& v3, _type w)
353 {
354 return coord(v3.x(), v3.y(), v3.z(), w);
355 }
356
4b0cb7e @iboB * Removed old yamgp references
authored
357 #include "vector1_swizzle.inl"
358 #include "vector2_swizzle.inl"
359 #include "vector3_swizzle.inl"
360 #include "vector4_swizzle.inl"
361
362 #if !defined(MATHGP_DISABLE_SWIZZLE)
363 # include "vector4_swizzle_declare.inl" // generated file
364 #endif
365
366 };
367
408fba9 @iboB more code dumped
authored
368 template <typename _type>
369 vector4t<_type> v(const _type& x, const _type& y, const _type& z, const _type& w)
370 {
371 return vector4t<_type>::coord(x, y, z, w);
372 }
373
4b0cb7e @iboB * Removed old yamgp references
authored
374
375 #if !defined(MATHGP_DISABLE_SWIZZLE)
376
377 namespace _internal
378 {
379
380 template <typename _type>
381 swizzle_vectornt<2, _type, vector2t<_type>> swizzle2(_type& x, _type& y)
382 {
383 swizzle_vectornt<2, _type, vector2t<_type>> ret;
384
385 ret.at(0).ref = &x;
386 ret.at(1).ref = &y;
387
388 return ret;
389 }
390
391 template <typename _type>
392 swizzle_vectornt<3, _type, vector3t<_type>> swizzle3(_type& x, _type& y, _type& z)
393 {
394 swizzle_vectornt<3, _type, vector3t<_type>> ret;
395
396 ret.at(0).ref = &x;
397 ret.at(1).ref = &y;
398 ret.at(2).ref = &z;
399
400 return ret;
401 }
402
403 template <typename _type>
404 swizzle_vectornt<4, _type, vector4t<_type>> swizzle4(_type& x, _type& y, _type& z, _type& w)
405 {
406 swizzle_vectornt<4, _type, vector4t<_type>> ret;
407
408 ret.at(0).ref = &x;
409 ret.at(1).ref = &y;
410 ret.at(2).ref = &z;
411 ret.at(3).ref = &w;
412
413 return ret;
414 }
415
416 } // namespace internal
417
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
418
4b0cb7e @iboB * Removed old yamgp references
authored
419 # include "vector1_swizzle_define.inl"
420 # include "vector2_swizzle_define.inl"
421 # include "vector3_swizzle_define.inl"
422 # include "vector4_swizzle_define.inl"
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
423
4b0cb7e @iboB * Removed old yamgp references
authored
424 #endif // #if !defined(MATHGP_DISABLE_SWIZZLE)
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
425
408fba9 @iboB more code dumped
authored
426 template <size_t _dim, typename _type>
427 class vectorntx : public _internal::vectornt<_dim, _type, vectorntx<_dim, _type>>
428 {
429 public:
430 };
431
432 template <size_t _dim, typename _type>
7e4afc5 @iboB * initial unit tests with boost::unit_test
authored
433 struct vector_space
408fba9 @iboB more code dumped
authored
434 {
435 typedef vectorntx<_dim, _type> vector;
436 };
437
438 template <typename _type>
f8838f6 @iboB * added a gcc code blocks project for the test
authored
439 struct vector_space<1, _type>
408fba9 @iboB more code dumped
authored
440 {
441 typedef vector1t<_type> vector;
442 };
443
444 template <typename _type>
f8838f6 @iboB * added a gcc code blocks project for the test
authored
445 struct vector_space<2, _type>
408fba9 @iboB more code dumped
authored
446 {
447 typedef vector2t<_type> vector;
448 };
449
450 template <typename _type>
f8838f6 @iboB * added a gcc code blocks project for the test
authored
451 struct vector_space<3, _type>
408fba9 @iboB more code dumped
authored
452 {
453 typedef vector3t<_type> vector;
454 };
455
456 template <typename _type>
f8838f6 @iboB * added a gcc code blocks project for the test
authored
457 struct vector_space<4, _type>
408fba9 @iboB more code dumped
authored
458 {
459 typedef vector4t<_type> vector;
460 };
9d4b880 @iboB Initial commit, before switching to generated swizzle files
authored
461
f8838f6 @iboB * added a gcc code blocks project for the test
authored
462 } // namespace mathgp
Something went wrong with that request. Please try again.