Main MRPT website > C++ reference for MRPT 1.9.9
CFrustum.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-2018, 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 #include <mrpt/opengl/CFrustum.h>
13 #include <mrpt/opengl/gl_utils.h>
14 
15 #include "opengl_internals.h"
16 
17 using namespace mrpt;
18 using namespace mrpt::opengl;
19 
20 using namespace mrpt::math;
21 using namespace std;
22 
24 
25 /*---------------------------------------------------------------
26  render
27  ---------------------------------------------------------------*/
28 void CFrustum::render_dl() const
29 {
30 #if MRPT_HAS_OPENGL_GLUT
31  if (m_color.A != 255 || (m_draw_planes && m_planes_color.A != 255))
32  {
35  }
36  else
37  {
40  }
41 
42  // Compute the 8 corners of the frustum:
43  TPoint3Df pts[8];
44  for (int j = 0; j < 2; j++)
45  {
46  const float r = j == 0 ? m_min_distance : m_max_distance;
47  for (int i = 0; i < 4; i++) pts[4 * j + i].x = r;
48  pts[4 * j + 0].y = -r * tan(m_fov_horz_left);
49  pts[4 * j + 1].y = -r * tan(m_fov_horz_left);
50  pts[4 * j + 2].y = r * tan(m_fov_horz_right);
51  pts[4 * j + 3].y = r * tan(m_fov_horz_right);
52  pts[4 * j + 0].z = -r * tan(m_fov_vert_down);
53  pts[4 * j + 1].z = r * tan(m_fov_vert_up);
54  pts[4 * j + 2].z = -r * tan(m_fov_vert_down);
55  pts[4 * j + 3].z = r * tan(m_fov_vert_up);
56  }
57 
58  // Render lines:
59  if (m_draw_lines)
60  {
61  glDisable(GL_LIGHTING); // Disable lights when drawing lines
62 
63  const int draw_path[] = {0, 1, 3, 2, 0, 4, 6, 2,
64  3, 7, 6, 4, 5, 7, 5, 1};
65 
66  // wireframe:
67  glLineWidth(m_lineWidth);
70  glColor4ub(m_color.R, m_color.G, m_color.B, m_color.A);
71 
72  for (size_t i = 0; i < sizeof(draw_path) / sizeof(draw_path[0]); i++)
73  glVertex3fv(&pts[draw_path[i]].x);
74 
75  glEnd();
76 
77  glEnable(GL_LIGHTING); // Disable lights when drawing lines
78  }
79 
80  if (m_draw_planes)
81  {
82  // solid:
84  glColor4ub(
85  m_planes_color.R, m_planes_color.G, m_planes_color.B,
86  m_planes_color.A);
87 
88  gl_utils::renderQuadWithNormal(pts[0], pts[2], pts[6], pts[4]);
89  gl_utils::renderQuadWithNormal(pts[2], pts[3], pts[7], pts[6]);
90  gl_utils::renderQuadWithNormal(pts[4], pts[6], pts[7], pts[5]);
91  gl_utils::renderQuadWithNormal(pts[1], pts[5], pts[7], pts[3]);
92  gl_utils::renderQuadWithNormal(pts[1], pts[5], pts[7], pts[3]);
93  gl_utils::renderQuadWithNormal(pts[4], pts[5], pts[1], pts[0]);
94 
95  glEnd();
96  }
97 
99 
100 #endif
101 }
102 
103 // Ctors
105  : m_min_distance(0.1f),
106  m_max_distance(1.f),
107  m_fov_horz_left(mrpt::DEG2RAD(45)),
108  m_fov_horz_right(mrpt::DEG2RAD(45)),
109  m_fov_vert_down(mrpt::DEG2RAD(30)),
110  m_fov_vert_up(mrpt::DEG2RAD(30)),
111  m_draw_lines(true),
112  m_draw_planes(false),
113  m_lineWidth(1.5f),
114  m_planes_color(0xE0, 0x00, 0x00, 0x50) // RGBA
115 {
122  keep_min(m_fov_vert_up, DEG2RAD(89.9f));
124 }
125 
127  float near_distance, float far_distance, float horz_FOV_degrees,
128  float vert_FOV_degrees, float lineWidth, bool draw_lines, bool draw_planes)
129  : m_min_distance(near_distance),
130  m_max_distance(far_distance),
131  m_fov_horz_left(mrpt::DEG2RAD(.5f * horz_FOV_degrees)),
132  m_fov_horz_right(mrpt::DEG2RAD(.5f * horz_FOV_degrees)),
133  m_fov_vert_down(mrpt::DEG2RAD(.5f * vert_FOV_degrees)),
134  m_fov_vert_up(mrpt::DEG2RAD(.5f * vert_FOV_degrees)),
135  m_draw_lines(draw_lines),
136  m_draw_planes(draw_planes),
137  m_lineWidth(lineWidth),
138  m_planes_color(0xE0, 0x00, 0x00, 0x50) // RGBA
139 {
140 }
141 
144 {
145  writeToStreamRender(out);
146  // version 0
151 }
152 
154 {
155  switch (version)
156  {
157  case 0:
164  break;
165  default:
167  };
169 }
170 
171 bool CFrustum::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
172 {
174  MRPT_UNUSED_PARAM(dist);
175  THROW_EXCEPTION("TO DO");
176 }
177 
178 // setters:
180  const float near_distance, const float far_distance)
181 {
182  m_min_distance = near_distance;
183  m_max_distance = far_distance;
185 }
186 void CFrustum::setHorzFOV(const float fov_horz_degrees)
187 {
188  m_fov_horz_right = m_fov_horz_left = 0.5f * mrpt::DEG2RAD(fov_horz_degrees);
194 }
195 void CFrustum::setVertFOV(const float fov_vert_degrees)
196 {
197  m_fov_vert_down = m_fov_vert_up = 0.5f * mrpt::DEG2RAD(fov_vert_degrees);
200  keep_min(m_fov_vert_up, DEG2RAD(89.9f));
203 }
205  const float fov_horz_left_degrees, const float fov_horz_right_degrees)
206 {
207  m_fov_horz_left = mrpt::DEG2RAD(fov_horz_left_degrees);
208  m_fov_horz_right = mrpt::DEG2RAD(fov_horz_right_degrees);
214 }
216  const float fov_vert_down_degrees, const float fov_vert_up_degrees)
217 {
218  m_fov_vert_down = mrpt::DEG2RAD(fov_vert_down_degrees);
219  m_fov_vert_up = mrpt::DEG2RAD(fov_vert_up_degrees);
222  keep_min(m_fov_vert_up, DEG2RAD(89.9f));
225 }
226 
228  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
229 {
230  // Compute the 8 corners of the frustum:
231  TPoint3Df pts[8];
232  for (int j = 0; j < 2; j++)
233  {
234  const float r = j == 0 ? m_min_distance : m_max_distance;
235  for (int i = 0; i < 4; i++) pts[4 * j + i].x = r;
236  pts[4 * j + 0].y = -r * tan(m_fov_horz_left);
237  pts[4 * j + 1].y = -r * tan(m_fov_horz_left);
238  pts[4 * j + 2].y = r * tan(m_fov_horz_right);
239  pts[4 * j + 3].y = r * tan(m_fov_horz_right);
240  pts[4 * j + 0].z = -r * tan(m_fov_vert_down);
241  pts[4 * j + 1].z = r * tan(m_fov_vert_up);
242  pts[4 * j + 2].z = -r * tan(m_fov_vert_down);
243  pts[4 * j + 3].z = r * tan(m_fov_vert_up);
244  }
245 
246  bb_min = TPoint3D(
247  std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
248  std::numeric_limits<double>::max());
249  bb_max = TPoint3D(
250  -std::numeric_limits<double>::max(),
251  -std::numeric_limits<double>::max(),
252  -std::numeric_limits<double>::max());
253  for (int i = 0; i < 8; i++)
254  {
255  keep_min(bb_min.x, pts[i].x);
256  keep_min(bb_min.y, pts[i].y);
257  keep_min(bb_min.z, pts[i].z);
258 
259  keep_max(bb_max.x, pts[i].x);
260  keep_max(bb_max.y, pts[i].y);
261  keep_max(bb_max.z, pts[i].z);
262  }
263 
264  // Convert to coordinates of my parent:
265  m_pose.composePoint(bb_min, bb_min);
266  m_pose.composePoint(bb_max, bb_max);
267 }
mrpt::math::TPoint3Df
Lightweight 3D point (float version).
Definition: lightweight_geom_data.h:315
mrpt::keep_min
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.
Definition: core/include/mrpt/core/bits_math.h:124
mrpt::opengl::CFrustum::m_draw_planes
bool m_draw_planes
Definition: CFrustum.h:65
glBegin
GLAPI void GLAPIENTRY glBegin(GLenum mode)
mrpt::opengl::CFrustum::m_fov_vert_up
float m_fov_vert_up
Definition: CFrustum.h:64
glVertex3fv
GLAPI void GLAPIENTRY glVertex3fv(const GLfloat *v)
mrpt::opengl::CFrustum::m_planes_color
mrpt::img::TColor m_planes_color
Definition: CFrustum.h:67
mrpt::opengl::CFrustum::setHorzFOVAsymmetric
void setHorzFOVAsymmetric(const float fov_horz_left_degrees, const float fov_horz_right_degrees)
Changes horizontal FOV (asymmetric)
Definition: CFrustum.cpp:204
mrpt::opengl::CFrustum::serializeFrom
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: CFrustum.cpp:153
mrpt::opengl::CFrustum::setNearFarPlanes
void setNearFarPlanes(const float near_distance, const float far_distance)
Changes distance of near & far planes.
Definition: CFrustum.cpp:179
GL_SRC_ALPHA
#define GL_SRC_ALPHA
Definition: glew.h:286
mrpt::math::TPoint3Df::z
float z
Definition: lightweight_geom_data.h:323
mrpt::opengl::CFrustum::serializeGetVersion
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CFrustum.cpp:142
GL_BLEND
#define GL_BLEND
Definition: glew.h:432
mrpt::opengl::CFrustum::m_fov_horz_left
float m_fov_horz_left
Semi FOVs (in radians)
Definition: CFrustum.h:62
glColor4ub
GLAPI void GLAPIENTRY glColor4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha)
mrpt::opengl::CRenderizableDisplayList
A renderizable object suitable for rendering with OpenGL's display lists.
Definition: CRenderizableDisplayList.h:39
mrpt::math::TPoint3D::z
double z
Definition: lightweight_geom_data.h:385
glDisable
GLAPI void GLAPIENTRY glDisable(GLenum cap)
mrpt::opengl::CRenderizableDisplayList::notifyChange
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated)
Definition: CRenderizableDisplayList.h:57
MRPT_UNUSED_PARAM
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
mrpt::img::TColor::R
uint8_t R
Definition: TColor.h:48
glEnable
GLAPI void GLAPIENTRY glEnable(GLenum cap)
mrpt::opengl::gl_utils::renderQuadWithNormal
void renderQuadWithNormal(const mrpt::math::TPoint3Df &p1, const mrpt::math::TPoint3Df &p2, const mrpt::math::TPoint3Df &p3, const mrpt::math::TPoint3Df &p4)
Can be used by derived classes to draw a quad with a normal vector computed automatically - to be cal...
Definition: gl_utils.cpp:205
mrpt::opengl::CFrustum::m_max_distance
float m_max_distance
Definition: CFrustum.h:60
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
uint8_t
unsigned char uint8_t
Definition: rptypes.h:41
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
GL_LINE_STRIP
#define GL_LINE_STRIP
Definition: glew.h:275
mrpt::opengl::CFrustum::setVertFOVAsymmetric
void setVertFOVAsymmetric(const float fov_vert_down_degrees, const float fov_vert_up_degrees)
Changes vertical FOV (asymmetric)
Definition: CFrustum.cpp:215
mrpt::math::TPoint3Df::y
float y
Definition: lightweight_geom_data.h:322
mrpt::serialization::CArchive
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
mrpt::opengl::CFrustum::m_lineWidth
float m_lineWidth
Definition: CFrustum.h:66
glEnd
GLAPI void GLAPIENTRY glEnd(void)
mrpt::opengl::CFrustum::setVertFOV
void setVertFOV(const float fov_vert_degrees)
Changes vertical FOV (symmetric)
Definition: CFrustum.cpp:195
r
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
mrpt::img::TColor::B
uint8_t B
Definition: TColor.h:48
mrpt::opengl::CRenderizable::m_pose
mrpt::poses::CPose3D m_pose
6D pose wrt the parent coordinate reference.
Definition: CRenderizable.h:57
mrpt::opengl::gl_utils::checkOpenGLError
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:143
GL_LIGHTING
#define GL_LIGHTING
Definition: glew.h:385
GL_TRIANGLES
#define GL_TRIANGLES
Definition: glew.h:276
glLineWidth
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
GL_ONE_MINUS_SRC_ALPHA
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:287
mrpt::keep_max
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.
Definition: core/include/mrpt/core/bits_math.h:131
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
mrpt::math::TPoint3D::x
double x
X,Y,Z coordinates.
Definition: lightweight_geom_data.h:385
mrpt::opengl::CFrustum::setHorzFOV
void setHorzFOV(const float fov_horz_degrees)
Changes horizontal FOV (symmetric)
Definition: CFrustum.cpp:186
mrpt::opengl::CFrustum::serializeTo
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CFrustum.cpp:143
mrpt::opengl::CFrustum::getBoundingBox
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: CFrustum.cpp:227
mrpt::opengl::CFrustum
A solid or wireframe frustum in 3D (a rectangular truncated pyramid), with arbitrary (possibly assyme...
Definition: CFrustum.h:54
GL_DEPTH_TEST
#define GL_DEPTH_TEST
Definition: glew.h:401
mrpt::img::TColor::A
uint8_t A
Definition: TColor.h:48
IMPLEMENTS_SERIALIZABLE
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
Definition: CSerializable.h:114
gl_utils.h
mrpt::math::TPoint3D
Lightweight 3D point.
Definition: lightweight_geom_data.h:378
opengl-precomp.h
mrpt::math::TPoint3D::y
double y
Definition: lightweight_geom_data.h:385
mrpt::poses::CPose3D::composePoint
void composePoint(double lx, double ly, double lz, double &gx, double &gy, double &gz, mrpt::math::CMatrixFixedNumeric< double, 3, 3 > *out_jacobian_df_dpoint=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dpose=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dse3=nullptr, bool use_small_rot_approx=false) const
An alternative, slightly more efficient way of doing with G and L being 3D points and P this 6D pose...
Definition: CPose3D.cpp:379
mrpt::math::TPoint3Df::x
float x
Definition: lightweight_geom_data.h:321
opengl_internals.h
mrpt::opengl::CFrustum::m_fov_vert_down
float m_fov_vert_down
Semi FOVs (in radians)
Definition: CFrustum.h:64
mrpt::opengl::CFrustum::traceRay
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const override
Ray tracing.
Definition: CFrustum.cpp:171
mrpt::opengl::CFrustum::m_fov_horz_right
float m_fov_horz_right
Definition: CFrustum.h:62
void
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
mrpt::img::TColor::G
uint8_t G
Definition: TColor.h:48
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
CFrustum.h
in
GLuint in
Definition: glext.h:7274
CArchive.h
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:90
mrpt::opengl::CRenderizable::writeToStreamRender
void writeToStreamRender(mrpt::serialization::CArchive &out) const
Definition: CRenderizable.cpp:110
mrpt::opengl::CFrustum::m_draw_lines
bool m_draw_lines
Definition: CFrustum.h:65
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
mrpt::opengl::CRenderizableDisplayList::readFromStreamRender
void readFromStreamRender(mrpt::serialization::CArchive &in)
Definition: CRenderizableDisplayList.h:65
mrpt::opengl::CFrustum::CFrustum
CFrustum()
Basic empty constructor.
Definition: CFrustum.cpp:104
x
GLenum GLint x
Definition: glext.h:3538
glBlendFunc
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
mrpt::opengl::CFrustum::m_min_distance
float m_min_distance
Near and far planes.
Definition: CFrustum.h:60
mrpt::DEG2RAD
double DEG2RAD(const double x)
Degrees to radians.
Definition: core/include/mrpt/core/bits_math.h:42



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST