Main MRPT website > C++ reference for MRPT 1.9.9
CMeshFast.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "opengl-precomp.h" // Precompiled header
11 
12 #include <mrpt/poses/CPose3D.h>
13 #include <mrpt/opengl/CMeshFast.h>
15 #include <mrpt/img/color_maps.h>
17 
18 #include "opengl_internals.h"
19 
20 using namespace mrpt;
21 using namespace mrpt::opengl;
22 using namespace mrpt::img;
23 using namespace mrpt::poses;
24 using namespace mrpt::math;
25 using namespace std;
26 
28 
29 void CMeshFast::updatePoints() const
30 {
32 
33  const auto cols = Z.cols();
34  const auto rows = Z.rows();
35 
36  if ((m_colorFromZ) || (m_isImage)) updateColorsMatrix();
37 
38  ASSERT_((cols > 0) && (rows > 0));
39  ASSERT_((xMax > xMin) && (yMax > yMin));
40  X.setSize(rows, cols);
41  Y.setSize(rows, cols);
42  const float sCellX = (xMax - xMin) / (rows - 1);
43  const float sCellY = (yMax - yMin) / (cols - 1);
44 
45  for (int iX = 0; iX < rows; iX++)
46  for (int iY = 0; iY < cols; iY++)
47  {
48  X(iX, iY) = xMin + iX * sCellX;
49  Y(iX, iY) = yMin + iY * sCellY;
50  }
51 
52  pointsUpToDate = true;
53 }
54 
55 /*---------------------------------------------------------------
56  render
57  ---------------------------------------------------------------*/
59 {
60 #if MRPT_HAS_OPENGL_GLUT
61 
62  if (!pointsUpToDate) updatePoints();
63 
64  ASSERT_(X.size() == Y.size());
65  ASSERT_(X.size() == Z.size());
66 
67  if (m_color.A != 255)
68  {
71  }
72 
73  glPointSize(m_pointSize);
74 
75  if (m_pointSmooth)
77  else
79 
80  // Disable lighting for point clouds:
82 
84  for (unsigned int i = 0; i < X.rows(); i++)
85  for (unsigned int j = 0; j < X.cols(); j++)
86  {
87  if (m_isImage && m_textureImage.isColor())
88  glColor4f(C_r(i, j), C_g(i, j), C_b(i, j), m_color.A / 255.f);
89 
90  else if (m_isImage)
91  glColor4f(C(i, j), C(i, j), C(i, j), m_color.A / 255.f);
92 
93  else if (m_colorFromZ)
94  {
95  float rz, gz, bz;
96  colormap(m_colorMap, C(i, j), rz, gz, bz);
97  glColor4f(rz, gz, bz, m_color.A / 255.f);
98  }
99 
100  else
101  glColor4f(
102  m_color.R / 255.f, m_color.G / 255.f, m_color.B / 255.f,
103  m_color.A / 255.f);
104 
105  glVertex3f(X(i, j), Y(i, j), Z(i, j));
106  }
107 
108  glEnd();
109 
111 
112  // Undo flags:
113  if (m_color.A != 255) glDisable(GL_BLEND);
114 
115  if (m_pointSmooth) glDisable(GL_POINT_SMOOTH);
116 
118 #endif
119 }
120 
122 {
123  MRPT_START
124 
125  // Make a copy:
126  m_textureImage = img;
127 
128  // Delete content in Z
129  Z.setSize(img.getHeight(), img.getWidth());
130  Z.assign(0);
131 
132  // Update flags/states
133  m_modified_Image = true;
134  m_enableTransparency = false;
135  m_colorFromZ = false;
136  m_isImage = true;
137  pointsUpToDate = false;
138 
140 
141  MRPT_END
142 }
143 
146 {
147  MRPT_START
148 
149  ASSERT_(
150  (img.getWidth() == static_cast<size_t>(in_Z.cols())) &&
151  (img.getHeight() == static_cast<size_t>(in_Z.rows())));
152 
153  Z = in_Z;
154 
155  // Make a copy:
156  m_textureImage = img;
157 
158  // Update flags/states
159  m_modified_Image = true;
160  m_enableTransparency = false;
161  m_colorFromZ = false;
162  m_isImage = true;
163  pointsUpToDate = false;
164 
166 
167  MRPT_END
168 }
169 
172 {
173  writeToStreamRender(out);
174 
175  out << m_textureImage;
176  out << m_isImage;
177  out << xMin << xMax << yMin << yMax;
178  out << X << Y << Z; // We don't need to serialize C, it's computed
179  out << m_enableTransparency;
180  out << m_colorFromZ;
181  out << int16_t(m_colorMap);
182  out << m_pointSize;
183  out << m_pointSmooth;
184 }
185 
188 {
189  switch (version)
190  {
191  case 0:
192  {
193  readFromStreamRender(in);
194 
195  in >> m_textureImage;
196  in >> m_isImage;
197 
198  in >> xMin;
199  in >> xMax;
200  in >> yMin;
201  in >> yMax;
202 
203  in >> X >> Y >> Z;
204  in >> m_enableTransparency;
205  in >> m_colorFromZ;
206 
207  int16_t i;
208  in >> i;
209  m_colorMap = TColormap(i);
210  in >> m_pointSize;
211  in >> m_pointSmooth;
212  m_modified_Z = true;
213  }
214 
215  pointsUpToDate = false;
216  break;
217 
218  default:
220  };
222 }
223 
225 {
226  if ((!m_modified_Z) && (!m_modified_Image)) return;
227 
229 
230  if (m_isImage)
231  {
232  const int cols = m_textureImage.getWidth();
233  const int rows = m_textureImage.getHeight();
234 
235  if ((cols != Z.cols()) || (rows != Z.rows()))
236  {
237  printf("\nTexture Image and Z sizes have to be equal");
238  }
239  else if (m_textureImage.isColor())
240  {
241  C_r.setSize(rows, cols);
242  C_g.setSize(rows, cols);
243  C_b.setSize(rows, cols);
244  m_textureImage.getAsRGBMatrices(C_r, C_g, C_b);
245  }
246  else
247  {
248  C.setSize(rows, cols);
249  m_textureImage.getAsMatrix(C);
250  }
251  }
252  else
253  {
254  const size_t cols = Z.cols();
255  const size_t rows = Z.rows();
256 
257  C.setSize(rows, cols);
258 
259  // Color is proportional to difference between height of a cell and
260  // the mean of the nearby cells MEANS:
261  C = Z;
262  C.normalize(0.01f, 0.99f);
263  }
264 
265  m_modified_Image = false;
266  m_modified_Z = false; // Done
267  pointsUpToDate = false;
268 }
269 
271 {
272  Z = in_Z;
273  m_modified_Z = true;
274  pointsUpToDate = false;
275 
276  // Delete previously loaded images
277  m_isImage = false;
278 
280 }
281 
283  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
284 {
285  bb_min.x = xMin;
286  bb_min.y = yMin;
287  bb_min.z = Z.minCoeff();
288 
289  bb_max.x = xMax;
290  bb_max.y = yMax;
291  bb_max.z = Z.maxCoeff();
292 
293  // Convert to coordinates of my parent:
294  m_pose.composePoint(bb_min, bb_min);
295  m_pose.composePoint(bb_max, bb_max);
296 }
void updateColorsMatrix() const
Called internally to assure C is updated.
Definition: CMeshFast.cpp:224
void colormap(const TColormap &color_map, const float color_index, float &r, float &g, float &b)
Transform a float number in the range [0,1] into RGB components.
Definition: color_maps.cpp:113
TColormap
Different colormaps for use in mrpt::img::colormap()
Definition: color_maps.h:31
#define MRPT_START
Definition: exceptions.h:262
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: CMeshFast.cpp:186
GLbyte GLbyte bz
Definition: glext.h:6105
GLAPI void GLAPIENTRY glEnable(GLenum cap)
GLAPI void GLAPIENTRY glPointSize(GLfloat size)
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
A planar (XY) grid where each cell has an associated height and, optionally, a texture map...
Definition: CMeshFast.h:41
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CMeshFast.cpp:170
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CMeshFast.cpp:171
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
STL namespace.
void render_dl() const override
Render.
Definition: CMeshFast.cpp:58
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:287
#define GL_LIGHTING
Definition: glew.h:385
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
unsigned char uint8_t
Definition: rptypes.h:41
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:90
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
void assignImageAndZ(const mrpt::img::CImage &img, const mrpt::math::CMatrixTemplateNumeric< float > &in_Z)
Assigns a texture image and Z simultaneously, and disable transparency.
Definition: CMeshFast.cpp:144
This base provides a set of functions for maths stuff.
__int16 int16_t
Definition: rptypes.h:43
GLint GLvoid * img
Definition: glext.h:3763
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const override
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
Definition: CMeshFast.cpp:282
void setZ(const mrpt::math::CMatrixTemplateNumeric< float > &in_Z)
This method sets the matrix of heights for each position (cell) in the mesh grid. ...
Definition: CMeshFast.cpp:270
double x
X,Y,Z coordinates.
#define GL_POINT_SMOOTH
Definition: glew.h:363
GLAPI void GLAPIENTRY glBegin(GLenum mode)
#define GL_BLEND
Definition: glew.h:432
#define GL_POINTS
Definition: glew.h:272
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
#define GL_SRC_ALPHA
Definition: glew.h:286
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:48
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:143
#define MRPT_END
Definition: exceptions.h:266
GLuint in
Definition: glext.h:7274
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
GLAPI void GLAPIENTRY glEnd(void)
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
GLAPI void GLAPIENTRY glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
Lightweight 3D point.
GLAPI void GLAPIENTRY glDisable(GLenum cap)
void assignImage(const mrpt::img::CImage &img)
Assigns a texture image, and disable transparency.
Definition: CMeshFast.cpp:121
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019