Main MRPT website > C++ reference for MRPT 1.5.6
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-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 #include <mrpt/opengl/CFrustum.h>
12 #include <mrpt/utils/CStream.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 using namespace mrpt::utils;
20 using namespace mrpt::math;
21 using namespace std;
22 
24 
25 CFrustumPtr CFrustum::Create(float near_distance, float far_distance, float horz_FOV_degrees, float vert_FOV_degrees, float lineWidth, bool draw_lines, bool draw_planes)
26 {
27  return CFrustumPtr(new CFrustum(near_distance,far_distance,horz_FOV_degrees,vert_FOV_degrees,lineWidth,draw_lines,draw_planes));
28 }
29 
30 /*---------------------------------------------------------------
31  render
32  ---------------------------------------------------------------*/
33 void CFrustum::render_dl() const {
34 #if MRPT_HAS_OPENGL_GLUT
35  if (m_color.A!=255 || (m_draw_planes && m_planes_color.A!=255) )
36  {
39  }
40  else
41  {
44  }
45 
46  // Compute the 8 corners of the frustum:
47  TPoint3Df pts[8];
48  for (int j=0;j<2;j++)
49  {
50  const float r = j==0 ? m_min_distance : m_max_distance;
51  for (int i=0;i<4;i++)
52  pts[4*j+i].x = r;
53  pts[4*j+0].y = -r*tan(m_fov_horz_left);
54  pts[4*j+1].y = -r*tan(m_fov_horz_left);
55  pts[4*j+2].y = r*tan(m_fov_horz_right);
56  pts[4*j+3].y = r*tan(m_fov_horz_right);
57  pts[4*j+0].z = -r*tan(m_fov_vert_down);
58  pts[4*j+1].z = r*tan(m_fov_vert_up);
59  pts[4*j+2].z = -r*tan(m_fov_vert_down);
60  pts[4*j+3].z = r*tan(m_fov_vert_up);
61  }
62 
63  // Render lines:
64  if (m_draw_lines)
65  {
66  glDisable(GL_LIGHTING); // Disable lights when drawing lines
67 
68  const int draw_path[] = {
69  0,1,3,2,0,4,6,2,
70  3,7,6,4,5,7,5,1 };
71 
72  // wireframe:
73  glLineWidth(m_lineWidth); checkOpenGLError();
75  glColor4ub(m_color.R,m_color.G,m_color.B,m_color.A);
76 
77  for (size_t i=0;i<sizeof(draw_path)/sizeof(draw_path[0]);i++)
78  glVertex3fv(&pts[draw_path[i]].x);
79 
80  glEnd();
81 
82  glEnable(GL_LIGHTING); // Disable lights when drawing lines
83  }
84 
85  if (m_draw_planes)
86  {
87  // solid:
89  glColor4ub(m_planes_color.R,m_planes_color.G,m_planes_color.B,m_planes_color.A);
90 
91  gl_utils::renderQuadWithNormal( pts[0], pts[2], pts[6], pts[4] );
92  gl_utils::renderQuadWithNormal( pts[2], pts[3], pts[7], pts[6] );
93  gl_utils::renderQuadWithNormal( pts[4], pts[6], pts[7], pts[5] );
94  gl_utils::renderQuadWithNormal( pts[1], pts[5], pts[7], pts[3] );
95  gl_utils::renderQuadWithNormal( pts[1], pts[5], pts[7], pts[3] );
96  gl_utils::renderQuadWithNormal( pts[4], pts[5], pts[1], pts[0] );
97 
98  glEnd();
99  }
100 
102 
103 #endif
104 }
105 
106 //Ctors
108  m_min_distance(0.1f),
109  m_max_distance(1.f),
110  m_fov_horz_left(mrpt::utils::DEG2RAD(45)),
111  m_fov_horz_right(mrpt::utils::DEG2RAD(45)),
112  m_fov_vert_down(mrpt::utils::DEG2RAD(30)),
113  m_fov_vert_up(mrpt::utils::DEG2RAD(30)),
114  m_draw_lines(true),
115  m_draw_planes(false),
116  m_lineWidth(1.5f),
117  m_planes_color(0xE0,0x00,0x00, 0x50) // RGBA
118 {
123 }
124 
125 CFrustum::CFrustum(float near_distance, float far_distance, float horz_FOV_degrees, float vert_FOV_degrees, float lineWidth, bool draw_lines, bool draw_planes) :
126  m_min_distance(near_distance),
127  m_max_distance(far_distance),
128  m_fov_horz_left(mrpt::utils::DEG2RAD(.5f*horz_FOV_degrees)),
129  m_fov_horz_right(mrpt::utils::DEG2RAD(.5f*horz_FOV_degrees)),
130  m_fov_vert_down(mrpt::utils::DEG2RAD(.5f*vert_FOV_degrees)),
131  m_fov_vert_up(mrpt::utils::DEG2RAD(.5f*vert_FOV_degrees)),
132  m_draw_lines(draw_lines),
133  m_draw_planes(draw_planes),
134  m_lineWidth(lineWidth),
135  m_planes_color(0xE0,0x00,0x00, 0x50) // RGBA
136 {
137 }
138 
139 
140 /*---------------------------------------------------------------
141  Implements the writing to a CStream capability of
142  CSerializable objects
143  ---------------------------------------------------------------*/
145  if (version) *version=0;
146  else {
147  writeToStreamRender(out);
148  //version 0
153  << m_lineWidth
155  }
156 }
157 
158 /*---------------------------------------------------------------
159  Implements the reading from a CStream capability of
160  CSerializable objects
161  ---------------------------------------------------------------*/
163  switch (version) {
164  case 0:
170  >> m_lineWidth
172  break;
173  default:
175  };
177 }
178 
179 
180 bool CFrustum::traceRay(const mrpt::poses::CPose3D &o,double &dist) const
181 {
183  THROW_EXCEPTION("TO DO")
184 }
185 
186 
187 // setters:
188 void CFrustum::setNearFarPlanes(const float near_distance, const float far_distance)
189 {
190  m_min_distance = near_distance;
191  m_max_distance = far_distance;
193 }
194 void CFrustum::setHorzFOV(const float fov_horz_degrees)
195 {
196  m_fov_horz_right = m_fov_horz_left = 0.5f* mrpt::utils::DEG2RAD(fov_horz_degrees);
200 }
201 void CFrustum::setVertFOV(const float fov_vert_degrees)
202 {
203  m_fov_vert_down=m_fov_vert_up = 0.5f*mrpt::utils::DEG2RAD(fov_vert_degrees);
207 }
208 void CFrustum::setHorzFOVAsymmetric(const float fov_horz_left_degrees,const float fov_horz_right_degrees)
209 {
210  m_fov_horz_left= mrpt::utils::DEG2RAD(fov_horz_left_degrees);
211  m_fov_horz_right = mrpt::utils::DEG2RAD(fov_horz_right_degrees);
215 }
216 void CFrustum::setVertFOVAsymmetric(const float fov_vert_down_degrees,const float fov_vert_up_degrees)
217 {
218  m_fov_vert_down= mrpt::utils::DEG2RAD(fov_vert_down_degrees);
219  m_fov_vert_up = mrpt::utils::DEG2RAD(fov_vert_up_degrees);
223 }
224 
225 
227 {
228  // Compute the 8 corners of the frustum:
229  TPoint3Df pts[8];
230  for (int j=0;j<2;j++)
231  {
232  const float r = j==0 ? m_min_distance : m_max_distance;
233  for (int i=0;i<4;i++)
234  pts[4*j+i].x = r;
235  pts[4*j+0].y = -r*tan(m_fov_horz_left);
236  pts[4*j+1].y = -r*tan(m_fov_horz_left);
237  pts[4*j+2].y = r*tan(m_fov_horz_right);
238  pts[4*j+3].y = r*tan(m_fov_horz_right);
239  pts[4*j+0].z = -r*tan(m_fov_vert_down);
240  pts[4*j+1].z = r*tan(m_fov_vert_up);
241  pts[4*j+2].z = -r*tan(m_fov_vert_down);
242  pts[4*j+3].z = r*tan(m_fov_vert_up);
243  }
244 
245  bb_min = TPoint3D( std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), std::numeric_limits<double>::max() );
246  bb_max = TPoint3D(-std::numeric_limits<double>::max(),-std::numeric_limits<double>::max(),-std::numeric_limits<double>::max() );
247  for (int i=0;i<8;i++)
248  {
249  keep_min(bb_min.x, pts[i].x);
250  keep_min(bb_min.y, pts[i].y);
251  keep_min(bb_min.z, pts[i].z);
252 
253  keep_max(bb_max.x, pts[i].x);
254  keep_max(bb_max.y, pts[i].y);
255  keep_max(bb_max.z, pts[i].z);
256  }
257 
258  // Convert to coordinates of my parent:
259  m_pose.composePoint(bb_min, bb_min);
260  m_pose.composePoint(bb_max, bb_max);
261 }
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: CFrustum.cpp:226
GLAPI void GLAPIENTRY glVertex3fv(const GLfloat *v)
double DEG2RAD(const double x)
Degrees to radians.
GLAPI void GLAPIENTRY glEnable(GLenum cap)
void OPENGL_IMPEXP 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:187
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
#define THROW_EXCEPTION(msg)
#define GL_TRIANGLES
Definition: glew.h:272
void setVertFOV(const float fov_vert_degrees)
Changes vertical FOV (symmetric)
Definition: CFrustum.cpp:201
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
void render_dl() const MRPT_OVERRIDE
Render.
Definition: CFrustum.cpp:33
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:283
double z
X,Y,Z coordinates.
#define GL_DEPTH_TEST
Definition: glew.h:397
void setHorzFOV(const float fov_horz_degrees)
Changes horizontal FOV (symmetric)
Definition: CFrustum.cpp:194
#define GL_LIGHTING
Definition: glew.h:381
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
mrpt::poses::CPose3D m_pose
6D pose wrt the parent coordinate reference. This class automatically holds the cached 3x3 rotation m...
Definition: CRenderizable.h:55
GLuint in
Definition: glew.h:7146
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
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: CFrustum.cpp:162
A renderizable object suitable for rendering with OpenGL's display lists.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
float m_max_distance
Near and far planes.
Definition: CFrustum.h:52
Lightweight 3D point (float version).
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
float m_fov_vert_up
Semi FOVs (in radians)
Definition: CFrustum.h:54
CFrustum()
Basic empty constructor.
Definition: CFrustum.cpp:107
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
void readFromStreamRender(mrpt::utils::CStream &in)
GLint GLint GLint GLint GLint x
Definition: glew.h:1166
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const
Introduces a pure virtual method responsible for writing to a CStream.
Definition: CFrustum.cpp:144
int version
Definition: mrpt_jpeglib.h:898
GLAPI void GLAPIENTRY glBegin(GLenum mode)
#define GL_BLEND
Definition: glew.h:428
#define DEG2RAD
void writeToStreamRender(utils::CStream &out) const
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: bits.h:176
GLAPI void GLAPIENTRY glColor4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha)
#define GL_SRC_ALPHA
Definition: glew.h:282
void composePoint(double lx, double ly, double lz, double &gx, double &gy, double &gz, mrpt::math::CMatrixFixedNumeric< double, 3, 3 > *out_jacobian_df_dpoint=NULL, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dpose=NULL, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dse3=NULL, 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:427
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: bits.h:171
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:72
void setNearFarPlanes(const float near_distance, const float far_distance)
Changes distance of near & far planes.
Definition: CFrustum.cpp:188
void OPENGL_IMPEXP checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:134
void setVertFOVAsymmetric(const float fov_vert_down_degrees, const float fov_vert_up_degrees)
Changes vertical FOV (asymmetric)
Definition: CFrustum.cpp:216
GLdouble GLdouble GLdouble r
Definition: glew.h:1311
GLAPI void GLAPIENTRY glEnd(void)
A solid or wireframe frustum in 3D (a rectangular truncated pyramid), with arbitrary (possibly assyme...
Definition: CFrustum.h:47
#define GL_LINE_STRIP
Definition: glew.h:271
Lightweight 3D point.
void setHorzFOVAsymmetric(const float fov_horz_left_degrees, const float fov_horz_right_degrees)
Changes horizontal FOV (asymmetric)
Definition: CFrustum.cpp:208
GLAPI void GLAPIENTRY glDisable(GLenum cap)
mrpt::utils::TColor m_planes_color
Definition: CFrustum.h:57
GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble f
Definition: glew.h:5092
float m_fov_horz_right
Semi FOVs (in radians)
Definition: CFrustum.h:53
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const MRPT_OVERRIDE
Ray tracing.
Definition: CFrustum.cpp:180



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