MRPT  1.9.9
CRenderizable.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/img/TColor.h>
12 #include <mrpt/math/TPoint3D.h>
13 #include <mrpt/math/math_frwds.h>
15 #include <mrpt/poses/CPose3D.h>
17 #include <deque>
18 
19 namespace mrpt::opengl
20 {
21 class COpenGLViewport;
22 class CSetOfObjects;
23 
24 /** The base class of 3D objects that can be directly rendered through OpenGL.
25  * In this class there are a set of common properties to all 3D objects,
26  *mainly:
27  * - A name (m_name): A name that can be optionally asigned to objects for
28  *easing its reference.
29  * - 6D coordinates (x,y,z,yaw,pitch,roll), relative to the "current"
30  *reference framework. By default, any object is referenced to global scene
31  *coordinates.
32  * - A RGB color: This field will be used in simple elements (points,
33  *lines,
34  *text,...) but is ignored in more complex objects that carry their own color
35  *information (triangle sets,...)
36  * See the main class opengl::COpenGLScene
37  * \sa opengl::COpenGLScene, mrpt::opengl
38  * \ingroup mrpt_opengl_grp
39  */
41 {
43 
44  friend class mrpt::opengl::COpenGLViewport;
45  friend class mrpt::opengl::CSetOfObjects;
46 
47  protected:
48  std::string m_name;
49  bool m_show_name{false};
50  /** Color components in the range [0,255] */
52  /** 6D pose wrt the parent coordinate reference. This class automatically
53  * holds the cached 3x3 rotation matrix for quick load into opengl stack. */
55  /** Scale components to apply to the object (default=1) */
56  float m_scale_x{1}, m_scale_y{1}, m_scale_z{1};
57  /** Is the object visible? (default=true) */
58  bool m_visible{true};
59 
60  public:
61  /** @name Changes the appearance of the object to render
62  @{ */
63 
64  /** Changes the name of the object */
65  void setName(const std::string& n) { m_name = n; }
66  /** Returns the name of the object */
67  const std::string& getName() const { return m_name; }
68  inline bool isVisible()
69  const /** Is the object visible? \sa setVisibility */
70  {
71  return m_visible;
72  }
73  inline void setVisibility(
74  bool visible =
75  true) /** Set object visibility (default=true) \sa isVisible */
76  {
77  m_visible = visible;
78  }
79 
80  /** Enables or disables showing the name of the object as a label when
81  * rendering */
82  inline void enableShowName(bool showName = true) { m_show_name = showName; }
83  /** \sa enableShowName */
84  inline bool isShowNameEnabled() const { return m_show_name; }
85  /** Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to
86  * this) */
88  /** Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to
89  * this) */
91  /** Set the 3D pose from a mrpt::math::TPose3D object (return a ref to
92  * this) */
94  /** Set the 3D pose from a mrpt::math::TPose3D object (return a ref to
95  * this) */
97  /** Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to
98  * this) */
100  /** Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to
101  * this) */
103 
104  /** Returns the 3D pose of the object as TPose3D */
106  /** Returns a const ref to the 3D pose of the object as mrpt::poses::CPose3D
107  * (which explicitly contains the 3x3 rotation matrix) */
108  inline const mrpt::poses::CPose3D& getPoseRef() const { return m_pose; }
109  /** Changes the location of the object, keeping untouched the orientation
110  * \return a ref to this */
111  inline CRenderizable& setLocation(double x, double y, double z)
112  {
113  m_pose.x(x);
114  m_pose.y(y);
115  m_pose.z(z);
116  return *this;
117  }
118 
119  /** Changes the location of the object, keeping untouched the orientation
120  * \return a ref to this */
122  {
123  m_pose.x(p.x);
124  m_pose.y(p.y);
125  m_pose.z(p.z);
126  return *this;
127  }
128 
129  /** Translation relative to parent coordinate origin. */
130  inline double getPoseX() const { return m_pose.x(); }
131  /** Translation relative to parent coordinate origin. */
132  inline double getPoseY() const { return m_pose.y(); }
133  /** Translation relative to parent coordinate origin. */
134  inline double getPoseZ() const { return m_pose.z(); }
135  /** Rotation relative to parent coordinate origin, in **DEGREES**. */
136  inline double getPoseYaw() const { return mrpt::RAD2DEG(m_pose.yaw()); }
137  /** Rotation relative to parent coordinate origin, in **DEGREES**. */
138  inline double getPosePitch() const { return mrpt::RAD2DEG(m_pose.pitch()); }
139  /** Rotation relative to parent coordinate origin, in **DEGREES**. */
140  inline double getPoseRoll() const { return mrpt::RAD2DEG(m_pose.roll()); }
141  /** Rotation relative to parent coordinate origin, in radians. */
142  inline double getPoseYawRad() const { return m_pose.yaw(); }
143  /** Rotation relative to parent coordinate origin, in radians. */
144  inline double getPosePitchRad() const { return m_pose.pitch(); }
145  /** Rotation relative to parent coordinate origin, in radians. */
146  inline double getPoseRollRad() const { return m_pose.roll(); }
147  /** Color components in the range [0,1] */
148  inline double getColorR() const { return m_color.R / 255.; }
149  /** Color components in the range [0,1] */
150  inline double getColorG() const { return m_color.G / 255.; }
151  /** Color components in the range [0,1] */
152  inline double getColorB() const { return m_color.B / 255.; }
153  /** Color components in the range [0,1] */
154  inline double getColorA() const { return m_color.A / 255.; }
155  /** Color components in the range [0,255] */
156  inline uint8_t getColorR_u8() const { return m_color.R; }
157  /** Color components in the range [0,255] */
158  inline uint8_t getColorG_u8() const { return m_color.G; }
159  /** Color components in the range [0,255] */
160  inline uint8_t getColorB_u8() const { return m_color.B; }
161  /** Color components in the range [0,255] */
162  inline uint8_t getColorA_u8() const { return m_color.A; }
163  /**Color components in the range [0,1] \return a ref to this */
164  CRenderizable& setColorR(const double r)
165  {
166  return setColorR_u8(static_cast<uint8_t>(255 * r));
167  }
168  /**Color components in the range [0,1] \return a ref to this */
169  CRenderizable& setColorG(const double g)
170  {
171  return setColorG_u8(static_cast<uint8_t>(255 * g));
172  }
173  /**Color components in the range [0,1] \return a ref to this */
174  CRenderizable& setColorB(const double b)
175  {
176  return setColorB_u8(static_cast<uint8_t>(255 * b));
177  }
178  /**Color components in the range [0,1] \return a ref to this */
179  CRenderizable& setColorA(const double a)
180  {
181  return setColorA_u8(static_cast<uint8_t>(255 * a));
182  }
183  /**Color components in the range [0,255] \return a ref to this */
185  {
186  m_color.R = r;
187  return *this;
188  }
189  /**Color components in the range [0,255] \return a ref to this */
191  {
192  m_color.G = g;
193  return *this;
194  }
195  /**Color components in the range [0,255] \return a ref to this */
197  {
198  m_color.B = b;
199  return *this;
200  }
201  /**Color components in the range [0,255] \return a ref to this */
203  {
204  m_color.A = a;
205  return *this;
206  }
207 
208  /** Scale to apply to the object, in all three axes (default=1) \return a
209  * ref to this */
210  inline CRenderizable& setScale(float s)
211  {
213  return *this;
214  }
215  /** Scale to apply to the object in each axis (default=1) \return a ref to
216  * this */
217  inline CRenderizable& setScale(float sx, float sy, float sz)
218  {
219  m_scale_x = sx;
220  m_scale_y = sy;
221  m_scale_z = sz;
222  return *this;
223  }
224  /** Get the current scaling factor in one axis */
225  inline float getScaleX() const { return m_scale_x; }
226  /** Get the current scaling factor in one axis */
227  inline float getScaleY() const { return m_scale_y; }
228  /** Get the current scaling factor in one axis */
229  inline float getScaleZ() const { return m_scale_z; }
230  /** Returns the object color property as a TColorf */
232  {
233  return mrpt::img::TColorf(m_color);
234  }
235  /** Changes the default object color \return a ref to this */
237  {
239  c.R * 255.f, c.G * 255.f, c.B * 255.f, c.A * 255.f));
240  }
241 
242  /** Set the color components of this object (R,G,B,Alpha, in the range 0-1)
243  * \return a ref to this */
244  inline CRenderizable& setColor(double R, double G, double B, double A = 1)
245  {
246  return setColor_u8(R * 255, G * 255, B * 255, A * 255);
247  }
248 
249  /** Returns the object color property as a TColor */
250  inline const mrpt::img::TColor& getColor_u8() const { return m_color; }
251  /*** Changes the default object color \return a ref to this */
252  virtual CRenderizable& setColor_u8(const mrpt::img::TColor& c);
253 
254  /** Set the color components of this object (R,G,B,Alpha, in the range
255  * 0-255) \return a ref to this */
257  {
258  return setColor_u8(mrpt::img::TColor(R, G, B, A));
259  }
260 
261  /** @} */
262 
263  /** Default constructor: */
264  CRenderizable();
265  ~CRenderizable() override;
266 
267  /** Implements the rendering of 3D objects in each class derived from
268  * CRenderizable.
269  */
270  virtual void render() const = 0;
271 
272  /** Simulation of ray-trace, given a pose. Returns true if the ray
273  * effectively collisions with the object (returning the distance to the
274  * origin of the ray in "dist"), or false in other case. "dist" variable
275  * yields undefined behaviour when false is returned
276  */
277  virtual bool traceRay(const mrpt::poses::CPose3D& o, double& dist) const;
278 
279  /** This method is safe for calling from within ::render() methods \sa
280  * renderTextBitmap, mrpt::opengl::gl_utils */
281  static void renderTextBitmap(const char* str, void* fontStyle);
282 
283  /** Return the exact width in pixels for a given string, as will be rendered
284  * by renderTextBitmap().
285  * \sa renderTextBitmap, mrpt::opengl::gl_utils
286  */
287  static int textBitmapWidth(
288  const std::string& str,
291 
292  /** Render a text message in the current rendering context, creating a
293  * glViewport in the way (do not call within ::render() methods)
294  * - Coordinates (x,y) are 2D pixels, starting at bottom-left of the
295  * viewport. Negative numbers will wrap to the opposite side of the viewport
296  * (e.g. x=-10 means 10px fromt the right).
297  * - The text color is defined by (color_r,color_g,color_b), each float
298  * numbers in the range [0,1].
299  * \sa renderTextBitmap, textBitmapWidth, mrpt::opengl::gl_utils
300  */
301  static void renderTextBitmap(
302  int screen_x, int screen_y, const std::string& str, float color_r = 1,
303  float color_g = 1, float color_b = 1,
306 
307  /** Evaluates the bounding box of this object (including possible children)
308  * in the coordinate frame of the object parent. */
309  virtual void getBoundingBox(
311 
312  protected:
313  /** Checks glGetError and throws an exception if an error situation is found
314  */
315  static void checkOpenGLError();
316 
319 
320  /** Returns the lowest next free texture name (avoid using OpenGL's own
321  * function since we may call them from different threads and seem it's not
322  * cool). */
323  static unsigned int getNewTextureNumber();
324  static void releaseTextureName(unsigned int i);
325 };
326 /** A list of objects pointers, automatically managing memory free at
327  * destructor, and managing copies correctly. */
328 using CListOpenGLObjects = std::deque<CRenderizable::Ptr>;
329 
330 /** Applies a mrpt::poses::CPose3D transformation to the object. Note that this
331  * method doesn't <i>set</i> the pose to the given value, but <i>combines</i> it
332  * with the existing one.
333  * \sa setPose */
336 
337 } // namespace mrpt::opengl
double getColorA() const
Color components in the range [0,1].
void setName(const std::string &n)
Changes the name of the object.
Definition: CRenderizable.h:65
CRenderizable & setColorB(const double b)
Color components in the range [0,1].
double getPosePitch() const
Rotation relative to parent coordinate origin, in DEGREES.
GLdouble GLdouble z
Definition: glext.h:3879
double RAD2DEG(const double x)
Radians to degrees.
float getScaleX() const
Get the current scaling factor in one axis.
A set of object, which are referenced to the coordinates framework established in this object...
Definition: CSetOfObjects.h:26
virtual CRenderizable & setColorA_u8(const uint8_t a)
Color components in the range [0,255].
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &out, const mrpt::opengl::CLight &o)
Definition: CLight.cpp:123
CRenderizable & setLocation(const mrpt::math::TPoint3D &p)
Changes the location of the object, keeping untouched the orientation.
const double G
std::deque< CRenderizable::Ptr > CListOpenGLObjects
A list of objects pointers, automatically managing memory free at destructor, and managing copies cor...
virtual bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const
Simulation of ray-trace, given a pose.
double getPosePitchRad() const
Rotation relative to parent coordinate origin, in radians.
GLenum GLsizei n
Definition: glext.h:5136
uint8_t getColorR_u8() const
Color components in the range [0,255].
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:40
virtual void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const =0
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
double pitch() const
Get the PITCH angle (in radians)
Definition: CPose3D.h:548
CRenderizable & setLocation(double x, double y, double z)
Changes the location of the object, keeping untouched the orientation.
double yaw() const
Get the YAW angle (in radians)
Definition: CPose3D.h:542
double getPoseX() const
Translation relative to parent coordinate origin.
virtual CRenderizable & setColorR_u8(const uint8_t r)
Color components in the range [0,255].
void enableShowName(bool showName=true)
Enables or disables showing the name of the object as a label when rendering.
Definition: CRenderizable.h:82
STL namespace.
uint8_t B
Definition: TColor.h:46
uint8_t G
Definition: TColor.h:46
double getPoseZ() const
Translation relative to parent coordinate origin.
CRenderizable & setColorA(const double a)
Color components in the range [0,1].
uint8_t getColorA_u8() const
Color components in the range [0,255].
double getPoseRoll() const
Rotation relative to parent coordinate origin, in DEGREES.
GLdouble s
Definition: glext.h:3682
mrpt::poses::CPose3D m_pose
6D pose wrt the parent coordinate reference.
Definition: CRenderizable.h:54
A viewport within a COpenGLScene, containing a set of OpenGL objects to render.
CRenderizable & setColorR(const double r)
Color components in the range [0,1].
const mrpt::img::TColor & getColor_u8() const
Returns the object color property as a TColor.
#define DEFINE_VIRTUAL_SERIALIZABLE(class_name)
This declaration must be inserted in virtual CSerializable classes definition:
unsigned char uint8_t
Definition: rptypes.h:44
CRenderizable & setColor(const mrpt::img::TColorf &c)
Changes the default object color.
float m_scale_x
Scale components to apply to the object (default=1)
Definition: CRenderizable.h:56
virtual CRenderizable & setColorB_u8(const uint8_t b)
Color components in the range [0,255].
virtual CRenderizable & setColor_u8(const mrpt::img::TColor &c)
TOpenGLFont
Existing fonts for 2D texts in mrpt::opengl methods.
Definition: opengl_fonts.h:22
void writeToStreamRender(mrpt::serialization::CArchive &out) const
uint8_t getColorG_u8() const
Color components in the range [0,255].
CRenderizable & setColor_u8(uint8_t R, uint8_t G, uint8_t B, uint8_t A=255)
Set the color components of this object (R,G,B,Alpha, in the range 0-255)
virtual CRenderizable & setColorG_u8(const uint8_t g)
Color components in the range [0,255].
const GLubyte * c
Definition: glext.h:6406
bool isVisible() const
Is the object visible?
Definition: CRenderizable.h:68
CRenderizable & setScale(float sx, float sy, float sz)
Scale to apply to the object in each axis (default=1)
GLubyte g
Definition: glext.h:6372
GLubyte GLubyte b
Definition: glext.h:6372
const std::string & getName() const
Returns the name of the object.
Definition: CRenderizable.h:67
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:143
CRenderizable & setScale(float s)
Scale to apply to the object, in all three axes (default=1)
CRenderizable & setColor(double R, double G, double B, double A=1)
Set the color components of this object (R,G,B,Alpha, in the range 0-1)
GLsizei const GLchar ** string
Definition: glext.h:4116
A class used to store a 2D point.
Definition: CPoint2D.h:32
A class used to store a 3D point.
Definition: CPoint3D.h:31
double roll() const
Get the ROLL angle (in radians)
Definition: CPose3D.h:554
uint8_t R
Definition: TColor.h:46
double getPoseYawRad() const
Rotation relative to parent coordinate origin, in radians.
mrpt::img::TColor m_color
Color components in the range [0,255].
Definition: CRenderizable.h:51
double getPoseY() const
Translation relative to parent coordinate origin.
static void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:53
GLdouble GLdouble GLdouble r
Definition: glext.h:3711
static unsigned int getNewTextureNumber()
Returns the lowest next free texture name (avoid using OpenGL&#39;s own function since we may call them f...
void setVisibility(bool visible=true)
Set object visibility (default=true)
Definition: CRenderizable.h:73
static int textBitmapWidth(const std::string &str, mrpt::opengl::TOpenGLFont font=mrpt::opengl::MRPT_GLUT_BITMAP_TIMES_ROMAN_24)
Return the exact width in pixels for a given string, as will be rendered by renderTextBitmap().
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:39
const float R
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:84
double getPoseYaw() const
Rotation relative to parent coordinate origin, in DEGREES.
mrpt::img::TColorf getColor() const
Returns the object color property as a TColorf.
A RGB color - floats in the range [0,1].
Definition: TColor.h:77
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
Definition: TPose3D.h:23
GLuint in
Definition: glext.h:7391
Lightweight 2D pose.
Definition: TPose2D.h:22
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
CRenderizable & setColorG(const double g)
Color components in the range [0,1].
const auto bb_max
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
CRenderizable & setPose(const mrpt::poses::CPose3D &o)
Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to this)
GLenum GLint GLint y
Definition: glext.h:3542
double getPoseRollRad() const
Rotation relative to parent coordinate origin, in radians.
const mrpt::poses::CPose3D & getPoseRef() const
Returns a const ref to the 3D pose of the object as mrpt::poses::CPose3D (which explicitly contains t...
float getScaleY() const
Get the current scaling factor in one axis.
void readFromStreamRender(mrpt::serialization::CArchive &in)
static void renderTextBitmap(const char *str, void *fontStyle)
This method is safe for calling from within ::render() methods.
const auto bb_min
A RGB color - 8bit.
Definition: TColor.h:20
GLenum GLint x
Definition: glext.h:3542
double getColorB() const
Color components in the range [0,1].
Lightweight 3D point.
Definition: TPoint3D.h:90
uint8_t getColorB_u8() const
Color components in the range [0,255].
bool m_visible
Is the object visible? (default=true)
Definition: CRenderizable.h:58
CRenderizable()
Default constructor:
virtual void render() const =0
Implements the rendering of 3D objects in each class derived from CRenderizable.
double getColorG() const
Color components in the range [0,1].
mrpt::math::TPose3D getPose() const
Returns the 3D pose of the object as TPose3D.
GLubyte GLubyte GLubyte a
Definition: glext.h:6372
GLfloat GLfloat p
Definition: glext.h:6398
float getScaleZ() const
Get the current scaling factor in one axis.
static void releaseTextureName(unsigned int i)
uint8_t A
Definition: TColor.h:46
double getColorR() const
Color components in the range [0,1].



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019