move RcMath VDist, VLen -> Vector3f.Distance, Length

This commit is contained in:
ikpil 2023-05-18 08:25:57 +09:00
parent aeaf0f2c41
commit b1f29a0812
10 changed files with 42 additions and 59 deletions

View File

@ -172,19 +172,6 @@ namespace DotRecast.Core
} }
/// 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.
public static float VDist(Vector3f v1, Vector3f v2)
{
float dx = v2.x - v1.x;
float dy = v2.y - v1.y;
float dz = v2.z - v1.z;
return (float)Math.Sqrt(dx * dx + dy * dy + dz * dz);
}
/// Returns the distance between two points. /// Returns the distance between two points.
/// @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)]
@ -201,37 +188,12 @@ namespace DotRecast.Core
/// Derives the square of the scalar length of the vector. (len * len) /// Derives the square of the scalar length of the vector. (len * len)
/// @param[in] v The vector. [(x, y, z)] /// @param[in] v The vector. [(x, y, z)]
/// @return The square of the scalar length of the vector. /// @return The square of the scalar length of the vector.
public static float VLenSqr(float[] v)
{
return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
}
public static float VLenSqr(Vector3f v) public static float VLenSqr(Vector3f v)
{ {
return v.x * v.x + v.y * v.y + v.z * v.z; return v.x * v.x + v.y * v.y + v.z * v.z;
} }
public static float VLen(float[] v)
{
return (float)Math.Sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
}
public static float VLen(Vector3f v)
{
return (float)Math.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}
public static float VDist(float[] v1, float[] verts, int i)
{
float dx = verts[i] - v1[0];
float dy = verts[i + 1] - v1[1];
float dz = verts[i + 2] - v1[2];
return (float)Math.Sqrt(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)]

View File

@ -93,9 +93,15 @@ namespace DotRecast.Core
default: throw new IndexOutOfRangeException($"{index}-{value}"); default: throw new IndexOutOfRangeException($"{index}-{value}");
} }
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector3f Subtract(Vector3f right) public readonly float Length()
{
return (float)Math.Sqrt(x * x + y * y + z * z);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector3f Subtract(Vector3f right)
{ {
return new Vector3f( return new Vector3f(
x - right.x, x - right.x,
@ -105,7 +111,7 @@ namespace DotRecast.Core
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector3f Add(Vector3f v2) public readonly Vector3f Add(Vector3f v2)
{ {
return new Vector3f( return new Vector3f(
x + v2.x, x + v2.x,
@ -122,13 +128,13 @@ namespace DotRecast.Core
/// The vectors are projected onto the xz-plane, so the y-values are /// The vectors are projected onto the xz-plane, so the y-values are
/// ignored. /// ignored.
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Dot2D(Vector3f v) public readonly float Dot2D(Vector3f v)
{ {
return x * v.x + z * v.z; return x * v.x + z * v.z;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Dot2D(float[] v, int vi) public readonly float Dot2D(float[] v, int vi)
{ {
return x * v[vi] + z * v[vi + 2]; return x * v[vi] + z * v[vi + 2];
} }
@ -210,5 +216,20 @@ namespace DotRecast.Core
v1.z + (v2.z - v1.z) * t v1.z + (v2.z - v1.z) * t
); );
} }
/// 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 Distance(Vector3f v1, Vector3f v2)
{
float dx = v2.x - v1.x;
float dy = v2.y - v1.y;
float dz = v2.z - v1.z;
return (float)Math.Sqrt(dx * dx + dy * dy + dz * dz);
}
} }
} }

View File

@ -1055,7 +1055,7 @@ namespace DotRecast.Detour.Crowd
if (ag.targetState == MoveRequestState.DT_CROWDAGENT_TARGET_VELOCITY) if (ag.targetState == MoveRequestState.DT_CROWDAGENT_TARGET_VELOCITY)
{ {
dvel = ag.targetPos; dvel = ag.targetPos;
ag.desiredSpeed = VLen(ag.targetPos); ag.desiredSpeed = ag.targetPos.Length();
} }
else else
{ {

View File

@ -116,13 +116,13 @@ namespace DotRecast.Detour.Crowd
// Fake dynamic constraint. // Fake dynamic constraint.
float maxDelta = option.maxAcceleration * dt; float maxDelta = option.maxAcceleration * dt;
Vector3f dv = nvel.Subtract(vel); Vector3f dv = nvel.Subtract(vel);
float ds = VLen(dv); float ds = dv.Length();
if (ds > maxDelta) if (ds > maxDelta)
dv = VScale(dv, maxDelta / ds); dv = VScale(dv, maxDelta / ds);
vel = vel.Add(dv); vel = vel.Add(dv);
// Integrate // Integrate
if (VLen(vel) > 0.0001f) if (vel.Length() > 0.0001f)
npos = VMad(npos, vel, dt); npos = VMad(npos, vel, dt);
else else
vel = Vector3f.Zero; vel = Vector3f.Zero;
@ -174,8 +174,8 @@ namespace DotRecast.Detour.Crowd
dir0.y = 0; dir0.y = 0;
dir1.y = 0; dir1.y = 0;
float len0 = VLen(dir0); float len0 = dir0.Length();
float len1 = VLen(dir1); float len1 = dir1.Length();
if (len1 > 0.001f) if (len1 > 0.001f)
dir1 = VScale(dir1, 1.0f / len1); dir1 = VScale(dir1, 1.0f / len1);

View File

@ -89,7 +89,7 @@ namespace DotRecast.Detour
public float GetCost(Vector3f pa, Vector3f pb, long prevRef, MeshTile prevTile, Poly prevPoly, long curRef, public float GetCost(Vector3f pa, Vector3f pb, long prevRef, MeshTile prevTile, Poly prevPoly, long curRef,
MeshTile curTile, Poly curPoly, long nextRef, MeshTile nextTile, Poly nextPoly) MeshTile curTile, Poly curPoly, long nextRef, MeshTile nextTile, Poly nextPoly)
{ {
return VDist(pa, pb) * m_areaCost[curPoly.GetArea()]; return Vector3f.Distance(pa, pb) * m_areaCost[curPoly.GetArea()];
} }
public int GetIncludeFlags() public int GetIncludeFlags()

View File

@ -37,7 +37,7 @@ namespace DotRecast.Detour
public float GetCost(Vector3f neighbourPos, Vector3f endPos) public float GetCost(Vector3f neighbourPos, Vector3f endPos)
{ {
return VDist(neighbourPos, endPos) * scale; return Vector3f.Distance(neighbourPos, endPos) * scale;
} }
} }
} }

View File

@ -62,7 +62,7 @@ namespace DotRecast.Detour
startNode.pos = startPos; startNode.pos = startPos;
startNode.pidx = 0; startNode.pidx = 0;
startNode.cost = 0; startNode.cost = 0;
startNode.total = VDist(startPos, endPos) * H_SCALE; startNode.total = Vector3f.Distance(startPos, endPos) * H_SCALE;
startNode.id = startRef; startNode.id = startRef;
startNode.flags = Node.DT_NODE_OPEN; startNode.flags = Node.DT_NODE_OPEN;
m_openList.Push(startNode); m_openList.Push(startNode);
@ -173,7 +173,7 @@ namespace DotRecast.Detour
float curCost = filter.GetCost(bestNode.pos, neighbourNode.pos, parentRef, parentTile, parentPoly, float curCost = filter.GetCost(bestNode.pos, neighbourNode.pos, parentRef, parentTile, parentPoly,
bestRef, bestTile, bestPoly, neighbourRef, neighbourTile, neighbourPoly); bestRef, bestTile, bestPoly, neighbourRef, neighbourTile, neighbourPoly);
cost = bestNode.cost + curCost; cost = bestNode.cost + curCost;
heuristic = VDist(neighbourNode.pos, endPos) * H_SCALE; heuristic = Vector3f.Distance(neighbourNode.pos, endPos) * H_SCALE;
} }
float total = cost + heuristic; float total = cost + heuristic;
@ -410,7 +410,7 @@ namespace DotRecast.Detour
} }
else else
{ {
heuristic = VDist(neighbourNode.pos, m_query.endPos) * H_SCALE; heuristic = Vector3f.Distance(neighbourNode.pos, m_query.endPos) * H_SCALE;
} }
float total = cost + heuristic; float total = cost + heuristic;
@ -815,7 +815,7 @@ namespace DotRecast.Detour
} }
} }
float total = bestNode.total + VDist(bestNode.pos, neighbourNode.pos); float total = bestNode.total + Vector3f.Distance(bestNode.pos, neighbourNode.pos);
// The node is already in open list and the new result is worse, skip. // The node is already in open list and the new result is worse, skip.
if ((neighbourNode.flags & Node.DT_NODE_OPEN) != 0 && total >= neighbourNode.total) if ((neighbourNode.flags & Node.DT_NODE_OPEN) != 0 && total >= neighbourNode.total)

View File

@ -367,7 +367,7 @@ namespace DotRecast.Detour
neighbourNode.pos = Vector3f.Lerp(va, vb, 0.5f); neighbourNode.pos = Vector3f.Lerp(va, vb, 0.5f);
} }
float total = bestNode.total + VDist(bestNode.pos, neighbourNode.pos); float total = bestNode.total + Vector3f.Distance(bestNode.pos, neighbourNode.pos);
// The node is already in open list and the new result is worse, skip. // The node is already in open list and the new result is worse, skip.
if ((neighbourNode.flags & Node.DT_NODE_OPEN) != 0 && total >= neighbourNode.total) if ((neighbourNode.flags & Node.DT_NODE_OPEN) != 0 && total >= neighbourNode.total)
@ -1827,7 +1827,7 @@ namespace DotRecast.Detour
// Search constraints // Search constraints
var searchPos = Vector3f.Lerp(startPos, endPos, 0.5f); var searchPos = Vector3f.Lerp(startPos, endPos, 0.5f);
float searchRadSqr = Sqr(VDist(startPos, endPos) / 2.0f + 0.001f); float searchRadSqr = Sqr(Vector3f.Distance(startPos, endPos) / 2.0f + 0.001f);
float[] verts = new float[m_nav.GetMaxVertsPerPoly() * 3]; float[] verts = new float[m_nav.GetMaxVertsPerPoly() * 3];
@ -3339,7 +3339,7 @@ namespace DotRecast.Detour
} }
} }
float total = bestNode.total + VDist(bestNode.pos, neighbourNode.pos); float total = bestNode.total + Vector3f.Distance(bestNode.pos, neighbourNode.pos);
// The node is already in open list and the new result is worse, skip. // The node is already in open list and the new result is worse, skip.
if ((neighbourNode.flags & Node.DT_NODE_OPEN) != 0 && total >= neighbourNode.total) if ((neighbourNode.flags & Node.DT_NODE_OPEN) != 0 && total >= neighbourNode.total)

View File

@ -33,7 +33,7 @@ public class CapsuleGizmo : IColliderGizmo
var trY = Vector3f.Of(normals[0].y, normals[1].y, normals[2].y); var trY = Vector3f.Of(normals[0].y, normals[1].y, normals[2].y);
var trZ = Vector3f.Of(normals[0].z, normals[1].z, normals[2].z); var trZ = Vector3f.Of(normals[0].z, normals[1].z, normals[2].z);
float[] spVertices = GenerateSphericalVertices(); float[] spVertices = GenerateSphericalVertices();
float halfLength = 0.5f * VLen(axis); float halfLength = 0.5f * axis.Length();
vertices = new float[spVertices.Length]; vertices = new float[spVertices.Length];
gradient = new float[spVertices.Length / 3]; gradient = new float[spVertices.Length / 3];
Vector3f v = new Vector3f(); Vector3f v = new Vector3f();

View File

@ -33,7 +33,7 @@ public class CylinderGizmo : IColliderGizmo
Vector3f trY = Vector3f.Of(normals[0].y, normals[1].y, normals[2].y); Vector3f trY = Vector3f.Of(normals[0].y, normals[1].y, normals[2].y);
Vector3f trZ = Vector3f.Of(normals[0].z, normals[1].z, normals[2].z); Vector3f trZ = Vector3f.Of(normals[0].z, normals[1].z, normals[2].z);
vertices = GenerateCylindricalVertices(); vertices = GenerateCylindricalVertices();
float halfLength = 0.5f * VLen(axis); float halfLength = 0.5f * axis.Length();
gradient = new float[vertices.Length / 3]; gradient = new float[vertices.Length / 3];
Vector3f v = new Vector3f(); Vector3f v = new Vector3f();
for (int i = 0; i < vertices.Length; i += 3) for (int i = 0; i < vertices.Length; i += 3)