InterpolationDemo.hpp
Go to the documentation of this file.
1 /*
2  * InterpolationDemo.hpp
3  *
4  * Created on: Mar 16, 2020
5  * Author: Edo Jelavic
6  * Institute: ETH Zurich, Robotic Systems Lab
7  */
8 
9 #pragma once
10 
13 
14 #include <functional>
15 #include <map>
16 #include <string>
17 #include <chrono>
18 #include <ros/ros.h>
19 
20 namespace grid_map_demos {
21 
22 /*
23  * This demo visualizes the quality of function approximation
24  * using different interpolation algorithms. It creates an analytical function,
25  * which is sampled at discrete points. These discrete
26  * values are then used for interpolation in order to recover
27  * an approximation of the original function. The interpolation
28  * result is visualized and error measures are computed and
29  * printed in the console.
30  *
31  * The parameters are located in the "grid_map_demos/config/interpolation_demo.yaml"
32  * file.
33  *
34  * For more info refer to README.md in the grid_map folder.
35  *
36  * For more details on the algorithms used,
37  * refer to file: CubicInterpolation.hpp in the grid_map_core
38  * package.
39  *
40  * Options for which interpolation algorithm to use are defined in
41  * grid_map_core/include/grid_map_core/TypeDefs.hpp file.
42  *
43  * Interpolation is meant to be used with method "atPosition" defined in GridMap.hpp file.
44  * To see how to use different interpolation methods in your own code,
45  * take a look at definition of method "interpolateInputMap" in InterpolationDemo.cpp file.
46  *
47  */
48 
49 /*
50  * Different analytical functions used
51  * to evaluate the quality of interpolation approximation.
52  */
53 enum class Worlds: int {
54  Poly,
56  Tanh,
57  Sine,
59 };
60 
61 static const std::string demoLayer = "demo";
62 
64 {
65  std::function<double(double, double)> f_;
66 };
67 
68 struct Error {
69  double meanSquare_ = 0.0;
70  double meanAbs_ = 0.0;
71  double max_ = 0.0;
72 };
73 
74 /*
75  * Create map geometry and allocate memory
76  */
77 grid_map::GridMap createMap(const grid_map::Length &length, double resolution,
78  const grid_map::Position &pos);
79 
80 /*
81  * Fill grid map with values computed from analytical function
82  */
83 void fillGridMap(grid_map::GridMap *map, const AnalyticalFunctions &functions);
84 
85 /*
86  * Create various analytical functions
87  */
92 
93 
94 /*
95  * Create high resolution grid map, used for visualizing the ground truth,
96  * and create sparse grid map with data points that are used for the
97  * interpolation.
98  */
99 AnalyticalFunctions createWorld(Worlds world, double highResolution, double lowResolution,
100  double length, double width,
101  grid_map::GridMap *groundTruthHighRes,
102  grid_map::GridMap *groundTruthLowRes);
103 
104 /*
105  * Allocate memory and set geometry for the interpolated grid map
106  */
107 grid_map::GridMap createInterpolatedMapFromDataMap(const grid_map::GridMap &dataMap, double desiredResolution);
108 
109 /*
110  * Compute the interpolated values
111  */
112 void interpolateInputMap(const grid_map::GridMap &dataMap,
113  grid_map::InterpolationMethods interpolationMethod, grid_map::GridMap *inteprolatedMap);
114 
115 /*
116  * Compute error measures between the ground truth and the interpolated map
117  */
119  const grid_map::GridMap &map);
120 
121 static const std::map<std::string, Worlds> worlds = {
122  { "Sine", Worlds::Sine },
123  { "Poly", Worlds::Poly },
124  { "Gauss", Worlds::GaussMixture },
125  { "Tanh", Worlds::Tanh } };
126 
127 static const std::map<std::string, grid_map::InterpolationMethods> interpolationMethods = {
132 
133 /*
134  * Class that actually runs the demo
135  * publishes maps for visualization
136  * computes errors
137  * measures times required for computation
138  */
140 {
141 
142  public:
144 
145  private:
146  using clk = std::chrono::steady_clock;
147  using ErrorAndDuration = std::pair<Error, double>;
148  struct Statistic {
150  std::string world_;
151  std::string interpolationMethod_;
152  double duration_ = 0.0;
153  };
154  using Statistics = std::map<std::string, std::map<std::string, Statistic> >;
155 
156  void runDemo();
157  ErrorAndDuration interpolateAndComputeError(const std::string world, const std::string &method) const;
158  Statistics computeStatistics() const;
159  void printStatistics(const Statistics &stats) const;
160  void publishGridMaps() const;
161 
165 
169 
170  std::string world_;
171  std::string interpolationMethod_;
172  double groundTruthResolution_ = 0.02; // resolution in which ground truth is displayed in rviz
173  double dataResolution_ = 0.1; // resolution of the data points for the interpolating algorithm
174  double interpolatedResolution_ = 0.02; // resolution requested for the interpolated map
176  double worldWidth_ = 4.0;
177  double worldLength_ = 4.0;
178 
179 };
180 
181 } /* namespace grid_map_demos */
AnalyticalFunctions createPolyWorld(grid_map::GridMap *map)
static const std::string demoLayer
AnalyticalFunctions createSineWorld(grid_map::GridMap *map)
AnalyticalFunctions createWorld(Worlds world, double highResolution, double lowResolution, double length, double width, grid_map::GridMap *groundTruthHighRes, grid_map::GridMap *groundTruthLowRes)
std::map< std::string, std::map< std::string, Statistic > > Statistics
static const std::map< std::string, Worlds > worlds
std::pair< Error, double > ErrorAndDuration
InterpolationMethods
void interpolateInputMap(const grid_map::GridMap &dataMap, grid_map::InterpolationMethods interpolationMethod, grid_map::GridMap *inteprolatedMap)
AnalyticalFunctions createGaussianWorld(grid_map::GridMap *map)
AnalyticalFunctions createTanhWorld(grid_map::GridMap *map)
std::function< double(double, double)> f_
Eigen::Vector2d Position
void fillGridMap(grid_map::GridMap *map, const AnalyticalFunctions &functions)
Error computeInterpolationError(const AnalyticalFunctions &groundTruth, const grid_map::GridMap &map)
static const std::map< std::string, grid_map::InterpolationMethods > interpolationMethods
grid_map::GridMap createMap(const grid_map::Length &length, double resolution, const grid_map::Position &pos)
grid_map::GridMap createInterpolatedMapFromDataMap(const grid_map::GridMap &dataMap, double desiredResolution)
Eigen::Array2d Length


grid_map_demos
Author(s): Péter Fankhauser
autogenerated on Tue Jun 1 2021 02:13:55