Planeshift

DetourNavMeshQuery.h

Go to the documentation of this file.
00001 //
00002 // Copyright (c) 2009-2010 Mikko Mononen [email protected]
00003 //
00004 // This software is provided 'as-is', without any express or implied
00005 // warranty.  In no event will the authors be held liable for any damages
00006 // arising from the use of this software.
00007 // Permission is granted to anyone to use this software for any purpose,
00008 // including commercial applications, and to alter it and redistribute it
00009 // freely, subject to the following restrictions:
00010 // 1. The origin of this software must not be misrepresented; you must not
00011 //    claim that you wrote the original software. If you use this software
00012 //    in a product, an acknowledgment in the product documentation would be
00013 //    appreciated but is not required.
00014 // 2. Altered source versions must be plainly marked as such, and must not be
00015 //    misrepresented as being the original software.
00016 // 3. This notice may not be removed or altered from any source distribution.
00017 //
00018 
00019 #ifndef DETOURNAVMESHQUERY_H
00020 #define DETOURNAVMESHQUERY_H
00021 
00022 #include "DetourNavMesh.h"
00023 #include "DetourStatus.h"
00024 
00025 
00026 // Define DT_VIRTUAL_QUERYFILTER if you wish to derive a custom filter from dtQueryFilter.
00027 // On certain platforms indirect or virtual function call is expensive. The default
00028 // setting is to use non-virtual functions, the actual implementations of the functions
00029 // are declared as inline for maximum speed. 
00030 
00031 //#define DT_VIRTUAL_QUERYFILTER 1
00032 
00035 class dtQueryFilter
00036 {
00037         float m_areaCost[DT_MAX_AREAS];         
00038         unsigned short m_includeFlags;          
00039         unsigned short m_excludeFlags;          
00040         
00041 public:
00042         dtQueryFilter();
00043         
00048 #ifdef DT_VIRTUAL_QUERYFILTER
00049         virtual bool passFilter(const dtPolyRef ref,
00050                                                         const dtMeshTile* tile,
00051                                                         const dtPoly* poly) const;
00052 #else
00053         bool passFilter(const dtPolyRef ref,
00054                                         const dtMeshTile* tile,
00055                                         const dtPoly* poly) const;
00056 #endif
00057 
00071 #ifdef DT_VIRTUAL_QUERYFILTER
00072         virtual float getCost(const float* pa, const float* pb,
00073                                                   const dtPolyRef prevRef, const dtMeshTile* prevTile, const dtPoly* prevPoly,
00074                                                   const dtPolyRef curRef, const dtMeshTile* curTile, const dtPoly* curPoly,
00075                                                   const dtPolyRef nextRef, const dtMeshTile* nextTile, const dtPoly* nextPoly) const;
00076 #else
00077         float getCost(const float* pa, const float* pb,
00078                                   const dtPolyRef prevRef, const dtMeshTile* prevTile, const dtPoly* prevPoly,
00079                                   const dtPolyRef curRef, const dtMeshTile* curTile, const dtPoly* curPoly,
00080                                   const dtPolyRef nextRef, const dtMeshTile* nextTile, const dtPoly* nextPoly) const;
00081 #endif
00082 
00085 
00089         inline float getAreaCost(const int i) const { return m_areaCost[i]; }
00090 
00094         inline void setAreaCost(const int i, const float cost) { m_areaCost[i] = cost; } 
00095 
00099         inline unsigned short getIncludeFlags() const { return m_includeFlags; }
00100 
00103         inline void setIncludeFlags(const unsigned short flags) { m_includeFlags = flags; }
00104 
00108         inline unsigned short getExcludeFlags() const { return m_excludeFlags; }
00109 
00112         inline void setExcludeFlags(const unsigned short flags) { m_excludeFlags = flags; }     
00113 
00115 
00116 };
00117 
00121 class dtNavMeshQuery
00122 {
00123 public:
00124         dtNavMeshQuery();
00125         ~dtNavMeshQuery();
00126         
00131         dtStatus init(const dtNavMesh* nav, const int maxNodes);
00132         
00134         // /@{
00135 
00146         dtStatus findPath(dtPolyRef startRef, dtPolyRef endRef,
00147                                           const float* startPos, const float* endPos,
00148                                           const dtQueryFilter* filter,
00149                                           dtPolyRef* path, int* pathCount, const int maxPath) const;
00150         
00162         dtStatus findStraightPath(const float* startPos, const float* endPos,
00163                                                           const dtPolyRef* path, const int pathSize,
00164                                                           float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
00165                                                           int* straightPathCount, const int maxStraightPath) const;
00166 
00174 
00182         dtStatus initSlicedFindPath(dtPolyRef startRef, dtPolyRef endRef,
00183                                                                 const float* startPos, const float* endPos,
00184                                                                 const dtQueryFilter* filter);
00185 
00190         dtStatus updateSlicedFindPath(const int maxIter, int* doneIters);
00191 
00198         dtStatus finalizeSlicedFindPath(dtPolyRef* path, int* pathCount, const int maxPath);
00199         
00209         dtStatus finalizeSlicedFindPathPartial(const dtPolyRef* existing, const int existingSize,
00210                                                                                    dtPolyRef* path, int* pathCount, const int maxPath);
00211 
00215 
00228         dtStatus findPolysAroundCircle(dtPolyRef startRef, const float* centerPos, const float radius,
00229                                                                    const dtQueryFilter* filter,
00230                                                                    dtPolyRef* resultRef, dtPolyRef* resultParent, float* resultCost,
00231                                                                    int* resultCount, const int maxResult) const;
00232         
00246         dtStatus findPolysAroundShape(dtPolyRef startRef, const float* verts, const int nverts,
00247                                                                   const dtQueryFilter* filter,
00248                                                                   dtPolyRef* resultRef, dtPolyRef* resultParent, float* resultCost,
00249                                                                   int* resultCount, const int maxResult) const;
00250         
00254 
00262         dtStatus findNearestPoly(const float* center, const float* extents,
00263                                                          const dtQueryFilter* filter,
00264                                                          dtPolyRef* nearestRef, float* nearestPt) const;
00265         
00274         dtStatus queryPolygons(const float* center, const float* extents,
00275                                                    const dtQueryFilter* filter,
00276                                                    dtPolyRef* polys, int* polyCount, const int maxPolys) const;
00277 
00289         dtStatus findLocalNeighbourhood(dtPolyRef startRef, const float* centerPos, const float radius,
00290                                                                         const dtQueryFilter* filter,
00291                                                                         dtPolyRef* resultRef, dtPolyRef* resultParent,
00292                                                                         int* resultCount, const int maxResult) const;
00293 
00304         dtStatus moveAlongSurface(dtPolyRef startRef, const float* startPos, const float* endPos,
00305                                                           const dtQueryFilter* filter,
00306                                                           float* resultPos, dtPolyRef* visited, int* visitedCount, const int maxVisitedSize) const;
00307         
00321         dtStatus raycast(dtPolyRef startRef, const float* startPos, const float* endPos,
00322                                          const dtQueryFilter* filter,
00323                                          float* t, float* hitNormal, dtPolyRef* path, int* pathCount, const int maxPath) const;
00324         
00335         dtStatus findDistanceToWall(dtPolyRef startRef, const float* centerPos, const float maxRadius,
00336                                                                 const dtQueryFilter* filter,
00337                                                                 float* hitDist, float* hitPos, float* hitNormal) const;
00338         
00348         dtStatus getPolyWallSegments(dtPolyRef ref, const dtQueryFilter* filter,
00349                                                                  float* segmentVerts, dtPolyRef* segmentRefs, int* segmentCount,
00350                                                                  const int maxSegments) const;
00351 
00359         dtStatus findRandomPoint(const dtQueryFilter* filter, float (*frand)(),
00360                                                          dtPolyRef* randomRef, float* randomPt) const;
00361 
00372         dtStatus findRandomPointAroundCircle(dtPolyRef startRef, const float* centerPos, const float maxRadius,
00373                                                                                  const dtQueryFilter* filter, float (*frand)(),
00374                                                                                  dtPolyRef* randomRef, float* randomPt) const;
00375         
00381         dtStatus closestPointOnPoly(dtPolyRef ref, const float* pos, float* closest) const;
00382         
00389         dtStatus closestPointOnPolyBoundary(dtPolyRef ref, const float* pos, float* closest) const;
00390         
00396         dtStatus getPolyHeight(dtPolyRef ref, const float* pos, float* height) const;
00397 
00401 
00405         bool isValidPolyRef(dtPolyRef ref, const dtQueryFilter* filter) const;
00406 
00410         bool isInClosedList(dtPolyRef ref) const;
00411         
00414         class dtNodePool* getNodePool() const { return m_nodePool; }
00415         
00418         const dtNavMesh* getAttachedNavMesh() const { return m_nav; }
00419 
00421         
00422 private:
00423         
00425         dtMeshTile* getNeighbourTileAt(int x, int y, int side) const;
00426 
00428         int queryPolygonsInTile(const dtMeshTile* tile, const float* qmin, const float* qmax, const dtQueryFilter* filter,
00429                                                         dtPolyRef* polys, const int maxPolys) const;
00431         dtPolyRef findNearestPolyInTile(const dtMeshTile* tile, const float* center, const float* extents,
00432                                                                         const dtQueryFilter* filter, float* nearestPt) const;
00434         void closestPointOnPolyInTile(const dtMeshTile* tile, const dtPoly* poly, const float* pos, float* closest) const;
00435         
00437         dtStatus getPortalPoints(dtPolyRef from, dtPolyRef to, float* left, float* right,
00438                                                          unsigned char& fromType, unsigned char& toType) const;
00439         dtStatus getPortalPoints(dtPolyRef from, const dtPoly* fromPoly, const dtMeshTile* fromTile,
00440                                                          dtPolyRef to, const dtPoly* toPoly, const dtMeshTile* toTile,
00441                                                          float* left, float* right) const;
00442         
00444         dtStatus getEdgeMidPoint(dtPolyRef from, dtPolyRef to, float* mid) const;
00445         dtStatus getEdgeMidPoint(dtPolyRef from, const dtPoly* fromPoly, const dtMeshTile* fromTile,
00446                                                          dtPolyRef to, const dtPoly* toPoly, const dtMeshTile* toTile,
00447                                                          float* mid) const;
00448         
00449         const dtNavMesh* m_nav;                         
00450 
00451         struct dtQueryData
00452         {
00453                 dtStatus status;
00454                 struct dtNode* lastBestNode;
00455                 float lastBestNodeCost;
00456                 dtPolyRef startRef, endRef;
00457                 float startPos[3], endPos[3];
00458                 const dtQueryFilter* filter;
00459         };
00460         dtQueryData m_query;                            
00461 
00462         class dtNodePool* m_tinyNodePool;       
00463         class dtNodePool* m_nodePool;           
00464         class dtNodeQueue* m_openList;          
00465 };
00466 
00470 dtNavMeshQuery* dtAllocNavMeshQuery();
00471 
00475 void dtFreeNavMeshQuery(dtNavMeshQuery* query);
00476 
00477 #endif // DETOURNAVMESHQUERY_H