DetourLocalBoundary.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // Modified by cosmy1 for Urho3D
  19. #include <float.h>
  20. #include <string.h>
  21. #include "DetourLocalBoundary.h"
  22. #include "DetourNavMeshQuery.h"
  23. #include "DetourCommon.h"
  24. #include "DetourAssert.h"
  25. dtLocalBoundary::dtLocalBoundary() :
  26. m_nsegs(0),
  27. m_npolys(0)
  28. {
  29. dtVset(m_center, FLT_MAX,FLT_MAX,FLT_MAX);
  30. // Urho3D: initialize all class members
  31. memset(&m_segs, 0, sizeof(m_segs));
  32. memset(&m_polys, 0, sizeof(m_polys));
  33. }
  34. dtLocalBoundary::~dtLocalBoundary()
  35. {
  36. }
  37. void dtLocalBoundary::reset()
  38. {
  39. dtVset(m_center, FLT_MAX,FLT_MAX,FLT_MAX);
  40. m_npolys = 0;
  41. m_nsegs = 0;
  42. }
  43. void dtLocalBoundary::addSegment(const float dist, const float* s)
  44. {
  45. // Insert neighbour based on the distance.
  46. Segment* seg = 0;
  47. if (!m_nsegs)
  48. {
  49. // First, trivial accept.
  50. seg = &m_segs[0];
  51. }
  52. else if (dist >= m_segs[m_nsegs-1].d)
  53. {
  54. // Further than the last segment, skip.
  55. if (m_nsegs >= MAX_LOCAL_SEGS)
  56. return;
  57. // Last, trivial accept.
  58. seg = &m_segs[m_nsegs];
  59. }
  60. else
  61. {
  62. // Insert inbetween.
  63. int i;
  64. for (i = 0; i < m_nsegs; ++i)
  65. if (dist <= m_segs[i].d)
  66. break;
  67. const int tgt = i+1;
  68. const int n = dtMin(m_nsegs-i, MAX_LOCAL_SEGS-tgt);
  69. dtAssert(tgt+n <= MAX_LOCAL_SEGS);
  70. if (n > 0)
  71. memmove(&m_segs[tgt], &m_segs[i], sizeof(Segment)*n);
  72. seg = &m_segs[i];
  73. }
  74. seg->d = dist;
  75. memcpy(seg->s, s, sizeof(float)*6);
  76. if (m_nsegs < MAX_LOCAL_SEGS)
  77. m_nsegs++;
  78. }
  79. void dtLocalBoundary::update(dtPolyRef ref, const float* pos, const float collisionQueryRange,
  80. dtNavMeshQuery* navquery, const dtQueryFilter* filter)
  81. {
  82. static const int MAX_SEGS_PER_POLY = DT_VERTS_PER_POLYGON*3;
  83. if (!ref)
  84. {
  85. dtVset(m_center, FLT_MAX,FLT_MAX,FLT_MAX);
  86. m_nsegs = 0;
  87. m_npolys = 0;
  88. return;
  89. }
  90. dtVcopy(m_center, pos);
  91. // First query non-overlapping polygons.
  92. navquery->findLocalNeighbourhood(ref, pos, collisionQueryRange,
  93. filter, m_polys, 0, &m_npolys, MAX_LOCAL_POLYS);
  94. // Secondly, store all polygon edges.
  95. m_nsegs = 0;
  96. float segs[MAX_SEGS_PER_POLY*6];
  97. int nsegs = 0;
  98. for (int j = 0; j < m_npolys; ++j)
  99. {
  100. navquery->getPolyWallSegments(m_polys[j], filter, segs, 0, &nsegs, MAX_SEGS_PER_POLY);
  101. for (int k = 0; k < nsegs; ++k)
  102. {
  103. const float* s = &segs[k*6];
  104. // Skip too distant segments.
  105. float tseg;
  106. const float distSqr = dtDistancePtSegSqr2D(pos, s, s+3, tseg);
  107. if (distSqr > dtSqr(collisionQueryRange))
  108. continue;
  109. addSegment(distSqr, s);
  110. }
  111. }
  112. }
  113. bool dtLocalBoundary::isValid(dtNavMeshQuery* navquery, const dtQueryFilter* filter)
  114. {
  115. if (!m_npolys)
  116. return false;
  117. // Check that all polygons still pass query filter.
  118. for (int i = 0; i < m_npolys; ++i)
  119. {
  120. if (!navquery->isValidPolyRef(m_polys[i], filter))
  121. return false;
  122. }
  123. return true;
  124. }