1 //Copyright (c) 2008-2016 Emil Dotchevski and Reverge Studios, Inc.
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)
6 #ifndef UUID_E6519754D19211DFB8405F74DFD72085
7 #define UUID_E6519754D19211DFB8405F74DFD72085
9 #include <boost/qvm/inline.hpp>
10 #include <boost/qvm/enable_if.hpp>
11 #include <boost/qvm/deduce_quat.hpp>
12 #include <boost/qvm/mat_traits.hpp>
13 #include <boost/qvm/scalar_traits.hpp>
14 #include <boost/qvm/math.hpp>
15 #include <boost/qvm/assert.hpp>
16 #include <boost/qvm/error.hpp>
17 #include <boost/qvm/throw_exception.hpp>
29 BOOST_QVM_INLINE_CRITICAL
31 get_valid_ptr_quat_operations()
33 static int const obj=0;
38 ////////////////////////////////////////////////
41 msvc_parse_bug_workaround
43 template <class A,class B>
47 static bool const value=is_quat<A>::value && is_quat<B>::value;
55 std::string to_string( T const & x );
60 typename boost::enable_if_c<
63 to_string( A const & a )
65 using namespace qvm_to_string_detail;
67 to_string(quat_traits<A>::template read_element<0>(a))+','+
68 to_string(quat_traits<A>::template read_element<1>(a))+','+
69 to_string(quat_traits<A>::template read_element<2>(a))+','+
70 to_string(quat_traits<A>::template read_element<3>(a))+')';
73 ////////////////////////////////////////////////
75 template <class A,class B>
76 BOOST_QVM_INLINE_OPERATIONS
78 is_quat<A>::value && is_quat<B>::value,
80 assign( A & a, B const & b )
82 quat_traits<A>::template write_element<0>(a) = quat_traits<B>::template read_element<0>(b);
83 quat_traits<A>::template write_element<1>(a) = quat_traits<B>::template read_element<1>(b);
84 quat_traits<A>::template write_element<2>(a) = quat_traits<B>::template read_element<2>(b);
85 quat_traits<A>::template write_element<3>(a) = quat_traits<B>::template read_element<3>(b);
89 template <class A,class B,class Cmp>
90 BOOST_QVM_INLINE_OPERATIONS
92 is_quat<A>::value && is_quat<B>::value,
94 cmp( A const & a, B const & b, Cmp f )
96 typedef typename deduce_scalar<
97 typename quat_traits<A>::scalar_type,
98 typename quat_traits<B>::scalar_type>::type T;
101 quat_traits<A>::template read_element<0>(a),
102 quat_traits<A>::template read_element<1>(a),
103 quat_traits<A>::template read_element<2>(a),
104 quat_traits<A>::template read_element<3>(a)
108 quat_traits<B>::template read_element<0>(b),
109 quat_traits<B>::template read_element<1>(b),
110 quat_traits<B>::template read_element<2>(b),
111 quat_traits<B>::template read_element<3>(b)
114 for( i=0; i!=4; ++i )
115 if( !f(q1[i],q2[i]) )
119 for( i=0; i!=4; ++i )
120 if( !f(q1[i],-q2[i]) )
125 ////////////////////////////////////////////////
127 template <class R,class A>
128 BOOST_QVM_INLINE_TRIVIAL
129 typename enable_if_c<
130 is_quat<R>::value && is_quat<A>::value,
132 convert_to( A const & a )
135 quat_traits<R>::template write_element<0>(r) = quat_traits<A>::template read_element<0>(a);
136 quat_traits<R>::template write_element<1>(r) = quat_traits<A>::template read_element<1>(a);
137 quat_traits<R>::template write_element<2>(r) = quat_traits<A>::template read_element<2>(a);
138 quat_traits<R>::template write_element<3>(r) = quat_traits<A>::template read_element<3>(a);
142 template <class R,class A>
143 BOOST_QVM_INLINE_OPERATIONS
144 typename enable_if_c<
145 is_quat<R>::value && is_mat<A>::value &&
146 mat_traits<A>::rows==3 && mat_traits<A>::cols==3,
148 convert_to( A const & a )
150 typedef typename mat_traits<A>::scalar_type T;
153 { mat_traits<A>::template read_element<0,0>(a), mat_traits<A>::template read_element<0,1>(a), mat_traits<A>::template read_element<0,2>(a) },
154 { mat_traits<A>::template read_element<1,0>(a), mat_traits<A>::template read_element<1,1>(a), mat_traits<A>::template read_element<1,2>(a) },
155 { mat_traits<A>::template read_element<2,0>(a), mat_traits<A>::template read_element<2,1>(a), mat_traits<A>::template read_element<2,2>(a) }
158 if( mat[0][0]+mat[1][1]+mat[2][2] > scalar_traits<T>::value(0) )
160 T t = mat[0][0] + mat[1][1] + mat[2][2] + scalar_traits<T>::value(1);
161 T s = (scalar_traits<T>::value(1)/sqrt<T>(t))/2;
162 quat_traits<R>::template write_element<0>(r)=s*t;
163 quat_traits<R>::template write_element<1>(r)=(mat[2][1]-mat[1][2])*s;
164 quat_traits<R>::template write_element<2>(r)=(mat[0][2]-mat[2][0])*s;
165 quat_traits<R>::template write_element<3>(r)=(mat[1][0]-mat[0][1])*s;
167 else if( mat[0][0]>mat[1][1] && mat[0][0]>mat[2][2] )
169 T t = mat[0][0] - mat[1][1] - mat[2][2] + scalar_traits<T>::value(1);
170 T s = (scalar_traits<T>::value(1)/sqrt<T>(t))/2;
171 quat_traits<R>::template write_element<0>(r)=(mat[2][1]-mat[1][2])*s;
172 quat_traits<R>::template write_element<1>(r)=s*t;
173 quat_traits<R>::template write_element<2>(r)=(mat[1][0]+mat[0][1])*s;
174 quat_traits<R>::template write_element<3>(r)=(mat[0][2]+mat[2][0])*s;
176 else if( mat[1][1]>mat[2][2] )
178 T t = - mat[0][0] + mat[1][1] - mat[2][2] + scalar_traits<T>::value(1);
179 T s = (scalar_traits<T>::value(1)/sqrt<T>(t))/2;
180 quat_traits<R>::template write_element<0>(r)=(mat[0][2]-mat[2][0])*s;
181 quat_traits<R>::template write_element<1>(r)=(mat[1][0]+mat[0][1])*s;
182 quat_traits<R>::template write_element<2>(r)=s*t;
183 quat_traits<R>::template write_element<3>(r)=(mat[2][1]+mat[1][2])*s;
187 T t = - mat[0][0] - mat[1][1] + mat[2][2] + scalar_traits<T>::value(1);
188 T s = (scalar_traits<T>::value(1)/sqrt<T>(t))/2;
189 quat_traits<R>::template write_element<0>(r)=(mat[1][0]-mat[0][1])*s;
190 quat_traits<R>::template write_element<1>(r)=(mat[0][2]+mat[2][0])*s;
191 quat_traits<R>::template write_element<2>(r)=(mat[2][1]+mat[1][2])*s;
192 quat_traits<R>::template write_element<3>(r)=s*t;
197 ////////////////////////////////////////////////
200 BOOST_QVM_INLINE_OPERATIONS
201 typename lazy_enable_if_c<
203 deduce_quat<A> >::type
204 conjugate( A const & a )
206 typedef typename deduce_quat<A>::type R;
208 quat_traits<R>::template write_element<0>(r)=quat_traits<A>::template read_element<0>(a);
209 quat_traits<R>::template write_element<1>(r)=-quat_traits<A>::template read_element<1>(a);
210 quat_traits<R>::template write_element<2>(r)=-quat_traits<A>::template read_element<2>(a);
211 quat_traits<R>::template write_element<3>(r)=-quat_traits<A>::template read_element<3>(a);
215 ////////////////////////////////////////////////
224 identity_quat_( identity_quat_ const & );
225 identity_quat_ & operator=( identity_quat_ const & );
231 BOOST_QVM_INLINE_TRIVIAL
243 quat_traits< qvm_detail::identity_quat_<T> >
245 typedef qvm_detail::identity_quat_<T> this_quaternion;
246 typedef T scalar_type;
250 BOOST_QVM_INLINE_CRITICAL
252 read_element( this_quaternion const & x )
254 BOOST_QVM_STATIC_ASSERT(I>=0);
255 BOOST_QVM_STATIC_ASSERT(I<4);
256 return scalar_traits<T>::value(I==0);
260 BOOST_QVM_INLINE_CRITICAL
262 read_element_idx( int i, this_quaternion const & x )
264 BOOST_QVM_ASSERT(i>=0);
265 BOOST_QVM_ASSERT(i<4);
266 return scalar_traits<T>::value(i==0);
272 deduce_quat< qvm_detail::identity_quat_<T> >
274 typedef quat<T> type;
279 deduce_quat2< qvm_detail::identity_quat_<T>, qvm_detail::identity_quat_<T> >
281 typedef quat<T> type;
285 BOOST_QVM_INLINE_TRIVIAL
286 qvm_detail::identity_quat_<T> const &
289 return *(qvm_detail::identity_quat_<T> const *)qvm_detail::get_valid_ptr_quat_operations();
293 BOOST_QVM_INLINE_OPERATIONS
294 typename enable_if_c<
297 set_identity( A & a )
299 typedef typename quat_traits<A>::scalar_type T;
300 T const zero=scalar_traits<T>::value(0);
301 T const one=scalar_traits<T>::value(1);
302 quat_traits<A>::template write_element<0>(a) = one;
303 quat_traits<A>::template write_element<1>(a) = zero;
304 quat_traits<A>::template write_element<2>(a) = zero;
305 quat_traits<A>::template write_element<3>(a) = zero;
308 ////////////////////////////////////////////////
313 template <class OriginalType,class Scalar>
315 quaternion_scalar_cast_
317 quaternion_scalar_cast_( quaternion_scalar_cast_ const & );
318 quaternion_scalar_cast_ & operator=( quaternion_scalar_cast_ const & );
319 ~quaternion_scalar_cast_();
324 BOOST_QVM_INLINE_TRIVIAL
325 quaternion_scalar_cast_ &
326 operator=( T const & x )
333 BOOST_QVM_INLINE_TRIVIAL
342 template <bool> struct scalar_cast_quaternion_filter { };
343 template <> struct scalar_cast_quaternion_filter<true> { typedef int type; };
346 template <class OriginalType,class Scalar>
348 quat_traits< qvm_detail::quaternion_scalar_cast_<OriginalType,Scalar> >
350 typedef Scalar scalar_type;
351 typedef qvm_detail::quaternion_scalar_cast_<OriginalType,Scalar> this_quaternion;
355 BOOST_QVM_INLINE_CRITICAL
357 read_element( this_quaternion const & x )
359 BOOST_QVM_STATIC_ASSERT(I>=0);
360 BOOST_QVM_STATIC_ASSERT(I<4);
361 return scalar_type(quat_traits<OriginalType>::template read_element<I>(reinterpret_cast<OriginalType const &>(x)));
365 BOOST_QVM_INLINE_CRITICAL
367 read_element_idx( int i, this_quaternion const & x )
369 BOOST_QVM_ASSERT(i>=0);
370 BOOST_QVM_ASSERT(i<4);
371 return scalar_type(quat_traits<OriginalType>::read_element_idx(i,reinterpret_cast<OriginalType const &>(x)));
375 template <class Scalar,class T>
376 BOOST_QVM_INLINE_TRIVIAL
377 qvm_detail::quaternion_scalar_cast_<T,Scalar> const &
378 scalar_cast( T const & x, typename qvm_detail::scalar_cast_quaternion_filter<is_quat<T>::value>::type=0 )
380 return reinterpret_cast<qvm_detail::quaternion_scalar_cast_<T,Scalar> const &>(x);
383 ////////////////////////////////////////////////
385 template <class A,class B>
386 BOOST_QVM_INLINE_OPERATIONS
387 typename enable_if_c<
388 is_quat<A>::value && is_scalar<B>::value,
390 operator/=( A & a, B b )
392 quat_traits<A>::template write_element<0>(a)/=b;
393 quat_traits<A>::template write_element<1>(a)/=b;
394 quat_traits<A>::template write_element<2>(a)/=b;
395 quat_traits<A>::template write_element<3>(a)/=b;
399 template <class A,class B>
400 BOOST_QVM_INLINE_OPERATIONS
401 typename lazy_enable_if_c<
402 is_quat<A>::value && is_scalar<B>::value,
403 deduce_quat<A> >::type
404 operator/( A const & a, B b )
406 typedef typename deduce_quat<A>::type R;
408 quat_traits<R>::template write_element<0>(r) = quat_traits<A>::template read_element<0>(a)/b;
409 quat_traits<R>::template write_element<1>(r) = quat_traits<A>::template read_element<1>(a)/b;
410 quat_traits<R>::template write_element<2>(r) = quat_traits<A>::template read_element<2>(a)/b;
411 quat_traits<R>::template write_element<3>(r) = quat_traits<A>::template read_element<3>(a)/b;
415 template <class A,class B>
416 BOOST_QVM_INLINE_OPERATIONS
417 typename lazy_enable_if_c<
418 is_quat<A>::value && is_quat<B>::value,
419 deduce_scalar<typename quat_traits<A>::scalar_type,typename quat_traits<B>::scalar_type> >::type
420 dot( A const & a, B const & b )
422 typedef typename quat_traits<A>::scalar_type Ta;
423 typedef typename quat_traits<B>::scalar_type Tb;
424 typedef typename deduce_scalar<Ta,Tb>::type Tr;
425 Ta const a0=quat_traits<A>::template read_element<0>(a);
426 Ta const a1=quat_traits<A>::template read_element<1>(a);
427 Ta const a2=quat_traits<A>::template read_element<2>(a);
428 Ta const a3=quat_traits<A>::template read_element<3>(a);
429 Tb const b0=quat_traits<B>::template read_element<0>(b);
430 Tb const b1=quat_traits<B>::template read_element<1>(b);
431 Tb const b2=quat_traits<B>::template read_element<2>(b);
432 Tb const b3=quat_traits<B>::template read_element<3>(b);
433 Tr const dp=a0*b0+a1*b1+a2*b2+a3*b3;
437 template <class A,class B>
438 BOOST_QVM_INLINE_OPERATIONS
439 typename enable_if_c<
440 is_quat<A>::value && is_quat<B>::value,
442 operator==( A const & a, B const & b )
445 quat_traits<A>::template read_element<0>(a)==quat_traits<B>::template read_element<0>(b) &&
446 quat_traits<A>::template read_element<1>(a)==quat_traits<B>::template read_element<1>(b) &&
447 quat_traits<A>::template read_element<2>(a)==quat_traits<B>::template read_element<2>(b) &&
448 quat_traits<A>::template read_element<3>(a)==quat_traits<B>::template read_element<3>(b);
452 BOOST_QVM_INLINE_OPERATIONS
453 typename lazy_enable_if_c<
455 deduce_quat<A> >::type
456 inverse( A const & a )
458 typedef typename deduce_quat<A>::type R;
459 typedef typename quat_traits<A>::scalar_type TA;
460 TA aa = quat_traits<A>::template read_element<0>(a);
461 TA ab = quat_traits<A>::template read_element<1>(a);
462 TA ac = quat_traits<A>::template read_element<2>(a);
463 TA ad = quat_traits<A>::template read_element<3>(a);
464 TA m2 = ab*ab + ac*ac + ad*ad + aa*aa;
465 if( m2==scalar_traits<TA>::value(0) )
466 BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error());
467 TA rm=scalar_traits<TA>::value(1)/m2;
469 quat_traits<R>::template write_element<0>(r) = aa*rm;
470 quat_traits<R>::template write_element<1>(r) = -ab*rm;
471 quat_traits<R>::template write_element<2>(r) = -ac*rm;
472 quat_traits<R>::template write_element<3>(r) = -ad*rm;
477 BOOST_QVM_INLINE_OPERATIONS
478 typename enable_if_c<
480 typename quat_traits<A>::scalar_type>::type
481 mag_sqr( A const & a )
483 typedef typename quat_traits<A>::scalar_type T;
484 T x=quat_traits<A>::template read_element<0>(a);
485 T y=quat_traits<A>::template read_element<1>(a);
486 T z=quat_traits<A>::template read_element<2>(a);
487 T w=quat_traits<A>::template read_element<3>(a);
488 return x*x+y*y+z*z+w*w;
492 BOOST_QVM_INLINE_OPERATIONS
493 typename enable_if_c<
495 typename quat_traits<A>::scalar_type>::type
498 typedef typename quat_traits<A>::scalar_type T;
499 T x=quat_traits<A>::template read_element<0>(a);
500 T y=quat_traits<A>::template read_element<1>(a);
501 T z=quat_traits<A>::template read_element<2>(a);
502 T w=quat_traits<A>::template read_element<3>(a);
503 return sqrt<T>(x*x+y*y+z*z+w*w);
506 template <class A,class B>
507 BOOST_QVM_INLINE_OPERATIONS
509 msvc_parse_bug_workaround::quats<A,B>,
511 operator-=( A & a, B const & b )
513 quat_traits<A>::template write_element<0>(a)-=quat_traits<B>::template read_element<0>(b);
514 quat_traits<A>::template write_element<1>(a)-=quat_traits<B>::template read_element<1>(b);
515 quat_traits<A>::template write_element<2>(a)-=quat_traits<B>::template read_element<2>(b);
516 quat_traits<A>::template write_element<3>(a)-=quat_traits<B>::template read_element<3>(b);
520 template <class A,class B>
521 BOOST_QVM_INLINE_OPERATIONS
522 typename lazy_enable_if_c<
523 is_quat<A>::value && is_quat<B>::value,
524 deduce_quat2<A,B> >::type
525 operator-( A const & a, B const & b )
527 typedef typename deduce_quat2<A,B>::type R;
529 quat_traits<R>::template write_element<0>(r)=quat_traits<A>::template read_element<0>(a)-quat_traits<B>::template read_element<0>(b);
530 quat_traits<R>::template write_element<1>(r)=quat_traits<A>::template read_element<1>(a)-quat_traits<B>::template read_element<1>(b);
531 quat_traits<R>::template write_element<2>(r)=quat_traits<A>::template read_element<2>(a)-quat_traits<B>::template read_element<2>(b);
532 quat_traits<R>::template write_element<3>(r)=quat_traits<A>::template read_element<3>(a)-quat_traits<B>::template read_element<3>(b);
537 BOOST_QVM_INLINE_OPERATIONS
538 typename lazy_enable_if_c<
540 deduce_quat<A> >::type
541 operator-( A const & a )
543 typedef typename deduce_quat<A>::type R;
545 quat_traits<R>::template write_element<0>(r)=-quat_traits<A>::template read_element<0>(a);
546 quat_traits<R>::template write_element<1>(r)=-quat_traits<A>::template read_element<1>(a);
547 quat_traits<R>::template write_element<2>(r)=-quat_traits<A>::template read_element<2>(a);
548 quat_traits<R>::template write_element<3>(r)=-quat_traits<A>::template read_element<3>(a);
552 template <class A,class B>
553 BOOST_QVM_INLINE_OPERATIONS
555 msvc_parse_bug_workaround::quats<A,B>,
557 operator*=( A & a, B const & b )
559 typedef typename quat_traits<A>::scalar_type TA;
560 typedef typename quat_traits<B>::scalar_type TB;
561 TA const aa=quat_traits<A>::template read_element<0>(a);
562 TA const ab=quat_traits<A>::template read_element<1>(a);
563 TA const ac=quat_traits<A>::template read_element<2>(a);
564 TA const ad=quat_traits<A>::template read_element<3>(a);
565 TB const ba=quat_traits<B>::template read_element<0>(b);
566 TB const bb=quat_traits<B>::template read_element<1>(b);
567 TB const bc=quat_traits<B>::template read_element<2>(b);
568 TB const bd=quat_traits<B>::template read_element<3>(b);
569 quat_traits<A>::template write_element<0>(a) = aa*ba - ab*bb - ac*bc - ad*bd;
570 quat_traits<A>::template write_element<1>(a) = aa*bb + ab*ba + ac*bd - ad*bc;
571 quat_traits<A>::template write_element<2>(a) = aa*bc + ac*ba + ad*bb - ab*bd;
572 quat_traits<A>::template write_element<3>(a) = aa*bd + ad*ba + ab*bc - ac*bb;
576 template <class A,class B>
577 BOOST_QVM_INLINE_OPERATIONS
578 typename enable_if_c<
579 is_quat<A>::value && is_scalar<B>::value,
581 operator*=( A & a, B b )
583 quat_traits<A>::template write_element<0>(a)*=b;
584 quat_traits<A>::template write_element<1>(a)*=b;
585 quat_traits<A>::template write_element<2>(a)*=b;
586 quat_traits<A>::template write_element<3>(a)*=b;
590 template <class A,class B>
591 BOOST_QVM_INLINE_OPERATIONS
592 typename lazy_enable_if_c<
593 is_quat<A>::value && is_quat<B>::value,
594 deduce_quat2<A,B> >::type
595 operator*( A const & a, B const & b )
597 typedef typename deduce_quat2<A,B>::type R;
598 typedef typename quat_traits<A>::scalar_type TA;
599 typedef typename quat_traits<B>::scalar_type TB;
600 TA const aa=quat_traits<A>::template read_element<0>(a);
601 TA const ab=quat_traits<A>::template read_element<1>(a);
602 TA const ac=quat_traits<A>::template read_element<2>(a);
603 TA const ad=quat_traits<A>::template read_element<3>(a);
604 TB const ba=quat_traits<B>::template read_element<0>(b);
605 TB const bb=quat_traits<B>::template read_element<1>(b);
606 TB const bc=quat_traits<B>::template read_element<2>(b);
607 TB const bd=quat_traits<B>::template read_element<3>(b);
609 quat_traits<R>::template write_element<0>(r) = aa*ba - ab*bb - ac*bc - ad*bd;
610 quat_traits<R>::template write_element<1>(r) = aa*bb + ab*ba + ac*bd - ad*bc;
611 quat_traits<R>::template write_element<2>(r) = aa*bc + ac*ba + ad*bb - ab*bd;
612 quat_traits<R>::template write_element<3>(r) = aa*bd + ad*ba + ab*bc - ac*bb;
616 template <class A,class B>
617 BOOST_QVM_INLINE_OPERATIONS
618 typename lazy_enable_if_c<
619 is_quat<A>::value && is_scalar<B>::value,
620 deduce_quat<A> >::type
621 operator*( A const & a, B b )
623 typedef typename deduce_quat<A>::type R;
625 quat_traits<R>::template write_element<0>(r)=quat_traits<A>::template read_element<0>(a)*b;
626 quat_traits<R>::template write_element<1>(r)=quat_traits<A>::template read_element<1>(a)*b;
627 quat_traits<R>::template write_element<2>(r)=quat_traits<A>::template read_element<2>(a)*b;
628 quat_traits<R>::template write_element<3>(r)=quat_traits<A>::template read_element<3>(a)*b;
632 template <class A,class B>
633 BOOST_QVM_INLINE_OPERATIONS
634 typename enable_if_c<
635 is_quat<A>::value && is_quat<B>::value,
637 operator!=( A const & a, B const & b )
640 quat_traits<A>::template read_element<0>(a)!=quat_traits<B>::template read_element<0>(b) ||
641 quat_traits<A>::template read_element<1>(a)!=quat_traits<B>::template read_element<1>(b) ||
642 quat_traits<A>::template read_element<2>(a)!=quat_traits<B>::template read_element<2>(b) ||
643 quat_traits<A>::template read_element<3>(a)!=quat_traits<B>::template read_element<3>(b);
647 BOOST_QVM_INLINE_OPERATIONS
648 typename lazy_enable_if_c<
650 deduce_quat<A> >::type
651 normalized( A const & a )
653 typedef typename quat_traits<A>::scalar_type T;
654 T const a0=quat_traits<A>::template read_element<0>(a);
655 T const a1=quat_traits<A>::template read_element<1>(a);
656 T const a2=quat_traits<A>::template read_element<2>(a);
657 T const a3=quat_traits<A>::template read_element<3>(a);
658 T const m2=a0*a0+a1*a1+a2*a2+a3*a3;
659 if( m2==scalar_traits<typename quat_traits<A>::scalar_type>::value(0) )
660 BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error());
661 T const rm=scalar_traits<T>::value(1)/sqrt<T>(m2);
662 typedef typename deduce_quat<A>::type R;
664 quat_traits<R>::template write_element<0>(r)=a0*rm;
665 quat_traits<R>::template write_element<1>(r)=a1*rm;
666 quat_traits<R>::template write_element<2>(r)=a2*rm;
667 quat_traits<R>::template write_element<3>(r)=a3*rm;
672 BOOST_QVM_INLINE_OPERATIONS
673 typename enable_if_c<
678 typedef typename quat_traits<A>::scalar_type T;
679 T const a0=quat_traits<A>::template read_element<0>(a);
680 T const a1=quat_traits<A>::template read_element<1>(a);
681 T const a2=quat_traits<A>::template read_element<2>(a);
682 T const a3=quat_traits<A>::template read_element<3>(a);
683 T const m2=a0*a0+a1*a1+a2*a2+a3*a3;
684 if( m2==scalar_traits<typename quat_traits<A>::scalar_type>::value(0) )
685 BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error());
686 T const rm=scalar_traits<T>::value(1)/sqrt<T>(m2);
687 quat_traits<A>::template write_element<0>(a)*=rm;
688 quat_traits<A>::template write_element<1>(a)*=rm;
689 quat_traits<A>::template write_element<2>(a)*=rm;
690 quat_traits<A>::template write_element<3>(a)*=rm;
693 template <class A,class B>
694 BOOST_QVM_INLINE_OPERATIONS
696 msvc_parse_bug_workaround::quats<A,B>,
698 operator+=( A & a, B const & b )
700 quat_traits<A>::template write_element<0>(a)+=quat_traits<B>::template read_element<0>(b);
701 quat_traits<A>::template write_element<1>(a)+=quat_traits<B>::template read_element<1>(b);
702 quat_traits<A>::template write_element<2>(a)+=quat_traits<B>::template read_element<2>(b);
703 quat_traits<A>::template write_element<3>(a)+=quat_traits<B>::template read_element<3>(b);
707 template <class A,class B>
708 BOOST_QVM_INLINE_OPERATIONS
709 typename lazy_enable_if_c<
710 is_quat<A>::value && is_quat<B>::value,
711 deduce_quat2<A,B> >::type
712 operator+( A const & a, B const & b )
714 typedef typename deduce_quat2<A,B>::type R;
716 quat_traits<R>::template write_element<0>(r)=quat_traits<A>::template read_element<0>(a)+quat_traits<B>::template read_element<0>(b);
717 quat_traits<R>::template write_element<1>(r)=quat_traits<A>::template read_element<1>(a)+quat_traits<B>::template read_element<1>(b);
718 quat_traits<R>::template write_element<2>(r)=quat_traits<A>::template read_element<2>(a)+quat_traits<B>::template read_element<2>(b);
719 quat_traits<R>::template write_element<3>(r)=quat_traits<A>::template read_element<3>(a)+quat_traits<B>::template read_element<3>(b);
723 template <class A,class B,class C>
724 BOOST_QVM_INLINE_OPERATIONS
725 typename lazy_enable_if_c<
726 is_quat<A>::value && is_quat<B>::value && is_scalar<C>::value,
727 deduce_quat2<A,B> >::type
728 slerp( A const & a, B const & b, C t )
730 typedef typename deduce_quat2<A,B>::type R;
731 typedef typename quat_traits<R>::scalar_type TR;
732 TR const one = scalar_traits<TR>::value(1);
737 TR const theta = acosf(dp);
738 TR const invsintheta = one/sin<TR>(theta);
739 TR const scale = sin<TR>(theta*(one-t)) * invsintheta;
740 TR const invscale = sin<TR>(theta*t) * invsintheta * sc;
741 return a*scale + b*invscale;
744 return normalized(a+(b-a)*t);
747 ////////////////////////////////////////////////
756 qref_( qref_ const & );
757 qref_ & operator=( qref_ const & );
763 BOOST_QVM_INLINE_TRIVIAL
765 operator=( R const & x )
772 BOOST_QVM_INLINE_TRIVIAL
787 quat_traits< qvm_detail::qref_<Q> >
789 typedef typename quat_traits<Q>::scalar_type scalar_type;
790 typedef qvm_detail::qref_<Q> this_quaternion;
794 BOOST_QVM_INLINE_CRITICAL
796 read_element( this_quaternion const & x )
798 BOOST_QVM_STATIC_ASSERT(I>=0);
799 BOOST_QVM_STATIC_ASSERT(I<4);
800 return quat_traits<Q>::template read_element<I>(reinterpret_cast<Q const &>(x));
805 BOOST_QVM_INLINE_CRITICAL
807 write_element( this_quaternion & x )
809 BOOST_QVM_STATIC_ASSERT(I>=0);
810 BOOST_QVM_STATIC_ASSERT(I<4);
811 return quat_traits<Q>::template write_element<I>(reinterpret_cast<Q &>(x));
817 deduce_quat< qvm_detail::qref_<Q> >
819 typedef quat<typename quat_traits<Q>::scalar_type> type;
823 BOOST_QVM_INLINE_TRIVIAL
824 typename enable_if_c<
826 qvm_detail::qref_<Q> const &>::type
829 return reinterpret_cast<qvm_detail::qref_<Q> const &>(a);
833 BOOST_QVM_INLINE_TRIVIAL
834 typename enable_if_c<
836 qvm_detail::qref_<Q> &>::type
839 return reinterpret_cast<qvm_detail::qref_<Q> &>(a);
842 ////////////////////////////////////////////////
851 zero_q_( zero_q_ const & );
852 zero_q_ & operator=( zero_q_ const & );
858 BOOST_QVM_INLINE_TRIVIAL
870 quat_traits< qvm_detail::zero_q_<T> >
872 typedef qvm_detail::zero_q_<T> this_quaternion;
873 typedef T scalar_type;
877 BOOST_QVM_INLINE_CRITICAL
879 read_element( this_quaternion const & x )
881 BOOST_QVM_STATIC_ASSERT(I>=0);
882 BOOST_QVM_STATIC_ASSERT(I<4);
883 return scalar_traits<scalar_type>::value(0);
887 BOOST_QVM_INLINE_CRITICAL
889 read_element_idx( int i, this_quaternion const & x )
891 BOOST_QVM_ASSERT(i>=0);
892 BOOST_QVM_ASSERT(i<4);
893 return scalar_traits<scalar_type>::value(0);
898 BOOST_QVM_INLINE_TRIVIAL
899 qvm_detail::zero_q_<T> const &
902 return *(qvm_detail::zero_q_<T> const *)qvm_detail::get_valid_ptr_quat_operations();
906 BOOST_QVM_INLINE_OPERATIONS
907 typename enable_if_c<
912 typedef typename quat_traits<A>::scalar_type T;
913 T const zero=scalar_traits<T>::value(0);
914 quat_traits<A>::template write_element<0>(a) = zero;
915 quat_traits<A>::template write_element<1>(a) = zero;
916 quat_traits<A>::template write_element<2>(a) = zero;
917 quat_traits<A>::template write_element<3>(a) = zero;
920 ////////////////////////////////////////////////
929 typedef typename vec_traits<V>::scalar_type scalar_type;
932 template <class Angle>
934 rot_quat_( V const & axis, Angle angle )
936 scalar_type const x=vec_traits<V>::template read_element<0>(axis);
937 scalar_type const y=vec_traits<V>::template read_element<1>(axis);
938 scalar_type const z=vec_traits<V>::template read_element<2>(axis);
939 scalar_type const m2=x*x+y*y+z*z;
940 if( m2==scalar_traits<scalar_type>::value(0) )
941 BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error());
942 scalar_type const rm=scalar_traits<scalar_type>::value(1)/sqrt<scalar_type>(m2);
944 scalar_type const s=sin<Angle>(angle);
945 a[0] = cos<Angle>(angle);
952 BOOST_QVM_INLINE_TRIVIAL
964 quat_traits< qvm_detail::rot_quat_<V> >
966 typedef qvm_detail::rot_quat_<V> this_quaternion;
967 typedef typename this_quaternion::scalar_type scalar_type;
971 BOOST_QVM_INLINE_CRITICAL
973 read_element( this_quaternion const & x )
975 BOOST_QVM_STATIC_ASSERT(I>=0);
976 BOOST_QVM_STATIC_ASSERT(I<4);
983 deduce_quat< qvm_detail::rot_quat_<V> >
985 typedef quat<typename vec_traits<V>::scalar_type> type;
988 template <class A,class Angle>
990 typename enable_if_c<
991 is_vec<A>::value && vec_traits<A>::dim==3,
992 qvm_detail::rot_quat_<A> >::type
993 rot_quat( A const & axis, Angle angle )
995 return qvm_detail::rot_quat_<A>(axis,angle);
998 template <class A,class B,class Angle>
999 BOOST_QVM_INLINE_OPERATIONS
1000 typename enable_if_c<
1001 is_quat<A>::value &&
1002 is_vec<B>::value && vec_traits<B>::dim==3,
1004 set_rot( A & a, B const & axis, Angle angle )
1006 assign(a,rot_quat(axis,angle));
1009 template <class A,class B,class Angle>
1010 BOOST_QVM_INLINE_OPERATIONS
1011 typename enable_if_c<
1012 is_quat<A>::value &&
1013 is_vec<B>::value && vec_traits<B>::dim==3,
1015 rotate( A & a, B const & axis, Angle angle )
1017 a *= rot_quat(axis,angle);
1020 ////////////////////////////////////////////////
1029 BOOST_QVM_INLINE_TRIVIAL
1035 BOOST_QVM_INLINE_TRIVIAL
1045 rotx_quat_( rotx_quat_ const & );
1046 rotx_quat_ & operator=( rotx_quat_ const & );
1056 BOOST_QVM_INLINE_CRITICAL
1060 return scalar_traits<T>::value(0);
1070 BOOST_QVM_INLINE_CRITICAL
1072 get( T const & angle )
1074 return sin<T>(angle/2);
1084 BOOST_QVM_INLINE_CRITICAL
1086 get( T const & angle )
1088 return cos<T>(angle/2);
1093 template <class Angle>
1095 quat_traits< qvm_detail::rotx_quat_<Angle> >
1097 typedef qvm_detail::rotx_quat_<Angle> this_quaternion;
1098 typedef Angle scalar_type;
1102 BOOST_QVM_INLINE_CRITICAL
1104 read_element( this_quaternion const & x )
1106 BOOST_QVM_STATIC_ASSERT(I>=0);
1107 BOOST_QVM_STATIC_ASSERT(I<4);
1108 return qvm_detail::rotx_q_get<I>::get(reinterpret_cast<Angle const &>(x));
1112 template <class Angle>
1114 deduce_quat< qvm_detail::rotx_quat_<Angle> >
1116 typedef quat<Angle> type;
1119 template <class Angle>
1121 deduce_quat2< qvm_detail::rotx_quat_<Angle>, qvm_detail::rotx_quat_<Angle> >
1123 typedef quat<Angle> type;
1126 template <class Angle>
1127 BOOST_QVM_INLINE_TRIVIAL
1128 qvm_detail::rotx_quat_<Angle> const &
1129 rotx_quat( Angle const & angle )
1131 return reinterpret_cast<qvm_detail::rotx_quat_<Angle> const &>(angle);
1134 template <class A,class Angle>
1135 BOOST_QVM_INLINE_OPERATIONS
1136 typename enable_if_c<
1139 set_rotx( A & a, Angle angle )
1141 assign(a,rotx_quat(angle));
1144 template <class A,class Angle>
1145 BOOST_QVM_INLINE_OPERATIONS
1146 typename enable_if_c<
1149 rotate_x( A & a, Angle angle )
1151 a *= rotx_quat(angle);
1154 ////////////////////////////////////////////////
1163 BOOST_QVM_INLINE_TRIVIAL
1169 BOOST_QVM_INLINE_TRIVIAL
1179 roty_quat_( roty_quat_ const & );
1180 roty_quat_ & operator=( roty_quat_ const & );
1190 BOOST_QVM_INLINE_CRITICAL
1194 return scalar_traits<T>::value(0);
1204 BOOST_QVM_INLINE_CRITICAL
1206 get( T const & angle )
1208 return sin<T>(angle/2);
1218 BOOST_QVM_INLINE_CRITICAL
1220 get( T const & angle )
1222 return cos<T>(angle/2);
1227 template <class Angle>
1229 quat_traits< qvm_detail::roty_quat_<Angle> >
1231 typedef qvm_detail::roty_quat_<Angle> this_quaternion;
1232 typedef Angle scalar_type;
1236 BOOST_QVM_INLINE_CRITICAL
1238 read_element( this_quaternion const & x )
1240 BOOST_QVM_STATIC_ASSERT(I>=0);
1241 BOOST_QVM_STATIC_ASSERT(I<4);
1242 return qvm_detail::roty_q_get<I>::get(reinterpret_cast<Angle const &>(x));
1246 template <class Angle>
1248 deduce_quat< qvm_detail::roty_quat_<Angle> >
1250 typedef quat<Angle> type;
1253 template <class Angle>
1255 deduce_quat2< qvm_detail::roty_quat_<Angle>, qvm_detail::roty_quat_<Angle> >
1257 typedef quat<Angle> type;
1260 template <class Angle>
1261 BOOST_QVM_INLINE_TRIVIAL
1262 qvm_detail::roty_quat_<Angle> const &
1263 roty_quat( Angle const & angle )
1265 return reinterpret_cast<qvm_detail::roty_quat_<Angle> const &>(angle);
1268 template <class A,class Angle>
1269 BOOST_QVM_INLINE_OPERATIONS
1270 typename enable_if_c<
1273 set_roty( A & a, Angle angle )
1275 assign(a,roty_quat(angle));
1278 template <class A,class Angle>
1279 BOOST_QVM_INLINE_OPERATIONS
1280 typename enable_if_c<
1283 rotate_y( A & a, Angle angle )
1285 a *= roty_quat(angle);
1288 ////////////////////////////////////////////////
1297 BOOST_QVM_INLINE_TRIVIAL
1303 BOOST_QVM_INLINE_TRIVIAL
1313 rotz_quat_( rotz_quat_ const & );
1314 rotz_quat_ & operator=( rotz_quat_ const & );
1324 BOOST_QVM_INLINE_CRITICAL
1328 return scalar_traits<T>::value(0);
1338 BOOST_QVM_INLINE_CRITICAL
1340 get( T const & angle )
1342 return sin<T>(angle/2);
1352 BOOST_QVM_INLINE_CRITICAL
1354 get( T const & angle )
1356 return cos<T>(angle/2);
1361 template <class Angle>
1363 quat_traits< qvm_detail::rotz_quat_<Angle> >
1365 typedef qvm_detail::rotz_quat_<Angle> this_quaternion;
1366 typedef Angle scalar_type;
1370 BOOST_QVM_INLINE_CRITICAL
1372 read_element( this_quaternion const & x )
1374 BOOST_QVM_STATIC_ASSERT(I>=0);
1375 BOOST_QVM_STATIC_ASSERT(I<4);
1376 return qvm_detail::rotz_q_get<I>::get(reinterpret_cast<Angle const &>(x));
1380 template <class Angle>
1382 deduce_quat< qvm_detail::rotz_quat_<Angle> >
1384 typedef quat<Angle> type;
1387 template <class Angle>
1389 deduce_quat2< qvm_detail::rotz_quat_<Angle>, qvm_detail::rotz_quat_<Angle> >
1391 typedef quat<Angle> type;
1394 template <class Angle>
1395 BOOST_QVM_INLINE_TRIVIAL
1396 qvm_detail::rotz_quat_<Angle> const &
1397 rotz_quat( Angle const & angle )
1399 return reinterpret_cast<qvm_detail::rotz_quat_<Angle> const &>(angle);
1402 template <class A,class Angle>
1403 BOOST_QVM_INLINE_OPERATIONS
1404 typename enable_if_c<
1407 set_rotz( A & a, Angle angle )
1409 assign(a,rotz_quat(angle));
1412 template <class A,class Angle>
1413 BOOST_QVM_INLINE_OPERATIONS
1414 typename enable_if_c<
1417 rotate_z( A & a, Angle angle )
1419 a *= rotz_quat(angle);
1422 template <class A,class B>
1423 BOOST_QVM_INLINE_OPERATIONS
1424 typename enable_if_c<
1425 is_quat<A>::value && is_vec<B>::value && vec_traits<B>::dim==3,
1426 typename quat_traits<A>::scalar_type>::type
1427 axis_angle( A const & a, B & b )
1429 typedef typename quat_traits<A>::scalar_type T;
1430 T a0=quat_traits<A>::template read_element<0>(a);
1431 T a1=quat_traits<A>::template read_element<1>(a);
1432 T a2=quat_traits<A>::template read_element<2>(a);
1433 T a3=quat_traits<A>::template read_element<3>(a);
1436 T const m2=a0*a0+a1*a1+a2*a2+a3*a3;
1437 if( m2==scalar_traits<T>::value(0) )
1438 BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error());
1439 T const s=sqrt<T>(m2);
1445 if( T s=sqrt<T>(1-a0*a0) )
1447 vec_traits<B>::template write_element<0>(b) = a1/s;
1448 vec_traits<B>::template write_element<1>(b) = a2/s;
1449 vec_traits<B>::template write_element<2>(b) = a3/s;
1453 typedef typename vec_traits<B>::scalar_type T;
1454 vec_traits<B>::template write_element<0>(b) = scalar_traits<T>::value(1);
1455 vec_traits<B>::template write_element<1>(b) = vec_traits<B>::template write_element<2>(b) = scalar_traits<T>::value(0);
1457 return scalar_traits<T>::value(2) * qvm::acos(a0);
1460 ////////////////////////////////////////////////
1465 using ::boost::qvm::assign;
1466 using ::boost::qvm::cmp;
1467 using ::boost::qvm::convert_to;
1468 using ::boost::qvm::conjugate;
1469 using ::boost::qvm::set_identity;
1470 using ::boost::qvm::set_zero;
1471 using ::boost::qvm::scalar_cast;
1472 using ::boost::qvm::operator/=;
1473 using ::boost::qvm::operator/;
1474 using ::boost::qvm::dot;
1475 using ::boost::qvm::operator==;
1476 using ::boost::qvm::inverse;
1477 using ::boost::qvm::mag_sqr;
1478 using ::boost::qvm::mag;
1479 using ::boost::qvm::slerp;
1480 using ::boost::qvm::operator-=;
1481 using ::boost::qvm::operator-;
1482 using ::boost::qvm::operator*=;
1483 using ::boost::qvm::operator*;
1484 using ::boost::qvm::operator!=;
1485 using ::boost::qvm::normalized;
1486 using ::boost::qvm::normalize;
1487 using ::boost::qvm::operator+=;
1488 using ::boost::qvm::operator+;
1489 using ::boost::qvm::qref;
1490 using ::boost::qvm::rot_quat;
1491 using ::boost::qvm::set_rot;
1492 using ::boost::qvm::rotate;
1493 using ::boost::qvm::rotx_quat;
1494 using ::boost::qvm::set_rotx;
1495 using ::boost::qvm::rotate_x;
1496 using ::boost::qvm::roty_quat;
1497 using ::boost::qvm::set_roty;
1498 using ::boost::qvm::rotate_y;
1499 using ::boost::qvm::rotz_quat;
1500 using ::boost::qvm::set_rotz;
1501 using ::boost::qvm::rotate_z;
1504 ////////////////////////////////////////////////