vec_mat_operations.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //Copyright (c) 2008-2016 Emil Dotchevski and Reverge Studios, Inc.
  2. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #ifndef UUID_A61EC088D31511DFA59D2B03E0D72085
  5. #define UUID_A61EC088D31511DFA59D2B03E0D72085
  6. #include <boost/qvm/vec_mat_operations2.hpp>
  7. #include <boost/qvm/vec_mat_operations3.hpp>
  8. #include <boost/qvm/vec_mat_operations4.hpp>
  9. namespace
  10. boost
  11. {
  12. namespace
  13. qvm
  14. {
  15. ////////////////////////////////////////////////
  16. namespace
  17. qvm_detail
  18. {
  19. template <int M,int N>
  20. struct
  21. mul_mv_defined
  22. {
  23. static bool const value=false;
  24. };
  25. }
  26. template <class A,class B>
  27. BOOST_QVM_INLINE_OPERATIONS
  28. typename lazy_enable_if_c<
  29. is_mat<A>::value && is_vec<B>::value &&
  30. mat_traits<A>::cols==vec_traits<B>::dim &&
  31. !qvm_detail::mul_mv_defined<mat_traits<A>::rows,mat_traits<A>::cols>::value,
  32. deduce_vec2<A,B,mat_traits<A>::rows> >::type
  33. operator*( A const & a, B const & b )
  34. {
  35. typedef typename deduce_vec2<A,B,mat_traits<A>::rows>::type R;
  36. R r;
  37. for( int i=0; i<mat_traits<A>::rows; ++i )
  38. {
  39. typedef typename vec_traits<R>::scalar_type Tr;
  40. Tr x(scalar_traits<Tr>::value(0));
  41. for( int j=0; j<mat_traits<A>::cols; ++j )
  42. x += mat_traits<A>::read_element_idx(i,j,a)*vec_traits<B>::read_element_idx(j,b);
  43. vec_traits<R>::write_element_idx(i,r) = x;
  44. }
  45. return r;
  46. }
  47. namespace
  48. qvm_detail
  49. {
  50. template <int M,int N>
  51. struct
  52. mul_vm_defined
  53. {
  54. static bool const value=false;
  55. };
  56. }
  57. template <class A,class B>
  58. BOOST_QVM_INLINE_OPERATIONS
  59. typename lazy_enable_if_c<
  60. is_vec<A>::value && is_mat<B>::value &&
  61. vec_traits<A>::dim==mat_traits<B>::rows &&
  62. !qvm_detail::mul_vm_defined<mat_traits<B>::rows,mat_traits<B>::cols>::value,
  63. deduce_vec2<A,B,mat_traits<B>::cols> >::type
  64. operator*( A const & a, B const & b )
  65. {
  66. typedef typename deduce_vec2<A,B,mat_traits<B>::cols>::type R;
  67. R r;
  68. for( int i=0; i<mat_traits<B>::cols; ++i )
  69. {
  70. typedef typename vec_traits<R>::scalar_type Tr;
  71. Tr x(scalar_traits<Tr>::value(0));
  72. for( int j=0; j<mat_traits<B>::rows; ++j )
  73. x += vec_traits<A>::read_element_idx(j,a)*mat_traits<B>::read_element_idx(j,i,b);
  74. vec_traits<R>::write_element_idx(i,r) = x;
  75. }
  76. return r;
  77. }
  78. ////////////////////////////////////////////////
  79. template <class A,class B>
  80. BOOST_QVM_INLINE_OPERATIONS
  81. typename lazy_enable_if_c<
  82. mat_traits<A>::rows==4 && mat_traits<A>::cols==4 &&
  83. vec_traits<B>::dim==3,
  84. deduce_vec2<A,B,3> >::type
  85. transform_point( A const & a, B const & b )
  86. {
  87. typedef typename mat_traits<A>::scalar_type Ta;
  88. typedef typename vec_traits<B>::scalar_type Tb;
  89. Ta const a00 = mat_traits<A>::template read_element<0,0>(a);
  90. Ta const a01 = mat_traits<A>::template read_element<0,1>(a);
  91. Ta const a02 = mat_traits<A>::template read_element<0,2>(a);
  92. Ta const a03 = mat_traits<A>::template read_element<0,3>(a);
  93. Ta const a10 = mat_traits<A>::template read_element<1,0>(a);
  94. Ta const a11 = mat_traits<A>::template read_element<1,1>(a);
  95. Ta const a12 = mat_traits<A>::template read_element<1,2>(a);
  96. Ta const a13 = mat_traits<A>::template read_element<1,3>(a);
  97. Ta const a20 = mat_traits<A>::template read_element<2,0>(a);
  98. Ta const a21 = mat_traits<A>::template read_element<2,1>(a);
  99. Ta const a22 = mat_traits<A>::template read_element<2,2>(a);
  100. Ta const a23 = mat_traits<A>::template read_element<2,3>(a);
  101. Tb const b0 = vec_traits<B>::template read_element<0>(b);
  102. Tb const b1 = vec_traits<B>::template read_element<1>(b);
  103. Tb const b2 = vec_traits<B>::template read_element<2>(b);
  104. typedef typename deduce_vec2<A,B,3>::type R;
  105. BOOST_QVM_STATIC_ASSERT(vec_traits<R>::dim==3);
  106. R r;
  107. vec_traits<R>::template write_element<0>(r)=a00*b0+a01*b1+a02*b2+a03;
  108. vec_traits<R>::template write_element<1>(r)=a10*b0+a11*b1+a12*b2+a13;
  109. vec_traits<R>::template write_element<2>(r)=a20*b0+a21*b1+a22*b2+a23;
  110. return r;
  111. }
  112. template <class A,class B>
  113. BOOST_QVM_INLINE_OPERATIONS
  114. typename lazy_enable_if_c<
  115. mat_traits<A>::rows==4 && mat_traits<A>::cols==4 &&
  116. vec_traits<B>::dim==3,
  117. deduce_vec2<A,B,3> >::type
  118. transform_vector( A const & a, B const & b )
  119. {
  120. typedef typename mat_traits<A>::scalar_type Ta;
  121. typedef typename vec_traits<B>::scalar_type Tb;
  122. Ta const a00 = mat_traits<A>::template read_element<0,0>(a);
  123. Ta const a01 = mat_traits<A>::template read_element<0,1>(a);
  124. Ta const a02 = mat_traits<A>::template read_element<0,2>(a);
  125. Ta const a10 = mat_traits<A>::template read_element<1,0>(a);
  126. Ta const a11 = mat_traits<A>::template read_element<1,1>(a);
  127. Ta const a12 = mat_traits<A>::template read_element<1,2>(a);
  128. Ta const a20 = mat_traits<A>::template read_element<2,0>(a);
  129. Ta const a21 = mat_traits<A>::template read_element<2,1>(a);
  130. Ta const a22 = mat_traits<A>::template read_element<2,2>(a);
  131. Tb const b0 = vec_traits<B>::template read_element<0>(b);
  132. Tb const b1 = vec_traits<B>::template read_element<1>(b);
  133. Tb const b2 = vec_traits<B>::template read_element<2>(b);
  134. typedef typename deduce_vec2<A,B,3>::type R;
  135. BOOST_QVM_STATIC_ASSERT(vec_traits<R>::dim==3);
  136. R r;
  137. vec_traits<R>::template write_element<0>(r)=a00*b0+a01*b1+a02*b2;
  138. vec_traits<R>::template write_element<1>(r)=a10*b0+a11*b1+a12*b2;
  139. vec_traits<R>::template write_element<2>(r)=a20*b0+a21*b1+a22*b2;
  140. return r;
  141. }
  142. ////////////////////////////////////////////////
  143. namespace
  144. sfinae
  145. {
  146. using ::boost::qvm::operator*;
  147. using ::boost::qvm::transform_point;
  148. using ::boost::qvm::transform_vector;
  149. }
  150. ////////////////////////////////////////////////
  151. }
  152. }
  153. #endif