Menge
Modular Pedestrian Simulation Framework for Research and Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NavMesh.h
Go to the documentation of this file.
1 /*
2 
3 License
4 
5 Menge
6 Copyright and trademark 2012-14 University of North Carolina at Chapel Hill.
7 All rights reserved.
8 
9 Permission to use, copy, modify, and distribute this software and its documentation
10 for educational, research, and non-profit purposes, without fee, and without a
11 written agreement is hereby granted, provided that the above copyright notice,
12 this paragraph, and the following four paragraphs appear in all copies.
13 
14 This software program and documentation are copyrighted by the University of North
15 Carolina at Chapel Hill. The software program and documentation are supplied "as is,"
16 without any accompanying services from the University of North Carolina at Chapel
17 Hill or the authors. The University of North Carolina at Chapel Hill and the
18 authors do not warrant that the operation of the program will be uninterrupted
19 or error-free. The end-user understands that the program was developed for research
20 purposes and is advised not to rely exclusively on the program for any reason.
21 
22 IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE AUTHORS
23 BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
24 DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
25 DOCUMENTATION, EVEN IF THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE
26 AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS SPECIFICALLY
29 DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY STATUTORY WARRANTY
31 OF NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND
32 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS HAVE NO OBLIGATIONS
33 TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
34 
35 Any questions or comments should be sent to the authors {menge,geom}@cs.unc.edu
36 
37 */
38 
44 #ifndef __NAV_MESH_DATA_H__
45 #define __NAV_MESH_DATA_H__
46 
47 #include "mengeCommon.h"
48 #include "NavMeshObstacle.h"
49 #include "Resource.h"
50 #include <map>
51 #include <vector>
53 
54 namespace Menge {
55 
56  namespace Agents {
57  class SimulatorInterface;
58  }
59 
60  // forward declarations
61  class NavMesh;
62  class NavMeshNode;
63  class NavMeshEdge;
64  class NavMeshLocalizer;
65  class PathPlanner;
66 
74  class NMNodeGroup {
75  public:
79  NMNodeGroup(): _first(0), _last(0) {}
80 
88  NMNodeGroup( unsigned int first, unsigned int last ): _first(first), _last(last) {}
89 
93  inline size_t groupSize() const { return static_cast< size_t >( _last - _first + 1 ); }
94 
98  unsigned int _first;
99 
103  unsigned int _last;
104  };
105 
106  class NavMeshFactory;
107 
120  class NavMesh : public Resource {
121  public:
127  NavMesh( const std::string & name );
128 
129  protected:
133  ~NavMesh();
134 
135  public:
139  void clear();
140 
146  virtual const std::string & getLabel() const { return LABEL; }
147 
149  // Getters/Setters
151 
157  inline size_t getVertexCount() const { return _vCount; }
158 
164  inline size_t getNodeCount() const { return _nCount; }
165 
174  NavMeshNode & getNode( unsigned int i );
175 
184  const NavMeshNode & getNode( unsigned int i ) const;
185 
191  inline size_t getEdgeCount() const { return _eCount; }
192 
201  NavMeshEdge & getEdge( unsigned int i );
202 
211  const NavMeshEdge & getEdge( unsigned int i ) const;
212 
218  size_t getObstacleCount() const { return _obstCount; }
219 
227  NavMeshObstacle & getObstacle( unsigned int i );
228 
236  const NavMeshObstacle & getObstacle( unsigned int i ) const;
237 
243  inline Vector2 * getVertices() { return &_vertices[0]; }
244 
250  inline const Vector2 * getVertices() const { return &_vertices[0]; }
251 
253  // Geometric queries
255 
264  float getElevation( unsigned int nodeID, const Vector2 & p ) const;
265 
274  Vector2 getGradient( unsigned int nodeID, const Vector2 & p ) const;
275 
277  // Construction functions
279 
292  static Resource * load( const std::string & fileName );
293 
300  void setVertexCount( size_t count );
301 
310  void setVertex( unsigned int i, float x, float y );
311 
318  void setNodeCount( size_t count );
319 
326  void setEdgeCount( size_t count );
327 
334  void setObstacleCount( size_t count );
335 
341  bool finalize();
342 
355  bool addGroup( const std::string & grpName, size_t grpSize );
356 
358  // * @brief Adds the navigation mesh's obstacles to the simulator.
359  // *
360  // * This should only be used if the navigation mesh is used as a spatial query.
361  // *
362  // * @param simulator The simulator which receives the obstacles.
363  // *
364  //void addObstacles( Agents::SimulatorInterface * simulator );
365 
371  std::vector<Agents::ObstacleVertexList> getObstacles();
372 
377  static const std::string LABEL;
378 
379  friend class NavMeshFactory;
380  friend class PathPlanner;
381 
382  protected:
386  size_t _vCount;
387 
392 
396  size_t _nCount;
397 
402 
406  size_t _eCount;
407 
412 
416  size_t _obstCount;
417 
422 
426  std::map< const std::string, NMNodeGroup > _nodeGroups;
427  };
428 
433 
441  NavMeshPtr loadNavMesh( const std::string & fileName ) throw ( ResourceException );
442 
443 } // namespace Menge
444 
445 #endif // __NAV_MESH_DATA_H__
void setVertex(unsigned int i, float x, float y)
Sets the value of the ith vertex.
Definition: NavMesh.cpp:158
NavMeshPtr loadNavMesh(const std::string &fileName)
Loads the navigation mesh of the given name.
Definition: NavMesh.cpp:528
NavMesh(const std::string &name)
Constructor.
Definition: NavMesh.cpp:59
size_t _nCount
The number of nodes (aka polygons in the mesh).
Definition: NavMesh.h:396
NavMeshEdge * _edges
An array containing all edges.
Definition: NavMesh.h:411
The core namespace. All elements of Menge are contained in this namespace.
Definition: AgentGenerator.cpp:43
NavMeshObstacle * _obstacles
An array of obstacles.
Definition: NavMesh.h:421
NavMeshObstacle & getObstacle(unsigned int i)
Returns a reference to the ith obstacle.
Definition: NavMesh.cpp:120
std::map< const std::string, NMNodeGroup > _nodeGroups
The mapping from node group name to an instance of a NMNodeGroup.
Definition: NavMesh.h:426
bool addGroup(const std::string &grpName, size_t grpSize)
Adds a group of polygons to the navigation mesh.
Definition: NavMesh.cpp:195
void setVertexCount(size_t count)
Allocates memory for the given number of vertices. All previous vertices will be deleted.
Definition: NavMesh.cpp:148
unsigned int _first
The first index in the group.
Definition: NavMesh.h:98
const Vector2 * getVertices() const
Returns a const pointer to the array of vertices.
Definition: NavMesh.h:250
The navigation mesh adjacency graph node. It corresponds to a convex polygon in the navigation mesh...
Definition: NavMeshNode.h:64
bool finalize()
After initializing the navigation mesh's components this function needs to be called to make the mesh...
Definition: NavMesh.cpp:225
Vector2 getGradient(unsigned int nodeID, const Vector2 &p) const
Computes the gradient at a particular point based on the given node's geometry.
Definition: NavMesh.cpp:141
Basic class for managing on-disk resources.
Definition: Resource.h:98
size_t groupSize() const
Reports the number of nodes in this group.
Definition: NavMesh.h:93
The definition of the set of vertices for an explicit obstacle definition.
A base exception for resources to throw.
Definition: Resource.h:58
NMNodeGroup()
Default constructor.
Definition: NavMesh.h:79
The navigation mesh adjacency graph edge.
Definition: NavMeshEdge.h:72
ResourcePtr< NavMesh > NavMeshPtr
The definition of the managed pointer for NavMesh data.
Definition: NavMesh.h:432
Class for computing paths through a navigation mesh.
Definition: PathPlanner.h:133
std::vector< Agents::ObstacleVertexList > getObstacles()
Gets the navigation mesh's obstacles for the simulator.
Definition: NavMesh.cpp:364
Vector2 * _vertices
An array containing all vertices.
Definition: NavMesh.h:391
unsigned int _last
The last index in the group.
Definition: NavMesh.h:103
size_t _vCount
The number of vertices.
Definition: NavMesh.h:386
size_t getVertexCount() const
Reports the number of vertices in the navigation mesh.
Definition: NavMesh.h:157
size_t getEdgeCount() const
Reports the number of edges in the navigation mesh.
Definition: NavMesh.h:191
void setNodeCount(size_t count)
Allocates memory for the given number of nodes. All previous nodes will be deleted.
Definition: NavMesh.cpp:165
virtual const std::string & getLabel() const
Returns a unique resource label to be used to identify different resource types which use the same un...
Definition: NavMesh.h:146
Specification of an obstacle. It is the same as a pedModel specification but includes a pointer to a ...
Definition: NavMeshObstacle.h:61
NMNodeGroup(unsigned int first, unsigned int last)
Constructor.
Definition: NavMesh.h:88
size_t getObstacleCount() const
Returns the number of obstacles connected to this node.
Definition: NavMesh.h:218
static Resource * load(const std::string &fileName)
Parses a navigation mesh definition and returns a pointer to it.
Definition: NavMesh.cpp:417
size_t _obstCount
The number of obstacles in the scene.
Definition: NavMesh.h:416
A navigation mesh node group.
Definition: NavMesh.h:74
void setObstacleCount(size_t count)
Allocates memory for the given number of obstacles. All previous obstacles will be delete...
Definition: NavMesh.cpp:185
float getElevation(unsigned int nodeID, const Vector2 &p) const
Computes the elevation at a particular point based on the given node's geometry.
Definition: NavMesh.cpp:134
The class for defining a navigation mesh. A decomposition of the free space into a connected mesh of ...
Definition: NavMesh.h:120
size_t _eCount
The number of edges (aka portals or shared edges between polygons).
Definition: NavMesh.h:406
Specification for obstacles in a navigation mesh file.
The basic class for all on-disk resources.
NavMeshNode & getNode(unsigned int i)
Returns a reference to the ith node.
Definition: NavMesh.cpp:92
size_t getNodeCount() const
Reports the number of nodes in the navigation mesh.
Definition: NavMesh.h:164
void clear()
Clears the navigation mesh - removes edges and nodes.
Definition: NavMesh.cpp:70
The namespace that contains the basic simulation mechanisms.
NavMeshEdge & getEdge(unsigned int i)
Returns a reference to the ith edge.
Definition: NavMesh.cpp:106
static const std::string LABEL
The unique label for this data type to be used with resource management.
Definition: NavMesh.h:377
void setEdgeCount(size_t count)
Allocates memory for the given number of edges. All previous edges will be deleted.
Definition: NavMesh.cpp:175
~NavMesh()
Destructor.
Definition: NavMesh.cpp:64
Vector2 * getVertices()
Returns a pointer to the array of vertices.
Definition: NavMesh.h:243
NavMeshNode * _nodes
An array containing all nodes.
Definition: NavMesh.h:401