]>
Commit | Line | Data |
---|---|---|
1e59de90 | 1 | // Copyright 2008-2022 Emil Dotchevski and Reverge Studios, Inc. |
92f5a8d4 | 2 | |
1e59de90 TL |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
92f5a8d4 | 5 | |
1e59de90 TL |
6 | #ifdef BOOST_QVM_TEST_SINGLE_HEADER |
7 | # include BOOST_QVM_TEST_SINGLE_HEADER | |
8 | #else | |
9 | # include <boost/qvm/operations.hpp> | |
10 | # include <boost/qvm/quat.hpp> | |
11 | # include <boost/qvm/vec.hpp> | |
12 | # include <boost/qvm/mat.hpp> | |
13 | #endif | |
92f5a8d4 TL |
14 | |
15 | namespace | |
16 | my_stuff | |
17 | { | |
18 | struct | |
19 | mat | |
20 | { | |
21 | float a[3][3]; | |
22 | }; | |
23 | ||
24 | struct | |
25 | vec | |
26 | { | |
27 | float a[3]; | |
28 | }; | |
29 | ||
30 | struct | |
31 | quat | |
32 | { | |
33 | float a[4]; | |
34 | }; | |
35 | } | |
36 | ||
37 | namespace | |
38 | boost | |
39 | { | |
40 | namespace | |
41 | qvm | |
42 | { | |
43 | template <> | |
44 | struct | |
45 | mat_traits<my_stuff::mat> | |
46 | { | |
47 | typedef float scalar_type; | |
48 | static int const rows=3; | |
49 | static int const cols=3; | |
50 | ||
51 | template <int R,int C> | |
52 | static | |
53 | scalar_type & | |
54 | write_element( my_stuff::mat & m ) | |
55 | { | |
56 | BOOST_QVM_STATIC_ASSERT(R>=0); | |
57 | BOOST_QVM_STATIC_ASSERT(R<rows); | |
58 | BOOST_QVM_STATIC_ASSERT(C>=0); | |
59 | BOOST_QVM_STATIC_ASSERT(C<cols); | |
60 | return m.a[R][C]; | |
61 | } | |
62 | ||
63 | template <int R,int C> | |
64 | static | |
65 | scalar_type | |
66 | read_element( my_stuff::mat const & m ) | |
67 | { | |
68 | BOOST_QVM_STATIC_ASSERT(R>=0); | |
69 | BOOST_QVM_STATIC_ASSERT(R<rows); | |
70 | BOOST_QVM_STATIC_ASSERT(C>=0); | |
71 | BOOST_QVM_STATIC_ASSERT(C<cols); | |
72 | return m.a[R][C]; | |
73 | } | |
74 | ||
75 | static | |
76 | inline | |
77 | scalar_type & | |
78 | write_element_idx( int r, int c, my_stuff::mat & m ) | |
79 | { | |
80 | BOOST_QVM_ASSERT(r>=0); | |
81 | BOOST_QVM_ASSERT(r<rows); | |
82 | BOOST_QVM_ASSERT(c>=0); | |
83 | BOOST_QVM_ASSERT(c<cols); | |
84 | return m.a[r][c]; | |
85 | } | |
86 | ||
87 | static | |
88 | inline | |
89 | scalar_type | |
90 | read_element_idx( int r, int c, my_stuff::mat const & m ) | |
91 | { | |
92 | BOOST_QVM_ASSERT(r>=0); | |
93 | BOOST_QVM_ASSERT(r<rows); | |
94 | BOOST_QVM_ASSERT(c>=0); | |
95 | BOOST_QVM_ASSERT(c<cols); | |
96 | return m.a[r][c]; | |
97 | } | |
98 | }; | |
99 | ||
100 | template <> | |
101 | struct | |
102 | vec_traits<my_stuff::vec> | |
103 | { | |
104 | static int const dim=3; | |
105 | typedef float scalar_type; | |
106 | ||
107 | template <int I> | |
108 | static | |
109 | scalar_type & | |
110 | write_element( my_stuff::vec & m ) | |
111 | { | |
112 | BOOST_QVM_STATIC_ASSERT(I>=0); | |
113 | BOOST_QVM_STATIC_ASSERT(I<dim); | |
114 | return m.a[I]; | |
115 | } | |
116 | ||
117 | template <int I> | |
118 | static | |
119 | scalar_type | |
120 | read_element( my_stuff::vec const & m ) | |
121 | { | |
122 | BOOST_QVM_STATIC_ASSERT(I>=0); | |
123 | BOOST_QVM_STATIC_ASSERT(I<dim); | |
124 | return m.a[I]; | |
125 | } | |
126 | ||
127 | static | |
128 | inline | |
129 | scalar_type & | |
130 | write_element_idx( int i, my_stuff::vec & m ) | |
131 | { | |
132 | BOOST_QVM_ASSERT(i>=0); | |
133 | BOOST_QVM_ASSERT(i<dim); | |
134 | return m.a[i]; | |
135 | } | |
136 | ||
137 | static | |
138 | inline | |
139 | scalar_type | |
140 | read_element_idx( int i, my_stuff::vec const & m ) | |
141 | { | |
142 | BOOST_QVM_ASSERT(i>=0); | |
143 | BOOST_QVM_ASSERT(i<dim); | |
144 | return m.a[i]; | |
145 | } | |
146 | }; | |
147 | ||
148 | template <> | |
149 | struct | |
150 | quat_traits<my_stuff::quat> | |
151 | { | |
152 | typedef float scalar_type; | |
153 | ||
154 | template <int I> | |
155 | static | |
156 | scalar_type & | |
157 | write_element( my_stuff::quat & m ) | |
158 | { | |
159 | BOOST_QVM_STATIC_ASSERT(I>=0); | |
160 | BOOST_QVM_STATIC_ASSERT(I<4); | |
161 | return m.a[I]; | |
162 | } | |
163 | ||
164 | template <int I> | |
165 | static | |
166 | scalar_type | |
167 | read_element( my_stuff::quat const & m ) | |
168 | { | |
169 | BOOST_QVM_STATIC_ASSERT(I>=0); | |
170 | BOOST_QVM_STATIC_ASSERT(I<4); | |
171 | return m.a[I]; | |
172 | } | |
173 | }; | |
174 | } | |
175 | } | |
176 | ||
177 | namespace | |
178 | my_stuff | |
179 | { | |
180 | mat & | |
181 | operator/=( mat & x, float y ) | |
182 | { | |
183 | return boost::qvm::operator/=(x,y); | |
184 | } | |
185 | ||
186 | vec & | |
187 | operator/=( vec & x, float y ) | |
188 | { | |
189 | return boost::qvm::operator/=(x,y); | |
190 | } | |
191 | ||
192 | quat & | |
193 | operator/=( quat & x, float y ) | |
194 | { | |
195 | return boost::qvm::operator/=(x,y); | |
196 | } | |
197 | ||
198 | mat & | |
199 | operator*=( mat & x, float y ) | |
200 | { | |
201 | return boost::qvm::operator*=(x,y); | |
202 | } | |
203 | ||
204 | vec & | |
205 | operator*=( vec & x, float y ) | |
206 | { | |
207 | return boost::qvm::operator*=(x,y); | |
208 | } | |
209 | ||
210 | quat & | |
211 | operator*=( quat & x, float y ) | |
212 | { | |
213 | return boost::qvm::operator*=(x,y); | |
214 | } | |
215 | ||
216 | mat | |
217 | operator/( mat const & x, float y ) | |
218 | { | |
219 | return boost::qvm::operator/(x,y); | |
220 | } | |
221 | ||
222 | vec | |
223 | operator/( vec const & x, float y ) | |
224 | { | |
225 | return boost::qvm::operator/(x,y); | |
226 | } | |
227 | ||
228 | quat | |
229 | operator/( quat const & x, float y ) | |
230 | { | |
231 | return boost::qvm::operator/(x,y); | |
232 | } | |
233 | ||
234 | mat | |
235 | operator*( mat const & x, float y ) | |
236 | { | |
237 | return boost::qvm::operator*(x,y); | |
238 | } | |
239 | ||
240 | vec | |
241 | operator*( vec const & x, float y ) | |
242 | { | |
243 | return boost::qvm::operator*(x,y); | |
244 | } | |
245 | ||
246 | quat | |
247 | operator*( quat const & x, float y ) | |
248 | { | |
249 | return boost::qvm::operator*(x,y); | |
250 | } | |
251 | ||
252 | mat & | |
253 | operator*=( mat & x, mat const & y ) | |
254 | { | |
255 | return boost::qvm::operator*=(x,y); | |
256 | } | |
257 | ||
258 | mat | |
259 | operator*=( mat const & x, mat const & y ) | |
260 | { | |
261 | return boost::qvm::operator*(x,y); | |
262 | } | |
263 | ||
264 | vec | |
265 | operator*( mat const & x, vec const & y ) | |
266 | { | |
267 | return boost::qvm::operator*(x,y); | |
268 | } | |
269 | ||
270 | vec | |
271 | operator*( quat const & x, vec const & y ) | |
272 | { | |
273 | return boost::qvm::operator*(x,y); | |
274 | } | |
275 | ||
276 | vec | |
277 | operator*( vec const & x, mat const & y ) | |
278 | { | |
279 | return boost::qvm::operator*(x,y); | |
280 | } | |
281 | ||
282 | bool | |
283 | operator==( mat const & x, mat const & y ) | |
284 | { | |
285 | return boost::qvm::operator==(x,y); | |
286 | } | |
287 | ||
288 | bool | |
289 | operator!=( mat const & x, mat const & y ) | |
290 | { | |
291 | return boost::qvm::operator!=(x,y); | |
292 | } | |
293 | ||
294 | bool | |
295 | operator==( vec const & x, vec const & y ) | |
296 | { | |
297 | return boost::qvm::operator==(x,y); | |
298 | } | |
299 | ||
300 | bool | |
301 | operator!=( vec const & x, vec const & y ) | |
302 | { | |
303 | return boost::qvm::operator!=(x,y); | |
304 | } | |
305 | ||
306 | bool | |
307 | operator==( quat const & x, quat const & y ) | |
308 | { | |
309 | return boost::qvm::operator==(x,y); | |
310 | } | |
311 | ||
312 | bool | |
313 | operator!=( quat const & x, quat const & y ) | |
314 | { | |
315 | return boost::qvm::operator!=(x,y); | |
316 | } | |
317 | } | |
318 | ||
319 | int | |
320 | main() | |
321 | { | |
322 | using namespace boost::qvm::sfinae; | |
323 | using namespace my_stuff; | |
324 | typedef boost::qvm::mat<double,3,3> mat2; | |
325 | typedef boost::qvm::vec<double,3> vec2; | |
326 | typedef boost::qvm::quat<double> quat2; | |
327 | ||
328 | mat ma1, mb1; set_zero(ma1); set_zero(mb1); | |
329 | vec va1, vb1; set_zero(va1); set_zero(vb1); | |
330 | quat qa1, qb1; set_zero(qa1); set_zero(qb1); | |
331 | mat2 ma2, mb2; set_zero(ma2); set_zero(mb2); | |
332 | vec2 va2, vb2; set_zero(va2); set_zero(vb2); | |
333 | quat2 qa2, qb2; set_zero(qa2); set_zero(qb2); | |
334 | ||
335 | set_zero(ma1); | |
336 | set_zero(mb1); | |
337 | set_zero(va1); | |
338 | set_zero(vb1); | |
339 | set_zero(qa1); | |
340 | set_zero(qb1); | |
341 | set_zero(ma2); | |
342 | set_zero(mb2); | |
343 | set_zero(va2); | |
344 | set_zero(vb2); | |
345 | set_zero(qa2); | |
346 | set_zero(qb2); | |
347 | ||
348 | ma1/=2; | |
349 | va1/=2; | |
350 | qa1/=2; | |
351 | ma2/=2; | |
352 | va2/=2; | |
353 | qa2/=2; | |
354 | ||
355 | ma1*=2; | |
356 | va1*=2; | |
357 | qa1*=2; | |
358 | ma2*=2; | |
359 | va2*=2; | |
360 | qa2*=2; | |
361 | ||
362 | mb1=ma1/2; | |
363 | vb1=va1/2; | |
364 | qb1=qb1/2; | |
365 | mb2=convert_to<mat2>(ma1/2); | |
366 | vb2=convert_to<vec2>(va1/2); | |
367 | qb2=convert_to<quat2>(qa1/2); | |
368 | mb1=scalar_cast<float>(ma2/2); | |
369 | vb1=scalar_cast<float>(va2/2); | |
370 | qb1=scalar_cast<float>(qa2/2); | |
371 | ||
372 | mb1=ma1*2; | |
373 | vb1=va1*2; | |
374 | qb1=qa1*2; | |
375 | mb2=convert_to<mat2>(ma1*2); | |
376 | vb2=convert_to<vec2>(va1*2); | |
377 | qb2=convert_to<quat2>(qa1*2); | |
378 | mb1=scalar_cast<float>(ma2*2); | |
379 | vb1=scalar_cast<float>(va2*2); | |
380 | qb1=scalar_cast<float>(qa2*2); | |
381 | ||
382 | ma1*=mb1; | |
383 | ma1*=scalar_cast<float>(ma2); | |
384 | ma2*=ma1; | |
385 | ||
386 | va1=ma1*va1; | |
387 | va1=qa1*va1; | |
388 | va1=scalar_cast<float>(ma2*va1); | |
389 | va1=scalar_cast<float>(ma1*va2); | |
390 | va1=scalar_cast<float>(ma2*va2); | |
391 | va1=scalar_cast<float>(qa2*va1); | |
392 | va1=scalar_cast<float>(qa1*va2); | |
393 | va1=scalar_cast<float>(qa2*va2); | |
394 | ||
395 | va2=convert_to<vec2>(ma1*va1); | |
396 | va2=convert_to<vec2>(qa1*va1); | |
397 | va2=ma2*va1; | |
398 | va2=ma1*va2; | |
399 | va2=ma2*va2; | |
400 | va2=qa2*va1; | |
401 | va2=qa1*va2; | |
402 | va2=qa2*va2; | |
403 | ||
404 | va1=va1*ma1; | |
405 | va1=scalar_cast<float>(va1*ma2); | |
406 | va1=scalar_cast<float>(va2*ma1); | |
407 | va1=scalar_cast<float>(va2*ma2); | |
408 | ||
409 | va2=convert_to<vec2>(va1*ma1); | |
410 | va2=va1*ma2; | |
411 | va2=va2*ma1; | |
412 | va2=va2*ma2; | |
413 | ||
414 | (void) (ma1==mb1); | |
415 | (void) (ma1==ma2); | |
416 | (void) (ma2==ma1); | |
417 | ||
418 | (void) (ma1!=mb1); | |
419 | (void) (ma1!=ma2); | |
420 | (void) (ma2!=ma1); | |
421 | ||
422 | (void) (va1==vb1); | |
423 | (void) (va1==va2); | |
424 | (void) (va2==va1); | |
425 | ||
426 | (void) (va1!=vb1); | |
427 | (void) (va1!=va2); | |
428 | (void) (va2!=va1); | |
429 | ||
430 | (void) (qa1==qb1); | |
431 | (void) (qa1==qa2); | |
432 | (void) (qa2==qa1); | |
433 | ||
434 | (void) (qa1!=qb1); | |
435 | (void) (qa1!=qa2); | |
436 | (void) (qa2!=qa1); | |
437 | ||
438 | return 0; | |
439 | } |