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



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 814d80880 Fri Aug 24 01:51:28 2018 +0200 at mar 26 may 2026 12:30:59 CEST