test_geometry_2d.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /**************************************************************************/
  2. /* test_geometry_2d.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_2D_H
  31. #define TEST_GEOMETRY_2D_H
  32. #include "core/math/geometry_2d.h"
  33. #include "thirdparty/doctest/doctest.h"
  34. namespace TestGeometry2D {
  35. TEST_CASE("[Geometry2D] Point in circle") {
  36. CHECK(Geometry2D::is_point_in_circle(Vector2(0, 0), Vector2(0, 0), 1.0));
  37. CHECK(Geometry2D::is_point_in_circle(Vector2(0, 0), Vector2(11.99, 0), 12));
  38. CHECK(Geometry2D::is_point_in_circle(Vector2(-11.99, 0), Vector2(0, 0), 12));
  39. CHECK_FALSE(Geometry2D::is_point_in_circle(Vector2(0, 0), Vector2(12.01, 0), 12));
  40. CHECK_FALSE(Geometry2D::is_point_in_circle(Vector2(-12.01, 0), Vector2(0, 0), 12));
  41. CHECK(Geometry2D::is_point_in_circle(Vector2(7, -42), Vector2(4, -40), 3.7));
  42. CHECK_FALSE(Geometry2D::is_point_in_circle(Vector2(7, -42), Vector2(4, -40), 3.5));
  43. // This tests points on the edge of the circle. They are treated as being inside the circle.
  44. CHECK(Geometry2D::is_point_in_circle(Vector2(1.0, 0.0), Vector2(0, 0), 1.0));
  45. CHECK(Geometry2D::is_point_in_circle(Vector2(0.0, -1.0), Vector2(0, 0), 1.0));
  46. }
  47. TEST_CASE("[Geometry2D] Point in triangle") {
  48. CHECK(Geometry2D::is_point_in_triangle(Vector2(0, 0), Vector2(-1, 1), Vector2(0, -1), Vector2(1, 1)));
  49. CHECK_FALSE(Geometry2D::is_point_in_triangle(Vector2(-1.01, 1.0), Vector2(-1, 1), Vector2(0, -1), Vector2(1, 1)));
  50. CHECK(Geometry2D::is_point_in_triangle(Vector2(3, 2.5), Vector2(1, 4), Vector2(3, 2), Vector2(5, 4)));
  51. CHECK(Geometry2D::is_point_in_triangle(Vector2(-3, -2.5), Vector2(-1, -4), Vector2(-3, -2), Vector2(-5, -4)));
  52. CHECK_FALSE(Geometry2D::is_point_in_triangle(Vector2(0, 0), Vector2(1, 4), Vector2(3, 2), Vector2(5, 4)));
  53. // This tests points on the edge of the triangle. They are treated as being outside the triangle.
  54. // In `is_point_in_circle` and `is_point_in_polygon` they are treated as being inside, so in order the make
  55. // the behavior consistent this may change in the future (see issue #44717 and PR #44274).
  56. CHECK_FALSE(Geometry2D::is_point_in_triangle(Vector2(1, 1), Vector2(-1, 1), Vector2(0, -1), Vector2(1, 1)));
  57. CHECK_FALSE(Geometry2D::is_point_in_triangle(Vector2(0, 1), Vector2(-1, 1), Vector2(0, -1), Vector2(1, 1)));
  58. }
  59. TEST_CASE("[Geometry2D] Point in polygon") {
  60. Vector<Vector2> p;
  61. CHECK_FALSE(Geometry2D::is_point_in_polygon(Vector2(0, 0), p));
  62. p.push_back(Vector2(-88, 120));
  63. p.push_back(Vector2(-74, -38));
  64. p.push_back(Vector2(135, -145));
  65. p.push_back(Vector2(425, 70));
  66. p.push_back(Vector2(68, 112));
  67. p.push_back(Vector2(-120, 370));
  68. p.push_back(Vector2(-323, -145));
  69. CHECK_FALSE(Geometry2D::is_point_in_polygon(Vector2(-350, 0), p));
  70. CHECK_FALSE(Geometry2D::is_point_in_polygon(Vector2(-110, 60), p));
  71. CHECK_FALSE(Geometry2D::is_point_in_polygon(Vector2(412, 96), p));
  72. CHECK_FALSE(Geometry2D::is_point_in_polygon(Vector2(83, 130), p));
  73. CHECK_FALSE(Geometry2D::is_point_in_polygon(Vector2(-320, -153), p));
  74. CHECK(Geometry2D::is_point_in_polygon(Vector2(0, 0), p));
  75. CHECK(Geometry2D::is_point_in_polygon(Vector2(-230, 0), p));
  76. CHECK(Geometry2D::is_point_in_polygon(Vector2(130, -110), p));
  77. CHECK(Geometry2D::is_point_in_polygon(Vector2(370, 55), p));
  78. CHECK(Geometry2D::is_point_in_polygon(Vector2(-160, 190), p));
  79. // This tests points on the edge of the polygon. They are treated as being inside the polygon.
  80. int c = p.size();
  81. for (int i = 0; i < c; i++) {
  82. const Vector2 &p1 = p[i];
  83. CHECK(Geometry2D::is_point_in_polygon(p1, p));
  84. const Vector2 &p2 = p[(i + 1) % c];
  85. Vector2 midpoint((p1 + p2) * 0.5);
  86. CHECK(Geometry2D::is_point_in_polygon(midpoint, p));
  87. }
  88. }
  89. TEST_CASE("[Geometry2D] Polygon clockwise") {
  90. Vector<Vector2> p;
  91. CHECK_FALSE(Geometry2D::is_polygon_clockwise(p));
  92. p.push_back(Vector2(5, -5));
  93. p.push_back(Vector2(-1, -5));
  94. p.push_back(Vector2(-5, -1));
  95. p.push_back(Vector2(-1, 3));
  96. p.push_back(Vector2(1, 5));
  97. CHECK(Geometry2D::is_polygon_clockwise(p));
  98. p.reverse();
  99. CHECK_FALSE(Geometry2D::is_polygon_clockwise(p));
  100. }
  101. TEST_CASE("[Geometry2D] Line intersection") {
  102. Vector2 r;
  103. CHECK(Geometry2D::line_intersects_line(Vector2(2, 0), Vector2(0, 1), Vector2(0, 2), Vector2(1, 0), r));
  104. CHECK(r.is_equal_approx(Vector2(2, 2)));
  105. CHECK(Geometry2D::line_intersects_line(Vector2(-1, 1), Vector2(1, -1), Vector2(4, 1), Vector2(-1, -1), r));
  106. CHECK(r.is_equal_approx(Vector2(1.5, -1.5)));
  107. CHECK(Geometry2D::line_intersects_line(Vector2(-1, 0), Vector2(-1, -1), Vector2(1, 0), Vector2(1, -1), r));
  108. CHECK(r.is_equal_approx(Vector2(0, 1)));
  109. CHECK_FALSE_MESSAGE(
  110. Geometry2D::line_intersects_line(Vector2(-1, 1), Vector2(1, -1), Vector2(0, 1), Vector2(1, -1), r),
  111. "Parallel lines should not intersect.");
  112. }
  113. TEST_CASE("[Geometry2D] Segment intersection") {
  114. Vector2 r;
  115. CHECK(Geometry2D::segment_intersects_segment(Vector2(-1, 1), Vector2(1, -1), Vector2(1, 1), Vector2(-1, -1), &r));
  116. CHECK(r.is_equal_approx(Vector2(0, 0)));
  117. CHECK_FALSE(Geometry2D::segment_intersects_segment(Vector2(-1, 1), Vector2(1, -1), Vector2(1, 1), Vector2(0.1, 0.1), &r));
  118. CHECK_FALSE(Geometry2D::segment_intersects_segment(Vector2(-1, 1), Vector2(1, -1), Vector2(0.1, 0.1), Vector2(1, 1), &r));
  119. CHECK_FALSE_MESSAGE(
  120. Geometry2D::segment_intersects_segment(Vector2(-1, 1), Vector2(1, -1), Vector2(0, 1), Vector2(2, -1), &r),
  121. "Parallel segments should not intersect.");
  122. CHECK_FALSE_MESSAGE(
  123. Geometry2D::segment_intersects_segment(Vector2(1, 2), Vector2(3, 2), Vector2(0, 2), Vector2(-2, 2), &r),
  124. "Non-overlapping collinear segments should not intersect.");
  125. CHECK_MESSAGE(
  126. Geometry2D::segment_intersects_segment(Vector2(0, 0), Vector2(0, 1), Vector2(0, 0), Vector2(1, 0), &r),
  127. "Touching segments should intersect.");
  128. CHECK(r.is_equal_approx(Vector2(0, 0)));
  129. CHECK_MESSAGE(
  130. Geometry2D::segment_intersects_segment(Vector2(0, 1), Vector2(0, 0), Vector2(0, 0), Vector2(1, 0), &r),
  131. "Touching segments should intersect.");
  132. CHECK(r.is_equal_approx(Vector2(0, 0)));
  133. }
  134. TEST_CASE("[Geometry2D] Segment intersection with circle") {
  135. real_t minus_one = -1.0;
  136. real_t zero = 0.0;
  137. real_t one_quarter = 0.25;
  138. real_t three_quarters = 0.75;
  139. real_t one = 1.0;
  140. CHECK_MESSAGE(
  141. Geometry2D::segment_intersects_circle(Vector2(0, 0), Vector2(4, 0), Vector2(0, 0), 1.0) == doctest::Approx(one_quarter),
  142. "Segment from inside to outside of circle should intersect it.");
  143. CHECK_MESSAGE(
  144. Geometry2D::segment_intersects_circle(Vector2(4, 0), Vector2(0, 0), Vector2(0, 0), 1.0) == doctest::Approx(three_quarters),
  145. "Segment from outside to inside of circle should intersect it.");
  146. CHECK_MESSAGE(
  147. Geometry2D::segment_intersects_circle(Vector2(-2, 0), Vector2(2, 0), Vector2(0, 0), 1.0) == doctest::Approx(one_quarter),
  148. "Segment running through circle should intersect it.");
  149. CHECK_MESSAGE(
  150. Geometry2D::segment_intersects_circle(Vector2(2, 0), Vector2(-2, 0), Vector2(0, 0), 1.0) == doctest::Approx(one_quarter),
  151. "Segment running through circle should intersect it.");
  152. CHECK_MESSAGE(
  153. Geometry2D::segment_intersects_circle(Vector2(0, 0), Vector2(1, 0), Vector2(0, 0), 1.0) == doctest::Approx(one),
  154. "Segment starting inside the circle and ending on the circle should intersect it");
  155. CHECK_MESSAGE(
  156. Geometry2D::segment_intersects_circle(Vector2(1, 0), Vector2(0, 0), Vector2(0, 0), 1.0) == doctest::Approx(zero),
  157. "Segment starting on the circle and going inwards should intersect it");
  158. CHECK_MESSAGE(
  159. Geometry2D::segment_intersects_circle(Vector2(1, 0), Vector2(2, 0), Vector2(0, 0), 1.0) == doctest::Approx(zero),
  160. "Segment starting on the circle and going outwards should intersect it");
  161. CHECK_MESSAGE(
  162. Geometry2D::segment_intersects_circle(Vector2(2, 0), Vector2(1, 0), Vector2(0, 0), 1.0) == doctest::Approx(one),
  163. "Segment starting outside the circle and ending on the circle intersect it");
  164. CHECK_MESSAGE(
  165. Geometry2D::segment_intersects_circle(Vector2(-1, 0), Vector2(1, 0), Vector2(0, 0), 2.0) == doctest::Approx(minus_one),
  166. "Segment completely within the circle should not intersect it");
  167. CHECK_MESSAGE(
  168. Geometry2D::segment_intersects_circle(Vector2(1, 0), Vector2(-1, 0), Vector2(0, 0), 2.0) == doctest::Approx(minus_one),
  169. "Segment completely within the circle should not intersect it");
  170. CHECK_MESSAGE(
  171. Geometry2D::segment_intersects_circle(Vector2(2, 0), Vector2(3, 0), Vector2(0, 0), 1.0) == doctest::Approx(minus_one),
  172. "Segment completely outside the circle should not intersect it");
  173. CHECK_MESSAGE(
  174. Geometry2D::segment_intersects_circle(Vector2(3, 0), Vector2(2, 0), Vector2(0, 0), 1.0) == doctest::Approx(minus_one),
  175. "Segment completely outside the circle should not intersect it");
  176. }
  177. TEST_CASE("[Geometry2D] Segment intersection with polygon") {
  178. Vector<Point2> a;
  179. a.push_back(Point2(-2, 2));
  180. a.push_back(Point2(3, 4));
  181. a.push_back(Point2(1, 1));
  182. a.push_back(Point2(2, -2));
  183. a.push_back(Point2(-1, -1));
  184. CHECK_MESSAGE(
  185. Geometry2D::is_segment_intersecting_polygon(Vector2(0, 2), Vector2(2, 2), a),
  186. "Segment from inside to outside of polygon should intersect it.");
  187. CHECK_MESSAGE(
  188. Geometry2D::is_segment_intersecting_polygon(Vector2(2, 2), Vector2(0, 2), a),
  189. "Segment from outside to inside of polygon should intersect it.");
  190. CHECK_MESSAGE(
  191. Geometry2D::is_segment_intersecting_polygon(Vector2(2, 4), Vector2(3, 3), a),
  192. "Segment running through polygon should intersect it.");
  193. CHECK_MESSAGE(
  194. Geometry2D::is_segment_intersecting_polygon(Vector2(3, 3), Vector2(2, 4), a),
  195. "Segment running through polygon should intersect it.");
  196. CHECK_MESSAGE(
  197. Geometry2D::is_segment_intersecting_polygon(Vector2(0, 0), Vector2(1, 1), a),
  198. "Segment starting inside the polygon and ending on the polygon should intersect it");
  199. CHECK_MESSAGE(
  200. Geometry2D::is_segment_intersecting_polygon(Vector2(1, 1), Vector2(0, 0), a),
  201. "Segment starting on the polygon and going inwards should intersect it");
  202. CHECK_MESSAGE(
  203. Geometry2D::is_segment_intersecting_polygon(Vector2(-2, 2), Vector2(-2, -1), a),
  204. "Segment starting on the polygon and going outwards should intersect it");
  205. CHECK_MESSAGE(
  206. Geometry2D::is_segment_intersecting_polygon(Vector2(-2, 1), Vector2(-2, 2), a),
  207. "Segment starting outside the polygon and ending on the polygon intersect it");
  208. CHECK_FALSE_MESSAGE(
  209. Geometry2D::is_segment_intersecting_polygon(Vector2(-1, 2), Vector2(1, -1), a),
  210. "Segment completely within the polygon should not intersect it");
  211. CHECK_FALSE_MESSAGE(
  212. Geometry2D::is_segment_intersecting_polygon(Vector2(1, -1), Vector2(-1, 2), a),
  213. "Segment completely within the polygon should not intersect it");
  214. CHECK_FALSE_MESSAGE(
  215. Geometry2D::is_segment_intersecting_polygon(Vector2(2, 2), Vector2(2, -1), a),
  216. "Segment completely outside the polygon should not intersect it");
  217. CHECK_FALSE_MESSAGE(
  218. Geometry2D::is_segment_intersecting_polygon(Vector2(2, -1), Vector2(2, 2), a),
  219. "Segment completely outside the polygon should not intersect it");
  220. }
  221. TEST_CASE("[Geometry2D] Closest point to segment") {
  222. Vector2 s[] = { Vector2(-4, -4), Vector2(4, 4) };
  223. CHECK(Geometry2D::get_closest_point_to_segment(Vector2(4.1, 4.1), s).is_equal_approx(Vector2(4, 4)));
  224. CHECK(Geometry2D::get_closest_point_to_segment(Vector2(-4.1, -4.1), s).is_equal_approx(Vector2(-4, -4)));
  225. CHECK(Geometry2D::get_closest_point_to_segment(Vector2(-1, 1), s).is_equal_approx(Vector2(0, 0)));
  226. Vector2 t[] = { Vector2(1, -2), Vector2(1, -2) };
  227. CHECK_MESSAGE(
  228. Geometry2D::get_closest_point_to_segment(Vector2(-3, 4), t).is_equal_approx(Vector2(1, -2)),
  229. "Line segment is only a single point. This point should be the closest.");
  230. }
  231. TEST_CASE("[Geometry2D] Closest point to uncapped segment") {
  232. Vector2 s[] = { Vector2(-4, -4), Vector2(4, 4) };
  233. CHECK(Geometry2D::get_closest_point_to_segment_uncapped(Vector2(-1, 1), s).is_equal_approx(Vector2(0, 0)));
  234. CHECK(Geometry2D::get_closest_point_to_segment_uncapped(Vector2(-4, -6), s).is_equal_approx(Vector2(-5, -5)));
  235. CHECK(Geometry2D::get_closest_point_to_segment_uncapped(Vector2(4, 6), s).is_equal_approx(Vector2(5, 5)));
  236. }
  237. TEST_CASE("[Geometry2D] Closest points between segments") {
  238. Vector2 c1, c2;
  239. // Basis Path Testing suite
  240. SUBCASE("[Geometry2D] Both segments degenerate to a point") {
  241. Geometry2D::get_closest_points_between_segments(Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), c1, c2);
  242. CHECK(c1.is_equal_approx(Vector2(0, 0)));
  243. CHECK(c2.is_equal_approx(Vector2(0, 0)));
  244. }
  245. SUBCASE("[Geometry2D] Closest point on second segment trajectory is above [0,1]") {
  246. Geometry2D::get_closest_points_between_segments(Vector2(50, -25), Vector2(50, -10), Vector2(-50, 10), Vector2(-40, 10), c1, c2);
  247. CHECK(c1.is_equal_approx(Vector2(50, -10)));
  248. CHECK(c2.is_equal_approx(Vector2(-40, 10)));
  249. }
  250. SUBCASE("[Geometry2D] Parallel segments") {
  251. Geometry2D::get_closest_points_between_segments(Vector2(2, 1), Vector2(4, 3), Vector2(2, 3), Vector2(4, 5), c1, c2);
  252. CHECK(c1.is_equal_approx(Vector2(3, 2)));
  253. CHECK(c2.is_equal_approx(Vector2(2, 3)));
  254. }
  255. SUBCASE("[Geometry2D] Closest point on second segment trajectory is within [0,1]") {
  256. Geometry2D::get_closest_points_between_segments(Vector2(2, 4), Vector2(2, 3), Vector2(1, 1), Vector2(4, 4), c1, c2);
  257. CHECK(c1.is_equal_approx(Vector2(2, 3)));
  258. CHECK(c2.is_equal_approx(Vector2(2.5, 2.5)));
  259. }
  260. SUBCASE("[Geometry2D] Closest point on second segment trajectory is below [0,1]") {
  261. Geometry2D::get_closest_points_between_segments(Vector2(-20, -20), Vector2(-10, -40), Vector2(10, 25), Vector2(25, 40), c1, c2);
  262. CHECK(c1.is_equal_approx(Vector2(-20, -20)));
  263. CHECK(c2.is_equal_approx(Vector2(10, 25)));
  264. }
  265. SUBCASE("[Geometry2D] Second segment degenerates to a point") {
  266. Geometry2D::get_closest_points_between_segments(Vector2(1, 2), Vector2(2, 1), Vector2(3, 3), Vector2(3, 3), c1, c2);
  267. CHECK(c1.is_equal_approx(Vector2(1.5, 1.5)));
  268. CHECK(c2.is_equal_approx(Vector2(3, 3)));
  269. }
  270. SUBCASE("[Geometry2D] First segment degenerates to a point") {
  271. Geometry2D::get_closest_points_between_segments(Vector2(1, 1), Vector2(1, 1), Vector2(2, 2), Vector2(4, 4), c1, c2);
  272. CHECK(c1.is_equal_approx(Vector2(1, 1)));
  273. CHECK(c2.is_equal_approx(Vector2(2, 2)));
  274. }
  275. // End Basis Path Testing suite
  276. SUBCASE("[Geometry2D] Segments are equal vectors") {
  277. Geometry2D::get_closest_points_between_segments(Vector2(2, 2), Vector2(3, 3), Vector2(4, 4), Vector2(4, 5), c1, c2);
  278. CHECK(c1.is_equal_approx(Vector2(3, 3)));
  279. CHECK(c2.is_equal_approx(Vector2(4, 4)));
  280. }
  281. SUBCASE("[Geometry2D] Standard case") {
  282. Geometry2D::get_closest_points_between_segments(Vector2(0, 1), Vector2(-2, -1), Vector2(0, 0), Vector2(2, -2), c1, c2);
  283. CHECK(c1.is_equal_approx(Vector2(-0.5, 0.5)));
  284. CHECK(c2.is_equal_approx(Vector2(0, 0)));
  285. }
  286. SUBCASE("[Geometry2D] Segments intersect") {
  287. Geometry2D::get_closest_points_between_segments(Vector2(-1, 1), Vector2(1, -1), Vector2(1, 1), Vector2(-1, -1), c1, c2);
  288. CHECK(c1.is_equal_approx(Vector2(0, 0)));
  289. CHECK(c2.is_equal_approx(Vector2(0, 0)));
  290. }
  291. }
  292. TEST_CASE("[Geometry2D] Make atlas") {
  293. Vector<Point2i> result;
  294. Size2i size;
  295. Vector<Size2i> r;
  296. r.push_back(Size2i(2, 2));
  297. Geometry2D::make_atlas(r, result, size);
  298. CHECK(size == Size2i(2, 2));
  299. CHECK(result.size() == r.size());
  300. r.clear();
  301. result.clear();
  302. r.push_back(Size2i(1, 2));
  303. r.push_back(Size2i(3, 4));
  304. r.push_back(Size2i(5, 6));
  305. r.push_back(Size2i(7, 8));
  306. Geometry2D::make_atlas(r, result, size);
  307. CHECK(result.size() == r.size());
  308. }
  309. TEST_CASE("[Geometry2D] Polygon intersection") {
  310. Vector<Point2> a;
  311. Vector<Point2> b;
  312. Vector<Vector<Point2>> r;
  313. a.push_back(Point2(30, 60));
  314. a.push_back(Point2(70, 5));
  315. a.push_back(Point2(200, 40));
  316. a.push_back(Point2(80, 200));
  317. SUBCASE("[Geometry2D] Both polygons are empty") {
  318. r = Geometry2D::intersect_polygons(Vector<Point2>(), Vector<Point2>());
  319. CHECK_MESSAGE(r.is_empty(), "Both polygons are empty. The intersection should also be empty.");
  320. }
  321. SUBCASE("[Geometry2D] One polygon is empty") {
  322. r = Geometry2D::intersect_polygons(a, b);
  323. REQUIRE_MESSAGE(r.is_empty(), "One polygon is empty. The intersection should also be empty.");
  324. }
  325. SUBCASE("[Geometry2D] Basic intersection") {
  326. b.push_back(Point2(200, 300));
  327. b.push_back(Point2(90, 200));
  328. b.push_back(Point2(50, 100));
  329. b.push_back(Point2(200, 90));
  330. r = Geometry2D::intersect_polygons(a, b);
  331. REQUIRE_MESSAGE(r.size() == 1, "The polygons should intersect each other with 1 resulting intersection polygon.");
  332. REQUIRE_MESSAGE(r[0].size() == 3, "The resulting intersection polygon should have 3 vertices.");
  333. CHECK(r[0][0].is_equal_approx(Point2(86.52174, 191.30436)));
  334. CHECK(r[0][1].is_equal_approx(Point2(50, 100)));
  335. CHECK(r[0][2].is_equal_approx(Point2(160.52632, 92.63157)));
  336. }
  337. SUBCASE("[Geometry2D] Intersection with one polygon being completely inside the other polygon") {
  338. b.push_back(Point2(80, 100));
  339. b.push_back(Point2(50, 50));
  340. b.push_back(Point2(150, 50));
  341. r = Geometry2D::intersect_polygons(a, b);
  342. REQUIRE_MESSAGE(r.size() == 1, "The polygons should intersect each other with 1 resulting intersection polygon.");
  343. REQUIRE_MESSAGE(r[0].size() == 3, "The resulting intersection polygon should have 3 vertices.");
  344. CHECK(r[0][0].is_equal_approx(b[0]));
  345. CHECK(r[0][1].is_equal_approx(b[1]));
  346. CHECK(r[0][2].is_equal_approx(b[2]));
  347. }
  348. SUBCASE("[Geometry2D] No intersection with 2 non-empty polygons") {
  349. b.push_back(Point2(150, 150));
  350. b.push_back(Point2(250, 100));
  351. b.push_back(Point2(300, 200));
  352. r = Geometry2D::intersect_polygons(a, b);
  353. REQUIRE_MESSAGE(r.is_empty(), "The polygons should not intersect each other.");
  354. }
  355. SUBCASE("[Geometry2D] Intersection with 2 resulting polygons") {
  356. a.clear();
  357. a.push_back(Point2(70, 5));
  358. a.push_back(Point2(140, 7));
  359. a.push_back(Point2(100, 52));
  360. a.push_back(Point2(170, 50));
  361. a.push_back(Point2(60, 125));
  362. b.push_back(Point2(70, 105));
  363. b.push_back(Point2(115, 55));
  364. b.push_back(Point2(90, 15));
  365. b.push_back(Point2(160, 50));
  366. r = Geometry2D::intersect_polygons(a, b);
  367. REQUIRE_MESSAGE(r.size() == 2, "The polygons should intersect each other with 2 resulting intersection polygons.");
  368. REQUIRE_MESSAGE(r[0].size() == 4, "The resulting intersection polygon should have 4 vertices.");
  369. CHECK(r[0][0].is_equal_approx(Point2(70, 105)));
  370. CHECK(r[0][1].is_equal_approx(Point2(115, 55)));
  371. CHECK(r[0][2].is_equal_approx(Point2(112.894737, 51.63158)));
  372. CHECK(r[0][3].is_equal_approx(Point2(159.509537, 50.299728)));
  373. REQUIRE_MESSAGE(r[1].size() == 3, "The intersection polygon should have 3 vertices.");
  374. CHECK(r[1][0].is_equal_approx(Point2(119.692307, 29.846149)));
  375. CHECK(r[1][1].is_equal_approx(Point2(107.706421, 43.33028)));
  376. CHECK(r[1][2].is_equal_approx(Point2(90, 15)));
  377. }
  378. }
  379. TEST_CASE("[Geometry2D] Merge polygons") {
  380. Vector<Point2> a;
  381. Vector<Point2> b;
  382. Vector<Vector<Point2>> r;
  383. a.push_back(Point2(225, 180));
  384. a.push_back(Point2(160, 230));
  385. a.push_back(Point2(20, 212));
  386. a.push_back(Point2(50, 115));
  387. SUBCASE("[Geometry2D] Both polygons are empty") {
  388. r = Geometry2D::merge_polygons(Vector<Point2>(), Vector<Point2>());
  389. REQUIRE_MESSAGE(r.is_empty(), "Both polygons are empty. The union should also be empty.");
  390. }
  391. SUBCASE("[Geometry2D] One polygon is empty") {
  392. r = Geometry2D::merge_polygons(a, b);
  393. REQUIRE_MESSAGE(r.size() == 1, "One polygon is non-empty. There should be 1 resulting merged polygon.");
  394. REQUIRE_MESSAGE(r[0].size() == 4, "The resulting merged polygon should have 4 vertices.");
  395. CHECK(r[0][0].is_equal_approx(a[0]));
  396. CHECK(r[0][1].is_equal_approx(a[1]));
  397. CHECK(r[0][2].is_equal_approx(a[2]));
  398. CHECK(r[0][3].is_equal_approx(a[3]));
  399. }
  400. SUBCASE("[Geometry2D] Basic merge with 2 polygons") {
  401. b.push_back(Point2(180, 190));
  402. b.push_back(Point2(60, 140));
  403. b.push_back(Point2(160, 80));
  404. r = Geometry2D::merge_polygons(a, b);
  405. REQUIRE_MESSAGE(r.size() == 1, "The merged polygons should result in 1 polygon.");
  406. REQUIRE_MESSAGE(r[0].size() == 7, "The resulting merged polygon should have 7 vertices.");
  407. CHECK(r[0][0].is_equal_approx(Point2(174.791077, 161.350967)));
  408. CHECK(r[0][1].is_equal_approx(Point2(225, 180)));
  409. CHECK(r[0][2].is_equal_approx(Point2(160, 230)));
  410. CHECK(r[0][3].is_equal_approx(Point2(20, 212)));
  411. CHECK(r[0][4].is_equal_approx(Point2(50, 115)));
  412. CHECK(r[0][5].is_equal_approx(Point2(81.911758, 126.852943)));
  413. CHECK(r[0][6].is_equal_approx(Point2(160, 80)));
  414. }
  415. SUBCASE("[Geometry2D] Merge with 2 resulting merged polygons (outline and hole)") {
  416. b.push_back(Point2(180, 190));
  417. b.push_back(Point2(140, 125));
  418. b.push_back(Point2(60, 140));
  419. b.push_back(Point2(160, 80));
  420. r = Geometry2D::merge_polygons(a, b);
  421. REQUIRE_MESSAGE(r.size() == 2, "The merged polygons should result in 2 polygons.");
  422. REQUIRE_MESSAGE(!Geometry2D::is_polygon_clockwise(r[0]), "The merged polygon (outline) should be counter-clockwise.");
  423. REQUIRE_MESSAGE(r[0].size() == 7, "The resulting merged polygon (outline) should have 7 vertices.");
  424. CHECK(r[0][0].is_equal_approx(Point2(174.791077, 161.350967)));
  425. CHECK(r[0][1].is_equal_approx(Point2(225, 180)));
  426. CHECK(r[0][2].is_equal_approx(Point2(160, 230)));
  427. CHECK(r[0][3].is_equal_approx(Point2(20, 212)));
  428. CHECK(r[0][4].is_equal_approx(Point2(50, 115)));
  429. CHECK(r[0][5].is_equal_approx(Point2(81.911758, 126.852943)));
  430. CHECK(r[0][6].is_equal_approx(Point2(160, 80)));
  431. REQUIRE_MESSAGE(Geometry2D::is_polygon_clockwise(r[1]), "The resulting merged polygon (hole) should be clockwise.");
  432. REQUIRE_MESSAGE(r[1].size() == 3, "The resulting merged polygon (hole) should have 3 vertices.");
  433. CHECK(r[1][0].is_equal_approx(Point2(98.083069, 132.859421)));
  434. CHECK(r[1][1].is_equal_approx(Point2(158.689453, 155.370377)));
  435. CHECK(r[1][2].is_equal_approx(Point2(140, 125)));
  436. }
  437. }
  438. TEST_CASE("[Geometry2D] Clip polygons") {
  439. Vector<Point2> a;
  440. Vector<Point2> b;
  441. Vector<Vector<Point2>> r;
  442. a.push_back(Point2(225, 180));
  443. a.push_back(Point2(160, 230));
  444. a.push_back(Point2(20, 212));
  445. a.push_back(Point2(50, 115));
  446. SUBCASE("[Geometry2D] Both polygons are empty") {
  447. r = Geometry2D::clip_polygons(Vector<Point2>(), Vector<Point2>());
  448. CHECK_MESSAGE(r.is_empty(), "Both polygons are empty. The clip should also be empty.");
  449. }
  450. SUBCASE("[Geometry2D] Basic clip with one result polygon") {
  451. b.push_back(Point2(250, 170));
  452. b.push_back(Point2(175, 270));
  453. b.push_back(Point2(120, 260));
  454. b.push_back(Point2(25, 80));
  455. r = Geometry2D::clip_polygons(a, b);
  456. REQUIRE_MESSAGE(r.size() == 1, "The clipped polygons should result in 1 polygon.");
  457. REQUIRE_MESSAGE(r[0].size() == 3, "The resulting clipped polygon should have 3 vertices.");
  458. CHECK(r[0][0].is_equal_approx(Point2(100.102173, 222.298843)));
  459. CHECK(r[0][1].is_equal_approx(Point2(20, 212)));
  460. CHECK(r[0][2].is_equal_approx(Point2(47.588089, 122.798492)));
  461. }
  462. SUBCASE("[Geometry2D] Polygon b completely overlaps polygon a") {
  463. b.push_back(Point2(250, 170));
  464. b.push_back(Point2(175, 270));
  465. b.push_back(Point2(10, 210));
  466. b.push_back(Point2(55, 80));
  467. r = Geometry2D::clip_polygons(a, b);
  468. CHECK_MESSAGE(r.is_empty(), "Polygon 'b' completely overlaps polygon 'a'. This should result in no clipped polygons.");
  469. }
  470. SUBCASE("[Geometry2D] Polygon a completely overlaps polygon b") {
  471. b.push_back(Point2(150, 200));
  472. b.push_back(Point2(65, 190));
  473. b.push_back(Point2(80, 140));
  474. r = Geometry2D::clip_polygons(a, b);
  475. REQUIRE_MESSAGE(r.size() == 2, "Polygon 'a' completely overlaps polygon 'b'. This should result in 2 clipped polygons.");
  476. REQUIRE_MESSAGE(r[0].size() == 4, "The resulting clipped polygon should have 4 vertices.");
  477. REQUIRE_MESSAGE(!Geometry2D::is_polygon_clockwise(r[0]), "The resulting clipped polygon (outline) should be counter-clockwise.");
  478. CHECK(r[0][0].is_equal_approx(a[0]));
  479. CHECK(r[0][1].is_equal_approx(a[1]));
  480. CHECK(r[0][2].is_equal_approx(a[2]));
  481. CHECK(r[0][3].is_equal_approx(a[3]));
  482. REQUIRE_MESSAGE(r[1].size() == 3, "The resulting clipped polygon should have 3 vertices.");
  483. REQUIRE_MESSAGE(Geometry2D::is_polygon_clockwise(r[1]), "The resulting clipped polygon (hole) should be clockwise.");
  484. CHECK(r[1][0].is_equal_approx(b[1]));
  485. CHECK(r[1][1].is_equal_approx(b[0]));
  486. CHECK(r[1][2].is_equal_approx(b[2]));
  487. }
  488. }
  489. TEST_CASE("[Geometry2D] Exclude polygons") {
  490. Vector<Point2> a;
  491. Vector<Point2> b;
  492. Vector<Vector<Point2>> r;
  493. a.push_back(Point2(225, 180));
  494. a.push_back(Point2(160, 230));
  495. a.push_back(Point2(20, 212));
  496. a.push_back(Point2(50, 115));
  497. SUBCASE("[Geometry2D] Both polygons are empty") {
  498. r = Geometry2D::exclude_polygons(Vector<Point2>(), Vector<Point2>());
  499. CHECK_MESSAGE(r.is_empty(), "Both polygons are empty. The excluded polygon should also be empty.");
  500. }
  501. SUBCASE("[Geometry2D] One polygon is empty") {
  502. r = Geometry2D::exclude_polygons(a, b);
  503. REQUIRE_MESSAGE(r.size() == 1, "One polygon is non-empty. There should be 1 resulting excluded polygon.");
  504. REQUIRE_MESSAGE(r[0].size() == 4, "The resulting excluded polygon should have 4 vertices.");
  505. CHECK(r[0][0].is_equal_approx(a[0]));
  506. CHECK(r[0][1].is_equal_approx(a[1]));
  507. CHECK(r[0][2].is_equal_approx(a[2]));
  508. CHECK(r[0][3].is_equal_approx(a[3]));
  509. }
  510. SUBCASE("[Geometry2D] Exclude with 2 resulting polygons (outline and hole)") {
  511. b.push_back(Point2(140, 160));
  512. b.push_back(Point2(150, 220));
  513. b.push_back(Point2(40, 200));
  514. b.push_back(Point2(60, 140));
  515. r = Geometry2D::exclude_polygons(a, b);
  516. REQUIRE_MESSAGE(r.size() == 2, "There should be 2 resulting excluded polygons (outline and hole).");
  517. REQUIRE_MESSAGE(r[0].size() == 4, "The resulting excluded polygon should have 4 vertices.");
  518. REQUIRE_MESSAGE(!Geometry2D::is_polygon_clockwise(r[0]), "The resulting excluded polygon (outline) should be counter-clockwise.");
  519. CHECK(r[0][0].is_equal_approx(a[0]));
  520. CHECK(r[0][1].is_equal_approx(a[1]));
  521. CHECK(r[0][2].is_equal_approx(a[2]));
  522. CHECK(r[0][3].is_equal_approx(a[3]));
  523. REQUIRE_MESSAGE(r[1].size() == 4, "The resulting excluded polygon should have 4 vertices.");
  524. REQUIRE_MESSAGE(Geometry2D::is_polygon_clockwise(r[1]), "The resulting excluded polygon (hole) should be clockwise.");
  525. CHECK(r[1][0].is_equal_approx(Point2(40, 200)));
  526. CHECK(r[1][1].is_equal_approx(Point2(150, 220)));
  527. CHECK(r[1][2].is_equal_approx(Point2(140, 160)));
  528. CHECK(r[1][3].is_equal_approx(Point2(60, 140)));
  529. }
  530. }
  531. TEST_CASE("[Geometry2D] Intersect polyline with polygon") {
  532. Vector<Vector2> l;
  533. Vector<Vector2> p;
  534. Vector<Vector<Point2>> r;
  535. l.push_back(Vector2(100, 90));
  536. l.push_back(Vector2(120, 250));
  537. p.push_back(Vector2(225, 180));
  538. p.push_back(Vector2(160, 230));
  539. p.push_back(Vector2(20, 212));
  540. p.push_back(Vector2(50, 115));
  541. SUBCASE("[Geometry2D] Both line and polygon are empty") {
  542. r = Geometry2D::intersect_polyline_with_polygon(Vector<Vector2>(), Vector<Vector2>());
  543. CHECK_MESSAGE(r.is_empty(), "Both line and polygon are empty. The intersection line should also be empty.");
  544. }
  545. SUBCASE("[Geometry2D] Line is non-empty and polygon is empty") {
  546. r = Geometry2D::intersect_polyline_with_polygon(l, Vector<Vector2>());
  547. CHECK_MESSAGE(r.is_empty(), "The polygon is empty while the line is non-empty. The intersection line should be empty.");
  548. }
  549. SUBCASE("[Geometry2D] Basic intersection with 1 resulting intersection line") {
  550. r = Geometry2D::intersect_polyline_with_polygon(l, p);
  551. REQUIRE_MESSAGE(r.size() == 1, "There should be 1 resulting intersection line.");
  552. REQUIRE_MESSAGE(r[0].size() == 2, "The resulting intersection line should have 2 vertices.");
  553. CHECK(r[0][0].is_equal_approx(Vector2(105.711609, 135.692886)));
  554. CHECK(r[0][1].is_equal_approx(Vector2(116.805809, 224.446457)));
  555. }
  556. SUBCASE("[Geometry2D] Complex intersection with 2 resulting intersection lines") {
  557. l.clear();
  558. l.push_back(Vector2(100, 90));
  559. l.push_back(Vector2(190, 255));
  560. l.push_back(Vector2(135, 260));
  561. l.push_back(Vector2(57, 200));
  562. l.push_back(Vector2(50, 170));
  563. l.push_back(Vector2(15, 155));
  564. r = Geometry2D::intersect_polyline_with_polygon(l, p);
  565. REQUIRE_MESSAGE(r.size() == 2, "There should be 2 resulting intersection lines.");
  566. REQUIRE_MESSAGE(r[0].size() == 2, "The resulting intersection line should have 2 vertices.");
  567. CHECK(r[0][0].is_equal_approx(Vector2(129.804565, 144.641693)));
  568. CHECK(r[0][1].is_equal_approx(Vector2(171.527084, 221.132996)));
  569. REQUIRE_MESSAGE(r[1].size() == 4, "The resulting intersection line should have 4 vertices.");
  570. CHECK(r[1][0].is_equal_approx(Vector2(83.15609, 220.120087)));
  571. CHECK(r[1][1].is_equal_approx(Vector2(57, 200)));
  572. CHECK(r[1][2].is_equal_approx(Vector2(50, 170)));
  573. CHECK(r[1][3].is_equal_approx(Vector2(34.980492, 163.563065)));
  574. }
  575. }
  576. TEST_CASE("[Geometry2D] Clip polyline with polygon") {
  577. Vector<Vector2> l;
  578. Vector<Vector2> p;
  579. Vector<Vector<Point2>> r;
  580. l.push_back(Vector2(70, 140));
  581. l.push_back(Vector2(160, 320));
  582. p.push_back(Vector2(225, 180));
  583. p.push_back(Vector2(160, 230));
  584. p.push_back(Vector2(20, 212));
  585. p.push_back(Vector2(50, 115));
  586. SUBCASE("[Geometry2D] Both line and polygon are empty") {
  587. r = Geometry2D::clip_polyline_with_polygon(Vector<Vector2>(), Vector<Vector2>());
  588. CHECK_MESSAGE(r.is_empty(), "Both line and polygon are empty. The clipped line should also be empty.");
  589. }
  590. SUBCASE("[Geometry2D] Polygon is empty and line is non-empty") {
  591. r = Geometry2D::clip_polyline_with_polygon(l, Vector<Vector2>());
  592. REQUIRE_MESSAGE(r.size() == 1, "There should be 1 resulting clipped line.");
  593. REQUIRE_MESSAGE(r[0].size() == 2, "The resulting clipped line should have 2 vertices.");
  594. CHECK(r[0][0].is_equal_approx(l[0]));
  595. CHECK(r[0][1].is_equal_approx(l[1]));
  596. }
  597. SUBCASE("[Geometry2D] Basic clip with 1 resulting clipped line") {
  598. r = Geometry2D::clip_polyline_with_polygon(l, p);
  599. REQUIRE_MESSAGE(r.size() == 1, "There should be 1 resulting clipped line.");
  600. REQUIRE_MESSAGE(r[0].size() == 2, "The resulting clipped line should have 2 vertices.");
  601. CHECK(r[0][0].is_equal_approx(Vector2(111.908401, 223.816803)));
  602. CHECK(r[0][1].is_equal_approx(Vector2(160, 320)));
  603. }
  604. SUBCASE("[Geometry2D] Complex clip with 2 resulting clipped lines") {
  605. l.clear();
  606. l.push_back(Vector2(55, 70));
  607. l.push_back(Vector2(50, 190));
  608. l.push_back(Vector2(120, 165));
  609. l.push_back(Vector2(122, 250));
  610. l.push_back(Vector2(160, 320));
  611. r = Geometry2D::clip_polyline_with_polygon(l, p);
  612. REQUIRE_MESSAGE(r.size() == 2, "There should be 2 resulting clipped lines.");
  613. REQUIRE_MESSAGE(r[0].size() == 3, "The resulting clipped line should have 3 vertices.");
  614. CHECK(r[0][0].is_equal_approx(Vector2(121.412682, 225.038757)));
  615. CHECK(r[0][1].is_equal_approx(Vector2(122, 250)));
  616. CHECK(r[0][2].is_equal_approx(Vector2(160, 320)));
  617. REQUIRE_MESSAGE(r[1].size() == 2, "The resulting clipped line should have 2 vertices.");
  618. CHECK(r[1][0].is_equal_approx(Vector2(55, 70)));
  619. CHECK(r[1][1].is_equal_approx(Vector2(53.07737, 116.143021)));
  620. }
  621. }
  622. TEST_CASE("[Geometry2D] Convex hull") {
  623. Vector<Point2> a;
  624. Vector<Point2> r;
  625. a.push_back(Point2(-4, -8));
  626. a.push_back(Point2(-10, -4));
  627. a.push_back(Point2(8, 2));
  628. a.push_back(Point2(-6, 10));
  629. a.push_back(Point2(-12, 4));
  630. a.push_back(Point2(10, -8));
  631. a.push_back(Point2(4, 8));
  632. SUBCASE("[Geometry2D] No points") {
  633. r = Geometry2D::convex_hull(Vector<Vector2>());
  634. CHECK_MESSAGE(r.is_empty(), "The convex hull should be empty if there are no input points.");
  635. }
  636. SUBCASE("[Geometry2D] Single point") {
  637. Vector<Point2> b;
  638. b.push_back(Point2(4, -3));
  639. r = Geometry2D::convex_hull(b);
  640. REQUIRE_MESSAGE(r.size() == 1, "Convex hull should contain 1 point.");
  641. CHECK(r[0].is_equal_approx(b[0]));
  642. }
  643. SUBCASE("[Geometry2D] All points form the convex hull") {
  644. r = Geometry2D::convex_hull(a);
  645. REQUIRE_MESSAGE(r.size() == 8, "Convex hull should contain 8 points.");
  646. CHECK(r[0].is_equal_approx(Point2(-12, 4)));
  647. CHECK(r[1].is_equal_approx(Point2(-10, -4)));
  648. CHECK(r[2].is_equal_approx(Point2(-4, -8)));
  649. CHECK(r[3].is_equal_approx(Point2(10, -8)));
  650. CHECK(r[4].is_equal_approx(Point2(8, 2)));
  651. CHECK(r[5].is_equal_approx(Point2(4, 8)));
  652. CHECK(r[6].is_equal_approx(Point2(-6, 10)));
  653. CHECK(r[7].is_equal_approx(Point2(-12, 4)));
  654. }
  655. SUBCASE("[Geometry2D] Add extra points inside original convex hull") {
  656. a.push_back(Point2(-4, -8));
  657. a.push_back(Point2(0, 0));
  658. a.push_back(Point2(0, 8));
  659. a.push_back(Point2(-10, -3));
  660. a.push_back(Point2(9, -4));
  661. a.push_back(Point2(6, 4));
  662. r = Geometry2D::convex_hull(a);
  663. REQUIRE_MESSAGE(r.size() == 8, "Convex hull should contain 8 points.");
  664. CHECK(r[0].is_equal_approx(Point2(-12, 4)));
  665. CHECK(r[1].is_equal_approx(Point2(-10, -4)));
  666. CHECK(r[2].is_equal_approx(Point2(-4, -8)));
  667. CHECK(r[3].is_equal_approx(Point2(10, -8)));
  668. CHECK(r[4].is_equal_approx(Point2(8, 2)));
  669. CHECK(r[5].is_equal_approx(Point2(4, 8)));
  670. CHECK(r[6].is_equal_approx(Point2(-6, 10)));
  671. CHECK(r[7].is_equal_approx(Point2(-12, 4)));
  672. }
  673. SUBCASE("[Geometry2D] Add extra points on border of original convex hull") {
  674. a.push_back(Point2(9, -3));
  675. a.push_back(Point2(-2, -8));
  676. r = Geometry2D::convex_hull(a);
  677. REQUIRE_MESSAGE(r.size() == 8, "Convex hull should contain 8 points.");
  678. CHECK(r[0].is_equal_approx(Point2(-12, 4)));
  679. CHECK(r[1].is_equal_approx(Point2(-10, -4)));
  680. CHECK(r[2].is_equal_approx(Point2(-4, -8)));
  681. CHECK(r[3].is_equal_approx(Point2(10, -8)));
  682. CHECK(r[4].is_equal_approx(Point2(8, 2)));
  683. CHECK(r[5].is_equal_approx(Point2(4, 8)));
  684. CHECK(r[6].is_equal_approx(Point2(-6, 10)));
  685. CHECK(r[7].is_equal_approx(Point2(-12, 4)));
  686. }
  687. SUBCASE("[Geometry2D] Add extra points outside border of original convex hull") {
  688. a.push_back(Point2(-11, -1));
  689. a.push_back(Point2(7, 6));
  690. r = Geometry2D::convex_hull(a);
  691. REQUIRE_MESSAGE(r.size() == 10, "Convex hull should contain 10 points.");
  692. CHECK(r[0].is_equal_approx(Point2(-12, 4)));
  693. CHECK(r[1].is_equal_approx(Point2(-11, -1)));
  694. CHECK(r[2].is_equal_approx(Point2(-10, -4)));
  695. CHECK(r[3].is_equal_approx(Point2(-4, -8)));
  696. CHECK(r[4].is_equal_approx(Point2(10, -8)));
  697. CHECK(r[5].is_equal_approx(Point2(8, 2)));
  698. CHECK(r[6].is_equal_approx(Point2(7, 6)));
  699. CHECK(r[7].is_equal_approx(Point2(4, 8)));
  700. CHECK(r[8].is_equal_approx(Point2(-6, 10)));
  701. CHECK(r[9].is_equal_approx(Point2(-12, 4)));
  702. }
  703. }
  704. TEST_CASE("[Geometry2D] Bresenham line") {
  705. Vector<Vector2i> r;
  706. SUBCASE("[Geometry2D] Single point") {
  707. r = Geometry2D::bresenham_line(Point2i(0, 0), Point2i(0, 0));
  708. REQUIRE_MESSAGE(r.size() == 1, "The Bresenham line should contain exactly one point.");
  709. CHECK(r[0] == Vector2i(0, 0));
  710. }
  711. SUBCASE("[Geometry2D] Line parallel to x-axis") {
  712. r = Geometry2D::bresenham_line(Point2i(1, 2), Point2i(5, 2));
  713. REQUIRE_MESSAGE(r.size() == 5, "The Bresenham line should contain exactly five points.");
  714. CHECK(r[0] == Vector2i(1, 2));
  715. CHECK(r[1] == Vector2i(2, 2));
  716. CHECK(r[2] == Vector2i(3, 2));
  717. CHECK(r[3] == Vector2i(4, 2));
  718. CHECK(r[4] == Vector2i(5, 2));
  719. }
  720. SUBCASE("[Geometry2D] 45 degree line from the origin") {
  721. r = Geometry2D::bresenham_line(Point2i(0, 0), Point2i(4, 4));
  722. REQUIRE_MESSAGE(r.size() == 5, "The Bresenham line should contain exactly five points.");
  723. CHECK(r[0] == Vector2i(0, 0));
  724. CHECK(r[1] == Vector2i(1, 1));
  725. CHECK(r[2] == Vector2i(2, 2));
  726. CHECK(r[3] == Vector2i(3, 3));
  727. CHECK(r[4] == Vector2i(4, 4));
  728. }
  729. SUBCASE("[Geometry2D] Sloped line going up one unit") {
  730. r = Geometry2D::bresenham_line(Point2i(0, 0), Point2i(4, 1));
  731. REQUIRE_MESSAGE(r.size() == 5, "The Bresenham line should contain exactly five points.");
  732. CHECK(r[0] == Vector2i(0, 0));
  733. CHECK(r[1] == Vector2i(1, 0));
  734. CHECK(r[2] == Vector2i(2, 0));
  735. CHECK(r[3] == Vector2i(3, 1));
  736. CHECK(r[4] == Vector2i(4, 1));
  737. }
  738. SUBCASE("[Geometry2D] Sloped line going up two units") {
  739. r = Geometry2D::bresenham_line(Point2i(0, 0), Point2i(4, 2));
  740. REQUIRE_MESSAGE(r.size() == 5, "The Bresenham line should contain exactly five points.");
  741. CHECK(r[0] == Vector2i(0, 0));
  742. CHECK(r[1] == Vector2i(1, 0));
  743. CHECK(r[2] == Vector2i(2, 1));
  744. CHECK(r[3] == Vector2i(3, 1));
  745. CHECK(r[4] == Vector2i(4, 2));
  746. }
  747. SUBCASE("[Geometry2D] Long sloped line") {
  748. r = Geometry2D::bresenham_line(Point2i(0, 0), Point2i(11, 5));
  749. REQUIRE_MESSAGE(r.size() == 12, "The Bresenham line should contain exactly twelve points.");
  750. CHECK(r[0] == Vector2i(0, 0));
  751. CHECK(r[1] == Vector2i(1, 0));
  752. CHECK(r[2] == Vector2i(2, 1));
  753. CHECK(r[3] == Vector2i(3, 1));
  754. CHECK(r[4] == Vector2i(4, 2));
  755. CHECK(r[5] == Vector2i(5, 2));
  756. CHECK(r[6] == Vector2i(6, 3));
  757. CHECK(r[7] == Vector2i(7, 3));
  758. CHECK(r[8] == Vector2i(8, 4));
  759. CHECK(r[9] == Vector2i(9, 4));
  760. CHECK(r[10] == Vector2i(10, 5));
  761. CHECK(r[11] == Vector2i(11, 5));
  762. }
  763. }
  764. } // namespace TestGeometry2D
  765. #endif // TEST_GEOMETRY_2D_H