refactor: vector3

This commit is contained in:
ikpil 2023-10-27 23:52:55 +09:00
parent 21b3cd6caa
commit 2e7a75624c
16 changed files with 59 additions and 47 deletions

View File

@ -50,13 +50,13 @@ namespace DotRecast.Core.Numerics
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] // [MethodImpl(MethodImplOptions.AggressiveInlining)]
public RcVec3f(ReadOnlySpan<float> values) // public RcVec3f(ReadOnlySpan<float> values)
{ // {
X = values[0]; // X = values[0];
Y = values[1]; // Y = values[1];
Z = values[2]; // Z = values[2];
} // }
public float this[int index] public float this[int index]
{ {

View File

@ -6,6 +6,18 @@ namespace DotRecast.Core.Numerics
public static class RcVecUtils public static class RcVecUtils
{ {
public const float EPSILON = 1e-6f; public const float EPSILON = 1e-6f;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RcVec3f Create(float[] values)
{
return Create(values, 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RcVec3f Create(float[] values, int n)
{
return new RcVec3f(values[n + 0], values[n + 1], values[n + 2]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Get(this RcVec2f v, int i) public static float Get(this RcVec2f v, int i)

View File

@ -412,7 +412,7 @@ namespace DotRecast.Detour.Crowd
else if (n == 0) else if (n == 0)
{ {
// The first polyref is bad, use current safe values. // The first polyref is bad, use current safe values.
m_pos = new RcVec3f(safePos); m_pos = RcVecUtils.Create(safePos);
m_path.Clear(); m_path.Clear();
m_path.Add(safeRef); m_path.Add(safeRef);
} }

View File

@ -40,8 +40,8 @@ namespace DotRecast.Detour.Extras
BVItem it = new BVItem(); BVItem it = new BVItem();
items[i] = it; items[i] = it;
it.i = i; it.i = i;
RcVec3f bmin = new RcVec3f(data.verts.AsSpan(data.polys[i].verts[0] * 3)); RcVec3f bmin = RcVecUtils.Create(data.verts, data.polys[i].verts[0] * 3);
RcVec3f bmax = new RcVec3f(data.verts.AsSpan(data.polys[i].verts[0] * 3)); RcVec3f bmax = RcVecUtils.Create(data.verts, data.polys[i].verts[0] * 3);
for (int j = 1; j < data.polys[i].vertCount; j++) for (int j = 1; j < data.polys[i].vertCount; j++)
{ {
bmin = RcVecUtils.Min(bmin, data.verts, data.polys[i].verts[j] * 3); bmin = RcVecUtils.Min(bmin, data.verts, data.polys[i].verts[j] * 3);

View File

@ -56,7 +56,7 @@ namespace DotRecast.Detour.Extras.Unity.Astar
connection.rad = 0.1f; connection.rad = 0.1f;
connection.side = startTile == endTile connection.side = startTile == endTile
? 0xFF ? 0xFF
: DtNavMeshBuilder.ClassifyOffMeshPoint(new RcVec3f(connection.pos.AsSpan(3)), startTile.header.bmin, startTile.header.bmax); : DtNavMeshBuilder.ClassifyOffMeshPoint(RcVecUtils.Create(connection.pos, 3), startTile.header.bmin, startTile.header.bmax);
connection.userId = (int)l.linkID; connection.userId = (int)l.linkID;
if (startTile.offMeshCons == null) if (startTile.offMeshCons == null)
{ {

View File

@ -53,10 +53,10 @@ namespace DotRecast.Detour
do do
{ {
a = new RcVec3f(p.AsSpan(3 * (ai % n))); a = RcVecUtils.Create(p, 3 * (ai % n));
b = new RcVec3f(q.AsSpan(3 * (bi % m))); b = RcVecUtils.Create(q, 3 * (bi % m));
a1 = new RcVec3f(p.AsSpan(3 * ((ai + n - 1) % n))); // prev a a1 = RcVecUtils.Create(p, 3 * ((ai + n - 1) % n)); // prev a
b1 = new RcVec3f(q.AsSpan(3 * ((bi + m - 1) % m))); // prev b b1 = RcVecUtils.Create(q, 3 * ((bi + m - 1) % m)); // prev b
RcVec3f A = RcVec3f.Subtract(a, a1); RcVec3f A = RcVec3f.Subtract(a, a1);
RcVec3f B = RcVec3f.Subtract(b, b1); RcVec3f B = RcVec3f.Subtract(b, b1);

View File

@ -401,8 +401,8 @@ namespace DotRecast.Detour
// Calc polygon bounds. // Calc polygon bounds.
int v = p.verts[0] * 3; int v = p.verts[0] * 3;
bmin = new RcVec3f(tile.data.verts.AsSpan(v)); bmin = RcVecUtils.Create(tile.data.verts, v);
bmax = new RcVec3f(tile.data.verts.AsSpan(v)); bmax = RcVecUtils.Create(tile.data.verts, v);
for (int j = 1; j < p.vertCount; ++j) for (int j = 1; j < p.vertCount; ++j)
{ {
v = p.verts[j] * 3; v = p.verts[j] * 3;
@ -1569,8 +1569,8 @@ namespace DotRecast.Detour
} }
} }
startPos = new RcVec3f(tile.data.verts.AsSpan(poly.verts[idx0] * 3)); startPos = RcVecUtils.Create(tile.data.verts, poly.verts[idx0] * 3);
endPos = new RcVec3f(tile.data.verts.AsSpan(poly.verts[idx1] * 3)); endPos = RcVecUtils.Create(tile.data.verts, poly.verts[idx1] * 3);
return DtStatus.DT_SUCCSESS; return DtStatus.DT_SUCCSESS;
} }

View File

@ -158,8 +158,8 @@ namespace DotRecast.Detour
int vb = option.detailMeshes[i * 4 + 0]; int vb = option.detailMeshes[i * 4 + 0];
int ndv = option.detailMeshes[i * 4 + 1]; int ndv = option.detailMeshes[i * 4 + 1];
int dv = vb * 3; int dv = vb * 3;
var bmin = new RcVec3f(option.detailVerts.AsSpan(dv)); var bmin = RcVecUtils.Create(option.detailVerts, dv);
var bmax = new RcVec3f(option.detailVerts.AsSpan(dv)); var bmax = RcVecUtils.Create(option.detailVerts, dv);
for (int j = 1; j < ndv; j++) for (int j = 1; j < ndv; j++)
{ {
bmin = RcVecUtils.Min(bmin, option.detailVerts, dv + j * 3); bmin = RcVecUtils.Min(bmin, option.detailVerts, dv + j * 3);
@ -315,8 +315,8 @@ namespace DotRecast.Detour
for (int i = 0; i < option.offMeshConCount; ++i) for (int i = 0; i < option.offMeshConCount; ++i)
{ {
var p0 = new RcVec3f(option.offMeshConVerts.AsSpan((i * 2 + 0) * 3)); var p0 = RcVecUtils.Create(option.offMeshConVerts, (i * 2 + 0) * 3);
var p1 = new RcVec3f(option.offMeshConVerts.AsSpan((i * 2 + 1) * 3)); var p1 = RcVecUtils.Create(option.offMeshConVerts, (i * 2 + 1) * 3);
offMeshConClass[i * 2 + 0] = ClassifyOffMeshPoint(p0, bmin, bmax); offMeshConClass[i * 2 + 0] = ClassifyOffMeshPoint(p0, bmin, bmax);
offMeshConClass[i * 2 + 1] = ClassifyOffMeshPoint(p1, bmin, bmax); offMeshConClass[i * 2 + 1] = ClassifyOffMeshPoint(p1, bmin, bmax);

View File

@ -641,8 +641,8 @@ namespace DotRecast.Detour
// Calc polygon bounds. // Calc polygon bounds.
int v = p.verts[0] * 3; int v = p.verts[0] * 3;
bmin = new RcVec3f(tile.data.verts.AsSpan(v)); bmin = RcVecUtils.Create(tile.data.verts, v);
bmax = new RcVec3f(tile.data.verts.AsSpan(v)); bmax = RcVecUtils.Create(tile.data.verts, v);
for (int j = 1; j < p.vertCount; ++j) for (int j = 1; j < p.vertCount; ++j)
{ {
v = p.verts[j] * 3; v = p.verts[j] * 3;
@ -2202,7 +2202,7 @@ namespace DotRecast.Detour
int nv = 0; int nv = 0;
for (int i = 0; i < poly.vertCount; ++i) for (int i = 0; i < poly.vertCount; ++i)
{ {
verts[nv] = new RcVec3f(tile.data.verts.AsSpan(poly.verts[i] * 3)); verts[nv] = RcVecUtils.Create(tile.data.verts, poly.verts[i] * 3);
nv++; nv++;
} }
@ -3031,8 +3031,8 @@ namespace DotRecast.Detour
int ivj = poly.verts[j] * 3; int ivj = poly.verts[j] * 3;
int ivi = poly.verts[i] * 3; int ivi = poly.verts[i] * 3;
var seg = new RcSegmentVert(); var seg = new RcSegmentVert();
seg.vmin = new RcVec3f(tile.data.verts.AsSpan(ivj)); seg.vmin = RcVecUtils.Create(tile.data.verts, ivj);
seg.vmax = new RcVec3f(tile.data.verts.AsSpan(ivi)); seg.vmax = RcVecUtils.Create(tile.data.verts, ivi);
// Array.Copy(tile.data.verts, ivj, seg, 0, 3); // Array.Copy(tile.data.verts, ivj, seg, 0, 3);
// Array.Copy(tile.data.verts, ivi, seg, 3, 3); // Array.Copy(tile.data.verts, ivi, seg, 3, 3);
segmentVerts.Add(seg); segmentVerts.Add(seg);
@ -3212,8 +3212,8 @@ namespace DotRecast.Detour
hitPos.Y = bestTile.data.verts[vj + 1] + (bestTile.data.verts[vi + 1] - bestTile.data.verts[vj + 1]) * tseg; hitPos.Y = bestTile.data.verts[vj + 1] + (bestTile.data.verts[vi + 1] - bestTile.data.verts[vj + 1]) * tseg;
hitPos.Z = bestTile.data.verts[vj + 2] + (bestTile.data.verts[vi + 2] - bestTile.data.verts[vj + 2]) * tseg; hitPos.Z = bestTile.data.verts[vj + 2] + (bestTile.data.verts[vi + 2] - bestTile.data.verts[vj + 2]) * tseg;
hasBestV = true; hasBestV = true;
bestvj = new RcVec3f(bestTile.data.verts.AsSpan(vj)); bestvj = RcVecUtils.Create(bestTile.data.verts, vj);
bestvi = new RcVec3f(bestTile.data.verts.AsSpan(vi)); bestvi = RcVecUtils.Create(bestTile.data.verts, vi);
} }
for (int i = bestTile.polyLinks[bestPoly.index]; i != DtNavMesh.DT_NULL_LINK; i = bestTile.links[i].next) for (int i = bestTile.polyLinks[bestPoly.index]; i != DtNavMesh.DT_NULL_LINK; i = bestTile.links[i].next)

View File

@ -309,8 +309,8 @@ namespace DotRecast.Detour
public static float DistancePtSegSqr2D(RcVec3f pt, float[] verts, int p, int q, out float t) public static float DistancePtSegSqr2D(RcVec3f pt, float[] verts, int p, int q, out float t)
{ {
var vp = new RcVec3f(verts.AsSpan(p)); var vp = RcVecUtils.Create(verts, p);
var vq = new RcVec3f(verts.AsSpan(q)); var vq = RcVecUtils.Create(verts, q);
return DistancePtSegSqr2D(pt, vp, vq, out t); return DistancePtSegSqr2D(pt, vp, vq, out t);
} }

View File

@ -234,7 +234,7 @@ public class CrowdSampleTool : ISampleTool
dd.Vertex(prev.X, prev.Y + 0.1f, prev.Z, DuRGBA(0, 0, 0, (int)(128 * preva))); dd.Vertex(prev.X, prev.Y + 0.1f, prev.Z, DuRGBA(0, 0, 0, (int)(128 * preva)));
dd.Vertex(trail.trail[v], trail.trail[v + 1] + 0.1f, trail.trail[v + 2], DuRGBA(0, 0, 0, (int)(128 * a))); dd.Vertex(trail.trail[v], trail.trail[v + 1] + 0.1f, trail.trail[v + 2], DuRGBA(0, 0, 0, (int)(128 * a)));
preva = a; preva = a;
prev = new RcVec3f(trail.trail.AsSpan(v)); prev = RcVecUtils.Create(trail.trail, v);
} }
dd.End(); dd.End();

View File

@ -57,8 +57,8 @@ namespace DotRecast.Recast.Toolset.Geom
this.faces = faces; this.faces = faces;
normals = new float[faces.Length]; normals = new float[faces.Length];
CalculateNormals(); CalculateNormals();
bmin = new RcVec3f(vertices); bmin = RcVecUtils.Create(vertices);
bmax = new RcVec3f(vertices); bmax = RcVecUtils.Create(vertices);
for (int i = 1; i < vertices.Length / 3; i++) for (int i = 1; i < vertices.Length / 3; i++)
{ {
bmin = RcVecUtils.Min(bmin, vertices, i * 3); bmin = RcVecUtils.Min(bmin, vertices, i * 3);

View File

@ -77,8 +77,8 @@ namespace DotRecast.Recast.Geom
this.faces = faces; this.faces = faces;
normals = new float[faces.Length]; normals = new float[faces.Length];
CalculateNormals(); CalculateNormals();
bmin = new RcVec3f(vertices); bmin = RcVecUtils.Create(vertices);
bmax = new RcVec3f(vertices); bmax = RcVecUtils.Create(vertices);
for (int i = 1; i < vertices.Length / 3; i++) for (int i = 1; i < vertices.Length / 3; i++)
{ {
bmin = RcVecUtils.Min(bmin, vertices, i * 3); bmin = RcVecUtils.Min(bmin, vertices, i * 3);

View File

@ -457,8 +457,8 @@ namespace DotRecast.Recast
int zStride = xSize; // For readability int zStride = xSize; // For readability
// Compute the bounding box of the polygon // Compute the bounding box of the polygon
RcVec3f bmin = new RcVec3f(verts); RcVec3f bmin = RcVecUtils.Create(verts);
RcVec3f bmax = new RcVec3f(verts); RcVec3f bmax = RcVecUtils.Create(verts);
for (int i = 3; i < verts.Length; i += 3) for (int i = 3; i < verts.Length; i += 3)
{ {
bmin = RcVecUtils.Min(bmin, verts, i); bmin = RcVecUtils.Min(bmin, verts, i);
@ -753,9 +753,9 @@ namespace DotRecast.Recast
int vertIndexB = vertIndex; int vertIndexB = vertIndex;
int vertIndexC = (vertIndex + 1) % numVerts; int vertIndexC = (vertIndex + 1) % numVerts;
RcVec3f vertA = new RcVec3f(verts.AsSpan(vertIndexA * 3)); RcVec3f vertA = RcVecUtils.Create(verts, vertIndexA * 3);
RcVec3f vertB = new RcVec3f(verts.AsSpan(vertIndexB * 3)); RcVec3f vertB = RcVecUtils.Create(verts, vertIndexB * 3);
RcVec3f vertC = new RcVec3f(verts.AsSpan(vertIndexC * 3)); RcVec3f vertC = RcVecUtils.Create(verts, vertIndexC * 3);
// From A to B on the x/z plane // From A to B on the x/z plane
RcVec3f prevSegmentDir = RcVec3f.Subtract(vertB, vertA); RcVec3f prevSegmentDir = RcVec3f.Subtract(vertB, vertA);

View File

@ -178,7 +178,7 @@ namespace DotRecast.Recast
return true; return true;
} }
c = new RcVec3f(verts.AsSpan(p1)); c = RcVecUtils.Create(verts, p1);
r.Exchange(0f); r.Exchange(0f);
return false; return false;
} }
@ -1000,8 +1000,8 @@ namespace DotRecast.Recast
if (sampleDist > 0) if (sampleDist > 0)
{ {
// Create sample locations in a grid. // Create sample locations in a grid.
RcVec3f bmin = new RcVec3f(@in); RcVec3f bmin = RcVecUtils.Create(@in);
RcVec3f bmax = new RcVec3f(@in); RcVec3f bmax = RcVecUtils.Create(@in);
for (int i = 1; i < nin; ++i) for (int i = 1; i < nin; ++i)
{ {
bmin = RcVecUtils.Min(bmin, @in, i * 3); bmin = RcVecUtils.Min(bmin, @in, i * 3);

View File

@ -237,8 +237,8 @@ namespace DotRecast.Recast
float by = heightfieldBBMax.Y - heightfieldBBMin.Y; float by = heightfieldBBMax.Y - heightfieldBBMin.Y;
// Calculate the bounding box of the triangle. // Calculate the bounding box of the triangle.
RcVec3f tmin = new RcVec3f(verts.AsSpan(v0 * 3)); RcVec3f tmin = RcVecUtils.Create(verts, v0 * 3);
RcVec3f tmax = new RcVec3f(verts.AsSpan(v0 * 3)); RcVec3f tmax = RcVecUtils.Create(verts, v0 * 3);
tmin = RcVecUtils.Min(tmin, verts, v1 * 3); tmin = RcVecUtils.Min(tmin, verts, v1 * 3);
tmin = RcVecUtils.Min(tmin, verts, v2 * 3); tmin = RcVecUtils.Min(tmin, verts, v2 * 3);
tmax = RcVecUtils.Max(tmax, verts, v1 * 3); tmax = RcVecUtils.Max(tmax, verts, v1 * 3);