Main MRPT website > C++ reference for MRPT 1.5.6
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 CMeshFastPtr CMeshFast::Create(bool enableTransparency, float xMin, float xMax , float yMin, float yMax )
30 {
31  return CMeshFastPtr( new CMeshFast( enableTransparency, xMin ,xMax , yMin ,yMax ) );
32 }
35 
36  const size_t cols = Z.getColCount();
37  const size_t rows = Z.getRowCount();
38 
39  if ((m_colorFromZ)||(m_isImage))
40  updateColorsMatrix();
41 
42  ASSERT_((cols>0)&&(rows>0))
43  ASSERT_((xMax>xMin)&&(yMax>yMin))
44 
45  X.setSize(rows,cols);
46  Y.setSize(rows,cols);
47  const float sCellX=(xMax-xMin)/(rows-1);
48  const float sCellY=(yMax-yMin)/(cols-1);
49 
50  for (size_t iX=0;iX<rows;iX++)
51  for (size_t iY=0;iY<cols;iY++)
52  {
53  X(iX,iY) = xMin+iX*sCellX;
54  Y(iX,iY) = yMin+iY*sCellY;
55 
56  }
57 
58  pointsUpToDate = true;
59 }
60 
61 /*---------------------------------------------------------------
62  render
63  ---------------------------------------------------------------*/
64 void CMeshFast::render_dl() const {
65 #if MRPT_HAS_OPENGL_GLUT
66 
67  if (!pointsUpToDate)
68  updatePoints();
69 
70  ASSERT_(X.size() == Y.size());
71  ASSERT_(X.size() == Z.size());
72 
73  if ( m_color.A != 255 )
74  {
77  }
78 
79  glPointSize( m_pointSize );
80 
81  if (m_pointSmooth)
83  else
85 
86  // Disable lighting for point clouds:
88 
89  glBegin( GL_POINTS );
90  for (unsigned int i=0; i<X.getRowCount(); i++)
91  for (unsigned int j=0; j<X.getColCount(); j++)
92  {
93  if ( m_isImage && m_textureImage.isColor())
94  glColor4f(C_r(i,j), C_g(i,j), C_b(i,j), m_color.A/255.f);
95 
96  else if (m_isImage)
97  glColor4f(C(i,j), C(i,j), C(i,j), m_color.A/255.f);
98 
99  else if (m_colorFromZ)
100  {
101  float rz, gz, bz;
102  colormap(m_colorMap, C(i,j), rz, gz, bz);
103  glColor4f(rz, gz, bz, m_color.A/255.f);
104  }
105 
106  else
107  glColor4f(m_color.R/255.f, m_color.G/255.f, m_color.B/255.f, m_color.A/255.f);
108 
109  glVertex3f(X(i,j), Y(i,j), Z(i,j));
110  }
111 
112  glEnd();
113 
115 
116  // Undo flags:
117  if ( m_color.A != 255 )
119 
120  if (m_pointSmooth)
122 
124 #endif
125 }
126 
127 /*---------------------------------------------------------------
128  assignImage
129  ---------------------------------------------------------------*/
131  const CImage& img )
132 {
133  MRPT_START
134 
135  // Make a copy:
136  m_textureImage = img;
137 
138  // Delete content in Z
139  Z.setSize( img.getHeight(), img.getWidth());
140  Z.assign(0);
141 
142  //Update flags/states
143  m_modified_Image = true;
144  m_enableTransparency = false;
145  m_colorFromZ = false;
146  m_isImage = true;
147  pointsUpToDate=false;
148 
149 
151 
152  MRPT_END
153 }
154 
155 /*---------------------------------------------------------------
156  assign Image and Z
157  ---------------------------------------------------------------*/
159 {
160  MRPT_START
161 
162  ASSERT_((img.getWidth() == static_cast<size_t>(in_Z.cols()))&&(img.getHeight() == static_cast<size_t>(in_Z.rows())))
163 
164  Z = in_Z;
165 
166  // Make a copy:
167  m_textureImage = img;
168 
169  //Update flags/states
170  m_modified_Image = true;
171  m_enableTransparency = false;
172  m_colorFromZ = false;
173  m_isImage = true;
174  pointsUpToDate = false;
175 
176 
178 
179  MRPT_END
180 }
181 
182 /*---------------------------------------------------------------
183  Implements the writing to a CStream capability of
184  CSerializable objects
185  ---------------------------------------------------------------*/
187 {
188 
189  if (version)
190  *version = 0;
191  else
192  {
193  writeToStreamRender(out);
194 
195  out << m_textureImage;
196  out << m_isImage;
197  out << xMin << xMax << yMin << yMax;
198  out << X << Y << Z; // We don't need to serialize C, it's computed
199  out << m_enableTransparency;
200  out << m_colorFromZ;
201  out << int16_t(m_colorMap);
202  out << m_pointSize;
203  out << m_pointSmooth;
204  }
205 }
206 
207 /*---------------------------------------------------------------
208  Implements the reading from a CStream capability of
209  CSerializable objects
210  ---------------------------------------------------------------*/
212 {
213  switch(version)
214  {
215  case 0:
216  {
217  readFromStreamRender(in);
218 
219  in >> m_textureImage;
220  in >> m_isImage;
221 
222  in >> xMin;
223  in >> xMax;
224  in >> yMin;
225  in >> yMax;
226 
227  in >> X >> Y >> Z;
228  in >> m_enableTransparency;
229  in >> m_colorFromZ;
230 
231  int16_t i;
232  in >> i;
233  m_colorMap = TColormap(i);
234  in >> m_pointSize;
235  in >> m_pointSmooth;
236  m_modified_Z = true;
237  }
238 
239  pointsUpToDate = false;
240  break;
241 
242  default:
244 
245  };
247 }
248 
249 
251 {
252  if ((!m_modified_Z)&&(!m_modified_Image)) return;
253 
255 
256  if (m_isImage)
257  {
258  const size_t cols = m_textureImage.getWidth();
259  const size_t rows = m_textureImage.getHeight();
260 
261  if ((cols != Z.getColCount())||(rows != Z.getRowCount()))
262  {
263  printf("\nTexture Image and Z sizes have to be equal");
264 
265  }
266  else if (m_textureImage.isColor())
267  {
268  C_r.setSize(rows, cols);
269  C_g.setSize(rows, cols);
270  C_b.setSize(rows, cols);
271  m_textureImage.getAsRGBMatrices(C_r, C_g, C_b);
272  }
273  else
274  {
275  C.setSize(rows, cols);
276  m_textureImage.getAsMatrix(C);
277  }
278  }
279  else
280  {
281  const size_t cols = Z.getColCount();
282  const size_t rows = Z.getRowCount();
283 
284  C.setSize(rows,cols);
285 
286  // Color is proportional to difference between height of a cell and
287  // the mean of the nearby cells MEANS:
288  C = Z;
289  C.normalize(0.01f,0.99f);
290  }
291 
292 
293  m_modified_Image = false;
294  m_modified_Z = false; // Done
295  pointsUpToDate = false;
296 }
297 
299 {
300  Z = in_Z;
301  m_modified_Z = true;
302  pointsUpToDate = false;
303 
304  //Delete previously loaded images
305  m_isImage = false;
306 
308 }
309 
310 
312 {
313  bb_min.x = xMin;
314  bb_min.y = yMin;
315  bb_min.z = Z.minCoeff();
316 
317  bb_max.x = xMax;
318  bb_max.y = yMax;
319  bb_max.z = Z.maxCoeff();
320 
321  // Convert to coordinates of my parent:
322  m_pose.composePoint(bb_min, bb_min);
323  m_pose.composePoint(bb_max, bb_max);
324 }
GLbyte GLbyte bz
Definition: glext.h:5451
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:30
void readFromStream(mrpt::utils::CStream &in, int version)
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
Definition: CMeshFast.cpp:211
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:101
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const
Introduces a pure virtual method responsible for writing to a CStream.
Definition: CMeshFast.cpp:186
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) ...
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:283
double z
X,Y,Z coordinates.
void BASE_IMPEXP 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:101
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:158
#define GL_LIGHTING
Definition: glew.h:381
void updatePoints() const
Definition: CMeshFast.cpp:33
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const MRPT_OVERRIDE
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
Definition: CMeshFast.cpp:311
GLuint in
Definition: glew.h:7146
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
A renderizable object suitable for rendering with OpenGL's display lists.
GLint GLvoid * img
Definition: glew.h:1290
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
__int16 int16_t
Definition: rptypes.h:45
#define MRPT_END
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
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:298
#define GL_POINT_SMOOTH
Definition: glew.h:359
int version
Definition: mrpt_jpeglib.h:898
GLAPI void GLAPIENTRY glBegin(GLenum mode)
#define GL_BLEND
Definition: glew.h:428
#define GL_POINTS
Definition: glew.h:268
void updateColorsMatrix() const
Called internally to assure C is updated.
Definition: CMeshFast.cpp:250
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
#define MRPT_START
#define GL_SRC_ALPHA
Definition: glew.h:282
void assignImage(const mrpt::utils::CImage &img)
Assigns a texture image, and disable transparency.
Definition: CMeshFast.cpp:130
void OPENGL_IMPEXP checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:134
#define ASSERT_(f)
GLAPI void GLAPIENTRY glEnd(void)
void render_dl() const MRPT_OVERRIDE
Render.
Definition: CMeshFast.cpp:64
GLAPI void GLAPIENTRY glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
Lightweight 3D point.
GLAPI void GLAPIENTRY glDisable(GLenum cap)
size_t getWidth() const MRPT_OVERRIDE
Returns the width of the image in pixels.
Definition: CImage.cpp:855
size_t getHeight() const MRPT_OVERRIDE
Returns the height of the image in pixels.
Definition: CImage.cpp:884
GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble f
Definition: glew.h:5092



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018