From 0614c040bac49fcfce51c87fb1622187e48ea50e Mon Sep 17 00:00:00 2001 From: ikpil Date: Sun, 30 Jul 2023 11:27:10 +0900 Subject: [PATCH] remove nullable float in IntersectSegmentTriangle --- src/DotRecast.Core/Intersections.cs | 17 +++++++++-------- src/DotRecast.Detour/NavMeshRaycast.cs | 3 +-- .../Geom/DemoInputGeomProvider.cs | 7 +++---- src/DotRecast.Recast/PolyMeshRaycast.cs | 6 ++---- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/DotRecast.Core/Intersections.cs b/src/DotRecast.Core/Intersections.cs index 6779bda..62d2d97 100644 --- a/src/DotRecast.Core/Intersections.cs +++ b/src/DotRecast.Core/Intersections.cs @@ -22,8 +22,9 @@ namespace DotRecast.Core { public static class Intersections { - public static float? IntersectSegmentTriangle(RcVec3f sp, RcVec3f sq, RcVec3f a, RcVec3f b, RcVec3f c) + public static bool IntersectSegmentTriangle(RcVec3f sp, RcVec3f sq, RcVec3f a, RcVec3f b, RcVec3f c, out float t) { + t = 0; float v, w; RcVec3f ab = b.Subtract(a); RcVec3f ac = c.Subtract(a); @@ -38,22 +39,22 @@ namespace DotRecast.Core float d = RcVec3f.Dot(qp, norm); if (d <= 0.0f) { - return null; + return false; } // 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 RcVec3f ap = sp.Subtract(a); - float t = RcVec3f.Dot(ap, norm); + t = RcVec3f.Dot(ap, norm); if (t < 0.0f) { - return null; + return false; } if (t > d) { - return null; // For segment; exclude this code line for a ray test + return false; // For segment; exclude this code line for a ray test } // Compute barycentric coordinate components and test if within bounds @@ -61,19 +62,19 @@ namespace DotRecast.Core v = RcVec3f.Dot(ac, e); if (v < 0.0f || v > d) { - return null; + return false; } w = -RcVec3f.Dot(ab, e); if (w < 0.0f || v + w > d) { - return null; + return false; } // Segment/ray intersects triangle. Perform delayed division t /= d; - return t; + return true; } public static bool IsectSegAABB(RcVec3f sp, RcVec3f sq, RcVec3f amin, RcVec3f amax, out float tmin, out float tmax) diff --git a/src/DotRecast.Detour/NavMeshRaycast.cs b/src/DotRecast.Detour/NavMeshRaycast.cs index 0bea893..b6c2f40 100644 --- a/src/DotRecast.Detour/NavMeshRaycast.cs +++ b/src/DotRecast.Detour/NavMeshRaycast.cs @@ -79,8 +79,7 @@ namespace DotRecast.Detour } } - float? intersection = Intersections.IntersectSegmentTriangle(sp, sq, verts[0], verts[1], verts[2]); - if (null != intersection) + if (Intersections.IntersectSegmentTriangle(sp, sq, verts[0], verts[1], verts[2], out var intersection)) { return intersection; } diff --git a/src/DotRecast.Recast.DemoTool/Geom/DemoInputGeomProvider.cs b/src/DotRecast.Recast.DemoTool/Geom/DemoInputGeomProvider.cs index df97561..21292bf 100644 --- a/src/DotRecast.Recast.DemoTool/Geom/DemoInputGeomProvider.cs +++ b/src/DotRecast.Recast.DemoTool/Geom/DemoInputGeomProvider.cs @@ -169,12 +169,11 @@ namespace DotRecast.Recast.DemoTool.Geom vertices[tris[j + 2] * 3 + 1], vertices[tris[j + 2] * 3 + 2] ); - float? t = Intersections.IntersectSegmentTriangle(src, dst, v1, v2, v3); - if (null != t) + if (Intersections.IntersectSegmentTriangle(src, dst, v1, v2, v3, out var t)) { - if (t.Value < tmin) + if (t < tmin) { - tmin = t.Value; + tmin = t; } hit = true; diff --git a/src/DotRecast.Recast/PolyMeshRaycast.cs b/src/DotRecast.Recast/PolyMeshRaycast.cs index eeb43ac..ac7e7b2 100644 --- a/src/DotRecast.Recast/PolyMeshRaycast.cs +++ b/src/DotRecast.Recast/PolyMeshRaycast.cs @@ -21,7 +21,6 @@ using DotRecast.Core; namespace DotRecast.Recast { - public static class PolyMeshRaycast { public static float? Raycast(IList results, RcVec3f src, RcVec3f dst) @@ -63,8 +62,7 @@ namespace DotRecast.Recast vs[k].z = meshDetail.verts[verts + meshDetail.tris[tris + j * 4 + k] * 3 + 2]; } - float? intersection = Intersections.IntersectSegmentTriangle(sp, sq, vs[0], vs[1], vs[2]); - if (null != intersection) + if (Intersections.IntersectSegmentTriangle(sp, sq, vs[0], vs[1], vs[2], out var intersection)) { return intersection; } @@ -79,4 +77,4 @@ namespace DotRecast.Recast return null; } } -} +} \ No newline at end of file