Main MRPT website > C++ reference
MRPT logo
CBeaconMap.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CBeaconMap_H
29 #define CBeaconMap_H
30 
31 #include <mrpt/slam/CMetricMap.h>
32 #include <mrpt/slam/CBeacon.h>
34 #include <mrpt/math/CMatrix.h>
37 
38 #include <mrpt/maps/link_pragmas.h>
39 
40 namespace mrpt
41 {
42 namespace slam
43 {
44  using namespace mrpt::utils;
45  using namespace mrpt::math;
46 
47  class CObservationBeaconRanges;
48 
49 
51 
52  /** A class for storing a map of 3D probabilistic beacons, using a Montecarlo, Gaussian, or Sum of Gaussians (SOG) representation (for range-only SLAM).
53  * <br>
54  * The individual beacons are defined as mrpt::slam::CBeacon objects.
55  * <br>
56  * When invoking CBeaconMap::insertObservation(), landmarks will be extracted and fused into the map.
57  * The only currently supported observation type is mrpt::slam::CObservationBeaconRanges.
58  * See insertionOptions and likelihoodOptions for parameters used when creating and fusing beacon landmarks.
59  * <br>
60  * Use "TInsertionOptions::insertAsMonteCarlo" to select between 2 different behaviors:
61  * - Initial PDF of beacons: MonteCarlo, after convergence, pass to Gaussians; or
62  * - Initial PDF of beacons: SOG, after convergence, a single Gaussian.
63  *
64  * Refer to the papers: []
65  *
66  * \ingroup mrpt_maps_grp
67  * \sa CMetricMap
68  */
70  {
71  // This must be added to any CSerializable derived class:
73 
74  public:
75  typedef std::deque<CBeacon> TSequenceBeacons;
76  typedef std::deque<CBeacon>::iterator iterator;
77  typedef std::deque<CBeacon>::const_iterator const_iterator;
78 
79  protected:
80  /** The individual beacons */
81  TSequenceBeacons m_beacons;
82 
83  /** Clear the map, erasing all landmarks.
84  */
85  virtual void internal_clear();
86 
87  /** Insert the observation information into this map. This method must be implemented
88  * in derived classes.
89  * \param obs The observation
90  * \param robotPose The 3D pose of the robot mobile base in the map reference system, or NULL (default) if you want to use CPose2D(0,0,deg)
91  *
92  * \sa CObservation::insertObservationInto
93  */
94  virtual bool internal_insertObservation( const CObservation *obs, const CPose3D *robotPose = NULL );
95 
96  public:
97  /** Constructor */
98  CBeaconMap();
99 
100  void resize(const size_t N); //!< Resize the number of SOG modes
101 
102  /** Access to individual beacons */
103  const CBeacon& operator [](size_t i) const {
104  ASSERT_(i<m_beacons.size())
105  return m_beacons[i];
106  }
107  /** Access to individual beacons */
108  const CBeacon& get(size_t i) const{
109  ASSERT_(i<m_beacons.size())
110  return m_beacons[i];
111  }
112  /** Access to individual beacons */
113  CBeacon& operator [](size_t i) {
114  ASSERT_(i<m_beacons.size())
115  return m_beacons[i];
116  }
117  /** Access to individual beacons */
118  CBeacon& get(size_t i) {
119  ASSERT_(i<m_beacons.size())
120  return m_beacons[i];
121  }
122 
123  iterator begin() { return m_beacons.begin(); }
124  const_iterator begin() const { return m_beacons.begin(); }
125  iterator end() { return m_beacons.end(); }
126  const_iterator end() const { return m_beacons.end(); }
127 
128  /** Inserts a copy of the given mode into the SOG */
129  void push_back(const CBeacon& m) {
130  m_beacons.push_back( m );
131  }
132 
133  /** Computes the ratio in [0,1] of correspondences between "this" and the "otherMap" map, whose 6D pose relative to "this" is "otherMapPose"
134  * In the case of a multi-metric map, this returns the average between the maps. This method always return 0 for grid maps.
135  * \param otherMap [IN] The other map to compute the matching with.
136  * \param otherMapPose [IN] The 6D pose of the other map as seen from "this".
137  * \param minDistForCorr [IN] The minimum distance between 2 non-probabilistic map elements for counting them as a correspondence.
138  * \param minMahaDistForCorr [IN] The minimum Mahalanobis distance between 2 probabilistic map elements for counting them as a correspondence.
139  *
140  * \return The matching ratio [0,1]
141  * \sa computeMatchingWith2D
142  */
143  float compute3DMatchingRatio(
144  const CMetricMap *otherMap,
145  const CPose3D &otherMapPose,
146  float minDistForCorr = 0.10f,
147  float minMahaDistForCorr = 2.0f
148  ) const;
149 
150  /** With this struct options are provided to the likelihood computations.
151  */
153  {
154  public:
155  /** Initilization of default parameters
156  */
158 
159  /** See utils::CLoadableOptions
160  */
161  void loadFromConfigFile(
162  const mrpt::utils::CConfigFileBase &source,
163  const std::string &section);
164 
165  /** See utils::CLoadableOptions
166  */
167  void dumpToTextStream(CStream &out) const;
168 
169  /** The standard deviation used for Beacon ranges likelihood (default=0.08m).
170  */
171  float rangeStd;
172 
173  } likelihoodOptions;
174 
175  /** This struct contains data for choosing the method by which new beacons are inserted in the map.
176  */
178  {
179  public:
180  /** Initilization of default parameters
181  */
183 
184  /** See utils::CLoadableOptions
185  */
186  void loadFromConfigFile(
187  const mrpt::utils::CConfigFileBase &source,
188  const std::string &section);
189 
190  /** See utils::CLoadableOptions
191  */
192  void dumpToTextStream(CStream &out) const;
193 
194  /** Insert a new beacon as a set of montecarlo samples (default=true), or, if false, as a sum of gaussians (see mrpt::slam::CBeacon).
195  * \sa MC_performResampling
196  */
198 
199  /** Minimum and maximum elevation angles (in degrees) for inserting new beacons at the first observation: the default values (both 0), makes the beacons to be in the same horizontal plane that the sensors, that is, 2D SLAM - the min/max values are -90/90.
200  */
201  float maxElevation_deg,minElevation_deg;
202 
203  /** Number of particles per meter of range, i.e. per meter of the "radius of the ring".
204  */
205  unsigned int MC_numSamplesPerMeter;
206 
207  /** The threshold for the maximum std (X,Y,and Z) before colapsing the particles into a Gaussian PDF (default=0,4).
208  */
210 
211  /** Threshold for the maximum difference from the maximun (log) weight in the set of samples for erasing a given sample (default=5).
212  */
214 
215  /** If set to false (default), the samples will be generated the first time a beacon is observed, and their weights just updated subsequently - if set to "true", fewer samples will be required since the particles will be resamples when necessary, and a small "noise" will be added to avoid depletion.
216  */
218 
219  /** The std.dev. of the Gaussian noise to be added to each sample after resampling, only if MC_performResampling=true.
220  */
222 
223  /** Threshold for the maximum difference from the maximun (log) weight in the SOG for erasing a given mode (default=20).
224  */
226 
227  /** A parameter for initializing 2D/3D SOGs
228  */
230 
231  /** Constant used to compute the std. dev. int the tangent direction when creating the Gaussians.
232  */
234 
235  } insertionOptions;
236 
237  /** Save to a MATLAB script which displays 3D error ellipses for the map.
238  * \param file The name of the file to save the script to.
239  * \param style The MATLAB-like string for the style of the lines (see 'help plot' in MATLAB for posibilities)
240  * \param stdCount The ellipsoids will be drawn from the center to a given confidence interval in [0,1], e.g. 2 sigmas=0.95 (default is 2std = 0.95 confidence intervals)
241  *
242  * \return Returns false if any error occured, true elsewere.
243  */
244  bool saveToMATLABScript3D(
245  std::string file,
246  const char *style="b",
247  float confInterval = 0.95f ) const;
248 
249 
250  /** Returns the stored landmarks count.
251  */
252  size_t size() const;
253 
254 
255  /** Computes the (logarithmic) likelihood that a given observation was taken from a given pose in the world being modeled with this map.
256  *
257  * In the current implementation, this method behaves in a different way according to the nature of
258  * the observation's class:
259  * - "mrpt::slam::CObservation2DRangeScan": This calls "computeLikelihood_RSLC_2007".
260  * - "mrpt::slam::CObservationStereoImages": This calls "computeLikelihood_SIFT_LandmarkMap".
261  *
262  * \param takenFrom The robot's pose the observation is supposed to be taken from.
263  * \param obs The observation.
264  * \return This method returns a likelihood value > 0.
265  *
266  * \sa Used in particle filter algorithms, see: CMultiMetricMapPDF::update
267  */
268  double computeObservationLikelihood( const CObservation *obs, const CPose3D &takenFrom );
269 
270  /** Computes the matchings between this and another 2D points map.
271  This includes finding:
272  - The set of points pairs in each map
273  - The mean squared distance between corresponding pairs.
274  This method is the most time critical one into the ICP algorithm.
275 
276  * \param otherMap [IN] The other map to compute the matching with.
277  * \param otherMapPose [IN] The pose of the other map as seen from "this".
278  * \param maxDistForCorrespondence [IN] Maximum 2D linear distance between two points to be matched.
279  * \param maxAngularDistForCorrespondence [IN] In radians: The aim is to allow larger distances to more distant correspondences.
280  * \param angularDistPivotPoint [IN] The point used to calculate distances from in both maps.
281  * \param correspondences [OUT] The detected matchings pairs.
282  * \param correspondencesRatio [OUT] The ratio [0,1] of points in otherMap with at least one correspondence.
283  * \param sumSqrDist [OUT] The sum of all matched points squared distances.If undesired, set to NULL, as default.
284  * \param covariance [OUT] The resulting matching covariance 3x3 matrix, or NULL if undesired.
285  * \param onlyKeepTheClosest [IN] If set to true, only the closest correspondence will be returned. If false (default) all are returned.
286  *
287  * \sa compute3DMatchingRatio
288  */
289  void computeMatchingWith2D(
290  const CMetricMap *otherMap,
291  const CPose2D &otherMapPose,
292  float maxDistForCorrespondence,
293  float maxAngularDistForCorrespondence,
294  const CPose2D &angularDistPivotPoint,
295  TMatchingPairList &correspondences,
296  float &correspondencesRatio,
297  float *sumSqrDist = NULL,
298  bool onlyKeepTheClosest = false,
299  bool onlyUniqueRobust = false,
300  const size_t decimation_other_map_points = 1,
301  const size_t offset_other_map_points = 0 ) const;
302 
303  /** Perform a search for correspondences between "this" and another lansmarks map:
304  * Firsly, the landmarks' descriptor is used to find correspondences, then inconsistent ones removed by
305  * looking at their 3D poses.
306  * \param otherMap [IN] The other map.
307  * \param correspondences [OUT] The matched pairs between maps.
308  * \param correspondencesRatio [OUT] This is NumberOfMatchings / NumberOfLandmarksInTheAnotherMap
309  * \param otherCorrespondences [OUT] Will be returned with a vector containing "true" for the indexes of the other map's landmarks with a correspondence.
310  */
311  void computeMatchingWith3DLandmarks(
312  const mrpt::slam::CBeaconMap *otherMap,
313  TMatchingPairList &correspondences,
314  float &correspondencesRatio,
315  std::vector<bool> &otherCorrespondences) const;
316 
317  /** Changes the reference system of the map to a given 3D pose.
318  */
319  void changeCoordinatesReference( const CPose3D &newOrg );
320 
321  /** Changes the reference system of the map "otherMap" and save the result in "this" map.
322  */
323  void changeCoordinatesReference( const CPose3D &newOrg, const mrpt::slam::CBeaconMap *otherMap );
324 
325 
326  /** Returns true if the map is empty/no observation has been inserted.
327  */
328  bool isEmpty() const;
329 
330  /** Simulates a reading toward each of the beacons in the landmarks map, if any.
331  * \param in_robotPose This robot pose is used to simulate the ranges to each beacon.
332  * \param in_sensorLocationOnRobot The 3D position of the sensor on the robot
333  * \param out_Observations The results will be stored here. NOTICE that the fields "CObservationBeaconRanges::minSensorDistance","CObservationBeaconRanges::maxSensorDistance" and "CObservationBeaconRanges::stdError" MUST BE FILLED OUT before calling this function.
334  * An observation will be generated for each beacon in the map, but notice that some of them may be missed if out of the sensor maximum range.
335  */
336  void simulateBeaconReadings(
337  const CPose3D &in_robotPose,
338  const CPoint3D &in_sensorLocationOnRobot,
339  CObservationBeaconRanges &out_Observations ) const;
340 
341  /** This virtual method saves the map to a file "filNamePrefix"+< some_file_extension >, as an image or in any other applicable way (Notice that other methods to save the map may be implemented in classes implementing this virtual interface).
342  * In the case of this class, these files are generated:
343  * - "filNamePrefix"+"_3D.m": A script for MATLAB for drawing landmarks as 3D ellipses.
344  * - "filNamePrefix"+"_3D.3DScene": A 3D scene with a "ground plane grid" and the set of ellipsoids in 3D.
345  * - "filNamePrefix"+"_covs.m": A textual representation (see saveToTextFile)
346  */
347  void saveMetricMapRepresentationToFile(
348  const std::string &filNamePrefix ) const;
349 
350  /** Save a text file with a row per beacon, containing this 11 elements:
351  * - X Y Z: Mean values
352  * - VX VY VZ: Variances of each dimension (C11, C22, C33)
353  * - DET2D DET3D: Determinant of the 2D and 3D covariance matrixes.
354  * - C12, C13, C23: Cross covariances
355  */
356  void saveToTextFile(const std::string &fil) const;
357 
358  /** Returns a 3D object representing the map.
359  */
360  void getAs3DObject ( mrpt::opengl::CSetOfObjectsPtr &outObj ) const;
361 
362  /** Returns a pointer to the beacon with the given ID, or NULL if it does not exist.
363  */
364  const CBeacon * getBeaconByID( CBeacon::TBeaconID id ) const;
365 
366  /** Returns a pointer to the beacon with the given ID, or NULL if it does not exist.
367  */
368  CBeacon * getBeaconByID( CBeacon::TBeaconID id );
369 
370  }; // End of class def.
371 
372 
373  } // End of namespace
374 } // End of namespace
375 
376 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013