MRPT  1.9.9
CSphere.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://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 void CSphere::render_dl() const
27 {
28 #if MRPT_HAS_OPENGL_GLUT
29  if (m_color.A != 255)
30  {
34  }
35 
36  // Determine radius depending on eye distance?
37  float real_radius;
38  if (m_keepRadiusIndependentEyeDistance)
39  {
40  glRasterPos3f(0.0f, 0.0f, 0.0f);
41 
42  GLfloat raster_pos[4];
44  float eye_distance = raster_pos[3];
45 
46  eye_distance = max(eye_distance, 0.1f);
47 
48  real_radius = 0.01 * m_radius * eye_distance;
49  }
50  else
51  real_radius = m_radius;
52 
53  GLUquadricObj* obj = gluNewQuadric();
55 
56  gluQuadricDrawStyle(obj, GLU_FILL);
57  gluQuadricNormals(obj, GLU_SMOOTH);
58 
59  gluSphere(obj, real_radius, m_nDivsLongitude, m_nDivsLatitude);
61 
62  gluDeleteQuadric(obj);
64 
65  if (m_color.A != 255)
66  {
69  }
70 
71 #endif
72 }
73 
74 uint8_t CSphere::serializeGetVersion() const { return 1; }
76 {
77  writeToStreamRender(out);
78  out << m_radius;
79  out << (uint32_t)m_nDivsLongitude << (uint32_t)m_nDivsLatitude
80  << m_keepRadiusIndependentEyeDistance;
81 }
83 {
84  switch (version)
85  {
86  case 0:
87  case 1:
88  {
89  readFromStreamRender(in);
90  in >> m_radius;
91  uint32_t i, j;
92  in >> i >> j;
93  m_nDivsLongitude = i;
94  m_nDivsLatitude = j;
95  if (version >= 1)
96  in >> m_keepRadiusIndependentEyeDistance;
97  else
98  m_keepRadiusIndependentEyeDistance = false;
99  }
100  break;
101  default:
103  };
105 }
106 
107 bool CSphere::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
108 {
109  // We need to find the points of the sphere which collide with the laser
110  // beam.
111  // The sphere's equation is invariant to rotations (but not to
112  // translations), and we can take advantage of this;
113  // we'll simply transform the center and then compute the beam's points
114  // whose distance to that transformed point
115  // equals the sphere's radius.
116 
117  CPose3D transf = this->m_pose - o;
118  double x = transf.x(), y = transf.y(), z = transf.z();
119  double r2 = m_radius * m_radius;
120  double dyz = y * y + z * z;
121  if (dyz > r2) return false;
122  double dx = sqrt(r2 - dyz);
123  if (x - dx >= 0)
124  {
125  dist = x - dx;
126  return true;
127  }
128  else if (x + dx >= 0)
129  {
130  dist = x + dx;
131  return true;
132  }
133  else
134  return false;
135 }
136 
139 {
140  bb_min.x = -m_radius;
141  bb_min.y = -m_radius;
142  bb_min.z = -m_radius;
143 
144  bb_max.x = m_radius;
145  bb_max.y = m_radius;
146  bb_max.z = m_radius;
147 
148  // Convert to coordinates of my parent:
149  m_pose.composePoint(bb_min, bb_min);
150  m_pose.composePoint(bb_max, bb_max);
151 }
void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
GLAPI void GLAPIENTRY glGetFloatv(GLenum pname, GLfloat *params)
GLdouble GLdouble z
Definition: glext.h:3879
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:107
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
STL namespace.
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:288
#define GL_DEPTH_TEST
Definition: glew.h:402
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CSphere.cpp:74
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
#define GL_CURRENT_RASTER_POSITION
Definition: glew.h:361
float GLfloat
Definition: glew.h:218
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
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:97
This base provides a set of functions for maths stuff.
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:143
#define GL_BLEND
Definition: glew.h:433
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
#define GL_SRC_ALPHA
Definition: glew.h:287
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:54
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CSphere.cpp:75
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:84
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:155
A solid or wire-frame sphere.
Definition: CSphere.h:28
GLuint in
Definition: glext.h:7391
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
const auto bb_max
GLenum GLint GLint y
Definition: glext.h:3542
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
const auto bb_min
GLenum GLint x
Definition: glext.h:3542
Lightweight 3D point.
Definition: TPoint3D.h:90
GLAPI void GLAPIENTRY glDisable(GLenum cap)
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:137
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: CSphere.cpp:82



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: abb8b1a1e Fri Oct 18 14:19:12 2019 +0200 at vie oct 18 14:20:13 CEST 2019