MRPT  2.0.4
CRenderizable.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, 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 
10 #include "opengl-precomp.h" // Precompiled header
11 
12 #include <mrpt/math/TPose3D.h>
13 #include <mrpt/math/utils.h>
14 #include <mrpt/opengl/CRenderizable.h> // Include these before windows.h!!
15 #include <mrpt/opengl/CText.h>
16 #include <mrpt/poses/CPoint2D.h>
17 #include <mrpt/poses/CPoint3D.h>
18 #include <mrpt/poses/CPose3D.h>
20 
21 #include <mutex>
22 
23 #include <mrpt/opengl/opengl_api.h>
24 
25 using namespace std;
26 using namespace mrpt;
27 using namespace mrpt::opengl;
28 
30 
31 // Default constructor:
33  : m_name(),
34 
35  m_color(255, 255, 255, 255),
36  m_pose()
37 
38 {
39 }
40 
41 // Destructor:
42 CRenderizable::~CRenderizable() = default;
43 
44 void CRenderizable::writeToStreamRender(
46 {
47  // MRPT 0.9.5 svn 2774 (Dec 14th 2011):
48  // Added support of versioning at this level of serialization too.
49  // Should have been done from the beginning, terrible mistake on my part.
50  // Now, the only solution is something as ugly as this:
51  //
52  // For reference: In the past this started as:
53  // out << m_name << (float)(m_color.R) << (float)(m_color.G) <<
54  // (float)(m_color.B) << (float)(m_color.A);
55  // ...
56 
57  // can't be >31 (but it would be mad geting to that situation!)
58  const uint8_t serialization_version = 1;
59 
60  const bool all_scales_equal =
61  (m_scale_x == m_scale_y && m_scale_z == m_scale_x);
62  const bool all_scales_unity = (all_scales_equal && m_scale_x == 1.0f);
63 
64  const uint8_t magic_signature[2] = {
65  0xFF,
66  // bit7: fixed to 1 to mark this new header format
67  // bit6: whether the 3 scale{x,y,z} are equal to 1.0
68  // bit5: whether the 3 scale{x,y,z} are equal to each other
69  static_cast<uint8_t>(
70  serialization_version |
71  (all_scales_unity ? 0xC0 : (all_scales_equal ? 0xA0 : 0x80)))};
72 
73  out << magic_signature[0] << magic_signature[1];
74 
75  // "m_name"
76  const auto nameLen = static_cast<uint16_t>(m_name.size());
77  out << nameLen;
78  if (nameLen) out.WriteBuffer(m_name.c_str(), m_name.size());
79 
80  // Color, as u8:
81  out << m_color.R << m_color.G << m_color.B << m_color.A;
82 
83  // the rest of fields:
84  out << (float)m_pose.x() << (float)m_pose.y() << (float)m_pose.z()
85  << (float)m_pose.yaw() << (float)m_pose.pitch() << (float)m_pose.roll();
86 
87  if (!all_scales_unity)
88  {
89  if (all_scales_equal)
90  out << m_scale_x;
91  else
92  out << m_scale_x << m_scale_y << m_scale_z;
93  }
94 
95  out << m_show_name << m_visible;
96  out << m_representativePoint; // v1
97 }
98 
99 void CRenderizable::readFromStreamRender(mrpt::serialization::CArchive& in)
100 {
101  // MRPT 0.9.5 svn 2774 (Dec 14th 2011):
102  // See comments in CRenderizable::writeToStreamRender() for the employed
103  // serialization mechanism.
104  //
105  // MRPT 1.9.9 (Aug 2019): Was
106  // union {
107  // uint8_t magic_signature[2 + 2];
108  // (the extra 4 bytes will be used only for the old format)
109  // uint32_t magic_signature_uint32;
110  // So we can interpret the 4bytes above as a 32bit number cleanly.
111  // };
112  // Get rid of the "old" serialization format to avoid using "union".
113 
114  uint8_t magic_signature[2];
115 
116  in >> magic_signature[0] >> magic_signature[1];
117 
118  const bool is_new_format =
119  (magic_signature[0] == 0xFF) && ((magic_signature[1] & 0x80) != 0);
120 
121  if (is_new_format)
122  {
123  // NEW FORMAT:
124  uint8_t serialization_version = (magic_signature[1] & 0x1F);
125  const bool all_scales_unity = ((magic_signature[1] & 0x40) != 0);
126  const bool all_scales_equal_but_not_unity =
127  ((magic_signature[1] & 0x20) != 0);
128 
129  switch (serialization_version)
130  {
131  case 0:
132  case 1:
133  {
134  // "m_name"
135  uint16_t nameLen;
136  in >> nameLen;
137  m_name.resize(nameLen);
138  if (nameLen) in.ReadBuffer((void*)(&m_name[0]), m_name.size());
139 
140  // Color, as u8:
141  in >> m_color.R >> m_color.G >> m_color.B >> m_color.A;
142 
143  // the rest of fields:
144  float x, y, z, yaw, pitch, roll;
145  in >> x >> y >> z >> yaw >> pitch >> roll;
146  m_pose.x(x);
147  m_pose.y(y);
148  m_pose.z(z);
149  m_pose.setYawPitchRoll(yaw, pitch, roll);
150 
151  if (all_scales_unity)
152  m_scale_x = m_scale_y = m_scale_z = 1;
153  else
154  {
155  if (all_scales_equal_but_not_unity)
156  {
157  in >> m_scale_x;
158  m_scale_y = m_scale_z = m_scale_x;
159  }
160  else
161  in >> m_scale_x >> m_scale_y >> m_scale_z;
162  }
163 
164  in >> m_show_name >> m_visible;
165  if (serialization_version >= 1)
166  in >> m_representativePoint;
167  else
168  m_representativePoint = mrpt::math::TPoint3Df(0, 0, 0);
169  }
170  break;
171  default:
173  "Can't parse CRenderizable standard data field: corrupt "
174  "data stream or format in a newer MRPT format? "
175  "(serialization version=%u)",
176  static_cast<unsigned int>(serialization_version));
177  };
178  }
179  else
180  {
181  // OLD FORMAT:
182  THROW_EXCEPTION("Serialized object is too old! Unsupported format.");
183  }
184 }
185 
186 /*--------------------------------------------------------------
187  setPose
188  ---------------------------------------------------------------*/
189 CRenderizable& CRenderizable::setPose(const mrpt::poses::CPose3D& o)
190 {
191  m_pose = o;
192  return *this;
193 }
194 CRenderizable& CRenderizable::setPose(const mrpt::poses::CPose2D& o)
195 {
196  m_pose = mrpt::poses::CPose3D(o);
197  return *this;
198 }
199 CRenderizable& CRenderizable::setPose(const mrpt::math::TPose3D& o)
200 {
201  m_pose = mrpt::poses::CPose3D(o);
202  return *this;
203 }
204 CRenderizable& CRenderizable::setPose(const mrpt::math::TPose2D& o)
205 {
206  m_pose = mrpt::poses::CPose3D(o);
207  return *this;
208 }
209 
210 /** Set the 3D pose from a mrpt::poses::CPose3D object */
211 CRenderizable& CRenderizable::setPose(const mrpt::poses::CPoint3D& o)
212 {
213  m_pose.setFromValues(o.x(), o.y(), o.z(), 0, 0, 0);
214  return *this;
215 }
216 /** Set the 3D pose from a mrpt::poses::CPose3D object */
217 CRenderizable& CRenderizable::setPose(const mrpt::poses::CPoint2D& o)
218 {
219  m_pose.setFromValues(o.x(), o.y(), 0, 0, 0, 0);
220  return *this;
221 }
222 
223 mrpt::math::TPose3D CRenderizable::getPose() const { return m_pose.asTPose(); }
225 {
226  return false;
227 }
228 
229 CRenderizable& CRenderizable::setColor_u8(const mrpt::img::TColor& c)
230 {
231  m_color.R = c.R;
232  m_color.G = c.G;
233  m_color.B = c.B;
234  m_color.A = c.A;
235  return *this;
236 }
237 
238 CText& CRenderizable::labelObject() const
239 {
240  if (!m_label_obj)
241  {
242  m_label_obj = std::make_shared<mrpt::opengl::CText>();
243  m_label_obj->setString(m_name);
244  }
245  return *m_label_obj;
246 }
A 2D text (bitmap rendering): it always "faces the observer" despite it&#39;s at some 3D location...
Definition: CText.h:35
#define IMPLEMENTS_VIRTUAL_SERIALIZABLE(class_name, base_class, NS)
This must be inserted as implementation of some required members for virtual CSerializable classes: ...
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
bool traceRay(const std::vector< TPolygonWithPlane > &vec, const mrpt::math::TPose3D &pose, double &dist)
Fast ray tracing method using polygons&#39; properties.
Definition: geometry.cpp:2484
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:48
STL namespace.
uint8_t B
Definition: TColor.h:51
uint8_t G
Definition: TColor.h:51
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:143
A class used to store a 2D point.
Definition: CPoint2D.h:32
A class used to store a 3D point.
Definition: CPoint3D.h:31
uint8_t R
Definition: TColor.h:51
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:54
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:39
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
mrpt::vision::TStereoCalibResults out
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
Definition: TPose3D.h:24
Lightweight 2D pose.
Definition: TPose2D.h:22
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
size_t ReadBuffer(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer.
Definition: CArchive.cpp:25
TPoint3D_< float > TPoint3Df
Definition: TPoint3D.h:269
A RGB color - 8bit.
Definition: TColor.h:25
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
uint8_t A
Definition: TColor.h:51
void setString(const std::string &s)
Sets the text to display.
Definition: CText.h:47



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020