testaabbox.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 (!core::equals(empty.getRadius(), (T)sqrt(3.0)))
  205. {
  206. logTestString("default aabbox3d<T> has wrong radius\n");
  207. return false;
  208. }
  209. if (empty.isEmpty())
  210. {
  211. logTestString("default aabbox3d<T> is empty\n");
  212. return false;
  213. }
  214. if (empty.getVolume() != 8)
  215. {
  216. logTestString("default aabbox3d<T> has wrong volume\n");
  217. return false;
  218. }
  219. if (empty.getArea() != 24)
  220. {
  221. logTestString("default aabbox3d<T> has wrong area\n");
  222. return false;
  223. }
  224. aabbox3d<T> two(core::vector3d<T>(-1,-1,-1),core::vector3d<T>(2,2,2));
  225. if (empty == two)
  226. {
  227. logTestString("empty aabbox3d<T> too large, or comparison failed\n");
  228. return false;
  229. }
  230. if (two.getCenter() != core::vector3d<T>((T)0.5,(T)0.5,(T)0.5))
  231. {
  232. logTestString("extended aabbox3d<T> has wrong Center\n");
  233. return false;
  234. }
  235. if (two.getExtent() != core::vector3d<T>(3,3,3))
  236. {
  237. logTestString("extended aabbox3d<T> has wrong Extent\n");
  238. return false;
  239. }
  240. if (!core::equals(two.getRadius(), (T)sqrt(27./4.)))
  241. {
  242. logTestString("extended aabbox3d<T> has wrong radius\n");
  243. return false;
  244. }
  245. if (two.isEmpty())
  246. {
  247. logTestString("extended aabbox3d<T> is empty\n");
  248. return false;
  249. }
  250. if (two.getVolume() != 27)
  251. {
  252. logTestString("extended aabbox3d<T> has wrong volume\n");
  253. return false;
  254. }
  255. if (two.getArea() != 54)
  256. {
  257. logTestString("extended aabbox3d<T> has wrong area\n");
  258. return false;
  259. }
  260. one.reset(1,1,1);
  261. if (one==empty)
  262. {
  263. logTestString("reset failed, or comparison failed\n");
  264. return false;
  265. }
  266. if (one.getCenter() != core::vector3d<T>(1,1,1))
  267. {
  268. logTestString("singular aabbox3d<T> has wrong Center\n");
  269. return false;
  270. }
  271. if (one.getExtent() != core::vector3d<T>(0,0,0))
  272. {
  273. logTestString("singular aabbox3d<T> has Extent\n");
  274. return false;
  275. }
  276. if (one.getRadius() != 0)
  277. {
  278. logTestString("singular aabbox3d<T> has radius\n");
  279. return false;
  280. }
  281. if (!one.isEmpty())
  282. {
  283. logTestString("empty aabbox3d<T> is not empty\n");
  284. return false;
  285. }
  286. if (one.getVolume() != 0)
  287. {
  288. logTestString("empty aabbox3d<T> has wrong volume\n");
  289. return false;
  290. }
  291. if (one.getArea() != 0)
  292. {
  293. logTestString("empty aabbox3d<T> has wrong area\n");
  294. return false;
  295. }
  296. one.addInternalPoint(core::vector3d<T>(-1,-1,-1));
  297. if (one!=empty)
  298. {
  299. logTestString("addInternalPoint failed, creating default bbox\n");
  300. return false;
  301. }
  302. one.reset(1,1,1);
  303. one.reset(empty);
  304. if (one!=empty)
  305. {
  306. logTestString("reset with bbox failed, creating default bbox\n");
  307. return false;
  308. }
  309. one.addInternalPoint(core::vector3d<T>(2,2,2));
  310. if (one != two)
  311. {
  312. logTestString("addInternalPoint for aabbox3d<T> failed.\n");
  313. return false;
  314. }
  315. one.addInternalBox(empty);
  316. if (one != two)
  317. {
  318. logTestString("addInternalBox with smaller box failed.\n");
  319. return false;
  320. }
  321. one.addInternalBox(two);
  322. if (one != two)
  323. {
  324. logTestString("addInternalBox with same box failed.\n");
  325. return false;
  326. }
  327. one.addInternalPoint(-1,-2,-3);
  328. two.addInternalPoint(-1,-2,-3);
  329. empty.addInternalBox(one);
  330. if (empty != two)
  331. {
  332. logTestString("addInternalBox with larger box failed\n");
  333. return false;
  334. }
  335. if (one.getCenter() != core::vector3d<T>((T)0.5,0,(T)-0.5))
  336. {
  337. logTestString("large aabbox3d<T> has wrong Center\n");
  338. return false;
  339. }
  340. if (one.getExtent() != core::vector3d<T>(3,4,5))
  341. {
  342. logTestString("large aabbox3d<T> has wrong Extent\n");
  343. return false;
  344. }
  345. if (!core::equals(one.getRadius(), (T)sqrt(50./4.)))
  346. {
  347. logTestString("large aabbox3d<T> has wrong radius\n");
  348. return false;
  349. }
  350. if (one.isEmpty())
  351. {
  352. logTestString("large aabbox3d<T> is empty\n");
  353. return false;
  354. }
  355. if (one.getVolume() != 60)
  356. {
  357. logTestString("large aabbox3d<T> has wrong volume\n");
  358. return false;
  359. }
  360. if (one.getArea() != 94)
  361. {
  362. logTestString("large aabbox3d<T> has wrong area\n");
  363. return false;
  364. }
  365. if (!checkPoints<T>())
  366. return false;
  367. return true;
  368. }
  369. /** Test the functionality of aabbox3d<T>. */
  370. bool testaabbox3d(void)
  371. {
  372. bool f32Success = doTests<f32>();
  373. f32Success &= checkCollisions<f32>();
  374. if(f32Success)
  375. logTestString("aabbox3d<f32> tests passed\n\n");
  376. else
  377. logTestString("\n*** aabbox3d<f32> tests failed ***\n\n");
  378. bool f64Success = doTests<f64>();
  379. f64Success &= checkCollisions<f64>();
  380. if(f64Success)
  381. logTestString("aabbox3d<f64> tests passed\n\n");
  382. else
  383. logTestString("\n*** aabbox3d<f64> tests failed ***\n\n");
  384. bool s32Success = doTests<s32>();
  385. if(s32Success)
  386. logTestString("aabbox3d<s32> tests passed\n\n");
  387. else
  388. logTestString("\n*** aabbox3d<s32> tests failed ***\n\n");
  389. return f32Success && f64Success && s32Success;
  390. }