DotRecastNetSim/src/DotRecast.Detour/ConvexConvexIntersection.cs

311 lines
9.4 KiB
C#
Raw Normal View History

2023-03-14 08:02:43 +03:00
/*
recast4j copyright (c) 2021 Piotr Piastucki piotr@jtilia.org
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
using System;
2023-03-28 18:03:33 +03:00
using DotRecast.Core;
2023-03-14 08:02:43 +03:00
2023-03-16 19:09:10 +03:00
namespace DotRecast.Detour
{
2023-05-10 16:44:51 +03:00
using static DotRecast.Core.RcMath;
2023-03-16 19:09:10 +03:00
2023-03-16 19:48:49 +03:00
/**
2023-03-14 08:02:43 +03:00
* Convex-convex intersection based on "Computational Geometry in C" by Joseph O'Rourke
*/
2023-03-16 19:48:49 +03:00
public static class ConvexConvexIntersection
{
private static readonly float EPSILON = 0.0001f;
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
public static float[] Intersect(float[] p, float[] q)
2023-03-16 19:48:49 +03:00
{
int n = p.Length / 3;
int m = q.Length / 3;
float[] inters = new float[Math.Max(m, n) * 3 * 3];
int ii = 0;
/* Initialize variables. */
2023-03-28 18:03:33 +03:00
Vector3f a = new Vector3f();
Vector3f b = new Vector3f();
Vector3f a1 = new Vector3f();
Vector3f b1 = new Vector3f();
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
int aa = 0;
int ba = 0;
int ai = 0;
int bi = 0;
InFlag f = InFlag.Unknown;
bool FirstPoint = true;
2023-03-28 18:03:33 +03:00
Vector3f ip = new Vector3f();
Vector3f iq = new Vector3f();
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
do
{
2023-05-20 06:33:51 +03:00
a.Set(p, 3 * (ai % n));
b.Set(q, 3 * (bi % m));
a1.Set(p, 3 * ((ai + n - 1) % n)); // prev a
b1.Set(q, 3 * ((bi + m - 1) % m)); // prev b
2023-03-16 19:48:49 +03:00
2023-05-14 10:57:57 +03:00
Vector3f A = a.Subtract(a1);
Vector3f B = b.Subtract(b1);
2023-03-16 19:48:49 +03:00
2023-05-05 02:44:48 +03:00
float cross = B.x * A.z - A.x * B.z; // TriArea2D({0, 0}, A, B);
2023-06-01 18:13:25 +03:00
float aHB = DetourCommon.TriArea2D(b1, b, a);
float bHA = DetourCommon.TriArea2D(a1, a, b);
2023-03-16 19:48:49 +03:00
if (Math.Abs(cross) < EPSILON)
{
cross = 0f;
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
bool parallel = cross == 0f;
2023-05-05 02:44:48 +03:00
Intersection code = parallel ? ParallelInt(a1, a, b1, b, ref ip, ref iq) : SegSegInt(a1, a, b1, b, ref ip, ref iq);
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
if (code == Intersection.Single)
{
if (FirstPoint)
{
FirstPoint = false;
aa = ba = 0;
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
ii = AddVertex(inters, ii, ip);
f = InOut(f, aHB, bHA);
2023-03-16 19:48:49 +03:00
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/*-----Advance rules-----*/
/* Special case: A & B overlap and oppositely oriented. */
2023-05-14 11:09:37 +03:00
if (code == Intersection.Overlap && A.Dot2D(B) < 0)
2023-03-16 19:48:49 +03:00
{
2023-05-05 02:44:48 +03:00
ii = AddVertex(inters, ii, ip);
ii = AddVertex(inters, ii, iq);
2023-03-16 19:48:49 +03:00
break;
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
/* Special case: A & B parallel and separated. */
if (parallel && aHB < 0f && bHA < 0f)
{
return null;
}
/* Special case: A & B collinear. */
else if (parallel && Math.Abs(aHB) < EPSILON && Math.Abs(bHA) < EPSILON)
{
/* Advance but do not output point. */
if (f == InFlag.Pin)
{
ba++;
bi++;
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
else
{
aa++;
ai++;
2023-03-14 08:02:43 +03:00
}
}
2023-03-16 19:48:49 +03:00
/* Generic cases. */
else if (cross >= 0)
{
if (bHA > 0)
{
if (f == InFlag.Pin)
{
2023-05-05 02:44:48 +03:00
ii = AddVertex(inters, ii, a);
2023-03-16 19:48:49 +03:00
}
aa++;
ai++;
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
else
{
if (f == InFlag.Qin)
{
2023-05-05 02:44:48 +03:00
ii = AddVertex(inters, ii, b);
2023-03-16 19:48:49 +03:00
}
ba++;
bi++;
2023-03-14 08:02:43 +03:00
}
}
2023-03-16 19:48:49 +03:00
else
{
if (aHB > 0)
{
if (f == InFlag.Qin)
{
2023-05-05 02:44:48 +03:00
ii = AddVertex(inters, ii, b);
2023-03-16 19:48:49 +03:00
}
ba++;
bi++;
}
else
{
if (f == InFlag.Pin)
{
2023-05-05 02:44:48 +03:00
ii = AddVertex(inters, ii, a);
2023-03-16 19:48:49 +03:00
}
aa++;
ai++;
}
}
/* Quit when both adv. indices have cycled, or one has cycled twice. */
} while ((aa < n || ba < m) && aa < 2 * n && ba < 2 * m);
/* Deal with special cases: not implemented. */
if (f == InFlag.Unknown)
{
return null;
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
float[] copied = new float[ii];
Array.Copy(inters, copied, ii);
return copied;
2023-03-14 08:02:43 +03:00
}
2023-05-05 02:44:48 +03:00
private static int AddVertex(float[] inters, int ii, float[] p)
2023-03-16 19:48:49 +03:00
{
if (ii > 0)
{
if (inters[ii - 3] == p[0] && inters[ii - 2] == p[1] && inters[ii - 1] == p[2])
{
return ii;
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
if (inters[0] == p[0] && inters[1] == p[1] && inters[2] == p[2])
{
return ii;
}
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
inters[ii] = p[0];
inters[ii + 1] = p[1];
inters[ii + 2] = p[2];
return ii + 3;
2023-03-14 08:02:43 +03:00
}
2023-05-07 11:55:13 +03:00
2023-05-05 02:44:48 +03:00
private static int AddVertex(float[] inters, int ii, Vector3f p)
2023-03-28 18:03:33 +03:00
{
if (ii > 0)
{
2023-04-29 06:48:56 +03:00
if (inters[ii - 3] == p.x && inters[ii - 2] == p.y && inters[ii - 1] == p.z)
2023-03-28 18:03:33 +03:00
{
return ii;
}
2023-04-29 06:48:56 +03:00
if (inters[0] == p.x && inters[1] == p.y && inters[2] == p.z)
2023-03-28 18:03:33 +03:00
{
return ii;
}
}
2023-04-29 06:48:56 +03:00
inters[ii] = p.x;
inters[ii + 1] = p.y;
inters[ii + 2] = p.z;
2023-03-28 18:03:33 +03:00
return ii + 3;
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
private static InFlag InOut(InFlag inflag, float aHB, float bHA)
2023-03-16 19:48:49 +03:00
{
if (aHB > 0)
{
return InFlag.Pin;
}
else if (bHA > 0)
{
return InFlag.Qin;
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
return inflag;
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
2023-05-05 02:44:48 +03:00
private static Intersection SegSegInt(Vector3f a, Vector3f b, Vector3f c, Vector3f d, ref Vector3f p, ref Vector3f q)
2023-03-16 19:48:49 +03:00
{
2023-06-01 18:13:25 +03:00
if (DetourCommon.IntersectSegSeg2D(a, b, c, d, out var s, out var t))
2023-03-16 19:48:49 +03:00
{
if (s >= 0.0f && s <= 1.0f && t >= 0.0f && t <= 1.0f)
{
2023-04-29 06:48:56 +03:00
p.x = a.x + (b.x - a.x) * s;
p.y = a.y + (b.y - a.y) * s;
p.z = a.z + (b.z - a.z) * s;
2023-03-16 19:48:49 +03:00
return Intersection.Single;
}
}
return Intersection.None;
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
2023-05-05 02:44:48 +03:00
private static Intersection ParallelInt(Vector3f a, Vector3f b, Vector3f c, Vector3f d, ref Vector3f p, ref Vector3f q)
2023-03-16 19:48:49 +03:00
{
2023-05-05 02:44:48 +03:00
if (Between(a, b, c) && Between(a, b, d))
2023-03-16 19:48:49 +03:00
{
2023-04-12 17:53:28 +03:00
p = c;
q = d;
2023-03-16 19:48:49 +03:00
return Intersection.Overlap;
}
2023-05-05 02:44:48 +03:00
if (Between(c, d, a) && Between(c, d, b))
2023-03-16 19:48:49 +03:00
{
2023-04-12 17:53:28 +03:00
p = a;
q = b;
2023-03-16 19:48:49 +03:00
return Intersection.Overlap;
}
2023-05-05 02:44:48 +03:00
if (Between(a, b, c) && Between(c, d, b))
2023-03-16 19:48:49 +03:00
{
2023-04-12 17:53:28 +03:00
p = c;
q = b;
2023-03-16 19:48:49 +03:00
return Intersection.Overlap;
}
2023-05-05 02:44:48 +03:00
if (Between(a, b, c) && Between(c, d, a))
2023-03-16 19:48:49 +03:00
{
2023-04-12 17:53:28 +03:00
p = c;
q = a;
2023-03-16 19:48:49 +03:00
return Intersection.Overlap;
}
2023-05-05 02:44:48 +03:00
if (Between(a, b, d) && Between(c, d, b))
2023-03-16 19:48:49 +03:00
{
2023-04-12 17:53:28 +03:00
p = d;
q = b;
2023-03-16 19:48:49 +03:00
return Intersection.Overlap;
}
2023-05-05 02:44:48 +03:00
if (Between(a, b, d) && Between(c, d, a))
2023-03-16 19:48:49 +03:00
{
2023-04-12 17:53:28 +03:00
p = d;
q = a;
2023-03-16 19:48:49 +03:00
return Intersection.Overlap;
}
return Intersection.None;
2023-03-14 08:02:43 +03:00
}
2023-05-05 02:44:48 +03:00
private static bool Between(Vector3f a, Vector3f b, Vector3f c)
2023-03-16 19:48:49 +03:00
{
2023-04-29 06:48:56 +03:00
if (Math.Abs(a.x - b.x) > Math.Abs(a.z - b.z))
2023-03-16 19:48:49 +03:00
{
2023-04-29 06:48:56 +03:00
return ((a.x <= c.x) && (c.x <= b.x)) || ((a.x >= c.x) && (c.x >= b.x));
2023-03-16 19:48:49 +03:00
}
else
{
2023-04-29 06:48:56 +03:00
return ((a.z <= c.z) && (c.z <= b.z)) || ((a.z >= c.z) && (c.z >= b.z));
2023-03-16 19:48:49 +03:00
}
2023-03-14 08:02:43 +03:00
}
}
2023-05-07 11:55:13 +03:00
}