Debian Codegraph @

debian/patches/0004-Improved-variable-naming-in-RecastArea.cpp-636.patch

raw
1 From 8a42fb634849505f536cff1716e9e11ce11cc88c Mon Sep 17 00:00:00 2001
2 From: Graham Pentheny <[email protected]>
3 Date: Sun, 23 Jul 2023 15:59:20 -0400
4 Subject: [PATCH 04/36] Improved variable naming in RecastArea.cpp (#636)
5
6 * Cleanup compact heightfield functions in RecastArea.cpp
7 * More improved variable names for RecastArea.cpp
8 * Improved variable names and documentation in rcOffsetPoly
9 * Don't normalize the miter bisector in rcOffsetPoly since this needs to be proportional to both segment normals.
10 * Moved vector normalization out of rcOffsetPoly into a helper function rcVsafeNormalize
11 * Rename local variables in rcOffsetPoly
12 * Rename BMiterX/Z to cornerMIterX/Z
13 * Also fixed some comment descriptions
14 * Added docstring for rcVsafeNormalize
15 * Improved clarity of a few comments
16 ---
17 Recast/Include/Recast.h | 118 ++--
18 Recast/Source/RecastArea.cpp | 955 +++++++++++++++-----------
19 RecastDemo/Source/Sample_SoloMesh.cpp | 4 +-
20 3 files changed, 618 insertions(+), 459 deletions(-)
21
22 diff --git a/Recast/Include/Recast.h b/Recast/Include/Recast.h
23 index 9def8fd..1107b85 100644
24 --- a/Recast/Include/Recast.h
25 +++ b/Recast/Include/Recast.h
26 @@ -669,7 +669,7 @@ template<class T> inline T rcAbs(T a) { return a < 0 ? -a : a; }
27 /// Returns the square of the value.
28 /// @param[in] a The value.
29 /// @return The square of the value.
30 -template<class T> inline T rcSqr(T a) { return a*a; }
31 +template<class T> inline T rcSqr(T a) { return a * a; }
32
33 /// Clamps the value to the specified range.
34 /// @param[in] value The value to clamp.
35 @@ -1085,66 +1085,98 @@ int rcGetHeightFieldSpanCount(rcContext* context, const rcHeightfield& heightfie
36 bool rcBuildCompactHeightfield(rcContext* context, int walkableHeight, int walkableClimb,
37 const rcHeightfield& heightfield, rcCompactHeightfield& compactHeightfield);
38
39 -/// Erodes the walkable area within the heightfield by the specified radius.
40 +/// Erodes the walkable area within the heightfield by the specified radius.
41 +///
42 +/// Basically, any spans that are closer to a boundary or obstruction than the specified radius
43 +/// are marked as un-walkable.
44 +///
45 +/// This method is usually called immediately after the heightfield has been built.
46 +///
47 +/// @see rcCompactHeightfield, rcBuildCompactHeightfield, rcConfig::walkableRadius
48 /// @ingroup recast
49 -/// @param[in,out] ctx The build context to use during the operation.
50 -/// @param[in] radius The radius of erosion. [Limits: 0 < value < 255] [Units: vx]
51 -/// @param[in,out] chf The populated compact heightfield to erode.
52 +///
53 +/// @param[in,out] context The build context to use during the operation.
54 +/// @param[in] erosionRadius The radius of erosion. [Limits: 0 < value < 255] [Units: vx]
55 +/// @param[in,out] compactHeightfield The populated compact heightfield to erode.
56 /// @returns True if the operation completed successfully.
57 -bool rcErodeWalkableArea(rcContext* ctx, int radius, rcCompactHeightfield& chf);
58 +bool rcErodeWalkableArea(rcContext* context, int erosionRadius, rcCompactHeightfield& compactHeightfield);
59
60 /// Applies a median filter to walkable area types (based on area id), removing noise.
61 +///
62 +/// This filter is usually applied after applying area id's using functions
63 +/// such as #rcMarkBoxArea, #rcMarkConvexPolyArea, and #rcMarkCylinderArea.
64 +///
65 +/// @see rcCompactHeightfield
66 /// @ingroup recast
67 -/// @param[in,out] ctx The build context to use during the operation.
68 -/// @param[in,out] chf A populated compact heightfield.
69 +///
70 +/// @param[in,out] context The build context to use during the operation.
71 +/// @param[in,out] compactHeightfield A populated compact heightfield.
72 /// @returns True if the operation completed successfully.
73 -bool rcMedianFilterWalkableArea(rcContext* ctx, rcCompactHeightfield& chf);
74 +bool rcMedianFilterWalkableArea(rcContext* context, rcCompactHeightfield& compactHeightfield);
75
76 /// Applies an area id to all spans within the specified bounding box. (AABB)
77 +///
78 +/// @see rcCompactHeightfield, rcMedianFilterWalkableArea
79 /// @ingroup recast
80 -/// @param[in,out] ctx The build context to use during the operation.
81 -/// @param[in] bmin The minimum of the bounding box. [(x, y, z)]
82 -/// @param[in] bmax The maximum of the bounding box. [(x, y, z)]
83 -/// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
84 -/// @param[in,out] chf A populated compact heightfield.
85 -void rcMarkBoxArea(rcContext* ctx, const float* bmin, const float* bmax, unsigned char areaId,
86 - rcCompactHeightfield& chf);
87 +///
88 +/// @param[in,out] context The build context to use during the operation.
89 +/// @param[in] boxMinBounds The minimum extents of the bounding box. [(x, y, z)] [Units: wu]
90 +/// @param[in] boxMaxBounds The maximum extents of the bounding box. [(x, y, z)] [Units: wu]
91 +/// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
92 +/// @param[in,out] compactHeightfield A populated compact heightfield.
93 +void rcMarkBoxArea(rcContext* context, const float* boxMinBounds, const float* boxMaxBounds, unsigned char areaId,
94 + rcCompactHeightfield& compactHeightfield);
95
96 /// Applies the area id to the all spans within the specified convex polygon.
97 +///
98 +/// The value of spacial parameters are in world units.
99 +///
100 +/// The y-values of the polygon vertices are ignored. So the polygon is effectively
101 +/// projected onto the xz-plane, translated to @p minY, and extruded to @p maxY.
102 +///
103 +/// @see rcCompactHeightfield, rcMedianFilterWalkableArea
104 /// @ingroup recast
105 -/// @param[in,out] ctx The build context to use during the operation.
106 -/// @param[in] verts The vertices of the polygon [Fomr: (x, y, z) * @p nverts]
107 -/// @param[in] nverts The number of vertices in the polygon.
108 -/// @param[in] hmin The height of the base of the polygon.
109 -/// @param[in] hmax The height of the top of the polygon.
110 -/// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
111 -/// @param[in,out] chf A populated compact heightfield.
112 -void rcMarkConvexPolyArea(rcContext* ctx, const float* verts, const int nverts,
113 - const float hmin, const float hmax, unsigned char areaId,
114 - rcCompactHeightfield& chf);
115 -
116 -/// Helper function to offset voncex polygons for rcMarkConvexPolyArea.
117 +///
118 +/// @param[in,out] context The build context to use during the operation.
119 +/// @param[in] verts The vertices of the polygon [For: (x, y, z) * @p numVerts]
120 +/// @param[in] numVerts The number of vertices in the polygon.
121 +/// @param[in] minY The height of the base of the polygon. [Units: wu]
122 +/// @param[in] maxY The height of the top of the polygon. [Units: wu]
123 +/// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
124 +/// @param[in,out] compactHeightfield A populated compact heightfield.
125 +void rcMarkConvexPolyArea(rcContext* context, const float* verts, int numVerts,
126 + float minY, float maxY, unsigned char areaId,
127 + rcCompactHeightfield& compactHeightfield);
128 +
129 +/// Expands a convex polygon along its vertex normals by the given offset amount.
130 +/// Inserts extra vertices to bevel sharp corners.
131 +///
132 +/// Helper function to offset convex polygons for rcMarkConvexPolyArea.
133 +///
134 /// @ingroup recast
135 -/// @param[in] verts The vertices of the polygon [Form: (x, y, z) * @p nverts]
136 -/// @param[in] nverts The number of vertices in the polygon.
137 +///
138 +/// @param[in] verts The vertices of the polygon [Form: (x, y, z) * @p numVerts]
139 +/// @param[in] numVerts The number of vertices in the polygon.
140 /// @param[in] offset How much to offset the polygon by. [Units: wu]
141 -/// @param[out] outVerts The offset vertices (should hold up to 2 * @p nverts) [Form: (x, y, z) * return value]
142 +/// @param[out] outVerts The offset vertices (should hold up to 2 * @p numVerts) [Form: (x, y, z) * return value]
143 /// @param[in] maxOutVerts The max number of vertices that can be stored to @p outVerts.
144 /// @returns Number of vertices in the offset polygon or 0 if too few vertices in @p outVerts.
145 -int rcOffsetPoly(const float* verts, const int nverts, const float offset,
146 - float* outVerts, const int maxOutVerts);
147 +int rcOffsetPoly(const float* verts, int numVerts, float offset, float* outVerts, int maxOutVerts);
148
149 -/// Applies the area id to all spans within the specified cylinder.
150 +/// Applies the area id to all spans within the specified y-axis-aligned cylinder.
151 +///
152 +/// @see rcCompactHeightfield, rcMedianFilterWalkableArea
153 +///
154 /// @ingroup recast
155 -/// @param[in,out] ctx The build context to use during the operation.
156 -/// @param[in] pos The center of the base of the cylinder. [Form: (x, y, z)]
157 -/// @param[in] r The radius of the cylinder.
158 -/// @param[in] h The height of the cylinder.
159 -/// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
160 -/// @param[in,out] chf A populated compact heightfield.
161 -void rcMarkCylinderArea(rcContext* ctx, const float* pos,
162 - const float r, const float h, unsigned char areaId,
163 - rcCompactHeightfield& chf);
164 +///
165 +/// @param[in,out] context The build context to use during the operation.
166 +/// @param[in] position The center of the base of the cylinder. [Form: (x, y, z)] [Units: wu]
167 +/// @param[in] radius The radius of the cylinder. [Units: wu] [Limit: > 0]
168 +/// @param[in] height The height of the cylinder. [Units: wu] [Limit: > 0]
169 +/// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
170 +/// @param[in,out] compactHeightfield A populated compact heightfield.
171 +void rcMarkCylinderArea(rcContext* context, const float* position, float radius, float height,
172 + unsigned char areaId, rcCompactHeightfield& compactHeightfield);
173
174 /// Builds the distance field for the specified compact heightfield.
175 /// @ingroup recast
176 diff --git a/Recast/Source/RecastArea.cpp b/Recast/Source/RecastArea.cpp
177 index 45406dc..7a7091c 100644
178 --- a/Recast/Source/RecastArea.cpp
179 +++ b/Recast/Source/RecastArea.cpp
180 @@ -16,573 +16,700 @@
181 // 3. This notice may not be removed or altered from any source distribution.
182 //
183
184 -#include <float.h>
185 -#include <math.h>
186 -#include <string.h>
187 -#include <stdlib.h>
188 -#include <stdio.h>
189 #include "Recast.h"
190 #include "RecastAlloc.h"
191 #include "RecastAssert.h"
192
193 -/// @par
194 -///
195 -/// Basically, any spans that are closer to a boundary or obstruction than the specified radius
196 -/// are marked as unwalkable.
197 +#include <string.h> // for memcpy and memset
198 +
199 +/// Sorts the given data in-place using insertion sort.
200 ///
201 -/// This method is usually called immediately after the heightfield has been built.
202 +/// @param data The data to sort
203 +/// @param dataLength The number of elements in @p data
204 +static void insertSort(unsigned char* data, const int dataLength)
205 +{
206 + for (int valueIndex = 1; valueIndex < dataLength; valueIndex++)
207 + {
208 + const unsigned char value = data[valueIndex];
209 + int insertionIndex;
210 + for (insertionIndex = valueIndex - 1; insertionIndex >= 0 && data[insertionIndex] > value; insertionIndex--)
211 + {
212 + // Shift over values
213 + data[insertionIndex + 1] = data[insertionIndex];
214 + }
215 +
216 + // Insert the value in sorted order.
217 + data[insertionIndex + 1] = value;
218 + }
219 +}
220 +
221 +// TODO (graham): This is duplicated in the ConvexVolumeTool in RecastDemo
222 +/// Checks if a point is contained within a polygon
223 ///
224 -/// @see rcCompactHeightfield, rcBuildCompactHeightfield, rcConfig::walkableRadius
225 -bool rcErodeWalkableArea(rcContext* ctx, int radius, rcCompactHeightfield& chf)
226 +/// @param[in] numVerts Number of vertices in the polygon
227 +/// @param[in] verts The polygon vertices
228 +/// @param[in] point The point to check
229 +/// @returns true if the point lies within the polygon, false otherwise.
230 +static bool pointInPoly(int numVerts, const float* verts, const float* point)
231 {
232 - rcAssert(ctx);
233 -
234 - const int w = chf.width;
235 - const int h = chf.height;
236 -
237 - rcScopedTimer timer(ctx, RC_TIMER_ERODE_AREA);
238 -
239 - unsigned char* dist = (unsigned char*)rcAlloc(sizeof(unsigned char)*chf.spanCount, RC_ALLOC_TEMP);
240 - if (!dist)
241 + bool inPoly = false;
242 + for (int i = 0, j = numVerts - 1; i < numVerts; j = i++)
243 + {
244 + const float* vi = &verts[i * 3];
245 + const float* vj = &verts[j * 3];
246 +
247 + if ((vi[2] > point[2]) == (vj[2] > point[2]))
248 + {
249 + continue;
250 + }
251 +
252 + if (point[0] >= (vj[0] - vi[0]) * (point[2] - vi[2]) / (vj[2] - vi[2]) + vi[0])
253 + {
254 + continue;
255 + }
256 + inPoly = !inPoly;
257 + }
258 + return inPoly;
259 +}
260 +
261 +bool rcErodeWalkableArea(rcContext* context, const int erosionRadius, rcCompactHeightfield& compactHeightfield)
262 +{
263 + rcAssert(context != NULL);
264 +
265 + const int xSize = compactHeightfield.width;
266 + const int zSize = compactHeightfield.height;
267 + const int& zStride = xSize; // For readability
268 +
269 + rcScopedTimer timer(context, RC_TIMER_ERODE_AREA);
270 +
271 + unsigned char* distanceToBoundary = (unsigned char*)rcAlloc(sizeof(unsigned char) * compactHeightfield.spanCount,
272 + RC_ALLOC_TEMP);
273 + if (!distanceToBoundary)
274 {
275 - ctx->log(RC_LOG_ERROR, "erodeWalkableArea: Out of memory 'dist' (%d).", chf.spanCount);
276 + context->log(RC_LOG_ERROR, "erodeWalkableArea: Out of memory 'dist' (%d).", compactHeightfield.spanCount);
277 return false;
278 }
279 -
280 - // Init distance.
281 - memset(dist, 0xff, sizeof(unsigned char)*chf.spanCount);
282 + memset(distanceToBoundary, 0xff, sizeof(unsigned char) * compactHeightfield.spanCount);
283
284 // Mark boundary cells.
285 - for (int y = 0; y < h; ++y)
286 + for (int z = 0; z < zSize; ++z)
287 {
288 - for (int x = 0; x < w; ++x)
289 + for (int x = 0; x < xSize; ++x)
290 {
291 - const rcCompactCell& c = chf.cells[x+y*w];
292 - for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
293 + const rcCompactCell& cell = compactHeightfield.cells[x + z * zStride];
294 + for (int spanIndex = (int)cell.index, maxSpanIndex = (int)(cell.index + cell.count); spanIndex < maxSpanIndex; ++spanIndex)
295 {
296 - if (chf.areas[i] == RC_NULL_AREA)
297 + if (compactHeightfield.areas[spanIndex] == RC_NULL_AREA)
298 {
299 - dist[i] = 0;
300 + distanceToBoundary[spanIndex] = 0;
301 + continue;
302 }
303 - else
304 + const rcCompactSpan& span = compactHeightfield.spans[spanIndex];
305 +
306 + // Check that there is a non-null adjacent span in each of the 4 cardinal directions.
307 + int neighborCount = 0;
308 + for (int direction = 0; direction < 4; ++direction)
309 {
310 - const rcCompactSpan& s = chf.spans[i];
311 - int nc = 0;
312 - for (int dir = 0; dir < 4; ++dir)
313 + const int neighborConnection = rcGetCon(span, direction);
314 + if (neighborConnection == RC_NOT_CONNECTED)
315 {
316 - if (rcGetCon(s, dir) != RC_NOT_CONNECTED)
317 - {
318 - const int nx = x + rcGetDirOffsetX(dir);
319 - const int ny = y + rcGetDirOffsetY(dir);
320 - const int nidx = (int)chf.cells[nx+ny*w].index + rcGetCon(s, dir);
321 - if (chf.areas[nidx] != RC_NULL_AREA)
322 - {
323 - nc++;
324 - }
325 - }
326 + break;
327 }
328 - // At least one missing neighbour.
329 - if (nc != 4)
330 - dist[i] = 0;
331 +
332 + const int neighborX = x + rcGetDirOffsetX(direction);
333 + const int neighborZ = z + rcGetDirOffsetY(direction);
334 + const int neighborSpanIndex = (int)compactHeightfield.cells[neighborX + neighborZ * zStride].index + neighborConnection;
335 +
336 + if (compactHeightfield.areas[neighborSpanIndex] == RC_NULL_AREA)
337 + {
338 + break;
339 + }
340 + neighborCount++;
341 + }
342 +
343 + // At least one missing neighbour, so this is a boundary cell.
344 + if (neighborCount != 4)
345 + {
346 + distanceToBoundary[spanIndex] = 0;
347 }
348 }
349 }
350 }
351
352 - unsigned char nd;
353 + unsigned char newDistance;
354
355 // Pass 1
356 - for (int y = 0; y < h; ++y)
357 + for (int z = 0; z < zSize; ++z)
358 {
359 - for (int x = 0; x < w; ++x)
360 + for (int x = 0; x < xSize; ++x)
361 {
362 - const rcCompactCell& c = chf.cells[x+y*w];
363 - for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
364 + const rcCompactCell& cell = compactHeightfield.cells[x + z * zStride];
365 + const int maxSpanIndex = (int)(cell.index + cell.count);
366 + for (int spanIndex = (int)cell.index; spanIndex < maxSpanIndex; ++spanIndex)
367 {
368 - const rcCompactSpan& s = chf.spans[i];
369 -
370 - if (rcGetCon(s, 0) != RC_NOT_CONNECTED)
371 + const rcCompactSpan& span = compactHeightfield.spans[spanIndex];
372 +
373 + if (rcGetCon(span, 0) != RC_NOT_CONNECTED)
374 {
375 // (-1,0)
376 - const int ax = x + rcGetDirOffsetX(0);
377 - const int ay = y + rcGetDirOffsetY(0);
378 - const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 0);
379 - const rcCompactSpan& as = chf.spans[ai];
380 - nd = (unsigned char)rcMin((int)dist[ai]+2, 255);
381 - if (nd < dist[i])
382 - dist[i] = nd;
383 -
384 + const int aX = x + rcGetDirOffsetX(0);
385 + const int aY = z + rcGetDirOffsetY(0);
386 + const int aIndex = (int)compactHeightfield.cells[aX + aY * xSize].index + rcGetCon(span, 0);
387 + const rcCompactSpan& aSpan = compactHeightfield.spans[aIndex];
388 + newDistance = (unsigned char)rcMin((int)distanceToBoundary[aIndex] + 2, 255);
389 + if (newDistance < distanceToBoundary[spanIndex])
390 + {
391 + distanceToBoundary[spanIndex] = newDistance;
392 + }
393 +
394 // (-1,-1)
395 - if (rcGetCon(as, 3) != RC_NOT_CONNECTED)
396 + if (rcGetCon(aSpan, 3) != RC_NOT_CONNECTED)
397 {
398 - const int aax = ax + rcGetDirOffsetX(3);
399 - const int aay = ay + rcGetDirOffsetY(3);
400 - const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 3);
401 - nd = (unsigned char)rcMin((int)dist[aai]+3, 255);
402 - if (nd < dist[i])
403 - dist[i] = nd;
404 + const int bX = aX + rcGetDirOffsetX(3);
405 + const int bY = aY + rcGetDirOffsetY(3);
406 + const int bIndex = (int)compactHeightfield.cells[bX + bY * xSize].index + rcGetCon(aSpan, 3);
407 + newDistance = (unsigned char)rcMin((int)distanceToBoundary[bIndex] + 3, 255);
408 + if (newDistance < distanceToBoundary[spanIndex])
409 + {
410 + distanceToBoundary[spanIndex] = newDistance;
411 + }
412 }
413 }
414 - if (rcGetCon(s, 3) != RC_NOT_CONNECTED)
415 + if (rcGetCon(span, 3) != RC_NOT_CONNECTED)
416 {
417 // (0,-1)
418 - const int ax = x + rcGetDirOffsetX(3);
419 - const int ay = y + rcGetDirOffsetY(3);
420 - const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 3);
421 - const rcCompactSpan& as = chf.spans[ai];
422 - nd = (unsigned char)rcMin((int)dist[ai]+2, 255);
423 - if (nd < dist[i])
424 - dist[i] = nd;
425 -
426 + const int aX = x + rcGetDirOffsetX(3);
427 + const int aY = z + rcGetDirOffsetY(3);
428 + const int aIndex = (int)compactHeightfield.cells[aX + aY * xSize].index + rcGetCon(span, 3);
429 + const rcCompactSpan& aSpan = compactHeightfield.spans[aIndex];
430 + newDistance = (unsigned char)rcMin((int)distanceToBoundary[aIndex] + 2, 255);
431 + if (newDistance < distanceToBoundary[spanIndex])
432 + {
433 + distanceToBoundary[spanIndex] = newDistance;
434 + }
435 +
436 // (1,-1)
437 - if (rcGetCon(as, 2) != RC_NOT_CONNECTED)
438 + if (rcGetCon(aSpan, 2) != RC_NOT_CONNECTED)
439 {
440 - const int aax = ax + rcGetDirOffsetX(2);
441 - const int aay = ay + rcGetDirOffsetY(2);
442 - const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 2);
443 - nd = (unsigned char)rcMin((int)dist[aai]+3, 255);
444 - if (nd < dist[i])
445 - dist[i] = nd;
446 + const int bX = aX + rcGetDirOffsetX(2);
447 + const int bY = aY + rcGetDirOffsetY(2);
448 + const int bIndex = (int)compactHeightfield.cells[bX + bY * xSize].index + rcGetCon(aSpan, 2);
449 + newDistance = (unsigned char)rcMin((int)distanceToBoundary[bIndex] + 3, 255);
450 + if (newDistance < distanceToBoundary[spanIndex])
451 + {
452 + distanceToBoundary[spanIndex] = newDistance;
453 + }
454 }
455 }
456 }
457 }
458 }
459 -
460 +
461 // Pass 2
462 - for (int y = h-1; y >= 0; --y)
463 + for (int z = zSize - 1; z >= 0; --z)
464 {
465 - for (int x = w-1; x >= 0; --x)
466 + for (int x = xSize - 1; x >= 0; --x)
467 {
468 - const rcCompactCell& c = chf.cells[x+y*w];
469 - for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
470 + const rcCompactCell& cell = compactHeightfield.cells[x + z * zStride];
471 + const int maxSpanIndex = (int)(cell.index + cell.count);
472 + for (int spanIndex = (int)cell.index; spanIndex < maxSpanIndex; ++spanIndex)
473 {
474 - const rcCompactSpan& s = chf.spans[i];
475 -
476 - if (rcGetCon(s, 2) != RC_NOT_CONNECTED)
477 + const rcCompactSpan& span = compactHeightfield.spans[spanIndex];
478 +
479 + if (rcGetCon(span, 2) != RC_NOT_CONNECTED)
480 {
481 // (1,0)
482 - const int ax = x + rcGetDirOffsetX(2);
483 - const int ay = y + rcGetDirOffsetY(2);
484 - const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 2);
485 - const rcCompactSpan& as = chf.spans[ai];
486 - nd = (unsigned char)rcMin((int)dist[ai]+2, 255);
487 - if (nd < dist[i])
488 - dist[i] = nd;
489 -
490 + const int aX = x + rcGetDirOffsetX(2);
491 + const int aY = z + rcGetDirOffsetY(2);
492 + const int aIndex = (int)compactHeightfield.cells[aX + aY * xSize].index + rcGetCon(span, 2);
493 + const rcCompactSpan& aSpan = compactHeightfield.spans[aIndex];
494 + newDistance = (unsigned char)rcMin((int)distanceToBoundary[aIndex] + 2, 255);
495 + if (newDistance < distanceToBoundary[spanIndex])
496 + {
497 + distanceToBoundary[spanIndex] = newDistance;
498 + }
499 +
500 // (1,1)
501 - if (rcGetCon(as, 1) != RC_NOT_CONNECTED)
502 + if (rcGetCon(aSpan, 1) != RC_NOT_CONNECTED)
503 {
504 - const int aax = ax + rcGetDirOffsetX(1);
505 - const int aay = ay + rcGetDirOffsetY(1);
506 - const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 1);
507 - nd = (unsigned char)rcMin((int)dist[aai]+3, 255);
508 - if (nd < dist[i])
509 - dist[i] = nd;
510 + const int bX = aX + rcGetDirOffsetX(1);
511 + const int bY = aY + rcGetDirOffsetY(1);
512 + const int bIndex = (int)compactHeightfield.cells[bX + bY * xSize].index + rcGetCon(aSpan, 1);
513 + newDistance = (unsigned char)rcMin((int)distanceToBoundary[bIndex] + 3, 255);
514 + if (newDistance < distanceToBoundary[spanIndex])
515 + {
516 + distanceToBoundary[spanIndex] = newDistance;
517 + }
518 }
519 }
520 - if (rcGetCon(s, 1) != RC_NOT_CONNECTED)
521 + if (rcGetCon(span, 1) != RC_NOT_CONNECTED)
522 {
523 // (0,1)
524 - const int ax = x + rcGetDirOffsetX(1);
525 - const int ay = y + rcGetDirOffsetY(1);
526 - const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 1);
527 - const rcCompactSpan& as = chf.spans[ai];
528 - nd = (unsigned char)rcMin((int)dist[ai]+2, 255);
529 - if (nd < dist[i])
530 - dist[i] = nd;
531 -
532 + const int aX = x + rcGetDirOffsetX(1);
533 + const int aY = z + rcGetDirOffsetY(1);
534 + const int aIndex = (int)compactHeightfield.cells[aX + aY * xSize].index + rcGetCon(span, 1);
535 + const rcCompactSpan& aSpan = compactHeightfield.spans[aIndex];
536 + newDistance = (unsigned char)rcMin((int)distanceToBoundary[aIndex] + 2, 255);
537 + if (newDistance < distanceToBoundary[spanIndex])
538 + {
539 + distanceToBoundary[spanIndex] = newDistance;
540 + }
541 +
542 // (-1,1)
543 - if (rcGetCon(as, 0) != RC_NOT_CONNECTED)
544 + if (rcGetCon(aSpan, 0) != RC_NOT_CONNECTED)
545 {
546 - const int aax = ax + rcGetDirOffsetX(0);
547 - const int aay = ay + rcGetDirOffsetY(0);
548 - const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 0);
549 - nd = (unsigned char)rcMin((int)dist[aai]+3, 255);
550 - if (nd < dist[i])
551 - dist[i] = nd;
552 + const int bX = aX + rcGetDirOffsetX(0);
553 + const int bY = aY + rcGetDirOffsetY(0);
554 + const int bIndex = (int)compactHeightfield.cells[bX + bY * xSize].index + rcGetCon(aSpan, 0);
555 + newDistance = (unsigned char)rcMin((int)distanceToBoundary[bIndex] + 3, 255);
556 + if (newDistance < distanceToBoundary[spanIndex])
557 + {
558 + distanceToBoundary[spanIndex] = newDistance;
559 + }
560 }
561 }
562 }
563 }
564 }
565 -
566 - const unsigned char thr = (unsigned char)(radius*2);
567 - for (int i = 0; i < chf.spanCount; ++i)
568 - if (dist[i] < thr)
569 - chf.areas[i] = RC_NULL_AREA;
570 -
571 - rcFree(dist);
572 -
573 - return true;
574 -}
575
576 -static void insertSort(unsigned char* a, const int n)
577 -{
578 - int i, j;
579 - for (i = 1; i < n; i++)
580 + const unsigned char minBoundaryDistance = (unsigned char)(erosionRadius * 2);
581 + for (int spanIndex = 0; spanIndex < compactHeightfield.spanCount; ++spanIndex)
582 {
583 - const unsigned char value = a[i];
584 - for (j = i - 1; j >= 0 && a[j] > value; j--)
585 - a[j+1] = a[j];
586 - a[j+1] = value;
587 + if (distanceToBoundary[spanIndex] < minBoundaryDistance)
588 + {
589 + compactHeightfield.areas[spanIndex] = RC_NULL_AREA;
590 + }
591 }
592 +
593 + rcFree(distanceToBoundary);
594 +
595 + return true;
596 }
597
598 -/// @par
599 -///
600 -/// This filter is usually applied after applying area id's using functions
601 -/// such as #rcMarkBoxArea, #rcMarkConvexPolyArea, and #rcMarkCylinderArea.
602 -///
603 -/// @see rcCompactHeightfield
604 -bool rcMedianFilterWalkableArea(rcContext* ctx, rcCompactHeightfield& chf)
605 +bool rcMedianFilterWalkableArea(rcContext* context, rcCompactHeightfield& compactHeightfield)
606 {
607 - rcAssert(ctx);
608 -
609 - const int w = chf.width;
610 - const int h = chf.height;
611 + rcAssert(context);
612
613 - rcScopedTimer timer(ctx, RC_TIMER_MEDIAN_AREA);
614 -
615 - unsigned char* areas = (unsigned char*)rcAlloc(sizeof(unsigned char)*chf.spanCount, RC_ALLOC_TEMP);
616 + const int xSize = compactHeightfield.width;
617 + const int zSize = compactHeightfield.height;
618 + const int zStride = xSize; // For readability
619 +
620 + rcScopedTimer timer(context, RC_TIMER_MEDIAN_AREA);
621 +
622 + unsigned char* areas = (unsigned char*)rcAlloc(sizeof(unsigned char) * compactHeightfield.spanCount, RC_ALLOC_TEMP);
623 if (!areas)
624 {
625 - ctx->log(RC_LOG_ERROR, "medianFilterWalkableArea: Out of memory 'areas' (%d).", chf.spanCount);
626 + context->log(RC_LOG_ERROR, "medianFilterWalkableArea: Out of memory 'areas' (%d).",
627 + compactHeightfield.spanCount);
628 return false;
629 }
630 -
631 - // Init distance.
632 - memset(areas, 0xff, sizeof(unsigned char)*chf.spanCount);
633 -
634 - for (int y = 0; y < h; ++y)
635 + memset(areas, 0xff, sizeof(unsigned char) * compactHeightfield.spanCount);
636 +
637 + for (int z = 0; z < zSize; ++z)
638 {
639 - for (int x = 0; x < w; ++x)
640 + for (int x = 0; x < xSize; ++x)
641 {
642 - const rcCompactCell& c = chf.cells[x+y*w];
643 - for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
644 + const rcCompactCell& cell = compactHeightfield.cells[x + z * zStride];
645 + const int maxSpanIndex = (int)(cell.index + cell.count);
646 + for (int spanIndex = (int)cell.index; spanIndex < maxSpanIndex; ++spanIndex)
647 {
648 - const rcCompactSpan& s = chf.spans[i];
649 - if (chf.areas[i] == RC_NULL_AREA)
650 + const rcCompactSpan& span = compactHeightfield.spans[spanIndex];
651 + if (compactHeightfield.areas[spanIndex] == RC_NULL_AREA)
652 {
653 - areas[i] = chf.areas[i];
654 + areas[spanIndex] = compactHeightfield.areas[spanIndex];
655 continue;
656 }
657 -
658 - unsigned char nei[9];
659 - for (int j = 0; j < 9; ++j)
660 - nei[j] = chf.areas[i];
661 -
662 +
663 + unsigned char neighborAreas[9];
664 + for (int neighborIndex = 0; neighborIndex < 9; ++neighborIndex)
665 + {
666 + neighborAreas[neighborIndex] = compactHeightfield.areas[spanIndex];
667 + }
668 +
669 for (int dir = 0; dir < 4; ++dir)
670 {
671 - if (rcGetCon(s, dir) != RC_NOT_CONNECTED)
672 + if (rcGetCon(span, dir) == RC_NOT_CONNECTED)
673 + {
674 + continue;
675 + }
676 +
677 + const int aX = x + rcGetDirOffsetX(dir);
678 + const int aZ = z + rcGetDirOffsetY(dir);
679 + const int aIndex = (int)compactHeightfield.cells[aX + aZ * zStride].index + rcGetCon(span, dir);
680 + if (compactHeightfield.areas[aIndex] != RC_NULL_AREA)
681 + {
682 + neighborAreas[dir * 2 + 0] = compactHeightfield.areas[aIndex];
683 + }
684 +
685 + const rcCompactSpan& aSpan = compactHeightfield.spans[aIndex];
686 + const int dir2 = (dir + 1) & 0x3;
687 + const int neighborConnection2 = rcGetCon(aSpan, dir2);
688 + if (neighborConnection2 != RC_NOT_CONNECTED)
689 {
690 - const int ax = x + rcGetDirOffsetX(dir);
691 - const int ay = y + rcGetDirOffsetY(dir);
692 - const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, dir);
693 - if (chf.areas[ai] != RC_NULL_AREA)
694 - nei[dir*2+0] = chf.areas[ai];
695 -
696 - const rcCompactSpan& as = chf.spans[ai];
697 - const int dir2 = (dir+1) & 0x3;
698 - if (rcGetCon(as, dir2) != RC_NOT_CONNECTED)
699 + const int bX = aX + rcGetDirOffsetX(dir2);
700 + const int bZ = aZ + rcGetDirOffsetY(dir2);
701 + const int bIndex = (int)compactHeightfield.cells[bX + bZ * zStride].index + neighborConnection2;
702 + if (compactHeightfield.areas[bIndex] != RC_NULL_AREA)
703 {
704 - const int ax2 = ax + rcGetDirOffsetX(dir2);
705 - const int ay2 = ay + rcGetDirOffsetY(dir2);
706 - const int ai2 = (int)chf.cells[ax2+ay2*w].index + rcGetCon(as, dir2);
707 - if (chf.areas[ai2] != RC_NULL_AREA)
708 - nei[dir*2+1] = chf.areas[ai2];
709 + neighborAreas[dir * 2 + 1] = compactHeightfield.areas[bIndex];
710 }
711 }
712 }
713 - insertSort(nei, 9);
714 - areas[i] = nei[4];
715 + insertSort(neighborAreas, 9);
716 + areas[spanIndex] = neighborAreas[4];
717 }
718 }
719 }
720 -
721 - memcpy(chf.areas, areas, sizeof(unsigned char)*chf.spanCount);
722 -
723 +
724 + memcpy(compactHeightfield.areas, areas, sizeof(unsigned char) * compactHeightfield.spanCount);
725 +
726 rcFree(areas);
727 -
728 +
729 return true;
730 }
731
732 -/// @par
733 -///
734 -/// The value of spacial parameters are in world units.
735 -///
736 -/// @see rcCompactHeightfield, rcMedianFilterWalkableArea
737 -void rcMarkBoxArea(rcContext* ctx, const float* bmin, const float* bmax, unsigned char areaId,
738 - rcCompactHeightfield& chf)
739 +void rcMarkBoxArea(rcContext* context, const float* boxMinBounds, const float* boxMaxBounds, unsigned char areaId,
740 + rcCompactHeightfield& compactHeightfield)
741 {
742 - rcAssert(ctx);
743 -
744 - rcScopedTimer timer(ctx, RC_TIMER_MARK_BOX_AREA);
745 -
746 - int minx = (int)((bmin[0]-chf.bmin[0])/chf.cs);
747 - int miny = (int)((bmin[1]-chf.bmin[1])/chf.ch);
748 - int minz = (int)((bmin[2]-chf.bmin[2])/chf.cs);
749 - int maxx = (int)((bmax[0]-chf.bmin[0])/chf.cs);
750 - int maxy = (int)((bmax[1]-chf.bmin[1])/chf.ch);
751 - int maxz = (int)((bmax[2]-chf.bmin[2])/chf.cs);
752 -
753 - if (maxx < 0) return;
754 - if (minx >= chf.width) return;
755 - if (maxz < 0) return;
756 - if (minz >= chf.height) return;
757 -
758 - if (minx < 0) minx = 0;
759 - if (maxx >= chf.width) maxx = chf.width-1;
760 - if (minz < 0) minz = 0;
761 - if (maxz >= chf.height) maxz = chf.height-1;
762 -
763 - for (int z = minz; z <= maxz; ++z)
764 + rcAssert(context);
765 +
766 + rcScopedTimer timer(context, RC_TIMER_MARK_BOX_AREA);
767 +
768 + const int xSize = compactHeightfield.width;
769 + const int zSize = compactHeightfield.height;
770 + const int zStride = xSize; // For readability
771 +
772 + // Find the footprint of the box area in grid cell coordinates.
773 + int minX = (int)((boxMinBounds[0] - compactHeightfield.bmin[0]) / compactHeightfield.cs);
774 + int minY = (int)((boxMinBounds[1] - compactHeightfield.bmin[1]) / compactHeightfield.ch);
775 + int minZ = (int)((boxMinBounds[2] - compactHeightfield.bmin[2]) / compactHeightfield.cs);
776 + int maxX = (int)((boxMaxBounds[0] - compactHeightfield.bmin[0]) / compactHeightfield.cs);
777 + int maxY = (int)((boxMaxBounds[1] - compactHeightfield.bmin[1]) / compactHeightfield.ch);
778 + int maxZ = (int)((boxMaxBounds[2] - compactHeightfield.bmin[2]) / compactHeightfield.cs);
779 +
780 + // Early-out if the box is outside the bounds of the grid.
781 + if (maxX < 0) { return; }
782 + if (minX >= xSize) { return; }
783 + if (maxZ < 0) { return; }
784 + if (minZ >= zSize) { return; }
785 +
786 + // Clamp relevant bound coordinates to the grid.
787 + if (minX < 0) { minX = 0; }
788 + if (maxX >= xSize) { maxX = xSize - 1; }
789 + if (minZ < 0) { minZ = 0; }
790 + if (maxZ >= zSize) { maxZ = zSize - 1; }
791 +
792 + // Mark relevant cells.
793 + for (int z = minZ; z <= maxZ; ++z)
794 {
795 - for (int x = minx; x <= maxx; ++x)
796 + for (int x = minX; x <= maxX; ++x)
797 {
798 - const rcCompactCell& c = chf.cells[x+z*chf.width];
799 - for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
800 + const rcCompactCell& cell = compactHeightfield.cells[x + z * zStride];
801 + const int maxSpanIndex = (int)(cell.index + cell.count);
802 + for (int spanIndex = (int)cell.index; spanIndex < maxSpanIndex; ++spanIndex)
803 {
804 - rcCompactSpan& s = chf.spans[i];
805 - if ((int)s.y >= miny && (int)s.y <= maxy)
806 + rcCompactSpan& span = compactHeightfield.spans[spanIndex];
807 +
808 + // Skip if the span is outside the box extents.
809 + if ((int)span.y < minY || (int)span.y > maxY)
810 {
811 - if (chf.areas[i] != RC_NULL_AREA)
812 - chf.areas[i] = areaId;
813 + continue;
814 }
815 +
816 + // Skip if the span has been removed.
817 + if (compactHeightfield.areas[spanIndex] == RC_NULL_AREA)
818 + {
819 + continue;
820 + }
821 +
822 + // Mark the span.
823 + compactHeightfield.areas[spanIndex] = areaId;
824 }
825 }
826 }
827 }
828
829 -
830 -static int pointInPoly(int nvert, const float* verts, const float* p)
831 +void rcMarkConvexPolyArea(rcContext* context, const float* verts, const int numVerts,
832 + const float minY, const float maxY, unsigned char areaId,
833 + rcCompactHeightfield& compactHeightfield)
834 {
835 - int i, j, c = 0;
836 - for (i = 0, j = nvert-1; i < nvert; j = i++)
837 - {
838 - const float* vi = &verts[i*3];
839 - const float* vj = &verts[j*3];
840 - if (((vi[2] > p[2]) != (vj[2] > p[2])) &&
841 - (p[0] < (vj[0]-vi[0]) * (p[2]-vi[2]) / (vj[2]-vi[2]) + vi[0]) )
842 - c = !c;
843 - }
844 - return c;
845 -}
846 + rcAssert(context);
847
848 -/// @par
849 -///
850 -/// The value of spacial parameters are in world units.
851 -///
852 -/// The y-values of the polygon vertices are ignored. So the polygon is effectively
853 -/// projected onto the xz-plane at @p hmin, then extruded to @p hmax.
854 -///
855 -/// @see rcCompactHeightfield, rcMedianFilterWalkableArea
856 -void rcMarkConvexPolyArea(rcContext* ctx, const float* verts, const int nverts,
857 - const float hmin, const float hmax, unsigned char areaId,
858 - rcCompactHeightfield& chf)
859 -{
860 - rcAssert(ctx);
861 -
862 - rcScopedTimer timer(ctx, RC_TIMER_MARK_CONVEXPOLY_AREA);
863 + rcScopedTimer timer(context, RC_TIMER_MARK_CONVEXPOLY_AREA);
864
865 - float bmin[3], bmax[3];
866 + const int xSize = compactHeightfield.width;
867 + const int zSize = compactHeightfield.height;
868 + const int zStride = xSize; // For readability
869 +
870 + // Compute the bounding box of the polygon
871 + float bmin[3];
872 + float bmax[3];
873 rcVcopy(bmin, verts);
874 rcVcopy(bmax, verts);
875 - for (int i = 1; i < nverts; ++i)
876 + for (int i = 1; i < numVerts; ++i)
877 {
878 - rcVmin(bmin, &verts[i*3]);
879 - rcVmax(bmax, &verts[i*3]);
880 + rcVmin(bmin, &verts[i * 3]);
881 + rcVmax(bmax, &verts[i * 3]);
882 }
883 - bmin[1] = hmin;
884 - bmax[1] = hmax;
885 -
886 - int minx = (int)((bmin[0]-chf.bmin[0])/chf.cs);
887 - int miny = (int)((bmin[1]-chf.bmin[1])/chf.ch);
888 - int minz = (int)((bmin[2]-chf.bmin[2])/chf.cs);
889 - int maxx = (int)((bmax[0]-chf.bmin[0])/chf.cs);
890 - int maxy = (int)((bmax[1]-chf.bmin[1])/chf.ch);
891 - int maxz = (int)((bmax[2]-chf.bmin[2])/chf.cs);
892 -
893 - if (maxx < 0) return;
894 - if (minx >= chf.width) return;
895 - if (maxz < 0) return;
896 - if (minz >= chf.height) return;
897 -
898 - if (minx < 0) minx = 0;
899 - if (maxx >= chf.width) maxx = chf.width-1;
900 - if (minz < 0) minz = 0;
901 - if (maxz >= chf.height) maxz = chf.height-1;
902 -
903 -
904 + bmin[1] = minY;
905 + bmax[1] = maxY;
906 +
907 + // Compute the grid footprint of the polygon
908 + int minx = (int)((bmin[0] - compactHeightfield.bmin[0]) / compactHeightfield.cs);
909 + int miny = (int)((bmin[1] - compactHeightfield.bmin[1]) / compactHeightfield.ch);
910 + int minz = (int)((bmin[2] - compactHeightfield.bmin[2]) / compactHeightfield.cs);
911 + int maxx = (int)((bmax[0] - compactHeightfield.bmin[0]) / compactHeightfield.cs);
912 + int maxy = (int)((bmax[1] - compactHeightfield.bmin[1]) / compactHeightfield.ch);
913 + int maxz = (int)((bmax[2] - compactHeightfield.bmin[2]) / compactHeightfield.cs);
914 +
915 + // Early-out if the polygon lies entirely outside the grid.
916 + if (maxx < 0) { return; }
917 + if (minx >= xSize) { return; }
918 + if (maxz < 0) { return; }
919 + if (minz >= zSize) { return; }
920 +
921 + // Clamp the polygon footprint to the grid
922 + if (minx < 0) { minx = 0; }
923 + if (maxx >= xSize) { maxx = xSize - 1; }
924 + if (minz < 0) { minz = 0; }
925 + if (maxz >= zSize) { maxz = zSize - 1; }
926 +
927 // TODO: Optimize.
928 for (int z = minz; z <= maxz; ++z)
929 {
930 for (int x = minx; x <= maxx; ++x)
931 {
932 - const rcCompactCell& c = chf.cells[x+z*chf.width];
933 - for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
934 + const rcCompactCell& cell = compactHeightfield.cells[x + z * zStride];
935 + const int maxSpanIndex = (int)(cell.index + cell.count);
936 + for (int spanIndex = (int)cell.index; spanIndex < maxSpanIndex; ++spanIndex)
937 {
938 - rcCompactSpan& s = chf.spans[i];
939 - if (chf.areas[i] == RC_NULL_AREA)
940 + rcCompactSpan& span = compactHeightfield.spans[spanIndex];
941 +
942 + // Skip if span is removed.
943 + if (compactHeightfield.areas[spanIndex] == RC_NULL_AREA)
944 + {
945 continue;
946 - if ((int)s.y >= miny && (int)s.y <= maxy)
947 + }
948 +
949 + // Skip if y extents don't overlap.
950 + if ((int)span.y < miny || (int)span.y > maxy)
951 {
952 - float p[3];
953 - p[0] = chf.bmin[0] + (x+0.5f)*chf.cs;
954 - p[1] = 0;
955 - p[2] = chf.bmin[2] + (z+0.5f)*chf.cs;
956 + continue;
957 + }
958
959 - if (pointInPoly(nverts, verts, p))
960 - {
961 - chf.areas[i] = areaId;
962 - }
963 + const float point[] = {
964 + compactHeightfield.bmin[0] + ((float)x + 0.5f) * compactHeightfield.cs,
965 + 0,
966 + compactHeightfield.bmin[2] + ((float)z + 0.5f) * compactHeightfield.cs
967 + };
968 +
969 + if (pointInPoly(numVerts, verts, point))
970 + {
971 + compactHeightfield.areas[spanIndex] = areaId;
972 }
973 }
974 }
975 }
976 }
977
978 -int rcOffsetPoly(const float* verts, const int nverts, const float offset,
979 - float* outVerts, const int maxOutVerts)
980 +static const float EPSILON = 1e-6f;
981 +
982 +/// Normalizes the vector if the length is greater than zero.
983 +/// If the magnitude is zero, the vector is unchanged.
984 +/// @param[in,out] v The vector to normalize. [(x, y, z)]
985 +static void rcVsafeNormalize(float* v)
986 {
987 - const float MITER_LIMIT = 1.20f;
988 + const float sqMag = rcSqr(v[0]) + rcSqr(v[1]) + rcSqr(v[2]);
989 + if (sqMag > EPSILON)
990 + {
991 + const float inverseMag = 1.0f / rcSqrt(sqMag);
992 + v[0] *= inverseMag;
993 + v[1] *= inverseMag;
994 + v[2] *= inverseMag;
995 + }
996 +}
997
998 - int n = 0;
999 +int rcOffsetPoly(const float* verts, const int numVerts, const float offset, float* outVerts, const int maxOutVerts)
1000 +{
1001 + // Defines the limit at which a miter becomes a bevel.
1002 + // Similar in behavior to https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-miterlimit
1003 + const float MITER_LIMIT = 1.20f;
1004 +
1005 + int numOutVerts = 0;
1006
1007 - for (int i = 0; i < nverts; i++)
1008 + for (int vertIndex = 0; vertIndex < numVerts; vertIndex++)
1009 {
1010 - const int a = (i+nverts-1) % nverts;
1011 - const int b = i;
1012 - const int c = (i+1) % nverts;
1013 - const float* va = &verts[a*3];
1014 - const float* vb = &verts[b*3];
1015 - const float* vc = &verts[c*3];
1016 - float dx0 = vb[0] - va[0];
1017 - float dy0 = vb[2] - va[2];
1018 - float d0 = dx0*dx0 + dy0*dy0;
1019 - if (d0 > 1e-6f)
1020 - {
1021 - d0 = 1.0f/rcSqrt(d0);
1022 - dx0 *= d0;
1023 - dy0 *= d0;
1024 - }
1025 - float dx1 = vc[0] - vb[0];
1026 - float dy1 = vc[2] - vb[2];
1027 - float d1 = dx1*dx1 + dy1*dy1;
1028 - if (d1 > 1e-6f)
1029 - {
1030 - d1 = 1.0f/rcSqrt(d1);
1031 - dx1 *= d1;
1032 - dy1 *= d1;
1033 - }
1034 - const float dlx0 = -dy0;
1035 - const float dly0 = dx0;
1036 - const float dlx1 = -dy1;
1037 - const float dly1 = dx1;
1038 - float cross = dx1*dy0 - dx0*dy1;
1039 - float dmx = (dlx0 + dlx1) * 0.5f;
1040 - float dmy = (dly0 + dly1) * 0.5f;
1041 - float dmr2 = dmx*dmx + dmy*dmy;
1042 - bool bevel = dmr2 * MITER_LIMIT*MITER_LIMIT < 1.0f;
1043 - if (dmr2 > 1e-6f)
1044 + // Grab three vertices of the polygon.
1045 + const int vertIndexA = (vertIndex + numVerts - 1) % numVerts;
1046 + const int vertIndexB = vertIndex;
1047 + const int vertIndexC = (vertIndex + 1) % numVerts;
1048 + const float* vertA = &verts[vertIndexA * 3];
1049 + const float* vertB = &verts[vertIndexB * 3];
1050 + const float* vertC = &verts[vertIndexC * 3];
1051 +
1052 + // From A to B on the x/z plane
1053 + float prevSegmentDir[3];
1054 + rcVsub(prevSegmentDir, vertB, vertA);
1055 + prevSegmentDir[1] = 0; // Squash onto x/z plane
1056 + rcVsafeNormalize(prevSegmentDir);
1057 +
1058 + // From B to C on the x/z plane
1059 + float currSegmentDir[3];
1060 + rcVsub(currSegmentDir, vertC, vertB);
1061 + currSegmentDir[1] = 0; // Squash onto x/z plane
1062 + rcVsafeNormalize(currSegmentDir);
1063 +
1064 + // The y component of the cross product of the two normalized segment directions.
1065 + // The X and Z components of the cross product are both zero because the two
1066 + // segment direction vectors fall within the x/z plane.
1067 + float cross = currSegmentDir[0] * prevSegmentDir[2] - prevSegmentDir[0] * currSegmentDir[2];
1068 +
1069 + // CCW perpendicular vector to AB. The segment normal.
1070 + const float prevSegmentNormX = -prevSegmentDir[2];
1071 + const float prevSegmentNormZ = prevSegmentDir[0];
1072 +
1073 + // CCW perpendicular vector to BC. The segment normal.
1074 + const float currSegmentNormX = -currSegmentDir[2];
1075 + const float currSegmentNormZ = currSegmentDir[0];
1076 +
1077 + // Average the two segment normals to get the proportional miter offset for B.
1078 + // This isn't normalized because it's defining the distance and direction the corner will need to be
1079 + // adjusted proportionally to the edge offsets to properly miter the adjoining edges.
1080 + float cornerMiterX = (prevSegmentNormX + currSegmentNormX) * 0.5f;
1081 + float cornerMiterZ = (prevSegmentNormZ + currSegmentNormZ) * 0.5f;
1082 + const float cornerMiterSqMag = rcSqr(cornerMiterX) + rcSqr(cornerMiterZ);
1083 +
1084 + // If the magnitude of the segment normal average is less than about .69444,
1085 + // the corner is an acute enough angle that the result should be beveled.
1086 + const bool bevel = cornerMiterSqMag * MITER_LIMIT * MITER_LIMIT < 1.0f;
1087 +
1088 + // Scale the corner miter so it's proportional to how much the corner should be offset compared to the edges.
1089 + if (cornerMiterSqMag > EPSILON)
1090 {
1091 - const float scale = 1.0f / dmr2;
1092 - dmx *= scale;
1093 - dmy *= scale;
1094 + const float scale = 1.0f / cornerMiterSqMag;
1095 + cornerMiterX *= scale;
1096 + cornerMiterZ *= scale;
1097 }
1098
1099 - if (bevel && cross < 0.0f)
1100 + if (bevel && cross < 0.0f) // If the corner is convex and an acute enough angle, generate a bevel.
1101 {
1102 - if (n+2 > maxOutVerts)
1103 + if (numOutVerts + 2 > maxOutVerts)
1104 + {
1105 return 0;
1106 - float d = (1.0f - (dx0*dx1 + dy0*dy1))*0.5f;
1107 - outVerts[n*3+0] = vb[0] + (-dlx0+dx0*d)*offset;
1108 - outVerts[n*3+1] = vb[1];
1109 - outVerts[n*3+2] = vb[2] + (-dly0+dy0*d)*offset;
1110 - n++;
1111 - outVerts[n*3+0] = vb[0] + (-dlx1-dx1*d)*offset;
1112 - outVerts[n*3+1] = vb[1];
1113 - outVerts[n*3+2] = vb[2] + (-dly1-dy1*d)*offset;
1114 - n++;
1115 + }
1116 +
1117 + // Generate two bevel vertices at a distances from B proportional to the angle between the two segments.
1118 + // Move each bevel vertex out proportional to the given offset.
1119 + float d = (1.0f - (prevSegmentDir[0] * currSegmentDir[0] + prevSegmentDir[2] * currSegmentDir[2])) * 0.5f;
1120 +
1121 + outVerts[numOutVerts * 3 + 0] = vertB[0] + (-prevSegmentNormX + prevSegmentDir[0] * d) * offset;
1122 + outVerts[numOutVerts * 3 + 1] = vertB[1];
1123 + outVerts[numOutVerts * 3 + 2] = vertB[2] + (-prevSegmentNormZ + prevSegmentDir[2] * d) * offset;
1124 + numOutVerts++;
1125 +
1126 + outVerts[numOutVerts * 3 + 0] = vertB[0] + (-currSegmentNormX - currSegmentDir[0] * d) * offset;
1127 + outVerts[numOutVerts * 3 + 1] = vertB[1];
1128 + outVerts[numOutVerts * 3 + 2] = vertB[2] + (-currSegmentNormZ - currSegmentDir[2] * d) * offset;
1129 + numOutVerts++;
1130 }
1131 else
1132 {
1133 - if (n+1 > maxOutVerts)
1134 + if (numOutVerts + 1 > maxOutVerts)
1135 + {
1136 return 0;
1137 - outVerts[n*3+0] = vb[0] - dmx*offset;
1138 - outVerts[n*3+1] = vb[1];
1139 - outVerts[n*3+2] = vb[2] - dmy*offset;
1140 - n++;
1141 + }
1142 +
1143 + // Move B along the miter direction by the specified offset.
1144 + outVerts[numOutVerts * 3 + 0] = vertB[0] - cornerMiterX * offset;
1145 + outVerts[numOutVerts * 3 + 1] = vertB[1];
1146 + outVerts[numOutVerts * 3 + 2] = vertB[2] - cornerMiterZ * offset;
1147 + numOutVerts++;
1148 }
1149 }
1150 -
1151 - return n;
1152 -}
1153
1154 + return numOutVerts;
1155 +}
1156
1157 -/// @par
1158 -///
1159 -/// The value of spacial parameters are in world units.
1160 -///
1161 -/// @see rcCompactHeightfield, rcMedianFilterWalkableArea
1162 -void rcMarkCylinderArea(rcContext* ctx, const float* pos,
1163 - const float r, const float h, unsigned char areaId,
1164 - rcCompactHeightfield& chf)
1165 +void rcMarkCylinderArea(rcContext* context, const float* position, const float radius, const float height,
1166 + unsigned char areaId, rcCompactHeightfield& compactHeightfield)
1167 {
1168 - rcAssert(ctx);
1169 -
1170 - rcScopedTimer timer(ctx, RC_TIMER_MARK_CYLINDER_AREA);
1171 -
1172 - float bmin[3], bmax[3];
1173 - bmin[0] = pos[0] - r;
1174 - bmin[1] = pos[1];
1175 - bmin[2] = pos[2] - r;
1176 - bmax[0] = pos[0] + r;
1177 - bmax[1] = pos[1] + h;
1178 - bmax[2] = pos[2] + r;
1179 - const float r2 = r*r;
1180 -
1181 - int minx = (int)((bmin[0]-chf.bmin[0])/chf.cs);
1182 - int miny = (int)((bmin[1]-chf.bmin[1])/chf.ch);
1183 - int minz = (int)((bmin[2]-chf.bmin[2])/chf.cs);
1184 - int maxx = (int)((bmax[0]-chf.bmin[0])/chf.cs);
1185 - int maxy = (int)((bmax[1]-chf.bmin[1])/chf.ch);
1186 - int maxz = (int)((bmax[2]-chf.bmin[2])/chf.cs);
1187 -
1188 - if (maxx < 0) return;
1189 - if (minx >= chf.width) return;
1190 - if (maxz < 0) return;
1191 - if (minz >= chf.height) return;
1192 -
1193 - if (minx < 0) minx = 0;
1194 - if (maxx >= chf.width) maxx = chf.width-1;
1195 - if (minz < 0) minz = 0;
1196 - if (maxz >= chf.height) maxz = chf.height-1;
1197 -
1198 -
1199 + rcAssert(context);
1200 +
1201 + rcScopedTimer timer(context, RC_TIMER_MARK_CYLINDER_AREA);
1202 +
1203 + const int xSize = compactHeightfield.width;
1204 + const int zSize = compactHeightfield.height;
1205 + const int zStride = xSize; // For readability
1206 +
1207 + // Compute the bounding box of the cylinder
1208 + const float cylinderBBMin[] =
1209 + {
1210 + position[0] - radius,
1211 + position[1],
1212 + position[2] - radius
1213 + };
1214 + const float cylinderBBMax[] =
1215 + {
1216 + position[0] + radius,
1217 + position[1] + height,
1218 + position[2] + radius
1219 + };
1220 +
1221 + // Compute the grid footprint of the cylinder
1222 + int minx = (int)((cylinderBBMin[0] - compactHeightfield.bmin[0]) / compactHeightfield.cs);
1223 + int miny = (int)((cylinderBBMin[1] - compactHeightfield.bmin[1]) / compactHeightfield.ch);
1224 + int minz = (int)((cylinderBBMin[2] - compactHeightfield.bmin[2]) / compactHeightfield.cs);
1225 + int maxx = (int)((cylinderBBMax[0] - compactHeightfield.bmin[0]) / compactHeightfield.cs);
1226 + int maxy = (int)((cylinderBBMax[1] - compactHeightfield.bmin[1]) / compactHeightfield.ch);
1227 + int maxz = (int)((cylinderBBMax[2] - compactHeightfield.bmin[2]) / compactHeightfield.cs);
1228 +
1229 + // Early-out if the cylinder is completely outside the grid bounds.
1230 + if (maxx < 0) { return; }
1231 + if (minx >= xSize) { return; }
1232 + if (maxz < 0) { return; }
1233 + if (minz >= zSize) { return; }
1234 +
1235 + // Clamp the cylinder bounds to the grid.
1236 + if (minx < 0) { minx = 0; }
1237 + if (maxx >= xSize) { maxx = xSize - 1; }
1238 + if (minz < 0) { minz = 0; }
1239 + if (maxz >= zSize) { maxz = zSize - 1; }
1240 +
1241 + const float radiusSq = radius * radius;
1242 +
1243 for (int z = minz; z <= maxz; ++z)
1244 {
1245 for (int x = minx; x <= maxx; ++x)
1246 {
1247 - const rcCompactCell& c = chf.cells[x+z*chf.width];
1248 - for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
1249 + const rcCompactCell& cell = compactHeightfield.cells[x + z * zStride];
1250 + const int maxSpanIndex = (int)(cell.index + cell.count);
1251 +
1252 + const float cellX = compactHeightfield.bmin[0] + ((float)x + 0.5f) * compactHeightfield.cs;
1253 + const float cellZ = compactHeightfield.bmin[2] + ((float)z + 0.5f) * compactHeightfield.cs;
1254 + const float deltaX = cellX - position[0];
1255 + const float deltaZ = cellZ - position[2];
1256 +
1257 + // Skip this column if it's too far from the center point of the cylinder.
1258 + if (rcSqr(deltaX) + rcSqr(deltaZ) >= radiusSq)
1259 + {
1260 + continue;
1261 + }
1262 +
1263 + // Mark all overlapping spans
1264 + for (int spanIndex = (int)cell.index; spanIndex < maxSpanIndex; ++spanIndex)
1265 {
1266 - rcCompactSpan& s = chf.spans[i];
1267 -
1268 - if (chf.areas[i] == RC_NULL_AREA)
1269 + rcCompactSpan& span = compactHeightfield.spans[spanIndex];
1270 +
1271 + // Skip if span is removed.
1272 + if (compactHeightfield.areas[spanIndex] == RC_NULL_AREA)
1273 + {
1274 continue;
1275 -
1276 - if ((int)s.y >= miny && (int)s.y <= maxy)
1277 + }
1278 +
1279 + // Mark if y extents overlap.
1280 + if ((int)span.y >= miny && (int)span.y <= maxy)
1281 {
1282 - const float sx = chf.bmin[0] + (x+0.5f)*chf.cs;
1283 - const float sz = chf.bmin[2] + (z+0.5f)*chf.cs;
1284 - const float dx = sx - pos[0];
1285 - const float dz = sz - pos[2];
1286 -
1287 - if (dx*dx + dz*dz < r2)
1288 - {
1289 - chf.areas[i] = areaId;
1290 - }
1291 + compactHeightfield.areas[spanIndex] = areaId;
1292 }
1293 }
1294 }
1295 diff --git a/RecastDemo/Source/Sample_SoloMesh.cpp b/RecastDemo/Source/Sample_SoloMesh.cpp
1296 index f9f7dc9..3375499 100644
1297 --- a/RecastDemo/Source/Sample_SoloMesh.cpp
1298 +++ b/RecastDemo/Source/Sample_SoloMesh.cpp
1299 @@ -466,10 +466,10 @@ bool Sample_SoloMesh::handleBuild()
1300 }
1301
1302 //
1303 - // Step 3. Filter walkables surfaces.
1304 + // Step 3. Filter walkable surfaces.
1305 //
1306
1307 - // Once all geoemtry is rasterized, we do initial pass of filtering to
1308 + // Once all geometry is rasterized, we do initial pass of filtering to
1309 // remove unwanted overhangs caused by the conservative rasterization
1310 // as well as filter spans where the character cannot possibly stand.
1311 if (m_filterLowHangingObstacles)
1312 --
1313 2.43.0
1314