Menge
Modular Pedestrian Simulation Framework for Research and Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NavMeshLocalizer.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 
45 #ifndef __NAV_MESH_LOCALIZER_H__
46 #define __NAV_MESH_LOCALIZER_H__
47 
48 #include "NavMesh.h"
49 #include "NavMeshNode.h"
50 #include "mengeCommon.h"
51 #include "Resource.h"
52 #include "ReadersWriterLock.h"
53 #include <map>
54 #include <set>
55 
56 namespace Menge {
57 
58  namespace Agents {
59  class BaseAgent;
60  }
61 
62  // Forward declaration
63  class PortalPath;
64  class PathPlanner;
65 
71  public:
75  NavMeshLocation():_nodeID(NO_NODE),_hasPath(false){}
76 
83  NavMeshLocation( unsigned int nodeID ):_nodeID(nodeID), _hasPath(false){}
84 
93  NavMeshLocation( PortalPath * path ):_path(path), _hasPath(true) {}
94 
102  void setNode( unsigned int nodeID );
103 
107  void clearPath();
108 
115  unsigned int getNode() const;
116 
124  void setPath( PortalPath * path );
125 
131  inline bool isPath() const { return _hasPath; }
132 
138  inline bool isNode() const { return !_hasPath; }
139 
145  union {
146  size_t _nodeID;
147  PortalPath * _path;
148  };
149 
155  bool _hasPath;
156 
161  const static unsigned int NO_NODE;
162  };
163 
165 
170  typedef std::set< size_t > OccupantSet;
171 
175  typedef OccupantSet::iterator OccupantSetItr;
176 
180  typedef OccupantSet::const_iterator OccupantSetCItr;
181 
186  class MENGE_API NavMeshLocalizer : public Resource {
187  public:
193  NavMeshLocalizer( const std::string & name );
194 
195  protected:
199  ~NavMeshLocalizer();
200 
201  public:
202 
203 
209  virtual const std::string & getLabel() const { return LABEL; }
210 
219  unsigned int getNode( const Agents::BaseAgent * agent ) const;
220 
228  unsigned int getNode( const Vector2 & p ) const;
229 
236  const NavMeshNode getNode( unsigned int i ) { return _navMesh->getNode( i ); }
237 
245  PortalPath * getPath( size_t id );
246 
253  void setPath( size_t agentID, PortalPath * path );
254 
260  void clearPath( size_t agentID );
261 
268  void setNode( size_t agentID, unsigned int nodeID );
269 
278  void setTrackAll() { _trackAll = true; }
279 
289  unsigned int updateLocation( const Agents::BaseAgent * agent, bool force=false ) const;
290 
296  void setPlanner( PathPlanner * planner ) { _planner = planner; }
297 
303  PathPlanner * getPlanner() { return _planner; }
304 
311  const OccupantSet * getNodeOccupants( unsigned int nodeID ) const { return &_nodeOccupants[ nodeID ]; }
312 
318  const NavMeshPtr getNavMesh() const { return _navMesh; }
319 
325  NavMeshPtr getNavMesh() { return _navMesh; }
326 
328  // Construction functions
330 
343  static Resource * load( const std::string & fileName );
344 
349  static const std::string LABEL;
350 
351  friend class PortalPath;
352 
353  protected:
358 
363  bool _trackAll;
364 
370 
375  // NOTE: In order to call the [] operator in a const function, this needs to
376  // be mutable. Even if the code guarantees no changes.
377  mutable HASH_MAP< size_t, NavMeshLocation > _locations;
378 
383 
388  OccupantSet * _nodeOccupants;
389 
403  unsigned int findNodeBlind( const Vector2 & p, float tgtElev=1e5f ) const;
404 
415  unsigned int testNeighbors( const NavMeshNode & node, const Vector2 & p ) const;
416  };
417 
418 
423 
433  NavMeshLocalizerPtr loadNavMeshLocalizer( const std::string & fileName, bool usePlanner ) throw ( ResourceException );
434 
435 } // namespace Menge
436 
437 #endif // __NAV_MESH_LOCALIZER_H__
Class responsible for tracking agent relatinoships to the navigation mesh: its current location and i...
Definition: NavMeshLocalizer.h:186
void setTrackAll()
Sets the tracking status of the localizer to all agents.
Definition: NavMeshLocalizer.h:278
NavMeshLocation(unsigned int nodeID)
Constructor Initializes the location to being a node id.
Definition: NavMeshLocalizer.h:83
NavMeshLocalizerPtr loadNavMeshLocalizer(const std::string &fileName, bool usePlanner)
Loads the navigation mesh of the given name.
Definition: NavMeshLocalizer.cpp:292
void setNode(unsigned int nodeID)
Sets the current position to being a node.
Definition: NavMeshLocalizer.cpp:56
OccupantSet * _nodeOccupants
A mapping from node id to agent ids, specifying the population of each agent.
Definition: NavMeshLocalizer.h:388
bool _hasPath
Determines interpretation of the location union. If true, _path contains a pointer. Otherwise, _nodeID is an index in the navigation mesh.
Definition: NavMeshLocalizer.h:155
The core namespace. All elements of Menge are contained in this namespace.
Definition: AgentGenerator.cpp:43
PathPlanner * _planner
Optional planner. This is only non-NULL if there is a PathPlanner created either by a NavMeshGoalGene...
Definition: NavMeshLocalizer.h:369
bool isPath() const
Reports if the location is a path.
Definition: NavMeshLocalizer.h:131
NavMeshLocation()
Default constructor.
Definition: NavMeshLocalizer.h:75
NavMeshPtr getNavMesh()
Returns a pointer to the underlying navigation mesh.
Definition: NavMeshLocalizer.h:325
Class for indicating how the location of the agent is defined. Either by a portal path or a node...
Definition: NavMeshLocalizer.h:70
ReadersWriterLock _locLock
Lock for location map (_locations).
Definition: NavMeshLocalizer.h:382
std::set< size_t > OccupantSet
A collection of agent ids. It represents the population of each nav mesh node.
Definition: NavMeshLocalizer.h:170
unsigned int getNode() const
Reports the node the agent is currently in.
Definition: NavMeshLocalizer.cpp:77
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: NavMeshLocalizer.h:209
The navigation mesh adjacency graph node. It corresponds to a convex polygon in the navigation mesh...
Definition: NavMeshNode.h:64
The definition of a readers-writer lock.
Definition: ReadersWriterLock.h:62
Basic class for managing on-disk resources.
Definition: Resource.h:98
const OccupantSet * getNodeOccupants(unsigned int nodeID) const
Returns the occupant set for the given node.
Definition: NavMeshLocalizer.h:311
void setPlanner(PathPlanner *planner)
Set the path planner for the localizer.
Definition: NavMeshLocalizer.h:296
A base exception for resources to throw.
Definition: Resource.h:58
bool isNode() const
Reports if the location is a node.
Definition: NavMeshLocalizer.h:138
OccupantSet::iterator OccupantSetItr
Iterator for an OccupantSet.
Definition: NavMeshLocalizer.h:175
Class for computing paths through a navigation mesh.
Definition: PathPlanner.h:133
OccupantSet::const_iterator OccupantSetCItr
Const iterator for an OccupantSet.
Definition: NavMeshLocalizer.h:180
NavMeshLocation(PortalPath *path)
Constructor Initializes the location to being a path. The NavMeshLocation takes responsibility for de...
Definition: NavMeshLocalizer.h:93
The definition of a readers-writer lock.
ResourcePtr< NavMeshLocalizer > NavMeshLocalizerPtr
forward declaration. See NavMeshLocalizer for more details
Definition: NavMeshLocalizerTask.h:56
PathPlanner * getPlanner()
Get the planner for the localizer.
Definition: NavMeshLocalizer.h:303
HASH_MAP< size_t, NavMeshLocation > _locations
A mapping from agent id to the agent's location w.r.t. the navigation mesh.
Definition: NavMeshLocalizer.h:377
Defines the basic agent properties and functionality that all simulation agents share.
Definition: BaseAgent.h:123
bool _trackAll
Determines if just the position of agents on paths is tracked (true) or the position of all agents (f...
Definition: NavMeshLocalizer.h:363
The basic class for all on-disk resources.
void clearPath()
Clears the path (if any), maintaining the node location.
Definition: NavMeshLocalizer.cpp:66
The definition of a path through space comprising of a sequence of portals.
Definition: PortalPath.h:70
Defines the classes which maintain the navigation mesh data.
The namespace that contains the basic simulation mechanisms.
void setPath(PortalPath *path)
Sets the current position to being the given path.
Definition: NavMeshLocalizer.cpp:88
static const std::string LABEL
The unique label for this data type to be used with resource management.
Definition: NavMeshLocalizer.h:349
static const unsigned int NO_NODE
Signal for indicating that the position is NOT on the navigation mesh.
Definition: NavMeshLocalizer.h:161
const NavMeshNode getNode(unsigned int i)
Returns the NavMeshNode of the given id.
Definition: NavMeshLocalizer.h:236
NavMeshPtr _navMesh
The underlying navigation mesh to perform operations on.
Definition: NavMeshLocalizer.h:357
Defines the "node" of the adjacency graph in a navigation mesh. The node corresponds to a polygon in ...
const NavMeshPtr getNavMesh() const
Returns a const pointer to the underlying navigation mesh.
Definition: NavMeshLocalizer.h:318