ARGoS  3
A parallel, multi-engine simulator for swarm robotics
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/space/positional_indices/positional_index.h>
22 #include <argos3/core/simulator/entity/embodied_entity.h>
23 #include <argos3/core/simulator/entity/controllable_entity.h>
24 
25 namespace argos {
26 
27  /****************************************/
28  /****************************************/
29 
31 
32  public:
33 
54  typedef std::map <std::string, CAny, std::less <std::string> > TMapPerType;
55 
77  typedef std::map <std::string, TMapPerType, std::less <std::string> > TMapPerTypePerId;
78 
79  /****************************************/
80  /****************************************/
81 
82  public:
83 
87  CSpace();
88 
92  virtual ~CSpace() {}
93 
98  virtual void Init(TConfigurationNode& t_tree);
99 
103  virtual void Reset();
104 
108  virtual void Destroy();
109 
113  inline UInt32 GetNumberEntities() const {
114  return m_vecEntities.size();
115  }
116 
124  return m_vecEntities;
125  }
126 
138  return m_vecRootEntities;
139  }
140 
147  inline CEntity& GetEntity(const std::string& str_id) {
148  CEntity::TMap::const_iterator it = m_mapEntitiesPerId.find(str_id);
149  if ( it != m_mapEntitiesPerId.end()) {
150  return *(it->second);
151  }
152  THROW_ARGOSEXCEPTION("Unknown entity id \"" << str_id <<
153  "\" when requesting entity from space.");
154  }
155 
164  void GetEntitiesMatching(CEntity::TVector& t_buffer,
165  const std::string& str_pattern);
166 
172  return m_mapEntitiesPerId;
173  }
174 
194  }
195 
212  TMapPerType& GetEntitiesByType(const std::string& str_type);
213 
220  if(m_pcFloorEntity != NULL) return *m_pcFloorEntity;
221  else THROW_ARGOSEXCEPTION("No floor entity has been added to the arena.");
222  }
223 
228  inline void SetFloorEntity(CFloorEntity& c_floor_entity) {
229  m_pcFloorEntity = &c_floor_entity;
230  }
231 
246  virtual void Update();
247 
253  template <typename ENTITY>
254  void AddEntity(ENTITY& c_entity) {
255  std::string strEntityQualifiedName = c_entity.GetContext() + c_entity.GetId();
256  /* Check that the id of the entity is not already present */
257  if(m_mapEntitiesPerId.find(strEntityQualifiedName) != m_mapEntitiesPerId.end()) {
258  THROW_ARGOSEXCEPTION("Error inserting a " <<
259  c_entity.GetTypeDescription() <<
260  " entity with id \"" <<
261  strEntityQualifiedName <<
262  "\". An entity with that id already exists.");
263  }
264  /* Add the entity to the indexes */
265  if(!c_entity.HasParent()) {
266  m_vecRootEntities.push_back(&c_entity);
267  }
268  m_vecEntities.push_back(&c_entity);
269  m_mapEntitiesPerId[strEntityQualifiedName] = &c_entity;
270  m_mapEntitiesPerTypePerId[c_entity.GetTypeDescription()][strEntityQualifiedName] = &c_entity;
271  }
272 
278  template <typename ENTITY>
279  void RemoveEntity(ENTITY& c_entity) {
280  std::string strEntityQualifiedName = c_entity.GetContext() + c_entity.GetId();
281  /* Search for entity in the index per type */
282  TMapPerTypePerId::iterator itMapPerType = m_mapEntitiesPerTypePerId.find(c_entity.GetTypeDescription());
283  if(itMapPerType != m_mapEntitiesPerTypePerId.end()) {
284  /* Search for entity in the index per type per id */
285  TMapPerType::iterator itMapPerTypePerId = itMapPerType->second.find(strEntityQualifiedName);
286  if(itMapPerTypePerId != itMapPerType->second.end()) {
287  /* Remove the entity from the indexes */
288  CEntity::TVector::iterator itVec = find(m_vecEntities.begin(),
289  m_vecEntities.end(),
290  &c_entity);
291  m_vecEntities.erase(itVec);
292  CEntity::TMap::iterator itMap = m_mapEntitiesPerId.find(strEntityQualifiedName);
293  itMapPerType->second.erase(itMapPerTypePerId);
294  m_mapEntitiesPerId.erase(itMap);
295  if(!c_entity.HasParent()) {
296  CEntity::TVector::iterator itRootVec = find(m_vecRootEntities.begin(),
297  m_vecRootEntities.end(),
298  &c_entity);
299  m_vecRootEntities.erase(itRootVec);
300  }
301  /* Remove entity object */
302  c_entity.Destroy();
303  delete &c_entity;
304  return;
305  }
306  }
307  THROW_ARGOSEXCEPTION("CSpace::RemoveEntity() : Entity \"" <<
308  strEntityQualifiedName <<
309  "\" has not been found in the indexes.");
310  }
311 
317  inline UInt32 GetSimulationClock() const {
318  return m_unSimulationClock;
319  }
320 
326  inline void SetSimulationClock(UInt32 un_simulation_clock) {
327  m_unSimulationClock = un_simulation_clock;
328  }
329 
335  inline void IncreaseSimulationClock(UInt32 un_increase = 1) {
336  m_unSimulationClock += un_increase;
337  }
338 
343  inline const CVector3& GetArenaSize() const {
344  return m_cArenaSize;
345  }
346 
351  inline void SetArenaSize(const CVector3& c_size) {
352  m_cArenaSize = c_size;
355  }
356 
361  inline const CVector3& GetArenaCenter() const {
362  return m_cArenaCenter;
363  }
364 
369  inline void SetArenaCenter(const CVector3& c_center) {
370  m_cArenaCenter = c_center;
373  }
374 
375  /*
376  * Returns the arena limits.
377  * The arena limits are defined by <tt>arena center - arena size</tt> and
378  * <tt>arena center - arena size</tt>.
379  * @return the arena limits.
380  */
381  inline const CRange<CVector3>& GetArenaLimits() const {
382  return m_cArenaLimits;
383  }
384 
385  virtual void AddControllableEntity(CControllableEntity& c_entity);
386  virtual void RemoveControllableEntity(CControllableEntity& c_entity);
387  virtual void AddEntityToPhysicsEngine(CEmbodiedEntity& c_entity);
388 
389  protected:
390 
391  virtual void UpdateControllableEntitiesAct() = 0;
392  virtual void UpdatePhysics() = 0;
393  virtual void UpdateMedia() = 0;
394  virtual void UpdateControllableEntitiesSenseStep() = 0;
395 
396  void Distribute(TConfigurationNode& t_tree);
397 
398  void AddBoxStrip(TConfigurationNode& t_tree);
399 
400  protected:
401 
405 
406  protected:
407 
408  /* The active simulator instance */
410 
413 
416 
419 
422 
425 
428 
431 
436 
439 
442 
445 
448  };
449 
450  /****************************************/
451  /****************************************/
452 
453  template <typename ACTION>
454  class CSpaceOperation : public CEntityOperation<ACTION, CSpace, void> {
455  public:
456  virtual ~CSpaceOperation() {}
457  };
458 
459  class CSpaceOperationAddEntity : public CSpaceOperation<CSpaceOperationAddEntity> {
460  public:
462  };
463  class CSpaceOperationRemoveEntity : public CSpaceOperation<CSpaceOperationRemoveEntity> {
464  public:
466  };
467 
468 }
469 
470  /****************************************/
471  /****************************************/
472 
473 #define SPACE_OPERATION_ADD_ENTITY(ENTITY) \
474  class CSpaceOperationAdd ## ENTITY : public CSpaceOperationAddEntity { \
475  public: \
476  void ApplyTo(CSpace& c_space, ENTITY& c_entity) { \
477  c_space.AddEntity(c_entity); \
478  } \
479  };
480 
481 #define SPACE_OPERATION_REMOVE_ENTITY(ENTITY) \
482  class CSpaceOperationRemove ## ENTITY : public CSpaceOperationRemoveEntity { \
483  public: \
484  void ApplyTo(CSpace& c_space, ENTITY& c_entity) { \
485  c_space.RemoveEntity(c_entity); \
486  } \
487  };
488 
489 #define REGISTER_SPACE_OPERATION(ACTION, OPERATION, ENTITY) \
490  REGISTER_ENTITY_OPERATION(ACTION, CSpace, OPERATION, void, ENTITY);
491 
492 #define REGISTER_STANDARD_SPACE_OPERATION_ADD_ENTITY(ENTITY) \
493  SPACE_OPERATION_ADD_ENTITY(ENTITY) \
494  REGISTER_SPACE_OPERATION(CSpaceOperationAddEntity, \
495  CSpaceOperationAdd ## ENTITY, \
496  ENTITY);
497 
498 #define REGISTER_STANDARD_SPACE_OPERATION_REMOVE_ENTITY(ENTITY) \
499  SPACE_OPERATION_REMOVE_ENTITY(ENTITY) \
500  REGISTER_SPACE_OPERATION(CSpaceOperationRemoveEntity, \
501  CSpaceOperationRemove ## ENTITY, \
502  ENTITY);
503 
504 #define REGISTER_STANDARD_SPACE_OPERATIONS_ON_ENTITY(ENTITY) \
505  REGISTER_STANDARD_SPACE_OPERATION_ADD_ENTITY(ENTITY) \
506  REGISTER_STANDARD_SPACE_OPERATION_REMOVE_ENTITY(ENTITY)
507 
508 #endif
argos::CSpace::CSpaceOperationRemoveControllableEntity
friend class CSpaceOperationRemoveControllableEntity
Definition: space.h:403
argos::CSpace::m_vecControllableEntities
CControllableEntity::TVector m_vecControllableEntities
A vector of controllable entities.
Definition: space.h:438
argos::CSpace::GetEntitiesByType
TMapPerType & GetEntitiesByType(const std::string &str_type)
Returns a map containing all the objects of a given type.
Definition: space.cpp:108
argos::CBaseConfigurableResource
This class is the base of all XML-configurable ARGoS interface.
Definition: base_configurable_resource.h:23
argos::CSpace::GetNumberEntities
UInt32 GetNumberEntities() const
Returns the number of entities contained in the space.
Definition: space.h:113
argos::CSpace::RemoveControllableEntity
virtual void RemoveControllableEntity(CControllableEntity &c_entity)
Definition: space.cpp:151
argos::CSpace::m_cArenaLimits
CRange< CVector3 > m_cArenaLimits
Arena limits.
Definition: space.h:421
argos::CSpace::Distribute
void Distribute(TConfigurationNode &t_tree)
Definition: space.cpp:396
argos::CEntityOperation
The basic operation to be stored in the vtable.
Definition: entity.h:278
argos::CSpace::UpdateMedia
virtual void UpdateMedia()=0
argos::CSpace::GetEntityVector
CEntity::TVector & GetEntityVector()
Returns a vector of all the entities in the space.
Definition: space.h:123
argos
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
argos::CSpace::Destroy
virtual void Destroy()
Destroys the space and all its entities.
Definition: space.cpp:85
argos::CVector3
A 3D vector class.
Definition: vector3.h:29
argos::CSpace
Definition: space.h:30
argos::CSpace::UpdateControllableEntitiesAct
virtual void UpdateControllableEntitiesAct()=0
argos::CSpace::UpdatePhysics
virtual void UpdatePhysics()=0
argos::CSpace::CSpaceOperationAddEmbodiedEntity
friend class CSpaceOperationAddEmbodiedEntity
Definition: space.h:404
argos::CSpace::GetEntity
CEntity & GetEntity(const std::string &str_id)
Returns the entity with the given id.
Definition: space.h:147
argos::CSpace::m_cArenaSize
CVector3 m_cArenaSize
Arena size.
Definition: space.h:418
argos::CSimulator
The core class of ARGOS.
Definition: simulator.h:62
argos::CEmbodiedEntity
This entity is a link to a body in the physics engine.
Definition: embodied_entity.h:48
argos::CSpace::m_cSimulator
CSimulator & m_cSimulator
Definition: space.h:409
argos::TConfigurationNode
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
Definition: argos_configuration.h:27
argos::CSpace::m_ptPhysicsEngines
CPhysicsEngine::TVector * m_ptPhysicsEngines
A pointer to the list of physics engines.
Definition: space.h:444
argos::CFloorEntity
Definition: floor_entity.h:21
argos::CSpaceOperationRemoveEntity
Definition: space.h:463
argos::CSpace::UpdateControllableEntitiesSenseStep
virtual void UpdateControllableEntitiesSenseStep()=0
argos::CSpace::CSpaceOperationAddControllableEntity
friend class CSpaceOperationAddControllableEntity
Definition: space.h:402
argos::CSpace::CSpace
CSpace()
Class constructor.
Definition: space.cpp:27
argos::CMedium::TVector
std::vector< CMedium * > TVector
Definition: medium.h:25
argos::CSpace::IncreaseSimulationClock
void IncreaseSimulationClock(UInt32 un_increase=1)
Increases the simulation clock by the wanted value.
Definition: space.h:335
argos::CSpace::GetArenaCenter
const CVector3 & GetArenaCenter() const
Returns the arena center.
Definition: space.h:361
argos::CSpace::AddEntity
void AddEntity(ENTITY &c_entity)
Adds an entity of the given type.
Definition: space.h:254
argos::CSpace::SetArenaCenter
void SetArenaCenter(const CVector3 &c_center)
Sets the arena center.
Definition: space.h:369
THROW_ARGOSEXCEPTION
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
Definition: argos_exception.h:111
argos::CSpace::AddControllableEntity
virtual void AddControllableEntity(CControllableEntity &c_entity)
Definition: space.cpp:144
argos::CSpace::AddBoxStrip
void AddBoxStrip(TConfigurationNode &t_tree)
argos::CSpace::SetFloorEntity
void SetFloorEntity(CFloorEntity &c_floor_entity)
Sets the floor entity.
Definition: space.h:228
argos::CSpace::GetArenaSize
const CVector3 & GetArenaSize() const
Returns the arena size.
Definition: space.h:343
argos::CSpaceOperation
Definition: space.h:454
argos::CEntity::TMap
unordered_map< std::string, CEntity * > TMap
A map of entities.
Definition: entity.h:100
argos::CSpace::GetSimulationClock
UInt32 GetSimulationClock() const
Returns the current value of the simulation clock.
Definition: space.h:317
argos::CSpace::GetFloorEntity
CFloorEntity & GetFloorEntity()
Returns the floor entity.
Definition: space.h:219
argos::CSpace::TMapPerType
std::map< std::string, CAny, std::less< std::string > > TMapPerType
A map of entities indexed by type description.
Definition: space.h:54
argos::CSpace::m_ptMedia
CMedium::TVector * m_ptMedia
A pointer to the list of media.
Definition: space.h:447
argos::CEntity
The basic entity type.
Definition: entity.h:89
argos::CSpace::~CSpace
virtual ~CSpace()
Class destructor.
Definition: space.h:92
UInt32
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
argos::CSpace::m_vecEntities
CEntity::TVector m_vecEntities
A vector of entities.
Definition: space.h:424
argos::CSpace::m_cArenaCenter
CVector3 m_cArenaCenter
Arena center.
Definition: space.h:415
argos::CSpace::m_pcFloorEntity
CFloorEntity * m_pcFloorEntity
The floor entity.
Definition: space.h:441
argos::CPhysicsEngine::TVector
std::vector< CPhysicsEngine * > TVector
Definition: physics_engine.h:130
argos::CSpaceOperationRemoveEntity::~CSpaceOperationRemoveEntity
virtual ~CSpaceOperationRemoveEntity()
Definition: space.h:465
argos::CSpace::m_mapEntitiesPerTypePerId
TMapPerTypePerId m_mapEntitiesPerTypePerId
A map of maps of all the simulated entities.
Definition: space.h:435
argos::CSpaceOperationAddEntity::~CSpaceOperationAddEntity
virtual ~CSpaceOperationAddEntity()
Definition: space.h:461
argos::CSpace::m_vecRootEntities
CEntity::TVector m_vecRootEntities
A vector of all the entities without a parent.
Definition: space.h:427
argos::CSpace::GetEntityMapPerTypePerId
TMapPerTypePerId & GetEntityMapPerTypePerId()
Returns a nested map of entities, ordered by type and by id.
Definition: space.h:192
argos::CSpace::Update
virtual void Update()
Updates the space.
Definition: space.cpp:121
argos::CRange
Definition: range.h:17
argos::CSpace::AddEntityToPhysicsEngine
virtual void AddEntityToPhysicsEngine(CEmbodiedEntity &c_entity)
Definition: space.cpp:163
argos::CSpace::Reset
virtual void Reset()
Reset the space and all its entities.
Definition: space.cpp:73
argos::CControllableEntity
An entity that contains a pointer to the user-defined controller.
Definition: controllable_entity.h:26
argos::CSpace::m_mapEntitiesPerId
CEntity::TMap m_mapEntitiesPerId
A map of entities.
Definition: space.h:430
argos::CSpace::GetEntityMapPerId
CEntity::TMap & GetEntityMapPerId()
Returns a map of all entities ordered by id.
Definition: space.h:171
argos::CSpaceOperation::~CSpaceOperation
virtual ~CSpaceOperation()
Definition: space.h:456
argos::CSpace::GetRootEntityVector
CEntity::TVector & GetRootEntityVector()
Returns a vector of all the root entities in the space.
Definition: space.h:137
argos::CSpace::GetArenaLimits
const CRange< CVector3 > & GetArenaLimits() const
Definition: space.h:381
argos::CEntity::TVector
std::vector< CEntity * > TVector
A vector of entities.
Definition: entity.h:97
argos::CSpace::RemoveEntity
void RemoveEntity(ENTITY &c_entity)
Removes an entity of the given type.
Definition: space.h:279
argos::CSpace::SetSimulationClock
void SetSimulationClock(UInt32 un_simulation_clock)
Sets a new value for the simulation clock.
Definition: space.h:326
argos::CSpace::SetArenaSize
void SetArenaSize(const CVector3 &c_size)
Sets the arena size.
Definition: space.h:351
argos::CSpace::m_unSimulationClock
UInt32 m_unSimulationClock
The current simulation clock.
Definition: space.h:412
argos::CSpaceOperationAddEntity
Definition: space.h:459
argos::CSpace::Init
virtual void Init(TConfigurationNode &t_tree)
Initializes the space using the <arena> section of the XML configuration file.
Definition: space.cpp:37
argos::CSpace::GetEntitiesMatching
void GetEntitiesMatching(CEntity::TVector &t_buffer, const std::string &str_pattern)
Returns the entities matching a given pattern.
Definition: space.cpp:95
argos::CSpace::TMapPerTypePerId
std::map< std::string, TMapPerType, std::less< std::string > > TMapPerTypePerId
A map of entities indexed by type description and by id.
Definition: space.h:77
argos::CControllableEntity::TVector
std::vector< CControllableEntity * > TVector
A vector of controllable entities.
Definition: controllable_entity.h:33