test_geometry_3d.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /**************************************************************************/
  2. /* test_geometry_3d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_GEOMETRY_3D_H
  31. #define TEST_GEOMETRY_3D_H
  32. #include "core/math/geometry_3d.h"
  33. #include "tests/test_macros.h"
  34. namespace TestGeometry3D {
  35. TEST_CASE("[Geometry3D] Closest Points Between Segments") {
  36. struct Case {
  37. Vector3 p_1, p_2, p_3, p_4;
  38. Vector3 got_1, got_2;
  39. Vector3 want_1, want_2;
  40. Case(){};
  41. Case(Vector3 p_p_1, Vector3 p_p_2, Vector3 p_p_3, Vector3 p_p_4, Vector3 p_want_1, Vector3 p_want_2) :
  42. p_1(p_p_1), p_2(p_p_2), p_3(p_p_3), p_4(p_p_4), want_1(p_want_1), want_2(p_want_2){};
  43. };
  44. Vector<Case> tt;
  45. tt.push_back(Case(Vector3(1, -1, 1), Vector3(1, 1, -1), Vector3(-1, -2, -1), Vector3(-1, 1, 1), Vector3(1, -0.2, 0.2), Vector3(-1, -0.2, 0.2)));
  46. for (int i = 0; i < tt.size(); ++i) {
  47. Case current_case = tt[i];
  48. Geometry3D::get_closest_points_between_segments(current_case.p_1, current_case.p_2, current_case.p_3, current_case.p_4, current_case.got_1, current_case.got_2);
  49. CHECK(current_case.got_1.is_equal_approx(current_case.want_1));
  50. CHECK(current_case.got_2.is_equal_approx(current_case.want_2));
  51. }
  52. }
  53. TEST_CASE("[Geometry3D] Closest Distance Between Segments") {
  54. struct Case {
  55. Vector3 p_1, p_2, p_3, p_4;
  56. float want;
  57. Case(){};
  58. Case(Vector3 p_p_1, Vector3 p_p_2, Vector3 p_p_3, Vector3 p_p_4, float p_want) :
  59. p_1(p_p_1), p_2(p_p_2), p_3(p_p_3), p_4(p_p_4), want(p_want){};
  60. };
  61. Vector<Case> tt;
  62. tt.push_back(Case(Vector3(1, -2, 0), Vector3(1, 2, 0), Vector3(-1, 2, 0), Vector3(-1, -2, 0), 2.0f));
  63. for (int i = 0; i < tt.size(); ++i) {
  64. Case current_case = tt[i];
  65. float out = Geometry3D::get_closest_distance_between_segments(current_case.p_1, current_case.p_2, current_case.p_3, current_case.p_4);
  66. CHECK(out == current_case.want);
  67. }
  68. }
  69. TEST_CASE("[Geometry3D] Build Box Planes") {
  70. const Vector3 extents = Vector3(5, 5, 20);
  71. Vector<Plane> box = Geometry3D::build_box_planes(extents);
  72. CHECK(box.size() == 6);
  73. CHECK(extents.x == box[0].d);
  74. CHECK(box[0].normal == Vector3(1, 0, 0));
  75. CHECK(extents.x == box[1].d);
  76. CHECK(box[1].normal == Vector3(-1, 0, 0));
  77. CHECK(extents.y == box[2].d);
  78. CHECK(box[2].normal == Vector3(0, 1, 0));
  79. CHECK(extents.y == box[3].d);
  80. CHECK(box[3].normal == Vector3(0, -1, 0));
  81. CHECK(extents.z == box[4].d);
  82. CHECK(box[4].normal == Vector3(0, 0, 1));
  83. CHECK(extents.z == box[5].d);
  84. CHECK(box[5].normal == Vector3(0, 0, -1));
  85. }
  86. TEST_CASE("[Geometry3D] Build Capsule Planes") {
  87. struct Case {
  88. real_t radius, height;
  89. int sides, lats;
  90. Vector3::Axis axis;
  91. int want_size;
  92. Case(){};
  93. Case(real_t p_radius, real_t p_height, int p_sides, int p_lats, Vector3::Axis p_axis, int p_want) :
  94. radius(p_radius), height(p_height), sides(p_sides), lats(p_lats), axis(p_axis), want_size(p_want){};
  95. };
  96. Vector<Case> tt;
  97. tt.push_back(Case(10, 20, 6, 10, Vector3::Axis(), 126));
  98. for (int i = 0; i < tt.size(); ++i) {
  99. Case current_case = tt[i];
  100. Vector<Plane> capsule = Geometry3D::build_capsule_planes(current_case.radius, current_case.height, current_case.sides, current_case.lats, current_case.axis);
  101. // Should equal (p_sides * p_lats) * 2 + p_sides
  102. CHECK(capsule.size() == current_case.want_size);
  103. }
  104. }
  105. TEST_CASE("[Geometry3D] Build Cylinder Planes") {
  106. struct Case {
  107. real_t radius, height;
  108. int sides;
  109. Vector3::Axis axis;
  110. int want_size;
  111. Case(){};
  112. Case(real_t p_radius, real_t p_height, int p_sides, Vector3::Axis p_axis, int p_want) :
  113. radius(p_radius), height(p_height), sides(p_sides), axis(p_axis), want_size(p_want){};
  114. };
  115. Vector<Case> tt;
  116. tt.push_back(Case(3.0f, 10.0f, 10, Vector3::Axis(), 12));
  117. for (int i = 0; i < tt.size(); ++i) {
  118. Case current_case = tt[i];
  119. Vector<Plane> planes = Geometry3D::build_cylinder_planes(current_case.radius, current_case.height, current_case.sides, current_case.axis);
  120. CHECK(planes.size() == current_case.want_size);
  121. }
  122. }
  123. TEST_CASE("[Geometry3D] Build Sphere Planes") {
  124. struct Case {
  125. real_t radius;
  126. int lats, lons;
  127. Vector3::Axis axis;
  128. int want_size;
  129. Case(){};
  130. Case(real_t p_radius, int p_lat, int p_lons, Vector3::Axis p_axis, int p_want) :
  131. radius(p_radius), lats(p_lat), lons(p_lons), axis(p_axis), want_size(p_want){};
  132. };
  133. Vector<Case> tt;
  134. tt.push_back(Case(10.0f, 10, 3, Vector3::Axis(), 63));
  135. for (int i = 0; i < tt.size(); ++i) {
  136. Case current_case = tt[i];
  137. Vector<Plane> planes = Geometry3D::build_sphere_planes(current_case.radius, current_case.lats, current_case.lons, current_case.axis);
  138. CHECK(planes.size() == 63);
  139. }
  140. }
  141. #if false
  142. // This test has been temporarily disabled because it's really fragile and
  143. // breaks if calculations change very slightly. For example, it breaks when
  144. // using doubles, and it breaks when making Plane calculations more accurate.
  145. TEST_CASE("[Geometry3D] Build Convex Mesh") {
  146. struct Case {
  147. Vector<Plane> object;
  148. int want_faces, want_edges, want_vertices;
  149. Case(){};
  150. Case(Vector<Plane> p_object, int p_want_faces, int p_want_edges, int p_want_vertices) :
  151. object(p_object), want_faces(p_want_faces), want_edges(p_want_edges), want_vertices(p_want_vertices){};
  152. };
  153. Vector<Case> tt;
  154. tt.push_back(Case(Geometry3D::build_box_planes(Vector3(5, 10, 5)), 6, 12, 8));
  155. tt.push_back(Case(Geometry3D::build_capsule_planes(5, 5, 20, 20, Vector3::Axis()), 820, 7603, 6243));
  156. tt.push_back(Case(Geometry3D::build_cylinder_planes(5, 5, 20, Vector3::Axis()), 22, 100, 80));
  157. tt.push_back(Case(Geometry3D::build_sphere_planes(5, 5, 20), 220, 1011, 522));
  158. for (int i = 0; i < tt.size(); ++i) {
  159. Case current_case = tt[i];
  160. Geometry3D::MeshData mesh = Geometry3D::build_convex_mesh(current_case.object);
  161. CHECK(mesh.faces.size() == current_case.want_faces);
  162. CHECK(mesh.edges.size() == current_case.want_edges);
  163. CHECK(mesh.vertices.size() == current_case.want_vertices);
  164. }
  165. }
  166. #endif
  167. TEST_CASE("[Geometry3D] Clip Polygon") {
  168. struct Case {
  169. Plane clipping_plane;
  170. Vector<Vector3> polygon;
  171. bool want;
  172. Case(){};
  173. Case(Plane p_clipping_plane, Vector<Vector3> p_polygon, bool p_want) :
  174. clipping_plane(p_clipping_plane), polygon(p_polygon), want(p_want){};
  175. };
  176. Vector<Case> tt;
  177. Vector<Plane> box_planes = Geometry3D::build_box_planes(Vector3(5, 10, 5));
  178. Vector<Vector3> box = Geometry3D::compute_convex_mesh_points(&box_planes[0], box_planes.size());
  179. tt.push_back(Case(Plane(), box, true));
  180. tt.push_back(Case(Plane(Vector3(0, 1, 0), Vector3(0, 3, 0)), box, false));
  181. for (int i = 0; i < tt.size(); ++i) {
  182. Case current_case = tt[i];
  183. Vector<Vector3> output = Geometry3D::clip_polygon(current_case.polygon, current_case.clipping_plane);
  184. if (current_case.want) {
  185. CHECK(output == current_case.polygon);
  186. } else {
  187. CHECK(output != current_case.polygon);
  188. }
  189. }
  190. }
  191. TEST_CASE("[Geometry3D] Compute Convex Mesh Points") {
  192. struct Case {
  193. Vector<Plane> mesh;
  194. Vector<Vector3> want;
  195. Case(){};
  196. Case(Vector<Plane> p_mesh, Vector<Vector3> p_want) :
  197. mesh(p_mesh), want(p_want){};
  198. };
  199. Vector<Case> tt;
  200. Vector<Vector3> cube;
  201. cube.push_back(Vector3(-5, -5, -5));
  202. cube.push_back(Vector3(5, -5, -5));
  203. cube.push_back(Vector3(-5, 5, -5));
  204. cube.push_back(Vector3(5, 5, -5));
  205. cube.push_back(Vector3(-5, -5, 5));
  206. cube.push_back(Vector3(5, -5, 5));
  207. cube.push_back(Vector3(-5, 5, 5));
  208. cube.push_back(Vector3(5, 5, 5));
  209. tt.push_back(Case(Geometry3D::build_box_planes(Vector3(5, 5, 5)), cube));
  210. for (int i = 0; i < tt.size(); ++i) {
  211. Case current_case = tt[i];
  212. Vector<Vector3> vectors = Geometry3D::compute_convex_mesh_points(&current_case.mesh[0], current_case.mesh.size());
  213. CHECK(vectors == current_case.want);
  214. }
  215. }
  216. TEST_CASE("[Geometry3D] Get Closest Point To Segment") {
  217. struct Case {
  218. Vector3 point;
  219. Vector<Vector3> segment;
  220. Vector3 want;
  221. Case(){};
  222. Case(Vector3 p_point, Vector<Vector3> p_segment, Vector3 p_want) :
  223. point(p_point), segment(p_segment), want(p_want){};
  224. };
  225. Vector<Case> tt;
  226. Vector<Vector3> test_segment;
  227. test_segment.push_back(Vector3(1, 1, 1));
  228. test_segment.push_back(Vector3(5, 5, 5));
  229. tt.push_back(Case(Vector3(2, 1, 4), test_segment, Vector3(2.33333, 2.33333, 2.33333)));
  230. for (int i = 0; i < tt.size(); ++i) {
  231. Case current_case = tt[i];
  232. Vector3 output = Geometry3D::get_closest_point_to_segment(current_case.point, &current_case.segment[0]);
  233. CHECK(output.is_equal_approx(current_case.want));
  234. }
  235. }
  236. TEST_CASE("[Geometry3D] Plane and Box Overlap") {
  237. struct Case {
  238. Vector3 normal, max_box;
  239. float d;
  240. bool want;
  241. Case(){};
  242. Case(Vector3 p_normal, float p_d, Vector3 p_max_box, bool p_want) :
  243. normal(p_normal), max_box(p_max_box), d(p_d), want(p_want){};
  244. };
  245. Vector<Case> tt;
  246. tt.push_back(Case(Vector3(3, 4, 2), 5, Vector3(5, 5, 5), true));
  247. tt.push_back(Case(Vector3(0, 1, 0), -10, Vector3(5, 5, 5), false));
  248. tt.push_back(Case(Vector3(1, 0, 0), -6, Vector3(5, 5, 5), false));
  249. for (int i = 0; i < tt.size(); ++i) {
  250. Case current_case = tt[i];
  251. bool overlap = Geometry3D::planeBoxOverlap(current_case.normal, current_case.d, current_case.max_box);
  252. CHECK(overlap == current_case.want);
  253. }
  254. }
  255. TEST_CASE("[Geometry3D] Is Point in Projected Triangle") {
  256. struct Case {
  257. Vector3 point, v_1, v_2, v_3;
  258. bool want;
  259. Case(){};
  260. Case(Vector3 p_point, Vector3 p_v_1, Vector3 p_v_2, Vector3 p_v_3, bool p_want) :
  261. point(p_point), v_1(p_v_1), v_2(p_v_2), v_3(p_v_3), want(p_want){};
  262. };
  263. Vector<Case> tt;
  264. tt.push_back(Case(Vector3(1, 1, 0), Vector3(3, 0, 0), Vector3(0, 3, 0), Vector3(-3, 0, 0), true));
  265. tt.push_back(Case(Vector3(5, 1, 0), Vector3(3, 0, 0), Vector3(0, 3, 0), Vector3(-3, 0, 0), false));
  266. tt.push_back(Case(Vector3(3, 0, 0), Vector3(3, 0, 0), Vector3(0, 3, 0), Vector3(-3, 0, 0), true));
  267. for (int i = 0; i < tt.size(); ++i) {
  268. Case current_case = tt[i];
  269. bool output = Geometry3D::point_in_projected_triangle(current_case.point, current_case.v_1, current_case.v_2, current_case.v_3);
  270. CHECK(output == current_case.want);
  271. }
  272. }
  273. TEST_CASE("[Geometry3D] Does Ray Intersect Triangle") {
  274. struct Case {
  275. Vector3 from, direction, v_1, v_2, v_3;
  276. Vector3 *result = nullptr;
  277. bool want;
  278. Case(){};
  279. Case(Vector3 p_from, Vector3 p_direction, Vector3 p_v_1, Vector3 p_v_2, Vector3 p_v_3, bool p_want) :
  280. from(p_from), direction(p_direction), v_1(p_v_1), v_2(p_v_2), v_3(p_v_3), result(nullptr), want(p_want){};
  281. };
  282. Vector<Case> tt;
  283. tt.push_back(Case(Vector3(0, 1, 1), Vector3(0, 0, -10), Vector3(0, 3, 0), Vector3(-3, 0, 0), Vector3(3, 0, 0), true));
  284. tt.push_back(Case(Vector3(5, 10, 1), Vector3(0, 0, -10), Vector3(0, 3, 0), Vector3(-3, 0, 0), Vector3(3, 0, 0), false));
  285. tt.push_back(Case(Vector3(0, 1, 1), Vector3(0, 0, 10), Vector3(0, 3, 0), Vector3(-3, 0, 0), Vector3(3, 0, 0), false));
  286. for (int i = 0; i < tt.size(); ++i) {
  287. Case current_case = tt[i];
  288. bool output = Geometry3D::ray_intersects_triangle(current_case.from, current_case.direction, current_case.v_1, current_case.v_2, current_case.v_3, current_case.result);
  289. CHECK(output == current_case.want);
  290. }
  291. }
  292. TEST_CASE("[Geometry3D] Does Segment Intersect Convex") {
  293. struct Case {
  294. Vector3 from, to;
  295. Vector<Plane> planes;
  296. Vector3 *result, *normal;
  297. bool want;
  298. Case(){};
  299. Case(Vector3 p_from, Vector3 p_to, Vector<Plane> p_planes, bool p_want) :
  300. from(p_from), to(p_to), planes(p_planes), result(nullptr), normal(nullptr), want(p_want){};
  301. };
  302. Vector<Case> tt;
  303. tt.push_back(Case(Vector3(10, 10, 10), Vector3(0, 0, 0), Geometry3D::build_box_planes(Vector3(5, 5, 5)), true));
  304. tt.push_back(Case(Vector3(10, 10, 10), Vector3(5, 5, 5), Geometry3D::build_box_planes(Vector3(5, 5, 5)), true));
  305. tt.push_back(Case(Vector3(10, 10, 10), Vector3(6, 5, 5), Geometry3D::build_box_planes(Vector3(5, 5, 5)), false));
  306. for (int i = 0; i < tt.size(); ++i) {
  307. Case current_case = tt[i];
  308. bool output = Geometry3D::segment_intersects_convex(current_case.from, current_case.to, &current_case.planes[0], current_case.planes.size(), current_case.result, current_case.normal);
  309. CHECK(output == current_case.want);
  310. }
  311. }
  312. TEST_CASE("[Geometry3D] Segment Intersects Cylinder") {
  313. struct Case {
  314. Vector3 from, to;
  315. real_t height, radius;
  316. Vector3 *result, *normal;
  317. bool want;
  318. Case(){};
  319. Case(Vector3 p_from, Vector3 p_to, real_t p_height, real_t p_radius, bool p_want) :
  320. from(p_from), to(p_to), height(p_height), radius(p_radius), result(nullptr), normal(nullptr), want(p_want){};
  321. };
  322. Vector<Case> tt;
  323. tt.push_back(Case(Vector3(10, 10, 10), Vector3(0, 0, 0), 5, 5, true));
  324. tt.push_back(Case(Vector3(10, 10, 10), Vector3(6, 6, 6), 5, 5, false));
  325. for (int i = 0; i < tt.size(); ++i) {
  326. Case current_case = tt[i];
  327. bool output = Geometry3D::segment_intersects_cylinder(current_case.from, current_case.to, current_case.height, current_case.radius, current_case.result, current_case.normal);
  328. CHECK(output == current_case.want);
  329. }
  330. }
  331. TEST_CASE("[Geometry3D] Segment Intersects Cylinder") {
  332. struct Case {
  333. Vector3 from, to, sphere_pos;
  334. real_t radius;
  335. Vector3 *result, *normal;
  336. bool want;
  337. Case(){};
  338. Case(Vector3 p_from, Vector3 p_to, Vector3 p_sphere_pos, real_t p_radius, bool p_want) :
  339. from(p_from), to(p_to), sphere_pos(p_sphere_pos), radius(p_radius), result(nullptr), normal(nullptr), want(p_want){};
  340. };
  341. Vector<Case> tt;
  342. tt.push_back(Case(Vector3(10, 10, 10), Vector3(0, 0, 0), Vector3(0, 0, 0), 5, true));
  343. tt.push_back(Case(Vector3(10, 10, 10), Vector3(0, 0, 2.5), Vector3(0, 0, 0), 5, true));
  344. tt.push_back(Case(Vector3(10, 10, 10), Vector3(5, 5, 5), Vector3(0, 0, 0), 5, false));
  345. for (int i = 0; i < tt.size(); ++i) {
  346. Case current_case = tt[i];
  347. bool output = Geometry3D::segment_intersects_sphere(current_case.from, current_case.to, current_case.sphere_pos, current_case.radius, current_case.result, current_case.normal);
  348. CHECK(output == current_case.want);
  349. }
  350. }
  351. TEST_CASE("[Geometry3D] Segment Intersects Triangle") {
  352. struct Case {
  353. Vector3 from, to, v_1, v_2, v_3, *result;
  354. bool want;
  355. Case(){};
  356. Case(Vector3 p_from, Vector3 p_to, Vector3 p_v_1, Vector3 p_v_2, Vector3 p_v_3, bool p_want) :
  357. from(p_from), to(p_to), v_1(p_v_1), v_2(p_v_2), v_3(p_v_3), result(nullptr), want(p_want){};
  358. };
  359. Vector<Case> tt;
  360. tt.push_back(Case(Vector3(1, 1, 1), Vector3(-1, -1, -1), Vector3(-3, 0, 0), Vector3(0, 3, 0), Vector3(3, 0, 0), true));
  361. tt.push_back(Case(Vector3(1, 1, 1), Vector3(3, 0, 0), Vector3(-3, 0, 0), Vector3(0, 3, 0), Vector3(3, 0, 0), true));
  362. tt.push_back(Case(Vector3(1, 1, 1), Vector3(10, -1, -1), Vector3(-3, 0, 0), Vector3(0, 3, 0), Vector3(3, 0, 0), false));
  363. for (int i = 0; i < tt.size(); ++i) {
  364. Case current_case = tt[i];
  365. bool output = Geometry3D::segment_intersects_triangle(current_case.from, current_case.to, current_case.v_1, current_case.v_2, current_case.v_3, current_case.result);
  366. CHECK(output == current_case.want);
  367. }
  368. }
  369. TEST_CASE("[Geometry3D] Triangle and Box Overlap") {
  370. struct Case {
  371. Vector3 box_center;
  372. Vector3 box_half_size;
  373. Vector3 *tri_verts = nullptr;
  374. bool want;
  375. Case(){};
  376. Case(Vector3 p_center, Vector3 p_half_size, Vector3 *p_verts, bool p_want) :
  377. box_center(p_center), box_half_size(p_half_size), tri_verts(p_verts), want(p_want){};
  378. };
  379. Vector<Case> tt;
  380. Vector3 GoodTriangle[3] = { Vector3(3, 2, 3), Vector3(2, 2, 1), Vector3(2, 1, 1) };
  381. tt.push_back(Case(Vector3(0, 0, 0), Vector3(5, 5, 5), GoodTriangle, true));
  382. Vector3 BadTriangle[3] = { Vector3(100, 100, 100), Vector3(-100, -100, -100), Vector3(10, 10, 10) };
  383. tt.push_back(Case(Vector3(1000, 1000, 1000), Vector3(1, 1, 1), BadTriangle, false));
  384. for (int i = 0; i < tt.size(); ++i) {
  385. Case current_case = tt[i];
  386. bool output = Geometry3D::triangle_box_overlap(current_case.box_center, current_case.box_half_size, current_case.tri_verts);
  387. CHECK(output == current_case.want);
  388. }
  389. }
  390. TEST_CASE("[Geometry3D] Triangle and Sphere Intersect") {
  391. struct Case {
  392. Vector<Vector3> triangle;
  393. Vector3 normal, sphere_pos, triangle_contact, sphere_contact;
  394. real_t sphere_radius;
  395. bool want;
  396. Case(){};
  397. Case(Vector<Vector3> p_triangle, Vector3 p_normal, Vector3 p_sphere_pos, real_t p_sphere_radius, bool p_want) :
  398. triangle(p_triangle), normal(p_normal), sphere_pos(p_sphere_pos), triangle_contact(Vector3()), sphere_contact(Vector3()), sphere_radius(p_sphere_radius), want(p_want){};
  399. };
  400. Vector<Case> tt;
  401. Vector<Vector3> triangle;
  402. triangle.push_back(Vector3(3, 0, 0));
  403. triangle.push_back(Vector3(-3, 0, 0));
  404. triangle.push_back(Vector3(0, 3, 0));
  405. tt.push_back(Case(triangle, Vector3(0, -1, 0), Vector3(0, 0, 0), 5, true));
  406. tt.push_back(Case(triangle, Vector3(0, 1, 0), Vector3(0, 0, 0), 5, true));
  407. tt.push_back(Case(triangle, Vector3(0, 1, 0), Vector3(20, 0, 0), 5, false));
  408. for (int i = 0; i < tt.size(); ++i) {
  409. Case current_case = tt[i];
  410. bool output = Geometry3D::triangle_sphere_intersection_test(&current_case.triangle[0], current_case.normal, current_case.sphere_pos, current_case.sphere_radius, current_case.triangle_contact, current_case.sphere_contact);
  411. CHECK(output == current_case.want);
  412. }
  413. }
  414. } // namespace TestGeometry3D
  415. #endif // TEST_GEOMETRY_3D_H