remove interectResult class

This commit is contained in:
ikpil 2023-07-20 23:44:06 +09:00
parent 70dc963563
commit 9a4ed56eed
6 changed files with 41 additions and 47 deletions

View File

@ -21,9 +21,7 @@ namespace DotRecast.Core
{ {
n--; n--;
int k = random.Next(n + 1); int k = random.Next(n + 1);
T value = list[k]; (list[k], list[n]) = (list[n], list[k]);
list[k] = list[n];
list[n] = value;
} }
} }
} }

View File

@ -1,11 +0,0 @@
namespace DotRecast.Core
{
public class IntersectResult
{
public bool intersects;
public float tmin;
public float tmax = 1f;
public int segMin = -1;
public int segMax = -1;
}
}

View File

@ -20,7 +20,7 @@ using System.Collections.Generic;
namespace DotRecast.Core namespace DotRecast.Core
{ {
public static class ConvexUtils public static class RcConvexUtils
{ {
// Calculates convex hull on xz-plane of points on 'pts', // Calculates convex hull on xz-plane of points on 'pts',
// stores the indices of the resulting hull in 'out' and // stores the indices of the resulting hull in 'out' and

View File

@ -339,10 +339,18 @@ namespace DotRecast.Detour
return dx * dx + dz * dz; return dx * dx + dz * dz;
} }
public static IntersectResult IntersectSegmentPoly2D(RcVec3f p0, RcVec3f p1, RcVec3f[] verts, int nverts) public static bool IntersectSegmentPoly2D(RcVec3f p0, RcVec3f p1,
RcVec3f[] verts, int nverts,
out float tmin, out float tmax,
out int segMin, out int segMax)
{ {
IntersectResult result = new IntersectResult();
const float EPS = 0.000001f; const float EPS = 0.000001f;
tmin = 0;
tmax = 1;
segMin = -1;
segMax = -1;
var dir = p1.Subtract(p0); var dir = p1.Subtract(p0);
var p0v = p0; var p0v = p0;
@ -359,7 +367,7 @@ namespace DotRecast.Detour
// S is nearly parallel to this edge // S is nearly parallel to this edge
if (n < 0) if (n < 0)
{ {
return result; return false;
} }
else else
{ {
@ -371,35 +379,34 @@ namespace DotRecast.Detour
if (d < 0) if (d < 0)
{ {
// segment S is entering across this edge // segment S is entering across this edge
if (t > result.tmin) if (t > tmin)
{ {
result.tmin = t; tmin = t;
result.segMin = j; segMin = j;
// S enters after leaving polygon // S enters after leaving polygon
if (result.tmin > result.tmax) if (tmin > tmax)
{ {
return result; return false;
} }
} }
} }
else else
{ {
// segment S is leaving across this edge // segment S is leaving across this edge
if (t < result.tmax) if (t < tmax)
{ {
result.tmax = t; tmax = t;
result.segMax = j; segMax = j;
// S leaves before entering polygon // S leaves before entering polygon
if (result.tmax < result.tmin) if (tmax < tmin)
{ {
return result; return false;
} }
} }
} }
} }
result.intersects = true; return true;
return result;
} }
public static int OppositeTile(int side) public static int OppositeTile(int side)

View File

@ -2233,26 +2233,26 @@ namespace DotRecast.Detour
nv++; nv++;
} }
IntersectResult iresult = DetourCommon.IntersectSegmentPoly2D(startPos, endPos, verts, nv); bool intersects = DetourCommon.IntersectSegmentPoly2D(startPos, endPos, verts, nv, out var tmin, out var tmax, out var segMin, out var segMax);
if (!iresult.intersects) if (!intersects)
{ {
// Could not hit the polygon, keep the old t and report hit. // Could not hit the polygon, keep the old t and report hit.
return DtStatus.DT_SUCCSESS; return DtStatus.DT_SUCCSESS;
} }
hit.hitEdgeIndex = iresult.segMax; hit.hitEdgeIndex = segMax;
// Keep track of furthest t so far. // Keep track of furthest t so far.
if (iresult.tmax > hit.t) if (tmax > hit.t)
{ {
hit.t = iresult.tmax; hit.t = tmax;
} }
// Store visited polygons. // Store visited polygons.
hit.path.Add(curRef); hit.path.Add(curRef);
// Ray end is completely inside the polygon. // Ray end is completely inside the polygon.
if (iresult.segMax == -1) if (segMax == -1)
{ {
hit.t = float.MaxValue; hit.t = float.MaxValue;
@ -2274,7 +2274,7 @@ namespace DotRecast.Detour
DtLink link = tile.links[i]; DtLink link = tile.links[i];
// Find link which contains this edge. // Find link which contains this edge.
if (link.edge != iresult.segMax) if (link.edge != segMax)
{ {
continue; continue;
} }
@ -2331,7 +2331,7 @@ namespace DotRecast.Detour
} }
// Find Z intersection. // Find Z intersection.
float z = startPos.z + (endPos.z - startPos.z) * iresult.tmax; float z = startPos.z + (endPos.z - startPos.z) * tmax;
if (z >= lmin && z <= lmax) if (z >= lmin && z <= lmax)
{ {
nextRef = link.refs; nextRef = link.refs;
@ -2352,7 +2352,7 @@ namespace DotRecast.Detour
} }
// Find X intersection. // Find X intersection.
float x = startPos.x + (endPos.x - startPos.x) * iresult.tmax; float x = startPos.x + (endPos.x - startPos.x) * tmax;
if (x >= lmin && x <= lmax) if (x >= lmin && x <= lmax)
{ {
nextRef = link.refs; nextRef = link.refs;
@ -2368,8 +2368,8 @@ namespace DotRecast.Detour
// and correct the height (since the raycast moves in 2d) // and correct the height (since the raycast moves in 2d)
lastPos = curPos; lastPos = curPos;
curPos = RcVec3f.Mad(startPos, dir, hit.t); curPos = RcVec3f.Mad(startPos, dir, hit.t);
var e1 = verts[iresult.segMax]; var e1 = verts[segMax];
var e2 = verts[(iresult.segMax + 1) % nv]; var e2 = verts[(segMax + 1) % nv];
var eDir = e2.Subtract(e1); var eDir = e2.Subtract(e1);
var diff = curPos.Subtract(e1); var diff = curPos.Subtract(e1);
float s = Sqr(eDir.x) > Sqr(eDir.z) ? diff.x / eDir.x : diff.z / eDir.z; float s = Sqr(eDir.x) > Sqr(eDir.z) ? diff.x / eDir.x : diff.z / eDir.z;
@ -2384,8 +2384,8 @@ namespace DotRecast.Detour
// No neighbour, we hit a wall. // No neighbour, we hit a wall.
// Calculate hit normal. // Calculate hit normal.
int a = iresult.segMax; int a = segMax;
int b = iresult.segMax + 1 < nv ? iresult.segMax + 1 : 0; int b = segMax + 1 < nv ? segMax + 1 : 0;
// int va = a * 3; // int va = a * 3;
// int vb = b * 3; // int vb = b * 3;
float dx = verts[b].x - verts[a].x; float dx = verts[b].x - verts[a].x;
@ -2718,13 +2718,13 @@ namespace DotRecast.Detour
} }
// If the poly is not touching the edge to the next polygon, skip the connection it. // If the poly is not touching the edge to the next polygon, skip the connection it.
IntersectResult ir = DetourCommon.IntersectSegmentPoly2D(va, vb, verts, nverts); bool intersects = DetourCommon.IntersectSegmentPoly2D(va, vb, verts, nverts, out var tmin, out var tmax, out var segMin, out var segMax);
if (!ir.intersects) if (!intersects)
{ {
continue; continue;
} }
if (ir.tmin > 1.0f || ir.tmax < 0.0f) if (tmin > 1.0f || tmax < 0.0f)
{ {
continue; continue;
} }

View File

@ -94,7 +94,7 @@ public class ConvexVolumeTool : IRcTool
if (pts.Count > 3) if (pts.Count > 3)
{ {
hull.Clear(); hull.Clear();
hull.AddRange(ConvexUtils.Convexhull(pts)); hull.AddRange(RcConvexUtils.Convexhull(pts));
} }
else else
{ {