Main MRPT website > C++ reference for MRPT 1.9.9
CGeneralizedEllipsoidTemplate.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 
13 #include <mrpt/opengl/gl_utils.h>
14 #include "opengl_internals.h"
15 
16 using namespace mrpt;
17 using namespace mrpt::opengl;
18 using namespace mrpt::utils;
19 using namespace mrpt::math;
20 using namespace std;
21 
22 /*---------------------------------------------------------------
23  Render: 2D implementation
24  ---------------------------------------------------------------*/
25 namespace mrpt
26 {
27 namespace opengl
28 {
29 namespace detail
30 {
31 template <>
33  const std::vector<mrpt::math::CMatrixFixedNumeric<float, 2, 1>>& pts,
34  const float lineWidth, const uint32_t slices, const uint32_t stacks)
35 {
36 #if MRPT_HAS_OPENGL_GLUT
41  glLineWidth(lineWidth);
43 
44  glDisable(GL_LIGHTING); // Disable lights when drawing lines
45 
47  const size_t N = pts.size();
48  for (size_t i = 0; i < N; i++) glVertex2f(pts[i][0], pts[i][1]);
49 
50  glEnd();
51 
54 #else
55  MRPT_UNUSED_PARAM(pts);
56  MRPT_UNUSED_PARAM(lineWidth);
57  MRPT_UNUSED_PARAM(slices);
58  MRPT_UNUSED_PARAM(stacks);
59 #endif
60 }
61 
62 /*---------------------------------------------------------------
63  Render: 3D implementation
64  ---------------------------------------------------------------*/
65 template <>
67  const std::vector<mrpt::math::CMatrixFixedNumeric<float, 3, 1>>& pts,
68  const float lineWidth, const uint32_t slices, const uint32_t stacks)
69 {
70 #if MRPT_HAS_OPENGL_GLUT
75  glLineWidth(lineWidth);
77  glDisable(GL_LIGHTING); // Disable lights when drawing lines
78 
79  // Points in the ellipsoid:
80  // * "#slices" slices, with "#stacks" points each, but for the two ends
81  // * 1 point at each end slice
82  // #total points = stacks*(slices-2) + 2
83  ASSERT_EQUAL_((slices - 2) * stacks + 2, pts.size())
84 
85  const size_t idx_1st_slice = 1;
86 
87  // 1st slice: triangle fan (if it were solid)
88  // ----------------------------
90  for (size_t i = 0; i < stacks; i++)
91  {
92  glVertex3fv(&pts[0][0]);
93  glVertex3fv(&pts[idx_1st_slice + i][0]);
94  }
95  glEnd();
96 
97  // Middle slices: triangle strip (if it were solid)
98  // ----------------------------
99  for (size_t s = 0; s < slices - 3; s++)
100  {
101  size_t idx_this_slice = idx_1st_slice + stacks * s;
102  size_t idx_next_slice = idx_this_slice + stacks;
103 
104  for (size_t i = 0; i < stacks; i++)
105  {
106  const size_t ii =
107  (i == (stacks - 1) ? 0 : i + 1); // next i with wrapping
108 
110  glVertex3fv(&pts[idx_this_slice + i][0]);
111  glVertex3fv(&pts[idx_next_slice + ii][0]);
112  glVertex3fv(&pts[idx_next_slice + i][0]);
113  glVertex3fv(&pts[idx_this_slice + i][0]);
114  glVertex3fv(&pts[idx_this_slice + ii][0]);
115  glVertex3fv(&pts[idx_next_slice + ii][0]);
116  glEnd();
117  }
118  }
119 
120  // Last slice: triangle fan (if it were solid)
121  // ----------------------------
122  const size_t idx_last_pt = pts.size() - 1;
123  const size_t idx_last_slice = idx_1st_slice + (slices - 3) * stacks;
124  glBegin(GL_LINES);
125  for (size_t i = 0; i < stacks; i++)
126  {
127  glVertex3fv(&pts[idx_last_pt][0]);
128  glVertex3fv(&pts[idx_last_slice + i][0]);
129  }
130  glEnd();
131 
132  // glBegin( GL_POINTS );
133  // const size_t N = pts.size();
134  // for (size_t i=0;i<N;i++)
135  // glVertex3f( pts[i][0], pts[i][1], pts[i][2] );
136  // glEnd();
137 
140 #else
141  MRPT_UNUSED_PARAM(pts);
142  MRPT_UNUSED_PARAM(lineWidth);
143  MRPT_UNUSED_PARAM(slices);
144  MRPT_UNUSED_PARAM(stacks);
145 #endif
146 }
147 
148 /*---------------------------------------------------------------
149  generalizedEllipsoidPoints: 2D
150  ---------------------------------------------------------------*/
151 template <>
155  std::vector<mrpt::math::CMatrixFixedNumeric<float, 2, 1>>& out_params_pts,
156  const uint32_t numSegments, const uint32_t numSegments_unused)
157 {
158  MRPT_UNUSED_PARAM(numSegments_unused);
159  out_params_pts.clear();
160  out_params_pts.reserve(numSegments);
161  const double Aa = 2 * M_PI / numSegments;
162  for (double ang = 0; ang < 2 * M_PI; ang += Aa)
163  {
164  const double ccos = cos(ang);
165  const double ssin = sin(ang);
166 
167  out_params_pts.resize(out_params_pts.size() + 1);
168 
169  Eigen::Matrix<float, 2, 1>& pt = out_params_pts.back();
170 
171  pt[0] = mean[0] + ccos * U.get_unsafe(0, 0) + ssin * U.get_unsafe(0, 1);
172  pt[1] = mean[1] + ccos * U.get_unsafe(1, 0) + ssin * U.get_unsafe(1, 1);
173  }
174 }
175 
177  const double x, const double y, const double z,
181 {
182  pts.resize(pts.size() + 1);
184  pt[0] = mean[0] + x * M.get_unsafe(0, 0) + y * M.get_unsafe(0, 1) +
185  z * M.get_unsafe(0, 2);
186  pt[1] = mean[1] + x * M.get_unsafe(1, 0) + y * M.get_unsafe(1, 1) +
187  z * M.get_unsafe(1, 2);
188  pt[2] = mean[2] + x * M.get_unsafe(2, 0) + y * M.get_unsafe(2, 1) +
189  z * M.get_unsafe(2, 2);
190 }
191 
192 /*---------------------------------------------------------------
193  generalizedEllipsoidPoints: 3D
194  ---------------------------------------------------------------*/
195 template <>
199  std::vector<mrpt::math::CMatrixFixedNumeric<float, 3, 1>>& pts,
200  const uint32_t slices, const uint32_t stacks)
201 {
202  MRPT_START
203  ASSERT_ABOVEEQ_(slices, 3)
204  ASSERT_ABOVEEQ_(stacks, 3)
205 
206  // sin/cos cache --------
207  // Slices: [0,pi]
208  std::vector<double> slice_cos(slices), slice_sin(slices);
209  for (uint32_t i = 0; i < slices; i++)
210  {
211  double angle = M_PI * i / double(slices - 1);
212  slice_sin[i] = sin(angle);
213  slice_cos[i] = cos(angle);
214  }
215  // Stacks: [0,2*pi]
216  std::vector<double> stack_sin(stacks), stack_cos(stacks);
217  for (uint32_t i = 0; i < stacks; i++)
218  {
219  double angle = 2 * M_PI * i / double(stacks);
220  stack_sin[i] = sin(angle);
221  stack_cos[i] = cos(angle);
222  }
223 
224  // Points in the ellipsoid:
225  // * "#slices" slices, with "#stacks" points each, but for the two ends
226  // * 1 point at each end slice
227  // #total points = stacks*(slices-2) + 2
228  pts.clear();
229  pts.reserve((slices - 2) * stacks + 2);
230 
231  for (uint32_t i = 0; i < slices; i++)
232  {
233  if (i == 0)
234  aux_add3DpointWithEigenVectors(1, 0, 0, pts, U, mean);
235  else if (i == (slices - 1))
236  aux_add3DpointWithEigenVectors(-1, 0, 0, pts, U, mean);
237  else
238  {
239  const double x = slice_cos[i];
240  const double R = slice_sin[i];
241 
242  for (uint32_t j = 0; j < stacks; j++)
243  {
244  const double y = R * stack_cos[j];
245  const double z = R * stack_sin[j];
247  }
248  }
249  }
250 
251  MRPT_END
252 }
253 }
254 }
255 } // end namespaces
#define ASSERT_EQUAL_(__A, __B)
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 glVertex3fv(const GLfloat *v)
GLAPI void GLAPIENTRY glEnable(GLenum cap)
STL namespace.
#define M_PI
Definition: bits.h:92
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:287
GLdouble s
Definition: glext.h:3676
#define GL_LIGHTING
Definition: glew.h:385
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
A numeric matrix of compile-time fixed size.
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
void aux_add3DpointWithEigenVectors(const double x, const double y, const double z, std::vector< mrpt::math::CMatrixFixedNumeric< float, 3, 1 >> &pts, const mrpt::math::CMatrixFixedNumeric< double, 3, 3 > &M, const mrpt::math::CMatrixFixedNumeric< double, 3, 1 > &mean)
GLAPI void GLAPIENTRY glBegin(GLenum mode)
void renderGeneralizedEllipsoidTemplate< 3 >(const std::vector< mrpt::math::CMatrixFixedNumeric< float, 3, 1 >> &pts, const float lineWidth, const uint32_t slices, const uint32_t stacks)
#define GL_BLEND
Definition: glew.h:432
#define GL_LINE_LOOP
Definition: glew.h:274
#define MRPT_START
#define GL_SRC_ALPHA
Definition: glew.h:286
void renderGeneralizedEllipsoidTemplate< 2 >(const std::vector< mrpt::math::CMatrixFixedNumeric< float, 2, 1 >> &pts, const float lineWidth, const uint32_t slices, const uint32_t stacks)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define ASSERT_ABOVEEQ_(__A, __B)
const float R
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:140
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
GLAPI void GLAPIENTRY glEnd(void)
GLenum GLint GLint y
Definition: glext.h:3538
#define GL_LINE_STRIP
Definition: glew.h:275
#define GL_LINES
Definition: glew.h:273
void generalizedEllipsoidPoints< 2 >(const mrpt::math::CMatrixFixedNumeric< double, 2, 2 > &U, const mrpt::math::CMatrixFixedNumeric< double, 2, 1 > &mean, std::vector< mrpt::math::CMatrixFixedNumeric< float, 2, 1 >> &out_params_pts, const uint32_t slices, const uint32_t stacks)
void generalizedEllipsoidPoints< 3 >(const mrpt::math::CMatrixFixedNumeric< double, 3, 3 > &U, const mrpt::math::CMatrixFixedNumeric< double, 3, 1 > &mean, std::vector< mrpt::math::CMatrixFixedNumeric< float, 3, 1 >> &out_params_pts, const uint32_t slices, const uint32_t stacks)
GLenum GLint x
Definition: glext.h:3538
unsigned __int32 uint32_t
Definition: rptypes.h:47
GLAPI void GLAPIENTRY glVertex2f(GLfloat x, GLfloat y)
GLAPI void GLAPIENTRY glDisable(GLenum cap)
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.



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