Skip to content

Commit

Permalink
Changed dtQueryFilter to contain the flags check and cost calculation…
Browse files Browse the repository at this point in the history
…, can customized. Fix for issue 47 and issue 103.

git-svn-id: http://recastnavigation.googlecode.com/svn/trunk@200 678f7576-1c49-11de-8b5c-13accb87f508
  • Loading branch information
memononen committed Aug 20, 2010
1 parent d2c425c commit c249a27
Show file tree
Hide file tree
Showing 19 changed files with 2,587 additions and 13,525 deletions.
1 change: 1 addition & 0 deletions DebugUtils/Include/DetourDebugDraw.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ enum DrawNavMeshFlags

void duDebugDrawNavMesh(struct duDebugDraw* dd, const dtNavMesh& mesh, unsigned char flags);
void duDebugDrawNavMeshWithClosedList(struct duDebugDraw* dd, const dtNavMesh& mesh, const dtNavMeshQuery& query, unsigned char flags);
void duDebugDrawNavMeshNodes(struct duDebugDraw* dd, const dtNavMeshQuery& query);
void duDebugDrawNavMeshBVTree(struct duDebugDraw* dd, const dtNavMesh& mesh);
void duDebugDrawNavMeshPortals(struct duDebugDraw* dd, const dtNavMesh& mesh);
void duDebugDrawNavMeshPoly(struct duDebugDraw* dd, const dtNavMesh& mesh, dtPolyRef ref, const unsigned int col);
Expand Down
101 changes: 40 additions & 61 deletions DebugUtils/Source/DetourDebugDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "DetourDebugDraw.h"
#include "DetourNavMesh.h"
#include "DetourCommon.h"
#include "DetourNode.h"


static float distancePtLine2d(const float* pt, const float* p, const float* q)
Expand Down Expand Up @@ -220,7 +221,6 @@ static void drawMeshTile(duDebugDraw* dd, const dtNavMesh& mesh, const dtNavMesh
dd->end();
}


const unsigned int vcol = duRGBA(0,0,0,196);
dd->begin(DU_DRAW_POINTS, 3.0f);
for (int i = 0; i < tile->header->vertCount; ++i)
Expand All @@ -229,7 +229,7 @@ static void drawMeshTile(duDebugDraw* dd, const dtNavMesh& mesh, const dtNavMesh
dd->vertex(v[0], v[1], v[2], vcol);
}
dd->end();

dd->depthMask(true);
}

Expand Down Expand Up @@ -257,6 +257,44 @@ void duDebugDrawNavMeshWithClosedList(struct duDebugDraw* dd, const dtNavMesh& m
}
}

void duDebugDrawNavMeshNodes(struct duDebugDraw* dd, const dtNavMeshQuery& query)
{
if (!dd) return;

const dtNodePool* pool = query.getNodePool();
if (pool)
{
const float off = 0.5f;
dd->begin(DU_DRAW_POINTS, 4.0f);
for (int i = 0; i < pool->getHashSize(); ++i)
{
for (unsigned short j = pool->getFirst(i); j != DT_NULL_IDX; j = pool->getNext(j))
{
const dtNode* node = pool->getNodeAtIdx(j+1);
if (!node) continue;
dd->vertex(node->pos[0],node->pos[1]+off,node->pos[2], duRGBA(255,192,0,255));
}
}
dd->end();

dd->begin(DU_DRAW_LINES, 2.0f);
for (int i = 0; i < pool->getHashSize(); ++i)
{
for (unsigned short j = pool->getFirst(i); j != DT_NULL_IDX; j = pool->getNext(j))
{
const dtNode* node = pool->getNodeAtIdx(j+1);
if (!node) continue;
if (!node->pidx) continue;
const dtNode* parent = pool->getNodeAtIdx(node->pidx);
if (!parent) continue;
dd->vertex(node->pos[0],node->pos[1]+off,node->pos[2], duRGBA(255,192,0,128));
dd->vertex(parent->pos[0],parent->pos[1]+off,parent->pos[2], duRGBA(255,192,0,128));
}
}
dd->end();
}
}


static void drawMeshTileBVTree(duDebugDraw* dd, const dtMeshTile* tile)
{
Expand Down Expand Up @@ -291,28 +329,6 @@ void duDebugDrawNavMeshBVTree(duDebugDraw* dd, const dtNavMesh& mesh)
}
}

/*
static void calcRect(const float* va, const float* vb,
float* bmin, float* bmax,
int side, float padx, float pady)
{
if (side == 0 || side == 4)
{
bmin[0] = dtMin(va[2],vb[2]) + padx;
bmin[1] = dtMin(va[1],vb[1]) - pady;
bmax[0] = dtMax(va[2],vb[2]) - padx;
bmax[1] = dtMax(va[1],vb[1]) + pady;
}
else if (side == 2 || side == 6)
{
bmin[0] = dtMin(va[0],vb[0]) + padx;
bmin[1] = dtMin(va[1],vb[1]) - pady;
bmax[0] = dtMax(va[0],vb[0]) - padx;
bmax[1] = dtMax(va[1],vb[1]) + pady;
}
}
*/

static void drawMeshTilePortal(duDebugDraw* dd, const dtMeshTile* tile)
{
// Draw portals
Expand Down Expand Up @@ -358,24 +374,6 @@ static void drawMeshTilePortal(duDebugDraw* dd, const dtMeshTile* tile)

dd->vertex(x,vb[1]-pady,vb[2], col);
dd->vertex(x,va[1]-pady,va[2], col);

/* const float zmin = dtMin(va[2], vb[2]) - padx;
const float zmax = dtMax(va[2], vb[2]) + padx;
const float ymin = dtMin(va[1], vb[1]) - pady;
const float ymax = dtMax(va[1], vb[1]) + pady;
const float x = va[0] + ((side == 0) ? -0.02f : 0.02f);
dd->vertex(x,ymin,zmin, col);
dd->vertex(x,ymin,zmax, col);
dd->vertex(x,ymin,zmax, col);
dd->vertex(x,ymax,zmax, col);
dd->vertex(x,ymax,zmax, col);
dd->vertex(x,ymax,zmin, col);
dd->vertex(x,ymax,zmin, col);
dd->vertex(x,ymin,zmin, col);*/
}
else if (side == 2 || side == 6)
{
Expand All @@ -394,25 +392,6 @@ static void drawMeshTilePortal(duDebugDraw* dd, const dtMeshTile* tile)

dd->vertex(vb[0],vb[1]-pady,z, col);
dd->vertex(va[0],va[1]-pady,z, col);


/* const float xmin = dtMin(va[0], vb[0]) - padx;
const float xmax = dtMax(va[0], vb[0]) + padx;
const float ymin = dtMin(va[1], vb[1]) - pady;
const float ymax = dtMax(va[1], vb[1]) + pady;
const float z = va[2] + ((side == 2) ? -0.02f : 0.02f);
dd->vertex(xmin,ymin,z, col);
dd->vertex(xmax,ymin,z, col);
dd->vertex(xmax,ymin,z, col);
dd->vertex(xmax,ymax,z, col);
dd->vertex(xmax,ymax,z, col);
dd->vertex(xmin,ymax,z, col);
dd->vertex(xmin,ymax,z, col);
dd->vertex(xmin,ymin,z, col);*/
}

}
Expand Down
95 changes: 82 additions & 13 deletions Detour/Include/DetourNavMeshQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,81 @@
#ifndef DETOURNAVMESHQUERY_H
#define DETOURNAVMESHQUERY_H

#include "DetourAlloc.h"
#include "DetourNavMesh.h"

struct dtQueryFilter

// Define DT_VIRTUAL_QUERYFILTER if you wish to derive a custom filter from dtQueryFilter.
// On certain platforms indirect or virtual function call is expensive. The default
// setting is to use non-virtual functions, the actualy implementations of the functions
// are declared as inline for maximum speed.

//#define DT_VIRTUAL_QUERYFILTER 1

// Class for polygon filtering and cost calculation during query operations.
// - It is possible to derive a custom query filter from dtQueryFilter by overriding
// the virtual functions passFilter() and getCost().
// - Both functions should be as fast as possible. Use cached local copy of data
// instead of accessing your own objects where possible.
// - You do not need to adhere to the flags and cost logic provided by the default
// implementation.
// - In order for the A* to work properly, the cost should be proportional to
// the travel distance. Using cost modifier less than 1.0 is likely to lead
// to problems during pathfinding.
class dtQueryFilter
{
dtQueryFilter() : includeFlags(0xffff), excludeFlags(0) {}
unsigned short includeFlags; // If any of the flags are set, the poly is included.
unsigned short excludeFlags; // If any of the flags are set, the poly is excluded.
float m_areaCost[DT_MAX_AREAS]; // Array storing cost per area type, used by default implementation.
unsigned short m_includeFlags; // Include poly flags, used by default implementation.
unsigned short m_excludeFlags; // Exclude poly flags, used by default implementation.

public:
dtQueryFilter();

// Returns true if the polygon is can visited.
// Params:
// ref - (in) reference to the polygon test.
// tile - (in) pointer to the tile of the polygon test.
// poly - (in) pointer to the polygon test.
#ifdef DT_VIRTUAL_QUERYFILTER
virtual bool passFilter(const dtPolyRef ref,
const dtMeshTile* tile,
const dtPoly* poly) const;
#else
bool passFilter(const dtPolyRef ref,
const dtMeshTile* tile,
const dtPoly* poly) const;
#endif

// Returns cost to travel from 'pa' to 'pb'.'
// The segment is fully contained inside 'cur'.
// 'pa' lies on the edge between 'prev' and 'cur',
// 'pb' lies on the edge between 'cur' and 'next'.
// Params:
// pa - (in) segment start position.
// pb - (in) segment end position.
// prevRef, prevTile, prevPoly - (in) data describing the previous polygon, can be null.
// curRef, curTile, curPoly - (in) data describing the current polygon.
// nextRef, nextTile, nextPoly - (in) data describing the next polygon, can be null.
#ifdef DT_VIRTUAL_QUERYFILTER
virtual float getCost(const float* pa, const float* pb,
const dtPolyRef prevRef, const dtMeshTile* prevTile, const dtPoly* prevPoly,
const dtPolyRef curRef, const dtMeshTile* curTile, const dtPoly* curPoly,
const dtPolyRef nextRef, const dtMeshTile* nextTile, const dtPoly* nextPoly) const;
#else
float getCost(const float* pa, const float* pb,
const dtPolyRef prevRef, const dtMeshTile* prevTile, const dtPoly* prevPoly,
const dtPolyRef curRef, const dtMeshTile* curTile, const dtPoly* curPoly,
const dtPolyRef nextRef, const dtMeshTile* nextTile, const dtPoly* nextPoly) const;
#endif

// Getters and setters for the default implementation data.
inline float getAreaCost(const int i) const { return m_areaCost[i]; }
inline void setAreaCost(const int i, const float cost) { m_areaCost[i] = cost; }

inline unsigned short getIncludeFlags() const { return m_includeFlags; }
inline void setIncludeFlags(const unsigned short flags) { m_includeFlags = flags; }

inline unsigned short getExcludeFlags() const { return m_excludeFlags; }
inline void setExcludeFlags(const unsigned short flags) { m_excludeFlags = flags; }
};

enum dtQueryState
Expand All @@ -47,18 +114,18 @@ class dtNavMeshQuery
// nav - (in) pointer to navigation mesh data.
// maxNodes - (in) Maximum number of search nodes to use (max 65536).
// Returns: True if succeed, else false.
bool init(dtNavMesh* nav, const int maxNodes);
bool init(class dtNavMesh* nav, const int maxNodes);

// Sets the pathfinding cost of the specified area.
// Params:
// area - (in) area ID (0-63).
// cost - (int) travel cost of the area.
void setAreaCost(const int area, float cost);
// void setAreaCost(const int area, float cost);

// Returns the pathfinding cost of the specified area.
// Params:
// area - (in) area ID (0-63).
float getAreaCost(const int area) const;
// float getAreaCost(const int area) const;

// Finds the nearest navigation polygon around the center location.
// Params:
Expand Down Expand Up @@ -101,8 +168,10 @@ class dtNavMeshQuery
dtPolyRef* path, const int maxPathSize) const;

// Intializes sliced path find query.
// Note: calling any other dtNavMeshQuery method before calling findPathEnd()
// Note 1: calling any other dtNavMeshQuery method before calling findPathEnd()
// may results in corrupted data!
// Note 2: The pointer to filter is store, and used in subsequent
// calls to updateSlicedFindPath().
// Params:
// startRef - (in) ref to path start polygon.
// endRef - (in) ref to path end polygon.
Expand Down Expand Up @@ -285,6 +354,8 @@ class dtNavMeshQuery
// Returns true if poly reference ins in closed list.
bool isInClosedList(dtPolyRef ref) const;

class dtNodePool* getNodePool() const { return m_nodePool; }

private:

// Returns neighbour tile based on side.
Expand Down Expand Up @@ -312,7 +383,7 @@ class dtNavMeshQuery
dtPolyRef to, const dtPoly* toPoly, const dtMeshTile* toTile,
float* mid) const;

dtNavMesh* m_nav; // Pointer to navmesh data.
class dtNavMesh* m_nav; // Pointer to navmesh data.

struct dtQueryData
{
Expand All @@ -321,12 +392,10 @@ class dtNavMeshQuery
float lastBestNodeCost;
dtPolyRef startRef, endRef;
float startPos[3], endPos[3];
dtQueryFilter filter;
const dtQueryFilter* filter;
};
dtQueryData m_query; // Sliced query state.

float m_areaCost[DT_MAX_AREAS]; // Cost per area.

class dtNodePool* m_tinyNodePool; // Pointer to small node pool.
class dtNodePool* m_nodePool; // Pointer to node pool.
class dtNodeQueue* m_openList; // Pointer to open list queue.
Expand Down
13 changes: 13 additions & 0 deletions Detour/Include/DetourNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ enum dtNodeFlags
DT_NODE_CLOSED = 0x02,
};

static const unsigned short DT_NULL_IDX = 0xffff;

struct dtNode
{
float pos[3];
float cost;
float total;
unsigned int id;
Expand Down Expand Up @@ -55,6 +58,12 @@ class dtNodePool
if (!idx) return 0;
return &m_nodes[idx-1];
}

inline const dtNode* getNodeAtIdx(unsigned int idx) const
{
if (!idx) return 0;
return &m_nodes[idx-1];
}

inline int getMemUsed() const
{
Expand All @@ -66,6 +75,10 @@ class dtNodePool

inline int getMaxNodes() const { return m_maxNodes; }

inline int getHashSize() const { return m_hashSize; }
inline unsigned short getFirst(int bucket) const { return m_first[bucket]; }
inline unsigned short getNext(int i) const { return m_next[i]; }

private:
inline unsigned int hashint(unsigned int a) const
{
Expand Down
Loading

0 comments on commit c249a27

Please sign in to comment.