RecastFilter.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  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. #define _USE_MATH_DEFINES
  19. #include <math.h>
  20. #include <stdio.h>
  21. #include "Recast.h"
  22. #include "RecastAssert.h"
  23. /// @par
  24. ///
  25. /// Allows the formation of walkable regions that will flow over low lying
  26. /// objects such as curbs, and up structures such as stairways.
  27. ///
  28. /// Two neighboring spans are walkable if: <tt>rcAbs(currentSpan.smax - neighborSpan.smax) < waklableClimb</tt>
  29. ///
  30. /// @warning Will override the effect of #rcFilterLedgeSpans. So if both filters are used, call
  31. /// #rcFilterLedgeSpans after calling this filter.
  32. ///
  33. /// @see rcHeightfield, rcConfig
  34. void rcFilterLowHangingWalkableObstacles(rcContext* ctx, const int walkableClimb, rcHeightfield& solid)
  35. {
  36. rcAssert(ctx);
  37. rcScopedTimer timer(ctx, RC_TIMER_FILTER_LOW_OBSTACLES);
  38. const int w = solid.width;
  39. const int h = solid.height;
  40. for (int y = 0; y < h; ++y)
  41. {
  42. for (int x = 0; x < w; ++x)
  43. {
  44. rcSpan* ps = 0;
  45. bool previousWalkable = false;
  46. unsigned char previousArea = RC_NULL_AREA;
  47. for (rcSpan* s = solid.spans[x + y*w]; s; ps = s, s = s->next)
  48. {
  49. const bool walkable = s->area != RC_NULL_AREA;
  50. // If current span is not walkable, but there is walkable
  51. // span just below it, mark the span above it walkable too.
  52. if (!walkable && previousWalkable)
  53. {
  54. if (rcAbs((int)s->smax - (int)ps->smax) <= walkableClimb)
  55. s->area = previousArea;
  56. }
  57. // Copy walkable flag so that it cannot propagate
  58. // past multiple non-walkable objects.
  59. previousWalkable = walkable;
  60. previousArea = s->area;
  61. }
  62. }
  63. }
  64. }
  65. /// @par
  66. ///
  67. /// A ledge is a span with one or more neighbors whose maximum is further away than @p walkableClimb
  68. /// from the current span's maximum.
  69. /// This method removes the impact of the overestimation of conservative voxelization
  70. /// so the resulting mesh will not have regions hanging in the air over ledges.
  71. ///
  72. /// A span is a ledge if: <tt>rcAbs(currentSpan.smax - neighborSpan.smax) > walkableClimb</tt>
  73. ///
  74. /// @see rcHeightfield, rcConfig
  75. void rcFilterLedgeSpans(rcContext* ctx, const int walkableHeight, const int walkableClimb,
  76. rcHeightfield& solid)
  77. {
  78. rcAssert(ctx);
  79. rcScopedTimer timer(ctx, RC_TIMER_FILTER_BORDER);
  80. const int w = solid.width;
  81. const int h = solid.height;
  82. const int MAX_HEIGHT = 0xffff;
  83. // Mark border spans.
  84. for (int y = 0; y < h; ++y)
  85. {
  86. for (int x = 0; x < w; ++x)
  87. {
  88. for (rcSpan* s = solid.spans[x + y*w]; s; s = s->next)
  89. {
  90. // Skip non walkable spans.
  91. if (s->area == RC_NULL_AREA)
  92. continue;
  93. const int bot = (int)(s->smax);
  94. const int top = s->next ? (int)(s->next->smin) : MAX_HEIGHT;
  95. // Find neighbours minimum height.
  96. int minh = MAX_HEIGHT;
  97. // Min and max height of accessible neighbours.
  98. int asmin = s->smax;
  99. int asmax = s->smax;
  100. for (int dir = 0; dir < 4; ++dir)
  101. {
  102. int dx = x + rcGetDirOffsetX(dir);
  103. int dy = y + rcGetDirOffsetY(dir);
  104. // Skip neighbours which are out of bounds.
  105. if (dx < 0 || dy < 0 || dx >= w || dy >= h)
  106. {
  107. minh = rcMin(minh, -walkableClimb - bot);
  108. continue;
  109. }
  110. // From minus infinity to the first span.
  111. rcSpan* ns = solid.spans[dx + dy*w];
  112. int nbot = -walkableClimb;
  113. int ntop = ns ? (int)ns->smin : MAX_HEIGHT;
  114. // Skip neightbour if the gap between the spans is too small.
  115. if (rcMin(top,ntop) - rcMax(bot,nbot) > walkableHeight)
  116. minh = rcMin(minh, nbot - bot);
  117. // Rest of the spans.
  118. for (ns = solid.spans[dx + dy*w]; ns; ns = ns->next)
  119. {
  120. nbot = (int)ns->smax;
  121. ntop = ns->next ? (int)ns->next->smin : MAX_HEIGHT;
  122. // Skip neightbour if the gap between the spans is too small.
  123. if (rcMin(top,ntop) - rcMax(bot,nbot) > walkableHeight)
  124. {
  125. minh = rcMin(minh, nbot - bot);
  126. // Find min/max accessible neighbour height.
  127. if (rcAbs(nbot - bot) <= walkableClimb)
  128. {
  129. if (nbot < asmin) asmin = nbot;
  130. if (nbot > asmax) asmax = nbot;
  131. }
  132. }
  133. }
  134. }
  135. // The current span is close to a ledge if the drop to any
  136. // neighbour span is less than the walkableClimb.
  137. if (minh < -walkableClimb)
  138. {
  139. s->area = RC_NULL_AREA;
  140. }
  141. // If the difference between all neighbours is too large,
  142. // we are at steep slope, mark the span as ledge.
  143. else if ((asmax - asmin) > walkableClimb)
  144. {
  145. s->area = RC_NULL_AREA;
  146. }
  147. }
  148. }
  149. }
  150. }
  151. /// @par
  152. ///
  153. /// For this filter, the clearance above the span is the distance from the span's
  154. /// maximum to the next higher span's minimum. (Same grid column.)
  155. ///
  156. /// @see rcHeightfield, rcConfig
  157. void rcFilterWalkableLowHeightSpans(rcContext* ctx, int walkableHeight, rcHeightfield& solid)
  158. {
  159. rcAssert(ctx);
  160. rcScopedTimer timer(ctx, RC_TIMER_FILTER_WALKABLE);
  161. const int w = solid.width;
  162. const int h = solid.height;
  163. const int MAX_HEIGHT = 0xffff;
  164. // Remove walkable flag from spans which do not have enough
  165. // space above them for the agent to stand there.
  166. for (int y = 0; y < h; ++y)
  167. {
  168. for (int x = 0; x < w; ++x)
  169. {
  170. for (rcSpan* s = solid.spans[x + y*w]; s; s = s->next)
  171. {
  172. const int bot = (int)(s->smax);
  173. const int top = s->next ? (int)(s->next->smin) : MAX_HEIGHT;
  174. if ((top - bot) <= walkableHeight)
  175. s->area = RC_NULL_AREA;
  176. }
  177. }
  178. }
  179. }