| 1 |
From c393777d26d2ff6519ac23612abf8af42678c9dd Mon Sep 17 00:00:00 2001 |
| 2 |
From: Jakob Botsch Nielsen <[email protected]> |
| 3 |
Date: Wed, 20 Dec 2023 06:46:50 +0100 |
| 4 |
Subject: [PATCH 15/36] Make detail mesh edge detection more robust (#657) |
| 5 |
|
| 6 |
Instead of using a distance check which can fail at large magnitudes due |
| 7 |
to low precision we can check whether the edges are actually on the |
| 8 |
hull. |
| 9 |
--- |
| 10 |
Recast/Source/RecastMeshDetail.cpp | 64 ++++++++++++++++++------------ |
| 11 |
1 file changed, 38 insertions(+), 26 deletions(-) |
| 12 |
|
| 13 |
diff --git a/Recast/Source/RecastMeshDetail.cpp b/Recast/Source/RecastMeshDetail.cpp |
| 14 |
index 479037a..957245c 100644 |
| 15 |
--- a/Recast/Source/RecastMeshDetail.cpp |
| 16 |
+++ b/Recast/Source/RecastMeshDetail.cpp |
| 17 |
@@ -634,6 +634,40 @@ inline float getJitterY(const int i) |
| 18 |
return (((i * 0xd8163841) & 0xffff) / 65535.0f * 2.0f) - 1.0f; |
| 19 |
} |
| 20 |
|
| 21 |
+static bool onHull(int a, int b, int nhull, int* hull) |
| 22 |
+{ |
| 23 |
+ // All internal sampled points come after the hull so we can early out for those. |
| 24 |
+ if (a >= nhull || b >= nhull) |
| 25 |
+ return false; |
| 26 |
+ |
| 27 |
+ for (int j = nhull - 1, i = 0; i < nhull; j = i++) |
| 28 |
+ { |
| 29 |
+ if (a == hull[j] && b == hull[i]) |
| 30 |
+ return true; |
| 31 |
+ } |
| 32 |
+ |
| 33 |
+ return false; |
| 34 |
+} |
| 35 |
+ |
| 36 |
+// Find edges that lie on hull and mark them as such. |
| 37 |
+static void setTriFlags(rcIntArray& tris, int nhull, int* hull) |
| 38 |
+{ |
| 39 |
+ // Matches DT_DETAIL_EDGE_BOUNDARY |
| 40 |
+ const int DETAIL_EDGE_BOUNDARY = 0x1; |
| 41 |
+ |
| 42 |
+ for (int i = 0; i < tris.size(); i += 4) |
| 43 |
+ { |
| 44 |
+ int a = tris[i + 0]; |
| 45 |
+ int b = tris[i + 1]; |
| 46 |
+ int c = tris[i + 2]; |
| 47 |
+ unsigned short flags = 0; |
| 48 |
+ flags |= (onHull(a, b, nhull, hull) ? DETAIL_EDGE_BOUNDARY : 0) << 0; |
| 49 |
+ flags |= (onHull(b, c, nhull, hull) ? DETAIL_EDGE_BOUNDARY : 0) << 2; |
| 50 |
+ flags |= (onHull(c, a, nhull, hull) ? DETAIL_EDGE_BOUNDARY : 0) << 4; |
| 51 |
+ tris[i + 3] = (int)flags; |
| 52 |
+ } |
| 53 |
+} |
| 54 |
+ |
| 55 |
static bool buildPolyDetail(rcContext* ctx, const float* in, const int nin, |
| 56 |
const float sampleDist, const float sampleMaxError, |
| 57 |
const int heightSearchRadius, const rcCompactHeightfield& chf, |
| 58 |
@@ -771,6 +805,7 @@ static bool buildPolyDetail(rcContext* ctx, const float* in, const int nin, |
| 59 |
if (minExtent < sampleDist*2) |
| 60 |
{ |
| 61 |
triangulateHull(nverts, verts, nhull, hull, nin, tris); |
| 62 |
+ setTriFlags(tris, nhull, hull); |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
@@ -875,6 +910,8 @@ static bool buildPolyDetail(rcContext* ctx, const float* in, const int nin, |
| 67 |
tris.resize(MAX_TRIS*4); |
| 68 |
ctx->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Shrinking triangle count from %d to max %d.", ntris, MAX_TRIS); |
| 69 |
} |
| 70 |
+ |
| 71 |
+ setTriFlags(tris, nhull, hull); |
| 72 |
|
| 73 |
return true; |
| 74 |
} |
| 75 |
@@ -1137,31 +1174,6 @@ static void getHeightData(rcContext* ctx, const rcCompactHeightfield& chf, |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
-static unsigned char getEdgeFlags(const float* va, const float* vb, |
| 80 |
- const float* vpoly, const int npoly) |
| 81 |
-{ |
| 82 |
- // The flag returned by this function matches dtDetailTriEdgeFlags in Detour. |
| 83 |
- // Figure out if edge (va,vb) is part of the polygon boundary. |
| 84 |
- static const float thrSqr = rcSqr(0.001f); |
| 85 |
- for (int i = 0, j = npoly-1; i < npoly; j=i++) |
| 86 |
- { |
| 87 |
- if (distancePtSeg2d(va, &vpoly[j*3], &vpoly[i*3]) < thrSqr && |
| 88 |
- distancePtSeg2d(vb, &vpoly[j*3], &vpoly[i*3]) < thrSqr) |
| 89 |
- return 1; |
| 90 |
- } |
| 91 |
- return 0; |
| 92 |
-} |
| 93 |
- |
| 94 |
-static unsigned char getTriFlags(const float* va, const float* vb, const float* vc, |
| 95 |
- const float* vpoly, const int npoly) |
| 96 |
-{ |
| 97 |
- unsigned char flags = 0; |
| 98 |
- flags |= getEdgeFlags(va,vb,vpoly,npoly) << 0; |
| 99 |
- flags |= getEdgeFlags(vb,vc,vpoly,npoly) << 2; |
| 100 |
- flags |= getEdgeFlags(vc,va,vpoly,npoly) << 4; |
| 101 |
- return flags; |
| 102 |
-} |
| 103 |
- |
| 104 |
/// @par |
| 105 |
/// |
| 106 |
/// See the #rcConfig documentation for more information on the configuration parameters. |
| 107 |
@@ -1377,7 +1389,7 @@ bool rcBuildPolyMeshDetail(rcContext* ctx, const rcPolyMesh& mesh, const rcCompa |
| 108 |
dmesh.tris[dmesh.ntris*4+0] = (unsigned char)t[0]; |
| 109 |
dmesh.tris[dmesh.ntris*4+1] = (unsigned char)t[1]; |
| 110 |
dmesh.tris[dmesh.ntris*4+2] = (unsigned char)t[2]; |
| 111 |
- dmesh.tris[dmesh.ntris*4+3] = getTriFlags(&verts[t[0]*3], &verts[t[1]*3], &verts[t[2]*3], poly, npoly); |
| 112 |
+ dmesh.tris[dmesh.ntris*4+3] = (unsigned char)t[3]; |
| 113 |
dmesh.ntris++; |
| 114 |
} |
| 115 |
} |
| 116 |
-- |
| 117 |
2.43.0 |
| 118 |
|