physics_model.h
Go to the documentation of this file.
1 
7 #ifndef PHYSICS_MODEL_H
8 #define PHYSICS_MODEL_H
9 
10 namespace 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 
26 namespace argos {
27 
28  /****************************************/
29  /****************************************/
30 
38  struct SAnchor {
42  std::string Id;
67  SAnchor(CEmbodiedEntity& c_body,
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 
111  CPhysicsModel(CPhysicsEngine& c_engine,
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 
190  virtual void MoveTo(const CVector3& c_position,
191  const CQuaternion& c_orientation) = 0;
192 
198  inline const SBoundingBox& GetBoundingBox() const {
199  return m_sBoundingBox;
200  }
201 
206  virtual void CalculateBoundingBox() = 0;
207 
213  virtual void CalculateAnchors();
214 
219  virtual bool IsCollidingWithSomething() const = 0;
220 
227  return m_sBoundingBox;
228  }
229 
230  private:
231 
232  CPhysicsEngine& m_cEngine;
233  CEmbodiedEntity& m_cEmbodiedEntity;
234  SBoundingBox m_sBoundingBox;
235 
236  private:
237 
242  typedef void (CPhysicsModel::*TThunk)(SAnchor&);
243 
248  class CAnchorMethodHolder {};
249 
256  template <typename MODEL> class CAnchorMethodHolderImpl : public CAnchorMethodHolder {
257  public:
258  typedef void (MODEL::*TMethod)(SAnchor&);
259  TMethod Method;
260  CAnchorMethodHolderImpl(TMethod t_method) : Method(t_method) {}
261  };
262 
263  private:
264 
273  template <typename USER_IMPL>
274  void Thunk(SAnchor& s_anchor);
275 
276  private:
277 
282  std::vector<CAnchorMethodHolder*> m_vecAnchorMethodHolders;
283 
289  std::vector<TThunk> m_vecThunks;
290 
291  public:
292 
299  template <typename MODEL>
300  void RegisterAnchorMethod(const SAnchor& s_anchor,
301  void(MODEL::*pt_method)(SAnchor&));
302 
303  };
304 
305  /****************************************/
306  /****************************************/
307 
308  template <typename MODEL>
309  void CPhysicsModel::Thunk(SAnchor& s_anchor) {
310  /*
311  * When this method is called, the static type of 'this'
312  * is CPhysicsModel. Since we want to call
313  * method in MODEL (subclass of CPhysicsModel),
314  * we need a cast. The cast is static because we trust
315  * the user on not doing anything stupid.
316  */
317  MODEL& cImpl = static_cast<MODEL&>(*this);
318  /* Cast the method holder to its effective type */
319  CAnchorMethodHolderImpl<MODEL>& cMethodHolder = static_cast<CAnchorMethodHolderImpl<MODEL>&>(*m_vecAnchorMethodHolders[s_anchor.Index]);
320  /* Call the user-defined method */
321  (cImpl.*(cMethodHolder.Method))(s_anchor);
322  }
323 
324  template <typename MODEL>
326  void(MODEL::*pt_method)(SAnchor&)) {
327  /* Add the thunk to the VTable */
328  m_vecThunks[s_anchor.Index] = &CPhysicsModel::Thunk<MODEL>;
329  /* Add the method holder to the map */
330  m_vecAnchorMethodHolders[s_anchor.Index] = new CAnchorMethodHolderImpl<MODEL>(pt_method);
331  }
332 
333  /****************************************/
334  /****************************************/
335 
336 }
337 
338 #endif
A 3D vector class.
Definition: vector3.h:29
std::vector< CPhysicsModel * > TVector
CPhysicsModel(CPhysicsEngine &c_engine, CEmbodiedEntity &c_entity)
Real GetX() const
Returns the x coordinate of this vector.
Definition: vector3.h:93
CQuaternion OffsetOrientation
The initial orientation of the anchor wrt the body coordinate system.
Definition: physics_model.h:49
Real GetY() const
Returns the y coordinate of this vector.
Definition: vector3.h:109
virtual void CalculateBoundingBox()=0
Calculates the axis-aligned box that contains the entire physics model.
bool Intersects(const SBoundingBox &s_bb) const
Definition: physics_model.h:91
CEmbodiedEntity & Body
The embodied entity that owns this anchor.
Definition: physics_model.h:40
CQuaternion Orientation
The orientation of the anchor wrt the global coordinate system.
Definition: physics_model.h:53
SBoundingBox & GetBoundingBox()
Returns an axis-aligned box that contains the physics model.
std::map< std::string, CPhysicsModel * > TMap
This entity is a link to a body in the physics engine.
UInt32 Index
The index of the anchor assigned by the embodied entity.
Definition: physics_model.h:45
void Enable()
Enables this anchor.
virtual void UpdateEntityStatus()
Updates the status of the associated entity.
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
std::string Id
The id of the anchor.
Definition: physics_model.h:42
CPhysicsEngine & GetEngine()
Returns the physics engine associated to this physics model.
void RegisterAnchorMethod(const SAnchor &s_anchor, void(MODEL::*pt_method)(SAnchor &))
Registers an anchor method.
An anchor related to the body of an entity.
Definition: physics_model.h:38
virtual void UpdateFromEntityStatus()=0
Updates the state of this model from the status of the associated entity.
CEmbodiedEntity & GetEmbodiedEntity()
Returns the embodied entity associated to this physics model.
virtual void MoveTo(const CVector3 &c_position, const CQuaternion &c_orientation)=0
const SBoundingBox & GetBoundingBox() const
Returns an axis-aligned box that contains the physics model.
void Disable()
Disables this anchor.
CVector3 Position
The position of the anchor wrt the global coordinate system.
Definition: physics_model.h:51
CVector3 OffsetPosition
The initial position of the anchor wrt the body coordinate system.
Definition: physics_model.h:47
virtual void CalculateAnchors()
Calculates the anchors associated to this model.
UInt32 InUseCount
A counter for the devices using this anchor.
Definition: physics_model.h:55
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
Real GetZ() const
Returns the z coordinate of this vector.
Definition: vector3.h:125
const CEmbodiedEntity & GetEmbodiedEntity() const
Returns the embodied entity associated to this physics model.
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.
virtual bool IsCollidingWithSomething() const =0
Returns true if this model is colliding with another model.