30 bool enableTransparency,
float xMin,
float xMax,
float yMin,
float yMax)
31 : m_textureImage(0, 0),
32 m_enableTransparency(enableTransparency),
46 m_modified_Image(false),
51 trianglesUpToDate(false)
74 const size_t cols = Z.getColCount();
75 const size_t rows = Z.getRowCount();
78 if (cols == 0 && rows == 0)
return;
81 ASSERT_(xMax > xMin && yMax > yMin)
84 vertex_normals.assign(
85 (1 + cols) * (1 + rows),
86 std::pair<TPoint3D, size_t>(
TPoint3D(0, 0, 0), 0));
88 float cR[3], cG[3], cB[3], cA[3];
89 cA[0] = cA[1] = cA[2] = m_color.A / 255.f;
91 if ((m_colorFromZ) || (m_isImage))
97 cR[0] = cR[1] = cR[2] = m_color.R / 255.f;
98 cG[0] = cG[1] = cG[2] = m_color.G / 255.f;
99 cB[0] = cB[1] = cB[2] = m_color.B / 255.f;
102 bool useMask =
false;
103 if (
mask.getColCount() != 0 &&
mask.getRowCount() != 0)
108 const float sCellX = (xMax - xMin) / (rows - 1);
109 const float sCellY = (yMax - yMin) / (cols - 1);
112 for (
size_t iX = 0; iX < rows - 1; iX++)
113 for (
size_t iY = 0; iY < cols - 1; iY++)
115 if (useMask && (!
mask(iX, iY) || !
mask(iX + 1, iY + 1)))
continue;
116 tri.
x[0] = xMin + iX * sCellX;
117 tri.
y[0] = yMin + iY * sCellY;
118 tri.
z[0] = Z(iX, iY);
119 tri.
x[2] = tri.
x[0] + sCellX;
120 tri.
y[2] = tri.
y[0] + sCellY;
121 tri.
z[2] = Z(iX + 1, iY + 1);
125 tvi.
vind[0] = iX + rows * iY;
126 tvi.
vind[2] = (iX + 1) + rows * (iY + 1);
134 if (!useMask ||
mask(iX + 1, iY))
138 tri.
z[1] = Z(iX + 1, iY);
139 for (
int i = 0; i < 3; i++)
145 m_colorMap, C(iX, iY), tri.
r[0], tri.
g[0], tri.
b[0]);
147 m_colorMap, C(iX + 1, iY), tri.
r[1], tri.
g[1],
150 m_colorMap, C(iX + 1, iY + 1), tri.
r[2], tri.
g[2],
155 if (m_textureImage.isColor())
157 tri.
r[0] = tri.
r[1] = tri.
r[2] = C_r(iX, iY);
158 tri.
g[0] = tri.
g[1] = tri.
g[2] = C_g(iX, iY);
159 tri.
b[0] = tri.
b[1] = tri.
b[2] = C_b(iX, iY);
163 tri.
r[0] = tri.
r[1] = tri.
r[2] = C(iX, iY);
164 tri.
g[0] = tri.
g[1] = tri.
g[2] = C(iX, iY);
165 tri.
b[0] = tri.
b[1] = tri.
b[2] = C(iX, iY);
170 tri.
r[0] = tri.
r[1] = tri.
r[2] = m_color.R / 255.f;
171 tri.
g[0] = tri.
g[1] = tri.
g[2] = m_color.G / 255.f;
172 tri.
b[0] = tri.
b[1] = tri.
b[2] = m_color.B / 255.f;
178 float ax = tri.
x[1] - tri.
x[0];
179 float bx = tri.
x[2] - tri.
x[0];
180 float ay = tri.
y[1] - tri.
y[0];
181 float by = tri.
y[2] - tri.
y[0];
182 float az = tri.
z[1] - tri.
z[0];
183 float bz = tri.
z[2] - tri.
z[0];
185 ay *
bz - az *
by, az * bx - ax *
bz, ax *
by - ay * bx);
188 tvi.
vind[1] = iX + 1 + rows * iY;
191 actualMesh.push_back(
196 for (
int k = 0; k < 3; k++)
198 vertex_normals[tvi.
vind[k]].first += this_normal;
199 vertex_normals[tvi.
vind[k]].second++;
208 if (!useMask ||
mask(iX, iY + 1))
216 tri.
z[2] = Z(iX, iY + 1);
220 m_colorMap, C(iX, iY), tri.
r[0], tri.
g[0], tri.
b[0]);
222 m_colorMap, C(iX + 1, iY + 1), tri.
r[1], tri.
g[1],
225 m_colorMap, C(iX, iY + 1), tri.
r[2], tri.
g[2],
230 if (m_textureImage.isColor())
232 tri.
r[0] = tri.
r[1] = tri.
r[2] = C_r(iX, iY);
233 tri.
g[0] = tri.
g[1] = tri.
g[2] = C_g(iX, iY);
234 tri.
b[0] = tri.
b[1] = tri.
b[2] = C_b(iX, iY);
238 tri.
r[0] = tri.
r[1] = tri.
r[2] = C(iX, iY);
239 tri.
g[0] = tri.
g[1] = tri.
g[2] = C(iX, iY);
240 tri.
b[0] = tri.
b[1] = tri.
b[2] = C(iX, iY);
245 tri.
r[0] = tri.
r[1] = tri.
r[2] = m_color.R / 255.f;
246 tri.
g[0] = tri.
g[1] = tri.
g[2] = m_color.G / 255.f;
247 tri.
b[0] = tri.
b[1] = tri.
b[2] = m_color.B / 255.f;
253 float ax = tri.
x[1] - tri.
x[0];
254 float bx = tri.
x[2] - tri.
x[0];
255 float ay = tri.
y[1] - tri.
y[0];
256 float by = tri.
y[2] - tri.
y[0];
257 float az = tri.
z[1] - tri.
z[0];
258 float bz = tri.
z[2] - tri.
z[0];
260 ay *
bz - az *
by, az * bx - ax *
bz, ax *
by - ay * bx);
264 tvi.
vind[2] = iX + rows * (iY + 1);
267 actualMesh.push_back(
272 for (
int k = 0; k < 3; k++)
274 vertex_normals[tvi.
vind[k]].first += this_normal;
275 vertex_normals[tvi.
vind[k]].second++;
281 for (
size_t i = 0; i < vertex_normals.size(); i++)
283 const size_t N = vertex_normals[i].second;
284 if (N > 0) vertex_normals[i].first *= 1.0 / N;
287 trianglesUpToDate =
true;
288 polygonsUpToDate =
false;
296 #if MRPT_HAS_OPENGL_GLUT 297 if (m_enableTransparency)
312 if (!trianglesUpToDate) updateTriangles();
314 for (
size_t i = 0; i < actualMesh.size(); i++)
324 for (
int i = 0; i < 3; i++)
337 if (!m_isWireFrame)
glEnd();
352 m_textureImage =
img;
355 Z.setSize(
img.getHeight(),
img.getWidth());
358 m_modified_Image =
true;
359 m_enableTransparency =
false;
360 m_colorFromZ =
false;
362 trianglesUpToDate =
false;
378 (
img.getWidth() ==
static_cast<size_t>(in_Z.cols())) &&
379 (
img.getHeight() ==
static_cast<size_t>(in_Z.rows())))
384 m_textureImage =
img;
386 m_modified_Image =
true;
387 m_enableTransparency =
false;
388 m_colorFromZ =
false;
390 trianglesUpToDate =
false;
407 writeToStreamRender(out);
410 out << m_textureImage;
411 out << xMin << xMax << yMin << yMax;
414 out << m_enableTransparency;
417 out << m_isWireFrame;
433 readFromStreamRender(
in);
435 in >> m_textureImage;
442 in >> Z >> U >> V >>
mask;
443 in >> m_enableTransparency;
454 m_isWireFrame =
false;
458 trianglesUpToDate =
false;
463 trianglesUpToDate =
false;
469 if ((!m_modified_Z) && (!m_modified_Image))
return;
475 const size_t cols = m_textureImage.getWidth();
476 const size_t rows = m_textureImage.getHeight();
478 if ((cols != Z.getColCount()) || (rows != Z.getRowCount()))
479 printf(
"\nTexture Image and Z sizes have to be equal");
481 else if (m_textureImage.isColor())
483 C_r.setSize(rows, cols);
484 C_g.setSize(rows, cols);
485 C_b.setSize(rows, cols);
486 m_textureImage.getAsRGBMatrices(C_r, C_g, C_b);
490 C.setSize(rows, cols);
491 m_textureImage.getAsMatrix(C);
496 const size_t cols = Z.getColCount();
497 const size_t rows = Z.getRowCount();
498 C.setSize(rows, cols);
504 if (
mask.empty()) C.normalize(0.01f, 0.99f);
509 float val_max = -std::numeric_limits<float>::max(),
510 val_min = std::numeric_limits<float>::max();
511 bool any_valid =
false;
513 for (
size_t c = 0;
c < cols;
c++)
514 for (
size_t r = 0;
r < rows;
r++)
516 if (!
mask(
r,
c))
continue;
518 const float val = C(
r,
c);
525 float minMaxDelta = val_max - val_min;
526 if (minMaxDelta == 0) minMaxDelta = 1;
527 const float minMaxDelta_ = 1.0f / minMaxDelta;
528 C.array() = (C.array() - val_min) * minMaxDelta_;
533 m_modified_Image =
false;
534 m_modified_Z =
false;
535 trianglesUpToDate =
false;
542 trianglesUpToDate =
false;
553 trianglesUpToDate =
false;
568 if (!trianglesUpToDate || !polygonsUpToDate) updatePolygons();
574 const std::pair<CSetOfTriangles::TTriangle, CMesh::TTriangleVertexIndices>&
578 for (
size_t i = 0; i < 3; i++)
589 if (!trianglesUpToDate) updateTriangles();
590 size_t N = actualMesh.size();
593 actualMesh.begin(), actualMesh.end(), tmpPolys.begin(),
595 polygonsUpToDate =
true;
604 bb_min.
z = Z.minCoeff();
608 bb_max.
z = Z.maxCoeff();
611 m_pose.composePoint(bb_min, bb_min);
612 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 traceRay(const std::vector< TPolygonWithPlane > &vec, const mrpt::poses::CPose3D &pose, double &dist)
Fast ray tracing method using polygons' properties.
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
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 render_dl() const override
Render.
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
#define GL_COLOR_MATERIAL
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.
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.
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 override
Trace ray.
double x
X,Y,Z coordinates.
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
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...
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)
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...
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>