Debian Codegraph @

Recast/Source/RecastArea.cpp

raw ◀ ▶
2 Debian patches touch this file
1 //
2 // Copyright (c) 2009-2010 Mikko Mononen [email protected]
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 // Permission is granted to anyone to use this software for any purpose,
8 // including commercial applications, and to alter it and redistribute it
9 // freely, subject to the following restrictions:
10 // 1. The origin of this software must not be misrepresented; you must not
11 // claim that you wrote the original software. If you use this software
12 // in a product, an acknowledgment in the product documentation would be
13 // appreciated but is not required.
14 // 2. Altered source versions must be plainly marked as such, and must not be
15 // misrepresented as being the original software.
16 // 3. This notice may not be removed or altered from any source distribution.
17 //
18
19 #include "Recast.h"
20 #include "RecastAlloc.h"
21 #include "RecastAssert.h"
22
•23 #include <string.h> // for memcpy and memset
24
25 /// Sorts the given data in-place using insertion sort.
26 ///
•27 /// @param data The data to sort
28 /// @param dataLength The number of elements in @p data
29 static void insertSort(unsigned char* data, const int dataLength)
30 {
31 for (int valueIndex = 1; valueIndex < dataLength; valueIndex++)
32 {
33 const unsigned char value = data[valueIndex];
34 int insertionIndex;
35 for (insertionIndex = valueIndex - 1; insertionIndex >= 0 && data[insertionIndex] > value; insertionIndex--)
36 {
37 // Shift over values
38 data[insertionIndex + 1] = data[insertionIndex];
39 }
40
41 // Insert the value in sorted order.
42 data[insertionIndex + 1] = value;
43 }
44 }
45
46 // TODO (graham): This is duplicated in the ConvexVolumeTool in RecastDemo
47 /// Checks if a point is contained within a polygon
48 ///
•49 /// @param[in] numVerts Number of vertices in the polygon
50 /// @param[in] verts The polygon vertices
51 /// @param[in] point The point to check
52 /// @returns true if the point lies within the polygon, false otherwise.
53 static bool pointInPoly(int numVerts, const float* verts, const float* point)
54 {
•55 bool inPoly = false;
56 for (int i = 0, j = numVerts - 1; i < numVerts; j = i++)
57 {
58 const float* vi = &verts[i * 3];
59 const float* vj = &verts[j * 3];
60
61 if ((vi[2] > point[2]) == (vj[2] > point[2]))
62 {
63 continue;
64 }
65
66 if (point[0] >= (vj[0] - vi[0]) * (point[2] - vi[2]) / (vj[2] - vi[2]) + vi[0])
67 {
68 continue;
69 }
70 inPoly = !inPoly;
71 }
72 return inPoly;
73 }
74
75 bool rcErodeWalkableArea(rcContext* context, const int erosionRadius, rcCompactHeightfield& compactHeightfield)
76 {
77 rcAssert(context != NULL);
78
79 const int xSize = compactHeightfield.width;
80 const int zSize = compactHeightfield.height;
81 const int& zStride = xSize; // For readability
82
83 rcScopedTimer timer(context, RC_TIMER_ERODE_AREA);
84
85 unsigned char* distanceToBoundary = (unsigned char*)rcAlloc(sizeof(unsigned char) * compactHeightfield.spanCount,
86 RC_ALLOC_TEMP);
87 if (!distanceToBoundary)
88 {
•89 context->log(RC_LOG_ERROR, "erodeWalkableArea: Out of memory 'dist' (%d).", compactHeightfield.spanCount);
90 return false;
91 }
•92 memset(distanceToBoundary, 0xff, sizeof(unsigned char) * compactHeightfield.spanCount);
93
94 // Mark boundary cells.
•95 for (int z = 0; z < zSize; ++z)
96 {
•97 for (int x = 0; x < xSize; ++x)
98 {
•99 const rcCompactCell& cell = compactHeightfield.cells[x + z * zStride];
100 for (int spanIndex = (int)cell.index, maxSpanIndex = (int)(cell.index + cell.count); spanIndex < maxSpanIndex; ++spanIndex)
101 {
•102 if (compactHeightfield.areas[spanIndex] == RC_NULL_AREA)
103 {
•104 distanceToBoundary[spanIndex] = 0;
105 continue;
106 }
•107 const rcCompactSpan& span = compactHeightfield.spans[spanIndex];
108
109 // Check that there is a non-null adjacent span in each of the 4 cardinal directions.
110 int neighborCount = 0;
111 for (int direction = 0; direction < 4; ++direction)
112 {
•113 const int neighborConnection = rcGetCon(span, direction);
114 if (neighborConnection == RC_NOT_CONNECTED)
115 {
•116 break;
117 }
•118
119 const int neighborX = x + rcGetDirOffsetX(direction);
120 const int neighborZ = z + rcGetDirOffsetY(direction);
121 const int neighborSpanIndex = (int)compactHeightfield.cells[neighborX + neighborZ * zStride].index + neighborConnection;
122
123 if (compactHeightfield.areas[neighborSpanIndex] == RC_NULL_AREA)
124 {
125 break;
126 }
127 neighborCount++;
128 }
129
130 // At least one missing neighbour, so this is a boundary cell.
131 if (neighborCount != 4)
132 {
133 distanceToBoundary[spanIndex] = 0;
134 }
135 }
136 }
137 }
138
•139 unsigned char newDistance;
140
141 // Pass 1
•142 for (int z = 0; z < zSize; ++z)
143 {
•144 for (int x = 0; x < xSize; ++x)
145 {
•146 const rcCompactCell& cell = compactHeightfield.cells[x + z * zStride];
147 const int maxSpanIndex = (int)(cell.index + cell.count);
148 for (int spanIndex = (int)cell.index; spanIndex < maxSpanIndex; ++spanIndex)
149 {
•150 const rcCompactSpan& span = compactHeightfield.spans[spanIndex];
151
152 if (rcGetCon(span, 0) != RC_NOT_CONNECTED)
153 {
154 // (-1,0)
•155 const int aX = x + rcGetDirOffsetX(0);
156 const int aY = z + rcGetDirOffsetY(0);
157 const int aIndex = (int)compactHeightfield.cells[aX + aY * xSize].index + rcGetCon(span, 0);
158 const rcCompactSpan& aSpan = compactHeightfield.spans[aIndex];
159 newDistance = (unsigned char)rcMin((int)distanceToBoundary[aIndex] + 2, 255);
160 if (newDistance < distanceToBoundary[spanIndex])
161 {
162 distanceToBoundary[spanIndex] = newDistance;
163 }
164
165 // (-1,-1)
•166 if (rcGetCon(aSpan, 3) != RC_NOT_CONNECTED)
167 {
•168 const int bX = aX + rcGetDirOffsetX(3);
169 const int bY = aY + rcGetDirOffsetY(3);
170 const int bIndex = (int)compactHeightfield.cells[bX + bY * xSize].index + rcGetCon(aSpan, 3);
171 newDistance = (unsigned char)rcMin((int)distanceToBoundary[bIndex] + 3, 255);
172 if (newDistance < distanceToBoundary[spanIndex])
173 {
174 distanceToBoundary[spanIndex] = newDistance;
175 }
176 }
177 }
•178 if (rcGetCon(span, 3) != RC_NOT_CONNECTED)
179 {
180 // (0,-1)
•181 const int aX = x + rcGetDirOffsetX(3);
182 const int aY = z + rcGetDirOffsetY(3);
183 const int aIndex = (int)compactHeightfield.cells[aX + aY * xSize].index + rcGetCon(span, 3);
184 const rcCompactSpan& aSpan = compactHeightfield.spans[aIndex];
185 newDistance = (unsigned char)rcMin((int)distanceToBoundary[aIndex] + 2, 255);
186 if (newDistance < distanceToBoundary[spanIndex])
187 {
188 distanceToBoundary[spanIndex] = newDistance;
189 }
190
191 // (1,-1)
•192 if (rcGetCon(aSpan, 2) != RC_NOT_CONNECTED)
193 {
•194 const int bX = aX + rcGetDirOffsetX(2);
195 const int bY = aY + rcGetDirOffsetY(2);
196 const int bIndex = (int)compactHeightfield.cells[bX + bY * xSize].index + rcGetCon(aSpan, 2);
197 newDistance = (unsigned char)rcMin((int)distanceToBoundary[bIndex] + 3, 255);
198 if (newDistance < distanceToBoundary[spanIndex])
199 {
200 distanceToBoundary[spanIndex] = newDistance;
201 }
202 }
203 }
204 }
205 }
206 }
•207
208 // Pass 2
•209 for (int z = zSize - 1; z >= 0; --z)
210 {
•211 for (int x = xSize - 1; x >= 0; --x)
212 {
•213 const rcCompactCell& cell = compactHeightfield.cells[x + z * zStride];
214 const int maxSpanIndex = (int)(cell.index + cell.count);
215 for (int spanIndex = (int)cell.index; spanIndex < maxSpanIndex; ++spanIndex)
216 {
•217 const rcCompactSpan& span = compactHeightfield.spans[spanIndex];
218
219 if (rcGetCon(span, 2) != RC_NOT_CONNECTED)
220 {
221 // (1,0)
•222 const int aX = x + rcGetDirOffsetX(2);
223 const int aY = z + rcGetDirOffsetY(2);
224 const int aIndex = (int)compactHeightfield.cells[aX + aY * xSize].index + rcGetCon(span, 2);
225 const rcCompactSpan& aSpan = compactHeightfield.spans[aIndex];
226 newDistance = (unsigned char)rcMin((int)distanceToBoundary[aIndex] + 2, 255);
227 if (newDistance < distanceToBoundary[spanIndex])
228 {
229 distanceToBoundary[spanIndex] = newDistance;
230 }
231
232 // (1,1)
•233 if (rcGetCon(aSpan, 1) != RC_NOT_CONNECTED)
234 {
•235 const int bX = aX + rcGetDirOffsetX(1);
236 const int bY = aY + rcGetDirOffsetY(1);
237 const int bIndex = (int)compactHeightfield.cells[bX + bY * xSize].index + rcGetCon(aSpan, 1);
238 newDistance = (unsigned char)rcMin((int)distanceToBoundary[bIndex] + 3, 255);
239 if (newDistance < distanceToBoundary[spanIndex])
240 {
241 distanceToBoundary[spanIndex] = newDistance;
242 }
243 }
244 }
•245 if (rcGetCon(span, 1) != RC_NOT_CONNECTED)
246 {
247 // (0,1)
•248 const int aX = x + rcGetDirOffsetX(1);
249 const int aY = z + rcGetDirOffsetY(1);
250 const int aIndex = (int)compactHeightfield.cells[aX + aY * xSize].index + rcGetCon(span, 1);
251 const rcCompactSpan& aSpan = compactHeightfield.spans[aIndex];
252 newDistance = (unsigned char)rcMin((int)distanceToBoundary[aIndex] + 2, 255);
253 if (newDistance < distanceToBoundary[spanIndex])
254 {
255 distanceToBoundary[spanIndex] = newDistance;
256 }
257
258 // (-1,1)
•259 if (rcGetCon(aSpan, 0) != RC_NOT_CONNECTED)
260 {
•261 const int bX = aX + rcGetDirOffsetX(0);
262 const int bY = aY + rcGetDirOffsetY(0);
263 const int bIndex = (int)compactHeightfield.cells[bX + bY * xSize].index + rcGetCon(aSpan, 0);
264 newDistance = (unsigned char)rcMin((int)distanceToBoundary[bIndex] + 3, 255);
265 if (newDistance < distanceToBoundary[spanIndex])
266 {
267 distanceToBoundary[spanIndex] = newDistance;
268 }
269 }
270 }
271 }
272 }
273 }
274
•275 const unsigned char minBoundaryDistance = (unsigned char)(erosionRadius * 2);
276 for (int spanIndex = 0; spanIndex < compactHeightfield.spanCount; ++spanIndex)
277 {
•278 if (distanceToBoundary[spanIndex] < minBoundaryDistance)
279 {
280 compactHeightfield.areas[spanIndex] = RC_NULL_AREA;
281 }
282 }
•283
284 rcFree(distanceToBoundary);
285
286 return true;
287 }
288
•289 bool rcMedianFilterWalkableArea(rcContext* context, rcCompactHeightfield& compactHeightfield)
290 {
•291 rcAssert(context);
292
•293 const int xSize = compactHeightfield.width;
294 const int zSize = compactHeightfield.height;
295 const int zStride = xSize; // For readability
296
297 rcScopedTimer timer(context, RC_TIMER_MEDIAN_AREA);
298
299 unsigned char* areas = (unsigned char*)rcAlloc(sizeof(unsigned char) * compactHeightfield.spanCount, RC_ALLOC_TEMP);
300 if (!areas)
301 {
•302 context->log(RC_LOG_ERROR, "medianFilterWalkableArea: Out of memory 'areas' (%d).",
303 compactHeightfield.spanCount);
304 return false;
305 }
•306 memset(areas, 0xff, sizeof(unsigned char) * compactHeightfield.spanCount);
307
308 for (int z = 0; z < zSize; ++z)
309 {
•310 for (int x = 0; x < xSize; ++x)
311 {
•312 const rcCompactCell& cell = compactHeightfield.cells[x + z * zStride];
313 const int maxSpanIndex = (int)(cell.index + cell.count);
314 for (int spanIndex = (int)cell.index; spanIndex < maxSpanIndex; ++spanIndex)
315 {
•316 const rcCompactSpan& span = compactHeightfield.spans[spanIndex];
317 if (compactHeightfield.areas[spanIndex] == RC_NULL_AREA)
318 {
•319 areas[spanIndex] = compactHeightfield.areas[spanIndex];
320 continue;
321 }
•322
323 unsigned char neighborAreas[9];
324 for (int neighborIndex = 0; neighborIndex < 9; ++neighborIndex)
325 {
326 neighborAreas[neighborIndex] = compactHeightfield.areas[spanIndex];
327 }
328
329 for (int dir = 0; dir < 4; ++dir)
330 {
•331 if (rcGetCon(span, dir) == RC_NOT_CONNECTED)
332 {
333 continue;
334 }
335
336 const int aX = x + rcGetDirOffsetX(dir);
337 const int aZ = z + rcGetDirOffsetY(dir);
338 const int aIndex = (int)compactHeightfield.cells[aX + aZ * zStride].index + rcGetCon(span, dir);
339 if (compactHeightfield.areas[aIndex] != RC_NULL_AREA)
340 {
341 neighborAreas[dir * 2 + 0] = compactHeightfield.areas[aIndex];
342 }
343
344 const rcCompactSpan& aSpan = compactHeightfield.spans[aIndex];
345 const int dir2 = (dir + 1) & 0x3;
346 const int neighborConnection2 = rcGetCon(aSpan, dir2);
347 if (neighborConnection2 != RC_NOT_CONNECTED)
348 {
•349 const int bX = aX + rcGetDirOffsetX(dir2);
350 const int bZ = aZ + rcGetDirOffsetY(dir2);
351 const int bIndex = (int)compactHeightfield.cells[bX + bZ * zStride].index + neighborConnection2;
352 if (compactHeightfield.areas[bIndex] != RC_NULL_AREA)
353 {
•354 neighborAreas[dir * 2 + 1] = compactHeightfield.areas[bIndex];
355 }
356 }
357 }
•358 insertSort(neighborAreas, 9);
359 areas[spanIndex] = neighborAreas[4];
360 }
361 }
362 }
•363
364 memcpy(compactHeightfield.areas, areas, sizeof(unsigned char) * compactHeightfield.spanCount);
365
366 rcFree(areas);
•367
368 return true;
369 }
370
•371 void rcMarkBoxArea(rcContext* context, const float* boxMinBounds, const float* boxMaxBounds, unsigned char areaId,
372 rcCompactHeightfield& compactHeightfield)
373 {
•374 rcAssert(context);
375
376 rcScopedTimer timer(context, RC_TIMER_MARK_BOX_AREA);
377
378 const int xSize = compactHeightfield.width;
379 const int zSize = compactHeightfield.height;
380 const int zStride = xSize; // For readability
381
382 // Find the footprint of the box area in grid cell coordinates.
383 int minX = (int)((boxMinBounds[0] - compactHeightfield.bmin[0]) / compactHeightfield.cs);
384 int minY = (int)((boxMinBounds[1] - compactHeightfield.bmin[1]) / compactHeightfield.ch);
385 int minZ = (int)((boxMinBounds[2] - compactHeightfield.bmin[2]) / compactHeightfield.cs);
386 int maxX = (int)((boxMaxBounds[0] - compactHeightfield.bmin[0]) / compactHeightfield.cs);
387 int maxY = (int)((boxMaxBounds[1] - compactHeightfield.bmin[1]) / compactHeightfield.ch);
388 int maxZ = (int)((boxMaxBounds[2] - compactHeightfield.bmin[2]) / compactHeightfield.cs);
389
390 // Early-out if the box is outside the bounds of the grid.
391 if (maxX < 0) { return; }
392 if (minX >= xSize) { return; }
393 if (maxZ < 0) { return; }
394 if (minZ >= zSize) { return; }
395
396 // Clamp relevant bound coordinates to the grid.
397 if (minX < 0) { minX = 0; }
398 if (maxX >= xSize) { maxX = xSize - 1; }
399 if (minZ < 0) { minZ = 0; }
400 if (maxZ >= zSize) { maxZ = zSize - 1; }
401
402 // Mark relevant cells.
403 for (int z = minZ; z <= maxZ; ++z)
404 {
•405 for (int x = minX; x <= maxX; ++x)
406 {
•407 const rcCompactCell& cell = compactHeightfield.cells[x + z * zStride];
408 const int maxSpanIndex = (int)(cell.index + cell.count);
409 for (int spanIndex = (int)cell.index; spanIndex < maxSpanIndex; ++spanIndex)
410 {
•411 rcCompactSpan& span = compactHeightfield.spans[spanIndex];
412
413 // Skip if the span is outside the box extents.
414 if ((int)span.y < minY || (int)span.y > maxY)
415 {
•416 continue;
417 }
•418
419 // Skip if the span has been removed.
420 if (compactHeightfield.areas[spanIndex] == RC_NULL_AREA)
421 {
422 continue;
423 }
424
425 // Mark the span.
426 compactHeightfield.areas[spanIndex] = areaId;
427 }
428 }
429 }
430 }
431
•432 void rcMarkConvexPolyArea(rcContext* context, const float* verts, const int numVerts,
433 const float minY, const float maxY, unsigned char areaId,
434 rcCompactHeightfield& compactHeightfield)
435 {
•436 rcAssert(context);
437
•438 rcScopedTimer timer(context, RC_TIMER_MARK_CONVEXPOLY_AREA);
439
•440 const int xSize = compactHeightfield.width;
441 const int zSize = compactHeightfield.height;
442 const int zStride = xSize; // For readability
443
444 // Compute the bounding box of the polygon
445 float bmin[3];
446 float bmax[3];
447 rcVcopy(bmin, verts);
448 rcVcopy(bmax, verts);
•449 for (int i = 1; i < numVerts; ++i)
450 {
•451 rcVmin(bmin, &verts[i * 3]);
452 rcVmax(bmax, &verts[i * 3]);
453 }
•454 bmin[1] = minY;
455 bmax[1] = maxY;
456
457 // Compute the grid footprint of the polygon
458 int minx = (int)((bmin[0] - compactHeightfield.bmin[0]) / compactHeightfield.cs);
459 int miny = (int)((bmin[1] - compactHeightfield.bmin[1]) / compactHeightfield.ch);
460 int minz = (int)((bmin[2] - compactHeightfield.bmin[2]) / compactHeightfield.cs);
461 int maxx = (int)((bmax[0] - compactHeightfield.bmin[0]) / compactHeightfield.cs);
462 int maxy = (int)((bmax[1] - compactHeightfield.bmin[1]) / compactHeightfield.ch);
463 int maxz = (int)((bmax[2] - compactHeightfield.bmin[2]) / compactHeightfield.cs);
464
465 // Early-out if the polygon lies entirely outside the grid.
466 if (maxx < 0) { return; }
467 if (minx >= xSize) { return; }
468 if (maxz < 0) { return; }
469 if (minz >= zSize) { return; }
470
471 // Clamp the polygon footprint to the grid
472 if (minx < 0) { minx = 0; }
473 if (maxx >= xSize) { maxx = xSize - 1; }
474 if (minz < 0) { minz = 0; }
475 if (maxz >= zSize) { maxz = zSize - 1; }
476
477 // TODO: Optimize.
478 for (int z = minz; z <= maxz; ++z)
479 {
480 for (int x = minx; x <= maxx; ++x)
481 {
•482 const rcCompactCell& cell = compactHeightfield.cells[x + z * zStride];
483 const int maxSpanIndex = (int)(cell.index + cell.count);
484 for (int spanIndex = (int)cell.index; spanIndex < maxSpanIndex; ++spanIndex)
485 {
•486 rcCompactSpan& span = compactHeightfield.spans[spanIndex];
487
488 // Skip if span is removed.
489 if (compactHeightfield.areas[spanIndex] == RC_NULL_AREA)
490 {
491 continue;
•492 }
493
494 // Skip if y extents don't overlap.
495 if ((int)span.y < miny || (int)span.y > maxy)
•496 {
•497 continue;
498 }
499
•500 const float point[] = {
501 compactHeightfield.bmin[0] + ((float)x + 0.5f) * compactHeightfield.cs,
502 0,
503 compactHeightfield.bmin[2] + ((float)z + 0.5f) * compactHeightfield.cs
504 };
505
506 if (pointInPoly(numVerts, verts, point))
507 {
508 compactHeightfield.areas[spanIndex] = areaId;
509 }
•510 }
511 }
512 }
513 }
514
•515 static const float EPSILON = 1e-6f;
516
517 /// Normalizes the vector if the length is greater than zero.
518 /// If the magnitude is zero, the vector is unchanged.
519 /// @param[in,out] v The vector to normalize. [(x, y, z)]
520 static void rcVsafeNormalize(float* v)
521 {
•522 const float sqMag = rcSqr(v[0]) + rcSqr(v[1]) + rcSqr(v[2]);
523 if (sqMag > EPSILON)
524 {
525 const float inverseMag = 1.0f / rcSqrt(sqMag);
526 v[0] *= inverseMag;
527 v[1] *= inverseMag;
528 v[2] *= inverseMag;
529 }
530 }
531
•532 int rcOffsetPoly(const float* verts, const int numVerts, const float offset, float* outVerts, const int maxOutVerts)
533 {
534 // Defines the limit at which a miter becomes a bevel.
535 // Similar in behavior to https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-miterlimit
536 const float MITER_LIMIT = 1.20f;
537
538 int numOutVerts = 0;
539
•540 for (int vertIndex = 0; vertIndex < numVerts; vertIndex++)
541 {
•542 // Grab three vertices of the polygon.
543 const int vertIndexA = (vertIndex + numVerts - 1) % numVerts;
544 const int vertIndexB = vertIndex;
545 const int vertIndexC = (vertIndex + 1) % numVerts;
546 const float* vertA = &verts[vertIndexA * 3];
547 const float* vertB = &verts[vertIndexB * 3];
548 const float* vertC = &verts[vertIndexC * 3];
549
550 // From A to B on the x/z plane
551 float prevSegmentDir[3];
552 rcVsub(prevSegmentDir, vertB, vertA);
553 prevSegmentDir[1] = 0; // Squash onto x/z plane
554 rcVsafeNormalize(prevSegmentDir);
555
556 // From B to C on the x/z plane
557 float currSegmentDir[3];
558 rcVsub(currSegmentDir, vertC, vertB);
559 currSegmentDir[1] = 0; // Squash onto x/z plane
560 rcVsafeNormalize(currSegmentDir);
561
562 // The y component of the cross product of the two normalized segment directions.
563 // The X and Z components of the cross product are both zero because the two
564 // segment direction vectors fall within the x/z plane.
565 float cross = currSegmentDir[0] * prevSegmentDir[2] - prevSegmentDir[0] * currSegmentDir[2];
566
567 // CCW perpendicular vector to AB. The segment normal.
568 const float prevSegmentNormX = -prevSegmentDir[2];
569 const float prevSegmentNormZ = prevSegmentDir[0];
570
571 // CCW perpendicular vector to BC. The segment normal.
572 const float currSegmentNormX = -currSegmentDir[2];
573 const float currSegmentNormZ = currSegmentDir[0];
574
575 // Average the two segment normals to get the proportional miter offset for B.
576 // This isn't normalized because it's defining the distance and direction the corner will need to be
577 // adjusted proportionally to the edge offsets to properly miter the adjoining edges.
578 float cornerMiterX = (prevSegmentNormX + currSegmentNormX) * 0.5f;
579 float cornerMiterZ = (prevSegmentNormZ + currSegmentNormZ) * 0.5f;
580 const float cornerMiterSqMag = rcSqr(cornerMiterX) + rcSqr(cornerMiterZ);
581
582 // If the magnitude of the segment normal average is less than about .69444,
583 // the corner is an acute enough angle that the result should be beveled.
584 const bool bevel = cornerMiterSqMag * MITER_LIMIT * MITER_LIMIT < 1.0f;
585
586 // Scale the corner miter so it's proportional to how much the corner should be offset compared to the edges.
587 if (cornerMiterSqMag > EPSILON)
588 {
•589 const float scale = 1.0f / cornerMiterSqMag;
590 cornerMiterX *= scale;
591 cornerMiterZ *= scale;
592 }
593
•594 if (bevel && cross < 0.0f) // If the corner is convex and an acute enough angle, generate a bevel.
595 {
•596 if (numOutVerts + 2 > maxOutVerts)
597 {
598 return 0;
•599 }
600
601 // Generate two bevel vertices at a distances from B proportional to the angle between the two segments.
602 // Move each bevel vertex out proportional to the given offset.
603 float d = (1.0f - (prevSegmentDir[0] * currSegmentDir[0] + prevSegmentDir[2] * currSegmentDir[2])) * 0.5f;
604
605 outVerts[numOutVerts * 3 + 0] = vertB[0] + (-prevSegmentNormX + prevSegmentDir[0] * d) * offset;
606 outVerts[numOutVerts * 3 + 1] = vertB[1];
607 outVerts[numOutVerts * 3 + 2] = vertB[2] + (-prevSegmentNormZ + prevSegmentDir[2] * d) * offset;
608 numOutVerts++;
609
610 outVerts[numOutVerts * 3 + 0] = vertB[0] + (-currSegmentNormX - currSegmentDir[0] * d) * offset;
611 outVerts[numOutVerts * 3 + 1] = vertB[1];
612 outVerts[numOutVerts * 3 + 2] = vertB[2] + (-currSegmentNormZ - currSegmentDir[2] * d) * offset;
613 numOutVerts++;
614 }
615 else
616 {
•617 if (numOutVerts + 1 > maxOutVerts)
618 {
619 return 0;
•620 }
621
622 // Move B along the miter direction by the specified offset.
623 outVerts[numOutVerts * 3 + 0] = vertB[0] - cornerMiterX * offset;
624 outVerts[numOutVerts * 3 + 1] = vertB[1];
625 outVerts[numOutVerts * 3 + 2] = vertB[2] - cornerMiterZ * offset;
626 numOutVerts++;
627 }
628 }
629
•630 return numOutVerts;
631 }
632
•633 void rcMarkCylinderArea(rcContext* context, const float* position, const float radius, const float height,
634 unsigned char areaId, rcCompactHeightfield& compactHeightfield)
635 {
•636 rcAssert(context);
637
638 rcScopedTimer timer(context, RC_TIMER_MARK_CYLINDER_AREA);
639
640 const int xSize = compactHeightfield.width;
641 const int zSize = compactHeightfield.height;
642 const int zStride = xSize; // For readability
643
644 // Compute the bounding box of the cylinder
645 const float cylinderBBMin[] =
646 {
647 position[0] - radius,
648 position[1],
649 position[2] - radius
650 };
651 const float cylinderBBMax[] =
652 {
653 position[0] + radius,
654 position[1] + height,
655 position[2] + radius
656 };
657
658 // Compute the grid footprint of the cylinder
659 int minx = (int)((cylinderBBMin[0] - compactHeightfield.bmin[0]) / compactHeightfield.cs);
660 int miny = (int)((cylinderBBMin[1] - compactHeightfield.bmin[1]) / compactHeightfield.ch);
661 int minz = (int)((cylinderBBMin[2] - compactHeightfield.bmin[2]) / compactHeightfield.cs);
662 int maxx = (int)((cylinderBBMax[0] - compactHeightfield.bmin[0]) / compactHeightfield.cs);
663 int maxy = (int)((cylinderBBMax[1] - compactHeightfield.bmin[1]) / compactHeightfield.ch);
664 int maxz = (int)((cylinderBBMax[2] - compactHeightfield.bmin[2]) / compactHeightfield.cs);
665
666 // Early-out if the cylinder is completely outside the grid bounds.
667 if (maxx < 0) { return; }
668 if (minx >= xSize) { return; }
669 if (maxz < 0) { return; }
670 if (minz >= zSize) { return; }
671
672 // Clamp the cylinder bounds to the grid.
673 if (minx < 0) { minx = 0; }
674 if (maxx >= xSize) { maxx = xSize - 1; }
675 if (minz < 0) { minz = 0; }
676 if (maxz >= zSize) { maxz = zSize - 1; }
677
678 const float radiusSq = radius * radius;
679
680 for (int z = minz; z <= maxz; ++z)
681 {
682 for (int x = minx; x <= maxx; ++x)
683 {
•684 const rcCompactCell& cell = compactHeightfield.cells[x + z * zStride];
685 const int maxSpanIndex = (int)(cell.index + cell.count);
686
687 const float cellX = compactHeightfield.bmin[0] + ((float)x + 0.5f) * compactHeightfield.cs;
688 const float cellZ = compactHeightfield.bmin[2] + ((float)z + 0.5f) * compactHeightfield.cs;
689 const float deltaX = cellX - position[0];
690 const float deltaZ = cellZ - position[2];
691
692 // Skip this column if it's too far from the center point of the cylinder.
693 if (rcSqr(deltaX) + rcSqr(deltaZ) >= radiusSq)
694 {
695 continue;
696 }
697
698 // Mark all overlapping spans
699 for (int spanIndex = (int)cell.index; spanIndex < maxSpanIndex; ++spanIndex)
700 {
•701 rcCompactSpan& span = compactHeightfield.spans[spanIndex];
702
703 // Skip if span is removed.
704 if (compactHeightfield.areas[spanIndex] == RC_NULL_AREA)
705 {
706 continue;
•707 }
708
709 // Mark if y extents overlap.
710 if ((int)span.y >= miny && (int)span.y <= maxy)
711 {
•712 compactHeightfield.areas[spanIndex] = areaId;
713 }
714 }
715 }
716 }
717 }