From 63427dce0aacfbd4ccb426ae65d70fc83a358d1a Mon Sep 17 00:00:00 2001 From: ikpil Date: Wed, 17 May 2023 14:30:41 +0900 Subject: [PATCH] =?UTF-8?q?[=EA=B0=9C=EB=B0=9C/=EC=B5=9C=EC=9D=B5=ED=95=84?= =?UTF-8?q?]=20VLerp=20->=20Vector3f.Lerp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/DotRecast.Core/RcMath.cs | 10 +------ src/DotRecast.Core/Vector3f.cs | 26 +++++++++++++------ src/DotRecast.Detour.Crowd/Crowd.cs | 4 +-- .../Jumplink/AbstractGroundSampler.cs | 2 +- src/DotRecast.Detour/NavMesh.cs | 20 +++++++------- src/DotRecast.Detour/NavMeshQuery.cs | 12 ++++----- .../Tools/JumpLinkBuilderTool.cs | 8 +++--- .../Tools/TestNavmeshTool.cs | 4 +-- 8 files changed, 44 insertions(+), 42 deletions(-) diff --git a/src/DotRecast.Core/RcMath.cs b/src/DotRecast.Core/RcMath.cs index 63f1370..831c458 100644 --- a/src/DotRecast.Core/RcMath.cs +++ b/src/DotRecast.Core/RcMath.cs @@ -125,14 +125,6 @@ namespace DotRecast.Core ); } - public static Vector3f VLerp(Vector3f v1, Vector3f v2, float t) - { - return new Vector3f( - v1.x + (v2.x - v1.x) * t, - v1.y + (v2.y - v1.y) * t, - v1.z + (v2.z - v1.z) * t - ); - } public static Vector3f VAdd(Vector3f v1, Vector3f v2) { @@ -323,7 +315,7 @@ namespace DotRecast.Core return d < thresholdSqr; } - + /// Derives the xz-plane 2D perp product of the two vectors. (uz*vx - ux*vz) /// @param[in] u The LHV vector [(x, y, z)] /// @param[in] v The RHV vector [(x, y, z)] diff --git a/src/DotRecast.Core/Vector3f.cs b/src/DotRecast.Core/Vector3f.cs index 3d92d0b..7098c5c 100644 --- a/src/DotRecast.Core/Vector3f.cs +++ b/src/DotRecast.Core/Vector3f.cs @@ -147,6 +147,18 @@ namespace DotRecast.Core return hash; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Normalize() + { + float d = (float)(1.0f / Math.Sqrt(RcMath.Sqr(x) + RcMath.Sqr(y) + RcMath.Sqr(z))); + if (d != 0) + { + x *= d; + y *= d; + z *= d; + } + } + public static bool operator ==(Vector3f left, Vector3f right) { return left.Equals(right); @@ -174,15 +186,13 @@ namespace DotRecast.Core } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Normalize() + public static Vector3f Lerp(Vector3f v1, Vector3f v2, float t) { - float d = (float)(1.0f / Math.Sqrt(RcMath.Sqr(x) + RcMath.Sqr(y) + RcMath.Sqr(z))); - if (d != 0) - { - x *= d; - y *= d; - z *= d; - } + return new Vector3f( + v1.x + (v2.x - v1.x) * t, + v1.y + (v2.y - v1.y) * t, + v1.z + (v2.z - v1.z) * t + ); } } } \ No newline at end of file diff --git a/src/DotRecast.Detour.Crowd/Crowd.cs b/src/DotRecast.Detour.Crowd/Crowd.cs index 5500201..0f41f87 100644 --- a/src/DotRecast.Detour.Crowd/Crowd.cs +++ b/src/DotRecast.Detour.Crowd/Crowd.cs @@ -1355,12 +1355,12 @@ namespace DotRecast.Detour.Crowd if (anim.t < ta) { float u = Tween(anim.t, 0.0f, ta); - ag.npos = VLerp(anim.initPos, anim.startPos, u); + ag.npos = Vector3f.Lerp(anim.initPos, anim.startPos, u); } else { float u = Tween(anim.t, ta, tb); - ag.npos = VLerp(anim.startPos, anim.endPos, u); + ag.npos = Vector3f.Lerp(anim.startPos, anim.endPos, u); } // Update velocity. diff --git a/src/DotRecast.Detour.Extras/Jumplink/AbstractGroundSampler.cs b/src/DotRecast.Detour.Extras/Jumplink/AbstractGroundSampler.cs index e995360..52bb347 100644 --- a/src/DotRecast.Detour.Extras/Jumplink/AbstractGroundSampler.cs +++ b/src/DotRecast.Detour.Extras/Jumplink/AbstractGroundSampler.cs @@ -32,7 +32,7 @@ namespace DotRecast.Detour.Extras.Jumplink GroundSample s = new GroundSample(); seg.gsamples[i] = s; - Vector3f pt = VLerp(seg.p, seg.q, u); + Vector3f pt = Vector3f.Lerp(seg.p, seg.q, u); Tuple height = heightFunc.Invoke(pt, seg.height); s.p.x = pt.x; s.p.y = height.Item2; diff --git a/src/DotRecast.Detour/NavMesh.cs b/src/DotRecast.Detour/NavMesh.cs index 16013a6..13195e6 100644 --- a/src/DotRecast.Detour/NavMesh.cs +++ b/src/DotRecast.Detour/NavMesh.cs @@ -836,8 +836,8 @@ namespace DotRecast.Detour var ext = new Vector3f() { - x = targetCon.rad, - y = target.data.header.walkableClimb, + x = targetCon.rad, + y = target.data.header.walkableClimb, z = targetCon.rad }; @@ -1070,8 +1070,8 @@ namespace DotRecast.Detour var ext = new Vector3f() { - x = con.rad, - y = tile.data.header.walkableClimb, + x = con.rad, + y = tile.data.header.walkableClimb, z = con.rad, }; @@ -1160,7 +1160,7 @@ namespace DotRecast.Detour int index = poly.verts[tris[ti + j]] * 3; v[j] = new Vector3f { - x = tile.data.verts[index], + x = tile.data.verts[index], y = tile.data.verts[index + 1], z = tile.data.verts[index + 2] }; @@ -1170,7 +1170,7 @@ namespace DotRecast.Detour int index = (pd.vertBase + (tris[ti + j] - poly.vertCount)) * 3; v[j] = new Vector3f { - x = tile.data.detailVerts[index], + x = tile.data.detailVerts[index], y = tile.data.detailVerts[index + 1], z = tile.data.detailVerts[index + 2] }; @@ -1226,7 +1226,7 @@ namespace DotRecast.Detour } } - return VLerp(pmin, pmax, tmin); + return Vector3f.Lerp(pmin, pmax, tmin); } public float? GetPolyHeight(MeshTile tile, Poly poly, Vector3f pos) @@ -1267,7 +1267,7 @@ namespace DotRecast.Detour int index = poly.verts[tile.data.detailTris[t + k]] * 3; v[k] = new Vector3f { - x = tile.data.verts[index], + x = tile.data.verts[index], y = tile.data.verts[index + 1], z = tile.data.verts[index + 2] }; @@ -1277,7 +1277,7 @@ namespace DotRecast.Detour int index = (pd.vertBase + (tile.data.detailTris[t + k] - poly.vertCount)) * 3; v[k] = new Vector3f { - x = tile.data.detailVerts[index], + x = tile.data.detailVerts[index], y = tile.data.detailVerts[index + 1], z = tile.data.detailVerts[index + 2] }; @@ -1344,7 +1344,7 @@ namespace DotRecast.Detour i = poly.verts[1] * 3; var v1 = new Vector3f { x = tile.data.verts[i], y = tile.data.verts[i + 1], z = tile.data.verts[i + 2] }; Tuple dt = DistancePtSegSqr2D(pos, v0, v1); - return new ClosestPointOnPolyResult(false, VLerp(v0, v1, dt.Item2)); + return new ClosestPointOnPolyResult(false, Vector3f.Lerp(v0, v1, dt.Item2)); } // Outside poly that is not an offmesh connection. diff --git a/src/DotRecast.Detour/NavMeshQuery.cs b/src/DotRecast.Detour/NavMeshQuery.cs index f723c5a..d12575e 100644 --- a/src/DotRecast.Detour/NavMeshQuery.cs +++ b/src/DotRecast.Detour/NavMeshQuery.cs @@ -364,7 +364,7 @@ namespace DotRecast.Detour // Cost if (neighbourNode.flags == 0) { - neighbourNode.pos = VLerp(va, vb, 0.5f); + neighbourNode.pos = Vector3f.Lerp(va, vb, 0.5f); } float total = bestNode.total + VDist(bestNode.pos, neighbourNode.pos); @@ -1505,7 +1505,7 @@ namespace DotRecast.Detour if (null != interect) { float t = interect.Item2; - var pt = VLerp(left, right, t); + var pt = Vector3f.Lerp(left, right, t); stat = AppendVertex(pt, 0, path[i + 1], straightPath, maxStraightPath); if (!stat.IsInProgress()) { @@ -1826,7 +1826,7 @@ namespace DotRecast.Detour bestPos = startPos; // Search constraints - var searchPos = VLerp(startPos, endPos, 0.5f); + var searchPos = Vector3f.Lerp(startPos, endPos, 0.5f); float searchRadSqr = Sqr(VDist(startPos, endPos) / 2.0f + 0.001f); float[] verts = new float[m_nav.GetMaxVertsPerPoly() * 3]; @@ -2134,7 +2134,7 @@ namespace DotRecast.Detour t = Clamp(interect.Item2, 0.1f, 0.9f); } - Vector3f pt = VLerp(left, right, t); + Vector3f pt = Vector3f.Lerp(left, right, t); return Results.Success(pt); } @@ -2571,7 +2571,7 @@ namespace DotRecast.Detour // Cost if (neighbourNode.flags == 0) { - neighbourNode.pos = VLerp(va, vb, 0.5f); + neighbourNode.pos = Vector3f.Lerp(va, vb, 0.5f); } float cost = filter.GetCost(bestNode.pos, neighbourNode.pos, parentRef, parentTile, parentPoly, bestRef, @@ -2763,7 +2763,7 @@ namespace DotRecast.Detour // Cost if (neighbourNode.flags == 0) { - neighbourNode.pos = VLerp(va, vb, 0.5f); + neighbourNode.pos = Vector3f.Lerp(va, vb, 0.5f); } float cost = filter.GetCost(bestNode.pos, neighbourNode.pos, parentRef, parentTile, parentPoly, bestRef, diff --git a/src/DotRecast.Recast.Demo/Tools/JumpLinkBuilderTool.cs b/src/DotRecast.Recast.Demo/Tools/JumpLinkBuilderTool.cs index 778162c..1b2f97a 100644 --- a/src/DotRecast.Recast.Demo/Tools/JumpLinkBuilderTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/JumpLinkBuilderTool.cs @@ -236,7 +236,7 @@ public class JumpLinkBuilderTool : Tool { GroundSample s = link.start.gsamples[i]; float u = i / (float)(link.start.gsamples.Length - 1); - Vector3f spt = VLerp(link.start.p, link.start.q, u); + Vector3f spt = Vector3f.Lerp(link.start.p, link.start.q, u); int col = DuRGBA(48, 16, 16, 255); // DuRGBA(255,(s->flags & 4)?255:0,0,255); float off = 0.1f; if (!s.validHeight) @@ -256,7 +256,7 @@ public class JumpLinkBuilderTool : Tool { GroundSample s = link.start.gsamples[i]; float u = i / (float)(link.start.gsamples.Length - 1); - Vector3f spt = VLerp(link.start.p, link.start.q, u); + Vector3f spt = Vector3f.Lerp(link.start.p, link.start.q, u); int col = DuRGBA(255, 255, 255, 255); float off = 0; if (s.validHeight) @@ -276,7 +276,7 @@ public class JumpLinkBuilderTool : Tool { GroundSample s = end.gsamples[i]; float u = i / (float)(end.gsamples.Length - 1); - Vector3f spt = VLerp(end.p, end.q, u); + Vector3f spt = Vector3f.Lerp(end.p, end.q, u); int col = DuRGBA(48, 16, 16, 255); // DuRGBA(255,(s->flags & 4)?255:0,0,255); float off = 0.1f; if (!s.validHeight) @@ -295,7 +295,7 @@ public class JumpLinkBuilderTool : Tool { GroundSample s = end.gsamples[i]; float u = i / (float)(end.gsamples.Length - 1); - Vector3f spt = VLerp(end.p, end.q, u); + Vector3f spt = Vector3f.Lerp(end.p, end.q, u); int col = DuRGBA(255, 255, 255, 255); float off = 0; if (s.validHeight) diff --git a/src/DotRecast.Recast.Demo/Tools/TestNavmeshTool.cs b/src/DotRecast.Recast.Demo/Tools/TestNavmeshTool.cs index df6dee3..366df06 100644 --- a/src/DotRecast.Recast.Demo/Tools/TestNavmeshTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/TestNavmeshTool.cs @@ -380,7 +380,7 @@ public class TestNavmeshTool : Tool else { // Hit - m_hitPos = VLerp(m_spos, m_epos, hit.result.t); + m_hitPos = Vector3f.Lerp(m_spos, m_epos, hit.result.t); m_hitNormal = hit.result.hitNormal; m_hitResult = true; } @@ -949,7 +949,7 @@ public class TestNavmeshTool : Tool private Vector3f GetPolyCenter(NavMesh navMesh, long refs) { Vector3f center = Vector3f.Zero; - + Result> tileAndPoly = navMesh.GetTileAndPolyByRef(refs); if (tileAndPoly.Succeeded()) {