PageRenderTime 33ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/tests/llbbox_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 367 lines | 213 code | 79 blank | 75 comment | 10 complexity | 36b19f289330b9ddec2551978e53f09d MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llbbox_test.cpp
  3. * @author Martin Reddy
  4. * @date 2009-06-25
  5. * @brief Test for llbbox.cpp.
  6. *
  7. * $LicenseInfo:firstyear=2009&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 "../test/lltut.h"
  30. #include "../llbbox.h"
  31. #define ANGLE (3.14159265f / 2.0f)
  32. #define APPROX_EQUAL(a, b) (dist_vec_squared((a),(b)) < 1e-10)
  33. namespace tut
  34. {
  35. struct LLBBoxData
  36. {
  37. };
  38. typedef test_group<LLBBoxData> factory;
  39. typedef factory::object object;
  40. }
  41. namespace
  42. {
  43. tut::factory llbbox_test_factory("LLBBox");
  44. }
  45. namespace tut
  46. {
  47. template<> template<>
  48. void object::test<1>()
  49. {
  50. //
  51. // test the default constructor
  52. //
  53. LLBBox bbox1;
  54. ensure_equals("Default bbox min", bbox1.getMinLocal(), LLVector3(0.0f, 0.0f, 0.0f));
  55. ensure_equals("Default bbox max", bbox1.getMaxLocal(), LLVector3(0.0f, 0.0f, 0.0f));
  56. ensure_equals("Default bbox pos agent", bbox1.getPositionAgent(), LLVector3(0.0f, 0.0f, 0.0f));
  57. ensure_equals("Default bbox rotation", bbox1.getRotation(), LLQuaternion(0.0f, 0.0f, 0.0f, 1.0f));
  58. }
  59. template<> template<>
  60. void object::test<2>()
  61. {
  62. //
  63. // test the non-default constructor
  64. //
  65. LLBBox bbox2(LLVector3(1.0f, 2.0f, 3.0f), LLQuaternion(),
  66. LLVector3(2.0f, 3.0f, 4.0f), LLVector3(4.0f, 5.0f, 6.0f));
  67. ensure_equals("Custom bbox min", bbox2.getMinLocal(), LLVector3(2.0f, 3.0f, 4.0f));
  68. ensure_equals("Custom bbox max", bbox2.getMaxLocal(), LLVector3(4.0f, 5.0f, 6.0f));
  69. ensure_equals("Custom bbox pos agent", bbox2.getPositionAgent(), LLVector3(1.0f, 2.0f, 3.0f));
  70. ensure_equals("Custom bbox rotation", bbox2.getRotation(), LLQuaternion(0.0f, 0.0f, 0.0f, 1.0f));
  71. }
  72. template<> template<>
  73. void object::test<3>()
  74. {
  75. //
  76. // test the setMinLocal() method
  77. //
  78. LLBBox bbox2;
  79. bbox2.setMinLocal(LLVector3(3.0f, 3.0f, 3.0f));
  80. ensure_equals("Custom bbox min (2)", bbox2.getMinLocal(), LLVector3(3.0f, 3.0f, 3.0f));
  81. }
  82. template<> template<>
  83. void object::test<4>()
  84. {
  85. //
  86. // test the setMaxLocal() method
  87. //
  88. LLBBox bbox2;
  89. bbox2.setMaxLocal(LLVector3(5.0f, 5.0f, 5.0f));
  90. ensure_equals("Custom bbox max (2)", bbox2.getMaxLocal(), LLVector3(5.0f, 5.0f, 5.0f));
  91. }
  92. template<> template<>
  93. void object::test<5>()
  94. {
  95. //
  96. // test the getCenterLocal() method
  97. //
  98. ensure_equals("Default bbox local center", LLBBox().getCenterLocal(), LLVector3(0.0f, 0.0f, 0.0f));
  99. LLBBox bbox1(LLVector3(1.0f, 2.0f, 3.0f), LLQuaternion(),
  100. LLVector3(2.0f, 4.0f, 6.0f), LLVector3(4.0f, 6.0f, 8.0f));
  101. ensure_equals("Custom bbox center local", bbox1.getCenterLocal(), LLVector3(3.0f, 5.0f, 7.0f));
  102. LLBBox bbox2(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(ANGLE, LLVector3(0.0f, 0.0f, 1.0f)),
  103. LLVector3(2.0f, 2.0f, 2.0f), LLVector3(4.0f, 4.0f, 4.0f));
  104. ensure_equals("Custom bbox center local with rot", bbox2.getCenterLocal(), LLVector3(3.0f, 3.0f, 3.0f));
  105. }
  106. template<> template<>
  107. void object::test<6>()
  108. {
  109. //
  110. // test the getCenterAgent()
  111. //
  112. ensure_equals("Default bbox agent center", LLBBox().getCenterAgent(), LLVector3(0.0f, 0.0f, 0.0f));
  113. LLBBox bbox1(LLVector3(1.0f, 2.0f, 3.0f), LLQuaternion(),
  114. LLVector3(2.0f, 4.0f, 6.0f), LLVector3(4.0f, 6.0f, 8.0f));
  115. ensure_equals("Custom bbox center agent", bbox1.getCenterAgent(), LLVector3(4.0f, 7.0f, 10.0f));
  116. LLBBox bbox2(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(ANGLE, LLVector3(0.0f, 0.0f, 1.0f)),
  117. LLVector3(2.0f, 2.0f, 2.0f), LLVector3(4.0f, 4.0f, 4.0f));
  118. ensure("Custom bbox center agent with rot", APPROX_EQUAL(bbox2.getCenterAgent(), LLVector3(-2.0f, 4.0f, 4.0f)));
  119. }
  120. template<> template<>
  121. void object::test<7>()
  122. {
  123. //
  124. // test the getExtentLocal() method
  125. //
  126. ensure_equals("Default bbox local extent", LLBBox().getExtentLocal(), LLVector3(0.0f, 0.0f, 0.0f));
  127. LLBBox bbox1(LLVector3(1.0f, 2.0f, 3.0f), LLQuaternion(),
  128. LLVector3(2.0f, 4.0f, 6.0f), LLVector3(4.0f, 6.0f, 8.0f));
  129. ensure_equals("Custom bbox extent local", bbox1.getExtentLocal(), LLVector3(2.0f, 2.0f, 2.0f));
  130. LLBBox bbox2(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(ANGLE, LLVector3(0.0f, 0.0f, 1.0f)),
  131. LLVector3(2.0f, 2.0f, 2.0f), LLVector3(4.0f, 4.0f, 4.0f));
  132. ensure_equals("Custom bbox extent local with rot", bbox1.getExtentLocal(), LLVector3(2.0f, 2.0f, 2.0f));
  133. }
  134. template<> template<>
  135. void object::test<8>()
  136. {
  137. //
  138. // test the addPointLocal() method
  139. //
  140. LLBBox bbox1;
  141. bbox1.addPointLocal(LLVector3(1.0f, 1.0f, 1.0f));
  142. bbox1.addPointLocal(LLVector3(3.0f, 3.0f, 3.0f));
  143. ensure_equals("addPointLocal center local (1)", bbox1.getCenterLocal(), LLVector3(2.0f, 2.0f, 2.0f));
  144. ensure_equals("addPointLocal center agent (1)", bbox1.getCenterAgent(), LLVector3(2.0f, 2.0f, 2.0f));
  145. ensure_equals("addPointLocal min (1)", bbox1.getMinLocal(), LLVector3(1.0f, 1.0f, 1.0f));
  146. ensure_equals("addPointLocal max (1)", bbox1.getMaxLocal(), LLVector3(3.0f, 3.0f, 3.0f));
  147. bbox1.addPointLocal(LLVector3(0.0f, 0.0f, 0.0f));
  148. bbox1.addPointLocal(LLVector3(1.0f, 1.0f, 1.0f));
  149. bbox1.addPointLocal(LLVector3(2.0f, 2.0f, 2.0f));
  150. ensure_equals("addPointLocal center local (2)", bbox1.getCenterLocal(), LLVector3(1.5f, 1.5f, 1.5f));
  151. ensure_equals("addPointLocal min (2)", bbox1.getMinLocal(), LLVector3(0.0f, 0.0f, 0.0f));
  152. ensure_equals("addPointLocal max (2)", bbox1.getMaxLocal(), LLVector3(3.0f, 3.0f, 3.0f));
  153. }
  154. template<> template<>
  155. void object::test<9>()
  156. {
  157. //
  158. // test the addBBoxLocal() method
  159. //
  160. LLBBox bbox1;
  161. bbox1.addBBoxLocal(LLBBox(LLVector3(), LLQuaternion(),
  162. LLVector3(0.0f, 0.0f, 0.0f), LLVector3(3.0f, 3.0f, 3.0f)));
  163. ensure_equals("addPointLocal center local (3)", bbox1.getCenterLocal(), LLVector3(1.5f, 1.5f, 1.5f));
  164. ensure_equals("addPointLocal min (3)", bbox1.getMinLocal(), LLVector3(0.0f, 0.0f, 0.0f));
  165. ensure_equals("addPointLocal max (3)", bbox1.getMaxLocal(), LLVector3(3.0f, 3.0f, 3.0f));
  166. bbox1.addBBoxLocal(LLBBox(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(),
  167. LLVector3(5.0f, 5.0f, 5.0f), LLVector3(10.0f, 10.0f, 10.0f)));
  168. ensure_equals("addPointLocal center local (4)", bbox1.getCenterLocal(), LLVector3(5.0f, 5.0f, 5.0f));
  169. ensure_equals("addPointLocal center agent (4)", bbox1.getCenterAgent(), LLVector3(5.0f, 5.0f, 5.0f));
  170. ensure_equals("addPointLocal min (4)", bbox1.getMinLocal(), LLVector3(0.0f, 0.0f, 0.0f));
  171. ensure_equals("addPointLocal max (4)", bbox1.getMaxLocal(), LLVector3(10.0f, 10.0f, 10.0f));
  172. }
  173. template<> template<>
  174. void object::test<10>()
  175. {
  176. //
  177. // test the addPointAgent() method
  178. //
  179. LLBBox bbox1(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(1.0, 0.0, 0.0, 1.0),
  180. LLVector3(2.0f, 2.0f, 2.0f), LLVector3(4.0f, 4.0f, 4.0f));
  181. bbox1.addPointAgent(LLVector3(1.0f, 1.0f, 1.0f));
  182. bbox1.addPointAgent(LLVector3(3.0f, 3.0f, 3.0f));
  183. ensure_equals("addPointAgent center local (1)", bbox1.getCenterLocal(), LLVector3(2.0f, 2.0f, -2.0f));
  184. ensure_equals("addPointAgent center agent (1)", bbox1.getCenterAgent(), LLVector3(3.0f, 3.0f, 7.0f));
  185. ensure_equals("addPointAgent min (1)", bbox1.getMinLocal(), LLVector3(0.0f, 0.0f, -4.0f));
  186. ensure_equals("addPointAgent max (1)", bbox1.getMaxLocal(), LLVector3(4.0f, 4.0f, 0.0f));
  187. }
  188. template<> template<>
  189. void object::test<11>()
  190. {
  191. //
  192. // test the addBBoxAgent() method
  193. //
  194. LLBBox bbox1(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(1.0, 0.0, 0.0, 1.0),
  195. LLVector3(2.0f, 2.0f, 2.0f), LLVector3(4.0f, 4.0f, 4.0f));
  196. bbox1.addPointAgent(LLVector3(1.0f, 1.0f, 1.0f));
  197. bbox1.addPointAgent(LLVector3(3.0f, 3.0f, 3.0f));
  198. bbox1.addBBoxLocal(LLBBox(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(),
  199. LLVector3(5.0f, 5.0f, 5.0f), LLVector3(10.0f, 10.0f, 10.0f)));
  200. ensure_equals("addPointAgent center local (2)", bbox1.getCenterLocal(), LLVector3(5.0f, 5.0f, 3.0f));
  201. ensure_equals("addPointAgent center agent (2)", bbox1.getCenterAgent(), LLVector3(6.0f, -10.0f, 8.0f));
  202. ensure_equals("addPointAgent min (2)", bbox1.getMinLocal(), LLVector3(0.0f, 0.0f, -4.0f));
  203. ensure_equals("addPointAgent max (2)", bbox1.getMaxLocal(), LLVector3(10.0f, 10.0f, 10.0f));
  204. }
  205. template<> template<>
  206. void object::test<12>()
  207. {
  208. //
  209. // test the expand() method
  210. //
  211. LLBBox bbox1;
  212. bbox1.expand(0.0);
  213. ensure_equals("Zero-expanded Default BBox center", bbox1.getCenterLocal(), LLVector3(0.0f, 0.0f, 0.0f));
  214. LLBBox bbox2(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(),
  215. LLVector3(1.0f, 1.0f, 1.0f), LLVector3(3.0f, 3.0f, 3.0f));
  216. bbox2.expand(0.0);
  217. ensure_equals("Zero-expanded center local", bbox2.getCenterLocal(), LLVector3(2.0f, 2.0f, 2.0f));
  218. ensure_equals("Zero-expanded center agent", bbox2.getCenterAgent(), LLVector3(3.0f, 3.0f, 3.0f));
  219. ensure_equals("Zero-expanded min", bbox2.getMinLocal(), LLVector3(1.0f, 1.0f, 1.0f));
  220. ensure_equals("Zero-expanded max", bbox2.getMaxLocal(), LLVector3(3.0f, 3.0f, 3.0f));
  221. bbox2.expand(0.5);
  222. ensure_equals("Positive-expanded center", bbox2.getCenterLocal(), LLVector3(2.0f, 2.0f, 2.0f));
  223. ensure_equals("Positive-expanded min", bbox2.getMinLocal(), LLVector3(0.5f, 0.5f, 0.5f));
  224. ensure_equals("Positive-expanded max", bbox2.getMaxLocal(), LLVector3(3.5f, 3.5f, 3.5f));
  225. bbox2.expand(-1.0);
  226. ensure_equals("Negative-expanded center", bbox2.getCenterLocal(), LLVector3(2.0f, 2.0f, 2.0f));
  227. ensure_equals("Negative-expanded min", bbox2.getMinLocal(), LLVector3(1.5f, 1.5f, 1.5f));
  228. ensure_equals("Negative-expanded max", bbox2.getMaxLocal(), LLVector3(2.5f, 2.5f, 2.5f));
  229. }
  230. template<> template<>
  231. void object::test<13>()
  232. {
  233. //
  234. // test the localToAgent() method
  235. //
  236. LLBBox bbox1(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(),
  237. LLVector3(1.0f, 1.0f, 1.0f), LLVector3(3.0f, 3.0f, 3.0f));
  238. ensure_equals("localToAgent(1,2,3)", bbox1.localToAgent(LLVector3(1.0f, 2.0f, 3.0f)), LLVector3(2.0f, 3.0f, 4.0f));
  239. LLBBox bbox2(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(ANGLE, LLVector3(1.0f, 0.0f, 0.0f)),
  240. LLVector3(1.0f, 1.0f, 1.0f), LLVector3(3.0f, 3.0f, 3.0f));
  241. ensure("localToAgent(1,2,3) rot", APPROX_EQUAL(bbox2.localToAgent(LLVector3(1.0f, 2.0f, 3.0f)), LLVector3(2.0f, -2.0f, 3.0f)));
  242. }
  243. template<> template<>
  244. void object::test<14>()
  245. {
  246. //
  247. // test the agentToLocal() method
  248. //
  249. LLBBox bbox1(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(),
  250. LLVector3(1.0f, 1.0f, 1.0f), LLVector3(3.0f, 3.0f, 3.0f));
  251. ensure_equals("agentToLocal(1,2,3)", bbox1.agentToLocal(LLVector3(1.0f, 2.0f, 3.0f)), LLVector3(0.0f, 1.0f, 2.0f));
  252. ensure_equals("agentToLocal(localToAgent)", bbox1.agentToLocal(bbox1.localToAgent(LLVector3(1.0f, 2.0f, 3.0f))),
  253. LLVector3(1.0f, 2.0f, 3.0f));
  254. LLBBox bbox2(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(ANGLE, LLVector3(1.0f, 0.0f, 0.0f)),
  255. LLVector3(1.0f, 1.0f, 1.0f), LLVector3(3.0f, 3.0f, 3.0f));
  256. ensure("agentToLocal(1,2,3) rot", APPROX_EQUAL(bbox2.agentToLocal(LLVector3(1.0f, 2.0f, 3.0f)), LLVector3(0.0f, 2.0f, -1.0f)));
  257. ensure("agentToLocal(localToAgent) rot", APPROX_EQUAL(bbox2.agentToLocal(bbox2.localToAgent(LLVector3(1.0f, 2.0f, 3.0f))),
  258. LLVector3(1.0f, 2.0f, 3.0f)));
  259. }
  260. template<> template<>
  261. void object::test<15>()
  262. {
  263. //
  264. // test the containsPointLocal() method
  265. //
  266. LLBBox bbox1(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(),
  267. LLVector3(1.0f, 2.0f, 3.0f), LLVector3(3.0f, 4.0f, 5.0f));
  268. ensure("containsPointLocal(0,0,0)", bbox1.containsPointLocal(LLVector3(0.0f, 0.0f, 0.0f)) == FALSE);
  269. ensure("containsPointLocal(1,2,3)", bbox1.containsPointLocal(LLVector3(1.0f, 2.0f, 3.0f)) == TRUE);
  270. ensure("containsPointLocal(0.999,2,3)", bbox1.containsPointLocal(LLVector3(0.999f, 2.0f, 3.0f)) == FALSE);
  271. ensure("containsPointLocal(3,4,5)", bbox1.containsPointLocal(LLVector3(3.0f, 4.0f, 5.0f)) == TRUE);
  272. ensure("containsPointLocal(3,4,5.001)", bbox1.containsPointLocal(LLVector3(3.0f, 4.0f, 5.001f)) == FALSE);
  273. }
  274. template<> template<>
  275. void object::test<16>()
  276. {
  277. //
  278. // test the containsPointAgent() method
  279. //
  280. LLBBox bbox1(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(),
  281. LLVector3(1.0f, 2.0f, 3.0f), LLVector3(3.0f, 4.0f, 5.0f));
  282. ensure("containsPointAgent(0,0,0)", bbox1.containsPointAgent(LLVector3(0.0f, 0.0f, 0.0f)) == FALSE);
  283. ensure("containsPointAgent(2,3,4)", bbox1.containsPointAgent(LLVector3(2.0f, 3.0f, 4.0f)) == TRUE);
  284. ensure("containsPointAgent(2,2.999,4)", bbox1.containsPointAgent(LLVector3(2.0f, 2.999f, 4.0f)) == FALSE);
  285. ensure("containsPointAgent(4,5,6)", bbox1.containsPointAgent(LLVector3(4.0f, 5.0f, 6.0f)) == TRUE);
  286. ensure("containsPointAgent(4,5.001,6)", bbox1.containsPointAgent(LLVector3(4.0f, 5.001f, 6.0f)) == FALSE);
  287. }
  288. }