refactor: RcVec3f.DistSqr, RcVec3f.LenSqr

This commit is contained in:
ikpil 2023-10-25 00:40:00 +09:00
parent 182c230c10
commit 28ad34b032
11 changed files with 56 additions and 51 deletions

View File

@ -23,7 +23,6 @@ namespace DotRecast.Core.Numerics
{ {
public struct RcVec3f public struct RcVec3f
{ {
public float X; public float X;
public float Y; public float Y;
public float Z; public float Z;
@ -103,6 +102,9 @@ namespace DotRecast.Core.Numerics
return MathF.Sqrt(lengthSquared); return MathF.Sqrt(lengthSquared);
} }
/// Derives the square of the scalar length of the vector. (len * len)
/// @param[in] v The vector. [(x, y, z)]
/// @return The square of the scalar length of the vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly float LengthSquared() public readonly float LengthSquared()
{ {
@ -313,29 +315,6 @@ namespace DotRecast.Core.Numerics
); );
} }
/// Returns the distance between two points.
/// @param[in] v1 A point. [(x, y, z)]
/// @param[in] v2 A point. [(x, y, z)]
/// @return The distance between the two points.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float DistSqr(RcVec3f v1, float[] v2, int i)
{
float dx = v2[i] - v1.X;
float dy = v2[i + 1] - v1.Y;
float dz = v2[i + 2] - v1.Z;
return dx * dx + dy * dy + dz * dz;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float DistSqr(RcVec3f v1, RcVec3f v2)
{
float dx = v2.X - v1.X;
float dy = v2.Y - v1.Y;
float dz = v2.Z - v1.Z;
return dx * dx + dy * dy + dz * dz;
}
/// Derives the distance between the specified points on the xz-plane. /// Derives the distance between the specified points on the xz-plane.
/// @param[in] v1 A point. [(x, y, z)] /// @param[in] v1 A point. [(x, y, z)]
/// @param[in] v2 A point. [(x, y, z)] /// @param[in] v2 A point. [(x, y, z)]
@ -380,15 +359,6 @@ namespace DotRecast.Core.Numerics
return u.Z * v.X - u.X * v.Z; return u.Z * v.X - u.X * v.Z;
} }
/// Derives the square of the scalar length of the vector. (len * len)
/// @param[in] v The vector. [(x, y, z)]
/// @return The square of the scalar length of the vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float LenSqr(RcVec3f v)
{
return v.X * v.X + v.Y * v.Y + v.Z * v.Z;
}
/// Checks that the specified vector's components are all finite. /// Checks that the specified vector's components are all finite.
/// @param[in] v A point. [(x, y, z)] /// @param[in] v A point. [(x, y, z)]

View File

@ -123,6 +123,19 @@ namespace DotRecast.Core.Numerics
v1[2] * vector2.Z; v1[2] * vector2.Z;
} }
/// Returns the distance between two points.
/// @param[in] v1 A point. [(x, y, z)]
/// @param[in] v2 A point. [(x, y, z)]
/// @return The distance between the two points.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float DistanceSquared(RcVec3f v1, float[] v2, int i)
{
float dx = v2[i] - v1.X;
float dy = v2[i + 1] - v1.Y;
float dz = v2[i + 2] - v1.Z;
return dx * dx + dy * dy + dz * dz;
}
/// Normalizes the vector if the length is greater than zero. /// Normalizes the vector if the length is greater than zero.
/// If the magnitude is zero, the vector is unchanged. /// If the magnitude is zero, the vector is unchanged.
/// @param[in,out] v The vector to normalize. [(x, y, z)] /// @param[in,out] v The vector to normalize. [(x, y, z)]

View File

@ -911,7 +911,7 @@ namespace DotRecast.Detour.Crowd
} }
diff.Y = 0; diff.Y = 0;
float distSqr = RcVec3f.LenSqr(diff); float distSqr = diff.LengthSquared();
if (distSqr > RcMath.Sqr(range)) if (distSqr > RcMath.Sqr(range))
{ {
continue; continue;
@ -1080,7 +1080,7 @@ namespace DotRecast.Detour.Crowd
RcVec3f diff = RcVec3f.Subtract(ag.npos, nei.npos); RcVec3f diff = RcVec3f.Subtract(ag.npos, nei.npos);
diff.Y = 0; diff.Y = 0;
float distSqr = RcVec3f.LenSqr(diff); float distSqr = diff.LengthSquared();
if (distSqr < 0.00001f) if (distSqr < 0.00001f)
{ {
continue; continue;
@ -1103,7 +1103,7 @@ namespace DotRecast.Detour.Crowd
// Adjust desired velocity. // Adjust desired velocity.
dvel = RcVec3f.Mad(dvel, disp, 1.0f / w); dvel = RcVec3f.Mad(dvel, disp, 1.0f / w);
// Clamp desired velocity to desired speed. // Clamp desired velocity to desired speed.
float speedSqr = RcVec3f.LenSqr(dvel); float speedSqr = dvel.LengthSquared();
float desiredSqr = RcMath.Sqr(ag.desiredSpeed); float desiredSqr = RcMath.Sqr(ag.desiredSpeed);
if (speedSqr > desiredSqr) if (speedSqr > desiredSqr)
{ {
@ -1227,7 +1227,7 @@ namespace DotRecast.Detour.Crowd
RcVec3f diff = RcVec3f.Subtract(ag.npos, nei.npos); RcVec3f diff = RcVec3f.Subtract(ag.npos, nei.npos);
diff.Y = 0; diff.Y = 0;
float dist = RcVec3f.LenSqr(diff); float dist = diff.LengthSquared();
if (dist > RcMath.Sqr(ag.option.radius + nei.option.radius)) if (dist > RcMath.Sqr(ag.option.radius + nei.option.radius))
{ {
continue; continue;

View File

@ -36,7 +36,7 @@ namespace DotRecast.Detour
} }
else else
{ {
d = RcVec3f.LenSqr(diff); d = diff.LengthSquared();
} }
if (d < _nearestDistanceSqr) if (d < _nearestDistanceSqr)

View File

@ -1377,7 +1377,7 @@ namespace DotRecast.Detour
} }
else else
{ {
d = RcVec3f.LenSqr(diff); d = diff.LengthSquared();
} }
if (d < nearestDistanceSqr) if (d < nearestDistanceSqr)

View File

@ -830,8 +830,8 @@ namespace DotRecast.Detour
bool tryLOS = false; bool tryLOS = false;
if ((options & DtFindPathOptions.DT_FINDPATH_ANY_ANGLE) != 0) if ((options & DtFindPathOptions.DT_FINDPATH_ANY_ANGLE) != 0)
{ {
if ((parentRef != 0) && (raycastLimitSqr >= float.MaxValue if ((parentRef != 0) &&
|| RcVec3f.DistSqr(parentNode.pos, bestNode.pos) < raycastLimitSqr)) (raycastLimitSqr >= float.MaxValue || RcVec3f.DistanceSquared(parentNode.pos, bestNode.pos) < raycastLimitSqr))
{ {
tryLOS = true; tryLOS = true;
} }
@ -1148,8 +1148,8 @@ namespace DotRecast.Detour
bool tryLOS = false; bool tryLOS = false;
if ((m_query.options & DtFindPathOptions.DT_FINDPATH_ANY_ANGLE) != 0) if ((m_query.options & DtFindPathOptions.DT_FINDPATH_ANY_ANGLE) != 0)
{ {
if ((parentRef != 0) && (m_query.raycastLimitSqr >= float.MaxValue if ((parentRef != 0) &&
|| RcVec3f.DistSqr(parentNode.pos, bestNode.pos) < m_query.raycastLimitSqr)) (m_query.raycastLimitSqr >= float.MaxValue || RcVec3f.DistanceSquared(parentNode.pos, bestNode.pos) < m_query.raycastLimitSqr))
{ {
tryLOS = true; tryLOS = true;
} }

View File

@ -53,7 +53,7 @@ namespace DotRecast.Detour
public static bool VEqual(RcVec3f p0, RcVec3f p1, float thresholdSqr) public static bool VEqual(RcVec3f p0, RcVec3f p1, float thresholdSqr)
{ {
float d = RcVec3f.DistSqr(p0, p1); float d = RcVec3f.DistanceSquared(p0, p1);
return d < thresholdSqr; return d < thresholdSqr;
} }

View File

@ -45,7 +45,7 @@ namespace DotRecast.Recast.Toolset.Tools
// Create // Create
// If clicked on that last pt, create the shape. // If clicked on that last pt, create the shape.
if (_pts.Count > 0 && RcVec3f.DistSqr(p, _pts[_pts.Count - 1]) < 0.2f * 0.2f) if (_pts.Count > 0 && RcVec3f.DistanceSquared(p, _pts[_pts.Count - 1]) < 0.2f * 0.2f)
{ {
pts = new List<RcVec3f>(_pts); pts = new List<RcVec3f>(_pts);
hull = new List<int>(_hull); hull = new List<int>(_hull);

View File

@ -107,7 +107,7 @@ namespace DotRecast.Recast.Toolset.Tools
bool valid = true; bool valid = true;
foreach (var zone in _polyPoints) foreach (var zone in _polyPoints)
{ {
if (RcVec3f.DistSqr(zone.pt, randomPt) < zoneSeparation) if (RcVec3f.DistanceSquared(zone.pt, randomPt) < zoneSeparation)
{ {
valid = false; valid = false;
break; break;
@ -285,7 +285,7 @@ namespace DotRecast.Recast.Toolset.Tools
List<DtPolyPoint> potentialTargets = new List<DtPolyPoint>(); List<DtPolyPoint> potentialTargets = new List<DtPolyPoint>();
foreach (var zone in _polyPoints) foreach (var zone in _polyPoints)
{ {
if (RcVec3f.DistSqr(zone.pt, ag.npos) > _cfg.zoneRadius * _cfg.zoneRadius) if (RcVec3f.DistanceSquared(zone.pt, ag.npos) > _cfg.zoneRadius * _cfg.zoneRadius)
{ {
potentialTargets.Add(zone); potentialTargets.Add(zone);
} }

View File

@ -34,7 +34,7 @@ namespace DotRecast.Recast.Toolset.Tools
RcOffMeshConnection nearestConnection = null; RcOffMeshConnection nearestConnection = null;
foreach (RcOffMeshConnection offMeshCon in geom.GetOffMeshConnections()) foreach (RcOffMeshConnection offMeshCon in geom.GetOffMeshConnections())
{ {
float d = Math.Min(RcVec3f.DistSqr(p, offMeshCon.verts, 0), RcVec3f.DistSqr(p, offMeshCon.verts, 3)); float d = Math.Min(RcVecUtils.DistanceSquared(p, offMeshCon.verts, 0), RcVecUtils.DistanceSquared(p, offMeshCon.verts, 3));
if (d < nearestDist && Math.Sqrt(d) < settings.agentRadius) if (d < nearestDist && Math.Sqrt(d) < settings.agentRadius)
{ {
nearestDist = d; nearestDist = d;

View File

@ -141,6 +141,28 @@ public class Vector3Tests
Assert.That(d4, Is.EqualTo(d44)); Assert.That(d4, Is.EqualTo(d44));
} }
[Test]
[Repeat(100000)]
public void TestVectorMinMax()
{
// Min
// Max
}
[Test]
[Repeat(100000)]
public void TestVectorIsFinite()
{
// IsFinite
}
[Test]
[Repeat(100000)]
public void TestVectorPerp2D()
{
// Perp2D
}
// [Test] // [Test]
// [Repeat(100000)] // [Repeat(100000)]
// public void TestVectorLerp() // public void TestVectorLerp()