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