space.h
Go to the documentation of this file.
1 
9 #ifndef SPACE_H
10 #define SPACE_H
11 
12 namespace argos {
13  class CSpace;
14  class CRay3;
15  class CFloorEntity;
16  class CSimulator;
17 }
18 
19 #include <argos3/core/utility/datatypes/any.h>
20 #include <argos3/core/simulator/medium/medium.h>
21 #include <argos3/core/simulator/entity/embodied_entity.h>
22 #include <argos3/core/simulator/entity/controllable_entity.h>
23 
24 namespace argos {
25 
26  /****************************************/
27  /****************************************/
28 
30 
31  public:
32 
53  typedef std::map <std::string, CAny, std::less <std::string> > TMapPerType;
54 
76  typedef std::map <std::string, TMapPerType, std::less <std::string> > TMapPerTypePerId;
77 
78  /****************************************/
79  /****************************************/
80 
81  public:
82 
86  CSpace();
87 
91  virtual ~CSpace() {}
92 
97  virtual void Init(TConfigurationNode& t_tree);
98 
102  virtual void Reset();
103 
107  virtual void Destroy();
108 
112  inline UInt32 GetNumberEntities() const {
113  return m_vecEntities.size();
114  }
115 
123  return m_vecEntities;
124  }
125 
137  return m_vecRootEntities;
138  }
139 
146  inline CEntity& GetEntity(const std::string& str_id) {
147  CEntity::TMap::const_iterator it = m_mapEntitiesPerId.find(str_id);
148  if ( it != m_mapEntitiesPerId.end()) {
149  return *(it->second);
150  }
151  THROW_ARGOSEXCEPTION("Unknown entity id \"" << str_id <<
152  "\" when requesting entity from space.");
153  }
154 
163  void GetEntitiesMatching(CEntity::TVector& t_buffer,
164  const std::string& str_pattern);
165 
171  return m_mapEntitiesPerId;
172  }
173 
191  inline TMapPerTypePerId& GetEntityMapPerTypePerId() {
193  }
194 
211  TMapPerType& GetEntitiesByType(const std::string& str_type);
212 
219  if(m_pcFloorEntity != NULL) return *m_pcFloorEntity;
220  else THROW_ARGOSEXCEPTION("No floor entity has been added to the arena.");
221  }
222 
227  inline void SetFloorEntity(CFloorEntity& c_floor_entity) {
228  m_pcFloorEntity = &c_floor_entity;
229  }
230 
245  virtual void Update();
246 
252  template <typename ENTITY>
253  void AddEntity(ENTITY& c_entity) {
254  std::string strEntityQualifiedName = c_entity.GetContext() + c_entity.GetId();
255  /* Check that the id of the entity is not already present */
256  if(m_mapEntitiesPerId.find(strEntityQualifiedName) != m_mapEntitiesPerId.end()) {
257  THROW_ARGOSEXCEPTION("Error inserting a " <<
258  c_entity.GetTypeDescription() <<
259  " entity with id \"" <<
260  strEntityQualifiedName <<
261  "\". An entity with that id already exists.");
262  }
263  /* Add the entity to the indexes */
264  if(!c_entity.HasParent()) {
265  m_vecRootEntities.push_back(&c_entity);
266  }
267  /* Calculate index of in the global vector */
268  size_t unIdx =
269  !m_vecEntities.empty() ?
270  m_vecEntities.back()->GetIndex() + 1
271  :
272  0;
273  /* Add entity to global vector */
274  m_vecEntities.push_back(&c_entity);
275  c_entity.SetIndex(unIdx);
276  m_mapEntitiesPerId[strEntityQualifiedName] = &c_entity;
277  m_mapEntitiesPerTypePerId[c_entity.GetTypeDescription()][strEntityQualifiedName] = &c_entity;
278  }
279 
285  template <typename ENTITY>
286  void RemoveEntity(ENTITY& c_entity) {
287  std::string strEntityQualifiedName = c_entity.GetContext() + c_entity.GetId();
288  /* Search for entity in the index per type */
289  TMapPerTypePerId::iterator itMapPerType = m_mapEntitiesPerTypePerId.find(c_entity.GetTypeDescription());
290  if(itMapPerType != m_mapEntitiesPerTypePerId.end()) {
291  /* Search for entity in the index per type per id */
292  TMapPerType::iterator itMapPerTypePerId = itMapPerType->second.find(strEntityQualifiedName);
293  if(itMapPerTypePerId != itMapPerType->second.end()) {
294  /* Remove the entity from the indexes */
295  CEntity::TVector::iterator itVec = find(m_vecEntities.begin(),
296  m_vecEntities.end(),
297  &c_entity);
298  m_vecEntities.erase(itVec);
299  CEntity::TMap::iterator itMap = m_mapEntitiesPerId.find(strEntityQualifiedName);
300  itMapPerType->second.erase(itMapPerTypePerId);
301  m_mapEntitiesPerId.erase(itMap);
302  if(!c_entity.HasParent()) {
303  CEntity::TVector::iterator itRootVec = find(m_vecRootEntities.begin(),
304  m_vecRootEntities.end(),
305  &c_entity);
306  m_vecRootEntities.erase(itRootVec);
307  }
308  /* Remove entity object */
309  c_entity.Destroy();
310  delete &c_entity;
311  return;
312  }
313  }
314  THROW_ARGOSEXCEPTION("CSpace::RemoveEntity() : Entity \"" <<
315  strEntityQualifiedName <<
316  "\" has not been found in the indexes.");
317  }
318 
324  inline UInt32 GetSimulationClock() const {
325  return m_unSimulationClock;
326  }
327 
333  inline void SetSimulationClock(UInt32 un_simulation_clock) {
334  m_unSimulationClock = un_simulation_clock;
335  }
336 
342  inline void IncreaseSimulationClock(UInt32 un_increase = 1) {
343  m_unSimulationClock += un_increase;
344  }
345 
350  inline const CVector3& GetArenaSize() const {
351  return m_cArenaSize;
352  }
353 
358  inline void SetArenaSize(const CVector3& c_size) {
359  m_cArenaSize = c_size;
362  }
363 
368  inline const CVector3& GetArenaCenter() const {
369  return m_cArenaCenter;
370  }
371 
376  inline void SetArenaCenter(const CVector3& c_center) {
377  m_cArenaCenter = c_center;
380  }
381 
382  /*
383  * Returns the arena limits.
384  * The arena limits are defined by <tt>arena center - arena size</tt> and
385  * <tt>arena center - arena size</tt>.
386  * @return the arena limits.
387  */
388  inline const CRange<CVector3>& GetArenaLimits() const {
389  return m_cArenaLimits;
390  }
391 
392  virtual void AddControllableEntity(CControllableEntity& c_entity);
393  virtual void RemoveControllableEntity(CControllableEntity& c_entity);
394  virtual void AddEntityToPhysicsEngine(CEmbodiedEntity& c_entity);
395 
396  protected:
397 
398  virtual void UpdateControllableEntitiesAct() = 0;
399  virtual void UpdatePhysics() = 0;
400  virtual void UpdateMedia() = 0;
401  virtual void UpdateControllableEntitiesSenseStep() = 0;
402 
403  void Distribute(TConfigurationNode& t_tree);
404 
405  void AddBoxStrip(TConfigurationNode& t_tree);
406 
407  protected:
408 
412 
413  protected:
414 
415  /* The active simulator instance */
417 
420 
423 
426 
429 
432 
435 
438 
442  TMapPerTypePerId m_mapEntitiesPerTypePerId;
443 
446 
449 
452 
455  };
456 
457  /****************************************/
458  /****************************************/
459 
460  template <typename ACTION>
461  class CSpaceOperation : public CEntityOperation<ACTION, CSpace, void> {
462  public:
463  virtual ~CSpaceOperation() {}
464  };
465 
466  class CSpaceOperationAddEntity : public CSpaceOperation<CSpaceOperationAddEntity> {
467  public:
469  };
470  class CSpaceOperationRemoveEntity : public CSpaceOperation<CSpaceOperationRemoveEntity> {
471  public:
473  };
474 
475 }
476 
477  /****************************************/
478  /****************************************/
479 
480 #define SPACE_OPERATION_ADD_ENTITY(ENTITY) \
481  class CSpaceOperationAdd ## ENTITY : public CSpaceOperationAddEntity { \
482  public: \
483  void ApplyTo(CSpace& c_space, ENTITY& c_entity) { \
484  c_space.AddEntity(c_entity); \
485  } \
486  };
487 
488 #define SPACE_OPERATION_REMOVE_ENTITY(ENTITY) \
489  class CSpaceOperationRemove ## ENTITY : public CSpaceOperationRemoveEntity { \
490  public: \
491  void ApplyTo(CSpace& c_space, ENTITY& c_entity) { \
492  c_space.RemoveEntity(c_entity); \
493  } \
494  };
495 
496 #define REGISTER_SPACE_OPERATION(ACTION, OPERATION, ENTITY) \
497  REGISTER_ENTITY_OPERATION(ACTION, CSpace, OPERATION, void, ENTITY);
498 
499 #define REGISTER_STANDARD_SPACE_OPERATION_ADD_ENTITY(ENTITY) \
500  SPACE_OPERATION_ADD_ENTITY(ENTITY) \
501  REGISTER_SPACE_OPERATION(CSpaceOperationAddEntity, \
502  CSpaceOperationAdd ## ENTITY, \
503  ENTITY);
504 
505 #define REGISTER_STANDARD_SPACE_OPERATION_REMOVE_ENTITY(ENTITY) \
506  SPACE_OPERATION_REMOVE_ENTITY(ENTITY) \
507  REGISTER_SPACE_OPERATION(CSpaceOperationRemoveEntity, \
508  CSpaceOperationRemove ## ENTITY, \
509  ENTITY);
510 
511 #define REGISTER_STANDARD_SPACE_OPERATIONS_ON_ENTITY(ENTITY) \
512  REGISTER_STANDARD_SPACE_OPERATION_ADD_ENTITY(ENTITY) \
513  REGISTER_STANDARD_SPACE_OPERATION_REMOVE_ENTITY(ENTITY)
514 
515 #endif
CVector3 m_cArenaCenter
Arena center.
Definition: space.h:422
CEntity::TMap m_mapEntitiesPerId
A map of entities.
Definition: space.h:437
const CVector3 & GetArenaCenter() const
Returns the arena center.
Definition: space.h:368
void IncreaseSimulationClock(UInt32 un_increase=1)
Increases the simulation clock by the wanted value.
Definition: space.h:342
std::vector< CEntity * > TVector
A vector of entities.
Definition: entity.h:97
An entity that contains a pointer to the user-defined controller.
void AddEntity(ENTITY &c_entity)
Adds an entity of the given type.
Definition: space.h:253
CEntity & GetEntity(const std::string &str_id)
Returns the entity with the given id.
Definition: space.h:146
A 3D vector class.
Definition: vector3.h:29
virtual void RemoveControllableEntity(CControllableEntity &c_entity)
Definition: space.cpp:151
CEntity::TVector & GetRootEntityVector()
Returns a vector of all the root entities in the space.
Definition: space.h:136
CMedium::TVector * m_ptMedia
A pointer to the list of media.
Definition: space.h:454
std::vector< CMedium * > TVector
Definition: medium.h:26
void Distribute(TConfigurationNode &t_tree)
Definition: space.cpp:396
void RemoveEntity(ENTITY &c_entity)
Removes an entity of the given type.
Definition: space.h:286
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
friend class CSpaceOperationAddControllableEntity
Definition: space.h:409
virtual void AddControllableEntity(CControllableEntity &c_entity)
Definition: space.cpp:144
CEntity::TVector m_vecEntities
A vector of entities.
Definition: space.h:431
std::map< std::string, TMapPerType, std::less< std::string > > TMapPerTypePerId
A map of entities indexed by type description and by id.
Definition: space.h:76
std::vector< CControllableEntity * > TVector
A vector of controllable entities.
The basic entity type.
Definition: entity.h:89
TMapPerTypePerId m_mapEntitiesPerTypePerId
A map of maps of all the simulated entities.
Definition: space.h:442
UInt32 GetNumberEntities() const
Returns the number of entities contained in the space.
Definition: space.h:112
void AddBoxStrip(TConfigurationNode &t_tree)
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
TMapPerTypePerId & GetEntityMapPerTypePerId()
Returns a nested map of entities, ordered by type and by id.
Definition: space.h:191
CEntity::TVector m_vecRootEntities
A vector of all the entities without a parent.
Definition: space.h:434
virtual void Destroy()
Destroys the space and all its entities.
Definition: space.cpp:85
This entity is a link to a body in the physics engine.
TMapPerType & GetEntitiesByType(const std::string &str_type)
Returns a map containing all the objects of a given type.
Definition: space.cpp:108
CSimulator & m_cSimulator
Definition: space.h:416
virtual ~CSpaceOperationRemoveEntity()
Definition: space.h:472
void GetEntitiesMatching(CEntity::TVector &t_buffer, const std::string &str_pattern)
Returns the entities matching a given pattern.
Definition: space.cpp:95
CEntity::TMap & GetEntityMapPerId()
Returns a map of all entities ordered by id.
Definition: space.h:170
std::vector< CPhysicsEngine * > TVector
CRange< CVector3 > m_cArenaLimits
Arena limits.
Definition: space.h:428
const CVector3 & GetArenaSize() const
Returns the arena size.
Definition: space.h:350
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
CFloorEntity * m_pcFloorEntity
The floor entity.
Definition: space.h:448
unordered_map< std::string, CEntity * > TMap
A map of entities.
Definition: entity.h:100
std::map< std::string, CAny, std::less< std::string > > TMapPerType
A map of entities indexed by type description.
Definition: space.h:53
CPhysicsEngine::TVector * m_ptPhysicsEngines
A pointer to the list of physics engines.
Definition: space.h:451
virtual ~CSpaceOperationAddEntity()
Definition: space.h:468
virtual void Init(TConfigurationNode &t_tree)
Initializes the space using the section of the XML configuration file.
Definition: space.cpp:37
void SetArenaCenter(const CVector3 &c_center)
Sets the arena center.
Definition: space.h:376
virtual ~CSpaceOperation()
Definition: space.h:463
const CRange< CVector3 > & GetArenaLimits() const
Definition: space.h:388
virtual void UpdateMedia()=0
void SetFloorEntity(CFloorEntity &c_floor_entity)
Sets the floor entity.
Definition: space.h:227
CSpace()
Class constructor.
Definition: space.cpp:27
virtual void UpdateControllableEntitiesAct()=0
CVector3 m_cArenaSize
Arena size.
Definition: space.h:425
The basic operation to be stored in the vtable.
Definition: entity.h:331
virtual ~CSpace()
Class destructor.
Definition: space.h:91
virtual void Update()
Updates the space.
Definition: space.cpp:121
virtual void AddEntityToPhysicsEngine(CEmbodiedEntity &c_entity)
Definition: space.cpp:163
CControllableEntity::TVector m_vecControllableEntities
A vector of controllable entities.
Definition: space.h:445
virtual void Reset()
Reset the space and all its entities.
Definition: space.cpp:73
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
friend class CSpaceOperationAddEmbodiedEntity
Definition: space.h:411
virtual void UpdatePhysics()=0
void SetSimulationClock(UInt32 un_simulation_clock)
Sets a new value for the simulation clock.
Definition: space.h:333
UInt32 GetSimulationClock() const
Returns the current value of the simulation clock.
Definition: space.h:324
The core class of ARGOS.
Definition: simulator.h:62
CFloorEntity & GetFloorEntity()
Returns the floor entity.
Definition: space.h:218
This class is the base of all XML-configurable ARGoS interface.
CEntity::TVector & GetEntityVector()
Returns a vector of all the entities in the space.
Definition: space.h:122
UInt32 m_unSimulationClock
The current simulation clock.
Definition: space.h:419
virtual void UpdateControllableEntitiesSenseStep()=0
void SetArenaSize(const CVector3 &c_size)
Sets the arena size.
Definition: space.h:358
friend class CSpaceOperationRemoveControllableEntity
Definition: space.h:410