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-2017, 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/utils/color_maps.h>
16 #include <mrpt/utils/CStream.h>
17 
18 #include "opengl_internals.h"
19 
20 using namespace mrpt;
21 using namespace mrpt::opengl;
22 using namespace mrpt::utils;
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 size_t cols = Z.getColCount();
34  const size_t rows = Z.getRowCount();
35 
36  if ((m_colorFromZ) || (m_isImage)) updateColorsMatrix();
37 
38  ASSERT_((cols > 0) && (rows > 0))
39  ASSERT_((xMax > xMin) && (yMax > yMin))
40 
41  X.setSize(rows, cols);
42  Y.setSize(rows, cols);
43  const float sCellX = (xMax - xMin) / (rows - 1);
44  const float sCellY = (yMax - yMin) / (cols - 1);
45 
46  for (size_t iX = 0; iX < rows; iX++)
47  for (size_t iY = 0; iY < cols; iY++)
48  {
49  X(iX, iY) = xMin + iX * sCellX;
50  Y(iX, iY) = yMin + iY * sCellY;
51  }
52 
53  pointsUpToDate = true;
54 }
55 
56 /*---------------------------------------------------------------
57  render
58  ---------------------------------------------------------------*/
60 {
61 #if MRPT_HAS_OPENGL_GLUT
62 
63  if (!pointsUpToDate) updatePoints();
64 
65  ASSERT_(X.size() == Y.size());
66  ASSERT_(X.size() == Z.size());
67 
68  if (m_color.A != 255)
69  {
72  }
73 
74  glPointSize(m_pointSize);
75 
76  if (m_pointSmooth)
78  else
80 
81  // Disable lighting for point clouds:
83 
85  for (unsigned int i = 0; i < X.getRowCount(); i++)
86  for (unsigned int j = 0; j < X.getColCount(); j++)
87  {
88  if (m_isImage && m_textureImage.isColor())
89  glColor4f(C_r(i, j), C_g(i, j), C_b(i, j), m_color.A / 255.f);
90 
91  else if (m_isImage)
92  glColor4f(C(i, j), C(i, j), C(i, j), m_color.A / 255.f);
93 
94  else if (m_colorFromZ)
95  {
96  float rz, gz, bz;
97  colormap(m_colorMap, C(i, j), rz, gz, bz);
98  glColor4f(rz, gz, bz, m_color.A / 255.f);
99  }
100 
101  else
102  glColor4f(
103  m_color.R / 255.f, m_color.G / 255.f, m_color.B / 255.f,
104  m_color.A / 255.f);
105 
106  glVertex3f(X(i, j), Y(i, j), Z(i, j));
107  }
108 
109  glEnd();
110 
112 
113  // Undo flags:
114  if (m_color.A != 255) glDisable(GL_BLEND);
115 
116  if (m_pointSmooth) glDisable(GL_POINT_SMOOTH);
117 
119 #endif
120 }
121 
122 /*---------------------------------------------------------------
123  assignImage
124  ---------------------------------------------------------------*/
126 {
127  MRPT_START
128 
129  // Make a copy:
130  m_textureImage = img;
131 
132  // Delete content in Z
133  Z.setSize(img.getHeight(), img.getWidth());
134  Z.assign(0);
135 
136  // Update flags/states
137  m_modified_Image = true;
138  m_enableTransparency = false;
139  m_colorFromZ = false;
140  m_isImage = true;
141  pointsUpToDate = false;
142 
144 
145  MRPT_END
146 }
147 
148 /*---------------------------------------------------------------
149  assign Image and Z
150  ---------------------------------------------------------------*/
153 {
154  MRPT_START
155 
156  ASSERT_(
157  (img.getWidth() == static_cast<size_t>(in_Z.cols())) &&
158  (img.getHeight() == static_cast<size_t>(in_Z.rows())))
159 
160  Z = in_Z;
161 
162  // Make a copy:
163  m_textureImage = img;
164 
165  // Update flags/states
166  m_modified_Image = true;
167  m_enableTransparency = false;
168  m_colorFromZ = false;
169  m_isImage = true;
170  pointsUpToDate = false;
171 
173 
174  MRPT_END
175 }
176 
177 /*---------------------------------------------------------------
178  Implements the writing to a CStream capability of
179  CSerializable objects
180  ---------------------------------------------------------------*/
181 void CMeshFast::writeToStream(mrpt::utils::CStream& out, int* version) const
182 {
183  if (version)
184  *version = 0;
185  else
186  {
187  writeToStreamRender(out);
188 
189  out << m_textureImage;
190  out << m_isImage;
191  out << xMin << xMax << yMin << yMax;
192  out << X << Y << Z; // We don't need to serialize C, it's computed
193  out << m_enableTransparency;
194  out << m_colorFromZ;
195  out << int16_t(m_colorMap);
196  out << m_pointSize;
197  out << m_pointSmooth;
198  }
199 }
200 
201 /*---------------------------------------------------------------
202  Implements the reading from a CStream capability of
203  CSerializable objects
204  ---------------------------------------------------------------*/
206 {
207  switch (version)
208  {
209  case 0:
210  {
211  readFromStreamRender(in);
212 
213  in >> m_textureImage;
214  in >> m_isImage;
215 
216  in >> xMin;
217  in >> xMax;
218  in >> yMin;
219  in >> yMax;
220 
221  in >> X >> Y >> Z;
222  in >> m_enableTransparency;
223  in >> m_colorFromZ;
224 
225  int16_t i;
226  in >> i;
227  m_colorMap = TColormap(i);
228  in >> m_pointSize;
229  in >> m_pointSmooth;
230  m_modified_Z = true;
231  }
232 
233  pointsUpToDate = false;
234  break;
235 
236  default:
238  };
240 }
241 
243 {
244  if ((!m_modified_Z) && (!m_modified_Image)) return;
245 
247 
248  if (m_isImage)
249  {
250  const size_t cols = m_textureImage.getWidth();
251  const size_t rows = m_textureImage.getHeight();
252 
253  if ((cols != Z.getColCount()) || (rows != Z.getRowCount()))
254  {
255  printf("\nTexture Image and Z sizes have to be equal");
256  }
257  else if (m_textureImage.isColor())
258  {
259  C_r.setSize(rows, cols);
260  C_g.setSize(rows, cols);
261  C_b.setSize(rows, cols);
262  m_textureImage.getAsRGBMatrices(C_r, C_g, C_b);
263  }
264  else
265  {
266  C.setSize(rows, cols);
267  m_textureImage.getAsMatrix(C);
268  }
269  }
270  else
271  {
272  const size_t cols = Z.getColCount();
273  const size_t rows = Z.getRowCount();
274 
275  C.setSize(rows, cols);
276 
277  // Color is proportional to difference between height of a cell and
278  // the mean of the nearby cells MEANS:
279  C = Z;
280  C.normalize(0.01f, 0.99f);
281  }
282 
283  m_modified_Image = false;
284  m_modified_Z = false; // Done
285  pointsUpToDate = false;
286 }
287 
289 {
290  Z = in_Z;
291  m_modified_Z = true;
292  pointsUpToDate = false;
293 
294  // Delete previously loaded images
295  m_isImage = false;
296 
298 }
299 
301  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
302 {
303  bb_min.x = xMin;
304  bb_min.y = yMin;
305  bb_min.z = Z.minCoeff();
306 
307  bb_max.x = xMax;
308  bb_max.y = yMax;
309  bb_max.z = Z.maxCoeff();
310 
311  // Convert to coordinates of my parent:
312  m_pose.composePoint(bb_min, bb_min);
313  m_pose.composePoint(bb_max, bb_max);
314 }
void updateColorsMatrix() const
Called internally to assure C is updated.
Definition: CMeshFast.cpp:242
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
GLbyte GLbyte bz
Definition: glext.h:6105
GLAPI void GLAPIENTRY glEnable(GLenum cap)
GLAPI void GLAPIENTRY glPointSize(GLfloat size)
TColormap
Different colormaps for use in mrpt::utils::colormap()
Definition: color_maps.h:31
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:118
#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
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:59
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:287
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:115
void assignImageAndZ(const mrpt::utils::CImage &img, const mrpt::math::CMatrixTemplateNumeric< float > &in_Z)
Assigns a texture image and Z simultaneously, and disable transparency.
Definition: CMeshFast.cpp:151
#define GL_LIGHTING
Definition: glew.h:385
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
Definition: CMeshFast.cpp:205
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
__int16 int16_t
Definition: rptypes.h:43
#define MRPT_END
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
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:300
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:288
double x
X,Y,Z coordinates.
#define GL_POINT_SMOOTH
Definition: glew.h:363
GLAPI void GLAPIENTRY glBegin(GLenum mode)
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
Definition: CMeshFast.cpp:181
#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...
Definition: CPoint.h:17
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
#define MRPT_START
#define GL_SRC_ALPHA
Definition: glew.h:286
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void assignImage(const mrpt::utils::CImage &img)
Assigns a texture image, and disable transparency.
Definition: CMeshFast.cpp:125
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:140
GLuint in
Definition: glext.h:7274
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
#define ASSERT_(f)
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)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019