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
12namespace argos {
13 class CSpace;
14 class CRay3;
15 class CFloorEntity;
16 class CSimulator;
17}
18
19#include <functional>
20#include <string>
21
22#include <argos3/core/utility/datatypes/any.h>
23#include <argos3/core/simulator/medium/medium.h>
24#include <argos3/core/simulator/entity/embodied_entity.h>
25#include <argos3/core/simulator/entity/controllable_entity.h>
26
27namespace argos {
28
29 /****************************************/
30 /****************************************/
31
33
34 public:
35
56 typedef std::map <std::string, CAny, std::less <std::string> > TMapPerType;
57
79 typedef std::map <std::string, TMapPerType, std::less <std::string> > TMapPerTypePerId;
80
91
92 /****************************************/
93 /****************************************/
94
95 public:
96
100 CSpace();
101
105 virtual ~CSpace() {}
106
111 virtual void Init(TConfigurationNode& t_tree);
112
116 virtual void Reset();
117
121 virtual void Destroy();
122
126 inline UInt32 GetNumberEntities() const {
127 return m_vecEntities.size();
128 }
129
137 return m_vecEntities;
138 }
139
153
160 inline CEntity& GetEntity(const std::string& str_id) {
161 CEntity::TMap::const_iterator it = m_mapEntitiesPerId.find(str_id);
162 if ( it != m_mapEntitiesPerId.end()) {
163 return *(it->second);
164 }
165 THROW_ARGOSEXCEPTION("Unknown entity id \"" << str_id <<
166 "\" when requesting entity from space.");
167 }
168
178 const std::string& str_pattern);
179
187
208
226 TMapPerType& GetEntitiesByType(const std::string& str_type) {
227 return GetEntitiesByTypeImpl(str_type);
228 }
229
230 const TMapPerType& GetEntitiesByType(const std::string& str_type) const {
231 return GetEntitiesByTypeImpl(str_type);
232 }
233
240 if(m_pcFloorEntity != NULL) return *m_pcFloorEntity;
241 else THROW_ARGOSEXCEPTION("No floor entity has been added to the arena.");
242 }
243
248 inline void SetFloorEntity(CFloorEntity& c_floor_entity) {
249 m_pcFloorEntity = &c_floor_entity;
250 }
251
266 virtual void Update();
267
273 template <typename ENTITY>
274 void AddEntity(ENTITY& c_entity) {
275 std::string strEntityQualifiedName = c_entity.GetContext() + c_entity.GetId();
276 /* Check that the id of the entity is not already present */
277 if(m_mapEntitiesPerId.find(strEntityQualifiedName) != m_mapEntitiesPerId.end()) {
278 THROW_ARGOSEXCEPTION("Error inserting a " <<
279 c_entity.GetTypeDescription() <<
280 " entity with id \"" <<
281 strEntityQualifiedName <<
282 "\". An entity with that id already exists.");
283 }
284 /* Add the entity to the indexes */
285 if(!c_entity.HasParent()) {
286 m_vecRootEntities.push_back(&c_entity);
287 }
288 /* Calculate index of in the global vector */
289 size_t unIdx =
290 !m_vecEntities.empty() ?
291 m_vecEntities.back()->GetIndex() + 1
292 :
293 0;
294 /* Add entity to global vector */
295 m_vecEntities.push_back(&c_entity);
296 c_entity.SetIndex(unIdx);
297 m_mapEntitiesPerId[strEntityQualifiedName] = &c_entity;
298 m_mapEntitiesPerTypePerId[c_entity.GetTypeDescription()][strEntityQualifiedName] = &c_entity;
299 }
300
306 template <typename ENTITY>
307 void RemoveEntity(ENTITY& c_entity) {
308 std::string strEntityQualifiedName = c_entity.GetContext() + c_entity.GetId();
309 /* Search for entity in the index per type */
310 TMapPerTypePerId::iterator itMapPerType = m_mapEntitiesPerTypePerId.find(c_entity.GetTypeDescription());
311 if(itMapPerType != m_mapEntitiesPerTypePerId.end()) {
312 /* Search for entity in the index per type per id */
313 TMapPerType::iterator itMapPerTypePerId = itMapPerType->second.find(strEntityQualifiedName);
314 if(itMapPerTypePerId != itMapPerType->second.end()) {
315 /* Remove the entity from the indexes */
316 CEntity::TVector::iterator itVec = find(m_vecEntities.begin(),
317 m_vecEntities.end(),
318 &c_entity);
319 m_vecEntities.erase(itVec);
320 CEntity::TMap::iterator itMap = m_mapEntitiesPerId.find(strEntityQualifiedName);
321 itMapPerType->second.erase(itMapPerTypePerId);
322 m_mapEntitiesPerId.erase(itMap);
323 if(!c_entity.HasParent()) {
324 CEntity::TVector::iterator itRootVec = find(m_vecRootEntities.begin(),
325 m_vecRootEntities.end(),
326 &c_entity);
327 m_vecRootEntities.erase(itRootVec);
328 }
329 /* Remove entity object */
330 c_entity.Destroy();
331 delete &c_entity;
332 return;
333 }
334 }
335 THROW_ARGOSEXCEPTION("CSpace::RemoveEntity() : Entity \"" <<
336 strEntityQualifiedName <<
337 "\" has not been found in the indexes.");
338 }
339
345 inline UInt32 GetSimulationClock() const {
346 return m_unSimulationClock;
347 }
348
354 inline void SetSimulationClock(UInt32 un_simulation_clock) {
355 m_unSimulationClock = un_simulation_clock;
356 }
357
363 inline void IncreaseSimulationClock(UInt32 un_increase = 1) {
364 m_unSimulationClock += un_increase;
365 }
366
371 inline const CVector3& GetArenaSize() const {
372 return m_cArenaSize;
373 }
374
379 inline void SetArenaSize(const CVector3& c_size) {
380 m_cArenaSize = c_size;
383 }
384
389 inline const CVector3& GetArenaCenter() const {
390 return m_cArenaCenter;
391 }
392
397 inline void SetArenaCenter(const CVector3& c_center) {
398 m_cArenaCenter = c_center;
401 }
402
409 inline const CRange<CVector3>& GetArenaLimits() const {
410 return m_cArenaLimits;
411 }
412
413 virtual void AddControllableEntity(CControllableEntity& c_entity);
414 virtual void RemoveControllableEntity(CControllableEntity& c_entity);
415 virtual void AddEntityToPhysicsEngine(CEmbodiedEntity& c_entity);
416
423 const TControllableEntityIterCBType& c_cb) = 0;
424
425 protected:
426
428 virtual void UpdatePhysics() = 0;
429 virtual void UpdateMedia() = 0;
431
444
445 void Distribute(TConfigurationNode& t_tree);
446
448
450 return nullptr != m_cbControllableEntityIter;
451 }
452
453 protected:
454
458
459 protected:
460
461 /* The active simulator instance */
463
466
469
472
475
478
481
484
489
492
495
498
501
504
505 private:
506 TMapPerType& GetEntitiesByTypeImpl(const std::string& str_type) const;
507
508 };
509
510 /****************************************/
511 /****************************************/
512
513 template <typename ACTION>
514 class CSpaceOperation : public CEntityOperation<ACTION, CSpace, void> {
515 public:
516 virtual ~CSpaceOperation() {}
517 };
518
519 class CSpaceOperationAddEntity : public CSpaceOperation<CSpaceOperationAddEntity> {
520 public:
522 };
523 class CSpaceOperationRemoveEntity : public CSpaceOperation<CSpaceOperationRemoveEntity> {
524 public:
526 };
527
528}
529
530 /****************************************/
531 /****************************************/
532
533#define SPACE_OPERATION_ADD_ENTITY(ENTITY) \
534 class CSpaceOperationAdd ## ENTITY : public CSpaceOperationAddEntity { \
535 public: \
536 void ApplyTo(CSpace& c_space, ENTITY& c_entity) { \
537 c_space.AddEntity(c_entity); \
538 } \
539 };
540
541#define SPACE_OPERATION_REMOVE_ENTITY(ENTITY) \
542 class CSpaceOperationRemove ## ENTITY : public CSpaceOperationRemoveEntity { \
543 public: \
544 void ApplyTo(CSpace& c_space, ENTITY& c_entity) { \
545 c_space.RemoveEntity(c_entity); \
546 } \
547 };
548
549#define REGISTER_SPACE_OPERATION(ACTION, OPERATION, ENTITY) \
550 REGISTER_ENTITY_OPERATION(ACTION, CSpace, OPERATION, void, ENTITY);
551
552#define REGISTER_STANDARD_SPACE_OPERATION_ADD_ENTITY(ENTITY) \
553 SPACE_OPERATION_ADD_ENTITY(ENTITY) \
554 REGISTER_SPACE_OPERATION(CSpaceOperationAddEntity, \
555 CSpaceOperationAdd ## ENTITY, \
556 ENTITY);
557
558#define REGISTER_STANDARD_SPACE_OPERATION_REMOVE_ENTITY(ENTITY) \
559 SPACE_OPERATION_REMOVE_ENTITY(ENTITY) \
560 REGISTER_SPACE_OPERATION(CSpaceOperationRemoveEntity, \
561 CSpaceOperationRemove ## ENTITY, \
562 ENTITY);
563
564#define REGISTER_STANDARD_SPACE_OPERATIONS_ON_ENTITY(ENTITY) \
565 REGISTER_STANDARD_SPACE_OPERATION_ADD_ENTITY(ENTITY) \
566 REGISTER_STANDARD_SPACE_OPERATION_REMOVE_ENTITY(ENTITY)
567
568#endif
unsigned int UInt32
32-bit unsigned integer.
Definition datatypes.h:97
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
The namespace containing all the ARGoS related code.
Definition ci_actuator.h:12
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
An entity that contains a pointer to the user-defined controller.
std::vector< CControllableEntity * > TVector
A vector of controllable entities.
This entity is a link to a body in the physics engine.
The basic entity type.
Definition entity.h:90
std::vector< CEntity * > TVector
A vector of entities.
Definition entity.h:97
unordered_map< std::string, CEntity * > TMap
A map of entities.
Definition entity.h:100
The basic operation to be stored in the vtable.
Definition entity.h:331
std::vector< CMedium * > TVector
Definition medium.h:26
std::vector< CPhysicsEngine * > TVector
The core class of ARGOS.
Definition simulator.h:62
void Distribute(TConfigurationNode &t_tree)
Definition space.cpp:433
CControllableEntity::TVector m_vecControllableEntities
A vector of controllable entities.
Definition space.h:491
virtual void Init(TConfigurationNode &t_tree)
Initializes the space using the <arena> section of the XML configuration file.
Definition space.cpp:37
virtual void Destroy()
Destroys the space and all its entities.
Definition space.cpp:85
const CVector3 & GetArenaCenter() const
Returns the arena center.
Definition space.h:389
const CRange< CVector3 > & GetArenaLimits() const
Returns the arena limits.
Definition space.h:409
virtual void Reset()
Reset the space and all its entities.
Definition space.cpp:73
void AddBoxStrip(TConfigurationNode &t_tree)
bool ControllableEntityIterationEnabled() const
Definition space.h:449
CRange< CVector3 > m_cArenaLimits
Arena limits.
Definition space.h:474
friend class CSpaceOperationRemoveControllableEntity
Definition space.h:456
UInt32 GetNumberEntities() const
Returns the number of entities contained in the space.
Definition space.h:126
virtual void UpdatePhysics()=0
const CVector3 & GetArenaSize() const
Returns the arena size.
Definition space.h:371
virtual void Update()
Updates the space.
Definition space.cpp:119
virtual ~CSpace()
Class destructor.
Definition space.h:105
std::map< std::string, CAny, std::less< std::string > > TMapPerType
A map of entities indexed by type description.
Definition space.h:56
CFloorEntity & GetFloorEntity()
Returns the floor entity.
Definition space.h:239
CSpace()
Class constructor.
Definition space.cpp:27
CPhysicsEngine::TVector * m_ptPhysicsEngines
A pointer to the list of physics engines.
Definition space.h:497
void SetArenaCenter(const CVector3 &c_center)
Sets the arena center.
Definition space.h:397
CEntity::TMap m_mapEntitiesPerId
A map of entities.
Definition space.h:483
void SetFloorEntity(CFloorEntity &c_floor_entity)
Sets the floor entity.
Definition space.h:248
UInt32 m_unSimulationClock
The current simulation clock.
Definition space.h:465
std::function< void(CControllableEntity *)> TControllableEntityIterCBType
The callback type for iteration over controllable entities within the PreStep() and/or PostStep() par...
Definition space.h:90
void GetEntitiesMatching(CEntity::TVector &t_buffer, const std::string &str_pattern)
Returns the entities matching a given pattern.
Definition space.cpp:95
TControllableEntityIterCBType m_cbControllableEntityIter
Callback for iterating over entities from within the loop functions.
Definition space.h:503
TMapPerTypePerId m_mapEntitiesPerTypePerId
A map of maps of all the simulated entities.
Definition space.h:488
CEntity::TVector & GetEntityVector()
Returns a vector of all the entities in the space.
Definition space.h:136
CMedium::TVector * m_ptMedia
A pointer to the list of media.
Definition space.h:500
void SetSimulationClock(UInt32 un_simulation_clock)
Sets a new value for the simulation clock.
Definition space.h:354
CEntity & GetEntity(const std::string &str_id)
Returns the entity with the given id.
Definition space.h:160
virtual void ControllableEntityIterationWaitAbort()
If the loop functions do not perform entity iteration in either of the PreStep() or PostStep() functi...
Definition space.h:443
virtual void IterateOverControllableEntities(const TControllableEntityIterCBType &c_cb)=0
Given a callback specified in the loop functions, iterate over all controllable entities currently pr...
virtual void UpdateControllableEntitiesAct()=0
void AddEntity(ENTITY &c_entity)
Adds an entity of the given type.
Definition space.h:274
void IncreaseSimulationClock(UInt32 un_increase=1)
Increases the simulation clock by the wanted value.
Definition space.h:363
UInt32 GetSimulationClock() const
Returns the current value of the simulation clock.
Definition space.h:345
CVector3 m_cArenaSize
Arena size.
Definition space.h:471
CVector3 m_cArenaCenter
Arena center.
Definition space.h:468
CEntity::TMap & GetEntityMapPerId()
Returns a map of all entities ordered by id.
Definition space.h:184
const TMapPerType & GetEntitiesByType(const std::string &str_type) const
Definition space.h:230
TMapPerTypePerId & GetEntityMapPerTypePerId()
Returns a nested map of entities, ordered by type and by id.
Definition space.h:205
friend class CSpaceOperationAddEmbodiedEntity
Definition space.h:457
CEntity::TVector m_vecRootEntities
A vector of all the entities without a parent.
Definition space.h:480
CFloorEntity * m_pcFloorEntity
The floor entity.
Definition space.h:494
virtual void UpdateMedia()=0
virtual void AddEntityToPhysicsEngine(CEmbodiedEntity &c_entity)
Definition space.cpp:186
void SetArenaSize(const CVector3 &c_size)
Sets the arena size.
Definition space.h:379
virtual void AddControllableEntity(CControllableEntity &c_entity)
Definition space.cpp:167
TMapPerType & GetEntitiesByType(const std::string &str_type)
Returns a map containing all the objects of a given type.
Definition space.h:226
CSimulator & m_cSimulator
Definition space.h:462
CEntity::TVector m_vecEntities
A vector of entities.
Definition space.h:477
virtual void UpdateControllableEntitiesSenseStep()=0
std::map< std::string, TMapPerType, std::less< std::string > > TMapPerTypePerId
A map of entities indexed by type description and by id.
Definition space.h:79
virtual void RemoveControllableEntity(CControllableEntity &c_entity)
Definition space.cpp:174
CEntity::TVector & GetRootEntityVector()
Returns a vector of all the root entities in the space.
Definition space.h:150
void RemoveEntity(ENTITY &c_entity)
Removes an entity of the given type.
Definition space.h:307
friend class CSpaceOperationAddControllableEntity
Definition space.h:455
virtual ~CSpaceOperation()
Definition space.h:516
virtual ~CSpaceOperationAddEntity()
Definition space.h:521
This class is the base of all XML-configurable ARGoS interface.
A 3D vector class.
Definition vector3.h:31
void Set(const Real f_x, const Real f_y, const Real f_z)
Sets the vector contents from Cartesian coordinates.
Definition vector3.h:155