Skip to content

Commit 9306bb4

Browse files
committed
New class "CMeshFast" added to opengl module. It draws a pseudo-mesh using points instead of lines and triangles to reduce the rendering computational effort.
1 parent c70e62e commit 9306bb4

6 files changed

Lines changed: 588 additions & 0 deletions

File tree

108 KB
Loading

doc/doxygen-pages/lib_mrpt_opengl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Below follows a table with the preview of most rendering primitive classes:
6969
<tr> <td> mrpt::opengl::CGridPlaneXY </td> <td> \image html preview_CGridPlaneXY.png </td> </tr>
7070
<tr> <td> mrpt::opengl::CGridPlaneXZ </td> <td> \image html preview_CGridPlaneXZ.png </td> </tr>
7171
<tr> <td> mrpt::opengl::CMesh </td> <td> \image html preview_CMesh.png </td> </tr>
72+
<tr> <td> mrpt::opengl::CMeshFast </td> <td> \image html preview_CMeshFast.png </td> </tr>
7273
<tr> <td> mrpt::opengl::CPointCloud </td> <td> \image html preview_CPointCloud.png </td> </tr>
7374
<tr> <td> mrpt::opengl::CPointCloudColoured </td> <td> \image html preview_CPointCloudColoured.png </td> </tr>
7475
<tr> <td> mrpt::opengl::CPolyhedron </td> <td> \image html preview_CPolyhedron.png </td> </tr>

libs/opengl/include/mrpt/opengl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <mrpt/opengl/CGridPlaneXY.h>
5858
#include <mrpt/opengl/CGridPlaneXZ.h>
5959
#include <mrpt/opengl/CMesh.h>
60+
#include <mrpt/opengl/CMeshFast.h>
6061
#include <mrpt/opengl/CPointCloud.h>
6162
#include <mrpt/opengl/CPointCloudColoured.h>
6263
#include <mrpt/opengl/CSetOfLines.h>
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/* +---------------------------------------------------------------------------+
2+
| The Mobile Robot Programming Toolkit (MRPT) |
3+
| |
4+
| http://www.mrpt.org/ |
5+
| |
6+
| Copyright (c) 2005-2013, Individual contributors, see AUTHORS file |
7+
| Copyright (c) 2005-2013, MAPIR group, University of Malaga |
8+
| Copyright (c) 2012-2013, University of Almeria |
9+
| All rights reserved. |
10+
| |
11+
| Redistribution and use in source and binary forms, with or without |
12+
| modification, are permitted provided that the following conditions are |
13+
| met: |
14+
| * Redistributions of source code must retain the above copyright |
15+
| notice, this list of conditions and the following disclaimer. |
16+
| * Redistributions in binary form must reproduce the above copyright |
17+
| notice, this list of conditions and the following disclaimer in the |
18+
| documentation and/or other materials provided with the distribution. |
19+
| * Neither the name of the copyright holders nor the |
20+
| names of its contributors may be used to endorse or promote products |
21+
| derived from this software without specific prior written permission.|
22+
| |
23+
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24+
| 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
25+
| TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR|
26+
| PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE |
27+
| FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL|
28+
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR|
29+
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
30+
| HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
31+
| STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
32+
| ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
33+
| POSSIBILITY OF SUCH DAMAGE. |
34+
+---------------------------------------------------------------------------+ */
35+
36+
#ifndef opengl_CMeshFast_H
37+
#define opengl_CMeshFast_H
38+
39+
#include <mrpt/opengl/CRenderizableDisplayList.h>
40+
#include <mrpt/math/CMatrix.h>
41+
#include <mrpt/utils/CImage.h>
42+
#include <mrpt/utils/color_maps.h>
43+
44+
namespace mrpt
45+
{
46+
namespace opengl
47+
{
48+
class OPENGL_IMPEXP CMeshFast;
49+
50+
// This must be added to any CSerializable derived class:
51+
DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CMeshFast, CRenderizableDisplayList, OPENGL_IMPEXP )
52+
53+
/** A planar (XY) grid where each cell has an associated height and, optionally, a texture map.
54+
* To make it faster to render, instead of drawing lines and triangles it draws a point at each
55+
* gridcell.
56+
* A typical usage example would be an elevation map or a 3D model of a terrain.
57+
* \sa opengl::COpenGLScene
58+
*
59+
* <div align="center">
60+
* <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px; border-style: solid;">
61+
* <tr> <td> mrpt::opengl::CMesh </td> <td> \image html preview_CMeshFast.png </td> </tr>
62+
* </table>
63+
* </div>
64+
*
65+
* \ingroup mrpt_opengl_grp
66+
*/
67+
class OPENGL_IMPEXP CMeshFast : public CRenderizableDisplayList
68+
{
69+
DEFINE_SERIALIZABLE( CMeshFast )
70+
71+
protected:
72+
mrpt::utils::CImage m_textureImage;
73+
74+
bool m_enableTransparency;
75+
bool m_colorFromZ;
76+
bool m_isImage;
77+
78+
mutable math::CMatrix X; //!< X(x,y): X-coordinate of the point (x,y)
79+
mutable math::CMatrix Y; //!< Y(x,y): Y-coordinate of the point (x,y)
80+
mutable math::CMatrix Z; //!< Z(x,y): Z-coordinate of the point (x,y)
81+
82+
mutable math::CMatrix C; //!< Grayscale Color [0,1] for each cell, updated by updateColorsMatrix
83+
mutable math::CMatrix C_r; //!< Red Component of the Color [0,1] for each cell, updated by updateColorsMatrix
84+
mutable math::CMatrix C_g; //!< Green Component of the Color [0,1] for each cell, updated by updateColorsMatrix
85+
mutable math::CMatrix C_b; //!< Blue Component of the Color [0,1] for each cell, updated by updateColorsMatrix
86+
87+
mrpt::utils::TColormap m_colorMap; //!< Used when m_colorFromZ is true
88+
float m_pointSize; //!< By default is 1.0
89+
bool m_pointSmooth; //!< Default: false
90+
91+
mutable bool m_modified_Z; //!< Whether C is not up-to-date wrt to Z
92+
mutable bool m_modified_Image; //!< Whether C is not up-to-date wrt to the texture image
93+
94+
void updateColorsMatrix() const; //!< Called internally to assure C is updated.
95+
void updatePoints() const;
96+
97+
float xMin,xMax,yMin,yMax; //!< Mesh bounds
98+
99+
mutable bool pointsUpToDate; //!<Whether the coordinates of the points needs to be recalculated
100+
101+
public:
102+
103+
inline void setPointSize(float p) { m_pointSize=p; } //!< By default is 1.0
104+
inline float getPointSize() const { return m_pointSize; }
105+
106+
inline void enablePointSmooth(bool enable=true) { m_pointSmooth=enable; }
107+
inline void disablePointSmooth() { m_pointSmooth=false; }
108+
109+
void setGridLimits(float xmin,float xmax, float ymin, float ymax)
110+
{
111+
xMin=xmin; xMax = xmax;
112+
yMin=ymin; yMax = ymax;
113+
CRenderizableDisplayList::notifyChange();
114+
}
115+
116+
void getGridLimits(float &xmin,float &xmax, float &ymin, float &ymax) const
117+
{
118+
xmin=xMin; xmax=xMax;
119+
ymin=yMin; ymax=yMax;
120+
}
121+
122+
void enableTransparency( bool v ) { m_enableTransparency = v; CRenderizableDisplayList::notifyChange(); }
123+
void enableColorFromZ( bool v, mrpt::utils::TColormap colorMap = mrpt::utils::cmJET )
124+
{
125+
m_colorFromZ = v;
126+
m_colorMap = colorMap;
127+
CRenderizableDisplayList::notifyChange();
128+
}
129+
130+
/** This method sets the matrix of heights for each position (cell) in the mesh grid */
131+
void setZ( const mrpt::math::CMatrixTemplateNumeric<float> &in_Z );
132+
133+
/** Returns a reference to the internal Z matrix, allowing changing it efficiently */
134+
inline void getZ(mrpt::math::CMatrixFloat &out) const { out=Z; }
135+
136+
inline float getXMin() const { return xMin; }
137+
inline float getXMax() const { return xMax; }
138+
inline float getYMin() const { return yMin; }
139+
inline float getYMax() const { return yMax; }
140+
inline void setXMin(const float &nxm) {
141+
xMin=nxm;
142+
pointsUpToDate=false; CRenderizableDisplayList::notifyChange();
143+
}
144+
inline void setXMax(const float &nxm) {
145+
xMax=nxm;
146+
pointsUpToDate=false; CRenderizableDisplayList::notifyChange();
147+
}
148+
inline void setYMin(const float &nym) {
149+
yMin=nym;
150+
pointsUpToDate=false; CRenderizableDisplayList::notifyChange();
151+
}
152+
inline void setYMax(const float &nym) {
153+
yMax=nym;
154+
pointsUpToDate=false; CRenderizableDisplayList::notifyChange();
155+
}
156+
inline void getXBounds(float &min,float &max) const {
157+
min=xMin;
158+
max=xMax;
159+
}
160+
inline void getYBounds(float &min,float &max) const {
161+
min=yMin;
162+
max=yMax;
163+
}
164+
inline void setXBounds(const float &min,const float &max) {
165+
xMin=min;
166+
xMax=max;
167+
pointsUpToDate=false; CRenderizableDisplayList::notifyChange();
168+
}
169+
inline void setYBounds(const float &min,const float &max) {
170+
yMin=min;
171+
yMax=max;
172+
pointsUpToDate=false; CRenderizableDisplayList::notifyChange();
173+
}
174+
175+
176+
/** Class factory */
177+
static CMeshFastPtr Create(bool enableTransparency, float xMin = -1.0f, float xMax = 1.0f, float yMin = -1.0f, float yMax = 1.0f )
178+
{
179+
return CMeshFastPtr( new CMeshFast( enableTransparency, xMin ,xMax , yMin ,yMax ) );
180+
}
181+
182+
/** Render
183+
*/
184+
void render_dl() const;
185+
186+
/** Evaluates the bounding box of this object (including possible children) in the coordinate frame of the object parent. */
187+
virtual void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const;
188+
189+
/** Assigns a texture image, and disable transparency.
190+
*/
191+
void assignImage(const mrpt::utils::CImage& img );
192+
193+
/** Assigns a texture image and Z simultaneously, and disable transparency.
194+
*/
195+
void assignImageAndZ( const mrpt::utils::CImage& img, const mrpt::math::CMatrixTemplateNumeric<float> &in_Z);
196+
197+
/** Adjust grid limits according to the image aspect ratio, maintaining the X limits and resizing in the Y direction.
198+
*/
199+
inline void adjustGridToImageAR() {
200+
ASSERT_(m_isImage);
201+
const float ycenter = 0.5*(yMin+yMax);
202+
const float xwidth = xMax - xMin;
203+
const float newratio = float(m_textureImage.getWidth())/float(m_textureImage.getHeight());
204+
yMax = ycenter + 0.5*newratio*xwidth;
205+
yMin = ycenter - 0.5*newratio*xwidth;
206+
CRenderizableDisplayList::notifyChange();
207+
}
208+
209+
210+
private:
211+
/** Constructor
212+
*/
213+
CMeshFast( bool enableTransparency = false, float xMin = -1.0f, float xMax = 1.0f, float yMin = -1.0f, float yMax = 1.0f ) :
214+
m_textureImage(0,0),
215+
m_enableTransparency(enableTransparency),
216+
m_colorFromZ(false),
217+
m_isImage(false),
218+
X(0,0), Y(0,0), Z(0,0), C(0,0), C_r(0,0), C_g(0,0), C_b(0,0),
219+
m_colorMap( mrpt::utils::cmJET ),
220+
m_modified_Z(true),
221+
m_modified_Image(false),
222+
xMin(xMin), xMax(xMax), yMin(yMin), yMax(yMax),
223+
pointsUpToDate(false)
224+
{
225+
m_color.A = 255;
226+
m_color.R = 0;
227+
m_color.G = 0;
228+
m_color.B = 150;
229+
}
230+
/** Private, virtual destructor: only can be deleted from smart pointers */
231+
virtual ~CMeshFast() { }
232+
233+
};
234+
235+
} // end namespace
236+
237+
} // End of namespace
238+
239+
#endif

0 commit comments

Comments
 (0)