tag_entity.cpp
Go to the documentation of this file.
1 
7 #include "tag_entity.h"
8 #include <argos3/core/simulator/simulator.h>
9 #include <argos3/core/simulator/space/space.h>
10 #include <argos3/plugins/simulator/media/tag_medium.h>
11 
12 namespace argos {
13 
14  /****************************************/
15  /****************************************/
16 
18  CPositionalEntity(pc_parent),
19  m_cObservableAngle(CRadians::PI),
20  m_fSideLength(0.0f),
21  m_pcMedium(nullptr) {
22  Disable();
23  }
24 
25  /****************************************/
26  /****************************************/
27 
29  const std::string& str_id,
30  const CVector3& c_position,
31  const CQuaternion& c_orientation,
32  const CRadians& c_observable_angle,
33  Real f_side_length,
34  const std::string& str_payload) :
35  CPositionalEntity(pc_parent, str_id, c_position, c_orientation),
36  m_cObservableAngle(c_observable_angle),
37  m_fSideLength(f_side_length),
38  m_strPayload(str_payload),
39  m_strInitPayload(str_payload),
40  m_pcMedium(nullptr) {
41  Disable();
42  }
43 
44  /****************************************/
45  /****************************************/
46 
48  try {
49  /* Parse XML */
53  CDegrees cObservableAngle;
54  GetNodeAttribute(t_tree, "observable_angle", cObservableAngle);
55  m_cObservableAngle = ToRadians(cObservableAngle);
56  GetNodeAttribute(t_tree, "side_length", m_fSideLength);
57  }
58  catch(CARGoSException& ex) {
59  THROW_ARGOSEXCEPTION_NESTED("Error while initializing tag entity", ex);
60  }
61  }
62 
63  /****************************************/
64  /****************************************/
65 
68  }
69 
70  /****************************************/
71  /****************************************/
72 
74  Disable();
75  }
76 
77  /****************************************/
78  /****************************************/
79 
80  void CTagEntity::SetEnabled(bool b_enabled) {
81  /* Perform generic enable behavior */
82  CEntity::SetEnabled(b_enabled);
83  if(b_enabled) {
84  /* Enable entity in medium */
85  if(m_pcMedium && GetIndex() >= 0)
86  m_pcMedium->AddEntity(*this);
87  }
88  else {
89  /* Disable entity in medium */
90  if(m_pcMedium)
91  m_pcMedium->RemoveEntity(*this);
92  }
93  }
94 
95  /****************************************/
96  /****************************************/
97 
98  void CTagEntity::SetPayload(const std::string& str_payload) {
99  m_strPayload = str_payload;
100  }
101 
102  /****************************************/
103  /****************************************/
104 
106  if(m_pcMedium == nullptr) {
107  THROW_ARGOSEXCEPTION("Tag entity \"" << GetContext() + GetId() <<
108  "\" has no medium associated.");
109  }
110  return *m_pcMedium;
111  }
112 
113  /****************************************/
114  /****************************************/
115 
117  if(m_pcMedium != nullptr && m_pcMedium != &c_medium)
118  m_pcMedium->RemoveEntity(*this);
119  m_pcMedium = &c_medium;
120  }
121 
122  /****************************************/
123  /****************************************/
124 
126  CTagEntity& c_element) {
127  /* Calculate the position of the tag in the space hash */
128  c_space_hash.SpaceToHashTable(m_nI, m_nJ, m_nK, c_element.GetPosition());
129  /* Update the corresponding cell */
130  c_space_hash.UpdateCell(m_nI, m_nJ, m_nK, c_element);
131  }
132 
133  /****************************************/
134  /****************************************/
135 
137  m_cGrid(c_grid) {}
138 
139  /****************************************/
140  /****************************************/
141 
143  try {
144  /* Calculate the position of the tag in the space hash */
145  m_cGrid.PositionToCell(m_nI, m_nJ, m_nK, c_entity.GetPosition());
146  /* Update the corresponding cell */
147  m_cGrid.UpdateCell(m_nI, m_nJ, m_nK, c_entity);
148  }
149  catch(CARGoSException& ex) {
150  THROW_ARGOSEXCEPTION_NESTED("While updating the tag grid for tag \"" <<
151  c_entity.GetContext() + c_entity.GetId() << "\"", ex);
152  }
153  /* Continue with the other entities */
154  return true;
155  }
156 
157  /****************************************/
158  /****************************************/
159 
161  public:
162  void ApplyTo(CSpace& c_space, CTagEntity& c_entity) {
163  /* Add entity to space - this ensures that the tag entity
164  * gets an id before being added to the tag medium */
165  c_space.AddEntity(c_entity);
166  /* Enable the tag entity, if it's enabled - this ensures that
167  * the entity gets added to the tag if it's enabled */
168  c_entity.SetEnabled(c_entity.IsEnabled());
169  }
170  };
171 
173  public:
174  void ApplyTo(CSpace& c_space, CTagEntity& c_entity) {
175  /* Disable the entity - this ensures that the entity is
176  * removed from the tag medium */
177  c_entity.Disable();
178  /* Remove the tag entity from space */
179  c_space.RemoveEntity(c_entity);
180  }
181  };
182 
185  CTagEntity);
188  CTagEntity);
189 
190  /****************************************/
191  /****************************************/
192 
193 }
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception.
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
void GetNodeAttributeOrDefault(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer, const T &t_default)
Returns the value of a node's attribute, or the passed default value.
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
REGISTER_SPACE_OPERATION(CSpaceOperationAddEntity, CSpaceOperationAddCFloorEntity, CFloorEntity)
CRadians ToRadians(const CDegrees &c_degrees)
Converts CDegrees to CRadians.
Definition: angles.h:498
void GetNodeAttribute(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer)
Returns the value of a node's attribute.
Basic class for an entity that contains other entities.
void Disable()
Disables the entity.
Definition: entity.h:275
const std::string & GetId() const
Returns the id of this entity.
Definition: entity.h:157
std::string GetContext() const
Returns the context of this entity.
Definition: entity.cpp:79
bool IsEnabled() const
Returns true if the entity is enabled.
Definition: entity.h:255
virtual void SetEnabled(bool b_enabled)
Enables or disables an entity.
Definition: entity.cpp:139
ssize_t GetIndex() const
Returns the entity index.
Definition: entity.h:234
const CVector3 & GetPosition() const
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
The abstract definition of a space hash.
Definition: space_hash.h:34
virtual void UpdateCell(SInt32 n_x, SInt32 n_y, SInt32 n_z, ENTITY &c_entity)=0
Adds an entity to a cell of the space hash.
virtual SInt32 SpaceToHashTable(Real f_coord, UInt32 un_axis)
Converts a single space coordinate into a space hash cell coordinate.
Definition: space_hash.h:163
void AddEntity(ENTITY &c_entity)
Adds an entity of the given type.
Definition: space.h:274
void RemoveEntity(ENTITY &c_entity)
Removes an entity of the given type.
Definition: space.h:307
The exception that wraps all errors in ARGoS.
It defines the basic type CRadians, used to store an angle value in radians.
Definition: angles.h:42
It defines the basic type CDegrees, used to store an angle value in degrees.
Definition: angles.h:288
A 3D vector class.
Definition: vector3.h:31
void ApplyTo(CSpace &c_space, CTagEntity &c_entity)
Definition: tag_entity.cpp:162
void ApplyTo(CSpace &c_space, CTagEntity &c_entity)
Definition: tag_entity.cpp:174
virtual void SetEnabled(bool b_enabled)
Enables or disables an entity.
Definition: tag_entity.cpp:80
virtual void Reset()
Resets the state of the entity to whatever it was after Init() or the standalone constructor was call...
Definition: tag_entity.cpp:66
CTagMedium & GetMedium() const
Returns the medium associated to this tag.
Definition: tag_entity.cpp:105
void SetPayload(const std::string &str_payload)
Sets the current payload of the tag.
Definition: tag_entity.cpp:98
void SetMedium(CTagMedium &c_medium)
Sets the medium associated to this entity.
Definition: tag_entity.cpp:116
CRadians m_cObservableAngle
Definition: tag_entity.h:139
virtual void Destroy()
Destroys the entity, undoing whatever was done by Init() or by the standalone constructor.
Definition: tag_entity.cpp:73
CTagEntity(CComposableEntity *pc_parent)
Definition: tag_entity.cpp:17
std::string m_strPayload
Definition: tag_entity.h:141
std::string m_strInitPayload
Definition: tag_entity.h:142
CTagMedium * m_pcMedium
Definition: tag_entity.h:143
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
Definition: tag_entity.cpp:47
virtual void operator()(CAbstractSpaceHash< CTagEntity > &c_space_hash, CTagEntity &c_element)
Updates the necessary cells of a space hash.
Definition: tag_entity.cpp:125
CTagEntityGridUpdater(CGrid< CTagEntity > &c_grid)
Definition: tag_entity.cpp:136
virtual bool operator()(CTagEntity &c_entity)
Definition: tag_entity.cpp:142
void AddEntity(CTagEntity &c_entity)
Adds the specified entity to the list of managed entities.
Definition: tag_medium.cpp:94
void RemoveEntity(CTagEntity &c_entity)
Removes the specified entity from the list of managed entities.
Definition: tag_medium.cpp:102