Main MRPT website > C++ reference for MRPT 1.5.7
CCylinder.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/CCylinder.h>
12 #include <mrpt/math/geometry.h>
13 #include <mrpt/utils/CStream.h>
14 
15 #include "opengl_internals.h"
16 
17 using namespace mrpt;
18 using namespace mrpt::opengl;
19 using namespace mrpt::math;
20 using namespace mrpt::utils;
21 using namespace std;
22 
24 
25 CCylinderPtr CCylinder::Create(const float baseRadius,const float topRadius,const float height,const int slices,const int stacks)
26 {
27  return CCylinderPtr(new CCylinder(baseRadius,topRadius,height,slices,stacks));
28 }
29 /*---------------------------------------------------------------
30  render
31  ---------------------------------------------------------------*/
32 void CCylinder::render_dl() const {
33 #if MRPT_HAS_OPENGL_GLUT
38  GLUquadricObj *obj=gluNewQuadric();
39 
40  // This is required to draw cylinders of negative height.
41  const float absHeight = std::abs(mHeight);
42  if (mHeight<0)
43  {
44  glPushMatrix();
45  glTranslatef(0,0,mHeight);
46  }
47 
48  gluCylinder(obj,mBaseRadius,mTopRadius,absHeight,mSlices,mStacks);
49 
50  if (mHeight<0)
51  glPopMatrix();
52 
53  if (mHasBottomBase) gluDisk(obj,0,mBaseRadius,mSlices,1);
54  if (mHasTopBase&&mTopRadius>0) {
55  glPushMatrix();
56  glTranslatef(0,0,mHeight);
57  gluDisk(obj,0,mTopRadius,mSlices,1);
58  glPopMatrix();
59  }
60  gluDeleteQuadric(obj);
62 
63 #endif
64 }
65 
66 /*---------------------------------------------------------------
67  Implements the writing to a CStream capability of
68  CSerializable objects
69  ---------------------------------------------------------------*/
71  if (version) *version=0;
72  else {
73  writeToStreamRender(out);
74  //version 0
75  out<<mBaseRadius<<mTopRadius<<mHeight<<mSlices<<mStacks<<mHasBottomBase<<mHasTopBase;
76  }
77 }
78 
79 /*---------------------------------------------------------------
80  Implements the reading from a CStream capability of
81  CSerializable objects
82  ---------------------------------------------------------------*/
84  switch (version) {
85  case 0:
86  readFromStreamRender(in);
87  in>>mBaseRadius>>mTopRadius>>mHeight>>mSlices>>mStacks>>mHasBottomBase>>mHasTopBase;
88  break;
89  default:
91  };
93 }
94 
95 bool solveEqn(double a,double b,double c,double &t) { //Actually, the b from the quadratic equation is the DOUBLE of this. But this way, operations are simpler.
96  if (a<0) {
97  a=-a;
98  b=-b;
99  c=-c;
100  }
102  double delta=square(b)-a*c;
103  if (delta==0) return (t=-b/a)>=0;
104  else if (delta>=0) {
105  delta=sqrt(delta);
106  if (-b-delta>0) {
107  t=(-b-delta)/a;
108  return true;
109  } else if (-b+delta>0) {
110  t=(-b+delta)/a;
111  return true;
112  } //else return false; Both solutions are negative
113  } //else return false; Both solutions are complex
114  } else if (abs(b)>=mrpt::math::geometryEpsilon) {
115  t=-c/(b+b);
116  return t>=0;
117  } //else return false; This actually isn't an equation
118  return false;
119 }
120 
121 bool CCylinder::traceRay(const mrpt::poses::CPose3D &o,double &dist) const {
122  TLine3D lin;
123  createFromPoseX(o-this->m_pose,lin);
124  lin.unitarize(); //By adding this line, distance from any point of the line to its base is exactly equal to the "t".
125  if (abs(lin.director[2])<geometryEpsilon) {
126  if (!reachesHeight(lin.pBase.z)) return false;
127  float r;
128  return getRadius(static_cast<float>(lin.pBase.z),r)?solveEqn(square(lin.director[0])+square(lin.director[1]),lin.director[0]*lin.pBase.x+lin.director[1]*lin.pBase.y,square(lin.pBase.x)+square(lin.pBase.y)-square(r),dist):false;
129  }
130  bool fnd=false;
131  double nDist,tZ0;
132  if (mHasBottomBase&&(tZ0=-lin.pBase.z/lin.director[2])>0) {
133  nDist=sqrt(square(lin.pBase.x+tZ0*lin.director[0])+square(lin.pBase.y+tZ0*lin.director[1]));
134  if (nDist<=mBaseRadius) {
135  fnd=true;
136  dist=tZ0;
137  }
138  }
139  if (mHasTopBase) {
140  tZ0=(mHeight-lin.pBase.z)/lin.director[2];
141  if (tZ0>0&&(!fnd||tZ0<dist)) {
142  nDist=sqrt(square(lin.pBase.x+tZ0*lin.director[0])+square(lin.pBase.y+tZ0*lin.director[1]));
143  if (nDist<=mTopRadius) {
144  fnd=true;
145  dist=tZ0;
146  }
147  }
148  }
149  if (mBaseRadius==mTopRadius) {
150  if (solveEqn(square(lin.director[0])+square(lin.director[1]),lin.director[0]*lin.pBase.x+lin.director[1]*lin.pBase.y,square(lin.pBase.x)+square(lin.pBase.y)-square(mBaseRadius),nDist)) if ((!fnd||nDist<dist)&&reachesHeight(lin.pBase.z+nDist*lin.director[2])) {
151  dist=nDist;
152  fnd=true;
153  }
154  } else {
155  double slope=(mTopRadius-mBaseRadius)/mHeight;
156  if (solveEqn(square(lin.director[0])+square(lin.director[1])-square(lin.director[2]*slope),lin.pBase.x*lin.director[0]+lin.pBase.y*lin.director[1]-(mBaseRadius+slope*lin.pBase.z)*slope*lin.director[2],square(lin.pBase.x)+square(lin.pBase.y)-square(mBaseRadius+slope*lin.pBase.z),nDist)) if ((!fnd||nDist<dist)&&reachesHeight(lin.pBase.z+nDist*lin.director[2])) {
157  dist=nDist;
158  fnd=true;
159  }
160  }
161  return fnd;
162 }
163 
164 
165 
167 {
168  bb_min.x = -std::max(mBaseRadius,mTopRadius);
169  bb_min.y = bb_min.x;
170  bb_min.z = 0;
171 
172  bb_max.x = std::max(mBaseRadius,mTopRadius);
173  bb_max.y = bb_max.x;
174  bb_max.z = mHeight;
175 
176  // Convert to coordinates of my parent:
177  m_pose.composePoint(bb_min, bb_min);
178  m_pose.composePoint(bb_max, bb_max);
179 }
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const
Introduces a pure virtual method responsible for writing to a CStream.
Definition: CCylinder.cpp:70
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
GLdouble GLdouble t
Definition: glext.h:3610
GLAPI void GLAPIENTRY glEnable(GLenum cap)
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: CCylinder.cpp:166
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
GLAPI void GLAPIENTRY glPopMatrix(void)
TPoint3D pBase
Base point.
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:283
class OPENGL_IMPEXP CCylinder
Definition: CCylinder.h:16
double z
X,Y,Z coordinates.
GLsizei GLsizei GLuint * obj
Definition: glext.h:3902
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:52
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
void BASE_IMPEXP createFromPoseX(const mrpt::poses::CPose3D &p, TLine3D &r)
Gets a 3D line corresponding to the X axis in a given pose.
Definition: geometry.cpp:781
void render_dl() const MRPT_OVERRIDE
Render.
Definition: CCylinder.cpp:32
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
const GLubyte * c
Definition: glext.h:5590
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
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: CCylinder.cpp:83
A cylinder or cone whose base lies in the XY plane.
Definition: CCylinder.h:30
GLubyte GLubyte b
Definition: glext.h:5575
int version
Definition: mrpt_jpeglib.h:898
#define GL_BLEND
Definition: glew.h:428
void unitarize()
Unitarize director vector.
double director[3]
Director vector.
GLAPI void GLAPIENTRY glTranslatef(GLfloat x, GLfloat y, GLfloat z)
#define GL_SRC_ALPHA
Definition: glew.h:282
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const MRPT_OVERRIDE
Ray tracing.
Definition: CCylinder.cpp:121
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
Definition: glext.h:3618
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:72
bool solveEqn(double a, double b, double c, double &t)
Definition: CCylinder.cpp:95
void OPENGL_IMPEXP checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:134
GLuint in
Definition: glext.h:6301
The namespace for 3D scene representation and rendering.
GLAPI void GLAPIENTRY glPushMatrix(void)
Lightweight 3D point.
GLenum GLsizei GLsizei height
Definition: glext.h:3523
GLAPI void GLAPIENTRY glDisable(GLenum cap)
GLubyte GLubyte GLubyte a
Definition: glext.h:5575
double BASE_IMPEXP geometryEpsilon
Global epsilon to overcome small precision errors (Default=1e-5)
Definition: geometry.cpp:28
3D line, represented by a base point and a director vector.



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019