magnet_equipped_entity.cpp
Go to the documentation of this file.
1 
8 
9 namespace argos {
10 
11  /****************************************/
12  /****************************************/
13 
15  SAnchor& s_anchor,
16  const CVector3& c_offset) :
17  Magnet(c_magnet),
18  Anchor(s_anchor),
19  Offset(c_offset) {}
20 
21  /****************************************/
22  /****************************************/
23 
25  CComposableEntity(pc_parent) {
26  Disable();
27  }
28 
29  /****************************************/
30  /****************************************/
31 
33  const std::string& str_id) :
34  CComposableEntity(pc_parent, str_id) {
35  Disable();
36  }
37 
38  /****************************************/
39  /****************************************/
40 
42  try {
43  /* Init parent */
45  /* Go through the magnet entries */
46  TConfigurationNodeIterator itMagnet("magnet");
47  for(itMagnet = itMagnet.begin(&t_tree);
48  itMagnet != itMagnet.end();
49  ++itMagnet) {
50  /* Initialise the Magnet using the XML */
51  auto* pcMagnet = new CMagnetEntity(this);
52  pcMagnet->Init(*itMagnet);
53  CVector3 cOffset;
54  GetNodeAttribute(*itMagnet, "offset", cOffset);
55  /* Parse and look up the anchor */
56  std::string strAnchorId;
57  GetNodeAttribute(*itMagnet, "anchor", strAnchorId);
58  /*
59  * NOTE: here we get a reference to the embodied entity
60  * This line works under the assumption that:
61  * 1. the MagnetEquippedEntity has a parent;
62  * 2. the parent has a child whose id is "body"
63  * 3. the "body" is an embodied entity
64  * If any of the above is false, this line will bomb out.
65  */
66  auto& cBody = GetParent().GetComponent<CEmbodiedEntity>("body");
67  /* Add the magnet to this container */
68  m_vecInstances.emplace_back(*pcMagnet,
69  cBody.GetAnchor(strAnchorId),
70  cOffset);
71  AddComponent(*pcMagnet);
72  }
74  }
75  catch(CARGoSException& ex) {
76  THROW_ARGOSEXCEPTION_NESTED("Failed to initialize magnet equipped entity \"" << GetId() << "\".", ex);
77  }
78  }
79 
80  /****************************************/
81  /****************************************/
82 
85  for(SInstance& s_instance : m_vecInstances) {
86  s_instance.Magnet.Enable();
87  }
88  }
89 
90  /****************************************/
91  /****************************************/
92 
95  for(SInstance& s_instance : m_vecInstances) {
96  s_instance.Magnet.Disable();
97  }
98  }
99 
100  /****************************************/
101  /****************************************/
102 
104  ARGOS_ASSERT(un_index < m_vecInstances.size(),
105  "CMagnetEquippedEntity::GetMagnet(), id=\"" <<
106  GetId() <<
107  "\": index out of bounds: un_index = " <<
108  un_index <<
109  ", m_vecInstances.size() = " <<
110  m_vecInstances.size());
111  return m_vecInstances[un_index].Magnet;
112  }
113 
114  /****************************************/
115  /****************************************/
116 
118  SAnchor& s_anchor,
119  const CVector3& c_position_offset,
120  const CVector3& c_passive_field,
121  const CVector3& c_active_field) {
122  auto* pcMagnet = new CMagnetEntity(this, str_id, c_passive_field, c_active_field);
123  m_vecInstances.emplace_back(*pcMagnet, s_anchor, c_position_offset);
124  AddComponent(*pcMagnet);
125  return *pcMagnet;
126  }
127 
128  /****************************************/
129  /****************************************/
130 
132 
133  /****************************************/
134  /****************************************/
135 
136 }
#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.
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
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
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.
A 3D vector class.
Definition: vector3.h:31
A container of CMagnetEntity.
CMagnetEntity & GetMagnet(UInt32 un_index)
Returns an magnet by numeric index.
CMagnetEntity & AddMagnet(const std::string &str_id, SAnchor &s_anchor, const CVector3 &c_position_offset, const CVector3 &c_passive_field, const CVector3 &c_active_field=CVector3())
SInstance::TVector m_vecInstances
A list of the magnets managed by this entity.
virtual void UpdateComponents()
Calls the Update() method on all the components.
CMagnetEquippedEntity(CComposableEntity *pc_parent)
Class constructor.
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
SInstance(CMagnetEntity &c_magnet, SAnchor &s_anchor, const CVector3 &c_position_offset)