ConvexConvexIntersection.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. recast4j copyright (c) 2021 Piotr Piastucki piotr@jtilia.org
  3. DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. using System;
  19. using DotRecast.Core;
  20. namespace DotRecast.Detour
  21. {
  22. /**
  23. * Convex-convex intersection based on "Computational Geometry in C" by Joseph O'Rourke
  24. */
  25. public static class ConvexConvexIntersection
  26. {
  27. private static readonly float EPSILON = 0.0001f;
  28. public static float[] Intersect(float[] p, float[] q)
  29. {
  30. int n = p.Length / 3;
  31. int m = q.Length / 3;
  32. float[] inters = new float[Math.Max(m, n) * 3 * 3];
  33. int ii = 0;
  34. /* Initialize variables. */
  35. RcVec3f a = new RcVec3f();
  36. RcVec3f b = new RcVec3f();
  37. RcVec3f a1 = new RcVec3f();
  38. RcVec3f b1 = new RcVec3f();
  39. int aa = 0;
  40. int ba = 0;
  41. int ai = 0;
  42. int bi = 0;
  43. InFlag f = InFlag.Unknown;
  44. bool FirstPoint = true;
  45. RcVec3f ip = new RcVec3f();
  46. RcVec3f iq = new RcVec3f();
  47. do
  48. {
  49. a.Set(p, 3 * (ai % n));
  50. b.Set(q, 3 * (bi % m));
  51. a1.Set(p, 3 * ((ai + n - 1) % n)); // prev a
  52. b1.Set(q, 3 * ((bi + m - 1) % m)); // prev b
  53. RcVec3f A = a.Subtract(a1);
  54. RcVec3f B = b.Subtract(b1);
  55. float cross = B.x * A.z - A.x * B.z; // TriArea2D({0, 0}, A, B);
  56. float aHB = DtUtils.TriArea2D(b1, b, a);
  57. float bHA = DtUtils.TriArea2D(a1, a, b);
  58. if (Math.Abs(cross) < EPSILON)
  59. {
  60. cross = 0f;
  61. }
  62. bool parallel = cross == 0f;
  63. Intersection code = parallel ? ParallelInt(a1, a, b1, b, ref ip, ref iq) : SegSegInt(a1, a, b1, b, ref ip, ref iq);
  64. if (code == Intersection.Single)
  65. {
  66. if (FirstPoint)
  67. {
  68. FirstPoint = false;
  69. aa = ba = 0;
  70. }
  71. ii = AddVertex(inters, ii, ip);
  72. f = InOut(f, aHB, bHA);
  73. }
  74. /*-----Advance rules-----*/
  75. /* Special case: A & B overlap and oppositely oriented. */
  76. if (code == Intersection.Overlap && A.Dot2D(B) < 0)
  77. {
  78. ii = AddVertex(inters, ii, ip);
  79. ii = AddVertex(inters, ii, iq);
  80. break;
  81. }
  82. /* Special case: A & B parallel and separated. */
  83. if (parallel && aHB < 0f && bHA < 0f)
  84. {
  85. return null;
  86. }
  87. /* Special case: A & B collinear. */
  88. else if (parallel && Math.Abs(aHB) < EPSILON && Math.Abs(bHA) < EPSILON)
  89. {
  90. /* Advance but do not output point. */
  91. if (f == InFlag.Pin)
  92. {
  93. ba++;
  94. bi++;
  95. }
  96. else
  97. {
  98. aa++;
  99. ai++;
  100. }
  101. }
  102. /* Generic cases. */
  103. else if (cross >= 0)
  104. {
  105. if (bHA > 0)
  106. {
  107. if (f == InFlag.Pin)
  108. {
  109. ii = AddVertex(inters, ii, a);
  110. }
  111. aa++;
  112. ai++;
  113. }
  114. else
  115. {
  116. if (f == InFlag.Qin)
  117. {
  118. ii = AddVertex(inters, ii, b);
  119. }
  120. ba++;
  121. bi++;
  122. }
  123. }
  124. else
  125. {
  126. if (aHB > 0)
  127. {
  128. if (f == InFlag.Qin)
  129. {
  130. ii = AddVertex(inters, ii, b);
  131. }
  132. ba++;
  133. bi++;
  134. }
  135. else
  136. {
  137. if (f == InFlag.Pin)
  138. {
  139. ii = AddVertex(inters, ii, a);
  140. }
  141. aa++;
  142. ai++;
  143. }
  144. }
  145. /* Quit when both adv. indices have cycled, or one has cycled twice. */
  146. } while ((aa < n || ba < m) && aa < 2 * n && ba < 2 * m);
  147. /* Deal with special cases: not implemented. */
  148. if (f == InFlag.Unknown)
  149. {
  150. return null;
  151. }
  152. float[] copied = new float[ii];
  153. Array.Copy(inters, copied, ii);
  154. return copied;
  155. }
  156. private static int AddVertex(float[] inters, int ii, float[] p)
  157. {
  158. if (ii > 0)
  159. {
  160. if (inters[ii - 3] == p[0] && inters[ii - 2] == p[1] && inters[ii - 1] == p[2])
  161. {
  162. return ii;
  163. }
  164. if (inters[0] == p[0] && inters[1] == p[1] && inters[2] == p[2])
  165. {
  166. return ii;
  167. }
  168. }
  169. inters[ii] = p[0];
  170. inters[ii + 1] = p[1];
  171. inters[ii + 2] = p[2];
  172. return ii + 3;
  173. }
  174. private static int AddVertex(float[] inters, int ii, RcVec3f p)
  175. {
  176. if (ii > 0)
  177. {
  178. if (inters[ii - 3] == p.x && inters[ii - 2] == p.y && inters[ii - 1] == p.z)
  179. {
  180. return ii;
  181. }
  182. if (inters[0] == p.x && inters[1] == p.y && inters[2] == p.z)
  183. {
  184. return ii;
  185. }
  186. }
  187. inters[ii] = p.x;
  188. inters[ii + 1] = p.y;
  189. inters[ii + 2] = p.z;
  190. return ii + 3;
  191. }
  192. private static InFlag InOut(InFlag inflag, float aHB, float bHA)
  193. {
  194. if (aHB > 0)
  195. {
  196. return InFlag.Pin;
  197. }
  198. else if (bHA > 0)
  199. {
  200. return InFlag.Qin;
  201. }
  202. return inflag;
  203. }
  204. private static Intersection SegSegInt(RcVec3f a, RcVec3f b, RcVec3f c, RcVec3f d, ref RcVec3f p, ref RcVec3f q)
  205. {
  206. if (DtUtils.IntersectSegSeg2D(a, b, c, d, out var s, out var t))
  207. {
  208. if (s >= 0.0f && s <= 1.0f && t >= 0.0f && t <= 1.0f)
  209. {
  210. p.x = a.x + (b.x - a.x) * s;
  211. p.y = a.y + (b.y - a.y) * s;
  212. p.z = a.z + (b.z - a.z) * s;
  213. return Intersection.Single;
  214. }
  215. }
  216. return Intersection.None;
  217. }
  218. private static Intersection ParallelInt(RcVec3f a, RcVec3f b, RcVec3f c, RcVec3f d, ref RcVec3f p, ref RcVec3f q)
  219. {
  220. if (Between(a, b, c) && Between(a, b, d))
  221. {
  222. p = c;
  223. q = d;
  224. return Intersection.Overlap;
  225. }
  226. if (Between(c, d, a) && Between(c, d, b))
  227. {
  228. p = a;
  229. q = b;
  230. return Intersection.Overlap;
  231. }
  232. if (Between(a, b, c) && Between(c, d, b))
  233. {
  234. p = c;
  235. q = b;
  236. return Intersection.Overlap;
  237. }
  238. if (Between(a, b, c) && Between(c, d, a))
  239. {
  240. p = c;
  241. q = a;
  242. return Intersection.Overlap;
  243. }
  244. if (Between(a, b, d) && Between(c, d, b))
  245. {
  246. p = d;
  247. q = b;
  248. return Intersection.Overlap;
  249. }
  250. if (Between(a, b, d) && Between(c, d, a))
  251. {
  252. p = d;
  253. q = a;
  254. return Intersection.Overlap;
  255. }
  256. return Intersection.None;
  257. }
  258. private static bool Between(RcVec3f a, RcVec3f b, RcVec3f c)
  259. {
  260. if (Math.Abs(a.x - b.x) > Math.Abs(a.z - b.z))
  261. {
  262. return ((a.x <= c.x) && (c.x <= b.x)) || ((a.x >= c.x) && (c.x >= b.x));
  263. }
  264. else
  265. {
  266. return ((a.z <= c.z) && (c.z <= b.z)) || ((a.z >= c.z) && (c.z >= b.z));
  267. }
  268. }
  269. }
  270. }