PageRenderTime 17ms CodeModel.GetById 2ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/tests/m3math_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 332 lines | 236 code | 53 blank | 43 comment | 54 complexity | 6d74f61881a289c0cc9ea195d082939c MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file m3math_test.cpp
  3. * @author Adroit
  4. * @date 2007-03
  5. * @brief Test cases of m3math.h
  6. *
  7. * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. #include "linden_common.h"
  29. #include "../m3math.h"
  30. #include "../v3math.h"
  31. #include "../v4math.h"
  32. #include "../m4math.h"
  33. #include "../llquaternion.h"
  34. #include "../v3dmath.h"
  35. #include "../test/lltut.h"
  36. #if LL_WINDOWS
  37. // disable unreachable code warnings caused by usage of skip.
  38. #pragma warning(disable: 4702)
  39. #endif
  40. #if LL_WINDOWS
  41. // disable unreachable code warnings caused by usage of skip.
  42. #pragma warning(disable: 4702)
  43. #endif
  44. namespace tut
  45. {
  46. struct m3math_test
  47. {
  48. };
  49. typedef test_group<m3math_test> m3math_test_t;
  50. typedef m3math_test_t::object m3math_test_object_t;
  51. tut::m3math_test_t tut_m3math_test("m3math_h");
  52. //test case for setIdentity() fn.
  53. template<> template<>
  54. void m3math_test_object_t::test<1>()
  55. {
  56. LLMatrix3 llmat3_obj;
  57. llmat3_obj.setIdentity();
  58. ensure("LLMatrix3::setIdentity failed", 1.f == llmat3_obj.mMatrix[0][0] &&
  59. 0.f == llmat3_obj.mMatrix[0][1] &&
  60. 0.f == llmat3_obj.mMatrix[0][2] &&
  61. 0.f == llmat3_obj.mMatrix[1][0] &&
  62. 1.f == llmat3_obj.mMatrix[1][1] &&
  63. 0.f == llmat3_obj.mMatrix[1][2] &&
  64. 0.f == llmat3_obj.mMatrix[2][0] &&
  65. 0.f == llmat3_obj.mMatrix[2][1] &&
  66. 1.f == llmat3_obj.mMatrix[2][2]);
  67. }
  68. //test case for LLMatrix3& setZero() fn.
  69. template<> template<>
  70. void m3math_test_object_t::test<2>()
  71. {
  72. LLMatrix3 llmat3_obj(30, 1, 2, 3);
  73. llmat3_obj.setZero();
  74. ensure("LLMatrix3::setZero failed", 0.f == llmat3_obj.setZero().mMatrix[0][0] &&
  75. 0.f == llmat3_obj.setZero().mMatrix[0][1] &&
  76. 0.f == llmat3_obj.setZero().mMatrix[0][2] &&
  77. 0.f == llmat3_obj.setZero().mMatrix[1][0] &&
  78. 0.f == llmat3_obj.setZero().mMatrix[1][1] &&
  79. 0.f == llmat3_obj.setZero().mMatrix[1][2] &&
  80. 0.f == llmat3_obj.setZero().mMatrix[2][0] &&
  81. 0.f == llmat3_obj.setZero().mMatrix[2][1] &&
  82. 0.f == llmat3_obj.setZero().mMatrix[2][2]);
  83. }
  84. //test case for setRows(const LLVector3 &x_axis, const LLVector3 &y_axis, const LLVector3 &z_axis) fns.
  85. template<> template<>
  86. void m3math_test_object_t::test<3>()
  87. {
  88. LLMatrix3 llmat3_obj;
  89. LLVector3 vect1(2, 1, 4);
  90. LLVector3 vect2(3, 5, 7);
  91. LLVector3 vect3(6, 9, 7);
  92. llmat3_obj.setRows(vect1, vect2, vect3);
  93. ensure("LLVector3::setRows failed ", 2 == llmat3_obj.mMatrix[0][0] &&
  94. 1 == llmat3_obj.mMatrix[0][1] &&
  95. 4 == llmat3_obj.mMatrix[0][2] &&
  96. 3 == llmat3_obj.mMatrix[1][0] &&
  97. 5 == llmat3_obj.mMatrix[1][1] &&
  98. 7 == llmat3_obj.mMatrix[1][2] &&
  99. 6 == llmat3_obj.mMatrix[2][0] &&
  100. 9 == llmat3_obj.mMatrix[2][1] &&
  101. 7 == llmat3_obj.mMatrix[2][2]);
  102. }
  103. //test case for getFwdRow(), getLeftRow(), getUpRow() fns.
  104. template<> template<>
  105. void m3math_test_object_t::test<4>()
  106. {
  107. LLMatrix3 llmat3_obj;
  108. LLVector3 vect1(2, 1, 4);
  109. LLVector3 vect2(3, 5, 7);
  110. LLVector3 vect3(6, 9, 7);
  111. llmat3_obj.setRows(vect1, vect2, vect3);
  112. ensure("LLVector3::getFwdRow failed ", vect1 == llmat3_obj.getFwdRow());
  113. ensure("LLVector3::getLeftRow failed ", vect2 == llmat3_obj.getLeftRow());
  114. ensure("LLVector3::getUpRow failed ", vect3 == llmat3_obj.getUpRow());
  115. }
  116. //test case for operator*(const LLMatrix3 &a, const LLMatrix3 &b)
  117. template<> template<>
  118. void m3math_test_object_t::test<5>()
  119. {
  120. LLMatrix3 llmat_obj1;
  121. LLMatrix3 llmat_obj2;
  122. LLMatrix3 llmat_obj3;
  123. LLVector3 llvec1(1, 3, 5);
  124. LLVector3 llvec2(3, 6, 1);
  125. LLVector3 llvec3(4, 6, 9);
  126. LLVector3 llvec4(1, 1, 5);
  127. LLVector3 llvec5(3, 6, 8);
  128. LLVector3 llvec6(8, 6, 2);
  129. LLVector3 llvec7(0, 0, 0);
  130. LLVector3 llvec8(0, 0, 0);
  131. LLVector3 llvec9(0, 0, 0);
  132. llmat_obj1.setRows(llvec1, llvec2, llvec3);
  133. llmat_obj2.setRows(llvec4, llvec5, llvec6);
  134. llmat_obj3.setRows(llvec7, llvec8, llvec9);
  135. llmat_obj3 = llmat_obj1 * llmat_obj2;
  136. ensure("LLMatrix3::operator*(const LLMatrix3 &a, const LLMatrix3 &b) failed",
  137. 50 == llmat_obj3.mMatrix[0][0] &&
  138. 49 == llmat_obj3.mMatrix[0][1] &&
  139. 39 == llmat_obj3.mMatrix[0][2] &&
  140. 29 == llmat_obj3.mMatrix[1][0] &&
  141. 45 == llmat_obj3.mMatrix[1][1] &&
  142. 65 == llmat_obj3.mMatrix[1][2] &&
  143. 94 == llmat_obj3.mMatrix[2][0] &&
  144. 94 == llmat_obj3.mMatrix[2][1] &&
  145. 86 == llmat_obj3.mMatrix[2][2]);
  146. }
  147. //test case for operator*(const LLVector3 &a, const LLMatrix3 &b)
  148. template<> template<>
  149. void m3math_test_object_t::test<6>()
  150. {
  151. LLMatrix3 llmat_obj1;
  152. LLVector3 llvec(1, 3, 5);
  153. LLVector3 res_vec(0, 0, 0);
  154. LLVector3 llvec1(1, 3, 5);
  155. LLVector3 llvec2(3, 6, 1);
  156. LLVector3 llvec3(4, 6, 9);
  157. llmat_obj1.setRows(llvec1, llvec2, llvec3);
  158. res_vec = llvec * llmat_obj1;
  159. LLVector3 expected_result(30, 51, 53);
  160. ensure("LLMatrix3::operator*(const LLVector3 &a, const LLMatrix3 &b) failed", res_vec == expected_result);
  161. }
  162. //test case for operator*(const LLVector3d &a, const LLMatrix3 &b)
  163. template<> template<>
  164. void m3math_test_object_t::test<7>()
  165. {
  166. LLMatrix3 llmat_obj1;
  167. LLVector3d llvec3d1;
  168. LLVector3d llvec3d2(0, 3, 4);
  169. LLVector3 llvec1(1, 3, 5);
  170. LLVector3 llvec2(3, 2, 1);
  171. LLVector3 llvec3(4, 6, 0);
  172. llmat_obj1.setRows(llvec1, llvec2, llvec3);
  173. llvec3d1 = llvec3d2 * llmat_obj1;
  174. LLVector3d expected_result(25, 30, 3);
  175. ensure("LLMatrix3::operator*(const LLVector3 &a, const LLMatrix3 &b) failed", llvec3d1 == expected_result);
  176. }
  177. // test case for operator==(const LLMatrix3 &a, const LLMatrix3 &b)
  178. template<> template<>
  179. void m3math_test_object_t::test<8>()
  180. {
  181. LLMatrix3 llmat_obj1;
  182. LLMatrix3 llmat_obj2;
  183. LLVector3 llvec1(1, 3, 5);
  184. LLVector3 llvec2(3, 6, 1);
  185. LLVector3 llvec3(4, 6, 9);
  186. llmat_obj1.setRows(llvec1, llvec2, llvec3);
  187. llmat_obj2.setRows(llvec1, llvec2, llvec3);
  188. ensure("LLMatrix3::operator==(const LLMatrix3 &a, const LLMatrix3 &b) failed", llmat_obj1 == llmat_obj2);
  189. llmat_obj2.setRows(llvec2, llvec2, llvec3);
  190. ensure("LLMatrix3::operator!=(const LLMatrix3 &a, const LLMatrix3 &b) failed", llmat_obj1 != llmat_obj2);
  191. }
  192. //test case for quaternion() fn.
  193. template<> template<>
  194. void m3math_test_object_t::test<9>()
  195. {
  196. LLMatrix3 llmat_obj1;
  197. LLQuaternion llmat_quat;
  198. LLVector3 llmat1(2.0f, 1.0f, 6.0f);
  199. LLVector3 llmat2(1.0f, 1.0f, 3.0f);
  200. LLVector3 llmat3(1.0f, 7.0f, 5.0f);
  201. llmat_obj1.setRows(llmat1, llmat2, llmat3);
  202. llmat_quat = llmat_obj1.quaternion();
  203. ensure("LLMatrix3::quaternion failed ", is_approx_equal(-0.66666669f, llmat_quat.mQ[0]) &&
  204. is_approx_equal(-0.83333337f, llmat_quat.mQ[1]) &&
  205. is_approx_equal(0.0f, llmat_quat.mQ[2]) &&
  206. is_approx_equal(1.5f, llmat_quat.mQ[3]));
  207. }
  208. //test case for transpose() fn.
  209. template<> template<>
  210. void m3math_test_object_t::test<10>()
  211. {
  212. LLMatrix3 llmat_obj;
  213. LLVector3 llvec1(1, 2, 3);
  214. LLVector3 llvec2(3, 2, 1);
  215. LLVector3 llvec3(2, 2, 2);
  216. llmat_obj.setRows(llvec1, llvec2, llvec3);
  217. llmat_obj.transpose();
  218. LLVector3 resllvec1(1, 3, 2);
  219. LLVector3 resllvec2(2, 2, 2);
  220. LLVector3 resllvec3(3, 1, 2);
  221. LLMatrix3 expectedllmat_obj;
  222. expectedllmat_obj.setRows(resllvec1, resllvec2, resllvec3);
  223. ensure("LLMatrix3::transpose failed ", llmat_obj == expectedllmat_obj);
  224. }
  225. //test case for determinant() fn.
  226. template<> template<>
  227. void m3math_test_object_t::test<11>()
  228. {
  229. LLMatrix3 llmat_obj1;
  230. LLVector3 llvec1(1, 2, 3);
  231. LLVector3 llvec2(3, 2, 1);
  232. LLVector3 llvec3(2, 2, 2);
  233. llmat_obj1.setRows(llvec1, llvec2, llvec3);
  234. ensure("LLMatrix3::determinant failed ", 0.0f == llmat_obj1.determinant());
  235. }
  236. //test case for orthogonalize() fn.
  237. template<> template<>
  238. void m3math_test_object_t::test<12>()
  239. {
  240. LLMatrix3 llmat_obj;
  241. LLVector3 llvec1(1, 4, 3);
  242. LLVector3 llvec2(1, 2, 0);
  243. LLVector3 llvec3(2, 4, 2);
  244. skip("This test fails depending on architecture. Need to fix comparison operation, is_approx_equal, to work on more than one platform.");
  245. llmat_obj.setRows(llvec1, llvec2, llvec3);
  246. llmat_obj.orthogonalize();
  247. ensure("LLMatrix3::orthogonalize failed ",
  248. is_approx_equal(0.19611614f, llmat_obj.mMatrix[0][0]) &&
  249. is_approx_equal(0.78446454f, llmat_obj.mMatrix[0][1]) &&
  250. is_approx_equal(0.58834841f, llmat_obj.mMatrix[0][2]) &&
  251. is_approx_equal(0.47628204f, llmat_obj.mMatrix[1][0]) &&
  252. is_approx_equal(0.44826545f, llmat_obj.mMatrix[1][1]) &&
  253. is_approx_equal(-0.75644795f, llmat_obj.mMatrix[1][2]) &&
  254. is_approx_equal(-0.85714286f, llmat_obj.mMatrix[2][0]) &&
  255. is_approx_equal(0.42857143f, llmat_obj.mMatrix[2][1]) &&
  256. is_approx_equal(-0.28571429f, llmat_obj.mMatrix[2][2]));
  257. }
  258. //test case for adjointTranspose() fn.
  259. template<> template<>
  260. void m3math_test_object_t::test<13>()
  261. {
  262. LLMatrix3 llmat_obj;
  263. LLVector3 llvec1(3, 2, 1);
  264. LLVector3 llvec2(6, 2, 1);
  265. LLVector3 llvec3(3, 6, 8);
  266. llmat_obj.setRows(llvec1, llvec2, llvec3);
  267. llmat_obj.adjointTranspose();
  268. ensure("LLMatrix3::adjointTranspose failed ", 10 == llmat_obj.mMatrix[0][0] &&
  269. -45 == llmat_obj.mMatrix[1][0] &&
  270. 30 == llmat_obj.mMatrix[2][0] &&
  271. -10 == llmat_obj.mMatrix[0][1] &&
  272. 21 == llmat_obj.mMatrix[1][1] &&
  273. -12 == llmat_obj.mMatrix[2][1] &&
  274. 0 == llmat_obj.mMatrix[0][2] &&
  275. 3 == llmat_obj.mMatrix[1][2] &&
  276. -6 == llmat_obj.mMatrix[2][2]);
  277. }
  278. /* TBD: Need to add test cases for getEulerAngles() and setRot() functions */
  279. }