RecastFilter.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #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. ctx->startTimer(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. ctx->stopTimer(RC_TIMER_FILTER_LOW_OBSTACLES);
  65. }
  66. /// @par
  67. ///
  68. /// A ledge is a span with one or more neighbors whose maximum is further away than @p walkableClimb
  69. /// from the current span's maximum.
  70. /// This method removes the impact of the overestimation of conservative voxelization
  71. /// so the resulting mesh will not have regions hanging in the air over ledges.
  72. ///
  73. /// A span is a ledge if: <tt>rcAbs(currentSpan.smax - neighborSpan.smax) > walkableClimb</tt>
  74. ///
  75. /// @see rcHeightfield, rcConfig
  76. void rcFilterLedgeSpans(rcContext* ctx, const int walkableHeight, const int walkableClimb,
  77. rcHeightfield& solid)
  78. {
  79. rcAssert(ctx);
  80. ctx->startTimer(RC_TIMER_FILTER_BORDER);
  81. const int w = solid.width;
  82. const int h = solid.height;
  83. const int MAX_HEIGHT = 0xffff;
  84. // Mark border spans.
  85. for (int y = 0; y < h; ++y)
  86. {
  87. for (int x = 0; x < w; ++x)
  88. {
  89. for (rcSpan* s = solid.spans[x + y*w]; s; s = s->next)
  90. {
  91. // Skip non walkable spans.
  92. if (s->area == RC_NULL_AREA)
  93. continue;
  94. const int bot = (int)(s->smax);
  95. const int top = s->next ? (int)(s->next->smin) : MAX_HEIGHT;
  96. // Find neighbours minimum height.
  97. int minh = MAX_HEIGHT;
  98. // Min and max height of accessible neighbours.
  99. int asmin = s->smax;
  100. int asmax = s->smax;
  101. for (int dir = 0; dir < 4; ++dir)
  102. {
  103. int dx = x + rcGetDirOffsetX(dir);
  104. int dy = y + rcGetDirOffsetY(dir);
  105. // Skip neighbours which are out of bounds.
  106. if (dx < 0 || dy < 0 || dx >= w || dy >= h)
  107. {
  108. minh = rcMin(minh, -walkableClimb - bot);
  109. continue;
  110. }
  111. // From minus infinity to the first span.
  112. rcSpan* ns = solid.spans[dx + dy*w];
  113. int nbot = -walkableClimb;
  114. int ntop = ns ? (int)ns->smin : MAX_HEIGHT;
  115. // Skip neightbour if the gap between the spans is too small.
  116. if (rcMin(top,ntop) - rcMax(bot,nbot) > walkableHeight)
  117. minh = rcMin(minh, nbot - bot);
  118. // Rest of the spans.
  119. for (ns = solid.spans[dx + dy*w]; ns; ns = ns->next)
  120. {
  121. nbot = (int)ns->smax;
  122. ntop = ns->next ? (int)ns->next->smin : MAX_HEIGHT;
  123. // Skip neightbour if the gap between the spans is too small.
  124. if (rcMin(top,ntop) - rcMax(bot,nbot) > walkableHeight)
  125. {
  126. minh = rcMin(minh, nbot - bot);
  127. // Find min/max accessible neighbour height.
  128. if (rcAbs(nbot - bot) <= walkableClimb)
  129. {
  130. if (nbot < asmin) asmin = nbot;
  131. if (nbot > asmax) asmax = nbot;
  132. }
  133. }
  134. }
  135. }
  136. // The current span is close to a ledge if the drop to any
  137. // neighbour span is less than the walkableClimb.
  138. if (minh < -walkableClimb)
  139. s->area = RC_NULL_AREA;
  140. // If the difference between all neighbours is too large,
  141. // we are at steep slope, mark the span as ledge.
  142. if ((asmax - asmin) > walkableClimb)
  143. {
  144. s->area = RC_NULL_AREA;
  145. }
  146. }
  147. }
  148. }
  149. ctx->stopTimer(RC_TIMER_FILTER_BORDER);
  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. ctx->startTimer(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. ctx->stopTimer(RC_TIMER_FILTER_WALKABLE);
  180. }