29 CMesh::
CMesh(
bool enableTransparency,
float xMin,
float xMax,
float yMin,
float yMax) :
31 m_enableTransparency(enableTransparency),
35 Z(0, 0),
mask(0, 0), U(0, 0), V(0, 0), C(0, 0), C_r(0, 0), C_g(0, 0), C_b(0, 0),
38 m_modified_Image(false),
39 xMin(xMin), xMax(xMax), yMin(yMin), yMax(yMax),
40 trianglesUpToDate(false)
53 CMeshPtr
CMesh::Create(
bool enableTransparency,
float xMin,
float xMax ,
float yMin ,
float yMax )
55 return CMeshPtr(
new CMesh( enableTransparency, xMin ,xMax , yMin ,yMax ) );
64 const size_t cols=Z.getColCount();
65 const size_t rows=Z.getRowCount();
68 if (cols == 0 && rows == 0)
75 vertex_normals.assign((1+cols)*(1+rows), std::pair<TPoint3D,size_t>(
TPoint3D(0,0,0),0) );
77 float cR[3], cG[3], cB[3], cA[3];
78 cA[0] = cA[1] = cA[2] = m_color.A / 255.f;
80 if ((m_colorFromZ) || (m_isImage)) {
83 cR[0]=cR[1]=cR[2]=m_color.R/255.f;
84 cG[0]=cG[1]=cG[2]=m_color.G/255.f;
85 cB[0]=cB[1]=cB[2]=m_color.B/255.f;
90 if (
mask.getColCount()!=0&&
mask.getRowCount()!=0) {
94 const float sCellX=(xMax-xMin)/(rows-1);
95 const float sCellY=(yMax-yMin)/(cols-1);
98 for (
size_t iX=0;iX<rows-1;iX++)
for (
size_t iY=0;iY<cols-1;iY++) {
99 if (useMask&&(!
mask(iX,iY)||!
mask(iX+1,iY+1)))
continue;
100 tri.
x[0]=xMin+iX*sCellX;
101 tri.
y[0]=yMin+iY*sCellY;
103 tri.
x[2]=tri.
x[0]+sCellX;
104 tri.
y[2]=tri.
y[0]+sCellY;
105 tri.
z[2]=Z(iX+1,iY+1);
109 tvi.
vind[0] = iX+rows*iY;
110 tvi.
vind[2] = (iX+1)+rows*(iY+1);
118 if (!useMask||
mask(iX+1,iY))
123 for (
int i=0;i<3;i++) tri.
a[i] = cA[i];
126 colormap(m_colorMap,C(iX,iY),tri.
r[0],tri.
g[0],tri.
b[0]);
127 colormap(m_colorMap,C(iX+1,iY),tri.
r[1],tri.
g[1],tri.
b[1]);
128 colormap(m_colorMap,C(iX+1,iY+1),tri.
r[2],tri.
g[2],tri.
b[2]);
130 else if (m_isImage) {
131 if (m_textureImage.isColor())
133 tri.
r[0]=tri.
r[1]=tri.
r[2]=C_r(iX,iY);
134 tri.
g[0]=tri.
g[1]=tri.
g[2]=C_g(iX,iY);
135 tri.
b[0]=tri.
b[1]=tri.
b[2]=C_b(iX,iY);
139 tri.
r[0]=tri.
r[1]=tri.
r[2]=C(iX,iY);
140 tri.
g[0]=tri.
g[1]=tri.
g[2]=C(iX,iY);
141 tri.
b[0]=tri.
b[1]=tri.
b[2]=C(iX,iY);
145 tri.
r[0]=tri.
r[1]=tri.
r[2]=m_color.R/255.f;
146 tri.
g[0]=tri.
g[1]=tri.
g[2]=m_color.G/255.f;
147 tri.
b[0]=tri.
b[1]=tri.
b[2]=m_color.B/255.f;
152 float ax=tri.
x[1]-tri.
x[0];
153 float bx=tri.
x[2]-tri.
x[0];
154 float ay=tri.
y[1]-tri.
y[0];
155 float by=tri.
y[2]-tri.
y[0];
156 float az=tri.
z[1]-tri.
z[0];
157 float bz=tri.
z[2]-tri.
z[0];
161 tvi.
vind[1] = iX+1+rows*iY;
164 actualMesh.push_back( std::pair<CSetOfTriangles::TTriangle,TTriangleVertexIndices>(tri,tvi) );
167 for (
int k=0;k<3;k++) {
168 vertex_normals[ tvi.
vind[k] ].first += this_normal;
169 vertex_normals[ tvi.
vind[k] ].second ++;
178 if (!useMask||
mask(iX,iY+1))
188 colormap(m_colorMap,C(iX,iY),tri.
r[0],tri.
g[0],tri.
b[0]);
189 colormap(m_colorMap,C(iX+1,iY+1),tri.
r[1],tri.
g[1],tri.
b[1]);
190 colormap(m_colorMap,C(iX,iY+1),tri.
r[2],tri.
g[2],tri.
b[2]);
192 else if (m_isImage) {
193 if (m_textureImage.isColor())
195 tri.
r[0]=tri.
r[1]=tri.
r[2]=C_r(iX,iY);
196 tri.
g[0]=tri.
g[1]=tri.
g[2]=C_g(iX,iY);
197 tri.
b[0]=tri.
b[1]=tri.
b[2]=C_b(iX,iY);
201 tri.
r[0]=tri.
r[1]=tri.
r[2]=C(iX,iY);
202 tri.
g[0]=tri.
g[1]=tri.
g[2]=C(iX,iY);
203 tri.
b[0]=tri.
b[1]=tri.
b[2]=C(iX,iY);
207 tri.
r[0]=tri.
r[1]=tri.
r[2]=m_color.R/255.f;
208 tri.
g[0]=tri.
g[1]=tri.
g[2]=m_color.G/255.f;
209 tri.
b[0]=tri.
b[1]=tri.
b[2]=m_color.B/255.f;
214 float ax=tri.
x[1]-tri.
x[0];
215 float bx=tri.
x[2]-tri.
x[0];
216 float ay=tri.
y[1]-tri.
y[0];
217 float by=tri.
y[2]-tri.
y[0];
218 float az=tri.
z[1]-tri.
z[0];
219 float bz=tri.
z[2]-tri.
z[0];
224 tvi.
vind[2] = iX+rows*(iY+1);
227 actualMesh.push_back( std::pair<CSetOfTriangles::TTriangle,TTriangleVertexIndices>(tri,tvi) );
230 for (
int k=0;k<3;k++) {
231 vertex_normals[ tvi.
vind[k] ].first += this_normal;
232 vertex_normals[ tvi.
vind[k] ].second ++;
238 for (
size_t i=0;i<vertex_normals.size();i++)
240 const size_t N = vertex_normals[i].second;
241 if (N>0) vertex_normals[i].first *= 1.0/N;
244 trianglesUpToDate=
true;
245 polygonsUpToDate=
false;
252 #if MRPT_HAS_OPENGL_GLUT 253 if (m_enableTransparency) {
264 if (!trianglesUpToDate) updateTriangles();
266 for (
size_t i=0;i<actualMesh.size();i++) {
274 for (
int i=0;i<3;i++) {
286 if (!m_isWireFrame)
glEnd();
302 m_textureImage =
img;
305 Z.setSize(
img.getHeight(),
img.getWidth());
309 m_modified_Image =
true;
310 m_enableTransparency =
false;
311 m_colorFromZ =
false;
313 trianglesUpToDate=
false;
328 ASSERT_((
img.getWidth() ==
static_cast<size_t>(in_Z.cols()))&&(
img.getHeight() ==
static_cast<size_t>(in_Z.rows())))
333 m_textureImage =
img;
336 m_modified_Image =
true;
337 m_enableTransparency =
false;
338 m_colorFromZ =
false;
340 trianglesUpToDate =
false;
359 writeToStreamRender(out);
362 out << m_textureImage;
363 out << xMin << xMax << yMin << yMax;
364 out << Z << U << V <<
mask;
365 out << m_enableTransparency;
368 out << m_isWireFrame;
384 readFromStreamRender(
in);
386 in >> m_textureImage;
393 in >> Z >> U >> V >>
mask;
394 in >> m_enableTransparency;
404 else m_isWireFrame =
false;
408 trianglesUpToDate=
false;
414 trianglesUpToDate=
false;
421 if ((!m_modified_Z)&&(!m_modified_Image))
return;
427 const size_t cols = m_textureImage.getWidth();
428 const size_t rows = m_textureImage.getHeight();
430 if ((cols != Z.getColCount())||(rows != Z.getRowCount()))
431 printf(
"\nTexture Image and Z sizes have to be equal");
433 else if (m_textureImage.isColor())
435 C_r.setSize(rows, cols);
436 C_g.setSize(rows, cols);
437 C_b.setSize(rows, cols);
438 m_textureImage.getAsRGBMatrices(C_r, C_g, C_b);
442 C.setSize(rows, cols);
443 m_textureImage.getAsMatrix(C);
448 const size_t cols = Z.getColCount();
449 const size_t rows = Z.getRowCount();
450 C.setSize(rows,cols);
457 C.normalize(0.01f,0.99f);
462 float val_max = -std::numeric_limits<float>::max(), val_min = std::numeric_limits<float>::max();
463 bool any_valid =
false;
465 for (
size_t c=0;
c<cols;
c++)
466 for (
size_t r=0;
r<rows;
r++)
470 const float val = C(
r,
c);
477 float minMaxDelta = val_max - val_min;
478 if (minMaxDelta==0) minMaxDelta = 1;
479 const float minMaxDelta_ = 1.0f/minMaxDelta;
480 C.array() = (C.array()-val_min)*minMaxDelta_;
485 m_modified_Image =
false;
486 m_modified_Z =
false;
487 trianglesUpToDate=
false;
494 trianglesUpToDate=
false;
505 trianglesUpToDate=
false;
517 if (!trianglesUpToDate||!polygonsUpToDate) updatePolygons();
524 for (
size_t i=0;i<3;i++) {
533 if (!trianglesUpToDate) updateTriangles();
534 size_t N=actualMesh.size();
537 polygonsUpToDate=
true;
546 bb_min.
z = Z.minCoeff();
550 bb_max.
z = Z.maxCoeff();
553 m_pose.composePoint(bb_min, bb_min);
554 m_pose.composePoint(bb_max, bb_max);
virtual ~CMesh()
Private, virtual destructor: only can be deleted from smart pointers.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
GLAPI void GLAPIENTRY glEnable(GLenum cap)
TColormap
Different colormaps for use in mrpt::utils::colormap()
A class for storing images as grayscale or RGB bitmaps.
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
bool BASE_IMPEXP traceRay(const std::vector< TPolygonWithPlane > &vec, const mrpt::poses::CPose3D &pose, double &dist)
Fast ray tracing method using polygons' properties.
void setZ(const mrpt::math::CMatrixTemplateNumeric< float > &in_Z)
This method sets the matrix of heights for each position (cell) in the mesh grid. ...
GLAPI void GLAPIENTRY glNormal3f(GLfloat nx, GLfloat ny, GLfloat nz)
Slightly heavyweight type to speed-up calculations with polygons in 3D.
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...
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
double z
X,Y,Z coordinates.
#define GL_COLOR_MATERIAL
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.
GLAPI void GLAPIENTRY glShadeModel(GLenum mode)
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
A renderizable object suitable for rendering with OpenGL's display lists.
void assignImage(const mrpt::utils::CImage &img)
Assigns a texture image, and disable transparency.
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const
Introduces a pure virtual method responsible for writing to a CStream.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
This base provides a set of functions for maths stuff.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const MRPT_OVERRIDE
Trace ray.
void assignImageAndZ(const mrpt::utils::CImage &img, const mrpt::math::CMatrixTemplateNumeric< float > &in_Z)
Assigns a texture image and Z simultaneously, and disable transparency.
GLAPI void GLAPIENTRY glBegin(GLenum mode)
void updatePolygons() const
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
The namespace for 3D scene representation and rendering.
void updateColorsMatrix() const
Called internally to assure C is updated.
GLAPI void GLAPIENTRY glEnd(void)
A matrix of dynamic size.
mrpt::math::TPolygonWithPlane createPolygonFromTriangle(const std::pair< CSetOfTriangles::TTriangle, CMesh::TTriangleVertexIndices > &p)
void setUV(const mrpt::math::CMatrixTemplateNumeric< float > &in_U, const mrpt::math::CMatrixTemplateNumeric< float > &in_V)
Sets the (u,v) texture coordinates (in range [0,1]) for each cell.
GLAPI void GLAPIENTRY glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
A planar (XY) grid where each cell has an associated height and, optionally, a texture map...
void setMask(const mrpt::math::CMatrixTemplateNumeric< float > &in_mask)
This method sets the boolean mask of valid heights for each position (cell) in the mesh grid...
GLuint GLenum GLenum transform
GLAPI void GLAPIENTRY glDisable(GLenum cap)
static math::TPolygon3D tmpPoly(3)
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
void updateTriangles() const
Called internally to assure the triangle list is updated.
3D polygon, inheriting from std::vector<TPoint3D>
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...
void render_dl() const MRPT_OVERRIDE
Render.