ARGoS  3
A parallel, multi-engine simulator for swarm robotics
qtopengl_user_functions.h
Go to the documentation of this file.
1 
7 #ifndef QTOPENGL_USER_FUNCTIONS_H
8 #define QTOPENGL_USER_FUNCTIONS_H
9 
10 namespace argos {
11  class CQTOpenGLUserFunctions;
12  class CFloorEntity;
13 }
14 
15 class QPainter;
16 
17 #include <argos3/core/utility/configuration/base_configurable_resource.h>
18 #include <argos3/plugins/simulator/visualizations/qt-opengl/qtopengl_main_window.h>
19 #include <argos3/plugins/simulator/visualizations/qt-opengl/qtopengl_widget.h>
20 #include <argos3/core/utility/datatypes/color.h>
21 #include <argos3/core/utility/math/quaternion.h>
22 
23 namespace argos {
24 
75 
76  public:
77 
82 
86  virtual ~CQTOpenGLUserFunctions();
87 
88  virtual void Init(TConfigurationNode& t_tree) {}
89  virtual void Reset() {}
90  virtual void Destroy() {}
91 
104  virtual void KeyPressed(QKeyEvent* pc_event);
105 
118  virtual void KeyReleased(QKeyEvent* pc_event);
119 
124  virtual void EntitySelected(CEntity& c_entity) {}
125 
130  virtual void EntityDeselected(CEntity& c_entity) {}
131 
136 
145  virtual void SelectEntity(CEntity& c_entity);
146 
153  virtual void DeselectEntity();
154 
158  virtual void Draw(CFloorEntity& c_entity) {}
159 
165  virtual void DrawInWorld() {}
166 
174  virtual void DrawOverlay(QPainter& c_painter) {}
175 
181 
186  void SetMainWindow(CQTOpenGLMainWindow& c_main_win);
187 
193 
198  void SetColor(const CColor& c_color);
199 
207  void DrawPoint(const CVector3& c_position,
208  const CColor& c_color = CColor::RED,
209  Real f_diameter = 5.0);
210 
222  void DrawTriangle(const CVector3& c_position,
223  const CQuaternion& c_orientation,
224  Real f_base,
225  Real f_height,
226  const CColor& c_color = CColor::RED,
227  const bool b_fill = true);
228 
238  void DrawPolygon(const CVector3& c_position,
239  const CQuaternion& c_orientation,
240  const std::vector<CVector2>& vec_points,
241  const CColor& c_color = CColor::RED,
242  const bool b_fill = true);
243 
255  void DrawCircle(const CVector3& c_position,
256  const CQuaternion& c_orientation,
257  Real f_radius,
258  const CColor& c_color = CColor::RED,
259  const bool b_fill = true,
260  GLuint un_vertices = 20);
261 
273  void DrawCylinder(const CVector3& c_position,
274  const CQuaternion& c_orientation,
275  Real f_radius,
276  Real f_height,
277  const CColor& c_color = CColor::RED,
278  GLuint un_vertices = 20);
279 
289  void DrawBox(const CVector3& c_position,
290  const CQuaternion& c_orientation,
291  const CVector3& c_size,
292  const CColor& c_color = CColor::RED);
293 
301  void DrawRay(const CRay3& c_ray,
302  const CColor& c_color = CColor::RED,
303  Real f_width = 1.0f);
304 
313  void DrawText(const CVector3& c_position,
314  const std::string& str_text,
315  const CColor& c_color = CColor::BLACK,
316  const QFont& c_font = QFont());
317 
318  protected:
319 
325 
335  template <typename USER_IMPL, typename ENTITY>
336  void Thunk(CEntity& c_entity);
337 
342  class CFunctionHolder {};
343 
351  template <typename USER_IMPL, typename ENTITY> class CFunctionHolderImpl : public CFunctionHolder {
352  public:
353  typedef void (USER_IMPL::*TFunction)(ENTITY&);
355  CFunctionHolderImpl(TFunction t_function) : Function(t_function) {}
356  };
357 
364 
369  std::vector<CFunctionHolder*> m_vecFunctionHolders;
370 
371  public:
372 
379  template <typename USER_IMPL, typename ENTITY>
380  void RegisterUserFunction(void(USER_IMPL::*pt_function)(ENTITY&));
381 
386  virtual void Call(CEntity& c_entity);
387 
388  private:
389 
393  CQTOpenGLMainWindow* m_pcQTOpenGLMainWindow;
394 
395  };
396 
397  /****************************************/
398  /****************************************/
399 
400  template <typename USER_IMPL, typename ENTITY>
402  /*
403  * When this method is called, the static type of 'this'
404  * is CQTOpenGLUserFunctions. Since we want to call
405  * method in USER_IMPL (subclass of CQTOpenGLUserFunctions),
406  * we need a cast. The cast is static because we trust
407  * the user on not doing anything stupid.
408  * The variable cImpl can be static because the cast is necessary
409  * only the first time this function is called.
410  */
411  static USER_IMPL& cImpl = static_cast<USER_IMPL&>(*this);
412  /* Cast the argument to the right type */
413  ENTITY& cEntity = static_cast<ENTITY&>(c_entity);
414  /* Cast the function holder to its effective type */
415  CFunctionHolderImpl<USER_IMPL,ENTITY>& cFunctionHolder = static_cast<CFunctionHolderImpl<USER_IMPL,ENTITY>&>(*m_vecFunctionHolders[GetTag<ENTITY,CEntity>()]);
416  /* Call the user-defined method */
417  (cImpl.*(cFunctionHolder.Function))(cEntity);
418  }
419 
420  template <typename USER_IMPL, typename ENTITY>
421  void CQTOpenGLUserFunctions::RegisterUserFunction(void(USER_IMPL::*pt_function)(ENTITY&)) {
422  /* Add the thunk to the VTable */
423  m_cThunks.Add<ENTITY>(&CQTOpenGLUserFunctions::Thunk<USER_IMPL,ENTITY>);
424  /* Add the function holder to the vector, padding gaps with NULL pointers */
425  size_t unIdx = GetTag<ENTITY,CEntity>();
426  if(m_vecFunctionHolders.size() <= unIdx) {
427  m_vecFunctionHolders.resize(unIdx+1, NULL);
428  }
430  }
431 
432  /****************************************/
433  /****************************************/
434 
435 }
436 
437 /* Definitions useful for dynamic linking of user functions */
438 #define REGISTER_QTOPENGL_USER_FUNCTIONS(CLASSNAME, LABEL) \
439  REGISTER_SYMBOL(CQTOpenGLUserFunctions, \
440  CLASSNAME, \
441  LABEL, \
442  "undefined", \
443  "undefined", \
444  "undefined", \
445  "undefined", \
446  "undefined")
447 
448 #endif
argos::CQTOpenGLUserFunctions::Destroy
virtual void Destroy()
Undoes whatever was done by Init().
Definition: qtopengl_user_functions.h:90
argos::CBaseConfigurableResource
This class is the base of all XML-configurable ARGoS interface.
Definition: base_configurable_resource.h:23
argos::CQTOpenGLUserFunctions::DrawRay
void DrawRay(const CRay3 &c_ray, const CColor &c_color=CColor::RED, Real f_width=1.0f)
Draws a ray, with optional endpoint markers.
Definition: qtopengl_user_functions.cpp:365
argos
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
argos::CVector3
A 3D vector class.
Definition: vector3.h:29
argos::CQTOpenGLUserFunctions::DrawCircle
void DrawCircle(const CVector3 &c_position, const CQuaternion &c_orientation, Real f_radius, const CColor &c_color=CColor::RED, const bool b_fill=true, GLuint un_vertices=20)
Draws a circle.
Definition: qtopengl_user_functions.cpp:219
argos::CQTOpenGLUserFunctions::DrawInWorld
virtual void DrawInWorld()
Drawing hook executed after all objects have been drawn.
Definition: qtopengl_user_functions.h:165
argos::CQTOpenGLUserFunctions::TThunk
void(CQTOpenGLUserFunctions::* TThunk)(CEntity &)
Pointer-to-thunk type definition.
Definition: qtopengl_user_functions.h:324
argos::CQTOpenGLWidget
Definition: qtopengl_widget.h:56
argos::CQTOpenGLUserFunctions::RegisterUserFunction
void RegisterUserFunction(void(USER_IMPL::*pt_function)(ENTITY &))
Registers a user method.
Definition: qtopengl_user_functions.h:421
argos::CQTOpenGLUserFunctions::CFunctionHolderImpl::TFunction
void(USER_IMPL::* TFunction)(ENTITY &)
Definition: qtopengl_user_functions.h:353
argos::CRay3
Definition: ray3.h:19
argos::CQTOpenGLUserFunctions::SetColor
void SetColor(const CColor &c_color)
Sets the current drawing color.
Definition: qtopengl_user_functions.cpp:114
argos::CQTOpenGLUserFunctions::DrawText
void DrawText(const CVector3 &c_position, const std::string &str_text, const CColor &c_color=CColor::BLACK, const QFont &c_font=QFont())
Draws a string of text.
Definition: qtopengl_user_functions.cpp:420
argos::CQTOpenGLUserFunctions::EntitySelected
virtual void EntitySelected(CEntity &c_entity)
Called every time an entity is selected.
Definition: qtopengl_user_functions.h:124
argos::CVTable
The actual vtable.
Definition: vtable.h:155
argos::TConfigurationNode
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
Definition: argos_configuration.h:27
argos::CFloorEntity
Definition: floor_entity.h:21
argos::CQTOpenGLUserFunctions::CQTOpenGLUserFunctions
CQTOpenGLUserFunctions()
Class constructor.
Definition: qtopengl_user_functions.cpp:39
argos::CQTOpenGLUserFunctions::DeselectEntity
virtual void DeselectEntity()
Deselects the currently selected entity.
Definition: qtopengl_user_functions.cpp:86
argos::CQTOpenGLUserFunctions::CFunctionHolderImpl
The actual function holder.
Definition: qtopengl_user_functions.h:351
argos::CQTOpenGLUserFunctions::DrawTriangle
void DrawTriangle(const CVector3 &c_position, const CQuaternion &c_orientation, Real f_base, Real f_height, const CColor &c_color=CColor::RED, const bool b_fill=true)
Draws an isosceles triangle.
Definition: qtopengl_user_functions.cpp:150
argos::CQTOpenGLUserFunctions::DrawPoint
void DrawPoint(const CVector3 &c_position, const CColor &c_color=CColor::RED, Real f_diameter=5.0)
Draws a point.
Definition: qtopengl_user_functions.cpp:129
argos::CQuaternion
Definition: quaternion.h:14
argos::CQTOpenGLUserFunctions::SelectEntity
virtual void SelectEntity(CEntity &c_entity)
Selects the specified entity.
Definition: qtopengl_user_functions.cpp:79
argos::CColor::RED
static CColor RED
Definition: color.h:31
argos::CQTOpenGLUserFunctions::SetMainWindow
void SetMainWindow(CQTOpenGLMainWindow &c_main_win)
Sets the QTOpenGL main window for these user functions.
Definition: qtopengl_user_functions.cpp:100
argos::CQTOpenGLUserFunctions::Thunk
void Thunk(CEntity &c_entity)
A templetized thunk.
Definition: qtopengl_user_functions.h:401
argos::CQTOpenGLUserFunctions::Draw
virtual void Draw(CFloorEntity &c_entity)
Drawing hook executed after the floor is drawn.
Definition: qtopengl_user_functions.h:158
argos::CQTOpenGLUserFunctions::EntityDeselected
virtual void EntityDeselected(CEntity &c_entity)
Called every time an entity is deselected.
Definition: qtopengl_user_functions.h:130
argos::CQTOpenGLUserFunctions::KeyPressed
virtual void KeyPressed(QKeyEvent *pc_event)
Called when a key press event occurs.
Definition: qtopengl_user_functions.cpp:58
argos::CQTOpenGLUserFunctions
The QTOpenGL user functions.
Definition: qtopengl_user_functions.h:74
argos::CQTOpenGLUserFunctions::m_vecFunctionHolders
std::vector< CFunctionHolder * > m_vecFunctionHolders
A vector of function holders.
Definition: qtopengl_user_functions.h:369
argos::CQTOpenGLUserFunctions::DrawPolygon
void DrawPolygon(const CVector3 &c_position, const CQuaternion &c_orientation, const std::vector< CVector2 > &vec_points, const CColor &c_color=CColor::RED, const bool b_fill=true)
Draws a 2D polygon.
Definition: qtopengl_user_functions.cpp:183
argos::CQTOpenGLUserFunctions::GetMainWindow
CQTOpenGLMainWindow & GetMainWindow()
Returns the QTOpenGL main window.
Definition: qtopengl_user_functions.cpp:93
argos::CQTOpenGLUserFunctions::Reset
virtual void Reset()
Resets the resource.
Definition: qtopengl_user_functions.h:89
argos::CQTOpenGLUserFunctions::~CQTOpenGLUserFunctions
virtual ~CQTOpenGLUserFunctions()
Class destructor.
Definition: qtopengl_user_functions.cpp:48
argos::CQTOpenGLUserFunctions::Init
virtual void Init(TConfigurationNode &t_tree)
Initializes the resource.
Definition: qtopengl_user_functions.h:88
argos::CQTOpenGLUserFunctions::DrawBox
void DrawBox(const CVector3 &c_position, const CQuaternion &c_orientation, const CVector3 &c_size, const CColor &c_color=CColor::RED)
Draws a box.
Definition: qtopengl_user_functions.cpp:305
argos::CQTOpenGLUserFunctions::DrawOverlay
virtual void DrawOverlay(QPainter &c_painter)
Drawing hook to put graphics on top of the OpenGL window.
Definition: qtopengl_user_functions.h:174
argos::CQTOpenGLMainWindow
Definition: qtopengl_main_window.h:36
argos::CEntity
The basic entity type.
Definition: entity.h:89
argos::CQTOpenGLUserFunctions::Call
virtual void Call(CEntity &c_entity)
Calls a user method for the given entity.
Definition: qtopengl_user_functions.cpp:487
argos::CQTOpenGLUserFunctions::KeyReleased
virtual void KeyReleased(QKeyEvent *pc_event)
Called when a key release event occurs.
Definition: qtopengl_user_functions.cpp:65
argos::CQTOpenGLUserFunctions::m_cThunks
CVTable< CQTOpenGLUserFunctions, CEntity, TThunk > m_cThunks
The vtable storing the thunks.
Definition: qtopengl_user_functions.h:363
argos::CQTOpenGLUserFunctions::DrawCylinder
void DrawCylinder(const CVector3 &c_position, const CQuaternion &c_orientation, Real f_radius, Real f_height, const CColor &c_color=CColor::RED, GLuint un_vertices=20)
Draws a cylinder, with the height perpendicular to the XY plane.
Definition: qtopengl_user_functions.cpp:255
argos::CColor
The basic color type.
Definition: color.h:25
argos::CQTOpenGLUserFunctions::GetSelectedEntity
CEntity * GetSelectedEntity()
Returns the currently selected entity, or NULL.
Definition: qtopengl_user_functions.cpp:72
argos::CColor::BLACK
static CColor BLACK
Definition: color.h:29
Real
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
argos::CQTOpenGLUserFunctions::CFunctionHolderImpl::Function
TFunction Function
Definition: qtopengl_user_functions.h:354
argos::CQTOpenGLUserFunctions::CFunctionHolder
The base function holder.
Definition: qtopengl_user_functions.h:342
argos::CQTOpenGLUserFunctions::GetQTOpenGLWidget
CQTOpenGLWidget & GetQTOpenGLWidget()
Returns the QTOpenGLWidget.
Definition: qtopengl_user_functions.cpp:107
argos::CQTOpenGLUserFunctions::CFunctionHolderImpl::CFunctionHolderImpl
CFunctionHolderImpl(TFunction t_function)
Definition: qtopengl_user_functions.h:355