Main MRPT website > C++ reference for MRPT 1.9.9
CSphere.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 
12 #include <mrpt/opengl/CSphere.h>
13 //#include <mrpt/poses/CPose3D.h>
14 #include <mrpt/utils/CStream.h>
15 #include "opengl_internals.h"
16 
17 using namespace mrpt;
18 using namespace mrpt::opengl;
19 using namespace mrpt::poses;
20 using namespace mrpt::utils;
21 using namespace mrpt::math;
22 using namespace std;
23 
25 
26 CSphere::Ptr CSphere::Create(
27  float radius, int nDivsLongitude, int nDivsLatitude)
28 {
29  return CSphere::Ptr(new CSphere(radius, nDivsLongitude, nDivsLatitude));
30 }
31 /*---------------------------------------------------------------
32  render_dl
33  ---------------------------------------------------------------*/
34 void CSphere::render_dl() const
35 {
36 #if MRPT_HAS_OPENGL_GLUT
37  if (m_color.A != 255)
38  {
42  }
43 
44  // Determine radius depending on eye distance?
45  float real_radius;
46  if (m_keepRadiusIndependentEyeDistance)
47  {
48  glRasterPos3f(0.0f, 0.0f, 0.0f);
49 
50  GLfloat raster_pos[4];
52  float eye_distance = raster_pos[3];
53 
54  eye_distance = max(eye_distance, 0.1f);
55 
56  real_radius = 0.01 * m_radius * eye_distance;
57  }
58  else
59  real_radius = m_radius;
60 
61  GLUquadricObj* obj = gluNewQuadric();
63 
64  gluQuadricDrawStyle(obj, GLU_FILL);
65  gluQuadricNormals(obj, GLU_SMOOTH);
66 
67  gluSphere(obj, real_radius, m_nDivsLongitude, m_nDivsLatitude);
69 
70  gluDeleteQuadric(obj);
72 
73  if (m_color.A != 255)
74  {
77  }
78 
79 #endif
80 }
81 /*---------------------------------------------------------------
82  Implements the writing to a CStream capability of
83  CSerializable objects
84  ---------------------------------------------------------------*/
85 void CSphere::writeToStream(mrpt::utils::CStream& out, int* version) const
86 {
87  if (version)
88  *version = 1;
89  else
90  {
91  writeToStreamRender(out);
92  out << m_radius;
93  out << (uint32_t)m_nDivsLongitude << (uint32_t)m_nDivsLatitude
94  << m_keepRadiusIndependentEyeDistance;
95  }
96 }
97 
98 /*---------------------------------------------------------------
99  Implements the reading from a CStream capability of
100  CSerializable objects
101  ---------------------------------------------------------------*/
103 {
104  switch (version)
105  {
106  case 0:
107  case 1:
108  {
109  readFromStreamRender(in);
110  in >> m_radius;
111  uint32_t i, j;
112  in >> i >> j;
113  m_nDivsLongitude = i;
114  m_nDivsLatitude = j;
115  if (version >= 1)
116  in >> m_keepRadiusIndependentEyeDistance;
117  else
118  m_keepRadiusIndependentEyeDistance = false;
119  }
120  break;
121  default:
123  };
125 }
126 
127 bool CSphere::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
128 {
129  // We need to find the points of the sphere which collide with the laser
130  // beam.
131  // The sphere's equation is invariant to rotations (but not to
132  // translations), and we can take advantage of this;
133  // we'll simply transform the center and then compute the beam's points
134  // whose distance to that transformed point
135  // equals the sphere's radius.
136 
137  CPose3D transf = this->m_pose - o;
138  double x = transf.x(), y = transf.y(), z = transf.z();
139  double r2 = m_radius * m_radius;
140  double dyz = y * y + z * z;
141  if (dyz > r2) return false;
142  double dx = sqrt(r2 - dyz);
143  if (x - dx >= 0)
144  {
145  dist = x - dx;
146  return true;
147  }
148  else if (x + dx >= 0)
149  {
150  dist = x + dx;
151  return true;
152  }
153  else
154  return false;
155 }
156 
158  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
159 {
160  bb_min.x = -m_radius;
161  bb_min.y = -m_radius;
162  bb_min.z = -m_radius;
163 
164  bb_max.x = m_radius;
165  bb_max.y = m_radius;
166  bb_max.z = m_radius;
167 
168  // Convert to coordinates of my parent:
169  m_pose.composePoint(bb_min, bb_min);
170  m_pose.composePoint(bb_max, bb_max);
171 }
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:135
GLAPI void GLAPIENTRY glGetFloatv(GLenum pname, GLfloat *params)
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
GLdouble GLdouble z
Definition: glext.h:3872
GLAPI void GLAPIENTRY glEnable(GLenum cap)
GLAPI void GLAPIENTRY glRasterPos3f(GLfloat x, GLfloat y, GLfloat z)
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const override
Ray tracing.
Definition: CSphere.cpp:127
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...
Definition: CSphere.cpp:102
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
STL namespace.
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:287
std::shared_ptr< CObject > Ptr
Definition: CObject.h:154
#define GL_DEPTH_TEST
Definition: glew.h:401
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
#define GL_CURRENT_RASTER_POSITION
Definition: glew.h:360
float GLfloat
Definition: glew.h:217
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
Definition: CSphere.cpp:85
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
double x
X,Y,Z coordinates.
#define GL_BLEND
Definition: glew.h:432
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
#define GL_SRC_ALPHA
Definition: glew.h:286
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:140
A solid or wire-frame sphere.
Definition: CSphere.h:31
GLuint in
Definition: glext.h:7274
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
GLenum GLint GLint y
Definition: glext.h:3538
GLenum GLint x
Definition: glext.h:3538
Lightweight 3D point.
void render_dl() const override
Render.
Definition: CSphere.cpp:34
unsigned __int32 uint32_t
Definition: rptypes.h:47
GLAPI void GLAPIENTRY glDisable(GLenum cap)
std::shared_ptr< CSphere > Ptr
Definition: CSphere.h:33
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: CSphere.cpp:157



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019