quat_traits_array.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #ifndef BOOST_QVM_QUAT_TRAITS_ARRAY_HPP_INCLUDED
  2. #define BOOST_QVM_QUAT_TRAITS_ARRAY_HPP_INCLUDED
  3. // Copyright 2008-2022 Emil Dotchevski and Reverge Studios, Inc.
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/qvm/config.hpp>
  7. #include <boost/qvm/deduce_quat.hpp>
  8. #include <boost/qvm/assert.hpp>
  9. #if __cplusplus > 199711L
  10. #include <array>
  11. namespace boost { namespace qvm {
  12. template <class T,std::size_t D>
  13. struct
  14. quat_traits<std::array<T,D>>
  15. {
  16. typedef void scalar_type;
  17. };
  18. template <class T,std::size_t D>
  19. struct
  20. quat_traits<std::array<std::array<T,D>,4> >
  21. {
  22. typedef void scalar_type;
  23. };
  24. template <class T,std::size_t D>
  25. struct
  26. quat_traits<std::array<std::array<T,4>,D> >
  27. {
  28. typedef void scalar_type;
  29. };
  30. template <class T>
  31. struct
  32. quat_traits<std::array<std::array<T,4>,4> >
  33. {
  34. typedef void scalar_type;
  35. };
  36. template <class T,std::size_t M,std::size_t N>
  37. struct
  38. quat_traits<std::array<std::array<T,M>,N> >
  39. {
  40. typedef void scalar_type;
  41. };
  42. template <class T>
  43. struct
  44. quat_traits<std::array<T,4> >
  45. {
  46. typedef std::array<T,4> this_quaternion;
  47. typedef T scalar_type;
  48. template <int I>
  49. static
  50. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  51. scalar_type
  52. read_element( this_quaternion const & x )
  53. {
  54. BOOST_QVM_STATIC_ASSERT(I>=0);
  55. BOOST_QVM_STATIC_ASSERT(I<4);
  56. return x[I];
  57. }
  58. template <int I>
  59. static
  60. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  61. scalar_type &
  62. write_element( this_quaternion & x )
  63. {
  64. BOOST_QVM_STATIC_ASSERT(I>=0);
  65. BOOST_QVM_STATIC_ASSERT(I<4);
  66. return x[I];
  67. }
  68. static
  69. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  70. scalar_type
  71. read_element_idx( int i, this_quaternion const & x )
  72. {
  73. BOOST_QVM_ASSERT(i>=0);
  74. BOOST_QVM_ASSERT(i<4);
  75. return x[i];
  76. }
  77. static
  78. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  79. scalar_type &
  80. write_element_idx( int i, this_quaternion & x )
  81. {
  82. BOOST_QVM_ASSERT(i>=0);
  83. BOOST_QVM_ASSERT(i<4);
  84. return x[i];
  85. }
  86. };
  87. template <class T>
  88. struct
  89. quat_traits<std::array<T,4> const>
  90. {
  91. typedef std::array<T,4> const this_quaternion;
  92. typedef T scalar_type;
  93. template <int I>
  94. static
  95. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  96. scalar_type
  97. read_element( this_quaternion & x )
  98. {
  99. BOOST_QVM_STATIC_ASSERT(I>=0);
  100. BOOST_QVM_STATIC_ASSERT(I<4);
  101. return x[I];
  102. }
  103. static
  104. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  105. scalar_type
  106. read_element_idx( int i, this_quaternion & x )
  107. {
  108. BOOST_QVM_ASSERT(i>=0);
  109. BOOST_QVM_ASSERT(i<4);
  110. return x[i];
  111. }
  112. };
  113. template <class T>
  114. struct
  115. deduce_quat<std::array<T,4> >
  116. {
  117. typedef quat<T> type;
  118. };
  119. template <class T>
  120. struct
  121. deduce_quat<std::array<T,4> const>
  122. {
  123. typedef quat<T> type;
  124. };
  125. template <class T1,class T2>
  126. struct
  127. deduce_quat2<std::array<T1,4>,std::array<T2,4> >
  128. {
  129. typedef quat<typename deduce_scalar<T1,T2>::type> type;
  130. };
  131. template <class T1,class T2>
  132. struct
  133. deduce_quat2<std::array<T1,4> const,std::array<T2,4> >
  134. {
  135. typedef quat<typename deduce_scalar<T1,T2>::type> type;
  136. };
  137. template <class T1,class T2>
  138. struct
  139. deduce_quat2<std::array<T1,4>,std::array<T2,4> const>
  140. {
  141. typedef quat<typename deduce_scalar<T1,T2>::type> type;
  142. };
  143. template <class T1,class T2>
  144. struct
  145. deduce_quat2<std::array<T1,4> const,std::array<T2,4> const>
  146. {
  147. typedef quat<typename deduce_scalar<T1,T2>::type> type;
  148. };
  149. } }
  150. #endif
  151. namespace boost { namespace qvm {
  152. template <class T,int D>
  153. struct
  154. quat_traits<T[D]>
  155. {
  156. typedef void scalar_type;
  157. };
  158. template <class T,int D>
  159. struct
  160. quat_traits<T[D][4]>
  161. {
  162. typedef void scalar_type;
  163. };
  164. template <class T,int D>
  165. struct
  166. quat_traits<T[4][D]>
  167. {
  168. typedef void scalar_type;
  169. };
  170. template <class T>
  171. struct
  172. quat_traits<T[4][4]>
  173. {
  174. typedef void scalar_type;
  175. };
  176. template <class T,int M,int N>
  177. struct
  178. quat_traits<T[M][N]>
  179. {
  180. typedef void scalar_type;
  181. };
  182. template <class T>
  183. struct
  184. quat_traits<T[4]>
  185. {
  186. typedef T this_quaternion[4];
  187. typedef T scalar_type;
  188. template <int I>
  189. static
  190. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  191. scalar_type
  192. read_element( this_quaternion const & x )
  193. {
  194. BOOST_QVM_STATIC_ASSERT(I>=0);
  195. BOOST_QVM_STATIC_ASSERT(I<4);
  196. return x[I];
  197. }
  198. template <int I>
  199. static
  200. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  201. scalar_type &
  202. write_element( this_quaternion & x )
  203. {
  204. BOOST_QVM_STATIC_ASSERT(I>=0);
  205. BOOST_QVM_STATIC_ASSERT(I<4);
  206. return x[I];
  207. }
  208. static
  209. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  210. scalar_type
  211. read_element_idx( int i, this_quaternion const & x )
  212. {
  213. BOOST_QVM_ASSERT(i>=0);
  214. BOOST_QVM_ASSERT(i<4);
  215. return x[i];
  216. }
  217. static
  218. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  219. scalar_type &
  220. write_element_idx( int i, this_quaternion & x )
  221. {
  222. BOOST_QVM_ASSERT(i>=0);
  223. BOOST_QVM_ASSERT(i<4);
  224. return x[i];
  225. }
  226. };
  227. template <class T>
  228. struct
  229. deduce_quat<T[4]>
  230. {
  231. typedef quat<T> type;
  232. };
  233. template <class T>
  234. struct
  235. deduce_quat<T const[4]>
  236. {
  237. typedef quat<T> type;
  238. };
  239. template <class T1,class T2>
  240. struct
  241. deduce_quat2<T1[4],T2[4]>
  242. {
  243. typedef quat<typename deduce_scalar<T1,T2>::type> type;
  244. };
  245. template <class T>
  246. T (&ptr_qref( T * ptr ))[4]
  247. {
  248. return *reinterpret_cast<T (*)[4]>(ptr);
  249. }
  250. } }
  251. #endif