gtx_euler_angle.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. // Code sample from Filippo Ramaciotti
  2. #define GLM_ENABLE_EXPERIMENTAL
  3. #include <glm/gtc/matrix_transform.hpp>
  4. #include <glm/gtx/matrix_cross_product.hpp>
  5. #include <glm/gtx/matrix_operation.hpp>
  6. #include <glm/gtc/epsilon.hpp>
  7. #include <glm/gtx/string_cast.hpp>
  8. #include <glm/gtx/euler_angles.hpp>
  9. #include <cstdio>
  10. #include <vector>
  11. #include <utility>
  12. namespace test_eulerAngleX
  13. {
  14. static int test()
  15. {
  16. int Error = 0;
  17. float const Angle(glm::pi<float>() * 0.5f);
  18. glm::vec3 const X(1.0f, 0.0f, 0.0f);
  19. glm::vec4 const Y(0.0f, 1.0f, 0.0f, 1.0f);
  20. glm::vec4 const Y1 = glm::rotate(glm::mat4(1.0f), Angle, X) * Y;
  21. glm::vec4 const Y2 = glm::eulerAngleX(Angle) * Y;
  22. glm::vec4 const Y3 = glm::eulerAngleXY(Angle, 0.0f) * Y;
  23. glm::vec4 const Y4 = glm::eulerAngleYX(0.0f, Angle) * Y;
  24. glm::vec4 const Y5 = glm::eulerAngleXZ(Angle, 0.0f) * Y;
  25. glm::vec4 const Y6 = glm::eulerAngleZX(0.0f, Angle) * Y;
  26. glm::vec4 const Y7 = glm::eulerAngleYXZ(0.0f, Angle, 0.0f) * Y;
  27. Error += glm::all(glm::epsilonEqual(Y1, Y2, 0.00001f)) ? 0 : 1;
  28. Error += glm::all(glm::epsilonEqual(Y1, Y3, 0.00001f)) ? 0 : 1;
  29. Error += glm::all(glm::epsilonEqual(Y1, Y4, 0.00001f)) ? 0 : 1;
  30. Error += glm::all(glm::epsilonEqual(Y1, Y5, 0.00001f)) ? 0 : 1;
  31. Error += glm::all(glm::epsilonEqual(Y1, Y6, 0.00001f)) ? 0 : 1;
  32. Error += glm::all(glm::epsilonEqual(Y1, Y7, 0.00001f)) ? 0 : 1;
  33. glm::vec4 const Z(0.0f, 0.0f, 1.0f, 1.0f);
  34. glm::vec4 const Z1 = glm::rotate(glm::mat4(1.0f), Angle, X) * Z;
  35. glm::vec4 const Z2 = glm::eulerAngleX(Angle) * Z;
  36. glm::vec4 const Z3 = glm::eulerAngleXY(Angle, 0.0f) * Z;
  37. glm::vec4 const Z4 = glm::eulerAngleYX(0.0f, Angle) * Z;
  38. glm::vec4 const Z5 = glm::eulerAngleXZ(Angle, 0.0f) * Z;
  39. glm::vec4 const Z6 = glm::eulerAngleZX(0.0f, Angle) * Z;
  40. glm::vec4 const Z7 = glm::eulerAngleYXZ(0.0f, Angle, 0.0f) * Z;
  41. Error += glm::all(glm::epsilonEqual(Z1, Z2, 0.00001f)) ? 0 : 1;
  42. Error += glm::all(glm::epsilonEqual(Z1, Z3, 0.00001f)) ? 0 : 1;
  43. Error += glm::all(glm::epsilonEqual(Z1, Z4, 0.00001f)) ? 0 : 1;
  44. Error += glm::all(glm::epsilonEqual(Z1, Z5, 0.00001f)) ? 0 : 1;
  45. Error += glm::all(glm::epsilonEqual(Z1, Z6, 0.00001f)) ? 0 : 1;
  46. Error += glm::all(glm::epsilonEqual(Z1, Z7, 0.00001f)) ? 0 : 1;
  47. return Error;
  48. }
  49. }//namespace test_eulerAngleX
  50. namespace test_eulerAngleY
  51. {
  52. static int test()
  53. {
  54. int Error = 0;
  55. float const Angle(glm::pi<float>() * 0.5f);
  56. glm::vec3 const Y(0.0f, 1.0f, 0.0f);
  57. glm::vec4 const X(1.0f, 0.0f, 0.0f, 1.0f);
  58. glm::vec4 const X1 = glm::rotate(glm::mat4(1.0f), Angle, Y) * X;
  59. glm::vec4 const X2 = glm::eulerAngleY(Angle) * X;
  60. glm::vec4 const X3 = glm::eulerAngleYX(Angle, 0.0f) * X;
  61. glm::vec4 const X4 = glm::eulerAngleXY(0.0f, Angle) * X;
  62. glm::vec4 const X5 = glm::eulerAngleYZ(Angle, 0.0f) * X;
  63. glm::vec4 const X6 = glm::eulerAngleZY(0.0f, Angle) * X;
  64. glm::vec4 const X7 = glm::eulerAngleYXZ(Angle, 0.0f, 0.0f) * X;
  65. Error += glm::all(glm::epsilonEqual(X1, X2, 0.00001f)) ? 0 : 1;
  66. Error += glm::all(glm::epsilonEqual(X1, X3, 0.00001f)) ? 0 : 1;
  67. Error += glm::all(glm::epsilonEqual(X1, X4, 0.00001f)) ? 0 : 1;
  68. Error += glm::all(glm::epsilonEqual(X1, X5, 0.00001f)) ? 0 : 1;
  69. Error += glm::all(glm::epsilonEqual(X1, X6, 0.00001f)) ? 0 : 1;
  70. Error += glm::all(glm::epsilonEqual(X1, X7, 0.00001f)) ? 0 : 1;
  71. glm::vec4 const Z(0.0f, 0.0f, 1.0f, 1.0f);
  72. glm::vec4 const Z1 = glm::eulerAngleY(Angle) * Z;
  73. glm::vec4 const Z2 = glm::rotate(glm::mat4(1.0f), Angle, Y) * Z;
  74. glm::vec4 const Z3 = glm::eulerAngleYX(Angle, 0.0f) * Z;
  75. glm::vec4 const Z4 = glm::eulerAngleXY(0.0f, Angle) * Z;
  76. glm::vec4 const Z5 = glm::eulerAngleYZ(Angle, 0.0f) * Z;
  77. glm::vec4 const Z6 = glm::eulerAngleZY(0.0f, Angle) * Z;
  78. glm::vec4 const Z7 = glm::eulerAngleYXZ(Angle, 0.0f, 0.0f) * Z;
  79. Error += glm::all(glm::epsilonEqual(Z1, Z2, 0.00001f)) ? 0 : 1;
  80. Error += glm::all(glm::epsilonEqual(Z1, Z3, 0.00001f)) ? 0 : 1;
  81. Error += glm::all(glm::epsilonEqual(Z1, Z4, 0.00001f)) ? 0 : 1;
  82. Error += glm::all(glm::epsilonEqual(Z1, Z5, 0.00001f)) ? 0 : 1;
  83. Error += glm::all(glm::epsilonEqual(Z1, Z6, 0.00001f)) ? 0 : 1;
  84. Error += glm::all(glm::epsilonEqual(Z1, Z7, 0.00001f)) ? 0 : 1;
  85. return Error;
  86. }
  87. }//namespace test_eulerAngleY
  88. namespace test_eulerAngleZ
  89. {
  90. static int test()
  91. {
  92. int Error = 0;
  93. float const Angle(glm::pi<float>() * 0.5f);
  94. glm::vec3 const Z(0.0f, 0.0f, 1.0f);
  95. glm::vec4 const X(1.0f, 0.0f, 0.0f, 1.0f);
  96. glm::vec4 const X1 = glm::rotate(glm::mat4(1.0f), Angle, Z) * X;
  97. glm::vec4 const X2 = glm::eulerAngleZ(Angle) * X;
  98. glm::vec4 const X3 = glm::eulerAngleZX(Angle, 0.0f) * X;
  99. glm::vec4 const X4 = glm::eulerAngleXZ(0.0f, Angle) * X;
  100. glm::vec4 const X5 = glm::eulerAngleZY(Angle, 0.0f) * X;
  101. glm::vec4 const X6 = glm::eulerAngleYZ(0.0f, Angle) * X;
  102. glm::vec4 const X7 = glm::eulerAngleYXZ(0.0f, 0.0f, Angle) * X;
  103. Error += glm::all(glm::epsilonEqual(X1, X2, 0.00001f)) ? 0 : 1;
  104. Error += glm::all(glm::epsilonEqual(X1, X3, 0.00001f)) ? 0 : 1;
  105. Error += glm::all(glm::epsilonEqual(X1, X4, 0.00001f)) ? 0 : 1;
  106. Error += glm::all(glm::epsilonEqual(X1, X5, 0.00001f)) ? 0 : 1;
  107. Error += glm::all(glm::epsilonEqual(X1, X6, 0.00001f)) ? 0 : 1;
  108. Error += glm::all(glm::epsilonEqual(X1, X7, 0.00001f)) ? 0 : 1;
  109. glm::vec4 const Y(1.0f, 0.0f, 0.0f, 1.0f);
  110. glm::vec4 const Z1 = glm::rotate(glm::mat4(1.0f), Angle, Z) * Y;
  111. glm::vec4 const Z2 = glm::eulerAngleZ(Angle) * Y;
  112. glm::vec4 const Z3 = glm::eulerAngleZX(Angle, 0.0f) * Y;
  113. glm::vec4 const Z4 = glm::eulerAngleXZ(0.0f, Angle) * Y;
  114. glm::vec4 const Z5 = glm::eulerAngleZY(Angle, 0.0f) * Y;
  115. glm::vec4 const Z6 = glm::eulerAngleYZ(0.0f, Angle) * Y;
  116. glm::vec4 const Z7 = glm::eulerAngleYXZ(0.0f, 0.0f, Angle) * Y;
  117. Error += glm::all(glm::epsilonEqual(Z1, Z2, 0.00001f)) ? 0 : 1;
  118. Error += glm::all(glm::epsilonEqual(Z1, Z3, 0.00001f)) ? 0 : 1;
  119. Error += glm::all(glm::epsilonEqual(Z1, Z4, 0.00001f)) ? 0 : 1;
  120. Error += glm::all(glm::epsilonEqual(Z1, Z5, 0.00001f)) ? 0 : 1;
  121. Error += glm::all(glm::epsilonEqual(Z1, Z6, 0.00001f)) ? 0 : 1;
  122. Error += glm::all(glm::epsilonEqual(Z1, Z7, 0.00001f)) ? 0 : 1;
  123. return Error;
  124. }
  125. }//namespace test_eulerAngleZ
  126. namespace test_derivedEulerAngles
  127. {
  128. static bool epsilonEqual(glm::mat4 const& mat1, glm::mat4 const& mat2, glm::mat4::value_type const& epsilon)
  129. {
  130. return glm::all(glm::epsilonEqual(mat1[0], mat2[0], epsilon)) ?
  131. (
  132. glm::all(glm::epsilonEqual(mat1[1], mat2[1], epsilon)) ?
  133. (
  134. glm::all(glm::epsilonEqual(mat1[2], mat2[2], epsilon)) ?
  135. (
  136. glm::all(glm::epsilonEqual(mat1[3], mat2[3], epsilon)) ? true : false
  137. ) : false
  138. ) : false
  139. ) : false;
  140. }
  141. template<typename RotationFunc, typename TestDerivedFunc>
  142. static int test(RotationFunc rotationFunc, TestDerivedFunc testDerivedFunc, const glm::vec3& basis)
  143. {
  144. int Error = 0;
  145. typedef glm::vec3::value_type value;
  146. value const zeroAngle(0.0f);
  147. value const Angle(glm::pi<float>() * 0.75f);
  148. value const negativeAngle(-Angle);
  149. value const zeroAngleVelocity(0.0f);
  150. value const AngleVelocity(glm::pi<float>() * 0.27f);
  151. value const negativeAngleVelocity(-AngleVelocity);
  152. typedef std::pair<value,value> AngleAndAngleVelocity;
  153. std::vector<AngleAndAngleVelocity> testPairs;
  154. testPairs.push_back(AngleAndAngleVelocity(zeroAngle, zeroAngleVelocity));
  155. testPairs.push_back(AngleAndAngleVelocity(zeroAngle, AngleVelocity));
  156. testPairs.push_back(AngleAndAngleVelocity(zeroAngle, negativeAngleVelocity));
  157. testPairs.push_back(AngleAndAngleVelocity(Angle, zeroAngleVelocity));
  158. testPairs.push_back(AngleAndAngleVelocity(Angle, AngleVelocity));
  159. testPairs.push_back(AngleAndAngleVelocity(Angle, negativeAngleVelocity));
  160. testPairs.push_back(AngleAndAngleVelocity(negativeAngle, zeroAngleVelocity));
  161. testPairs.push_back(AngleAndAngleVelocity(negativeAngle, AngleVelocity));
  162. testPairs.push_back(AngleAndAngleVelocity(negativeAngle, negativeAngleVelocity));
  163. for (size_t i = 0, size = testPairs.size(); i < size; ++i)
  164. {
  165. AngleAndAngleVelocity const& pair = testPairs.at(i);
  166. glm::mat4 const W = glm::matrixCross4(basis * pair.second);
  167. glm::mat4 const rotMt = glm::transpose(rotationFunc(pair.first));
  168. glm::mat4 const derivedRotM = testDerivedFunc(pair.first, pair.second);
  169. Error += epsilonEqual(W, derivedRotM * rotMt, 0.00001f) ? 0 : 1;
  170. }
  171. return Error;
  172. }
  173. }//namespace test_derivedEulerAngles
  174. namespace test_eulerAngleXY
  175. {
  176. static int test()
  177. {
  178. int Error = 0;
  179. glm::vec4 const V(1.0f);
  180. float const AngleX(glm::pi<float>() * 0.5f);
  181. float const AngleY(glm::pi<float>() * 0.25f);
  182. glm::vec3 const axisX(1.0f, 0.0f, 0.0f);
  183. glm::vec3 const axisY(0.0f, 1.0f, 0.0f);
  184. glm::vec4 const V1 = (glm::rotate(glm::mat4(1.0f), AngleX, axisX) * glm::rotate(glm::mat4(1.0f), AngleY, axisY)) * V;
  185. glm::vec4 const V2 = glm::eulerAngleXY(AngleX, AngleY) * V;
  186. glm::vec4 const V3 = glm::eulerAngleX(AngleX) * glm::eulerAngleY(AngleY) * V;
  187. Error += glm::all(glm::epsilonEqual(V1, V2, 0.00001f)) ? 0 : 1;
  188. Error += glm::all(glm::epsilonEqual(V1, V3, 0.00001f)) ? 0 : 1;
  189. return Error;
  190. }
  191. }//namespace test_eulerAngleXY
  192. namespace test_eulerAngleYX
  193. {
  194. static int test()
  195. {
  196. int Error = 0;
  197. glm::vec4 const V(1.0f);
  198. float const AngleX(glm::pi<float>() * 0.5f);
  199. float const AngleY(glm::pi<float>() * 0.25f);
  200. glm::vec3 const axisX(1.0f, 0.0f, 0.0f);
  201. glm::vec3 const axisY(0.0f, 1.0f, 0.0f);
  202. glm::vec4 const V1 = (glm::rotate(glm::mat4(1.0f), AngleY, axisY) * glm::rotate(glm::mat4(1.0f), AngleX, axisX)) * V;
  203. glm::vec4 const V2 = glm::eulerAngleYX(AngleY, AngleX) * V;
  204. glm::vec4 const V3 = glm::eulerAngleY(AngleY) * glm::eulerAngleX(AngleX) * V;
  205. Error += glm::all(glm::epsilonEqual(V1, V2, 0.00001f)) ? 0 : 1;
  206. Error += glm::all(glm::epsilonEqual(V1, V3, 0.00001f)) ? 0 : 1;
  207. return Error;
  208. }
  209. }//namespace test_eulerAngleYX
  210. namespace test_eulerAngleXZ
  211. {
  212. static int test()
  213. {
  214. int Error = 0;
  215. glm::vec4 const V(1.0f);
  216. float const AngleX(glm::pi<float>() * 0.5f);
  217. float const AngleZ(glm::pi<float>() * 0.25f);
  218. glm::vec3 const axisX(1.0f, 0.0f, 0.0f);
  219. glm::vec3 const axisZ(0.0f, 0.0f, 1.0f);
  220. glm::vec4 const V1 = (glm::rotate(glm::mat4(1.0f), AngleX, axisX) * glm::rotate(glm::mat4(1.0f), AngleZ, axisZ)) * V;
  221. glm::vec4 const V2 = glm::eulerAngleXZ(AngleX, AngleZ) * V;
  222. glm::vec4 const V3 = glm::eulerAngleX(AngleX) * glm::eulerAngleZ(AngleZ) * V;
  223. Error += glm::all(glm::epsilonEqual(V1, V2, 0.00001f)) ? 0 : 1;
  224. Error += glm::all(glm::epsilonEqual(V1, V3, 0.00001f)) ? 0 : 1;
  225. return Error;
  226. }
  227. }//namespace test_eulerAngleXZ
  228. namespace test_eulerAngleZX
  229. {
  230. static int test()
  231. {
  232. int Error = 0;
  233. glm::vec4 const V(1.0f);
  234. float const AngleX(glm::pi<float>() * 0.5f);
  235. float const AngleZ(glm::pi<float>() * 0.25f);
  236. glm::vec3 const axisX(1.0f, 0.0f, 0.0f);
  237. glm::vec3 const axisZ(0.0f, 0.0f, 1.0f);
  238. glm::vec4 const V1 = (glm::rotate(glm::mat4(1.0f), AngleZ, axisZ) * glm::rotate(glm::mat4(1.0f), AngleX, axisX)) * V;
  239. glm::vec4 const V2 = glm::eulerAngleZX(AngleZ, AngleX) * V;
  240. glm::vec4 const V3 = glm::eulerAngleZ(AngleZ) * glm::eulerAngleX(AngleX) * V;
  241. Error += glm::all(glm::epsilonEqual(V1, V2, 0.00001f)) ? 0 : 1;
  242. Error += glm::all(glm::epsilonEqual(V1, V3, 0.00001f)) ? 0 : 1;
  243. return Error;
  244. }
  245. }//namespace test_eulerAngleZX
  246. namespace test_eulerAngleYZ
  247. {
  248. static int test()
  249. {
  250. int Error = 0;
  251. glm::vec4 const V(1.0f);
  252. float const AngleY(glm::pi<float>() * 0.5f);
  253. float const AngleZ(glm::pi<float>() * 0.25f);
  254. glm::vec3 const axisY(0.0f, 1.0f, 0.0f);
  255. glm::vec3 const axisZ(0.0f, 0.0f, 1.0f);
  256. glm::vec4 const V1 = (glm::rotate(glm::mat4(1.0f), AngleY, axisY) * glm::rotate(glm::mat4(1.0f), AngleZ, axisZ)) * V;
  257. glm::vec4 const V2 = glm::eulerAngleYZ(AngleY, AngleZ) * V;
  258. glm::vec4 const V3 = glm::eulerAngleY(AngleY) * glm::eulerAngleZ(AngleZ) * V;
  259. Error += glm::all(glm::epsilonEqual(V1, V2, 0.00001f)) ? 0 : 1;
  260. Error += glm::all(glm::epsilonEqual(V1, V3, 0.00001f)) ? 0 : 1;
  261. return Error;
  262. }
  263. }//namespace test_eulerAngleYZ
  264. namespace test_eulerAngleZY
  265. {
  266. static int test()
  267. {
  268. int Error = 0;
  269. glm::vec4 const V(1.0f);
  270. float const AngleY(glm::pi<float>() * 0.5f);
  271. float const AngleZ(glm::pi<float>() * 0.25f);
  272. glm::vec3 const axisY(0.0f, 1.0f, 0.0f);
  273. glm::vec3 const axisZ(0.0f, 0.0f, 1.0f);
  274. glm::vec4 const V1 = (glm::rotate(glm::mat4(1.0f), AngleZ, axisZ) * glm::rotate(glm::mat4(1.0f), AngleY, axisY)) * V;
  275. glm::vec4 const V2 = glm::eulerAngleZY(AngleZ, AngleY) * V;
  276. glm::vec4 const V3 = glm::eulerAngleZ(AngleZ) * glm::eulerAngleY(AngleY) * V;
  277. Error += glm::all(glm::epsilonEqual(V1, V2, 0.00001f)) ? 0 : 1;
  278. Error += glm::all(glm::epsilonEqual(V1, V3, 0.00001f)) ? 0 : 1;
  279. return Error;
  280. }
  281. }//namespace test_eulerAngleZY
  282. namespace test_eulerAngleYXZ
  283. {
  284. static int test()
  285. {
  286. glm::f32 first = 1.046f;
  287. glm::f32 second = 0.52f;
  288. glm::f32 third = -0.785f;
  289. glm::fmat4 rotationEuler = glm::eulerAngleYXZ(first, second, third);
  290. glm::fmat4 rotationInvertedY = glm::eulerAngleY(-1.f*first) * glm::eulerAngleX(second) * glm::eulerAngleZ(third);
  291. glm::fmat4 rotationDumb = glm::fmat4();
  292. rotationDumb = glm::rotate(rotationDumb, first, glm::fvec3(0,1,0));
  293. rotationDumb = glm::rotate(rotationDumb, second, glm::fvec3(1,0,0));
  294. rotationDumb = glm::rotate(rotationDumb, third, glm::fvec3(0,0,1));
  295. std::printf("%s\n", glm::to_string(glm::fmat3(rotationEuler)).c_str());
  296. std::printf("%s\n", glm::to_string(glm::fmat3(rotationDumb)).c_str());
  297. std::printf("%s\n", glm::to_string(glm::fmat3(rotationInvertedY)).c_str());
  298. std::printf("\nRESIDUAL\n");
  299. std::printf("%s\n", glm::to_string(glm::fmat3(rotationEuler-(rotationDumb))).c_str());
  300. std::printf("%s\n", glm::to_string(glm::fmat3(rotationEuler-(rotationInvertedY))).c_str());
  301. return 0;
  302. }
  303. }//namespace eulerAngleYXZ
  304. namespace test_eulerAngles
  305. {
  306. template<typename TestRotationFunc>
  307. static int test(TestRotationFunc testRotationFunc, glm::vec3 const& I, glm::vec3 const& J, glm::vec3 const& K)
  308. {
  309. int Error = 0;
  310. typedef glm::mat4::value_type value;
  311. value const minAngle(-glm::pi<value>());
  312. value const maxAngle(glm::pi<value>());
  313. value const maxAngleWithDelta(maxAngle - 0.0000001f);
  314. value const minMidAngle(-glm::pi<value>() * 0.5f);
  315. value const maxMidAngle(glm::pi<value>() * 0.5f);
  316. std::vector<glm::vec3> testEulerAngles;
  317. testEulerAngles.push_back(glm::vec3(1.046f, 0.52f, -0.785f));
  318. testEulerAngles.push_back(glm::vec3(minAngle, minMidAngle, minAngle));
  319. testEulerAngles.push_back(glm::vec3(minAngle, minMidAngle, maxAngle));
  320. testEulerAngles.push_back(glm::vec3(minAngle, minMidAngle, maxAngleWithDelta));
  321. testEulerAngles.push_back(glm::vec3(minAngle, maxMidAngle, minAngle));
  322. testEulerAngles.push_back(glm::vec3(minAngle, maxMidAngle, maxAngle));
  323. testEulerAngles.push_back(glm::vec3(minAngle, maxMidAngle, maxAngleWithDelta));
  324. testEulerAngles.push_back(glm::vec3(maxAngle, minMidAngle, minAngle));
  325. testEulerAngles.push_back(glm::vec3(maxAngle, minMidAngle, maxAngle));
  326. testEulerAngles.push_back(glm::vec3(maxAngle, minMidAngle, maxAngleWithDelta));
  327. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, minMidAngle, maxAngle));
  328. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, minMidAngle, maxAngleWithDelta));
  329. testEulerAngles.push_back(glm::vec3(maxAngle, maxMidAngle, minAngle));
  330. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, maxMidAngle, minAngle));
  331. testEulerAngles.push_back(glm::vec3(maxAngle, maxMidAngle, maxAngle));
  332. testEulerAngles.push_back(glm::vec3(maxAngle, maxMidAngle, maxAngleWithDelta));
  333. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, maxMidAngle, maxAngle));
  334. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, maxMidAngle, maxAngleWithDelta));
  335. testEulerAngles.push_back(glm::vec3(minAngle, 0.0f, minAngle));
  336. testEulerAngles.push_back(glm::vec3(minAngle, 0.0f, maxAngle));
  337. testEulerAngles.push_back(glm::vec3(maxAngle, maxAngle, minAngle));
  338. testEulerAngles.push_back(glm::vec3(maxAngle, maxAngle, maxAngle));
  339. for (size_t i = 0, size = testEulerAngles.size(); i < size; ++i)
  340. {
  341. glm::vec3 const& angles = testEulerAngles.at(i);
  342. glm::mat4 const rotationEuler = testRotationFunc(angles.x, angles.y, angles.z);
  343. glm::mat4 rotationDumb = glm::diagonal4x4(glm::mat4::col_type(1.0f));
  344. rotationDumb = glm::rotate(rotationDumb, angles.x, I);
  345. rotationDumb = glm::rotate(rotationDumb, angles.y, J);
  346. rotationDumb = glm::rotate(rotationDumb, angles.z, K);
  347. glm::vec4 const V(1.0f,1.0f,1.0f,1.0f);
  348. glm::vec4 const V1 = rotationEuler * V;
  349. glm::vec4 const V2 = rotationDumb * V;
  350. Error += glm::all(glm::epsilonEqual(V1, V2, 0.00001f)) ? 0 : 1;
  351. }
  352. return Error;
  353. }
  354. }//namespace test_extractsEulerAngles
  355. namespace test_extractsEulerAngles
  356. {
  357. template<typename RotationFunc, typename TestExtractionFunc>
  358. static int test(RotationFunc rotationFunc, TestExtractionFunc testExtractionFunc)
  359. {
  360. int Error = 0;
  361. typedef glm::mat4::value_type value;
  362. value const minAngle(-glm::pi<value>());
  363. value const maxAngle(glm::pi<value>());
  364. value const maxAngleWithDelta(maxAngle - 0.0000001f);
  365. value const minMidAngle(-glm::pi<value>() * 0.5f);
  366. value const maxMidAngle(glm::pi<value>() * 0.5f);
  367. std::vector<glm::vec3> testEulerAngles;
  368. testEulerAngles.push_back(glm::vec3(1.046f, 0.52f, -0.785f));
  369. testEulerAngles.push_back(glm::vec3(minAngle, minMidAngle, minAngle));
  370. testEulerAngles.push_back(glm::vec3(minAngle, minMidAngle, maxAngle));
  371. testEulerAngles.push_back(glm::vec3(minAngle, minMidAngle, maxAngleWithDelta));
  372. testEulerAngles.push_back(glm::vec3(minAngle, maxMidAngle, minAngle));
  373. testEulerAngles.push_back(glm::vec3(minAngle, maxMidAngle, maxAngle));
  374. testEulerAngles.push_back(glm::vec3(minAngle, maxMidAngle, maxAngleWithDelta));
  375. testEulerAngles.push_back(glm::vec3(maxAngle, minMidAngle, minAngle));
  376. testEulerAngles.push_back(glm::vec3(maxAngle, minMidAngle, maxAngle));
  377. testEulerAngles.push_back(glm::vec3(maxAngle, minMidAngle, maxAngleWithDelta));
  378. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, minMidAngle, maxAngle));
  379. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, minMidAngle, maxAngleWithDelta));
  380. testEulerAngles.push_back(glm::vec3(maxAngle, maxMidAngle, minAngle));
  381. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, maxMidAngle, minAngle));
  382. testEulerAngles.push_back(glm::vec3(maxAngle, maxMidAngle, maxAngle));
  383. testEulerAngles.push_back(glm::vec3(maxAngle, maxMidAngle, maxAngleWithDelta));
  384. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, maxMidAngle, maxAngle));
  385. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, maxMidAngle, maxAngleWithDelta));
  386. testEulerAngles.push_back(glm::vec3(minAngle, 0.0f, minAngle));
  387. testEulerAngles.push_back(glm::vec3(minAngle, 0.0f, maxAngle));
  388. testEulerAngles.push_back(glm::vec3(maxAngle, maxAngle, minAngle));
  389. testEulerAngles.push_back(glm::vec3(maxAngle, maxAngle, maxAngle));
  390. for (size_t i = 0, size = testEulerAngles.size(); i < size; ++i)
  391. {
  392. glm::vec3 const& angles = testEulerAngles.at(i);
  393. glm::mat4 const rotation = rotationFunc(angles.x, angles.y, angles.z);
  394. glm::vec3 extractedEulerAngles(0.0f);
  395. testExtractionFunc(rotation, extractedEulerAngles.x, extractedEulerAngles.y, extractedEulerAngles.z);
  396. glm::mat4 const extractedRotation = rotationFunc(extractedEulerAngles.x, extractedEulerAngles.y, extractedEulerAngles.z);
  397. glm::vec4 const V(1.0f,1.0f,1.0f,1.0f);
  398. glm::vec4 const V1 = rotation * V;
  399. glm::vec4 const V2 = extractedRotation * V;
  400. Error += glm::all(glm::epsilonEqual(V1, V2, 0.00001f)) ? 0 : 1;
  401. }
  402. return Error;
  403. }
  404. }//namespace test_extractsEulerAngles
  405. int main()
  406. {
  407. int Error = 0;
  408. typedef glm::mat4::value_type value;
  409. glm::vec3 const X(1.0f, 0.0f, 0.0f);
  410. glm::vec3 const Y(0.0f, 1.0f, 0.0f);
  411. glm::vec3 const Z(0.0f, 0.0f, 1.0f);
  412. Error += test_eulerAngleX::test();
  413. Error += test_eulerAngleY::test();
  414. Error += test_eulerAngleZ::test();
  415. Error += test_derivedEulerAngles::test(glm::eulerAngleX<value>, glm::derivedEulerAngleX<value>, X);
  416. Error += test_derivedEulerAngles::test(glm::eulerAngleY<value>, glm::derivedEulerAngleY<value>, Y);
  417. Error += test_derivedEulerAngles::test(glm::eulerAngleZ<value>, glm::derivedEulerAngleZ<value>, Z);
  418. Error += test_eulerAngleXY::test();
  419. Error += test_eulerAngleYX::test();
  420. Error += test_eulerAngleXZ::test();
  421. Error += test_eulerAngleZX::test();
  422. Error += test_eulerAngleYZ::test();
  423. Error += test_eulerAngleZY::test();
  424. Error += test_eulerAngleYXZ::test();
  425. Error += test_eulerAngles::test(glm::eulerAngleXZX<value>, X, Z, X);
  426. Error += test_eulerAngles::test(glm::eulerAngleXYX<value>, X, Y, X);
  427. Error += test_eulerAngles::test(glm::eulerAngleYXY<value>, Y, X, Y);
  428. Error += test_eulerAngles::test(glm::eulerAngleYZY<value>, Y, Z, Y);
  429. Error += test_eulerAngles::test(glm::eulerAngleZYZ<value>, Z, Y, Z);
  430. Error += test_eulerAngles::test(glm::eulerAngleZXZ<value>, Z, X, Z);
  431. Error += test_eulerAngles::test(glm::eulerAngleXZY<value>, X, Z, Y);
  432. Error += test_eulerAngles::test(glm::eulerAngleYZX<value>, Y, Z, X);
  433. Error += test_eulerAngles::test(glm::eulerAngleZYX<value>, Z, Y, X);
  434. Error += test_eulerAngles::test(glm::eulerAngleZXY<value>, Z, X, Y);
  435. Error += test_extractsEulerAngles::test(glm::eulerAngleYXZ<value>, glm::extractEulerAngleYXZ<value>);
  436. Error += test_extractsEulerAngles::test(glm::eulerAngleXZX<value>, glm::extractEulerAngleXZX<value>);
  437. Error += test_extractsEulerAngles::test(glm::eulerAngleXYX<value>, glm::extractEulerAngleXYX<value>);
  438. Error += test_extractsEulerAngles::test(glm::eulerAngleYXY<value>, glm::extractEulerAngleYXY<value>);
  439. Error += test_extractsEulerAngles::test(glm::eulerAngleYZY<value>, glm::extractEulerAngleYZY<value>);
  440. Error += test_extractsEulerAngles::test(glm::eulerAngleZYZ<value>, glm::extractEulerAngleZYZ<value>);
  441. Error += test_extractsEulerAngles::test(glm::eulerAngleZXZ<value>, glm::extractEulerAngleZXZ<value>);
  442. Error += test_extractsEulerAngles::test(glm::eulerAngleXZY<value>, glm::extractEulerAngleXZY<value>);
  443. Error += test_extractsEulerAngles::test(glm::eulerAngleYZX<value>, glm::extractEulerAngleYZX<value>);
  444. Error += test_extractsEulerAngles::test(glm::eulerAngleZYX<value>, glm::extractEulerAngleZYX<value>);
  445. Error += test_extractsEulerAngles::test(glm::eulerAngleZXY<value>, glm::extractEulerAngleZXY<value>);
  446. return Error;
  447. }