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  CMagnetEntity* 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  */
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 
119  /****************************************/
120  /****************************************/
121 
122 }
A 3D vector class.
Definition: vector3.h:29
void Enable()
Enables the entity.
Definition: entity.h:265
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
This entity is a link to a body in the physics engine.
virtual void UpdateComponents()
Calls the Update() method on all the components.
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception...
SInstance(CMagnetEntity &c_magnet, SAnchor &s_anchor, const CVector3 &c_position_offset)
#define ARGOS_ASSERT(condition, message)
When code is compiled in debug, this macro throws an ARGoS exception with the passed message if the s...
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
Basic class for an entity that contains other entities.
CMagnetEntity & GetMagnet(UInt32 un_index)
Returns an magnet by numeric index.
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
An anchor related to the body of an entity.
Definition: physics_model.h:38
void AddComponent(CEntity &c_component)
Adds a component to this composable entity.
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
ticpp::Iterator< ticpp::Element > TConfigurationNodeIterator
The iterator for the ARGoS configuration XML node.
void GetNodeAttribute(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer)
Returns the value of a node's attribute.
The exception that wraps all errors in ARGoS.
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
Definition: entity.cpp:40
SInstance::TVector m_vecInstances
A list of the magnets managed by this entity.
void Disable()
Disables the entity.
Definition: entity.h:275
A container of CMagnetEntity.
const SAnchor & GetAnchor(const std::string &str_id) const
Returns the wanted anchor as a const reference.
const std::string & GetId() const
Returns the id of this entity.
Definition: entity.h:157
CComposableEntity & GetParent()
Returns this entity's parent.
Definition: entity.cpp:91
REGISTER_STANDARD_SPACE_OPERATIONS_ON_COMPOSABLE(CComposableEntity)
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
CMagnetEquippedEntity(CComposableEntity *pc_parent)
Class constructor.
CEntity & GetComponent(const std::string &str_component)
Returns the component with the passed string label.