Main MRPT website > C++ reference for MRPT 1.9.9
test.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 
12 #include <mrpt/math/utils.h>
13 #include <mrpt/system/CTicTac.h>
14 #include <mrpt/poses/CPose3D.h>
16 #include <mrpt/poses/CPose3DQuat.h>
21 #include <mrpt/opengl/CEllipsoid.h>
22 #include <iostream>
23 
24 using namespace mrpt;
25 using namespace mrpt::math;
26 using namespace mrpt::poses;
27 using namespace mrpt::system;
28 using namespace std;
29 
30 // Example non-linear function for SUT
31 // f: R^5 => R^3
32 void myFun1(
33  const CArrayDouble<5>& x, const double& user_param, CArrayDouble<3>& y)
34 {
35  y[0] = cos(x[0]) * exp(x[1]) + x[4];
36  y[1] = x[1] / (1 + square(x[0]));
37  y[2] = x[4] / (1 + square(x[3])) + sin(x[1] * x[0]);
38 }
39 
40 /* ------------------------------------------------------------------------
41  Test_SUT: Scaled Unscented Transform
42  ------------------------------------------------------------------------ */
43 void Test_SUT()
44 {
45  // Inputs:
46  const double x0[] = {1.8, 0.7, 0.9, -5.6, 8.9};
47  const double x0cov[] = {
48  0.049400, 0.011403, -0.006389, 0.008132, -0.008595,
49  0.011403, 0.026432, 0.005382, 0.008622, -0.017399,
50  -0.006389, 0.005382, 0.063268, -0.019310, -0.017868,
51  0.008132, 0.008622, -0.019310, 0.028474, 0.003507,
52  -0.008595, -0.017399, -0.017868, 0.003507, 0.17398};
53 
54  const CArrayDouble<5> x_mean(x0);
55  const CMatrixFixedNumeric<double, 5, 5> x_cov(x0cov);
56  const double dumm = 0;
57 
58  // Outputs:
59  CArrayDouble<3> y_mean;
60  CMatrixDouble33 y_cov;
61 
62  // Do SUT:
63  CTicTac tictac;
64  size_t N = 10000;
65 
66  tictac.Tic();
67  for (size_t i = 0; i < N; i++)
69  x_mean, x_cov, myFun1,
70  dumm, // fixed parameter: not used in this example
71  y_mean, y_cov);
72 
73  cout << "SUT: Time (ms): " << 1e3 * tictac.Tac() / N << endl;
74 
75  // Print:
76  cout << " ======= Scaled Unscented Transform ======== " << endl;
77  cout << "y_mean: " << y_mean << endl;
78  cout << "y_cov: " << endl << y_cov << endl << endl;
79 
80  // 3D view:
82  mrpt::make_aligned_shared<mrpt::opengl::COpenGLScene>();
83  scene->insert(
84  mrpt::make_aligned_shared<opengl::CGridPlaneXY>(
85  -10, 10, -10, 10, 0, 1));
86 
87  {
89  mrpt::make_aligned_shared<opengl::CEllipsoid>();
90  el->enableDrawSolid3D(false);
91  el->setLocation(y_mean[0], y_mean[1], y_mean[2]);
92  el->setCovMatrix(y_cov);
93  el->setColor(0, 0, 1);
94  scene->insert(el);
95  }
96 
97  // Do Montecarlo for comparison:
98  N = 10;
99 
101 
102  tictac.Tic();
103  for (size_t i = 0; i < N; i++)
105  x_mean, x_cov, myFun1,
106  dumm, // fixed parameter: not used in this example
107  y_mean, y_cov,
108  5e5, // Samples
109  &MC_samples // we want the samples.
110  );
111 
112  cout << "MC: Time (ms): " << 1e3 * tictac.Tac() / N << endl;
113 
114  CVectorDouble MC_y[3];
115 
116  for (int i = 0; i < 3; i++)
117  extractColumnFromVectorOfVectors(i, MC_samples, MC_y[i]);
118 
119  {
121  mrpt::make_aligned_shared<opengl::CEllipsoid>();
122  el->enableDrawSolid3D(false);
123  el->setLocation(y_mean[0], y_mean[1], y_mean[2]);
124  el->setCovMatrix(y_cov);
125  el->setColor(0, 1, 0);
126  scene->insert(el);
127  }
128 
129  // Print:
130  cout << " ======= Montecarlo Transform ======== " << endl;
131  cout << "y_mean: " << y_mean << endl;
132  cout << "y_cov: " << endl << y_cov << endl;
133 
134  // Do Linear for comparison:
135  N = 100;
136 
137  CArrayDouble<5> x_incrs;
138  x_incrs.fill(1e-6);
139 
140  tictac.Tic();
141  for (size_t i = 0; i < N; i++)
143  x_mean, x_cov, myFun1,
144  dumm, // fixed parameter: not used in this example
145  y_mean, y_cov, x_incrs);
146 
147  cout << "LIN: Time (ms): " << 1e3 * tictac.Tac() / N << endl;
148 
149  // Print:
150  cout << " ======= Linear Transform ======== " << endl;
151  cout << "y_mean: " << y_mean << endl;
152  cout << "y_cov: " << endl << y_cov << endl;
153 
154  {
156  mrpt::make_aligned_shared<opengl::CEllipsoid>();
157  el->enableDrawSolid3D(false);
158  el->setLocation(y_mean[0], y_mean[1], y_mean[2]);
159  el->setCovMatrix(y_cov);
160  el->setColor(1, 0, 0);
161  scene->insert(el);
162  }
163 
165  "Comparison SUT (blue), Linear (red), MC (green)", 400, 300);
166  win.get3DSceneAndLock() = scene;
167  win.unlockAccess3DScene();
168 
169  win.setCameraPointingToPoint(y_mean[0], y_mean[1], y_mean[2]);
170  win.setCameraZoom(5.0);
171 
172  // MC-based histograms:
174 
175  for (int i = 0; i < 3; i++)
176  {
177  winHistos[i] =
178  mrpt::make_aligned_shared<mrpt::gui::CDisplayWindowPlots>(
179  format("MC-based histogram of the %i dim", i), 300, 150);
180 
181  std::vector<double> X;
182  std::vector<double> H = mrpt::math::histogram(
183  MC_y[i], MC_y[i].minimum(), MC_y[i].maximum(), 40, true, &X);
184 
185  winHistos[i]->plot(X, H, "b");
186  winHistos[i]->axis_fit();
187  }
188 
189  win.forceRepaint();
190 
191  cout << endl << "Press any key to exit" << endl;
192  win.waitForKey();
193 }
194 
195 // Calibration of SUT parameters for Quat -> 3D pose
196 // -----------------------------------------------------------
197 
199  const CArrayDouble<7>& x, const double& dummy, CArrayDouble<6>& y)
200 {
201  const CPose3DQuat p(
202  x[0], x[1], x[2],
203  mrpt::math::CQuaternionDouble(x[3], x[4], x[5], x[6]));
204  const CPose3D p2 = CPose3D(p);
205  for (int i = 0; i < 6; i++) y[i] = p2[i];
206  // cout << "p2: " << y[3] << endl;
207 }
208 
210 {
211  // Take a 7x7 representation:
213  o.mean = CPose3DQuat(
214  CPose3D(1.0, 2.0, 3.0, DEG2RAD(-30), DEG2RAD(10), DEG2RAD(60)));
215  // o.mean = CPose3D(1.0,2.0,3.0, DEG2RAD(00),DEG2RAD(90),DEG2RAD(0));
216 
219  v *= 1e-3;
220  o.cov.multiply_AAt(v); // COV = v*vt
221  for (int i = 0; i < 7; i++) o.cov(i, i) += 0.01;
222 
223  o.cov(0, 1) = o.cov(1, 0) = 0.007;
224 
225  cout << "p1quat: " << endl << o << endl;
226 
227  // Use UT transformation:
228  // f: R^7 => R^6
229  const CArrayDouble<7> x_mean(o.mean);
230  CArrayDouble<6> y_mean;
231  static const bool elements_do_wrapPI[6] = {
232  false, false, false, true, true, true}; // xyz yaw pitch roll
233 
234  static const double dummy = 0;
235  // MonteCarlo:
236  CArrayDouble<6> MC_y_mean;
237  CMatrixDouble66 MC_y_cov;
239  x_mean, o.cov, aux_posequat2poseypr,
240  dummy, // fixed parameter: not used in this example
241  MC_y_mean, MC_y_cov, 500);
242  cout << "MC: " << endl
243  << MC_y_mean << endl
244  << endl
245  << MC_y_cov << endl
246  << endl;
247 
248  // SUT:
249 
250  CPose3DPDFGaussian p_ypr;
251 
252  // double = 1e-3;
253  // alpha = x_mean.size()-3;
254 
256  x_mean, o.cov, aux_posequat2poseypr, dummy, y_mean, p_ypr.cov,
257  elements_do_wrapPI,
258  1e-3, // alpha
259  0, // K
260  2.0 // beta
261  );
262 
263  cout << "SUT: " << endl
264  << y_mean << endl
265  << endl
266  << p_ypr.cov << endl
267  << endl;
268 }
269 
270 // ------------------------------------------------------
271 // MAIN
272 // ------------------------------------------------------
273 int main(int argc, char** argv)
274 {
275  try
276  {
277  Test_SUT();
278 
279  // TestCalibrate_pose2quat();
280 
281  return 0;
282  }
283  catch (std::exception& e)
284  {
285  std::cout << "Exception: " << e.what() << std::endl;
286  return -1;
287  }
288  catch (...)
289  {
290  printf("Untyped exception!");
291  return -1;
292  }
293 }
mrpt::poses::CPose3DPDFGaussian
Declares a class that represents a Probability Density function (PDF) of a 3D pose .
Definition: CPose3DPDFGaussian.h:40
Test_SUT
void Test_SUT()
Definition: vision_stereo_rectify/test.cpp:43
mrpt::math::maximum
CONTAINER::Scalar maximum(const CONTAINER &v)
Definition: ops_containers.h:139
mrpt::gui::CDisplayWindowPlots::Ptr
std::shared_ptr< CDisplayWindowPlots > Ptr
Definition: CDisplayWindowPlots.h:34
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
mrpt::math::dynamic_vector
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction.
Definition: eigen_frwds.h:44
mrpt::math::histogram
std::vector< double > histogram(const CONTAINER &v, double limit_min, double limit_max, size_t number_bins, bool do_normalization=false, std::vector< double > *out_bin_centers=nullptr)
Computes the normalized or normal histogram of a sequence of numbers given the number of bins and the...
Definition: ops_containers.h:65
CPose3DPDFGaussian.h
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
CEllipsoid.h
mrpt::poses::CPose3DQuat
A class used to store a 3D pose as a translation (x,y,z) and a quaternion (qr,qx,qy,...
Definition: CPose3DQuat.h:48
p
GLfloat GLfloat p
Definition: glext.h:6305
mrpt::square
T square(const T x)
Inline function for the square of a number.
Definition: core/include/mrpt/core/bits_math.h:18
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
CDisplayWindow3D.h
mrpt::math::transform_gaussian_montecarlo
void transform_gaussian_montecarlo(const VECTORLIKE1 &x_mean, const MATLIKE1 &x_cov, void(*functor)(const VECTORLIKE1 &x, const USERPARAM &fixed_param, VECTORLIKE3 &y), const USERPARAM &fixed_param, VECTORLIKE2 &y_mean, MATLIKE2 &y_cov, const size_t num_samples=1000, mrpt::aligned_std_vector< VECTORLIKE3 > *out_samples_y=nullptr)
Simple Montecarlo-base estimation of the Gaussian distribution of a variable Y=f(X) for an arbitrary ...
Definition: transform_gaussian.h:114
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
v
const GLdouble * v
Definition: glext.h:3678
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::math::transform_gaussian_linear
void transform_gaussian_linear(const VECTORLIKE1 &x_mean, const MATLIKE1 &x_cov, void(*functor)(const VECTORLIKE1 &x, const USERPARAM &fixed_param, VECTORLIKE3 &y), const USERPARAM &fixed_param, VECTORLIKE2 &y_mean, MATLIKE2 &y_cov, const VECTORLIKE1 &x_increments)
First order uncertainty propagation estimator of the Gaussian distribution of a variable Y=f(X) for a...
Definition: transform_gaussian.h:150
mrpt::aligned_std_vector
std::vector< T, mrpt::aligned_allocator_cpp11< T > > aligned_std_vector
Definition: aligned_std_vector.h:15
TestCalibrate_pose2quat
void TestCalibrate_pose2quat()
Definition: vision_stereo_rectify/test.cpp:209
mrpt::poses::CPose3DQuatPDFGaussian::cov
mrpt::math::CMatrixDouble77 cov
The 7x7 covariance matrix.
Definition: CPose3DQuatPDFGaussian.h:82
myFun1
void myFun1(const CArrayDouble< 5 > &x, const double &user_param, CArrayDouble< 3 > &y)
Definition: vision_stereo_rectify/test.cpp:32
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
CPose3DQuat.h
mrpt::math::CArrayNumeric
CArrayNumeric is an array for numeric types supporting several mathematical operations (actually,...
Definition: CArrayNumeric.h:25
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
utils.h
mrpt::math::CMatrixFixedNumeric
A numeric matrix of compile-time fixed size.
Definition: CMatrixFixedNumeric.h:40
CPose3D.h
mrpt::math::CQuaternion
A quaternion, which can represent a 3D rotation as pair , with a real part "r" and a 3D vector ,...
Definition: CQuaternion.h:46
CPose3DQuatPDFGaussian.h
mrpt::poses::CPose3DQuatPDFGaussian::mean
CPose3DQuat mean
The mean value.
Definition: CPose3DQuatPDFGaussian.h:80
mrpt::random::getRandomGenerator
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
Definition: RandomGenerator.cpp:19
CTicTac.h
CArrayNumeric.h
mrpt::poses::CPose3DQuatPDFGaussian
Declares a class that represents a Probability Density function (PDF) of a 3D pose using a quaternion...
Definition: CPose3DQuatPDFGaussian.h:44
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
aux_posequat2poseypr
void aux_posequat2poseypr(const CArrayDouble< 7 > &x, const double &dummy, CArrayDouble< 6 > &y)
Definition: CPose3DPDFGaussian.cpp:106
mrpt::poses::CPose3DPDFGaussian::cov
mrpt::math::CMatrixDouble66 cov
The 6x6 covariance matrix.
Definition: CPose3DPDFGaussian.h:83
mrpt::opengl::COpenGLScene::Ptr
std::shared_ptr< COpenGLScene > Ptr
Definition: COpenGLScene.h:61
mrpt::math::transform_gaussian_unscented
void transform_gaussian_unscented(const VECTORLIKE1 &x_mean, const MATLIKE1 &x_cov, void(*functor)(const VECTORLIKE1 &x, const USERPARAM &fixed_param, VECTORLIKE3 &y), const USERPARAM &fixed_param, VECTORLIKE2 &y_mean, MATLIKE2 &y_cov, const bool *elem_do_wrap2pi=nullptr, const double alpha=1e-3, const double K=0, const double beta=2.0)
Scaled unscented transformation (SUT) for estimating the Gaussian distribution of a variable Y=f(X) f...
Definition: transform_gaussian.h:47
mrpt::opengl::CEllipsoid::Ptr
std::shared_ptr< CEllipsoid > Ptr
Definition: CEllipsoid.h:49
transform_gaussian.h
CDisplayWindowPlots.h
mrpt::math::minimum
CONTAINER::Scalar minimum(const CONTAINER &v)
Definition: ops_containers.h:144
mrpt::math::extractColumnFromVectorOfVectors
void extractColumnFromVectorOfVectors(const size_t colIndex, const VECTOR_OF_VECTORS &data, VECTORLIKE &out_column)
Extract a column from a vector of vectors, and store it in another vector.
Definition: math/include/mrpt/math/utils.h:142
CGridPlaneXY.h
y
GLenum GLint GLint y
Definition: glext.h:3538
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117
mrpt::random::CRandomGenerator::drawGaussian1DMatrix
void drawGaussian1DMatrix(MAT &matrix, const double mean=0, const double std=1)
Fills the given matrix with independent, 1D-normally distributed samples.
Definition: RandomGenerators.h:174
x
GLenum GLint x
Definition: glext.h:3538
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25
mrpt::DEG2RAD
double DEG2RAD(const double x)
Degrees to radians.
Definition: core/include/mrpt/core/bits_math.h:42



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST