ARGoS 3
A parallel, multi-engine simulator for swarm robotics
physics_model.h
Go to the documentation of this file.
1
7#ifndef PHYSICS_MODEL_H
8#define PHYSICS_MODEL_H
9
10namespace argos {
11 class CPhysicsModel;
12 class CPhysicsEngine;
13 class CRay3;
14 class CQuaternion;
15 class CEmbodiedEntity;
16 struct SAnchor;
17}
18
19#include <argos3/core/utility/datatypes/datatypes.h>
20#include <argos3/core/utility/math/vector3.h>
21#include <argos3/core/utility/math/quaternion.h>
22#include <map>
23#include <vector>
24#include <string>
25
26namespace argos {
27
28 /****************************************/
29 /****************************************/
30
38 struct SAnchor {
42 std::string Id;
68 const std::string& str_id,
69 UInt32 un_index,
70 const CVector3& c_offset_position,
71 const CQuaternion& c_offset_orientation,
72 const CVector3& c_position,
73 const CQuaternion& c_orientation);
77 void Enable();
81 void Disable();
82 };
83
84 /****************************************/
85 /****************************************/
86
87 struct SBoundingBox {
90
91 inline bool Intersects(const SBoundingBox& s_bb) const {
92 return
93 (MinCorner.GetX() < s_bb.MaxCorner.GetX()) && (MaxCorner.GetX() > s_bb.MinCorner.GetX()) &&
94 (MinCorner.GetY() < s_bb.MaxCorner.GetY()) && (MaxCorner.GetY() > s_bb.MinCorner.GetY()) &&
95 (MinCorner.GetZ() < s_bb.MaxCorner.GetZ()) && (MaxCorner.GetZ() > s_bb.MinCorner.GetZ());
96 }
97 };
98
99 /****************************************/
100 /****************************************/
101
103
104 public:
105
106 typedef std::map<std::string, CPhysicsModel*> TMap;
107 typedef std::vector<CPhysicsModel*> TVector;
108
109 public:
110
112 CEmbodiedEntity& c_entity);
113
114 virtual ~CPhysicsModel() {
115 while(!m_vecAnchorMethodHolders.empty()) {
116 delete m_vecAnchorMethodHolders.back();
117 m_vecAnchorMethodHolders.pop_back();
118 }
119 }
120
126 return m_cEngine;
127 }
128
134 return m_cEmbodiedEntity;
135 }
136
141 inline const CEmbodiedEntity& GetEmbodiedEntity() const {
142 return m_cEmbodiedEntity;
143 }
144
160 virtual void UpdateEntityStatus();
161
168 virtual void UpdateFromEntityStatus() = 0;
169
184 virtual void UpdatePhysics() {}
185
206 virtual void MoveTo(const CVector3& c_position,
207 const CQuaternion& c_orientation) = 0;
208
214 inline const SBoundingBox& GetBoundingBox() const {
215 return m_sBoundingBox;
216 }
217
222 virtual void CalculateBoundingBox() = 0;
223
229 virtual void CalculateAnchors();
230
235 virtual bool IsCollidingWithSomething() const = 0;
236
243 return m_sBoundingBox;
244 }
245
246 private:
247
248 CPhysicsEngine& m_cEngine;
249 CEmbodiedEntity& m_cEmbodiedEntity;
250 SBoundingBox m_sBoundingBox;
251
252 private:
253
258 typedef void (CPhysicsModel::*TThunk)(SAnchor&);
259
264 class CAnchorMethodHolder {};
265
272 template <typename MODEL> class CAnchorMethodHolderImpl : public CAnchorMethodHolder {
273 public:
274 typedef void (MODEL::*TMethod)(SAnchor&);
275 TMethod Method;
276 CAnchorMethodHolderImpl(TMethod t_method) : Method(t_method) {}
277 };
278
279 private:
280
289 template <typename USER_IMPL>
290 void Thunk(SAnchor& s_anchor);
291
292 private:
293
298 std::vector<CAnchorMethodHolder*> m_vecAnchorMethodHolders;
299
305 std::vector<TThunk> m_vecThunks;
306
307 public:
308
315 template <typename MODEL>
316 void RegisterAnchorMethod(const SAnchor& s_anchor,
317 void(MODEL::*pt_method)(SAnchor&));
318
319 };
320
321 /****************************************/
322 /****************************************/
323
324 template <typename MODEL>
325 void CPhysicsModel::Thunk(SAnchor& s_anchor) {
326 /*
327 * When this method is called, the static type of 'this'
328 * is CPhysicsModel. Since we want to call
329 * method in MODEL (subclass of CPhysicsModel),
330 * we need a cast. The cast is static because we trust
331 * the user on not doing anything stupid.
332 */
333 MODEL& cImpl = static_cast<MODEL&>(*this);
334 /* Cast the method holder to its effective type */
335 CAnchorMethodHolderImpl<MODEL>& cMethodHolder = static_cast<CAnchorMethodHolderImpl<MODEL>&>(*m_vecAnchorMethodHolders[s_anchor.Index]);
336 /* Call the user-defined method */
337 (cImpl.*(cMethodHolder.Method))(s_anchor);
338 }
339
340 template <typename MODEL>
342 void(MODEL::*pt_method)(SAnchor&)) {
343 /* Add the thunk to the VTable */
344 m_vecThunks[s_anchor.Index] = &CPhysicsModel::Thunk<MODEL>;
345 /* Add the method holder to the map */
346 m_vecAnchorMethodHolders[s_anchor.Index] = new CAnchorMethodHolderImpl<MODEL>(pt_method);
347 }
348
349 /****************************************/
350 /****************************************/
351
352}
353
354#endif
unsigned int UInt32
32-bit unsigned integer.
Definition datatypes.h:97
The namespace containing all the ARGoS related code.
Definition ci_actuator.h:12
This entity is a link to a body in the physics engine.
An anchor related to the body of an entity.
CEmbodiedEntity & Body
The embodied entity that owns this anchor.
CQuaternion Orientation
The orientation of the anchor wrt the global coordinate system.
CQuaternion OffsetOrientation
The initial orientation of the anchor wrt the body coordinate system.
std::string Id
The id of the anchor.
void Disable()
Disables this anchor.
SAnchor(CEmbodiedEntity &c_body, const std::string &str_id, UInt32 un_index, const CVector3 &c_offset_position, const CQuaternion &c_offset_orientation, const CVector3 &c_position, const CQuaternion &c_orientation)
Struct constructor.
UInt32 Index
The index of the anchor assigned by the embodied entity.
CVector3 Position
The position of the anchor wrt the global coordinate system.
void Enable()
Enables this anchor.
UInt32 InUseCount
A counter for the devices using this anchor.
CVector3 OffsetPosition
The initial position of the anchor wrt the body coordinate system.
bool Intersects(const SBoundingBox &s_bb) const
std::map< std::string, CPhysicsModel * > TMap
virtual void UpdateFromEntityStatus()=0
Updates the state of this model from the status of the associated entity.
void RegisterAnchorMethod(const SAnchor &s_anchor, void(MODEL::*pt_method)(SAnchor &))
Registers an anchor method.
CPhysicsEngine & GetEngine()
Returns the physics engine associated to this physics model.
const SBoundingBox & GetBoundingBox() const
Returns an axis-aligned box that contains the physics model.
virtual void UpdatePhysics()
Performs extra work just before the physics update takes place.
CEmbodiedEntity & GetEmbodiedEntity()
Returns the embodied entity associated to this physics model.
virtual void MoveTo(const CVector3 &c_position, const CQuaternion &c_orientation)=0
virtual void CalculateAnchors()
Calculates the anchors associated to this model.
CPhysicsModel(CPhysicsEngine &c_engine, CEmbodiedEntity &c_entity)
SBoundingBox & GetBoundingBox()
Returns an axis-aligned box that contains the physics model.
virtual bool IsCollidingWithSomething() const =0
Returns true if this model is colliding with another model.
virtual void CalculateBoundingBox()=0
Calculates the axis-aligned box that contains the entire physics model.
const CEmbodiedEntity & GetEmbodiedEntity() const
Returns the embodied entity associated to this physics model.
std::vector< CPhysicsModel * > TVector
virtual void UpdateEntityStatus()
Updates the status of the associated entity.
A 3D vector class.
Definition vector3.h:31
Real GetX() const
Returns the x coordinate of this vector.
Definition vector3.h:105
Real GetY() const
Returns the y coordinate of this vector.
Definition vector3.h:121
Real GetZ() const
Returns the z coordinate of this vector.
Definition vector3.h:137