Debian Codegraph @

Recast/Source/RecastFilter.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 2 of 3 — not the installed (fully patched) source.
3 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 "RecastAssert.h"
21
22 #include <stdlib.h>
23
24 namespace
25 {
26 const int MAX_HEIGHTFIELD_HEIGHT = 0xffff; // TODO (graham): Move this to a more visible constant and update usages.
27 }
28
29 void rcFilterLowHangingWalkableObstacles(rcContext* context, const int walkableClimb, rcHeightfield& heightfield)
30 {
31 rcAssert(context);
32
33 rcScopedTimer timer(context, RC_TIMER_FILTER_LOW_OBSTACLES);
34
35 const int xSize = heightfield.width;
36 const int zSize = heightfield.height;
37
38 for (int z = 0; z < zSize; ++z)
39 {
40 for (int x = 0; x < xSize; ++x)
41 {
42 rcSpan* previousSpan = NULL;
43 bool previousWasWalkable = false;
44 unsigned char previousArea = RC_NULL_AREA;
45
46 for (rcSpan* span = heightfield.spans[x + z * xSize]; span != NULL; previousSpan = span, span = span->next)
47 {
48 const bool walkable = span->area != RC_NULL_AREA;
49 // If current span is not walkable, but there is walkable
50 // span just below it, mark the span above it walkable too.
51 if (!walkable && previousWasWalkable)
52 {
53 if (rcAbs((int)span->smax - (int)previousSpan->smax) <= walkableClimb)
54 {
55 span->area = previousArea;
56 }
57 }
58 // Copy walkable flag so that it cannot propagate
59 // past multiple non-walkable objects.
60 previousWasWalkable = walkable;
61 previousArea = span->area;
62 }
63 }
64 }
65 }
66
67 void rcFilterLedgeSpans(rcContext* context, const int walkableHeight, const int walkableClimb, rcHeightfield& heightfield)
68 {
69 rcAssert(context);
70
71 rcScopedTimer timer(context, RC_TIMER_FILTER_BORDER);
72
73 const int xSize = heightfield.width;
74 const int zSize = heightfield.height;
75
76 // Mark border spans.
77 for (int z = 0; z < zSize; ++z)
78 {
79 for (int x = 0; x < xSize; ++x)
80 {
81 for (rcSpan* span = heightfield.spans[x + z * xSize]; span; span = span->next)
82 {
83 // Skip non walkable spans.
84 if (span->area == RC_NULL_AREA)
85 {
86 continue;
87 }
88
89 const int bot = (int)(span->smax);
90 const int top = span->next ? (int)(span->next->smin) : MAX_HEIGHTFIELD_HEIGHT;
91
92 // Find neighbours minimum height.
93 int minNeighborHeight = MAX_HEIGHTFIELD_HEIGHT;
94
95 // Min and max height of accessible neighbours.
96 int accessibleNeighborMinHeight = span->smax;
97 int accessibleNeighborMaxHeight = span->smax;
98
99 for (int direction = 0; direction < 4; ++direction)
100 {
101 int dx = x + rcGetDirOffsetX(direction);
102 int dz = z + rcGetDirOffsetY(direction);
103 // Skip neighbours which are out of bounds.
104 if (dx < 0 || dz < 0 || dx >= xSize || dz >= zSize)
105 {
106 minNeighborHeight = (-walkableClimb - 1) ;
107 break;
108 }
109
110 // From minus infinity to the first span.
111 const rcSpan* neighborSpan = heightfield.spans[dx + dz * xSize];
112 int neighborTop = neighborSpan ? (int)neighborSpan->smin : MAX_HEIGHTFIELD_HEIGHT;
113
114 // Skip neighbour if the gap between the spans is too small.
115 if (rcMin(top, neighborTop) - bot >= walkableHeight)
116 {
117 minNeighborHeight = (-walkableClimb - 1);
118 break;
119 }
120
121 // Rest of the spans.
122 for (neighborSpan = heightfield.spans[dx + dz * xSize]; neighborSpan; neighborSpan = neighborSpan->next)
123 {
124 int neighborBot = (int)neighborSpan->smax;
125 neighborTop = neighborSpan->next ? (int)neighborSpan->next->smin : MAX_HEIGHTFIELD_HEIGHT;
126
127 // Skip neighbour if the gap between the spans is too small.
128 if (rcMin(top, neighborTop) - rcMax(bot, neighborBot) >= walkableHeight)
129 {
130 int accessibleNeighbourHeight = neighborBot - bot;
131 minNeighborHeight = rcMin(minNeighborHeight, accessibleNeighbourHeight);
132
133 // Find min/max accessible neighbour height.
134 if (rcAbs(accessibleNeighbourHeight) <= walkableClimb)
135 {
136 if (neighborBot < accessibleNeighborMinHeight) accessibleNeighborMinHeight = neighborBot;
137 if (neighborBot > accessibleNeighborMaxHeight) accessibleNeighborMaxHeight = neighborBot;
138 }
139 else if (accessibleNeighbourHeight < -walkableClimb)
140 {
141 break;
142 }
143 }
144 }
145 }
146
147 // The current span is close to a ledge if the drop to any neighbour span is less than the walkableClimb.
148 if (minNeighborHeight < -walkableClimb)
149 {
150 span->area = RC_NULL_AREA;
151 }
152 // If the difference between all neighbours is too large, we are at steep slope, mark the span as ledge.
153 else if ((accessibleNeighborMaxHeight - accessibleNeighborMinHeight) > walkableClimb)
154 {
155 span->area = RC_NULL_AREA;
156 }
157 }
158 }
159 }
160 }
161
162 void rcFilterWalkableLowHeightSpans(rcContext* context, const int walkableHeight, rcHeightfield& heightfield)
163 {
164 rcAssert(context);
165 rcScopedTimer timer(context, RC_TIMER_FILTER_WALKABLE);
166
167 const int xSize = heightfield.width;
168 const int zSize = heightfield.height;
169
170 // Remove walkable flag from spans which do not have enough
171 // space above them for the agent to stand there.
172 for (int z = 0; z < zSize; ++z)
173 {
174 for (int x = 0; x < xSize; ++x)
175 {
176 for (rcSpan* span = heightfield.spans[x + z*xSize]; span; span = span->next)
177 {
178 const int bot = (int)(span->smax);
179 const int top = span->next ? (int)(span->next->smin) : MAX_HEIGHTFIELD_HEIGHT;
180 if ((top - bot) < walkableHeight)
181 {
182 span->area = RC_NULL_AREA;
183 }
184 }
185 }
186 }
187 }