tag_equipped_entity.cpp
Go to the documentation of this file.
1 
7 #include "tag_equipped_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  SAnchor& s_anchor,
19  const CVector3& c_position_offset,
20  const CQuaternion& c_orientation_offset) :
21  Tag(c_tag),
22  Anchor(s_anchor),
23  PositionOffset(c_position_offset),
24  OrientationOffset(c_orientation_offset) {}
25 
26  /****************************************/
27  /****************************************/
28 
30  CComposableEntity(pc_parent) {
31  Disable();
32  }
33 
34  /****************************************/
35  /****************************************/
36 
38  const std::string& str_id) :
39  CComposableEntity(pc_parent, str_id) {
40  Disable();
41  }
42 
43  /****************************************/
44  /****************************************/
45 
47  try {
48  /* Init parent */
50  /* Go through the tag entries */
51  TConfigurationNodeIterator itTag("tag");
52  for(itTag = itTag.begin(&t_tree);
53  itTag != itTag.end();
54  ++itTag) {
55  /* Initialise the Tag using the XML */
56  auto* pcTag = new CTagEntity(this);
57  pcTag->Init(*itTag);
58  CVector3 cPositionOffset;
59  GetNodeAttribute(*itTag, "position", cPositionOffset);
60  CQuaternion cOrientationOffset;
61  GetNodeAttribute(*itTag, "orientation", cOrientationOffset);
62  /* Parse and look up the anchor */
63  std::string strAnchorId;
64  GetNodeAttribute(*itTag, "anchor", strAnchorId);
65  /*
66  * NOTE: here we get a reference to the embodied entity
67  * This line works under the assumption that:
68  * 1. the TagEquippedEntity has a parent;
69  * 2. the parent has a child whose id is "body"
70  * 3. the "body" is an embodied entity
71  * If any of the above is false, this line will bomb out.
72  */
73  auto& cBody = GetParent().GetComponent<CEmbodiedEntity>("body");
74  /* Add the tag to this container */
75  m_vecInstances.emplace_back(*pcTag,
76  cBody.GetAnchor(strAnchorId),
77  cPositionOffset,
78  cOrientationOffset);
79  AddComponent(*pcTag);
80  }
82  }
83  catch(CARGoSException& ex) {
84  THROW_ARGOSEXCEPTION_NESTED("Failed to initialize tag equipped entity \"" <<
85  GetContext() + GetId() << "\".", ex);
86  }
87  }
88 
89  /****************************************/
90  /****************************************/
91 
93  /* Perform generic enable behavior */
95  /* Enable anchors */
96  for(SInstance& s_instance : m_vecInstances) {
97  s_instance.Anchor.Enable();
98  }
99  }
100 
101  /****************************************/
102  /****************************************/
103 
105  /* Perform generic disable behavior */
107  /* Disable anchors */
108  for(SInstance& s_instance : m_vecInstances) {
109  s_instance.Anchor.Disable();
110  }
111  }
112 
113  /****************************************/
114  /****************************************/
115 
116  void CTagEquippedEntity::AddTag(const std::string& str_id,
117  const CVector3& c_position,
118  const CQuaternion& c_orientation,
119  SAnchor& s_anchor,
120  const CRadians& c_observable_angle,
121  Real f_side_length,
122  const std::string& str_payload) {
123  /* create the new tag entity */
124  auto* pcTag =
125  new CTagEntity(this,
126  str_id,
127  c_position,
128  c_orientation,
129  c_observable_angle,
130  f_side_length,
131  str_payload);
132  /* add it to the instances vector */
133  m_vecInstances.emplace_back(*pcTag,
134  s_anchor,
135  c_position,
136  c_orientation);
137  /* inform the base class about the new entity */
138  AddComponent(*pcTag);
140  }
141 
142  /****************************************/
143  /****************************************/
144 
146  ARGOS_ASSERT(un_index < m_vecInstances.size(),
147  "CTagEquippedEntity::GetTag(), id=\"" <<
148  GetContext() << GetId() <<
149  "\": index out of bounds: un_index = " <<
150  un_index <<
151  ", m_vecInstances.size() = " <<
152  m_vecInstances.size());
153  return m_vecInstances[un_index].Tag;
154  }
155 
156  /****************************************/
157  /****************************************/
158 
160  const std::string& str_payload) {
161  ARGOS_ASSERT(un_index < m_vecInstances.size(),
162  "CTagEquippedEntity::SetTagPayload(), id=\"" <<
163  GetContext() << GetId() <<
164  "\": index out of bounds: un_index = " <<
165  un_index <<
166  ", m_vecInstances.size() = " <<
167  m_vecInstances.size());
168  m_vecInstances[un_index].Tag.SetPayload(str_payload);
169  }
170 
171  /****************************************/
172  /****************************************/
173 
174  void CTagEquippedEntity::SetTagPayloads(const std::string& str_payload) {
175  for(SInstance& s_instance : m_vecInstances) {
176  s_instance.Tag.SetPayload(str_payload);
177  }
178  }
179 
180  /****************************************/
181  /****************************************/
182 
183  void CTagEquippedEntity::SetTagPayloads(const std::vector<std::string>& vec_payloads) {
184  if(vec_payloads.size() == m_vecInstances.size()) {
185  for(UInt32 i = 0; i < vec_payloads.size(); ++i) {
186  m_vecInstances[i].Tag.SetPayload(vec_payloads[i]);
187  }
188  }
189  else {
191  "CTagEquippedEntity::SetTagPayloads(), id=\"" <<
192  GetContext() << GetId() <<
193  "\": number of tags (" <<
194  m_vecInstances.size() <<
195  ") does not equal the passed payload vector size (" <<
196  vec_payloads.size() <<
197  ")");
198  }
199  }
200 
201  /****************************************/
202  /****************************************/
203 
205  /* Tag position wrt global reference frame */
206  CVector3 cTagPosition;
207  CQuaternion cTagOrientation;
208  for(SInstance& s_instance : m_vecInstances) {
209  if(s_instance.Tag.IsEnabled()) {
210  cTagPosition = s_instance.PositionOffset;
211  cTagPosition.Rotate(s_instance.Anchor.Orientation);
212  cTagPosition += s_instance.Anchor.Position;
213  cTagOrientation = s_instance.Anchor.Orientation *
214  s_instance.OrientationOffset;
215  s_instance.Tag.MoveTo(cTagPosition, cTagOrientation);
216  }
217  }
218  }
219 
220  /****************************************/
221  /****************************************/
222 
224  for(SInstance& s_instance : m_vecInstances) {
225  s_instance.Tag.SetMedium(c_medium);
226  }
227  }
228 
229  /****************************************/
230  /****************************************/
231 
233 
234  /****************************************/
235  /****************************************/
236 
237 }
#define ARGOS_ASSERT(condition, message)
When code is compiled in debug, this macro throws an ARGoS exception with the passed message if the s...
#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.
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
ticpp::Iterator< ticpp::Element > TConfigurationNodeIterator
The iterator for the ARGoS configuration XML node.
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
REGISTER_STANDARD_SPACE_OPERATIONS_ON_COMPOSABLE(CComposableEntity)
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.
CEntity & GetComponent(const std::string &str_component)
Returns the component with the passed string label.
void AddComponent(CEntity &c_component)
Adds a component to this composable entity.
This entity is a link to a body in the physics engine.
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
void Enable()
Enables the entity.
Definition: entity.h:265
CComposableEntity & GetParent()
Returns this entity's parent.
Definition: entity.cpp:91
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
Definition: entity.cpp:40
An anchor related to the body of an entity.
Definition: physics_model.h:38
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
A 3D vector class.
Definition: vector3.h:31
CVector3 & Rotate(const CQuaternion &c_quaternion)
Rotates this vector by the given quaternion.
Definition: vector3.cpp:23
A container of CTagEntity.
virtual void UpdateComponents()
Calls the Update() method on all the components.
void SetMedium(CTagMedium &c_medium)
Sets the medium associated to this entity.
CTagEntity & GetTag(UInt32 un_index)
Returns a tag by numeric index.
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
void SetTagPayload(UInt32 un_index, const std::string &str_payload)
Sets the payload of a tag.
SInstance::TVector m_vecInstances
List of the tags managed by this entity.
void AddTag(const std::string &str_id, const CVector3 &c_position, const CQuaternion &c_orientation, SAnchor &s_anchor, const CRadians &c_observable_angle, Real f_side_length, const std::string &str_payload)
Programmatically creates a new tag.
CTagEquippedEntity(CComposableEntity *pc_parent)
Class constructor.
void SetTagPayloads(const std::string &str_payload)
Sets the payload of all the tags to the given payload.
SInstance(CTagEntity &c_tag, SAnchor &s_anchor, const CVector3 &c_position_offset, const CQuaternion &c_orientation_offset)