GeometryUtil.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "EditorDefs.h"
  9. #include "GeometryUtil.h"
  10. #if 0
  11. struct SPointSorter
  12. {
  13. SPointSorter(const Vec3& pt)
  14. : pt(pt) {}
  15. bool operator()(const Vec3& lhs, const Vec3& rhs)
  16. {
  17. float isLeft = IsLeft(pt, lhs, rhs);
  18. if (isLeft > 0.0f)
  19. {
  20. return true;
  21. }
  22. else if (isLeft < 0.0f)
  23. {
  24. return false;
  25. }
  26. else
  27. {
  28. return (lhs - pt).GetLengthSquared2D() < (rhs - pt).GetLengthSquared2D();
  29. }
  30. }
  31. const Vec3 pt;
  32. };
  33. //===================================================================
  34. // ConvexHull2D
  35. // Implements Graham's scan
  36. //===================================================================
  37. void ConvexHull2DGraham(std::vector<Vec3>& ptsOut, const std::vector<Vec3>& ptsIn)
  38. {
  39. const unsigned nPtsIn = ptsIn.size();
  40. if (nPtsIn < 3)
  41. {
  42. ptsOut = ptsIn;
  43. return;
  44. }
  45. unsigned iBotRight = 0;
  46. for (unsigned iPt = 1; iPt < nPtsIn; ++iPt)
  47. {
  48. if (ptsIn[iPt].y < ptsIn[iBotRight].y)
  49. {
  50. iBotRight = iPt;
  51. }
  52. else if (ptsIn[iPt].y == ptsIn[iBotRight].y && ptsIn[iPt].x < ptsIn[iBotRight].x)
  53. {
  54. iBotRight = iPt;
  55. }
  56. }
  57. static std::vector<Vec3> ptsSorted; // avoid memory allocation
  58. ptsSorted.assign(ptsIn.begin(), ptsIn.end());
  59. std::swap(ptsSorted[0], ptsSorted[iBotRight]);
  60. {
  61. std::sort(ptsSorted.begin() + 1, ptsSorted.end(), SPointSorter(ptsSorted[0]));
  62. }
  63. ptsSorted.erase(std::unique(ptsSorted.begin(), ptsSorted.end(), ptEqual), ptsSorted.end());
  64. const unsigned nPtsSorted = ptsSorted.size();
  65. if (nPtsSorted < 3)
  66. {
  67. ptsOut = ptsSorted;
  68. return;
  69. }
  70. ptsOut.resize(0);
  71. ptsOut.push_back(ptsSorted[0]);
  72. ptsOut.push_back(ptsSorted[1]);
  73. unsigned int i = 2;
  74. while (i < nPtsSorted)
  75. {
  76. if (ptsOut.size() <= 1)
  77. {
  78. AIWarning("Badness in ConvexHull2D");
  79. AILogComment("i = %d ptsIn = ", i);
  80. for (unsigned j = 0; j < ptsIn.size(); ++j)
  81. {
  82. AILogComment("%6.3f, %6.3f, %6.3f", ptsIn[j].x, ptsIn[j].y, ptsIn[j].z);
  83. }
  84. AILogComment("ptsSorted = ");
  85. for (unsigned j = 0; j < ptsSorted.size(); ++j)
  86. {
  87. AILogComment("%6.3f, %6.3f, %6.3f", ptsSorted[j].x, ptsSorted[j].y, ptsSorted[j].z);
  88. }
  89. ptsOut.resize(0);
  90. return;
  91. }
  92. const Vec3& pt1 = ptsOut[ptsOut.size() - 1];
  93. const Vec3& pt2 = ptsOut[ptsOut.size() - 2];
  94. const Vec3& p = ptsSorted[i];
  95. float isLeft = IsLeft(pt2, pt1, p);
  96. if (isLeft > 0.0f)
  97. {
  98. ptsOut.push_back(p);
  99. ++i;
  100. }
  101. else if (isLeft < 0.0f)
  102. {
  103. ptsOut.pop_back();
  104. }
  105. else
  106. {
  107. ptsOut.pop_back();
  108. ptsOut.push_back(p);
  109. ++i;
  110. }
  111. }
  112. }
  113. #endif
  114. //===================================================================
  115. // IsLeft: tests if a point is Left|On|Right of an infinite line.
  116. // Input: three points P0, P1, and P2
  117. // Return: >0 for P2 left of the line through P0 and P1
  118. // =0 for P2 on the line
  119. // <0 for P2 right of the line
  120. //===================================================================
  121. inline float IsLeft(Vec3 P0, Vec3 P1, const Vec3& P2)
  122. {
  123. bool swap = false;
  124. if (P0.x < P1.x)
  125. {
  126. swap = true;
  127. }
  128. else if (P0.x == P1.x && P0.y < P1.y)
  129. {
  130. swap = true;
  131. }
  132. if (swap)
  133. {
  134. std::swap(P0, P1);
  135. }
  136. float res = (P1.x - P0.x) * (P2.y - P0.y) - (P2.x - P0.x) * (P1.y - P0.y);
  137. const float tol = 0.0000f;
  138. if (res > tol || res < -tol)
  139. {
  140. return swap ? -res : res;
  141. }
  142. else
  143. {
  144. return 0.0f;
  145. }
  146. }
  147. inline bool ptEqual(const Vec3& lhs, const Vec3& rhs)
  148. {
  149. const float tol = 0.01f;
  150. return (fabs(lhs.x - rhs.x) < tol) && (fabs(lhs.y - rhs.y) < tol);
  151. }
  152. inline float IsLeftAndrew(const Vec3& p0, const Vec3& p1, const Vec3& p2)
  153. {
  154. return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
  155. }
  156. inline bool PointSorterAndrew(const Vec3& lhs, const Vec3& rhs)
  157. {
  158. if (lhs.x < rhs.x)
  159. {
  160. return true;
  161. }
  162. if (lhs.x > rhs.x)
  163. {
  164. return false;
  165. }
  166. return lhs.y < rhs.y;
  167. }
  168. //===================================================================
  169. // ConvexHull2D
  170. // Implements Andrew's algorithm
  171. //
  172. // Copyright 2001, softSurfer (www.softsurfer.com)
  173. // This code may be freely used and modified for any purpose
  174. // providing that this copyright notice is included with it.
  175. // SoftSurfer makes no warranty for this code, and cannot be held
  176. // liable for any real or imagined damage resulting from its use.
  177. // Users of this code must verify correctness for their application.
  178. //===================================================================
  179. SANDBOX_API void ConvexHull2DAndrew(std::vector<Vec3>& ptsOut, const std::vector<Vec3>& ptsIn)
  180. {
  181. const int n = (int)ptsIn.size();
  182. if (n < 3)
  183. {
  184. ptsOut = ptsIn;
  185. return;
  186. }
  187. std::vector<Vec3> P = ptsIn;
  188. {
  189. std::sort(P.begin(), P.end(), PointSorterAndrew);
  190. }
  191. // the output array ptsOut[] will be used as the stack
  192. int i;
  193. ptsOut.clear();
  194. ptsOut.reserve(P.size());
  195. // Get the indices of points with min x-coord and min|max y-coord
  196. int minmin = 0, minmax;
  197. float xmin = P[0].x;
  198. for (i = 1; i < n; i++)
  199. {
  200. if (P[i].x != xmin)
  201. {
  202. break;
  203. }
  204. }
  205. minmax = i - 1;
  206. if (minmax == n - 1)
  207. {
  208. // degenerate case: all x-coords == xmin
  209. ptsOut.push_back(P[minmin]);
  210. if (P[minmax].y != P[minmin].y) // a nontrivial segment
  211. {
  212. ptsOut.push_back(P[minmax]);
  213. }
  214. ptsOut.push_back(P[minmin]); // add polygon endpoint
  215. return;
  216. }
  217. // Get the indices of points with max x-coord and min|max y-coord
  218. int maxmin, maxmax = n - 1;
  219. float xmax = P[n - 1].x;
  220. for (i = n - 2; i >= 0; i--)
  221. {
  222. if (P[i].x != xmax)
  223. {
  224. break;
  225. }
  226. }
  227. maxmin = i + 1;
  228. // Compute the lower hull on the stack H
  229. ptsOut.push_back(P[minmin]); // push minmin point onto stack
  230. i = minmax;
  231. while (++i <= maxmin)
  232. {
  233. // the lower line joins P[minmin] with P[maxmin]
  234. if (IsLeftAndrew(P[minmin], P[maxmin], P[i]) >= 0 && i < maxmin)
  235. {
  236. continue; // ignore P[i] above or on the lower line
  237. }
  238. while ((int)ptsOut.size() > 1) // there are at least 2 points on the stack
  239. {
  240. // test if P[i] is left of the line at the stack top
  241. if (IsLeftAndrew(ptsOut[ptsOut.size() - 2], ptsOut.back(), P[i]) > 0)
  242. {
  243. break; // P[i] is a new hull vertex
  244. }
  245. else
  246. {
  247. ptsOut.pop_back(); // pop top point off stack
  248. }
  249. }
  250. ptsOut.push_back(P[i]); // push P[i] onto stack
  251. }
  252. // Next, compute the upper hull on the stack H above the bottom hull
  253. if (maxmax != maxmin) // if distinct xmax points
  254. {
  255. ptsOut.push_back(P[maxmax]); // push maxmax point onto stack
  256. }
  257. int bot = (int)ptsOut.size() - 1; // the bottom point of the upper hull stack
  258. i = maxmin;
  259. while (--i >= minmax)
  260. {
  261. // the upper line joins P[maxmax] with P[minmax]
  262. if (IsLeftAndrew(P[maxmax], P[minmax], P[i]) >= 0 && i > minmax)
  263. {
  264. continue; // ignore P[i] below or on the upper line
  265. }
  266. while ((int)ptsOut.size() > bot + 1) // at least 2 points on the upper stack
  267. {
  268. // test if P[i] is left of the line at the stack top
  269. if (IsLeftAndrew(ptsOut[ptsOut.size() - 2], ptsOut.back(), P[i]) > 0)
  270. {
  271. break; // P[i] is a new hull vertex
  272. }
  273. else
  274. {
  275. ptsOut.pop_back(); // pop top po2int off stack
  276. }
  277. }
  278. ptsOut.push_back(P[i]); // push P[i] onto stack
  279. }
  280. if (minmax != minmin)
  281. {
  282. ptsOut.push_back(P[minmin]); // push joining endpoint onto stack
  283. }
  284. if (!ptsOut.empty() && ptEqual(ptsOut.front(), ptsOut.back()))
  285. {
  286. ptsOut.pop_back();
  287. }
  288. }