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 
10 #include <mrpt/system/os.h>
11 #include <mrpt/system/datetime.h>
12 #include <mrpt/poses/CPose3D.h>
15 #include <mrpt/math/interp_fit.hpp>
16 #include <mrpt/math/utils.h>
17 #include <mrpt/random.h>
19 #include <iostream>
20 
21 using namespace mrpt::gui;
22 using namespace mrpt::random;
23 using namespace mrpt::math;
24 using namespace std;
25 
26 // ------------------------------------------------------
27 // TestCapture
28 // ------------------------------------------------------
30 {
32  mrpt::Clock::time_point ts = iniTs;
33  mrpt::Clock::time_point ots = iniTs;
34  mrpt::poses::CPose3D pose(0, 0, 0, 0, 0, 0);
36  std::vector<mrpt::poses::CPose3D> p;
37  mrpt::poses::CPose3D outPose(0, 0, 0, 0, 0, 0);
38  bool valid;
39 
40  FILE* f = mrpt::system::os::fopen("interpolation.txt", "wt");
41 
42  // Set the maximum value of the interval time for considering interpolation
43  poseInt.setMaxTimeInterpolation(std::chrono::seconds(1));
44 
45  poseInt.insert(ts, pose); // First point
46 
47  ts += std::chrono::seconds(1);
48  pose.setFromValues(1, 1, 0, 0, 1, 0);
49  poseInt.insert(ts, pose); // Second point
50 
51  ts += std::chrono::seconds(1);
52  pose.setFromValues(2, 2.5, 1, 0, 1.3, 0);
53  poseInt.insert(ts, pose); // Third point
54 
55  ts += std::chrono::seconds(1);
56  pose.setFromValues(3, 1.7, 2, 0, 1.57, 0);
57  poseInt.insert(ts, pose); // Fourth point
58 
59  unsigned int i;
60  for (i = 0, ots = iniTs;
61  ots <= iniTs + std::chrono::seconds(3);
62  ots += std::chrono::milliseconds(100), i++)
63  {
64  poseInt.interpolate(ots, outPose, valid);
65  p.push_back(outPose);
67  f, "%d %f %f %f %f %f %f\n", i, outPose.x(), outPose.y(),
68  outPose.z(), outPose.yaw(), outPose.pitch(), outPose.roll());
69  }
70 
72 
73 } // end TestCPose3DInterpolation
74 
76 {
77  FILE* f = mrpt::system::os::fopen("out2", "wt");
78 
79  CVectorDouble x, y;
80  double t;
81  x.resize(4);
82  y.resize(4);
83 
84  // x[0] = 0.0; x[1] = 1.0; x[2] = 2.0; x[3] = 4.0;
85  // y[0] = 0.0; y[1] = 1.0; y[2] = 1.8; y[3] = 1.6;
86 
87  x[0] = -1.5;
88  x[1] = -0.5;
89  x[2] = 0.5;
90  x[3] = 1.5;
91  y[0] = 3.14;
92  y[1] = -3.10;
93  y[2] = -3.05;
94  y[3] = 3.10;
95 
96  CVectorDouble ts, ys_interp;
97 
98  for (t = x[1]; t <= x[2]; t += 0.01)
99  {
100  double w = mrpt::math::spline(t, x, y); // wrap no
101  // double w = mrpt::math::spline(t, x, y, true); // wrap yes
102  ts.push_back(t);
103  ys_interp.push_back(w);
104  mrpt::system::os::fprintf(f, "%f %f\n", t, w);
105  }
106 
108  cout << "Done" << endl;
109 
110 #if MRPT_HAS_WXWIDGETS
111  CDisplayWindowPlots figure("Interpolation results");
112  figure.plot(x, y, "r.4", "true points");
113  figure.plot(ts, ys_interp, "b", "interp");
114 
115  figure.axis_equal();
116  figure.axis_fit();
117 
118  figure.waitForKey();
119 #endif
120 }
121 
123 {
124  const size_t N = 15;
125  CVectorDouble data_x, data_y(N);
126 
127  // Create random data:
128  mrpt::math::linspace(-20.0, 20.0, N, data_x);
129  getRandomGenerator().drawGaussian1DVector(data_y, 2.0, 1.0);
130 
131  // Create interpolator
133 
134  // Generate sequence of where to interpolate:
135  CVectorDouble xs;
136  mrpt::math::linspace(-20.0, 20.0, 500, xs);
137 
138  CVectorDouble ys;
139  bool valid = interp.queryVector(xs, ys);
140  ASSERT_(valid);
141 
142 #if MRPT_HAS_WXWIDGETS
143  CDisplayWindowPlots figure("Interpolation results");
144  figure.plot(data_x, data_y, "r.6", "true points");
145  figure.plot(xs, ys, "b", "interp");
146 
147  figure.axis_equal();
148  figure.axis_fit();
149 
150  figure.waitForKey();
151 #endif
152 }
153 
154 int main(int argc, char** argv)
155 {
156  try
157  {
159  // TestSplineInterpolation();
160  // TestCPose3DInterpolation();
161 
162  return 0;
163  }
164  catch (std::exception& e)
165  {
166  std::cout << "MRPT exception caught: " << e.what() << std::endl;
167  return -1;
168  }
169  catch (...)
170  {
171  printf("Another exception!!");
172  return -1;
173  }
174 }
A namespace of pseudo-random numbers generators of diferent distributions.
GLdouble GLdouble t
Definition: glext.h:3689
std::chrono::time_point< Clock > time_point
Definition: Clock.h:26
Create a GUI window and display plots with MATLAB-like interfaces and commands.
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:273
void drawGaussian1DVector(VEC &v, const double mean=0, const double std=1)
Fills the given vector with independent, 1D-normally distributed samples.
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:44
STL namespace.
static time_point now() noexcept
Returns the current time, with the highest resolution available.
Definition: Clock.cpp:46
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
void TestSplineInterpolation()
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
A (persistent) sequence of (x,y) coordinates, allowing queries of intermediate points through spline ...
This base provides a set of functions for maths stuff.
void linspace(T first, T last, size_t count, VECTOR &out_vector)
Generates an equidistant sequence of numbers given the first one, the last one and the desired number...
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:406
void setMaxTimeInterpolation(const mrpt::Clock::duration &time)
Set value of the maximum time to consider interpolation.
pose_t & interpolate(const mrpt::Clock::time_point &t, pose_t &out_interp, bool &out_valid_interp) const
Returns the pose at a given time, or interpolates using splines if there is not an exact match...
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:86
NUMTYPE spline(const NUMTYPE t, const VECTORLIKE &x, const VECTORLIKE &y, bool wrap2pi=false)
Interpolates the value of a function in a point "t" given 4 SORTED points where "t" is between the tw...
Definition: interp_fit.hpp:34
void TestSplineInterpolationVector()
GLenum GLint GLint y
Definition: glext.h:3538
This class stores a time-stamped trajectory in SE(3) (CPose3D poses).
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:255
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
void insert(const mrpt::Clock::time_point &t, const pose_t &p)
Inserts a new pose in the sequence.
GLuint interp
Definition: glext.h:7133
GLenum GLint x
Definition: glext.h:3538
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
GLfloat GLfloat p
Definition: glext.h:6305
void TestCPose3DInterpolation()



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020