map_vec_mat.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. #ifndef BOOST_QVM_MAP_VEC_MAT_HPP_INCLUDED
  2. #define BOOST_QVM_MAP_VEC_MAT_HPP_INCLUDED
  3. /// Copyright (c) 2008-2021 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/inline.hpp>
  7. #include <boost/qvm/deduce_mat.hpp>
  8. #include <boost/qvm/vec_traits.hpp>
  9. #include <boost/qvm/assert.hpp>
  10. #include <boost/qvm/enable_if.hpp>
  11. namespace boost { namespace qvm {
  12. namespace
  13. qvm_detail
  14. {
  15. template <class OriginalVector>
  16. class
  17. col_mat_
  18. {
  19. col_mat_( col_mat_ const & );
  20. col_mat_ & operator=( col_mat_ const & );
  21. ~col_mat_();
  22. public:
  23. template <class T>
  24. BOOST_QVM_INLINE_TRIVIAL
  25. col_mat_ &
  26. operator=( T const & x )
  27. {
  28. assign(*this,x);
  29. return *this;
  30. }
  31. template <class R>
  32. BOOST_QVM_INLINE_TRIVIAL
  33. operator R() const
  34. {
  35. R r;
  36. assign(r,*this);
  37. return r;
  38. }
  39. };
  40. }
  41. template <class OriginalVector>
  42. struct
  43. mat_traits< qvm_detail::col_mat_<OriginalVector> >
  44. {
  45. typedef qvm_detail::col_mat_<OriginalVector> this_matrix;
  46. typedef typename vec_traits<OriginalVector>::scalar_type scalar_type;
  47. static int const rows=vec_traits<OriginalVector>::dim;
  48. static int const cols=1;
  49. template <int Row,int Col>
  50. static
  51. BOOST_QVM_INLINE_CRITICAL
  52. scalar_type
  53. read_element( this_matrix const & x )
  54. {
  55. BOOST_QVM_STATIC_ASSERT(Col==0);
  56. BOOST_QVM_STATIC_ASSERT(Row>=0);
  57. BOOST_QVM_STATIC_ASSERT(Row<rows);
  58. return vec_traits<OriginalVector>::template read_element<Row>(reinterpret_cast<OriginalVector const &>(x));
  59. }
  60. template <int Row,int Col>
  61. static
  62. BOOST_QVM_INLINE_CRITICAL
  63. scalar_type &
  64. write_element( this_matrix & x )
  65. {
  66. BOOST_QVM_STATIC_ASSERT(Col==0);
  67. BOOST_QVM_STATIC_ASSERT(Row>=0);
  68. BOOST_QVM_STATIC_ASSERT(Row<rows);
  69. return vec_traits<OriginalVector>::template write_element<Row>(reinterpret_cast<OriginalVector &>(x));
  70. }
  71. static
  72. BOOST_QVM_INLINE_CRITICAL
  73. scalar_type
  74. read_element_idx( int row, int col, this_matrix const & x )
  75. {
  76. BOOST_QVM_ASSERT(col==0);
  77. BOOST_QVM_ASSERT(row>=0);
  78. BOOST_QVM_ASSERT(row<rows);
  79. return vec_traits<OriginalVector>::read_element_idx(row,reinterpret_cast<OriginalVector const &>(x));
  80. }
  81. static
  82. BOOST_QVM_INLINE_CRITICAL
  83. scalar_type &
  84. write_element_idx( int row, int col, this_matrix & x )
  85. {
  86. BOOST_QVM_ASSERT(col==0);
  87. BOOST_QVM_ASSERT(row>=0);
  88. BOOST_QVM_ASSERT(row<rows);
  89. return vec_traits<OriginalVector>::write_element_idx(row,reinterpret_cast<OriginalVector &>(x));
  90. }
  91. };
  92. template <class OriginalVector,int R,int C>
  93. struct
  94. deduce_mat<qvm_detail::col_mat_<OriginalVector>,R,C>
  95. {
  96. typedef mat<typename vec_traits<OriginalVector>::scalar_type,R,C> type;
  97. };
  98. template <class OriginalVector,int R,int C>
  99. struct
  100. deduce_mat2<qvm_detail::col_mat_<OriginalVector>,qvm_detail::col_mat_<OriginalVector>,R,C>
  101. {
  102. typedef mat<typename vec_traits<OriginalVector>::scalar_type,R,C> type;
  103. };
  104. template <class A>
  105. typename enable_if_c<
  106. is_vec<A>::value,
  107. qvm_detail::col_mat_<A> const &>::type
  108. BOOST_QVM_INLINE_TRIVIAL
  109. col_mat( A const & a )
  110. {
  111. return reinterpret_cast<typename qvm_detail::col_mat_<A> const &>(a);
  112. }
  113. template <class A>
  114. typename enable_if_c<
  115. is_vec<A>::value,
  116. qvm_detail::col_mat_<A> &>::type
  117. BOOST_QVM_INLINE_TRIVIAL
  118. col_mat( A & a )
  119. {
  120. return reinterpret_cast<typename qvm_detail::col_mat_<A> &>(a);
  121. }
  122. ////////////////////////////////////////////////
  123. namespace
  124. qvm_detail
  125. {
  126. template <class OriginalVector>
  127. class
  128. row_mat_
  129. {
  130. row_mat_( row_mat_ const & );
  131. row_mat_ & operator=( row_mat_ const & );
  132. ~row_mat_();
  133. public:
  134. template <class T>
  135. BOOST_QVM_INLINE_TRIVIAL
  136. row_mat_ &
  137. operator=( T const & x )
  138. {
  139. assign(*this,x);
  140. return *this;
  141. }
  142. template <class R>
  143. BOOST_QVM_INLINE_TRIVIAL
  144. operator R() const
  145. {
  146. R r;
  147. assign(r,*this);
  148. return r;
  149. }
  150. };
  151. }
  152. template <class OriginalVector>
  153. struct
  154. mat_traits< qvm_detail::row_mat_<OriginalVector> >
  155. {
  156. typedef qvm_detail::row_mat_<OriginalVector> this_matrix;
  157. typedef typename vec_traits<OriginalVector>::scalar_type scalar_type;
  158. static int const rows=1;
  159. static int const cols=vec_traits<OriginalVector>::dim;
  160. template <int Row,int Col>
  161. static
  162. BOOST_QVM_INLINE_CRITICAL
  163. scalar_type
  164. read_element( this_matrix const & x )
  165. {
  166. BOOST_QVM_STATIC_ASSERT(Row==0);
  167. BOOST_QVM_STATIC_ASSERT(Col>=0);
  168. BOOST_QVM_STATIC_ASSERT(Col<cols);
  169. return vec_traits<OriginalVector>::template read_element<Col>(reinterpret_cast<OriginalVector const &>(x));
  170. }
  171. template <int Row,int Col>
  172. static
  173. BOOST_QVM_INLINE_CRITICAL
  174. scalar_type &
  175. write_element( this_matrix & x )
  176. {
  177. BOOST_QVM_STATIC_ASSERT(Row==0);
  178. BOOST_QVM_STATIC_ASSERT(Col>=0);
  179. BOOST_QVM_STATIC_ASSERT(Col<cols);
  180. return vec_traits<OriginalVector>::template write_element<Col>(reinterpret_cast<OriginalVector &>(x));
  181. }
  182. static
  183. BOOST_QVM_INLINE_CRITICAL
  184. scalar_type
  185. read_element_idx( int row, int col, this_matrix const & x )
  186. {
  187. BOOST_QVM_ASSERT(row==0);
  188. BOOST_QVM_ASSERT(col>=0);
  189. BOOST_QVM_ASSERT(col<cols);
  190. return vec_traits<OriginalVector>::read_element_idx(col,reinterpret_cast<OriginalVector const &>(x));
  191. }
  192. static
  193. BOOST_QVM_INLINE_CRITICAL
  194. scalar_type &
  195. write_element_idx( int row, int col, this_matrix & x )
  196. {
  197. BOOST_QVM_ASSERT(row==0);
  198. BOOST_QVM_ASSERT(col>=0);
  199. BOOST_QVM_ASSERT(col<cols);
  200. return vec_traits<OriginalVector>::write_element_idx(col,reinterpret_cast<OriginalVector &>(x));
  201. }
  202. };
  203. template <class OriginalVector,int R,int C>
  204. struct
  205. deduce_mat<qvm_detail::row_mat_<OriginalVector>,R,C>
  206. {
  207. typedef mat<typename vec_traits<OriginalVector>::scalar_type,R,C> type;
  208. };
  209. template <class OriginalVector,int R,int C>
  210. struct
  211. deduce_mat2<qvm_detail::row_mat_<OriginalVector>,qvm_detail::row_mat_<OriginalVector>,R,C>
  212. {
  213. typedef mat<typename vec_traits<OriginalVector>::scalar_type,R,C> type;
  214. };
  215. template <class A>
  216. typename enable_if_c<
  217. is_vec<A>::value,
  218. qvm_detail::row_mat_<A> const &>::type
  219. BOOST_QVM_INLINE_TRIVIAL
  220. row_mat( A const & a )
  221. {
  222. return reinterpret_cast<typename qvm_detail::row_mat_<A> const &>(a);
  223. }
  224. template <class A>
  225. typename enable_if_c<
  226. is_vec<A>::value,
  227. qvm_detail::row_mat_<A> &>::type
  228. BOOST_QVM_INLINE_TRIVIAL
  229. row_mat( A & a )
  230. {
  231. return reinterpret_cast<typename qvm_detail::row_mat_<A> &>(a);
  232. }
  233. ////////////////////////////////////////////////
  234. namespace
  235. qvm_detail
  236. {
  237. template <class OriginalVector>
  238. class
  239. translation_mat_
  240. {
  241. translation_mat_( translation_mat_ const & );
  242. translation_mat_ & operator=( translation_mat_ const & );
  243. ~translation_mat_();
  244. public:
  245. template <class T>
  246. BOOST_QVM_INLINE_TRIVIAL
  247. translation_mat_ &
  248. operator=( T const & x )
  249. {
  250. assign(*this,x);
  251. return *this;
  252. }
  253. template <class R>
  254. BOOST_QVM_INLINE_TRIVIAL
  255. operator R() const
  256. {
  257. R r;
  258. assign(r,*this);
  259. return r;
  260. }
  261. };
  262. template <class M,int Row,int Col,bool TransCol=(Col==mat_traits<M>::cols-1)>
  263. struct read_translation_matat;
  264. template <class OriginalVector,int Row,int Col,bool TransCol>
  265. struct
  266. read_translation_matat<translation_mat_<OriginalVector>,Row,Col,TransCol>
  267. {
  268. static
  269. BOOST_QVM_INLINE_CRITICAL
  270. typename mat_traits< translation_mat_<OriginalVector> >::scalar_type
  271. f( translation_mat_<OriginalVector> const & )
  272. {
  273. return scalar_traits<typename mat_traits< translation_mat_<OriginalVector> >::scalar_type>::value(0);
  274. }
  275. };
  276. template <class OriginalVector,int D>
  277. struct
  278. read_translation_matat<translation_mat_<OriginalVector>,D,D,false>
  279. {
  280. static
  281. BOOST_QVM_INLINE_CRITICAL
  282. typename mat_traits< translation_mat_<OriginalVector> >::scalar_type
  283. f( translation_mat_<OriginalVector> const & )
  284. {
  285. return scalar_traits<typename mat_traits< translation_mat_<OriginalVector> >::scalar_type>::value(1);
  286. }
  287. };
  288. template <class OriginalVector,int D>
  289. struct
  290. read_translation_matat<translation_mat_<OriginalVector>,D,D,true>
  291. {
  292. static
  293. BOOST_QVM_INLINE_CRITICAL
  294. typename mat_traits< translation_mat_<OriginalVector> >::scalar_type
  295. f( translation_mat_<OriginalVector> const & )
  296. {
  297. return scalar_traits<typename mat_traits< translation_mat_<OriginalVector> >::scalar_type>::value(1);
  298. }
  299. };
  300. template <class OriginalVector,int Row,int Col>
  301. struct
  302. read_translation_matat<translation_mat_<OriginalVector>,Row,Col,true>
  303. {
  304. static
  305. BOOST_QVM_INLINE_CRITICAL
  306. typename mat_traits< translation_mat_<OriginalVector> >::scalar_type
  307. f( translation_mat_<OriginalVector> const & x )
  308. {
  309. return vec_traits<OriginalVector>::template read_element<Row>(reinterpret_cast<OriginalVector const &>(x));
  310. }
  311. };
  312. }
  313. template <class OriginalVector>
  314. struct
  315. mat_traits< qvm_detail::translation_mat_<OriginalVector> >
  316. {
  317. typedef qvm_detail::translation_mat_<OriginalVector> this_matrix;
  318. typedef typename vec_traits<OriginalVector>::scalar_type scalar_type;
  319. static int const rows=vec_traits<OriginalVector>::dim+1;
  320. static int const cols=vec_traits<OriginalVector>::dim+1;
  321. template <int Row,int Col>
  322. static
  323. BOOST_QVM_INLINE_CRITICAL
  324. scalar_type
  325. read_element( this_matrix const & x )
  326. {
  327. BOOST_QVM_STATIC_ASSERT(Row>=0);
  328. BOOST_QVM_STATIC_ASSERT(Row<rows);
  329. BOOST_QVM_STATIC_ASSERT(Col>=0);
  330. BOOST_QVM_STATIC_ASSERT(Col<cols);
  331. return qvm_detail::read_translation_matat<qvm_detail::translation_mat_<OriginalVector>,Row,Col>::f(x);
  332. }
  333. template <int Row,int Col>
  334. static
  335. BOOST_QVM_INLINE_CRITICAL
  336. scalar_type &
  337. write_element( this_matrix & x )
  338. {
  339. BOOST_QVM_STATIC_ASSERT(Row>=0);
  340. BOOST_QVM_STATIC_ASSERT(Row<rows);
  341. BOOST_QVM_STATIC_ASSERT(Col==cols-1);
  342. BOOST_QVM_STATIC_ASSERT(Col!=Row);
  343. return vec_traits<OriginalVector>::template write_element<Row>(reinterpret_cast<OriginalVector &>(x));
  344. }
  345. static
  346. BOOST_QVM_INLINE_CRITICAL
  347. scalar_type
  348. read_element_idx( int row, int col, this_matrix const & x )
  349. {
  350. BOOST_QVM_ASSERT(row>=0);
  351. BOOST_QVM_ASSERT(row<rows);
  352. BOOST_QVM_ASSERT(col>=0);
  353. BOOST_QVM_ASSERT(col<cols);
  354. return
  355. row==col?
  356. scalar_traits<scalar_type>::value(1):
  357. (col==cols-1?
  358. vec_traits<OriginalVector>::read_element_idx(row,reinterpret_cast<OriginalVector const &>(x)):
  359. scalar_traits<scalar_type>::value(0));
  360. }
  361. static
  362. BOOST_QVM_INLINE_CRITICAL
  363. scalar_type &
  364. write_element_idx( int row, int col, this_matrix const & x )
  365. {
  366. BOOST_QVM_ASSERT(row>=0);
  367. BOOST_QVM_ASSERT(row<rows);
  368. BOOST_QVM_ASSERT(col==cols-1);
  369. BOOST_QVM_ASSERT(col!=row);
  370. return vec_traits<OriginalVector>::write_element_idx(row,reinterpret_cast<OriginalVector &>(x));
  371. }
  372. };
  373. template <class OriginalVector,int R,int C>
  374. struct
  375. deduce_mat<qvm_detail::translation_mat_<OriginalVector>,R,C>
  376. {
  377. typedef mat<typename vec_traits<OriginalVector>::scalar_type,R,C> type;
  378. };
  379. template <class OriginalVector,int R,int C>
  380. struct
  381. deduce_mat2<qvm_detail::translation_mat_<OriginalVector>,qvm_detail::translation_mat_<OriginalVector>,R,C>
  382. {
  383. typedef mat<typename vec_traits<OriginalVector>::scalar_type,R,C> type;
  384. };
  385. template <class A>
  386. typename enable_if_c<
  387. is_vec<A>::value,
  388. qvm_detail::translation_mat_<A> const &>::type
  389. BOOST_QVM_INLINE_TRIVIAL
  390. translation_mat( A const & a )
  391. {
  392. return reinterpret_cast<typename qvm_detail::translation_mat_<A> const &>(a);
  393. }
  394. template <class A>
  395. typename enable_if_c<
  396. is_vec<A>::value,
  397. qvm_detail::translation_mat_<A> &>::type
  398. BOOST_QVM_INLINE_TRIVIAL
  399. translation_mat( A & a )
  400. {
  401. return reinterpret_cast<typename qvm_detail::translation_mat_<A> &>(a);
  402. }
  403. ////////////////////////////////////////////////
  404. namespace
  405. qvm_detail
  406. {
  407. template <class OriginalVector>
  408. class
  409. diag_mat_
  410. {
  411. diag_mat_( diag_mat_ const & );
  412. diag_mat_ & operator=( diag_mat_ const & );
  413. ~diag_mat_();
  414. public:
  415. template <class T>
  416. BOOST_QVM_INLINE_TRIVIAL
  417. diag_mat_ &
  418. operator=( T const & x )
  419. {
  420. assign(*this,x);
  421. return *this;
  422. }
  423. template <class R>
  424. BOOST_QVM_INLINE_TRIVIAL
  425. operator R() const
  426. {
  427. R r;
  428. assign(r,*this);
  429. return r;
  430. }
  431. };
  432. }
  433. template <class OriginalVector>
  434. struct
  435. mat_traits< qvm_detail::diag_mat_<OriginalVector> >
  436. {
  437. typedef qvm_detail::diag_mat_<OriginalVector> this_matrix;
  438. typedef typename vec_traits<OriginalVector>::scalar_type scalar_type;
  439. static int const rows=vec_traits<OriginalVector>::dim;
  440. static int const cols=vec_traits<OriginalVector>::dim;
  441. template <int Row,int Col>
  442. static
  443. BOOST_QVM_INLINE_CRITICAL
  444. scalar_type
  445. read_element( this_matrix const & x )
  446. {
  447. BOOST_QVM_STATIC_ASSERT(Row>=0);
  448. BOOST_QVM_STATIC_ASSERT(Row<rows);
  449. BOOST_QVM_STATIC_ASSERT(Col>=0);
  450. BOOST_QVM_STATIC_ASSERT(Col<cols);
  451. return Row==Col?vec_traits<OriginalVector>::template read_element<Row>(reinterpret_cast<OriginalVector const &>(x)):scalar_traits<scalar_type>::value(0);
  452. }
  453. template <int Row,int Col>
  454. static
  455. BOOST_QVM_INLINE_CRITICAL
  456. scalar_type &
  457. write_element( this_matrix & x )
  458. {
  459. BOOST_QVM_STATIC_ASSERT(Row>=0);
  460. BOOST_QVM_STATIC_ASSERT(Row<rows);
  461. BOOST_QVM_STATIC_ASSERT(Row==Col);
  462. return vec_traits<OriginalVector>::template write_element<Row>(reinterpret_cast<OriginalVector &>(x));
  463. }
  464. static
  465. BOOST_QVM_INLINE_CRITICAL
  466. scalar_type
  467. read_element_idx( int row, int col, this_matrix const & x )
  468. {
  469. BOOST_QVM_ASSERT(row>=0);
  470. BOOST_QVM_ASSERT(row<rows);
  471. BOOST_QVM_ASSERT(col>=0);
  472. BOOST_QVM_ASSERT(col<cols);
  473. return row==col?vec_traits<OriginalVector>::read_element_idx(row,reinterpret_cast<OriginalVector const &>(x)):scalar_traits<scalar_type>::value(0);
  474. }
  475. static
  476. BOOST_QVM_INLINE_CRITICAL
  477. scalar_type &
  478. write_element_idx( int row, int col, this_matrix & x )
  479. {
  480. BOOST_QVM_ASSERT(row>=0);
  481. BOOST_QVM_ASSERT(row<rows);
  482. BOOST_QVM_ASSERT(row==col);
  483. return vec_traits<OriginalVector>::write_element_idx(row,reinterpret_cast<OriginalVector &>(x));
  484. }
  485. };
  486. template <class OriginalVector,int R,int C>
  487. struct
  488. deduce_mat<qvm_detail::diag_mat_<OriginalVector>,R,C>
  489. {
  490. typedef mat<typename vec_traits<OriginalVector>::scalar_type,R,C> type;
  491. };
  492. template <class OriginalVector,int R,int C>
  493. struct
  494. deduce_mat2<qvm_detail::diag_mat_<OriginalVector>,qvm_detail::diag_mat_<OriginalVector>,R,C>
  495. {
  496. typedef mat<typename vec_traits<OriginalVector>::scalar_type,R,C> type;
  497. };
  498. template <class A>
  499. typename enable_if_c<
  500. is_vec<A>::value,
  501. qvm_detail::diag_mat_<A> const &>::type
  502. BOOST_QVM_INLINE_TRIVIAL
  503. diag_mat( A const & a )
  504. {
  505. return reinterpret_cast<typename qvm_detail::diag_mat_<A> const &>(a);
  506. }
  507. template <class A>
  508. typename enable_if_c<
  509. is_vec<A>::value,
  510. qvm_detail::diag_mat_<A> &>::type
  511. BOOST_QVM_INLINE_TRIVIAL
  512. diag_mat( A & a )
  513. {
  514. return reinterpret_cast<typename qvm_detail::diag_mat_<A> &>(a);
  515. }
  516. } }
  517. #endif