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_4F915D9ED30A11DF962186E3DFD72085
7 #define UUID_4F915D9ED30A11DF962186E3DFD72085
9 #include <boost/qvm/detail/mat_assign.hpp>
10 #include <boost/qvm/mat_operations2.hpp>
11 #include <boost/qvm/mat_operations3.hpp>
12 #include <boost/qvm/mat_operations4.hpp>
13 #include <boost/qvm/math.hpp>
14 #include <boost/qvm/detail/determinant_impl.hpp>
15 #include <boost/qvm/detail/cofactor_impl.hpp>
16 #include <boost/qvm/detail/transp_impl.hpp>
17 #include <boost/qvm/scalar_traits.hpp>
29 BOOST_QVM_INLINE_CRITICAL
31 get_valid_ptr_mat_operations()
33 static int const obj=0;
38 ////////////////////////////////////////////////
44 std::string to_string( T const & x );
50 template <int R,int C>
54 static bool const value=false;
57 template <int I,int SizeMinusOne>
59 to_string_matrix_elements
66 using namespace qvm_to_string_detail;
68 ( (I%mat_traits<A>::cols)==0 ? '(' : ',' ) +
69 to_string(mat_traits<A>::template read_element<I/mat_traits<A>::cols,I%mat_traits<A>::cols>(a)) +
70 ( (I%mat_traits<A>::cols)==mat_traits<A>::cols-1 ? ")" : "" ) +
71 to_string_matrix_elements<I+1,SizeMinusOne>::f(a);
75 template <int SizeMinusOne>
77 to_string_matrix_elements<SizeMinusOne,SizeMinusOne>
84 using namespace qvm_to_string_detail;
86 ( (SizeMinusOne%mat_traits<A>::cols)==0 ? '(' : ',' ) +
87 to_string(mat_traits<A>::template read_element<SizeMinusOne/mat_traits<A>::cols,SizeMinusOne%mat_traits<A>::cols>(a)) +
95 typename boost::enable_if_c<
97 !qvm_detail::to_string_m_defined<mat_traits<A>::rows,mat_traits<A>::cols>::value,
99 to_string( A const & a )
101 return "("+qvm_detail::to_string_matrix_elements<0,mat_traits<A>::rows*mat_traits<A>::cols-1>::f(a)+')';
104 ////////////////////////////////////////////////
106 template <class A,class B,class Cmp>
107 BOOST_QVM_INLINE_OPERATIONS
108 typename enable_if_c<
109 is_mat<A>::value && is_mat<B>::value &&
110 mat_traits<A>::rows==mat_traits<B>::rows &&
111 mat_traits<A>::cols==mat_traits<B>::cols,
113 cmp( A const & a, B const & b, Cmp f )
115 typedef typename deduce_scalar<
116 typename mat_traits<A>::scalar_type,
117 typename mat_traits<B>::scalar_type>::type T;
118 int const rows=mat_traits<A>::rows;
119 int const cols=mat_traits<A>::cols;
120 T m1[rows][cols]; assign(m1,a);
121 T m2[rows][cols]; assign(m2,b);
122 for( int i=0; i!=rows; ++i )
123 for( int j=0; j!=cols; ++j )
124 if( !f(m1[i][j],m2[i][j]) )
129 ////////////////////////////////////////////////
134 template <int M,int N>
138 static bool const value=false;
142 template <class R,class A>
143 BOOST_QVM_INLINE_TRIVIAL
144 typename enable_if_c<
145 is_mat<R>::value && is_mat<A>::value &&
146 mat_traits<R>::rows==mat_traits<A>::rows &&
147 mat_traits<R>::cols==mat_traits<A>::cols &&
148 !qvm_detail::convert_to_m_defined<mat_traits<A>::rows,mat_traits<A>::cols>::value,
150 convert_to( A const & a )
156 ////////////////////////////////////////////////
165 static bool const value=false;
170 BOOST_QVM_INLINE_TRIVIAL
171 typename enable_if_c<
173 mat_traits<A>::rows==mat_traits<A>::cols &&
174 !qvm_detail::determinant_defined<mat_traits<A>::rows>::value,
175 typename mat_traits<A>::scalar_type>::type
176 determinant( A const & a )
178 return qvm_detail::determinant_impl(a);
181 ////////////////////////////////////////////////
186 template <class T,int Dim>
190 identity_mat_( identity_mat_ const & );
191 identity_mat_ & operator=( identity_mat_ const & );
197 BOOST_QVM_INLINE_TRIVIAL
207 template <class T,int Dim>
209 mat_traits< qvm_detail::identity_mat_<T,Dim> >
211 typedef qvm_detail::identity_mat_<T,Dim> this_matrix;
212 typedef T scalar_type;
213 static int const rows=Dim;
214 static int const cols=Dim;
216 template <int Row,int Col>
218 BOOST_QVM_INLINE_CRITICAL
220 read_element( this_matrix const & /*x*/ )
222 BOOST_QVM_STATIC_ASSERT(Row>=0);
223 BOOST_QVM_STATIC_ASSERT(Row<Dim);
224 BOOST_QVM_STATIC_ASSERT(Col>=0);
225 BOOST_QVM_STATIC_ASSERT(Col<Dim);
226 return scalar_traits<scalar_type>::value(Row==Col);
230 BOOST_QVM_INLINE_CRITICAL
232 read_element_idx( int row, int col, this_matrix const & /*x*/ )
234 BOOST_QVM_ASSERT(row>=0);
235 BOOST_QVM_ASSERT(row<Dim);
236 BOOST_QVM_ASSERT(col>=0);
237 BOOST_QVM_ASSERT(col<Dim);
238 return scalar_traits<scalar_type>::value(row==col);
242 template <class T,int Dim>
243 BOOST_QVM_INLINE_TRIVIAL
244 qvm_detail::identity_mat_<T,Dim> const &
247 return *(qvm_detail::identity_mat_<T,Dim> const *)qvm_detail::get_valid_ptr_mat_operations();
251 BOOST_QVM_INLINE_OPERATIONS
252 typename enable_if_c<
254 mat_traits<A>::rows==mat_traits<A>::cols,
256 set_identity( A & a )
258 assign(a,identity_mat<typename mat_traits<A>::scalar_type,mat_traits<A>::rows>());
261 ////////////////////////////////////////////////
276 BOOST_QVM_INLINE_TRIVIAL
277 projection_( T _00, T _11, T _22, T _23, T _32 ):
287 BOOST_QVM_INLINE_TRIVIAL
296 template <int Row,int Col>
302 BOOST_QVM_INLINE_CRITICAL
304 get( projection_<T> const & )
306 return scalar_traits<T>::value(0);
310 template <> struct projection_get<0,0> { template <class T> static BOOST_QVM_INLINE_CRITICAL T get( projection_<T> const & m ) { return m._00; } };
311 template <> struct projection_get<1,1> { template <class T> static BOOST_QVM_INLINE_CRITICAL T get( projection_<T> const & m ) { return m._11; } };
312 template <> struct projection_get<2,2> { template <class T> static BOOST_QVM_INLINE_CRITICAL T get( projection_<T> const & m ) { return m._22; } };
313 template <> struct projection_get<2,3> { template <class T> static BOOST_QVM_INLINE_CRITICAL T get( projection_<T> const & m ) { return m._23; } };
314 template <> struct projection_get<3,2> { template <class T> static BOOST_QVM_INLINE_CRITICAL T get( projection_<T> const & m ) { return m._32; } };
319 mat_traits< qvm_detail::projection_<T> >
321 typedef qvm_detail::projection_<T> this_matrix;
322 typedef T scalar_type;
323 static int const rows=4;
324 static int const cols=4;
326 template <int Row,int Col>
328 BOOST_QVM_INLINE_CRITICAL
330 read_element( this_matrix const & x )
332 BOOST_QVM_STATIC_ASSERT(Row>=0);
333 BOOST_QVM_STATIC_ASSERT(Row<rows);
334 BOOST_QVM_STATIC_ASSERT(Col>=0);
335 BOOST_QVM_STATIC_ASSERT(Col<cols);
336 return qvm_detail::projection_get<Row,Col>::get(x);
341 qvm_detail::projection_<T>
342 BOOST_QVM_INLINE_OPERATIONS
343 perspective_lh( T fov_y, T aspect_ratio, T z_near, T z_far )
345 T const one = scalar_traits<T>::value(1);
346 T const ys = one/tan<T>(fov_y/scalar_traits<T>::value(2));
347 T const xs = ys/aspect_ratio;
348 T const zd = z_far-z_near;
349 T const z1 = z_far/zd;
350 T const z2 = -z_near*z1;
351 return qvm_detail::projection_<T>(xs,ys,z1,z2,one);
355 qvm_detail::projection_<T>
356 BOOST_QVM_INLINE_OPERATIONS
357 perspective_rh( T fov_y, T aspect_ratio, T z_near, T z_far )
359 T const one = scalar_traits<T>::value(1);
360 T const ys = one/tan<T>(fov_y/scalar_traits<T>::value(2));
361 T const xs = ys/aspect_ratio;
362 T const zd = z_near-z_far;
363 T const z1 = z_far/zd;
364 T const z2 = z_near*z1;
365 return qvm_detail::projection_<T>(xs,ys,z1,z2,-one);
368 ////////////////////////////////////////////////
373 template <class OriginalType,class Scalar>
377 matrix_scalar_cast_( matrix_scalar_cast_ const & );
378 matrix_scalar_cast_ & operator=( matrix_scalar_cast_ const & );
379 ~matrix_scalar_cast_();
384 BOOST_QVM_INLINE_TRIVIAL
385 matrix_scalar_cast_ &
386 operator=( T const & x )
393 BOOST_QVM_INLINE_TRIVIAL
402 template <bool> struct scalar_cast_matrix_filter { };
403 template <> struct scalar_cast_matrix_filter<true> { typedef int type; };
406 template <class OriginalType,class Scalar>
408 mat_traits< qvm_detail::matrix_scalar_cast_<OriginalType,Scalar> >
410 typedef Scalar scalar_type;
411 typedef qvm_detail::matrix_scalar_cast_<OriginalType,Scalar> this_matrix;
412 static int const rows=mat_traits<OriginalType>::rows;
413 static int const cols=mat_traits<OriginalType>::cols;
415 template <int Row,int Col>
417 BOOST_QVM_INLINE_CRITICAL
419 read_element( this_matrix const & x )
421 BOOST_QVM_STATIC_ASSERT(Row>=0);
422 BOOST_QVM_STATIC_ASSERT(Row<rows);
423 BOOST_QVM_STATIC_ASSERT(Col>=0);
424 BOOST_QVM_STATIC_ASSERT(Col<cols);
425 return scalar_type(mat_traits<OriginalType>::template read_element<Row,Col>(reinterpret_cast<OriginalType const &>(x)));
429 BOOST_QVM_INLINE_CRITICAL
431 read_element_idx( int row, int col, this_matrix const & x )
433 BOOST_QVM_ASSERT(row>=0);
434 BOOST_QVM_ASSERT(row<rows);
435 BOOST_QVM_ASSERT(col>=0);
436 BOOST_QVM_ASSERT(col<cols);
437 return scalar_type(mat_traits<OriginalType>::read_element_idx(col,row,reinterpret_cast<OriginalType const &>(x)));
441 template <class OriginalType,class Scalar,int R,int C>
443 deduce_mat<qvm_detail::matrix_scalar_cast_<OriginalType,Scalar>,R,C>
445 typedef mat<Scalar,R,C> type;
448 template <class Scalar,class T>
449 BOOST_QVM_INLINE_TRIVIAL
450 qvm_detail::matrix_scalar_cast_<T,Scalar> const &
451 scalar_cast( T const & x, typename qvm_detail::scalar_cast_matrix_filter<is_mat<T>::value>::type=0 )
453 return reinterpret_cast<qvm_detail::matrix_scalar_cast_<T,Scalar> const &>(x);
456 ////////////////////////////////////////////////
461 template <int M,int N>
465 static bool const value=false;
469 template <class A,class B>
470 BOOST_QVM_INLINE_OPERATIONS
471 typename enable_if_c<
472 is_mat<A>::value && is_scalar<B>::value &&
473 !qvm_detail::div_eq_ms_defined<mat_traits<A>::rows,mat_traits<A>::cols>::value,
475 operator/=( A & a, B b )
477 for( int i=0; i!=mat_traits<A>::rows; ++i )
478 for( int j=0; j!=mat_traits<A>::cols; ++j )
479 mat_traits<A>::write_element_idx(i,j,a)/=b;
483 ////////////////////////////////////////////////
488 template <int M,int N>
492 static bool const value=false;
496 template <class A,class B>
497 BOOST_QVM_INLINE_OPERATIONS
498 typename lazy_enable_if_c<
499 is_mat<A>::value && is_scalar<B>::value &&
500 !qvm_detail::div_ms_defined<mat_traits<A>::rows,mat_traits<A>::cols>::value,
501 deduce_mat<A> >::type
502 operator/( A const & a, B b )
504 typedef typename deduce_mat<A>::type R;
506 for( int i=0; i!=mat_traits<A>::rows; ++i )
507 for( int j=0; j!=mat_traits<A>::cols; ++j )
508 mat_traits<R>::write_element_idx(i,j,r)=mat_traits<A>::read_element_idx(i,j,a)/b;
512 ////////////////////////////////////////////////
517 template <int M,int N>
521 static bool const value=false;
525 template <class A,class B>
526 BOOST_QVM_INLINE_OPERATIONS
527 typename enable_if_c<
528 is_mat<A>::value && is_mat<B>::value &&
529 mat_traits<A>::rows==mat_traits<B>::rows &&
530 mat_traits<A>::cols==mat_traits<B>::cols &&
531 !qvm_detail::eq_mm_defined<mat_traits<A>::rows,mat_traits<A>::cols>::value,
533 operator==( A const & a, B const & b )
535 for( int i=0; i!=mat_traits<A>::rows; ++i )
536 for( int j=0; j!=mat_traits<A>::cols; ++j )
537 if( mat_traits<A>::read_element_idx(i,j,a)!=mat_traits<B>::read_element_idx(i,j,b) )
542 ////////////////////////////////////////////////
547 template <int M,int N>
551 static bool const value=false;
555 template <class A,class B>
556 BOOST_QVM_INLINE_OPERATIONS
557 typename enable_if_c<
558 is_mat<A>::value && is_mat<B>::value &&
559 mat_traits<A>::rows==mat_traits<B>::rows &&
560 mat_traits<A>::cols==mat_traits<B>::cols &&
561 !qvm_detail::minus_eq_mm_defined<mat_traits<A>::rows,mat_traits<A>::cols>::value,
563 operator-=( A & a, B const & b )
565 for( int i=0; i!=mat_traits<A>::rows; ++i )
566 for( int j=0; j!=mat_traits<A>::cols; ++j )
567 mat_traits<A>::write_element_idx(i,j,a)-=mat_traits<B>::read_element_idx(i,j,b);
571 ////////////////////////////////////////////////
576 template <int M,int N>
580 static bool const value=false;
585 BOOST_QVM_INLINE_OPERATIONS
586 typename lazy_enable_if_c<
588 !qvm_detail::minus_m_defined<mat_traits<A>::rows,mat_traits<A>::cols>::value,
589 deduce_mat<A> >::type
590 operator-( A const & a )
592 typedef typename deduce_mat<A>::type R;
594 for( int i=0; i!=mat_traits<A>::rows; ++i )
595 for( int j=0; j!=mat_traits<A>::cols; ++j )
596 mat_traits<R>::write_element_idx(i,j,r)=-mat_traits<A>::read_element_idx(i,j,a);
600 ////////////////////////////////////////////////
605 template <int M,int N>
609 static bool const value=false;
613 template <class A,class B>
614 BOOST_QVM_INLINE_OPERATIONS
615 typename lazy_enable_if_c<
616 is_mat<A>::value && is_mat<B>::value &&
617 mat_traits<A>::rows==mat_traits<B>::rows &&
618 mat_traits<A>::cols==mat_traits<B>::cols &&
619 !qvm_detail::minus_mm_defined<mat_traits<A>::rows,mat_traits<A>::cols>::value,
620 deduce_mat2<A,B,mat_traits<A>::rows,mat_traits<A>::cols> >::type
621 operator-( A const & a, B const & b )
623 typedef typename deduce_mat2<A,B,mat_traits<A>::rows,mat_traits<A>::cols>::type R;
625 for( int i=0; i!=mat_traits<A>::rows; ++i )
626 for( int j=0; j!=mat_traits<A>::cols; ++j )
627 mat_traits<R>::write_element_idx(i,j,r)=mat_traits<A>::read_element_idx(i,j,a)-mat_traits<B>::read_element_idx(i,j,b);
631 ////////////////////////////////////////////////
640 static bool const value=false;
644 template <class A,class B>
645 BOOST_QVM_INLINE_OPERATIONS
646 typename enable_if_c<
649 mat_traits<A>::rows==mat_traits<A>::cols &&
650 mat_traits<A>::rows==mat_traits<B>::rows &&
651 mat_traits<A>::cols==mat_traits<B>::cols &&
652 !qvm_detail::mul_eq_mm_defined<mat_traits<A>::rows>::value,
654 operator*=( A & r, B const & b )
656 typedef typename mat_traits<A>::scalar_type Ta;
657 Ta a[mat_traits<A>::rows][mat_traits<A>::cols];
658 for( int i=0; i<mat_traits<A>::rows; ++i )
659 for( int j=0; j<mat_traits<B>::cols; ++j )
660 a[i][j]=mat_traits<A>::read_element_idx(i,j,r);
661 for( int i=0; i<mat_traits<A>::rows; ++i )
662 for( int j=0; j<mat_traits<B>::cols; ++j )
664 Ta x(scalar_traits<Ta>::value(0));
665 for( int k=0; k<mat_traits<A>::cols; ++k )
666 x += a[i][k]*mat_traits<B>::read_element_idx(k,j,b);
667 mat_traits<A>::write_element_idx(i,j,r) = x;
672 ////////////////////////////////////////////////
677 template <int M,int N>
681 static bool const value=false;
685 template <class A,class B>
686 BOOST_QVM_INLINE_OPERATIONS
687 typename enable_if_c<
688 is_mat<A>::value && is_scalar<B>::value &&
689 !qvm_detail::mul_eq_ms_defined<mat_traits<A>::rows,mat_traits<A>::cols>::value,
691 operator*=( A & a, B b )
693 for( int i=0; i!=mat_traits<A>::rows; ++i )
694 for( int j=0; j!=mat_traits<A>::cols; ++j )
695 mat_traits<A>::write_element_idx(i,j,a)*=b;
699 ////////////////////////////////////////////////
704 template <int R,int CR,int C>
708 static bool const value=false;
712 template <class A,class B>
713 BOOST_QVM_INLINE_OPERATIONS
714 typename lazy_enable_if_c<
715 is_mat<A>::value && is_mat<B>::value &&
716 mat_traits<A>::cols==mat_traits<B>::rows &&
717 !qvm_detail::mul_mm_defined<mat_traits<A>::rows,mat_traits<A>::cols,mat_traits<B>::cols>::value,
718 deduce_mat2<A,B,mat_traits<A>::rows,mat_traits<B>::cols> >::type
719 operator*( A const & a, B const & b )
721 typedef typename deduce_mat2<A,B,mat_traits<A>::rows,mat_traits<B>::cols>::type R;
723 for( int i=0; i<mat_traits<A>::rows; ++i )
724 for( int j=0; j<mat_traits<B>::cols; ++j )
726 typedef typename mat_traits<A>::scalar_type Ta;
727 Ta x(scalar_traits<Ta>::value(0));
728 for( int k=0; k<mat_traits<A>::cols; ++k )
729 x += mat_traits<A>::read_element_idx(i,k,a)*mat_traits<B>::read_element_idx(k,j,b);
730 mat_traits<R>::write_element_idx(i,j,r) = x;
735 ////////////////////////////////////////////////
740 template <int M,int N>
744 static bool const value=false;
748 template <class A,class B>
749 BOOST_QVM_INLINE_OPERATIONS
750 typename lazy_enable_if_c<
751 is_mat<A>::value && is_scalar<B>::value &&
752 !qvm_detail::mul_ms_defined<mat_traits<A>::rows,mat_traits<A>::cols>::value,
753 deduce_mat<A> >::type
754 operator*( A const & a, B b )
756 typedef typename deduce_mat<A>::type R;
758 for( int i=0; i!=mat_traits<A>::rows; ++i )
759 for( int j=0; j!=mat_traits<A>::cols; ++j )
760 mat_traits<R>::write_element_idx(i,j,r)=mat_traits<A>::read_element_idx(i,j,a)*b;
764 ////////////////////////////////////////////////
769 template <int M,int N>
773 static bool const value=false;
777 template <class A,class B>
778 BOOST_QVM_INLINE_OPERATIONS
779 typename lazy_enable_if_c<
780 is_scalar<A>::value && is_mat<B>::value &&
781 !qvm_detail::mul_sm_defined<mat_traits<B>::rows,mat_traits<B>::cols>::value,
782 deduce_mat<B> >::type
783 operator*( A a, B const & b )
785 typedef typename deduce_mat<B>::type R;
787 for( int i=0; i!=mat_traits<B>::rows; ++i )
788 for( int j=0; j!=mat_traits<B>::cols; ++j )
789 mat_traits<R>::write_element_idx(i,j,r)=a*mat_traits<B>::read_element_idx(i,j,b);
793 ////////////////////////////////////////////////
798 template <int M,int N>
802 static bool const value=false;
806 template <class A,class B>
807 BOOST_QVM_INLINE_OPERATIONS
808 typename enable_if_c<
809 is_mat<A>::value && is_mat<B>::value &&
810 mat_traits<A>::rows==mat_traits<B>::rows &&
811 mat_traits<A>::cols==mat_traits<B>::cols &&
812 !qvm_detail::neq_mm_defined<mat_traits<A>::rows,mat_traits<A>::cols>::value,
814 operator!=( A const & a, B const & b )
816 for( int i=0; i!=mat_traits<A>::rows; ++i )
817 for( int j=0; j!=mat_traits<A>::cols; ++j )
818 if( mat_traits<A>::read_element_idx(i,j,a)!=mat_traits<B>::read_element_idx(i,j,b) )
823 ////////////////////////////////////////////////
828 template <int M,int N>
832 static bool const value=false;
836 template <class A,class B>
837 BOOST_QVM_INLINE_OPERATIONS
838 typename enable_if_c<
839 is_mat<A>::value && is_mat<B>::value &&
840 mat_traits<A>::rows==mat_traits<B>::rows &&
841 mat_traits<A>::cols==mat_traits<B>::cols &&
842 !qvm_detail::plus_eq_mm_defined<mat_traits<A>::rows,mat_traits<A>::cols>::value,
844 operator+=( A & a, B const & b )
846 for( int i=0; i!=mat_traits<A>::rows; ++i )
847 for( int j=0; j!=mat_traits<A>::cols; ++j )
848 mat_traits<A>::write_element_idx(i,j,a)+=mat_traits<B>::read_element_idx(i,j,b);
852 ////////////////////////////////////////////////
857 template <int M,int N>
861 static bool const value=false;
865 template <class A,class B>
866 BOOST_QVM_INLINE_OPERATIONS
867 typename lazy_enable_if_c<
868 is_mat<A>::value && is_mat<B>::value &&
869 mat_traits<A>::rows==mat_traits<B>::rows &&
870 mat_traits<A>::cols==mat_traits<B>::cols &&
871 !qvm_detail::plus_mm_defined<mat_traits<A>::rows,mat_traits<A>::cols>::value,
872 deduce_mat2<A,B,mat_traits<A>::rows,mat_traits<A>::cols> >::type
873 operator+( A const & a, B const & b )
875 typedef typename deduce_mat2<A,B,mat_traits<A>::rows,mat_traits<A>::cols>::type R;
877 for( int i=0; i!=mat_traits<A>::rows; ++i )
878 for( int j=0; j!=mat_traits<A>::cols; ++j )
879 mat_traits<R>::write_element_idx(i,j,r)=mat_traits<A>::read_element_idx(i,j,a)+mat_traits<B>::read_element_idx(i,j,b);
883 ////////////////////////////////////////////////
892 mref_( mref_ const & );
893 mref_ & operator=( mref_ const & );
899 BOOST_QVM_INLINE_TRIVIAL
901 operator=( R const & x )
908 BOOST_QVM_INLINE_TRIVIAL
920 mat_traits< qvm_detail::mref_<M> >
922 typedef typename mat_traits<M>::scalar_type scalar_type;
923 typedef qvm_detail::mref_<M> this_matrix;
924 static int const rows=mat_traits<M>::rows;
925 static int const cols=mat_traits<M>::cols;
927 template <int Row,int Col>
929 BOOST_QVM_INLINE_CRITICAL
931 read_element( this_matrix const & x )
933 BOOST_QVM_STATIC_ASSERT(Row>=0);
934 BOOST_QVM_STATIC_ASSERT(Row<rows);
935 BOOST_QVM_STATIC_ASSERT(Col>=0);
936 BOOST_QVM_STATIC_ASSERT(Col<cols);
937 return mat_traits<M>::template read_element<Row,Col>(reinterpret_cast<M const &>(x));
940 template <int Row,int Col>
942 BOOST_QVM_INLINE_CRITICAL
944 write_element( this_matrix & x )
946 BOOST_QVM_STATIC_ASSERT(Row>=0);
947 BOOST_QVM_STATIC_ASSERT(Row<rows);
948 BOOST_QVM_STATIC_ASSERT(Col>=0);
949 BOOST_QVM_STATIC_ASSERT(Col<cols);
950 return mat_traits<M>::template write_element<Row,Col>(reinterpret_cast<M &>(x));
954 BOOST_QVM_INLINE_CRITICAL
956 read_element_idx( int row, int col, this_matrix const & x )
958 BOOST_QVM_ASSERT(row>=0);
959 BOOST_QVM_ASSERT(row<rows);
960 BOOST_QVM_ASSERT(col>=0);
961 BOOST_QVM_ASSERT(col<cols);
962 return mat_traits<M>::read_element_idx(row,col,reinterpret_cast<M const &>(x));
966 BOOST_QVM_INLINE_CRITICAL
968 write_element_idx( int row, int col, this_matrix & x )
970 BOOST_QVM_ASSERT(row>=0);
971 BOOST_QVM_ASSERT(row<rows);
972 BOOST_QVM_ASSERT(col>=0);
973 BOOST_QVM_ASSERT(col<cols);
974 return mat_traits<M>::write_element_idx(row,col,reinterpret_cast<M &>(x));
978 template <class M,int R,int C>
980 deduce_mat<qvm_detail::mref_<M>,R,C>
982 typedef mat<typename mat_traits<M>::scalar_type,R,C> type;
986 BOOST_QVM_INLINE_TRIVIAL
987 typename enable_if_c<
989 qvm_detail::mref_<M> const &>::type
992 return reinterpret_cast<qvm_detail::mref_<M> const &>(a);
996 BOOST_QVM_INLINE_TRIVIAL
997 typename enable_if_c<
999 qvm_detail::mref_<M> &>::type
1002 return reinterpret_cast<qvm_detail::mref_<M> &>(a);
1005 ////////////////////////////////////////////////
1010 template <class T,int Rows,int Cols>
1014 zero_mat_( zero_mat_ const & );
1015 zero_mat_ & operator=( zero_mat_ const & );
1021 BOOST_QVM_INLINE_TRIVIAL
1031 template <class T,int Rows,int Cols>
1033 mat_traits< qvm_detail::zero_mat_<T,Rows,Cols> >
1035 typedef qvm_detail::zero_mat_<T,Rows,Cols> this_matrix;
1036 typedef T scalar_type;
1037 static int const rows=Rows;
1038 static int const cols=Cols;
1040 template <int Row,int Col>
1042 BOOST_QVM_INLINE_CRITICAL
1044 read_element( this_matrix const & )
1046 BOOST_QVM_STATIC_ASSERT(Row>=0);
1047 BOOST_QVM_STATIC_ASSERT(Row<Rows);
1048 BOOST_QVM_STATIC_ASSERT(Col>=0);
1049 BOOST_QVM_STATIC_ASSERT(Col<Cols);
1050 return scalar_traits<scalar_type>::value(0);
1054 BOOST_QVM_INLINE_CRITICAL
1056 read_element_idx( int row, int col, this_matrix const & )
1058 BOOST_QVM_ASSERT(row>=0);
1059 BOOST_QVM_ASSERT(row<rows);
1060 BOOST_QVM_ASSERT(col>=0);
1061 BOOST_QVM_ASSERT(col<cols);
1062 return scalar_traits<scalar_type>::value(0);
1066 template <class T,int Rows,int Cols,int R,int C>
1068 deduce_mat<qvm_detail::zero_mat_<T,Rows,Cols>,R,C>
1070 typedef mat<T,R,C> type;
1073 template <class T,int Rows,int Cols>
1074 BOOST_QVM_INLINE_TRIVIAL
1075 qvm_detail::zero_mat_<T,Rows,Cols> const &
1078 return *(qvm_detail::zero_mat_<T,Rows,Cols> const *)qvm_detail::get_valid_ptr_mat_operations();
1081 template <class T,int Dim>
1082 BOOST_QVM_INLINE_TRIVIAL
1083 qvm_detail::zero_mat_<T,Dim,Dim> const &
1086 return *(qvm_detail::zero_mat_<T,Dim,Dim> const *)qvm_detail::get_valid_ptr_mat_operations();
1090 BOOST_QVM_INLINE_OPERATIONS
1091 typename enable_if_c<
1096 assign(a,zero_mat<typename mat_traits<A>::scalar_type,mat_traits<A>::rows,mat_traits<A>::cols>());
1099 ////////////////////////////////////////////////
1104 template <int D,class S>
1108 typedef S scalar_type;
1109 scalar_type a[3][3];
1113 scalar_type a00, scalar_type a01, scalar_type a02,
1114 scalar_type a10, scalar_type a11, scalar_type a12,
1115 scalar_type a20, scalar_type a21, scalar_type a22 )
1129 BOOST_QVM_INLINE_TRIVIAL
1138 template <int Row,int Col>
1144 BOOST_QVM_INLINE_CRITICAL
1146 get( T const (&)[3][3] )
1148 return scalar_traits<T>::value(Row==Col);
1152 template <> struct rot_m_get<0,0> { template <class T> static BOOST_QVM_INLINE_CRITICAL T get( T const (&a)[3][3] ) { return a[0][0]; } };
1153 template <> struct rot_m_get<0,1> { template <class T> static BOOST_QVM_INLINE_CRITICAL T get( T const (&a)[3][3] ) { return a[0][1]; } };
1154 template <> struct rot_m_get<0,2> { template <class T> static BOOST_QVM_INLINE_CRITICAL T get( T const (&a)[3][3] ) { return a[0][2]; } };
1155 template <> struct rot_m_get<1,0> { template <class T> static BOOST_QVM_INLINE_CRITICAL T get( T const (&a)[3][3] ) { return a[1][0]; } };
1156 template <> struct rot_m_get<1,1> { template <class T> static BOOST_QVM_INLINE_CRITICAL T get( T const (&a)[3][3] ) { return a[1][1]; } };
1157 template <> struct rot_m_get<1,2> { template <class T> static BOOST_QVM_INLINE_CRITICAL T get( T const (&a)[3][3] ) { return a[1][2]; } };
1158 template <> struct rot_m_get<2,0> { template <class T> static BOOST_QVM_INLINE_CRITICAL T get( T const (&a)[3][3] ) { return a[2][0]; } };
1159 template <> struct rot_m_get<2,1> { template <class T> static BOOST_QVM_INLINE_CRITICAL T get( T const (&a)[3][3] ) { return a[2][1]; } };
1160 template <> struct rot_m_get<2,2> { template <class T> static BOOST_QVM_INLINE_CRITICAL T get( T const (&a)[3][3] ) { return a[2][2]; } };
1166 template <int D,class S>
1168 mat_traits< qvm_detail::rot_mat_<D,S> >
1170 typedef qvm_detail::rot_mat_<D,S> this_matrix;
1171 typedef typename this_matrix::scalar_type scalar_type;
1172 static int const rows=D;
1173 static int const cols=D;
1175 template <int Row,int Col>
1177 BOOST_QVM_INLINE_CRITICAL
1179 read_element( this_matrix const & x )
1181 BOOST_QVM_STATIC_ASSERT(Row>=0);
1182 BOOST_QVM_STATIC_ASSERT(Row<D);
1183 BOOST_QVM_STATIC_ASSERT(Col>=0);
1184 BOOST_QVM_STATIC_ASSERT(Col<D);
1185 return qvm_detail::rot_m_get<Row,Col>::get(x.a);
1189 BOOST_QVM_INLINE_CRITICAL
1191 read_element_idx( int row, int col, this_matrix const & x )
1193 BOOST_QVM_ASSERT(row>=0);
1194 BOOST_QVM_ASSERT(row<D);
1195 BOOST_QVM_ASSERT(col>=0);
1196 BOOST_QVM_ASSERT(col<D);
1197 return row<3 && col<3?
1199 scalar_traits<scalar_type>::value(row==col);
1203 template <int Dim,class V,class Angle>
1205 typename enable_if_c<
1206 is_vec<V>::value && vec_traits<V>::dim==3,
1207 qvm_detail::rot_mat_<Dim,Angle> >::type
1208 rot_mat( V const & axis, Angle angle )
1210 typedef Angle scalar_type;
1211 scalar_type const x=vec_traits<V>::template read_element<0>(axis);
1212 scalar_type const y=vec_traits<V>::template read_element<1>(axis);
1213 scalar_type const z=vec_traits<V>::template read_element<2>(axis);
1214 scalar_type const m2=x*x+y*y+z*z;
1215 if( m2==scalar_traits<scalar_type>::value(0) )
1216 BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error());
1217 scalar_type const s = sin<scalar_type>(angle);
1218 scalar_type const c = cos<scalar_type>(angle);
1219 scalar_type const x2 = x*x;
1220 scalar_type const y2 = y*y;
1221 scalar_type const z2 = z*z;
1222 scalar_type const xy = x*y;
1223 scalar_type const xz = x*z;
1224 scalar_type const yz = y*z;
1225 scalar_type const xs = x*s;
1226 scalar_type const ys = y*s;
1227 scalar_type const zs = z*s;
1228 scalar_type const one = scalar_traits<scalar_type>::value(1);
1229 scalar_type const c1 = one-c;
1230 return qvm_detail::rot_mat_<Dim,Angle>(
1231 x2+(one-x2)*c, xy*c1-zs, xz*(one-c)+ys,
1232 xy*c1+zs, y2+(one-y2)*c, yz*c1-xs,
1233 xz*c1-ys, yz*c1+xs, z2+(one-z2)*c );
1236 template <class A,class B,class Angle>
1237 BOOST_QVM_INLINE_OPERATIONS
1238 typename enable_if_c<
1240 mat_traits<A>::rows==mat_traits<A>::cols &&
1241 mat_traits<A>::rows>=3 &&
1242 is_vec<B>::value && vec_traits<B>::dim==3,
1244 set_rot( A & a, B const & axis, Angle angle )
1246 assign(a,rot_mat<mat_traits<A>::rows>(axis,angle));
1249 template <class A,class B,class Angle>
1250 BOOST_QVM_INLINE_OPERATIONS
1251 typename enable_if_c<
1253 mat_traits<A>::rows==mat_traits<A>::cols &&
1254 mat_traits<A>::rows>=3 &&
1255 is_vec<B>::value && vec_traits<B>::dim==3,
1257 rotate( A & a, B const & axis, Angle angle )
1259 a *= rot_mat<mat_traits<A>::rows>(axis,angle);
1262 ////////////////////////////////////////////////
1264 template <int Dim,class Angle>
1266 qvm_detail::rot_mat_<Dim,Angle>
1267 rot_mat_xzy( Angle x1, Angle z2, Angle y3 )
1269 typedef Angle scalar_type;
1270 scalar_type const c1 = cos<scalar_type>(x1);
1271 scalar_type const s1 = sin<scalar_type>(x1);
1272 scalar_type const c2 = cos<scalar_type>(z2);
1273 scalar_type const s2 = sin<scalar_type>(z2);
1274 scalar_type const c3 = cos<scalar_type>(y3);
1275 scalar_type const s3 = sin<scalar_type>(y3);
1276 return qvm_detail::rot_mat_<Dim,Angle>(
1278 s1*s3 + c1*c3*s2, c1*c2, c1*s2*s3 - c3*s1,
1279 c3*s1*s2 - c1*s3, c2*s1, c1*c3 + s1*s2*s3 );
1282 template <class A,class Angle>
1283 BOOST_QVM_INLINE_OPERATIONS
1284 typename enable_if_c<
1286 mat_traits<A>::rows==mat_traits<A>::cols &&
1287 mat_traits<A>::rows>=3,
1289 set_rot_xzy( A & a, Angle x1, Angle z2, Angle y3 )
1291 assign(a,rot_mat_xzy<mat_traits<A>::rows>(x1,z2,y3));
1294 template <class A,class Angle>
1295 BOOST_QVM_INLINE_OPERATIONS
1296 typename enable_if_c<
1298 mat_traits<A>::rows==mat_traits<A>::cols &&
1299 mat_traits<A>::rows>=3,
1301 rotate_xzy( A & a, Angle x1, Angle z2, Angle y3 )
1303 a *= rot_mat_xzy<mat_traits<A>::rows>(x1,z2,y3);
1306 ////////////////////////////////////////////////
1308 template <int Dim,class Angle>
1310 qvm_detail::rot_mat_<Dim,Angle>
1311 rot_mat_xyz( Angle x1, Angle y2, Angle z3 )
1313 typedef Angle scalar_type;
1314 scalar_type const c1 = cos<scalar_type>(x1);
1315 scalar_type const s1 = sin<scalar_type>(x1);
1316 scalar_type const c2 = cos<scalar_type>(y2);
1317 scalar_type const s2 = sin<scalar_type>(y2);
1318 scalar_type const c3 = cos<scalar_type>(z3);
1319 scalar_type const s3 = sin<scalar_type>(z3);
1320 return qvm_detail::rot_mat_<Dim,Angle>(
1322 c1*s3 + c3*s1*s2, c1*c3 - s1*s2*s3, -c2*s1,
1323 s1*s3 - c1*c3*s2, c3*s1 + c1*s2*s3, c1*c2 );
1326 template <class A,class Angle>
1327 BOOST_QVM_INLINE_OPERATIONS
1328 typename enable_if_c<
1330 mat_traits<A>::rows==mat_traits<A>::cols &&
1331 mat_traits<A>::rows>=3,
1333 set_rot_xyz( A & a, Angle x1, Angle y2, Angle z3 )
1335 assign(a,rot_mat_xyz<mat_traits<A>::rows>(x1,y2,z3));
1338 template <class A,class Angle>
1339 BOOST_QVM_INLINE_OPERATIONS
1340 typename enable_if_c<
1342 mat_traits<A>::rows==mat_traits<A>::cols &&
1343 mat_traits<A>::rows>=3,
1345 rotate_xyz( A & a, Angle x1, Angle y2, Angle z3 )
1347 a *= rot_mat_xyz<mat_traits<A>::rows>(x1,y2,z3);
1350 ////////////////////////////////////////////////
1352 template <int Dim,class Angle>
1354 qvm_detail::rot_mat_<Dim,Angle>
1355 rot_mat_yxz( Angle y1, Angle x2, Angle z3 )
1357 typedef Angle scalar_type;
1358 scalar_type const c1 = cos<scalar_type>(y1);
1359 scalar_type const s1 = sin<scalar_type>(y1);
1360 scalar_type const c2 = cos<scalar_type>(x2);
1361 scalar_type const s2 = sin<scalar_type>(x2);
1362 scalar_type const c3 = cos<scalar_type>(z3);
1363 scalar_type const s3 = sin<scalar_type>(z3);
1364 return qvm_detail::rot_mat_<Dim,Angle>(
1365 c1*c3 + s1*s2*s3, c3*s1*s2 - c1*s3, c2*s1,
1367 c1*s2*s3 - c3*s1, c1*c3*s2 + s1*s3, c1*c2 );
1370 template <class A,class Angle>
1371 BOOST_QVM_INLINE_OPERATIONS
1372 typename enable_if_c<
1374 mat_traits<A>::rows==mat_traits<A>::cols &&
1375 mat_traits<A>::rows>=3,
1377 set_rot_yxz( A & a, Angle y1, Angle x2, Angle z3 )
1379 assign(a,rot_mat_yxz<mat_traits<A>::rows>(y1,x2,z3));
1382 template <class A,class Angle>
1383 BOOST_QVM_INLINE_OPERATIONS
1384 typename enable_if_c<
1386 mat_traits<A>::rows==mat_traits<A>::cols &&
1387 mat_traits<A>::rows>=3,
1389 rotate_yxz( A & a, Angle y1, Angle x2, Angle z3 )
1391 a *= rot_mat_yxz<mat_traits<A>::rows>(y1,x2,z3);
1394 ////////////////////////////////////////////////
1396 template <int Dim,class Angle>
1398 qvm_detail::rot_mat_<Dim,Angle>
1399 rot_mat_yzx( Angle y1, Angle z2, Angle x3 )
1401 typedef Angle scalar_type;
1402 scalar_type const c1 = cos<scalar_type>(y1);
1403 scalar_type const s1 = sin<scalar_type>(y1);
1404 scalar_type const c2 = cos<scalar_type>(z2);
1405 scalar_type const s2 = sin<scalar_type>(z2);
1406 scalar_type const c3 = cos<scalar_type>(x3);
1407 scalar_type const s3 = sin<scalar_type>(x3);
1408 return qvm_detail::rot_mat_<Dim,Angle>(
1409 c1*c2, s1*s3 - c1*c3*s2, c3*s1 + c1*s2*s3,
1411 -c2*s1, c1*s3 + c3*s1*s2, c1*c3 - s1*s2*s3 );
1414 template <class A,class Angle>
1415 BOOST_QVM_INLINE_OPERATIONS
1416 typename enable_if_c<
1418 mat_traits<A>::rows==mat_traits<A>::cols &&
1419 mat_traits<A>::rows>=3,
1421 set_rot_yzx( A & a, Angle y1, Angle z2, Angle x3 )
1423 assign(a,rot_mat_yzx<mat_traits<A>::rows>(y1,z2,x3));
1426 template <class A,class Angle>
1427 BOOST_QVM_INLINE_OPERATIONS
1428 typename enable_if_c<
1430 mat_traits<A>::rows==mat_traits<A>::cols &&
1431 mat_traits<A>::rows>=3,
1433 rotate_yzx( A & a, Angle y1, Angle z2, Angle x3 )
1435 a *= rot_mat_yzx<mat_traits<A>::rows>(y1,z2,x3);
1438 ////////////////////////////////////////////////
1440 template <int Dim,class Angle>
1442 qvm_detail::rot_mat_<Dim,Angle>
1443 rot_mat_zyx( Angle z1, Angle y2, Angle x3 )
1445 typedef Angle scalar_type;
1446 scalar_type const c1 = cos<scalar_type>(z1);
1447 scalar_type const s1 = sin<scalar_type>(z1);
1448 scalar_type const c2 = cos<scalar_type>(y2);
1449 scalar_type const s2 = sin<scalar_type>(y2);
1450 scalar_type const c3 = cos<scalar_type>(x3);
1451 scalar_type const s3 = sin<scalar_type>(x3);
1452 return qvm_detail::rot_mat_<Dim,Angle>(
1453 c1*c2, c1*s2*s3 - c3*s1, s1*s3 + c1*c3*s2,
1454 c2*s1, c1*c3 + s1*s2*s3, c3*s1*s2 - c1*s3,
1455 -s2, c2*s3, c2*c3 );
1458 template <class A,class Angle>
1459 BOOST_QVM_INLINE_OPERATIONS
1460 typename enable_if_c<
1462 mat_traits<A>::rows==mat_traits<A>::cols &&
1463 mat_traits<A>::rows>=3,
1465 set_rot_zyx( A & a, Angle z1, Angle y2, Angle x3 )
1467 assign(a,rot_mat_zyx<mat_traits<A>::rows>(z1,y2,x3));
1470 template <class A,class Angle>
1471 BOOST_QVM_INLINE_OPERATIONS
1472 typename enable_if_c<
1474 mat_traits<A>::rows==mat_traits<A>::cols &&
1475 mat_traits<A>::rows>=3,
1477 rotate_zyx( A & a, Angle z1, Angle y2, Angle x3 )
1479 a *= rot_mat_zyx<mat_traits<A>::rows>(z1,y2,x3);
1482 ////////////////////////////////////////////////
1484 template <int Dim,class Angle>
1486 qvm_detail::rot_mat_<Dim,Angle>
1487 rot_mat_zxy( Angle z1, Angle x2, Angle y3 )
1489 typedef Angle scalar_type;
1490 scalar_type const c1 = cos<scalar_type>(z1);
1491 scalar_type const s1 = sin<scalar_type>(z1);
1492 scalar_type const c2 = cos<scalar_type>(x2);
1493 scalar_type const s2 = sin<scalar_type>(x2);
1494 scalar_type const c3 = cos<scalar_type>(y3);
1495 scalar_type const s3 = sin<scalar_type>(y3);
1496 return qvm_detail::rot_mat_<Dim,Angle>(
1497 c1*c3 - s1*s2*s3, -c2*s1, c1*s3 + c3*s1*s2,
1498 c3*s1 + c1*s2*s3, c1*c2, s1*s3 - c1*c3*s2,
1499 -c2*s3, s2, c2*c3 );
1502 template <class A,class Angle>
1503 BOOST_QVM_INLINE_OPERATIONS
1504 typename enable_if_c<
1506 mat_traits<A>::rows==mat_traits<A>::cols &&
1507 mat_traits<A>::rows>=3,
1509 set_rot_zxy( A & a, Angle z1, Angle x2, Angle y3 )
1511 assign(a,rot_mat_zxy<mat_traits<A>::rows>(z1,x2,y3));
1514 template <class A,class Angle>
1515 BOOST_QVM_INLINE_OPERATIONS
1516 typename enable_if_c<
1518 mat_traits<A>::rows==mat_traits<A>::cols &&
1519 mat_traits<A>::rows>=3,
1521 rotate_zxy( A & a, Angle z1, Angle x2, Angle y3 )
1523 a *= rot_mat_zxy<mat_traits<A>::rows>(z1,x2,y3);
1526 ////////////////////////////////////////////////
1528 template <int Dim,class Angle>
1530 qvm_detail::rot_mat_<Dim,Angle>
1531 rot_mat_xzx( Angle x1, Angle z2, Angle x3 )
1533 typedef Angle scalar_type;
1534 scalar_type const c1 = cos<scalar_type>(x1);
1535 scalar_type const s1 = sin<scalar_type>(x1);
1536 scalar_type const c2 = cos<scalar_type>(z2);
1537 scalar_type const s2 = sin<scalar_type>(z2);
1538 scalar_type const c3 = cos<scalar_type>(x3);
1539 scalar_type const s3 = sin<scalar_type>(x3);
1540 return qvm_detail::rot_mat_<Dim,Angle>(
1542 c1*s2, c1*c2*c3 - s1*s3, -c3*s1 - c1*c2*s3,
1543 s1*s2, c1*s3 + c2*c3*s1, c1*c3 - c2*s1*s3 );
1546 template <class A,class Angle>
1547 BOOST_QVM_INLINE_OPERATIONS
1548 typename enable_if_c<
1550 mat_traits<A>::rows==mat_traits<A>::cols &&
1551 mat_traits<A>::rows>=3,
1553 set_rot_xzx( A & a, Angle x1, Angle z2, Angle x3 )
1555 assign(a,rot_mat_xzx<mat_traits<A>::rows>(x1,z2,x3));
1558 template <class A,class Angle>
1559 BOOST_QVM_INLINE_OPERATIONS
1560 typename enable_if_c<
1562 mat_traits<A>::rows==mat_traits<A>::cols &&
1563 mat_traits<A>::rows>=3,
1565 rotate_xzx( A & a, Angle x1, Angle z2, Angle x3 )
1567 a *= rot_mat_xzx<mat_traits<A>::rows>(x1,z2,x3);
1570 ////////////////////////////////////////////////
1572 template <int Dim,class Angle>
1574 qvm_detail::rot_mat_<Dim,Angle>
1575 rot_mat_xyx( Angle x1, Angle y2, Angle x3 )
1577 typedef Angle scalar_type;
1578 scalar_type const c1 = cos<scalar_type>(x1);
1579 scalar_type const s1 = sin<scalar_type>(x1);
1580 scalar_type const c2 = cos<scalar_type>(y2);
1581 scalar_type const s2 = sin<scalar_type>(y2);
1582 scalar_type const c3 = cos<scalar_type>(x3);
1583 scalar_type const s3 = sin<scalar_type>(x3);
1584 return qvm_detail::rot_mat_<Dim,Angle>(
1586 s1*s2, c1*c3 - c2*s1*s3, -c1*s3 - c2*c3*s1,
1587 -c1*s2, c3*s1 + c1*c2*s3, c1*c2*c3 - s1*s3 );
1590 template <class A,class Angle>
1591 BOOST_QVM_INLINE_OPERATIONS
1592 typename enable_if_c<
1594 mat_traits<A>::rows==mat_traits<A>::cols &&
1595 mat_traits<A>::rows>=3,
1597 set_rot_xyx( A & a, Angle x1, Angle y2, Angle x3 )
1599 assign(a,rot_mat_xyx<mat_traits<A>::rows>(x1,y2,x3));
1602 template <class A,class Angle>
1603 BOOST_QVM_INLINE_OPERATIONS
1604 typename enable_if_c<
1606 mat_traits<A>::rows==mat_traits<A>::cols &&
1607 mat_traits<A>::rows>=3,
1609 rotate_xyx( A & a, Angle x1, Angle y2, Angle x3 )
1611 a *= rot_mat_xyx<mat_traits<A>::rows>(x1,y2,x3);
1614 ////////////////////////////////////////////////
1616 template <int Dim,class Angle>
1618 qvm_detail::rot_mat_<Dim,Angle>
1619 rot_mat_yxy( Angle y1, Angle x2, Angle y3 )
1621 typedef Angle scalar_type;
1622 scalar_type const c1 = cos<scalar_type>(y1);
1623 scalar_type const s1 = sin<scalar_type>(y1);
1624 scalar_type const c2 = cos<scalar_type>(x2);
1625 scalar_type const s2 = sin<scalar_type>(x2);
1626 scalar_type const c3 = cos<scalar_type>(y3);
1627 scalar_type const s3 = sin<scalar_type>(y3);
1628 return qvm_detail::rot_mat_<Dim,Angle>(
1629 c1*c3 - c2*s1*s3, s1*s2, c1*s3 + c2*c3*s1,
1631 -c3*s1 - c1*c2*s3, c1*s2, c1*c2*c3 - s1*s3 );
1634 template <class A,class Angle>
1635 BOOST_QVM_INLINE_OPERATIONS
1636 typename enable_if_c<
1638 mat_traits<A>::rows==mat_traits<A>::cols &&
1639 mat_traits<A>::rows>=3,
1641 set_rot_yxy( A & a, Angle y1, Angle x2, Angle y3 )
1643 assign(a,rot_mat_yxy<mat_traits<A>::rows>(y1,x2,y3));
1646 template <class A,class Angle>
1647 BOOST_QVM_INLINE_OPERATIONS
1648 typename enable_if_c<
1650 mat_traits<A>::rows==mat_traits<A>::cols &&
1651 mat_traits<A>::rows>=3,
1653 rotate_yxy( A & a, Angle y1, Angle x2, Angle y3 )
1655 a *= rot_mat_yxy<mat_traits<A>::rows>(y1,x2,y3);
1658 ////////////////////////////////////////////////
1660 template <int Dim,class Angle>
1662 qvm_detail::rot_mat_<Dim,Angle>
1663 rot_mat_yzy( Angle y1, Angle z2, Angle y3 )
1665 typedef Angle scalar_type;
1666 scalar_type const c1 = cos<scalar_type>(y1);
1667 scalar_type const s1 = sin<scalar_type>(y1);
1668 scalar_type const c2 = cos<scalar_type>(z2);
1669 scalar_type const s2 = sin<scalar_type>(z2);
1670 scalar_type const c3 = cos<scalar_type>(y3);
1671 scalar_type const s3 = sin<scalar_type>(y3);
1672 return qvm_detail::rot_mat_<Dim,Angle>(
1673 c1*c2*c3 - s1*s3, -c1*s2, c3*s1 + c1*c2*s3,
1675 -c1*s3 - c2*c3*s1, s1*s2, c1*c3 - c2*s1*s3 );
1678 template <class A,class Angle>
1679 BOOST_QVM_INLINE_OPERATIONS
1680 typename enable_if_c<
1682 mat_traits<A>::rows==mat_traits<A>::cols &&
1683 mat_traits<A>::rows>=3,
1685 set_rot_yzy( A & a, Angle y1, Angle z2, Angle y3 )
1687 assign(a,rot_mat_yzy<mat_traits<A>::rows>(y1,z2,y3));
1690 template <class A,class Angle>
1691 BOOST_QVM_INLINE_OPERATIONS
1692 typename enable_if_c<
1694 mat_traits<A>::rows==mat_traits<A>::cols &&
1695 mat_traits<A>::rows>=3,
1697 rotate_yzy( A & a, Angle y1, Angle z2, Angle y3 )
1699 a *= rot_mat_yzy<mat_traits<A>::rows>(y1,z2,y3);
1702 ////////////////////////////////////////////////
1704 template <int Dim,class Angle>
1706 qvm_detail::rot_mat_<Dim,Angle>
1707 rot_mat_zyz( Angle z1, Angle y2, Angle z3 )
1709 typedef Angle scalar_type;
1710 scalar_type const c1 = cos<scalar_type>(z1);
1711 scalar_type const s1 = sin<scalar_type>(z1);
1712 scalar_type const c2 = cos<scalar_type>(y2);
1713 scalar_type const s2 = sin<scalar_type>(y2);
1714 scalar_type const c3 = cos<scalar_type>(z3);
1715 scalar_type const s3 = sin<scalar_type>(z3);
1716 return qvm_detail::rot_mat_<Dim,Angle>(
1717 c1*c2*c3 - s1*s3, -c3*s1 - c1*c2*s3, c1*s2,
1718 c1*s3 + c2*c3*s1, c1*c3 - c2*s1*s3, s1*s2,
1719 -c3*s2, s2*s3, c2 );
1722 template <class A,class Angle>
1723 BOOST_QVM_INLINE_OPERATIONS
1724 typename enable_if_c<
1726 mat_traits<A>::rows==mat_traits<A>::cols &&
1727 mat_traits<A>::rows>=3,
1729 set_rot_zyz( A & a, Angle z1, Angle y2, Angle z3 )
1731 assign(a,rot_mat_zyz<mat_traits<A>::rows>(z1,y2,z3));
1734 template <class A,class Angle>
1735 BOOST_QVM_INLINE_OPERATIONS
1736 typename enable_if_c<
1738 mat_traits<A>::rows==mat_traits<A>::cols &&
1739 mat_traits<A>::rows>=3,
1741 rotate_zyz( A & a, Angle z1, Angle y2, Angle z3 )
1743 a *= rot_mat_zyz<mat_traits<A>::rows>(z1,y2,z3);
1746 ////////////////////////////////////////////////
1748 template <int Dim,class Angle>
1750 qvm_detail::rot_mat_<Dim,Angle>
1751 rot_mat_zxz( Angle z1, Angle x2, Angle z3 )
1753 typedef Angle scalar_type;
1754 scalar_type const c1 = cos<scalar_type>(z1);
1755 scalar_type const s1 = sin<scalar_type>(z1);
1756 scalar_type const c2 = cos<scalar_type>(x2);
1757 scalar_type const s2 = sin<scalar_type>(x2);
1758 scalar_type const c3 = cos<scalar_type>(z3);
1759 scalar_type const s3 = sin<scalar_type>(z3);
1760 return qvm_detail::rot_mat_<Dim,Angle>(
1761 c1*c3 - c2*s1*s3, -c1*s3 - c2*c3*s1, s1*s2,
1762 c3*s1 + c1*c2*s3, c1*c2*c3 - s1*s3, -c1*s2,
1766 template <class A,class Angle>
1767 BOOST_QVM_INLINE_OPERATIONS
1768 typename enable_if_c<
1770 mat_traits<A>::rows==mat_traits<A>::cols &&
1771 mat_traits<A>::rows>=3,
1773 set_rot_zxz( A & a, Angle z1, Angle x2, Angle z3 )
1775 assign(a,rot_mat_zxz<mat_traits<A>::rows>(z1,x2,z3));
1778 template <class A,class Angle>
1779 BOOST_QVM_INLINE_OPERATIONS
1780 typename enable_if_c<
1782 mat_traits<A>::rows==mat_traits<A>::cols &&
1783 mat_traits<A>::rows>=3,
1785 rotate_zxz( A & a, Angle z1, Angle x2, Angle z3 )
1787 a *= rot_mat_zxz<mat_traits<A>::rows>(z1,x2,z3);
1790 ////////////////////////////////////////////////
1795 template <int Dim,class Angle>
1799 BOOST_QVM_INLINE_TRIVIAL
1805 BOOST_QVM_INLINE_TRIVIAL
1815 rotx_mat_( rotx_mat_ const & );
1816 rotx_mat_ & operator=( rotx_mat_ const & );
1820 template <int Row,int Col>
1826 BOOST_QVM_INLINE_CRITICAL
1830 return scalar_traits<T>::value(Row==Col);
1840 BOOST_QVM_INLINE_CRITICAL
1842 get( T const & angle )
1844 return cos<T>(angle);
1854 BOOST_QVM_INLINE_CRITICAL
1856 get( T const & angle )
1858 return -sin<T>(angle);
1868 BOOST_QVM_INLINE_CRITICAL
1870 get( T const & angle )
1872 return sin<T>(angle);
1882 BOOST_QVM_INLINE_CRITICAL
1884 get( T const & angle )
1886 return cos<T>(angle);
1891 template <int Dim,class Angle>
1893 mat_traits< qvm_detail::rotx_mat_<Dim,Angle> >
1895 typedef qvm_detail::rotx_mat_<Dim,Angle> this_matrix;
1896 typedef Angle scalar_type;
1897 static int const rows=Dim;
1898 static int const cols=Dim;
1900 template <int Row,int Col>
1902 BOOST_QVM_INLINE_CRITICAL
1904 read_element( this_matrix const & x )
1906 BOOST_QVM_STATIC_ASSERT(Row>=0);
1907 BOOST_QVM_STATIC_ASSERT(Col>=0);
1908 BOOST_QVM_STATIC_ASSERT(Row<Dim);
1909 BOOST_QVM_STATIC_ASSERT(Col<Dim);
1910 return qvm_detail::rotx_m_get<Row,Col>::get(reinterpret_cast<Angle const &>(x));
1914 BOOST_QVM_INLINE_CRITICAL
1916 read_element_idx( int row, int col, this_matrix const & x )
1918 BOOST_QVM_ASSERT(row>=0);
1919 BOOST_QVM_ASSERT(col>=0);
1920 BOOST_QVM_ASSERT(row<Dim);
1921 BOOST_QVM_ASSERT(col<Dim);
1922 Angle const & a=reinterpret_cast<Angle const &>(x);
1926 return cos<scalar_type>(a);
1928 return -sin<scalar_type>(a);
1933 return sin<scalar_type>(a);
1935 return cos<scalar_type>(a);
1937 return scalar_traits<scalar_type>::value(row==col);
1941 template <int Dim,class Angle>
1943 deduce_mat<qvm_detail::rotx_mat_<Dim,Angle>,Dim,Dim>
1945 typedef mat<Angle,Dim,Dim> type;
1948 template <int Dim,class Angle>
1950 deduce_mat2<qvm_detail::rotx_mat_<Dim,Angle>,qvm_detail::rotx_mat_<Dim,Angle>,Dim,Dim>
1952 typedef mat<Angle,Dim,Dim> type;
1955 template <int Dim,class Angle>
1956 BOOST_QVM_INLINE_TRIVIAL
1957 qvm_detail::rotx_mat_<Dim,Angle> const &
1958 rotx_mat( Angle const & angle )
1960 BOOST_QVM_STATIC_ASSERT(Dim>=3);
1961 return reinterpret_cast<qvm_detail::rotx_mat_<Dim,Angle> const &>(angle);
1964 template <class A,class Angle>
1965 BOOST_QVM_INLINE_OPERATIONS
1966 typename enable_if_c<
1968 mat_traits<A>::rows>=3 &&
1969 mat_traits<A>::rows==mat_traits<A>::cols,
1971 set_rotx( A & a, Angle angle )
1973 assign(a,rotx_mat<mat_traits<A>::rows>(angle));
1976 template <class A,class Angle>
1977 BOOST_QVM_INLINE_OPERATIONS
1978 typename enable_if_c<
1980 mat_traits<A>::rows>=3 &&
1981 mat_traits<A>::rows==mat_traits<A>::cols,
1983 rotate_x( A & a, Angle angle )
1985 a *= rotx_mat<mat_traits<A>::rows>(angle);
1988 ////////////////////////////////////////////////
1993 template <int Dim,class Angle>
1997 BOOST_QVM_INLINE_TRIVIAL
2003 BOOST_QVM_INLINE_TRIVIAL
2013 roty_mat_( roty_mat_ const & );
2014 roty_mat_ & operator=( roty_mat_ const & );
2018 template <int Row,int Col>
2024 BOOST_QVM_INLINE_CRITICAL
2028 return scalar_traits<T>::value(Row==Col);
2038 BOOST_QVM_INLINE_CRITICAL
2040 get( T const & angle )
2042 return cos<T>(angle);
2052 BOOST_QVM_INLINE_CRITICAL
2054 get( T const & angle )
2056 return sin<T>(angle);
2066 BOOST_QVM_INLINE_CRITICAL
2068 get( T const & angle )
2070 return -sin<T>(angle);
2080 BOOST_QVM_INLINE_CRITICAL
2082 get( T const & angle )
2084 return cos<T>(angle);
2089 template <int Dim,class Angle>
2091 mat_traits< qvm_detail::roty_mat_<Dim,Angle> >
2093 typedef qvm_detail::roty_mat_<Dim,Angle> this_matrix;
2094 typedef Angle scalar_type;
2095 static int const rows=Dim;
2096 static int const cols=Dim;
2098 template <int Row,int Col>
2100 BOOST_QVM_INLINE_CRITICAL
2102 read_element( this_matrix const & x )
2104 BOOST_QVM_STATIC_ASSERT(Row>=0);
2105 BOOST_QVM_STATIC_ASSERT(Col>=0);
2106 BOOST_QVM_STATIC_ASSERT(Row<Dim);
2107 BOOST_QVM_STATIC_ASSERT(Col<Dim);
2108 return qvm_detail::roty_m_get<Row,Col>::get(reinterpret_cast<Angle const &>(x));
2112 BOOST_QVM_INLINE_CRITICAL
2114 read_element_idx( int row, int col, this_matrix const & x )
2116 BOOST_QVM_ASSERT(row>=0);
2117 BOOST_QVM_ASSERT(col>=0);
2118 BOOST_QVM_ASSERT(row<Dim);
2119 BOOST_QVM_ASSERT(col<Dim);
2120 Angle const & a=reinterpret_cast<Angle const &>(x);
2124 return cos<scalar_type>(a);
2126 return sin<scalar_type>(a);
2131 return -sin<scalar_type>(a);
2133 return cos<scalar_type>(a);
2135 return scalar_traits<scalar_type>::value(row==col);
2139 template <int Dim,class Angle>
2141 deduce_mat<qvm_detail::roty_mat_<Dim,Angle>,Dim,Dim>
2143 typedef mat<Angle,Dim,Dim> type;
2146 template <int Dim,class Angle>
2148 deduce_mat2<qvm_detail::roty_mat_<Dim,Angle>,qvm_detail::roty_mat_<Dim,Angle>,Dim,Dim>
2150 typedef mat<Angle,Dim,Dim> type;
2153 template <int Dim,class Angle>
2154 BOOST_QVM_INLINE_TRIVIAL
2155 qvm_detail::roty_mat_<Dim,Angle> const &
2156 roty_mat( Angle const & angle )
2158 BOOST_QVM_STATIC_ASSERT(Dim>=3);
2159 return reinterpret_cast<qvm_detail::roty_mat_<Dim,Angle> const &>(angle);
2162 template <class A,class Angle>
2163 BOOST_QVM_INLINE_OPERATIONS
2164 typename enable_if_c<
2166 mat_traits<A>::rows>=2 &&
2167 mat_traits<A>::rows==mat_traits<A>::cols,
2169 set_roty( A & a, Angle angle )
2171 assign(a,roty_mat<mat_traits<A>::rows>(angle));
2174 template <class A,class Angle>
2175 BOOST_QVM_INLINE_OPERATIONS
2176 typename enable_if_c<
2178 mat_traits<A>::rows>=3 &&
2179 mat_traits<A>::rows==mat_traits<A>::cols,
2181 rotate_y( A & a, Angle angle )
2183 a *= roty_mat<mat_traits<A>::rows>(angle);
2186 ////////////////////////////////////////////////
2191 template <int Dim,class Angle>
2195 BOOST_QVM_INLINE_TRIVIAL
2201 BOOST_QVM_INLINE_TRIVIAL
2211 rotz_mat_( rotz_mat_ const & );
2212 rotz_mat_ & operator=( rotz_mat_ const & );
2216 template <int Row,int Col>
2222 BOOST_QVM_INLINE_CRITICAL
2226 return scalar_traits<T>::value(Row==Col);
2236 BOOST_QVM_INLINE_CRITICAL
2238 get( T const & angle )
2240 return cos<T>(angle);
2250 BOOST_QVM_INLINE_CRITICAL
2252 get( T const & angle )
2254 return -sin<T>(angle);
2264 BOOST_QVM_INLINE_CRITICAL
2266 get( T const & angle )
2268 return sin<T>(angle);
2278 BOOST_QVM_INLINE_CRITICAL
2280 get( T const & angle )
2282 return cos<T>(angle);
2287 template <int Dim,class Angle>
2289 mat_traits< qvm_detail::rotz_mat_<Dim,Angle> >
2291 typedef qvm_detail::rotz_mat_<Dim,Angle> this_matrix;
2292 typedef Angle scalar_type;
2293 static int const rows=Dim;
2294 static int const cols=Dim;
2296 template <int Row,int Col>
2298 BOOST_QVM_INLINE_CRITICAL
2300 read_element( this_matrix const & x )
2302 BOOST_QVM_STATIC_ASSERT(Row>=0);
2303 BOOST_QVM_STATIC_ASSERT(Col>=0);
2304 BOOST_QVM_STATIC_ASSERT(Row<Dim);
2305 BOOST_QVM_STATIC_ASSERT(Col<Dim);
2306 return qvm_detail::rotz_m_get<Row,Col>::get(reinterpret_cast<Angle const &>(x));
2310 BOOST_QVM_INLINE_CRITICAL
2312 read_element_idx( int row, int col, this_matrix const & x )
2314 BOOST_QVM_ASSERT(row>=0);
2315 BOOST_QVM_ASSERT(col>=0);
2316 BOOST_QVM_ASSERT(row<Dim);
2317 BOOST_QVM_ASSERT(col<Dim);
2318 Angle const & a=reinterpret_cast<Angle const &>(x);
2322 return cos<scalar_type>(a);
2324 return -sin<scalar_type>(a);
2329 return sin<scalar_type>(a);
2331 return cos<scalar_type>(a);
2333 return scalar_traits<scalar_type>::value(row==col);
2337 template <int Dim,class Angle>
2339 deduce_mat<qvm_detail::rotz_mat_<Dim,Angle>,Dim,Dim>
2341 typedef mat<Angle,Dim,Dim> type;
2344 template <int Dim,class Angle,int R,int C>
2346 deduce_mat2<qvm_detail::rotz_mat_<Dim,Angle>,qvm_detail::rotz_mat_<Dim,Angle>,R,C>
2348 typedef mat<Angle,R,C> type;
2351 template <int Dim,class Angle>
2352 BOOST_QVM_INLINE_TRIVIAL
2353 qvm_detail::rotz_mat_<Dim,Angle> const &
2354 rotz_mat( Angle const & angle )
2356 BOOST_QVM_STATIC_ASSERT(Dim>=2);
2357 return reinterpret_cast<qvm_detail::rotz_mat_<Dim,Angle> const &>(angle);
2360 template <class A,class Angle>
2361 BOOST_QVM_INLINE_OPERATIONS
2362 typename enable_if_c<
2364 mat_traits<A>::rows>=2 &&
2365 mat_traits<A>::rows==mat_traits<A>::cols,
2367 set_rotz( A & a, Angle angle )
2369 assign(a,rotz_mat<mat_traits<A>::rows>(angle));
2372 template <class A,class Angle>
2373 BOOST_QVM_INLINE_OPERATIONS
2374 typename enable_if_c<
2376 mat_traits<A>::rows>=2 &&
2377 mat_traits<A>::rows==mat_traits<A>::cols,
2379 rotate_z( A & a, Angle angle )
2381 a *= rotz_mat<mat_traits<A>::rows>(angle);
2384 ////////////////////////////////////////////////
2393 static bool const value=false;
2397 template <class A,class B>
2398 BOOST_QVM_INLINE_TRIVIAL
2399 typename lazy_enable_if_c<
2400 is_mat<A>::value && is_scalar<B>::value &&
2401 mat_traits<A>::rows==mat_traits<A>::cols &&
2402 !qvm_detail::inverse_m_defined<mat_traits<A>::rows>::value,
2403 deduce_mat<A> >::type
2404 inverse( A const & a, B det )
2406 typedef typename mat_traits<A>::scalar_type T;
2407 BOOST_QVM_ASSERT(det!=scalar_traits<T>::value(0));
2408 T f=scalar_traits<T>::value(1)/det;
2409 typedef typename deduce_mat<A>::type cofactor_return_type;
2410 cofactor_return_type c=qvm_detail::cofactor_impl(a);
2411 return reinterpret_cast<qvm_detail::transposed_<cofactor_return_type> const &>(c) * f;
2415 BOOST_QVM_INLINE_TRIVIAL
2416 typename lazy_enable_if_c<
2418 mat_traits<A>::rows==mat_traits<A>::cols &&
2419 !qvm_detail::inverse_m_defined<mat_traits<A>::rows>::value,
2420 deduce_mat<A> >::type
2421 inverse( A const & a )
2423 typedef typename mat_traits<A>::scalar_type T;
2424 T det=determinant(a);
2425 if( det==scalar_traits<T>::value(0) )
2426 BOOST_QVM_THROW_EXCEPTION(zero_determinant_error());
2427 return inverse(a,det);
2430 ////////////////////////////////////////////////
2435 using ::boost::qvm::to_string;
2436 using ::boost::qvm::assign;
2437 using ::boost::qvm::determinant;
2438 using ::boost::qvm::cmp;
2439 using ::boost::qvm::convert_to;
2440 using ::boost::qvm::set_identity;
2441 using ::boost::qvm::set_zero;
2442 using ::boost::qvm::scalar_cast;
2443 using ::boost::qvm::operator/=;
2444 using ::boost::qvm::operator/;
2445 using ::boost::qvm::operator==;
2446 using ::boost::qvm::operator-=;
2447 using ::boost::qvm::operator-;
2448 using ::boost::qvm::operator*=;
2449 using ::boost::qvm::operator*;
2450 using ::boost::qvm::operator!=;
2451 using ::boost::qvm::operator+=;
2452 using ::boost::qvm::operator+;
2453 using ::boost::qvm::mref;
2454 using ::boost::qvm::rot_mat;
2455 using ::boost::qvm::set_rot;
2456 using ::boost::qvm::rotate;
2457 using ::boost::qvm::set_rotx;
2458 using ::boost::qvm::rotate_x;
2459 using ::boost::qvm::set_roty;
2460 using ::boost::qvm::rotate_y;
2461 using ::boost::qvm::set_rotz;
2462 using ::boost::qvm::rotate_z;
2463 using ::boost::qvm::inverse;
2466 ////////////////////////////////////////////////