Main MRPT website > C++ reference for MRPT 1.9.9
CGeneralizedEllipsoidTemplate.h
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 #ifndef opengl_CGeneralizedEllipsoidTemplate_H
10 #define opengl_CGeneralizedEllipsoidTemplate_H
11 
14 #include <mrpt/utils/types_math.h>
15 #include <mrpt/utils/CStream.h> // for >> ops
16 #include <mrpt/math/matrix_serialization.h> // for >> ops
17 
18 namespace mrpt
19 {
20 namespace opengl
21 {
22 namespace detail
23 {
24 template <int DIM>
26  const std::vector<mrpt::math::CMatrixFixedNumeric<float, DIM, 1>>& pts,
27  const float lineWidth, const uint32_t slices, const uint32_t stacks);
28 template <>
30  const std::vector<mrpt::math::CMatrixFixedNumeric<float, 2, 1>>& pts,
31  const float lineWidth, const uint32_t slices, const uint32_t stacks);
32 template <>
34  const std::vector<mrpt::math::CMatrixFixedNumeric<float, 3, 1>>& pts,
35  const float lineWidth, const uint32_t slices, const uint32_t stacks);
36 
37 template <int DIM>
41  std::vector<mrpt::math::CMatrixFixedNumeric<float, DIM, 1>>& out_params_pts,
42  const uint32_t slices, const uint32_t stacks);
43 template <>
47  std::vector<mrpt::math::CMatrixFixedNumeric<float, 2, 1>>& out_params_pts,
48  const uint32_t slices, const uint32_t stacks);
49 template <>
53  std::vector<mrpt::math::CMatrixFixedNumeric<float, 3, 1>>& out_params_pts,
54  const uint32_t slices, const uint32_t stacks);
55 }
56 
57 /** A class that generalizes the concept of an ellipsoid to arbitrary
58  * parameterizations of
59  * uncertainty shapes in either 2D or 3D. See derived classes for examples.
60  *
61  * Please read the documentation of
62  * CGeneralizedEllipsoidTemplate::setQuantiles() for learning
63  * the mathematical details about setting the desired confidence interval.
64  *
65  * The main method to set the modeled uncertainty is \a setCovMatrixAndMean()
66  *
67  * \tparam DIM The dimensionality of the parameter space, which must coincide
68  * with that of the rendering space (2 or 3)
69  *
70  * \ingroup mrpt_opengl_grp
71  */
72 template <int DIM>
74 {
75  public:
76  /** The type of fixed-size covariance matrices for this representation */
78  /** The type of fixed-size vector for this representation */
80 
83 
84  /** Set the NxN covariance matrix that will determine the aspect of the
85  * ellipsoid - Notice that the
86  * covariance determines the uncertainty in the parameter space, which
87  * would be transformed by derived function
88  */
89  template <typename MATRIX, typename VECTOR>
90  void setCovMatrixAndMean(const MATRIX& new_cov, const VECTOR& new_mean)
91  {
93  ASSERT_(
94  new_cov.getColCount() == new_cov.getRowCount() &&
95  new_cov.getColCount() == DIM)
96  m_cov = new_cov;
97  m_mean = new_mean;
100  MRPT_END
101  }
102 
103  /** Gets the current uncertainty covariance of parameter space */
104  const cov_matrix_t& getCovMatrix() const { return m_cov; }
105  /** Changes the scale of the "sigmas" for drawing the ellipse/ellipsoid
106  *(default=3, ~97 or ~98% CI); the exact mathematical meaning is:
107  * This value of "quantiles" \a q should be set to the square root of the
108  *chi-squared inverse cdf corresponding to
109  * the desired confidence interval.
110  * <b>Note that this value depends on the dimensionality</b>.
111  * Refer to the MATLAB functions \a chi2inv() and \a chi2cdf().
112  *
113  * Some common values follow here for the convenience of users:
114  * - Dimensionality=3 (3D ellipsoids):
115  * - 19.8748% CI -> q=1
116  * - 73.8536% CI -> q=2
117  * - 97.0709% CI -> q=3
118  * - 99.8866% CI -> q=4
119  * - Dimensionality=2 (2D ellipses):
120  * - 39.347% CI -> q=1
121  * - 86.466% CI -> q=2
122  * - 98.8891% CI -> q=3
123  * - 99.9664% CI -> q=4
124  * - Dimensionality=1 (Not aplicable to this class but provided for
125  *reference):
126  * - 68.27% CI -> q=1
127  * - 95.45% CI -> q=2
128  * - 99.73% CI -> q=3
129  * - 99.9937% CI -> q=4
130  *
131  */
132  void setQuantiles(float q)
133  {
134  m_quantiles = q;
136  }
137  /** Refer to documentation of \a setQuantiles() */
138  float getQuantiles() const { return m_quantiles; }
139  /** The line width for 2D ellipses or 3D wireframe ellipsoids (default=1) */
140  void setLineWidth(float w)
141  {
142  m_lineWidth = w;
144  }
145  float getLineWidth() const { return m_lineWidth; }
146  /** Set the number of segments of the surface/curve (higher means with
147  * greater resolution) */
148  void setNumberOfSegments(const uint32_t numSegments)
149  {
150  m_numSegments = numSegments;
152  }
154  /** Render
155  * If one of the eigen value of the covariance matrix of the ellipsoid is
156  *null, ellipsoid will not
157  * be rendered to ensure stability in the rendering process.
158  */
159  void render_dl() const override
160  {
161  MRPT_START
162  // 1) Update eigenvectors/values:
164  {
166  // Handle the special case of an ellipsoid of volume = 0
167  const double d = m_cov.det();
168  if (fabs(d) < 1e-20 || d != d) // Note: "d!=d" is a great test for
169  // invalid numbers, don't remove!
170  {
171  // All zeros:
172  m_U.setZero(DIM, DIM);
173  }
174  else
175  {
176  // Not null matrix:
177  m_cov.chol(m_U);
178  }
179  }
180 
181  // Only if all the eigenvalues are !=0
182  bool eig_ok = true;
183  for (int i = 0; i < DIM; i++)
184  if (m_U.coeff(i, i) == 0) eig_ok = false;
185 
186  if (eig_ok)
187  {
188  // 2) Generate "standard" ellipsoid:
189  std::vector<array_parameter_t> params_pts;
190  const cov_matrix_t Uscaled = static_cast<double>(m_quantiles) * m_U;
191  detail::generalizedEllipsoidPoints<DIM>(
192  Uscaled, m_mean, params_pts, m_numSegments, m_numSegments);
193 
194  // 3) Transform into 2D/3D render space:
195  std::vector<array_point_t> render_pts;
196  this->transformFromParameterSpace(params_pts, render_pts);
197 
198  // 3.5) Save bounding box:
200  std::numeric_limits<double>::max(),
201  std::numeric_limits<double>::max(), 0);
203  -std::numeric_limits<double>::max(),
204  -std::numeric_limits<double>::max(), 0);
205  for (size_t i = 0; i < render_pts.size(); i++)
206  for (int k = 0; k < DIM; k++)
207  {
208  mrpt::utils::keep_min(m_bb_min[k], render_pts[i][k]);
209  mrpt::utils::keep_max(m_bb_max[k], render_pts[i][k]);
210  }
211  // Convert to coordinates of my parent:
214 
215  // 4) Render them:
216  mrpt::opengl::detail::renderGeneralizedEllipsoidTemplate<DIM>(
217  render_pts, m_lineWidth, m_numSegments, m_numSegments);
218  }
219 
220  MRPT_END
221  }
222 
223  /** Evaluates the bounding box of this object (including possible children)
224  * in the coordinate frame of the object parent. */
225  virtual void getBoundingBox(
226  mrpt::math::TPoint3D& bb_min,
227  mrpt::math::TPoint3D& bb_max) const override
228  {
229  bb_min = m_bb_min;
230  bb_max = m_bb_max;
231  }
232 
233  /** Ray tracing
234  */
235  virtual bool traceRay(
236  const mrpt::poses::CPose3D& o, double& dist) const override
237  {
239  MRPT_UNUSED_PARAM(dist);
240  THROW_EXCEPTION("Not implemented ")
241  }
242 
243  protected:
244  /** To be implemented by derived classes: maps, using some arbitrary space
245  * transformation, a list of points
246  * defining an ellipsoid in parameter space into their corresponding
247  * points in 2D/3D space.
248  */
249  virtual void transformFromParameterSpace(
250  const std::vector<array_point_t>& params_pts,
251  std::vector<array_point_t>& out_pts) const = 0;
252 
256  /** The number of "sigmas" for drawing the ellipse/ellipsoid (default=3) */
257  float m_quantiles;
258  /** The line width for 2D ellipses or 3D wireframe ellipsoids (default=1) */
259  float m_lineWidth;
260  /** Number of segments in 2D/3D ellipsoids (default=10) */
263 
264  /** Cholesky U triangular matrix cache. */
265  mutable cov_matrix_t m_U;
266 
268  {
269  using namespace mrpt::math;
270  using namespace mrpt::utils;
271  const uint8_t version = 0;
272  out << version << m_cov << m_mean << m_quantiles << m_lineWidth
273  << m_numSegments;
274  }
276  {
277  uint8_t version;
278  in >> version;
279  switch (version)
280  {
281  case 0:
282  {
283  in >> m_cov >> m_mean >> m_quantiles >> m_lineWidth >>
286  }
287  break;
288  default:
290  };
292  }
293 
296  m_quantiles(3.f),
297  m_lineWidth(1.f),
298  m_numSegments(50),
299  m_bb_min(0, 0, 0),
300  m_bb_max(0, 0, 0)
301  {
302  }
304 };
305 
306 } // end namespace
307 
308 } // End of namespace
309 
310 #endif
mrpt::math::CMatrixFixedNumeric< float, DIM, 1 > array_point_t
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
float m_lineWidth
The line width for 2D ellipses or 3D wireframe ellipsoids (default=1)
void setNumberOfSegments(const uint32_t numSegments)
Set the number of segments of the surface/curve (higher means with greater resolution) ...
float m_quantiles
The number of "sigmas" for drawing the ellipse/ellipsoid (default=3)
#define THROW_EXCEPTION(msg)
GLdouble GLdouble GLdouble GLdouble q
Definition: glext.h:3721
void generalizedEllipsoidPoints(const mrpt::math::CMatrixFixedNumeric< double, DIM, DIM > &U, const mrpt::math::CMatrixFixedNumeric< double, DIM, 1 > &mean, std::vector< mrpt::math::CMatrixFixedNumeric< float, DIM, 1 >> &out_params_pts, const uint32_t slices, const uint32_t stacks)
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
virtual 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...
float getQuantiles() const
Refer to documentation of setQuantiles()
uint32_t m_numSegments
Number of segments in 2D/3D ellipsoids (default=10)
mrpt::poses::CPose3D m_pose
6D pose wrt the parent coordinate reference.
Definition: CRenderizable.h:57
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
void setCovMatrixAndMean(const MATRIX &new_cov, const VECTOR &new_mean)
Set the NxN covariance matrix that will determine the aspect of the ellipsoid - Notice that the covar...
unsigned char uint8_t
Definition: rptypes.h:41
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
A numeric matrix of compile-time fixed size.
void thisclass_writeToStream(mrpt::utils::CStream &out) const
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.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
void renderGeneralizedEllipsoidTemplate(const std::vector< mrpt::math::CMatrixFixedNumeric< float, DIM, 1 >> &pts, const float lineWidth, const uint32_t slices, const uint32_t stacks)
void setLineWidth(float w)
The line width for 2D ellipses or 3D wireframe ellipsoids (default=1)
void render_dl() const override
Render If one of the eigen value of the covariance matrix of the ellipsoid is null, ellipsoid will not be rendered to ensure stability in the rendering process.
void renderGeneralizedEllipsoidTemplate< 3 >(const std::vector< mrpt::math::CMatrixFixedNumeric< float, 3, 1 >> &pts, const float lineWidth, const uint32_t slices, const uint32_t stacks)
A class that generalizes the concept of an ellipsoid to arbitrary parameterizations of uncertainty sh...
#define MRPT_START
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.
virtual void transformFromParameterSpace(const std::vector< array_point_t > &params_pts, std::vector< array_point_t > &out_pts) const =0
To be implemented by derived classes: maps, using some arbitrary space transformation, a list of points defining an ellipsoid in parameter space into their corresponding points in 2D/3D space.
mrpt::math::CMatrixFixedNumeric< double, DIM, DIM > cov_matrix_t
The type of fixed-size covariance matrices for this representation.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
This file implements matrix/vector text and binary serialization.
void composePoint(double lx, double ly, double lz, double &gx, double &gy, double &gz, mrpt::math::CMatrixFixedNumeric< double, 3, 3 > *out_jacobian_df_dpoint=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dpose=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dse3=nullptr, bool use_small_rot_approx=false) const
An alternative, slightly more efficient way of doing with G and L being 3D points and P this 6D pose...
Definition: CPose3D.cpp:453
const cov_matrix_t & getCovMatrix() const
Gets the current uncertainty covariance of parameter space.
GLuint in
Definition: glext.h:7274
void setQuantiles(float q)
Changes the scale of the "sigmas" for drawing the ellipse/ellipsoid (default=3, ~97 or ~98% CI); the ...
#define ASSERT_(f)
virtual bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const override
Ray tracing.
cov_matrix_t m_U
Cholesky U triangular matrix cache.
mrpt::math::CMatrixFixedNumeric< double, DIM, 1 > mean_vector_t
The type of fixed-size vector for this representation.
mrpt::math::CMatrixFixedNumeric< float, DIM, 1 > array_parameter_t
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)
Lightweight 3D point.
unsigned __int32 uint32_t
Definition: rptypes.h:47
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
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