rot_mat_test.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. #include <boost/qvm/mat_operations.hpp>
  5. #include <boost/qvm/mat.hpp>
  6. #include "test_qvm_matrix.hpp"
  7. #include "test_qvm_vector.hpp"
  8. #include "gold.hpp"
  9. namespace
  10. {
  11. template <int D>
  12. void
  13. test_x()
  14. {
  15. using namespace boost::qvm;
  16. test_qvm::vector<V1,3> axis; axis.a[0]=1;
  17. for( float r=0; r<6.28f; r+=0.5f )
  18. {
  19. test_qvm::matrix<M1,D,D> const m1=rot_mat<D>(axis,r);
  20. test_qvm::rotation_x(m1.b,r);
  21. BOOST_QVM_TEST_EQ(m1.a,m1.b);
  22. test_qvm::matrix<M1,D,D> m2(42,1);
  23. set_rot(m2,axis,r);
  24. test_qvm::rotation_x(m2.b,r);
  25. BOOST_QVM_TEST_EQ(m2.a,m2.b);
  26. test_qvm::matrix<M1,D,D> m3(42,1);
  27. test_qvm::matrix<M1,D,D> m4(42,1);
  28. rotate(m3,axis,r);
  29. m3 = m3*m1;
  30. BOOST_QVM_TEST_EQ(m3.a,m3.a);
  31. }
  32. }
  33. template <int D>
  34. void
  35. test_y()
  36. {
  37. using namespace boost::qvm;
  38. test_qvm::vector<V1,3> axis; axis.a[1]=1;
  39. for( float r=0; r<6.28f; r+=0.5f )
  40. {
  41. test_qvm::matrix<M1,D,D> m1=rot_mat<D>(axis,r);
  42. test_qvm::rotation_y(m1.b,r);
  43. BOOST_QVM_TEST_EQ(m1.a,m1.b);
  44. test_qvm::matrix<M1,D,D> m2(42,1);
  45. set_rot(m2,axis,r);
  46. test_qvm::rotation_y(m2.b,r);
  47. BOOST_QVM_TEST_EQ(m2.a,m2.b);
  48. test_qvm::matrix<M1,D,D> m3(42,1);
  49. test_qvm::matrix<M1,D,D> m4(42,1);
  50. rotate(m3,axis,r);
  51. m3 = m3*m1;
  52. BOOST_QVM_TEST_EQ(m3.a,m3.a);
  53. }
  54. }
  55. template <int D>
  56. void
  57. test_z()
  58. {
  59. using namespace boost::qvm;
  60. test_qvm::vector<V1,3> axis; axis.a[2]=1;
  61. for( float r=0; r<6.28f; r+=0.5f )
  62. {
  63. test_qvm::matrix<M1,D,D> m1=rot_mat<D>(axis,r);
  64. test_qvm::rotation_z(m1.b,r);
  65. BOOST_QVM_TEST_EQ(m1.a,m1.b);
  66. test_qvm::matrix<M1,D,D> m2(42,1);
  67. set_rot(m2,axis,r);
  68. test_qvm::rotation_z(m2.b,r);
  69. BOOST_QVM_TEST_EQ(m2.a,m2.b);
  70. test_qvm::matrix<M1,D,D> m3(42,1);
  71. test_qvm::matrix<M1,D,D> m4(42,1);
  72. rotate(m3,axis,r);
  73. m3 = m3*m1;
  74. BOOST_QVM_TEST_EQ(m3.a,m3.a);
  75. }
  76. }
  77. template <int D>
  78. void
  79. test_xzy()
  80. {
  81. using namespace boost::qvm;
  82. for( float x1=0; x1<6.28f; x1+=0.5f )
  83. for( float z2=0; z2<6.28f; z2+=0.5f )
  84. for( float y3=0; y3<6.28f; y3+=0.5f )
  85. {
  86. mat<float,D,D> const m2 = rotx_mat<D>(x1) * rotz_mat<D>(z2) * roty_mat<D>(y3);
  87. {
  88. mat<float,D,D> m1 = rot_mat_xzy<D>(x1,z2,y3);
  89. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  90. }
  91. {
  92. mat<float,D,D> m1; set_rot_xzy(m1,x1,z2,y3);
  93. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  94. }
  95. {
  96. mat<float,D,D> m1 = identity_mat<float,D>(); rotate_xzy(m1,x1,z2,y3);
  97. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  98. }
  99. }
  100. }
  101. template <int D>
  102. void
  103. test_xyz()
  104. {
  105. using namespace boost::qvm;
  106. for( float x1=0; x1<6.28f; x1+=0.5f )
  107. for( float y2=0; y2<6.28f; y2+=0.5f )
  108. for( float z3=0; z3<6.28f; z3+=0.5f )
  109. {
  110. mat<float,D,D> const m2 = rotx_mat<D>(x1) * roty_mat<D>(y2) * rotz_mat<D>(z3);
  111. {
  112. mat<float,D,D> m1 = rot_mat_xyz<D>(x1,y2,z3);
  113. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  114. }
  115. {
  116. mat<float,D,D> m1; set_rot_xyz(m1,x1,y2,z3);
  117. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  118. }
  119. {
  120. mat<float,D,D> m1 = identity_mat<float,D>(); rotate_xyz(m1,x1,y2,z3);
  121. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  122. }
  123. }
  124. }
  125. template <int D>
  126. void
  127. test_yxz()
  128. {
  129. using namespace boost::qvm;
  130. for( float y1=0; y1<6.28f; y1+=0.5f )
  131. for( float x2=0; x2<6.28f; x2+=0.5f )
  132. for( float z3=0; z3<6.28f; z3+=0.5f )
  133. {
  134. mat<float,D,D> const m2 = roty_mat<D>(y1) * rotx_mat<D>(x2) * rotz_mat<D>(z3);
  135. {
  136. mat<float,D,D> m1 = rot_mat_yxz<D>(y1,x2,z3);
  137. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  138. }
  139. {
  140. mat<float,D,D> m1; set_rot_yxz(m1,y1,x2,z3);
  141. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  142. }
  143. {
  144. mat<float,D,D> m1 = identity_mat<float,D>(); rotate_yxz(m1,y1,x2,z3);
  145. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  146. }
  147. }
  148. }
  149. template <int D>
  150. void
  151. test_yzx()
  152. {
  153. using namespace boost::qvm;
  154. for( float y1=0; y1<6.28f; y1+=0.5f )
  155. for( float z2=0; z2<6.28f; z2+=0.5f )
  156. for( float x3=0; x3<6.28f; x3+=0.5f )
  157. {
  158. mat<float,D,D> const m2 = roty_mat<D>(y1) * rotz_mat<D>(z2) * rotx_mat<D>(x3);
  159. {
  160. mat<float,D,D> m1 = rot_mat_yzx<D>(y1,z2,x3);
  161. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  162. }
  163. {
  164. mat<float,D,D> m1; set_rot_yzx(m1,y1,z2,x3);
  165. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  166. }
  167. {
  168. mat<float,D,D> m1 = identity_mat<float,D>(); rotate_yzx(m1,y1,z2,x3);
  169. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  170. }
  171. }
  172. }
  173. template <int D>
  174. void
  175. test_zyx()
  176. {
  177. using namespace boost::qvm;
  178. for( float z1=0; z1<6.28f; z1+=0.5f )
  179. for( float y2=0; y2<6.28f; y2+=0.5f )
  180. for( float x3=0; x3<6.28f; x3+=0.5f )
  181. {
  182. mat<float,D,D> const m2 = rotz_mat<D>(z1) * roty_mat<D>(y2) * rotx_mat<D>(x3);
  183. {
  184. mat<float,D,D> m1 = rot_mat_zyx<D>(z1,y2,x3);
  185. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  186. }
  187. {
  188. mat<float,D,D> m1; set_rot_zyx(m1,z1,y2,x3);
  189. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  190. }
  191. {
  192. mat<float,D,D> m1 = identity_mat<float,D>(); rotate_zyx(m1,z1,y2,x3);
  193. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  194. }
  195. }
  196. }
  197. template <int D>
  198. void
  199. test_zxy()
  200. {
  201. using namespace boost::qvm;
  202. for( float z1=0; z1<6.28f; z1+=0.5f )
  203. for( float x2=0; x2<6.28f; x2+=0.5f )
  204. for( float y3=0; y3<6.28f; y3+=0.5f )
  205. {
  206. mat<float,D,D> const m2 = rotz_mat<D>(z1) * rotx_mat<D>(x2) * roty_mat<D>(y3);
  207. {
  208. mat<float,D,D> m1 = rot_mat_zxy<D>(z1,x2,y3);
  209. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  210. }
  211. {
  212. mat<float,D,D> m1; set_rot_zxy(m1,z1,x2,y3);
  213. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  214. }
  215. {
  216. mat<float,D,D> m1 = identity_mat<float,D>(); rotate_zxy(m1,z1,x2,y3);
  217. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  218. }
  219. }
  220. }
  221. template <int D>
  222. void
  223. test_xzx()
  224. {
  225. using namespace boost::qvm;
  226. for( float x1=0; x1<6.28f; x1+=0.5f )
  227. for( float z2=0; z2<6.28f; z2+=0.5f )
  228. for( float x3=0; x3<6.28f; x3+=0.5f )
  229. {
  230. mat<float,D,D> const m2 = rotx_mat<D>(x1) * rotz_mat<D>(z2) * rotx_mat<D>(x3);
  231. {
  232. mat<float,D,D> m1 = rot_mat_xzx<D>(x1,z2,x3);
  233. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  234. }
  235. {
  236. mat<float,D,D> m1; set_rot_xzx(m1,x1,z2,x3);
  237. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  238. }
  239. {
  240. mat<float,D,D> m1 = identity_mat<float,D>(); rotate_xzx(m1,x1,z2,x3);
  241. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  242. }
  243. }
  244. }
  245. template <int D>
  246. void
  247. test_xyx()
  248. {
  249. using namespace boost::qvm;
  250. for( float x1=0; x1<6.28f; x1+=0.5f )
  251. for( float y2=0; y2<6.28f; y2+=0.5f )
  252. for( float x3=0; x3<6.28f; x3+=0.5f )
  253. {
  254. mat<float,D,D> const m2 = rotx_mat<D>(x1) * roty_mat<D>(y2) * rotx_mat<D>(x3);
  255. {
  256. mat<float,D,D> m1 = rot_mat_xyx<D>(x1,y2,x3);
  257. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  258. }
  259. {
  260. mat<float,D,D> m1; set_rot_xyx(m1,x1,y2,x3);
  261. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  262. }
  263. {
  264. mat<float,D,D> m1 = identity_mat<float,D>(); rotate_xyx(m1,x1,y2,x3);
  265. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  266. }
  267. }
  268. }
  269. template <int D>
  270. void
  271. test_yxy()
  272. {
  273. using namespace boost::qvm;
  274. for( float y1=0; y1<6.28f; y1+=0.5f )
  275. for( float x2=0; x2<6.28f; x2+=0.5f )
  276. for( float y3=0; y3<6.28f; y3+=0.5f )
  277. {
  278. mat<float,D,D> const m2 = roty_mat<D>(y1) * rotx_mat<D>(x2) * roty_mat<D>(y3);
  279. {
  280. mat<float,D,D> m1 = rot_mat_yxy<D>(y1,x2,y3);
  281. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  282. }
  283. {
  284. mat<float,D,D> m1; set_rot_yxy(m1,y1,x2,y3);
  285. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  286. }
  287. {
  288. mat<float,D,D> m1 = identity_mat<float,D>(); rotate_yxy(m1,y1,x2,y3);
  289. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  290. }
  291. }
  292. }
  293. template <int D>
  294. void
  295. test_yzy()
  296. {
  297. using namespace boost::qvm;
  298. for( float y1=0; y1<6.28f; y1+=0.5f )
  299. for( float z2=0; z2<6.28f; z2+=0.5f )
  300. for( float y3=0; y3<6.28f; y3+=0.5f )
  301. {
  302. mat<float,D,D> const m2 = roty_mat<D>(y1) * rotz_mat<D>(z2) * roty_mat<D>(y3);
  303. {
  304. mat<float,D,D> m1 = rot_mat_yzy<D>(y1,z2,y3);
  305. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  306. }
  307. {
  308. mat<float,D,D> m1; set_rot_yzy(m1,y1,z2,y3);
  309. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  310. }
  311. {
  312. mat<float,D,D> m1 = identity_mat<float,D>(); rotate_yzy(m1,y1,z2,y3);
  313. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  314. }
  315. }
  316. }
  317. template <int D>
  318. void
  319. test_zyz()
  320. {
  321. using namespace boost::qvm;
  322. for( float z1=0; z1<6.28f; z1+=0.5f )
  323. for( float y2=0; y2<6.28f; y2+=0.5f )
  324. for( float z3=0; z3<6.28f; z3+=0.5f )
  325. {
  326. mat<float,D,D> const m2 = rotz_mat<D>(z1) * roty_mat<D>(y2) * rotz_mat<D>(z3);
  327. {
  328. mat<float,D,D> m1 = rot_mat_zyz<D>(z1,y2,z3);
  329. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  330. }
  331. {
  332. mat<float,D,D> m1; set_rot_zyz(m1,z1,y2,z3);
  333. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  334. }
  335. {
  336. mat<float,D,D> m1 = identity_mat<float,D>(); rotate_zyz(m1,z1,y2,z3);
  337. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  338. }
  339. }
  340. }
  341. template <int D>
  342. void
  343. test_zxz()
  344. {
  345. using namespace boost::qvm;
  346. for( float z1=0; z1<6.28f; z1+=0.5f )
  347. for( float x2=0; x2<6.28f; x2+=0.5f )
  348. for( float z3=0; z3<6.28f; z3+=0.5f )
  349. {
  350. mat<float,D,D> const m2 = rotz_mat<D>(z1) * rotx_mat<D>(x2) * rotz_mat<D>(z3);
  351. {
  352. mat<float,D,D> m1 = rot_mat_zxz<D>(z1,x2,z3);
  353. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  354. }
  355. {
  356. mat<float,D,D> m1; set_rot_zxz(m1,z1,x2,z3);
  357. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  358. }
  359. {
  360. mat<float,D,D> m1 = identity_mat<float,D>(); rotate_zxz(m1,z1,x2,z3);
  361. BOOST_QVM_TEST_CLOSE(m1.a,m2.a,0.0002f);
  362. }
  363. }
  364. }
  365. }
  366. int
  367. main()
  368. {
  369. test_x<3>();
  370. test_y<3>();
  371. test_z<3>();
  372. test_xzy<3>();
  373. test_xyz<3>();
  374. test_yxz<3>();
  375. test_yzx<3>();
  376. test_zyx<3>();
  377. test_zxy<3>();
  378. test_xzx<3>();
  379. test_xyx<3>();
  380. test_yxy<3>();
  381. test_yzy<3>();
  382. test_zyz<3>();
  383. test_zxz<3>();
  384. test_x<4>();
  385. test_y<4>();
  386. test_z<4>();
  387. test_xzy<4>();
  388. test_xyz<4>();
  389. test_yxz<4>();
  390. test_yzx<4>();
  391. test_zyx<4>();
  392. test_zxy<4>();
  393. test_xzx<4>();
  394. test_xyx<4>();
  395. test_yxy<4>();
  396. test_yzy<4>();
  397. test_zyz<4>();
  398. test_zxz<4>();
  399. test_x<5>();
  400. test_y<5>();
  401. test_z<5>();
  402. test_xzy<5>();
  403. test_xyz<5>();
  404. test_yxz<5>();
  405. test_yzx<5>();
  406. test_zyx<5>();
  407. test_zxy<5>();
  408. test_xzx<5>();
  409. test_xyx<5>();
  410. test_yxy<5>();
  411. test_yzy<5>();
  412. test_zyz<5>();
  413. test_zxz<5>();
  414. return boost::report_errors();
  415. }