Planeshift
celnavmesh.h
Go to the documentation of this file.
1 /*
2  Crystal Space Entity Layer
3  Copyright (C) 2010 by Leonardo Rodrigo Domingues
4  Copyright (C) 2011 by Matthieu Kraus
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public
17  License along with this library; if not, write to the Free
18  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20 
21 #ifndef __CEL_NAVMESH__
22 #define __CEL_NAVMESH__
23 
24 #include <cssysdef.h>
25 #include <csgeom/obb.h>
26 #include <csgeom/plane3.h>
27 #include <csgeom/tri.h>
28 #include <csgeom/vector3.h>
29 #include <csqsqrt.h>
30 #include <cstool/csapplicationframework.h>
31 #include <csutil/list.h>
32 #include <csutil/ref.h>
33 #include <csutil/scf_implementation.h>
34 #include <csutil/threadmanager.h>
35 #include <iengine/mesh.h>
36 #include <iengine/movable.h>
37 #include <iengine/portal.h>
38 #include <iengine/portalcontainer.h>
39 #include <iengine/sector.h>
40 #include <igeom/trimesh.h>
41 #include <imesh/objmodel.h>
42 #include <imesh/terrain2.h>
43 #include <iutil/comp.h>
44 #include <iutil/document.h>
45 #include <iutil/objreg.h>
46 #include <iutil/vfs.h>
47 #include <tools/celnavmesh.h>
55 
57 {
58 
59 
60 
61 /*
62  * Recast structures
63  */
64 static const int MAX_CONVEXVOL_PTS = 12;
65 struct ConvexVolume
66 {
67  float verts[MAX_CONVEXVOL_PTS * 3];
68  float hmin, hmax;
69  int nverts;
70  int area;
71 };
72 
73 // These are just sample areas to use consistent values across the samples.
74 // The use should specify these base on his needs.
75 enum SamplePolyAreas
76 {
77  SAMPLE_POLYAREA_GROUND,
78  SAMPLE_POLYAREA_WATER,
79  SAMPLE_POLYAREA_ROAD,
80  SAMPLE_POLYAREA_DOOR,
81  SAMPLE_POLYAREA_GRASS,
82  SAMPLE_POLYAREA_JUMP,
83 };
84 enum SamplePolyFlags
85 {
86  SAMPLE_POLYFLAGS_WALK = 0x01, // Ability to walk (ground, grass, road)
87  SAMPLE_POLYFLAGS_SWIM = 0x02, // Ability to swim (water).
88  SAMPLE_POLYFLAGS_DOOR = 0x04, // Ability to move through doors.
89  SAMPLE_POLYFLAGS_JUMP = 0x08, // Ability to jump.
90  SAMPLE_POLYFLAGS_ALL = 0xffff // All abilities.
91 };
92 
101 class DebugDrawCS : public duDebugDraw
102 {
103 private:
104  csSimpleRenderMesh* currentMesh;
105  csZBufMode currentZBufMode;
106  csArray<csSimpleRenderMesh*>* meshes;
107  csArray<csVector3> vertices;
108  csArray<csVector4> colors;
109  int nVertices;
110 
111 public:
112  DebugDrawCS ();
113  virtual ~DebugDrawCS ();
114  csArray<csSimpleRenderMesh*>* GetMeshes ();
115 
116  // duDebugDraw
117  virtual void depthMask (bool state);
118  virtual void texture(bool state);
119  virtual void begin (duDebugDrawPrimitives prim, float size = 1.0f);
120  virtual void vertex (const float* pos, unsigned int color);
121  virtual void vertex (const float x, const float y, const float z, unsigned int color);
122  virtual void vertex (const float* pos, unsigned int color, const float* uv);
123  virtual void vertex (const float x, const float y, const float z, unsigned int color, const float u, const float v);
124  virtual void end ();
125 };
126 
127 
128 
132 class celNavMeshParams : public scfImplementation1<celNavMeshParams, iCelNavMeshParams>
133 {
134 private:
135  float agentHeight;
136  float agentRadius;
137  float agentMaxSlopeAngle;
138  float agentMaxClimb;
139  float cellSize;
140  float cellHeight;
141  float maxSimplificationError;
142  float detailSampleDist;
143  float detailSampleMaxError;
144  float distanceThreshold;
145  int maxEdgeLength;
146  int minRegionArea;
147  int mergeRegionArea;
148  int maxVertsPerPoly;
149  int tileSize;
150  int borderSize;
151  csVector3 polygonSearchBox;
152 
153 public:
154  celNavMeshParams ();
155  celNavMeshParams (const iCelNavMeshParams* parameters);
156  virtual ~celNavMeshParams ();
157 
158  iCelNavMeshParams* Clone () const;
159  virtual void SetSuggestedValues (float agentHeight, float agentRadius, float agentMaxSlopeAngle);
160 
161  virtual float GetAgentHeight () const;
162  virtual void SetAgentHeight (const float height);
163  virtual float GetAgentRadius () const;
164  virtual void SetAgentRadius (const float radius);
165  virtual float GetAgentMaxSlopeAngle () const;
166  virtual void SetAgentMaxSlopeAngle (const float angle);
167  virtual float GetAgentMaxClimb () const;
168  virtual void SetAgentMaxClimb (const float maxClimb);
169  virtual float GetCellSize () const;
170  virtual void SetCellSize (const float size);
171  virtual float GetCellHeight () const;
172  virtual void SetCellHeight (const float height);
173  virtual float GetMaxSimplificationError () const;
174  virtual void SetMaxSimplificationError (const float error);
175  virtual float GetDetailSampleDist () const;
176  virtual void SetDetailSampleDist (const float dist);
177  virtual float GetDetailSampleMaxError () const;
178  virtual void SetDetailSampleMaxError (const float error);
179  virtual int GetMaxEdgeLength () const;
180  virtual void SetMaxEdgeLength (const int length);
181  virtual int GetMinRegionArea () const;
182  virtual void SetMinRegionArea (const int area);
183  virtual int GetMergeRegionArea () const;
184  virtual void SetMergeRegionArea (const int area);
185  virtual int GetMaxVertsPerPoly () const;
186  virtual void SetMaxVertsPerPoly (const int maxVerts);
187  virtual int GetTileSize () const;
188  virtual void SetTileSize (const int size);
189  virtual int GetBorderSize () const;
190  virtual void SetBorderSize (const int size);
191  virtual csVector3 GetPolygonSearchBox () const;
192  virtual void SetPolygonSearchBox (const csVector3 box);
193 };
194 
195 
196 
200 // Based on Recast NavMeshTesterTool
201 class celNavMeshPath : public scfImplementation1<celNavMeshPath, iCelNavMeshPath>
202 {
203 private:
204  float* path;
205  int pathSize; // In nodes
206  int maxPathSize;
207  int currentPosition; // Path array position, not point index
208  int increasePosition; // Value to be added to currentPosition to get next element
209  static const int INCREASE_PATH_BY; // Increase path vector by this amount when the it gets full
210  csRef<iSector> sector;
211  csArray<csSimpleRenderMesh*>* debugMeshes;
212 
213 public:
214  celNavMeshPath (float* path, int pathSize, int maxPathSize, iSector* sector);
215  virtual ~celNavMeshPath ();
216 
217  // API
218  virtual iSector* GetSector () const;
219  virtual void Current (csVector3& vector) const;
220  virtual void Next (csVector3& vector);
221  virtual void Previous (csVector3& vector);
222  virtual void GetFirst (csVector3& vector) const;
223  virtual void GetLast (csVector3& vector) const;
224  virtual bool HasNext () const;
225  virtual bool HasPrevious () const;
226  virtual void Invert ();
227  virtual void Restart ();
228  virtual void AddNode (csVector3 node);
229  virtual void InsertNode (int pos, csVector3 node);
230  virtual float Length () const;
231  virtual int GetNodeCount () const;
232  virtual csArray<csSimpleRenderMesh*>* GetDebugMeshes ();
233 };
234 
235 
236 
240 class celNavMesh : public scfImplementation1<celNavMesh, iCelNavMesh>
241 {
242 private:
243  struct NavMeshSetHeader
244  {
245  int magic;
246  int version;
247  int numTiles;
248  dtNavMeshParams params;
249  };
250 
251  struct NavMeshTileHeader
252  {
253  dtTileRef tileRef;
254  int dataSize;
255  };
256 
257  csRef<iSector> sector;
258  iObjectRegistry* objectRegistry;
259  csRef<iCelNavMeshPath> path;
260  dtQueryFilter filter;
261  dtNavMesh* detourNavMesh;
262  dtNavMeshQuery* detourNavMeshQuery;
263  csRef<iCelNavMeshParams> parameters;
264  csArray<csSimpleRenderMesh*>* debugMeshes;
265  csArray<csSimpleRenderMesh*>* agentDebugMeshes;
266  float boundingMin[3];
267  float boundingMax[3];
268  unsigned char navMeshDrawFlags;
269  static const int MAX_NODES;
270  static const int NAVMESHSET_MAGIC;
271  static const int NAVMESHSET_VERSION;
272 
273  bool LoadCelNavMeshParams (iDocumentNode* mainNode);
274  bool LoadDtNavMeshParams (iDocumentNode* paramsNode, dtNavMeshParams& params);
275  bool LoadNavMeshLegacy (iFile* file);
276 public:
277  celNavMesh (iObjectRegistry* objectRegistry);
278  virtual ~celNavMesh ();
279 
280  bool Initialize (const iCelNavMeshParams* parameters, iSector* sector, const float* boundingMin,
281  const float* boundingMax);
282  bool AddTile (unsigned char* data, int dataSize);
283  bool RemoveTile (int x, int y);
284  bool LoadNavMesh (iFile* file);
285 
286  // API
287  virtual iCelNavMeshPath* ShortestPath (const csVector3& from, const csVector3& goal, int maxPathSize = 32);
288  virtual bool Update (const csBox3& boundingBox);
289  virtual bool Update (const csOBB& boundingBox);
290  virtual iSector* GetSector () const;
291  virtual void SetSector (iSector* sector);
292  virtual iCelNavMeshParams* GetParameters () const;
293  virtual csBox3 GetBoundingBox() const;
294  virtual csArray<csPoly3D> QueryPolygons(const csBox3& box) const;
295  virtual bool SaveToFile (iFile* file) const;
296  virtual csArray<csSimpleRenderMesh*>* GetDebugMeshes ();
297  virtual csArray<csSimpleRenderMesh*>* GetAgentDebugMeshes (const csVector3& pos);
298  virtual csArray<csSimpleRenderMesh*>* GetAgentDebugMeshes (const csVector3& pos, int red, int green,
299  int blue, int alpha);
300  virtual void ResetAgentDebugMeshes ();
301 };
302 
303 
304 
308 class celNavMeshBuilder : public ThreadedCallable<celNavMeshBuilder>,
309  public scfImplementation2<celNavMeshBuilder, iCelNavMeshBuilder, iComponent>
310 {
311 private:
312  // Crystal space & CEL
313  iObjectRegistry* objectRegistry;
314  csRef<iSector> currentSector;
315  csRef<iStringSet> strings;
316 
317  // Recast & Detour
318  rcChunkyTriMesh* chunkyTriMesh;
319 
320  // Tile specific
321  unsigned char* triangleAreas;
322  rcContext dummy;
323  rcHeightfield* solid;
325  rcContourSet* cSet;
326  rcPolyMesh* pMesh;
327  rcPolyMeshDetail* dMesh;
328 
329  // Off-Mesh connections.
330  static const int MAX_OFFMESH_CONNECTIONS = 256;
331  float offMeshConVerts[MAX_OFFMESH_CONNECTIONS * 3 * 2];
332  float offMeshConRads[MAX_OFFMESH_CONNECTIONS];
333  unsigned char offMeshConDirs[MAX_OFFMESH_CONNECTIONS];
334  unsigned char offMeshConAreas[MAX_OFFMESH_CONNECTIONS];
335  unsigned short offMeshConFlags[MAX_OFFMESH_CONNECTIONS];
336  int numberOfOffMeshCon;
337 
338  // Convex Volumes
339  static const int MAX_VOLUMES = 256;
340  ConvexVolume volumes[MAX_VOLUMES];
341  int numberOfVolumes;
342 
343  csRef<iCelNavMeshParams> parameters;
344  csRef<celNavMesh> navMesh;
345 
346  // Others
347  int numberOfVertices;
348  float* triangleVertices;
349  int numberOfTriangles;
350  int* triangleIndices;
351  float boundingMin[3];
352  float boundingMax[3];
353 
354  void CleanUpSectorData ();
355  void CleanUpTileData ();
356  bool GetSectorData ();
357  unsigned char* BuildTile(const int tx, const int ty, const float* bmin, const float* bmax,
358  const rcConfig& tileConfig, int& dataSize);
359  iObjectRegistry* GetObjectRegistry() const { return objectRegistry; }
360 
361  // helper function to check whether an object has to be clipped
362  // stores whether the object has to be clipped in result and returns true if it's visible
363  bool CheckClipping(const csPlane3& clipPlane, const csBox3& bbox, bool& result);
364 
365  // helper function to clip a convex polygon to a plane and triangulate it apllying an object to world transform
366  void SplitPolygon(int indexOffset, int numVerts, csVector3* poly, csArray<csVector3>& vertices, csArray<csTriangle>& triangles,
367  const csReversibleTransform& t, csPlane3& clipPlane, bool clipPolygon);
368 
369  // helper function to add a bul of triangles to the sector data
370 
371 public:
372  celNavMeshBuilder (iBase* parent);
373  virtual ~celNavMeshBuilder ();
374  virtual bool Initialize (iObjectRegistry*);
375  bool UpdateNavMesh (celNavMesh* navMesh, const csBox3& boundingBox);
376 
377  // API
378  virtual bool SetSector (iSector* sector);
379  THREADED_CALLABLE_DECL(celNavMeshBuilder,BuildNavMesh,csThreadReturn,THREADEDL,false,false);
380  virtual iCelNavMesh* LoadNavMesh (iFile* file);
381  virtual const iCelNavMeshParams* GetNavMeshParams () const;
382  virtual void SetNavMeshParams (const iCelNavMeshParams* parameters);
383  virtual iSector* GetSector () const;
384 
385 };
386 
387 }
388 CS_PLUGIN_NAMESPACE_END(celNavMesh)
389 
390 #endif // __CEL_NAVMESH__
A dynamic heightfield representing obstructed space.
Definition: Recast.h:274
unsigned int dtTileRef
A handle to a tile within a navigation mesh.
Definition: DetourNavMesh.h:34
Provides an interface for optional logging and performance tracking of the Recast build process...
Definition: Recast.h:101
Specifies a configuration to use when performing Recast builds.
Definition: Recast.h:178
CS_PLUGIN_NAMESPACE_BEGIN(celNavMesh)
Definition: celnavmesh.h:56
float hmin
Definition: InputGeom.h:29
Defines polygon filtering and traversal costs for navigation mesh query operations.
A navigation mesh based on tiles of convex polygons.
A compact, static heightfield representing unobstructed space.
Definition: Recast.h:305
Represents a group of related contours.
Definition: Recast.h:368
float hmax
Definition: InputGeom.h:29
Represents a polygon mesh suitable for use in building a navigation mesh.
Definition: Recast.h:383
void void Initialize(iObjectRegistry *object_reg)
static const int MAX_CONVEXVOL_PTS
Definition: InputGeom.h:25
float verts[MAX_CONVEXVOL_PTS *3]
Definition: InputGeom.h:28
Abstract debug draw interface.
Definition: DebugDraw.h:34
Configuration parameters used to define multi-tile navigation meshes. The values are used to allocate...
Provides the ability to perform pathfinding related queries against a navigation mesh.
duDebugDrawPrimitives
Definition: DebugDraw.h:25
Contains triangle meshes that represent detailed height data associated with the polygons in its asso...
Definition: Recast.h:404