gtx_dual_quaternion.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2015 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// Restrictions:
  16. /// By making use of the Software for military purposes, you choose to make
  17. /// a Bunny unhappy.
  18. ///
  19. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. /// THE SOFTWARE.
  26. ///
  27. /// @file test/gtx/gtx_dual_quaternion.cpp
  28. /// @date 2013-02-10 / 2014-11-25
  29. /// @author Christophe Riccio
  30. ///////////////////////////////////////////////////////////////////////////////////
  31. #include <glm/gtx/dual_quaternion.hpp>
  32. #include <glm/gtc/matrix_transform.hpp>
  33. #include <glm/gtc/epsilon.hpp>
  34. #include <glm/gtx/euler_angles.hpp>
  35. #include <glm/vector_relational.hpp>
  36. #if GLM_HAS_TRIVIAL_QUERIES
  37. # include <type_traits>
  38. #endif
  39. int myrand()
  40. {
  41. static int holdrand = 1;
  42. return (((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
  43. }
  44. float myfrand() // returns values from -1 to 1 inclusive
  45. {
  46. return float(double(myrand()) / double( 0x7ffff )) * 2.0f - 1.0f;
  47. }
  48. int test_dquat_type()
  49. {
  50. glm::dvec3 vA;
  51. glm::dquat dqA,dqB;
  52. glm::ddualquat C(dqA,dqB);
  53. glm::ddualquat B(dqA);
  54. glm::ddualquat D(dqA,vA);
  55. return 0;
  56. }
  57. int test_scalars()
  58. {
  59. float const Epsilon = 0.0001f;
  60. int Error(0);
  61. glm::quat src_q1 = glm::quat(1.0f,2.0f,3.0f,4.0f);
  62. glm::quat src_q2 = glm::quat(5.0f,6.0f,7.0f,8.0f);
  63. glm::dualquat src1(src_q1,src_q2);
  64. {
  65. glm::dualquat dst1 = src1 * 2.0f;
  66. glm::dualquat dst2 = 2.0f * src1;
  67. glm::dualquat dst3 = src1;
  68. dst3 *= 2.0f;
  69. glm::dualquat dstCmp(src_q1 * 2.0f,src_q2 * 2.0f);
  70. Error += glm::all(glm::epsilonEqual(dst1.real,dstCmp.real, Epsilon)) && glm::all(glm::epsilonEqual(dst1.dual,dstCmp.dual, Epsilon)) ? 0 : 1;
  71. Error += glm::all(glm::epsilonEqual(dst2.real,dstCmp.real, Epsilon)) && glm::all(glm::epsilonEqual(dst2.dual,dstCmp.dual, Epsilon)) ? 0 : 1;
  72. Error += glm::all(glm::epsilonEqual(dst3.real,dstCmp.real, Epsilon)) && glm::all(glm::epsilonEqual(dst3.dual,dstCmp.dual, Epsilon)) ? 0 : 1;
  73. }
  74. {
  75. glm::dualquat dst1 = src1 / 2.0f;
  76. glm::dualquat dst2 = src1;
  77. dst2 /= 2.0f;
  78. glm::dualquat dstCmp(src_q1 / 2.0f,src_q2 / 2.0f);
  79. Error += glm::all(glm::epsilonEqual(dst1.real,dstCmp.real, Epsilon)) && glm::all(glm::epsilonEqual(dst1.dual,dstCmp.dual, Epsilon)) ? 0 : 1;
  80. Error += glm::all(glm::epsilonEqual(dst2.real,dstCmp.real, Epsilon)) && glm::all(glm::epsilonEqual(dst2.dual,dstCmp.dual, Epsilon)) ? 0 : 1;
  81. }
  82. return Error;
  83. }
  84. int test_inverse()
  85. {
  86. int Error(0);
  87. float const Epsilon = 0.0001f;
  88. glm::dualquat dqid;
  89. glm::mat4x4 mid(1.0f);
  90. for (int j = 0; j < 100; ++j)
  91. {
  92. glm::mat4x4 rot = glm::yawPitchRoll(myfrand() * 360.0f, myfrand() * 360.0f, myfrand() * 360.0f);
  93. glm::vec3 vt = glm::vec3(myfrand() * 10.0f, myfrand() * 10.0f, myfrand() * 10.0f);
  94. glm::mat4x4 m = glm::translate(mid, vt) * rot;
  95. glm::quat qr = glm::quat_cast(m);
  96. glm::dualquat dq(qr);
  97. glm::dualquat invdq = glm::inverse(dq);
  98. glm::dualquat r1 = invdq * dq;
  99. glm::dualquat r2 = dq * invdq;
  100. Error += glm::all(glm::epsilonEqual(r1.real, dqid.real, Epsilon)) && glm::all(glm::epsilonEqual(r1.dual, dqid.dual, Epsilon)) ? 0 : 1;
  101. Error += glm::all(glm::epsilonEqual(r2.real, dqid.real, Epsilon)) && glm::all(glm::epsilonEqual(r2.dual, dqid.dual, Epsilon)) ? 0 : 1;
  102. // testing commutative property
  103. glm::dualquat r ( glm::quat( myfrand() * glm::pi<float>() * 2.0f, myfrand(), myfrand(), myfrand() ),
  104. glm::vec3(myfrand() * 10.0f, myfrand() * 10.0f, myfrand() * 10.0f) );
  105. glm::dualquat riq = (r * invdq) * dq;
  106. glm::dualquat rqi = (r * dq) * invdq;
  107. Error += glm::all(glm::epsilonEqual(riq.real, rqi.real, Epsilon)) && glm::all(glm::epsilonEqual(riq.dual, rqi.dual, Epsilon)) ? 0 : 1;
  108. }
  109. return Error;
  110. }
  111. int test_mul()
  112. {
  113. int Error(0);
  114. float const Epsilon = 0.0001f;
  115. glm::mat4x4 mid(1.0f);
  116. for (int j = 0; j < 100; ++j)
  117. {
  118. // generate random rotations and translations and compare transformed by matrix and dualquats random points
  119. glm::vec3 vt1 = glm::vec3(myfrand() * 10.0f, myfrand() * 10.0f, myfrand() * 10.0f);
  120. glm::vec3 vt2 = glm::vec3(myfrand() * 10.0f, myfrand() * 10.0f, myfrand() * 10.0f);
  121. glm::mat4x4 rot1 = glm::yawPitchRoll(myfrand() * 360.0f, myfrand() * 360.0f, myfrand() * 360.0f);
  122. glm::mat4x4 rot2 = glm::yawPitchRoll(myfrand() * 360.0f, myfrand() * 360.0f, myfrand() * 360.0f);
  123. glm::mat4x4 m1 = glm::translate(mid, vt1) * rot1;
  124. glm::mat4x4 m2 = glm::translate(mid, vt2) * rot2;
  125. glm::mat4x4 m3 = m2 * m1;
  126. glm::mat4x4 m4 = m1 * m2;
  127. glm::quat qrot1 = glm::quat_cast(rot1);
  128. glm::quat qrot2 = glm::quat_cast(rot2);
  129. glm::dualquat dq1 = glm::dualquat(qrot1,vt1);
  130. glm::dualquat dq2 = glm::dualquat(qrot2,vt2);
  131. glm::dualquat dq3 = dq2 * dq1;
  132. glm::dualquat dq4 = dq1 * dq2;
  133. for (int i = 0; i < 100; ++i)
  134. {
  135. glm::vec4 src_pt = glm::vec4(myfrand() * 4.0f, myfrand() * 5.0f, myfrand() * 3.0f,1.0f);
  136. // test both multiplication orders
  137. glm::vec4 dst_pt_m3 = m3 * src_pt;
  138. glm::vec4 dst_pt_dq3 = dq3 * src_pt;
  139. glm::vec4 dst_pt_m3_i = glm::inverse(m3) * src_pt;
  140. glm::vec4 dst_pt_dq3_i = src_pt * dq3;
  141. glm::vec4 dst_pt_m4 = m4 * src_pt;
  142. glm::vec4 dst_pt_dq4 = dq4 * src_pt;
  143. glm::vec4 dst_pt_m4_i = glm::inverse(m4) * src_pt;
  144. glm::vec4 dst_pt_dq4_i = src_pt * dq4;
  145. Error += glm::all(glm::epsilonEqual(dst_pt_m3, dst_pt_dq3, Epsilon)) ? 0 : 1;
  146. Error += glm::all(glm::epsilonEqual(dst_pt_m4, dst_pt_dq4, Epsilon)) ? 0 : 1;
  147. Error += glm::all(glm::epsilonEqual(dst_pt_m3_i, dst_pt_dq3_i, Epsilon)) ? 0 : 1;
  148. Error += glm::all(glm::epsilonEqual(dst_pt_m4_i, dst_pt_dq4_i, Epsilon)) ? 0 : 1;
  149. }
  150. }
  151. return Error;
  152. }
  153. int test_dual_quat_ctr()
  154. {
  155. int Error(0);
  156. # if GLM_HAS_TRIVIAL_QUERIES
  157. // Error += std::is_trivially_default_constructible<glm::dualquat>::value ? 0 : 1;
  158. // Error += std::is_trivially_default_constructible<glm::ddualquat>::value ? 0 : 1;
  159. // Error += std::is_trivially_copy_assignable<glm::dualquat>::value ? 0 : 1;
  160. // Error += std::is_trivially_copy_assignable<glm::ddualquat>::value ? 0 : 1;
  161. Error += std::is_trivially_copyable<glm::dualquat>::value ? 0 : 1;
  162. Error += std::is_trivially_copyable<glm::ddualquat>::value ? 0 : 1;
  163. Error += std::is_copy_constructible<glm::dualquat>::value ? 0 : 1;
  164. Error += std::is_copy_constructible<glm::ddualquat>::value ? 0 : 1;
  165. # endif
  166. return Error;
  167. }
  168. int main()
  169. {
  170. int Error(0);
  171. Error += test_dual_quat_ctr();
  172. Error += test_dquat_type();
  173. Error += test_scalars();
  174. Error += test_inverse();
  175. Error += test_mul();
  176. return Error;
  177. }