MRPT  1.9.9
CRandomFieldGridMap3D.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
15 #include <mrpt/math/TPoint3D.h>
18 
19 // Fwdr decl:
20 class vtkStructuredGrid;
21 
22 namespace mrpt::maps
23 {
24 /** The contents of each voxel in a CRandomFieldGridMap3D map.
25  * \ingroup mrpt_maps_grp
26  **/
27 #if defined(MRPT_IS_X86_AMD64) // Pragma defined to ensure no structure
28 // packing: since we'll serialize
29 // TRandomFieldVoxel to streams, we want it not
30 // to depend on compiler options, etc.
31 #pragma pack(push, 1)
32 #endif
34 {
35  /** Mean and sigma (standard deviation) estimated values for the voxel. */
37 
38  /** Constructor */
39  TRandomFieldVoxel(double _mean_value = .0, double _stddev_value = .0)
40  : mean_value(_mean_value), stddev_value(_stddev_value)
41  {
42  }
43 };
44 #if defined(MRPT_IS_X86_AMD64)
45 #pragma pack(pop)
46 #endif
47 
48 /** CRandomFieldGridMap3D represents a 3D regular grid where each voxel is
49  * associated one real-valued property which is to be estimated by this class.
50  *
51  * This class implements a Gaussian Markov Random Field (GMRF) estimator, with
52  * each voxel being connected to its
53  * 6 immediate neighbors (Up, down, left, right, front, back).
54  * - See papers:
55  * - "Time-variant gas distribution mapping with obstacle information",
56  * Monroy, J. G., Blanco, J. L., & Gonzalez-Jimenez, J. Autonomous Robots,
57  * 40(1), 1-16, 2016.
58  *
59  * Note that this class does not derive from mrpt::maps::CMetricMap since the
60  * estimated values do not have sensor-especific semantics,
61  * i.e. the grid can be used to estimate temperature, gas concentration, etc.
62  *
63  * Usage:
64  * - Define grid size with either constructor or via `setSize()`.
65  * - Initialize the map with `initialize()`. This resets the contents of the
66  * map, so previously-added observations will be lost.
67  * - Add observations of 3D voxels with `insertIndividualReading()`
68  *
69  * Custom connectivity patterns can be defined with setVoxelsConnectivity().
70  *
71  * \sa mrpt::maps::CRandomFieldGridMap3D
72  * \ingroup mrpt_maps_grp
73  * \note [New in MRPT 1.5.0]
74  */
76  : public mrpt::containers::CDynamicGrid3D<TRandomFieldVoxel>,
79 {
81 
83  public:
84  /** [default:false] Enables a profiler to show a performance report at
85  * application end. */
86  static bool ENABLE_GMRF_PROFILER;
87 
88  /** Constructor.
89  * If you set call_initialize_now to false, the object will be initialized
90  * immediately (without the heavy initialization of the GMRF),
91  * but you then must call `setSize()` or `clear()` later to properly
92  * initialize the object before using it to insert observations.
93  */
95  double x_min = -2, double x_max = 2, double y_min = -2,
96  double y_max = 2, double z_min = -2, double z_max = 2,
97  double voxel_size = 0.5, bool call_initialize_now = true);
98 
99  /** Erases all added observations and start again with an empty gridmap. */
100  void clear() override;
101 
102  /** Save the current estimated mean values to a CSV file (compatible with
103  * Paraview) with fields `x y z mean_value`.
104  * Optionally, std deviations can be also saved to another file with fields
105  * `x y z stddev_value`, if `filName_stddev` is provided.
106  * \return false on error writing to file
107  * \sa saveAsVtkStructuredGrid
108  */
109  bool saveAsCSV(
110  const std::string& filName_mean,
111  const std::string& filName_stddev = std::string()) const;
112 
113  /** Save the current estimated grid to a VTK file (.vts) as a "structured
114  * grid". \sa saveAsCSV */
115  bool saveAsVtkStructuredGrid(const std::string& fil) const;
116 
117  /** Parameters common to any derived class.
118  * Derived classes should derive a new struct from this one, plus "public
119  * utils::CLoadableOptions",
120  * and call the internal_* methods where appropiate to deal with the
121  * variables declared here.
122  * Derived classes instantions of their "TInsertionOptions" MUST set the
123  * pointer "m_insertOptions_common" upon construction.
124  */
126  {
127  /** Default values loader */
129 
130  /** See utils::CLoadableOptions */
131  void loadFromConfigFile(
133  const std::string& section) override;
134 
135  /** See utils::CLoadableOptions */
136  void dumpToTextStream(std::ostream& out) const override;
137 
138  /** @name Gaussian Markov Random Fields method
139  @{ */
140  /** The information (Lambda) of fixed map constraints */
141  double GMRF_lambdaPrior{0.01f};
142  /** (Default:false) Skip the computation of the variance, just compute
143  * the mean */
144  bool GMRF_skip_variance{false};
145  /** @} */
146  };
147 
148  /** \sa updateMapEstimation() */
150 
151  /** Changes the size of the grid, maintaining previous contents. \sa setSize
152  */
153  void resize(
154  double new_x_min, double new_x_max, double new_y_min, double new_y_max,
155  double new_z_min, double new_z_max,
156  const TRandomFieldVoxel& defaultValueNewvoxels,
157  double additionalMarginMeters = 2.0) override;
158 
159  /** Changes the size of the grid, erasing previous contents.If
160  * `resolution_z`<0, the same resolution will be used for all dimensions
161  * x,y,z as given in `resolution_xy` \sa resize.*/
162  void setSize(
163  const double x_min, const double x_max, const double y_min,
164  const double y_max, const double z_min, const double z_max,
165  const double resolution_xy, const double resolution_z = -1.0,
166  const TRandomFieldVoxel* fill_value = nullptr) override;
167 
168  /** Base class for user-supplied objects capable of describing voxels
169  * connectivity, used to build prior factors of the MRF graph. \sa
170  * setvoxelsConnectivity() */
172  {
174  // Virtual destructor for polymorphic type.
175  virtual ~ConnectivityDescriptor() = default;
176  /** Implement the check of whether node i=(icx,icy,icz) is connected
177  * with node j=(jcx,jcy,jcy).
178  * This visitor method will be called only for immediate neighbors.
179  * \return true if connected (and the "information" value should be also
180  * updated in out_edge_information), false otherwise.
181  */
182  virtual bool getEdgeInformation(
183  /** The parent map on which we are running */
184  const CRandomFieldGridMap3D* parent,
185  /** (cx,cy,cz) for node "i" */
186  size_t icx, size_t icy, size_t icz,
187  /** (cx,cy,cz) for node "j" */
188  size_t jcx, size_t jcy, size_t jcz,
189  /** Must output here the inverse of the variance of the constraint
190  edge. */
191  double& out_edge_information) = 0;
192  };
193 
194  /** Sets a custom object to define the connectivity between voxels. Must
195  * call clear() or setSize() afterwards for the changes to take place. */
197  const ConnectivityDescriptor::Ptr& new_connectivity_descriptor);
198 
200  {
203  };
204 
205  /** Direct update of the map with a reading in a given position of the map.
206  * \return false if point is out of the grid extension.
207  */
209  /** [in] The value observed in the (x,y,z) position */
210  const double sensorReading,
211  /** [in] The variance of the sensor observation */
212  const double sensorVariance,
213  /** [in] The (x,y,z) location */
214  const mrpt::math::TPoint3D& point,
215  /** [in] Voxel interpolation method: how many voxels will be
216  affected by the reading */
217  const TVoxelInterpolationMethod method,
218  /** [in] Run a global map update after inserting this observation
219  (algorithm-dependant) */
220  const bool update_map);
221 
222  /** Run the method-specific procedure required to ensure that the mean &
223  * variances are up-to-date with all inserted observations, using parameters
224  * in insertionOptions */
225  void updateMapEstimation();
226 
227  /** Returns the 3D grid contents as an VTK grid. */
229  vtkStructuredGrid* output,
230  const std::string& label_mean = std::string("mean"),
231  const std::string& label_stddev = std::string("stddev")) const;
232 
233  protected:
234  /** Internal: called called after each change of resolution, size, etc. to
235  * build the prior factor information */
236  void internal_initialize(bool erase_prev_contents = true);
237 
238  /** Empty: default */
240 
242 
245  {
246  /** Observation value */
247  double obsValue;
248  /** "Information" of the observation (=inverse of the variance) */
249  double Lambda;
250 
251  double evaluateResidual() const override;
252  double getInformation() const override;
253  void evalJacobian(double& dr_dx) const override;
254 
256  : obsValue(.0), Lambda(.0), m_parent(&parent)
257  {
258  }
259 
260  private:
262  };
263 
266  {
267  /** "Information" of the observation (=inverse of the variance) */
268  double Lambda;
269 
270  double evaluateResidual() const override;
271  double getInformation() const override;
272  void evalJacobian(double& dr_dx_i, double& dr_dx_j) const override;
273 
275  : Lambda(.0), m_parent(&parent)
276  {
277  }
278 
279  private:
281  };
282 
283  /** Vector with the active observations and their respective Information,
284  * for each map cell. */
285  std::vector<std::deque<TObservationGMRF>> m_mrf_factors_activeObs;
286  /** Vector with the precomputed priors for each GMRF model */
287  std::deque<TPriorFactorGMRF> m_mrf_factors_priors;
288 };
289 
290 } // namespace mrpt::maps
void dumpToTextStream(std::ostream &out) const override
See utils::CLoadableOptions.
bool GMRF_skip_variance
(Default:false) Skip the computation of the variance, just compute the mean
Simple, scalar (1-dim) constraint (edge) for a GMRF.
This is a virtual base class for sets of options than can be loaded from and/or saved to configuratio...
mrpt::graphs::ScalarFactorGraph m_gmrf
The contents of each voxel in a CRandomFieldGridMap3D map.
virtual bool getEdgeInformation(const CRandomFieldGridMap3D *parent, size_t icx, size_t icy, size_t icz, size_t jcx, size_t jcy, size_t jcz, double &out_edge_information)=0
Implement the check of whether node i=(icx,icy,icz) is connected with node j=(jcx,jcy,jcy).
double GMRF_lambdaPrior
The information (Lambda) of fixed map constraints.
void evalJacobian(double &dr_dx) const override
Returns the derivative of the residual wrt the node value.
void internal_initialize(bool erase_prev_contents=true)
Internal: called called after each change of resolution, size, etc.
double getInformation() const override
Return the inverse of the variance of this constraint.
double evaluateResidual() const override
Return the residual/error of this observation.
double getInformation() const override
Return the inverse of the variance of this constraint.
void setVoxelsConnectivity(const ConnectivityDescriptor::Ptr &new_connectivity_descriptor)
Sets a custom object to define the connectivity between voxels.
double evaluateResidual() const override
Return the residual/error of this observation.
This class allows loading and storing values and vectors of different types from a configuration text...
double Lambda
"Information" of the observation (=inverse of the variance)
A 3D rectangular grid of dynamic size which stores any kind of data at each voxel.
void resize(double new_x_min, double new_x_max, double new_y_min, double new_y_max, double new_z_min, double new_z_max, const TRandomFieldVoxel &defaultValueNewvoxels, double additionalMarginMeters=2.0) override
Changes the size of the grid, maintaining previous contents.
Versatile class for consistent logging and management of output messages.
std::vector< std::deque< TObservationGMRF > > m_mrf_factors_activeObs
Vector with the active observations and their respective Information, for each map cell...
double Lambda
"Information" of the observation (=inverse of the variance)
void clear() override
Erases all added observations and start again with an empty gridmap.
CRandomFieldGridMap3D(double x_min=-2, double x_max=2, double y_min=-2, double y_max=2, double z_min=-2, double z_max=2, double voxel_size=0.5, bool call_initialize_now=true)
Constructor.
ConnectivityDescriptor::Ptr m_gmrf_connectivity
Empty: default.
GLsizei const GLchar ** string
Definition: glext.h:4116
double mean_value
Mean and sigma (standard deviation) estimated values for the voxel.
bool saveAsCSV(const std::string &filName_mean, const std::string &filName_stddev=std::string()) const
Save the current estimated mean values to a CSV file (compatible with Paraview) with fields x y z mea...
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
CRandomFieldGridMap3D represents a 3D regular grid where each voxel is associated one real-valued pro...
void getAsVtkStructuredGrid(vtkStructuredGrid *output, const std::string &label_mean=std::string("mean"), const std::string &label_stddev=std::string("stddev")) const
Returns the 3D grid contents as an VTK grid.
bool saveAsVtkStructuredGrid(const std::string &fil) const
Save the current estimated grid to a VTK file (.vts) as a "structured grid".
GLsizei GLsizei GLchar * source
Definition: glext.h:4097
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
void loadFromConfigFile(const mrpt::config::CConfigFileBase &source, const std::string &section) override
See utils::CLoadableOptions.
bool insertIndividualReading(const double sensorReading, const double sensorVariance, const mrpt::math::TPoint3D &point, const TVoxelInterpolationMethod method, const bool update_map)
Direct update of the map with a reading in a given position of the map.
void updateMapEstimation()
Run the method-specific procedure required to ensure that the mean & variances are up-to-date with al...
Simple, scalar (1-dim) constraint (edge) for a GMRF.
Lightweight 3D point.
Definition: TPoint3D.h:90
void evalJacobian(double &dr_dx_i, double &dr_dx_j) const override
Returns the derivative of the residual wrt the node values.
static bool ENABLE_GMRF_PROFILER
[default:false] Enables a profiler to show a performance report at application end.
void setSize(const double x_min, const double x_max, const double y_min, const double y_max, const double z_min, const double z_max, const double resolution_xy, const double resolution_z=-1.0, const TRandomFieldVoxel *fill_value=nullptr) override
Changes the size of the grid, erasing previous contents.If resolution_z<0, the same resolution will b...
std::deque< TPriorFactorGMRF > m_mrf_factors_priors
Vector with the precomputed priors for each GMRF model.
Base class for user-supplied objects capable of describing voxels connectivity, used to build prior f...
TRandomFieldVoxel(double _mean_value=.0, double _stddev_value=.0)
Constructor.
Sparse solver for GMRF (Gaussian Markov Random Fields) graphical models.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019