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-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 
12 #include <mrpt/opengl/CSphere.h>
13 //#include <mrpt/poses/CPose3D.h>
15 #include "opengl_internals.h"
16 
17 using namespace mrpt;
18 using namespace mrpt::opengl;
19 using namespace mrpt::poses;
20 
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 
84 {
85  writeToStreamRender(out);
86  out << m_radius;
87  out << (uint32_t)m_nDivsLongitude << (uint32_t)m_nDivsLatitude
88  << m_keepRadiusIndependentEyeDistance;
89 }
91 {
92  switch (version)
93  {
94  case 0:
95  case 1:
96  {
97  readFromStreamRender(in);
98  in >> m_radius;
99  uint32_t i, j;
100  in >> i >> j;
101  m_nDivsLongitude = i;
102  m_nDivsLatitude = j;
103  if (version >= 1)
104  in >> m_keepRadiusIndependentEyeDistance;
105  else
106  m_keepRadiusIndependentEyeDistance = false;
107  }
108  break;
109  default:
111  };
113 }
114 
115 bool CSphere::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
116 {
117  // We need to find the points of the sphere which collide with the laser
118  // beam.
119  // The sphere's equation is invariant to rotations (but not to
120  // translations), and we can take advantage of this;
121  // we'll simply transform the center and then compute the beam's points
122  // whose distance to that transformed point
123  // equals the sphere's radius.
124 
125  CPose3D transf = this->m_pose - o;
126  double x = transf.x(), y = transf.y(), z = transf.z();
127  double r2 = m_radius * m_radius;
128  double dyz = y * y + z * z;
129  if (dyz > r2) return false;
130  double dx = sqrt(r2 - dyz);
131  if (x - dx >= 0)
132  {
133  dist = x - dx;
134  return true;
135  }
136  else if (x + dx >= 0)
137  {
138  dist = x + dx;
139  return true;
140  }
141  else
142  return false;
143 }
144 
146  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
147 {
148  bb_min.x = -m_radius;
149  bb_min.y = -m_radius;
150  bb_min.z = -m_radius;
151 
152  bb_max.x = m_radius;
153  bb_max.y = m_radius;
154  bb_max.z = m_radius;
155 
156  // Convert to coordinates of my parent:
157  m_pose.composePoint(bb_min, bb_min);
158  m_pose.composePoint(bb_max, bb_max);
159 }
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:140
GLAPI void GLAPIENTRY glGetFloatv(GLenum pname, GLfloat *params)
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:115
#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
#define GL_DEPTH_TEST
Definition: glew.h:401
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CSphere.cpp:82
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)
unsigned char uint8_t
Definition: rptypes.h:41
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:90
This base provides a set of functions for maths stuff.
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...
#define GL_SRC_ALPHA
Definition: glew.h:286
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:52
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CSphere.cpp:83
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:86
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:143
A solid or wire-frame sphere.
Definition: CSphere.h:29
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:31
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:145
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: CSphere.cpp:90



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020