Skip to content

Commit

Permalink
Changed to consolidate vector-related functions into one place.
Browse files Browse the repository at this point in the history
  • Loading branch information
ikpil committed Jun 19, 2024
1 parent 5911510 commit 0092761
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 66 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Changed `heights`, `areas`, `cons`, and `regs` arrays to byte arrays for uniformity and efficiency in `DtTileCacheLayer`
- Changed `reg`, `area` arrays to byte arrays for uniformity and efficiency in `DtTileCacheContour`
- Changed `RcChunkyTriMesh` to separate the function and variable.
- Changed to consolidate vector-related functions into one place.

### Removed
- Removed RcMeshDetails.VdistSq2(float[], float[])
Expand Down
48 changes: 48 additions & 0 deletions src/DotRecast.Core/Numerics/RcVecUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,54 @@ public static float Get(this RcVec3f v, int i)
}
}

public static float Dot2(RcVec3f a, RcVec3f b)
{
return a.X * b.X + a.Z * b.Z;
}


public static float DistSq2(float[] verts, int p, int q)
{
float dx = verts[q + 0] - verts[p + 0];
float dy = verts[q + 2] - verts[p + 2];
return dx * dx + dy * dy;
}

public static float Dist2(float[] verts, int p, int q)
{
return MathF.Sqrt(DistSq2(verts, p, q));
}

public static float DistSq2(RcVec3f p, RcVec3f q)
{
float dx = q.X - p.X;
float dy = q.Z - p.Z;
return dx * dx + dy * dy;
}

public static float Dist2(RcVec3f p, RcVec3f q)
{
return MathF.Sqrt(DistSq2(p, q));
}

public static float Cross2(float[] verts, int p1, int p2, int p3)
{
float u1 = verts[p2 + 0] - verts[p1 + 0];
float v1 = verts[p2 + 2] - verts[p1 + 2];
float u2 = verts[p3 + 0] - verts[p1 + 0];
float v2 = verts[p3 + 2] - verts[p1 + 2];
return u1 * v2 - v1 * u2;
}

public static float Cross2(RcVec3f p1, RcVec3f p2, RcVec3f p3)
{
float u1 = p2.X - p1.X;
float v1 = p2.Z - p1.Z;
float u2 = p3.X - p1.X;
float v2 = p3.Z - p1.Z;
return u1 * v2 - v1 * u2;
}

/// Derives the dot product of two vectors on the xz-plane. (@p u . @p v)
/// @param[in] u A vector [(x, y, z)]
/// @param[in] v A vector [(x, y, z)]
Expand Down
85 changes: 19 additions & 66 deletions src/DotRecast.Recast/RcMeshDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,60 +27,13 @@ 3. This notice may not be removed or altered from any source distribution.
namespace DotRecast.Recast
{
using static RcRecast;
using static RcVecUtils;
using static EdgeValues;

public static class RcMeshDetails
{
public const int RC_UNSET_HEIGHT = RC_SPAN_MAX_HEIGHT;

public static float Vdot2(RcVec3f a, RcVec3f b)
{
return a.X * b.X + a.Z * b.Z;
}


public static float VdistSq2(float[] verts, int p, int q)
{
float dx = verts[q + 0] - verts[p + 0];
float dy = verts[q + 2] - verts[p + 2];
return dx * dx + dy * dy;
}

public static float Vdist2(float[] verts, int p, int q)
{
return MathF.Sqrt(VdistSq2(verts, p, q));
}

public static float VdistSq2(RcVec3f p, RcVec3f q)
{
float dx = q.X - p.X;
float dy = q.Z - p.Z;
return dx * dx + dy * dy;
}

public static float Vdist2(RcVec3f p, RcVec3f q)
{
return MathF.Sqrt(VdistSq2(p, q));
}

public static float Vcross2(float[] verts, int p1, int p2, int p3)
{
float u1 = verts[p2 + 0] - verts[p1 + 0];
float v1 = verts[p2 + 2] - verts[p1 + 2];
float u2 = verts[p3 + 0] - verts[p1 + 0];
float v2 = verts[p3 + 2] - verts[p1 + 2];
return u1 * v2 - v1 * u2;
}

public static float Vcross2(RcVec3f p1, RcVec3f p2, RcVec3f p3)
{
float u1 = p2.X - p1.X;
float v1 = p2.Z - p1.Z;
float u2 = p3.X - p1.X;
float v2 = p3.Z - p1.Z;
return u1 * v2 - v1 * u2;
}


public static bool CircumCircle(RcVec3f p1, RcVec3f p2, RcVec3f p3, ref RcVec3f c, out float r)
{
Expand All @@ -90,16 +43,16 @@ public static bool CircumCircle(RcVec3f p1, RcVec3f p2, RcVec3f p3, ref RcVec3f
var v2 = p2 - p1;
var v3 = p3 - p1;

float cp = Vcross2(v1, v2, v3);
float cp = Cross2(v1, v2, v3);
if (MathF.Abs(cp) > EPS)
{
float v1Sq = Vdot2(v1, v1);
float v2Sq = Vdot2(v2, v2);
float v3Sq = Vdot2(v3, v3);
float v1Sq = Dot2(v1, v1);
float v2Sq = Dot2(v2, v2);
float v3Sq = Dot2(v3, v3);
c.X = (v1Sq * (v2.Z - v3.Z) + v2Sq * (v3.Z - v1.Z) + v3Sq * (v1.Z - v2.Z)) / (2 * cp);
c.Y = 0;
c.Z = (v1Sq * (v3.X - v2.X) + v2Sq * (v1.X - v3.X) + v3Sq * (v2.X - v1.X)) / (2 * cp);
r = Vdist2(c, v1);
r = Dist2(c, v1);
c = c + p1;
return true;
}
Expand All @@ -115,11 +68,11 @@ public static float DistPtTri(RcVec3f p, RcVec3f a, RcVec3f b, RcVec3f c)
var v1 = b - a;
var v2 = p - a;

float dot00 = Vdot2(v0, v0);
float dot01 = Vdot2(v0, v1);
float dot02 = Vdot2(v0, v2);
float dot11 = Vdot2(v1, v1);
float dot12 = Vdot2(v1, v2);
float dot00 = Dot2(v0, v0);
float dot01 = Dot2(v0, v1);
float dot02 = Dot2(v0, v2);
float dot11 = Dot2(v1, v1);
float dot12 = Dot2(v1, v2);

// Compute barycentric coordinates
float invDenom = 1.0f / (dot00 * dot11 - dot01 * dot01);
Expand Down Expand Up @@ -397,11 +350,11 @@ public static void UpdateLeftFace(List<int> edges, int e, int s, int t, int f)

public static bool OverlapSegSeg2d(float[] verts, int a, int b, int c, int d)
{
float a1 = Vcross2(verts, a, b, d);
float a2 = Vcross2(verts, a, b, c);
float a1 = Cross2(verts, a, b, d);
float a2 = Cross2(verts, a, b, c);
if (a1 * a2 < 0.0f)
{
float a3 = Vcross2(verts, c, d, a);
float a3 = Cross2(verts, c, d, a);
float a4 = a3 + a2 - a1;
if (a3 * a4 < 0.0f)
{
Expand Down Expand Up @@ -472,7 +425,7 @@ public static int CompleteFacet(RcContext ctx, float[] pts, int npts, List<int>
RcVec3f vt = RcVecUtils.Create(pts, t * 3);
RcVec3f vu = RcVecUtils.Create(pts, u * 3);

if (Vcross2(vs, vt, vu) > EPS)
if (Cross2(vs, vt, vu) > EPS)
{
if (r < 0)
{
Expand All @@ -482,7 +435,7 @@ public static int CompleteFacet(RcContext ctx, float[] pts, int npts, List<int>
continue;
}

float d = Vdist2(c, vu);
float d = Dist2(c, vu);
float tol = 0.001f;
if (d > r * (1 + tol))
{
Expand Down Expand Up @@ -696,7 +649,7 @@ public static void TriangulateHull(int nverts, float[] verts, int nhull, int[] h
int pv = hull[pi] * 3;
int cv = hull[i] * 3;
int nv = hull[ni] * 3;
float d = Vdist2(verts, pv, cv) + Vdist2(verts, cv, nv) + Vdist2(verts, nv, pv);
float d = Dist2(verts, pv, cv) + Dist2(verts, cv, nv) + Dist2(verts, nv, pv);
if (d < dmin)
{
start = i;
Expand Down Expand Up @@ -726,8 +679,8 @@ public static void TriangulateHull(int nverts, float[] verts, int nhull, int[] h
int nvleft = hull[nleft] * 3;
int cvright = hull[right] * 3;
int nvright = hull[nright] * 3;
float dleft = Vdist2(verts, cvleft, nvleft) + Vdist2(verts, nvleft, cvright);
float dright = Vdist2(verts, cvright, nvright) + Vdist2(verts, cvleft, nvright);
float dleft = Dist2(verts, cvleft, nvleft) + Dist2(verts, nvleft, cvright);
float dright = Dist2(verts, cvright, nvright) + Dist2(verts, cvleft, nvright);

if (dleft < dright)
{
Expand Down

0 comments on commit 0092761

Please sign in to comment.