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;
|
|
|
|
using DotRecast.Core;
|
|
|
|
using static DotRecast.Detour.DetourCommon;
|
|
|
|
|
|
|
|
namespace DotRecast.Recast.Demo.Geom;
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
public class Intersections
|
|
|
|
{
|
|
|
|
public static float? intersectSegmentTriangle(float[] sp, float[] sq, float[] a, float[] b, float[] c)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
float v, w;
|
|
|
|
float[] ab = vSub(b, a);
|
|
|
|
float[] ac = vSub(c, a);
|
|
|
|
float[] qp = vSub(sp, sq);
|
|
|
|
|
|
|
|
// Compute triangle normal. Can be precalculated or cached if
|
|
|
|
// intersecting multiple segments against the same triangle
|
|
|
|
float[] norm = DemoMath.vCross(ab, ac);
|
|
|
|
|
|
|
|
// Compute denominator d. If d <= 0, segment is parallel to or points
|
|
|
|
// away from triangle, so exit early
|
|
|
|
float d = DemoMath.vDot(qp, norm);
|
2023-03-16 19:48:49 +03:00
|
|
|
if (d <= 0.0f)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute intersection t value of pq with plane of triangle. A ray
|
|
|
|
// intersects iff 0 <= t. Segment intersects iff 0 <= t <= 1. Delay
|
|
|
|
// dividing by d until intersection has been found to pierce triangle
|
|
|
|
float[] ap = vSub(sp, a);
|
|
|
|
float t = DemoMath.vDot(ap, norm);
|
2023-03-16 19:48:49 +03:00
|
|
|
if (t < 0.0f)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
return null;
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
if (t > d)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
return null; // For segment; exclude this code line for a ray test
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute barycentric coordinate components and test if within bounds
|
|
|
|
float[] e = DemoMath.vCross(qp, ap);
|
|
|
|
v = DemoMath.vDot(ac, e);
|
2023-03-16 19:48:49 +03:00
|
|
|
if (v < 0.0f || v > d)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
return null;
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
w = -DemoMath.vDot(ab, e);
|
2023-03-16 19:48:49 +03:00
|
|
|
if (w < 0.0f || v + w > d)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Segment/ray intersects triangle. Perform delayed division
|
|
|
|
t /= d;
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
public static float[] intersectSegmentAABB(float[] sp, float[] sq, float[] amin, float[] amax)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
float EPS = 1e-6f;
|
|
|
|
|
|
|
|
float[] d = new float[3];
|
|
|
|
d[0] = sq[0] - sp[0];
|
|
|
|
d[1] = sq[1] - sp[1];
|
|
|
|
d[2] = sq[2] - sp[2];
|
|
|
|
float tmin = 0.0f;
|
|
|
|
float tmax = 1.0f;
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
if (Math.Abs(d[i]) < EPS)
|
|
|
|
{
|
|
|
|
if (sp[i] < amin[i] || sp[i] > amax[i])
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
return null;
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
float ood = 1.0f / d[i];
|
|
|
|
float t1 = (amin[i] - sp[i]) * ood;
|
|
|
|
float t2 = (amax[i] - sp[i]) * ood;
|
2023-03-16 19:48:49 +03:00
|
|
|
if (t1 > t2)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
float tmp = t1;
|
|
|
|
t1 = t2;
|
|
|
|
t2 = tmp;
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
if (t1 > tmin)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
tmin = t1;
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
if (t2 < tmax)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
tmax = t2;
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
if (tmin > tmax)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new float[] { tmin, tmax };
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|