MRPT  1.9.9
CMeshFast.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-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 
10 #include "opengl-precomp.h" // Precompiled header
11 
12 #include <mrpt/img/color_maps.h>
14 #include <mrpt/opengl/CMeshFast.h>
16 #include <mrpt/poses/CPose3D.h>
18 #include <Eigen/Dense>
19 #include "opengl_internals.h"
20 
21 using namespace mrpt;
22 using namespace mrpt::opengl;
23 using namespace mrpt::img;
24 using namespace mrpt::poses;
25 using namespace mrpt::math;
26 using namespace std;
27 
29 
30 void CMeshFast::updatePoints() const
31 {
33 
34  const auto cols = Z.cols();
35  const auto rows = Z.rows();
36 
37  if ((m_colorFromZ) || (m_isImage)) updateColorsMatrix();
38 
39  ASSERT_((cols > 0) && (rows > 0));
40  ASSERT_((xMax > xMin) && (yMax > yMin));
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 (int iX = 0; iX < rows; iX++)
47  for (int 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 (int i = 0; i < X.rows(); i++)
86  for (int j = 0; j < X.cols(); 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 
123 {
124  MRPT_START
125 
126  // Make a copy:
127  m_textureImage = img;
128 
129  // Delete content in Z
130  Z.setZero(img.getHeight(), img.getWidth());
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 
145  const CImage& img, const mrpt::math::CMatrixDynamic<float>& in_Z)
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 
263  mrpt::math::normalize(C, 0.01f, 0.99f);
264  }
265 
266  m_modified_Image = false;
267  m_modified_Z = false; // Done
268  pointsUpToDate = false;
269 }
270 
272 {
273  Z = in_Z;
274  m_modified_Z = true;
275  pointsUpToDate = false;
276 
277  // Delete previously loaded images
278  m_isImage = false;
279 
281 }
282 
285 {
286  bb_min.x = xMin;
287  bb_min.y = yMin;
288  bb_min.z = Z.minCoeff();
289 
290  bb_max.x = xMax;
291  bb_max.y = yMax;
292  bb_max.z = Z.maxCoeff();
293 
294  // Convert to coordinates of my parent:
295  m_pose.composePoint(bb_min, bb_min);
296  m_pose.composePoint(bb_max, bb_max);
297 }
void updateColorsMatrix() const
Called internally to assure C is updated.
Definition: CMeshFast.cpp:224
void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
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:114
TColormap
Different colormaps for use in mrpt::img::colormap()
Definition: color_maps.h:29
#define MRPT_START
Definition: exceptions.h:241
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:6193
GLAPI void GLAPIENTRY glEnable(GLenum cap)
GLAPI void GLAPIENTRY glPointSize(GLfloat size)
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
A planar (XY) grid where each cell has an associated height and, optionally, a texture map...
Definition: CMeshFast.h:38
This file implements several operations that operate element-wise on individual or pairs of container...
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
void assignImageAndZ(const mrpt::img::CImage &img, const mrpt::math::CMatrixDynamic< float > &in_Z)
Assigns a texture image and Z simultaneously, and disable transparency.
Definition: CMeshFast.cpp:144
STL namespace.
void render_dl() const override
Render.
Definition: CMeshFast.cpp:59
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:288
#define GL_LIGHTING
Definition: glew.h:386
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
unsigned char uint8_t
Definition: rptypes.h:44
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:97
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
This base provides a set of functions for maths stuff.
__int16 int16_t
Definition: rptypes.h:46
void normalize(CONTAINER &c, Scalar valMin, Scalar valMax)
Scales all elements such as the minimum & maximum values are shifted to the given values...
GLint GLvoid * img
Definition: glext.h:3769
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:283
void setZ(const mrpt::math::CMatrixDynamic< float > &in_Z)
This method sets the matrix of heights for each position (cell) in the mesh grid. ...
Definition: CMeshFast.cpp:271
#define GL_POINT_SMOOTH
Definition: glew.h:364
GLAPI void GLAPIENTRY glBegin(GLenum mode)
#define GL_BLEND
Definition: glew.h:433
#define GL_POINTS
Definition: glew.h:273
size_type rows() const
Number of rows in the matrix.
size_type cols() const
Number of columns in the matrix.
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:287
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
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:155
#define MRPT_END
Definition: exceptions.h:245
GLuint in
Definition: glext.h:7391
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
const auto bb_max
GLAPI void GLAPIENTRY glEnd(void)
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
const auto bb_min
GLAPI void GLAPIENTRY glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
Lightweight 3D point.
Definition: TPoint3D.h:90
GLAPI void GLAPIENTRY glDisable(GLenum cap)
This template class provides the basic functionality for a general 2D any-size, resizable container o...
void assignImage(const mrpt::img::CImage &img)
Assigns a texture image, and disable transparency.
Definition: CMeshFast.cpp:122
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:147



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