testaabbox.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // Copyright (C) 2008-2012 Colin MacDonald
  2. // No rights reserved: this software is in the public domain.
  3. #include "testUtils.h"
  4. using namespace irr;
  5. using namespace core;
  6. // These tests are also only called for f32 and f64, due to conversion problems
  7. // in the respective methods.
  8. template<class T>
  9. static bool checkCollisions()
  10. {
  11. aabbox3d<T> one(0,0,0,4,4,4);
  12. aabbox3d<T> two(2,2,2,4,4,4);
  13. if (two.getInterpolated(one, 1) != two)
  14. {
  15. logTestString("aabbox3d<T> interpolation wrong on 1\n");
  16. return false;
  17. }
  18. if (two.getInterpolated(one, 0) != one)
  19. {
  20. logTestString("aabbox3d<T> interpolation wrong on 0\n");
  21. return false;
  22. }
  23. aabbox3d<T> three(two.getInterpolated(one, 0.5f));
  24. if (two == one)
  25. {
  26. logTestString("aabbox3d<T> interpolation wrong on 0.5 (right)\n");
  27. return false;
  28. }
  29. if (two == three)
  30. {
  31. logTestString("aabbox3d<T> interpolation wrong on 0.5 (left)\n");
  32. return false;
  33. }
  34. three.reset(aabbox3d<T>(2,2,2,5,5,5));
  35. if (!two.isFullInside(one))
  36. {
  37. logTestString("small aabbox3d<T> is not fully inside\n");
  38. return false;
  39. }
  40. if (three.isFullInside(one))
  41. {
  42. logTestString("large aabbox3d<T> is fully inside\n");
  43. return false;
  44. }
  45. if (!two.intersectsWithBox(one))
  46. {
  47. logTestString("small aabbox3d<T> does not intersect\n");
  48. return false;
  49. }
  50. if (!three.intersectsWithBox(one))
  51. {
  52. logTestString("large aabbox3d<T> does not intersect\n");
  53. return false;
  54. }
  55. core::line3d<T> line(-2,-2,-2,2,2,2);
  56. if (!one.intersectsWithLine(line))
  57. {
  58. logTestString("aabbox3d<T> does not intersect with line(1)\n");
  59. return false;
  60. }
  61. line.end.set(2,2,10);
  62. if (!one.intersectsWithLine(line))
  63. {
  64. logTestString("aabbox3d<T> does not intersect with line(2)\n");
  65. return false;
  66. }
  67. line.end.set(0,2,10);
  68. if (one.intersectsWithLine(line))
  69. {
  70. logTestString("aabbox3d<T> does intersect with line(3)\n");
  71. return false;
  72. }
  73. return true;
  74. }
  75. template<class T>
  76. static bool checkPoints()
  77. {
  78. aabbox3d<T> one(-1,-2,-3,2,2,2);
  79. if (!one.isPointInside(core::vector3d<T>(-1,-2,-3)))
  80. {
  81. logTestString("isPointInside failed with min vertex\n");
  82. return false;
  83. }
  84. if (!one.isPointInside(core::vector3d<T>(-1,2,-3)))
  85. {
  86. logTestString("isPointInside failed with other min vertex\n");
  87. return false;
  88. }
  89. if (!one.isPointInside(core::vector3d<T>(2,-2,2)))
  90. {
  91. logTestString("isPointInside failed with other max vertex\n");
  92. return false;
  93. }
  94. if (!one.isPointInside(core::vector3d<T>(2,2,2)))
  95. {
  96. logTestString("isPointInside failed with max vertex\n");
  97. return false;
  98. }
  99. if (!one.isPointInside(core::vector3d<T>(0,0,0)))
  100. {
  101. logTestString("isPointInside failed with origin\n");
  102. return false;
  103. }
  104. if (!one.isPointInside(core::vector3d<T>((T)1.2,-1,1)))
  105. {
  106. logTestString("isPointInside failed with random point inside\n");
  107. return false;
  108. }
  109. if (one.isPointInside(core::vector3d<T>(-2,-2,-3)))
  110. {
  111. logTestString("isPointInside failed near min vertex\n");
  112. return false;
  113. }
  114. if (one.isPointInside(core::vector3d<T>(2,3,2)))
  115. {
  116. logTestString("isPointInside failed near max vertex\n");
  117. return false;
  118. }
  119. if (one.isPointInside(core::vector3d<T>(3,0,0)))
  120. {
  121. logTestString("isPointInside failed near origin\n");
  122. return false;
  123. }
  124. if (one.isPointInside(core::vector3d<T>((T)10.2,-1,1)))
  125. {
  126. logTestString("isPointInside failed with random point outside\n");
  127. return false;
  128. }
  129. if (one.isPointTotalInside(core::vector3d<T>(-1,-2,-3)))
  130. {
  131. logTestString("isPointTotalInside failed with min vertex\n");
  132. return false;
  133. }
  134. if (one.isPointTotalInside(core::vector3d<T>(-1,2,-3)))
  135. {
  136. logTestString("isPointTotalInside failed with other min vertex\n");
  137. return false;
  138. }
  139. if (one.isPointTotalInside(core::vector3d<T>(2,-2,2)))
  140. {
  141. logTestString("isPointTotalInside failed with other max vertex\n");
  142. return false;
  143. }
  144. if (one.isPointTotalInside(core::vector3d<T>(2,2,2)))
  145. {
  146. logTestString("isPointTotalInside failed with max vertex\n");
  147. return false;
  148. }
  149. if (!one.isPointTotalInside(core::vector3d<T>(0,0,0)))
  150. {
  151. logTestString("isPointTotalInside failed with origin\n");
  152. return false;
  153. }
  154. if (!one.isPointTotalInside(core::vector3d<T>((T)1.2,-1,1)))
  155. {
  156. logTestString("isPointTotalInside failed with random point inside\n");
  157. return false;
  158. }
  159. if (one.isPointTotalInside(core::vector3d<T>((T)10.2,-1,1)))
  160. {
  161. logTestString("isPointTotalInside failed with random point outside\n");
  162. return false;
  163. }
  164. core::plane3d<T> plane(core::vector3d<T>(0,0,-1), -10);
  165. if (one.classifyPlaneRelation(plane) != core::ISREL3D_BACK)
  166. {
  167. logTestString("box not behind\n");
  168. return false;
  169. }
  170. plane.D=0;
  171. if (one.classifyPlaneRelation(plane) != core::ISREL3D_CLIPPED)
  172. {
  173. logTestString("box not clipped\n");
  174. return false;
  175. }
  176. plane.D=10;
  177. if (one.classifyPlaneRelation(plane) != core::ISREL3D_FRONT)
  178. {
  179. logTestString("box not in front\n");
  180. return false;
  181. }
  182. return true;
  183. }
  184. template <class T>
  185. static bool doTests()
  186. {
  187. aabbox3d<T> empty;
  188. aabbox3d<T> one(-1,-1,-1,1,1,1);
  189. if (empty != one)
  190. {
  191. logTestString("default aabbox3d<T> wrong, or comparison failed\n");
  192. return false;
  193. }
  194. if (empty.getCenter() != core::vector3d<T>(0,0,0))
  195. {
  196. logTestString("default aabbox3d<T> has wrong Center\n");
  197. return false;
  198. }
  199. if (empty.getExtent() != core::vector3d<T>(2,2,2))
  200. {
  201. logTestString("default aabbox3d<T> has wrong Extent\n");
  202. return false;
  203. }
  204. if (empty.isEmpty())
  205. {
  206. logTestString("default aabbox3d<T> is empty\n");
  207. return false;
  208. }
  209. if (empty.getVolume() != 8)
  210. {
  211. logTestString("default aabbox3d<T> has wrong volume\n");
  212. return false;
  213. }
  214. if (empty.getArea() != 24)
  215. {
  216. logTestString("default aabbox3d<T> has wrong area\n");
  217. return false;
  218. }
  219. aabbox3d<T> two(core::vector3d<T>(-1,-1,-1),core::vector3d<T>(2,2,2));
  220. if (empty == two)
  221. {
  222. logTestString("empty aabbox3d<T> too large, or comparison failed\n");
  223. return false;
  224. }
  225. if (two.getCenter() != core::vector3d<T>((T)0.5,(T)0.5,(T)0.5))
  226. {
  227. logTestString("extended aabbox3d<T> has wrong Center\n");
  228. return false;
  229. }
  230. if (two.getExtent() != core::vector3d<T>(3,3,3))
  231. {
  232. logTestString("extended aabbox3d<T> has wrong Extent\n");
  233. return false;
  234. }
  235. if (two.isEmpty())
  236. {
  237. logTestString("extended aabbox3d<T> is empty\n");
  238. return false;
  239. }
  240. if (two.getVolume() != 27)
  241. {
  242. logTestString("extended aabbox3d<T> has wrong volume\n");
  243. return false;
  244. }
  245. if (two.getArea() != 54)
  246. {
  247. logTestString("extended aabbox3d<T> has wrong area\n");
  248. return false;
  249. }
  250. one.reset(1,1,1);
  251. if (one==empty)
  252. {
  253. logTestString("reset failed, or comparison failed\n");
  254. return false;
  255. }
  256. if (one.getCenter() != core::vector3d<T>(1,1,1))
  257. {
  258. logTestString("singular aabbox3d<T> has wrong Center\n");
  259. return false;
  260. }
  261. if (one.getExtent() != core::vector3d<T>(0,0,0))
  262. {
  263. logTestString("singular aabbox3d<T> has Extent\n");
  264. return false;
  265. }
  266. if (!one.isEmpty())
  267. {
  268. logTestString("empty aabbox3d<T> is not empty\n");
  269. return false;
  270. }
  271. if (one.getVolume() != 0)
  272. {
  273. logTestString("empty aabbox3d<T> has wrong volume\n");
  274. return false;
  275. }
  276. if (one.getArea() != 0)
  277. {
  278. logTestString("empty aabbox3d<T> has wrong area\n");
  279. return false;
  280. }
  281. one.addInternalPoint(core::vector3d<T>(-1,-1,-1));
  282. if (one!=empty)
  283. {
  284. logTestString("addInternalPoint failed, creating default bbox\n");
  285. return false;
  286. }
  287. one.reset(1,1,1);
  288. one.reset(empty);
  289. if (one!=empty)
  290. {
  291. logTestString("reset with bbox failed, creating default bbox\n");
  292. return false;
  293. }
  294. one.addInternalPoint(core::vector3d<T>(2,2,2));
  295. if (one != two)
  296. {
  297. logTestString("addInternalPoint for aabbox3d<T> failed.\n");
  298. return false;
  299. }
  300. one.addInternalBox(empty);
  301. if (one != two)
  302. {
  303. logTestString("addInternalBox with smaller box failed.\n");
  304. return false;
  305. }
  306. one.addInternalBox(two);
  307. if (one != two)
  308. {
  309. logTestString("addInternalBox with same box failed.\n");
  310. return false;
  311. }
  312. one.addInternalPoint(-1,-2,-3);
  313. two.addInternalPoint(-1,-2,-3);
  314. empty.addInternalBox(one);
  315. if (empty != two)
  316. {
  317. logTestString("addInternalBox with larger box failed\n");
  318. return false;
  319. }
  320. if (one.getCenter() != core::vector3d<T>((T)0.5,0,(T)-0.5))
  321. {
  322. logTestString("large aabbox3d<T> has wrong Center\n");
  323. return false;
  324. }
  325. if (one.getExtent() != core::vector3d<T>(3,4,5))
  326. {
  327. logTestString("large aabbox3d<T> has wrong Extent\n");
  328. return false;
  329. }
  330. if (one.isEmpty())
  331. {
  332. logTestString("large aabbox3d<T> is empty\n");
  333. return false;
  334. }
  335. if (one.getVolume() != 60)
  336. {
  337. logTestString("large aabbox3d<T> has wrong volume\n");
  338. return false;
  339. }
  340. if (one.getArea() != 94)
  341. {
  342. logTestString("large aabbox3d<T> has wrong area\n");
  343. return false;
  344. }
  345. if (!checkPoints<T>())
  346. return false;
  347. return true;
  348. }
  349. /** Test the functionality of aabbox3d<T>. */
  350. bool testaabbox3d(void)
  351. {
  352. bool f32Success = doTests<f32>();
  353. f32Success &= checkCollisions<f32>();
  354. if(f32Success)
  355. logTestString("aabbox3d<f32> tests passed\n\n");
  356. else
  357. logTestString("\n*** aabbox3d<f32> tests failed ***\n\n");
  358. bool f64Success = doTests<f64>();
  359. f64Success &= checkCollisions<f64>();
  360. if(f64Success)
  361. logTestString("aabbox3d<f64> tests passed\n\n");
  362. else
  363. logTestString("\n*** aabbox3d<f64> tests failed ***\n\n");
  364. bool s32Success = doTests<s32>();
  365. if(s32Success)
  366. logTestString("aabbox3d<s32> tests passed\n\n");
  367. else
  368. logTestString("\n*** aabbox3d<s32> tests failed ***\n\n");
  369. return f32Success && f64Success && s32Success;
  370. }