Main MRPT website > C++ reference for MRPT 1.9.9
CWeightedPointsMap.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-2017, 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 "maps-precomp.h" // Precomp header
11 
13 #include <mrpt/utils/CStream.h>
14 
15 #include "CPointsMap_crtp_common.h"
16 
17 using namespace std;
18 using namespace mrpt;
19 using namespace mrpt::maps;
20 using namespace mrpt::obs;
21 using namespace mrpt::utils;
22 using namespace mrpt::poses;
23 using namespace mrpt::math;
24 
25 // =========== Begin of Map definition ============
27  "CWeightedPointsMap,weightedPointsMap", mrpt::maps::CWeightedPointsMap)
28 
30 void CWeightedPointsMap::TMapDefinition::loadFromConfigFile_map_specific(
32  const std::string& sectionNamePrefix)
33 {
34  insertionOpts.loadFromConfigFile(
35  source, sectionNamePrefix + string("_insertOpts"));
36  likelihoodOpts.loadFromConfigFile(
37  source, sectionNamePrefix + string("_likelihoodOpts"));
38 }
39 
40 void CWeightedPointsMap::TMapDefinition::dumpToTextStream_map_specific(
41  mrpt::utils::CStream& out) const
42 {
43  this->insertionOpts.dumpToTextStream(out);
44  this->likelihoodOpts.dumpToTextStream(out);
45 }
46 
47 mrpt::maps::CMetricMap* CWeightedPointsMap::internal_CreateFromMapDefinition(
49 {
51  *dynamic_cast<const CWeightedPointsMap::TMapDefinition*>(&_def);
53  obj->insertionOptions = def.insertionOpts;
54  obj->likelihoodOptions = def.likelihoodOpts;
55  return obj;
56 }
57 // =========== End of Map definition Block =========
58 
60 
61 /*---------------------------------------------------------------
62  Constructor
63  ---------------------------------------------------------------*/
65 /*---------------------------------------------------------------
66  Destructor
67  ---------------------------------------------------------------*/
68 CWeightedPointsMap::~CWeightedPointsMap() {}
69 /*---------------------------------------------------------------
70  reserve & resize methods
71  ---------------------------------------------------------------*/
72 void CWeightedPointsMap::reserve(size_t newLength)
73 {
74  newLength = mrpt::utils::length2length4N(newLength);
75  x.reserve(newLength);
76  y.reserve(newLength);
77  z.reserve(newLength);
78  pointWeight.reserve(newLength);
79 }
80 
81 // Resizes all point buffers so they can hold the given number of points: newly
82 // created points are set to default values,
83 // and old contents are not changed.
84 void CWeightedPointsMap::resize(size_t newLength)
85 {
86  this->reserve(newLength); // to ensure 4N capacity
87  x.resize(newLength, 0);
88  y.resize(newLength, 0);
89  z.resize(newLength, 0);
90  pointWeight.resize(newLength, 1);
91 }
92 
93 // Resizes all point buffers so they can hold the given number of points,
94 // *erasing* all previous contents
95 // and leaving all points to default values.
96 void CWeightedPointsMap::setSize(size_t newLength)
97 {
98  this->reserve(newLength); // to ensure 4N capacity
99  x.assign(newLength, 0);
100  y.assign(newLength, 0);
101  z.assign(newLength, 0);
102  pointWeight.assign(newLength, 1);
103 }
104 
105 void CWeightedPointsMap::setPointFast(size_t index, float x, float y, float z)
106 {
107  this->x[index] = x;
108  this->y[index] = y;
109  this->z[index] = z;
110  // this->pointWeight: Unmodified
111  // mark_as_modified(); -> Fast
112 }
113 
114 void CWeightedPointsMap::insertPointFast(float x, float y, float z)
115 {
116  this->x.push_back(x);
117  this->y.push_back(y);
118  this->z.push_back(z);
119  this->pointWeight.push_back(1);
120  // mark_as_modified(); -> Fast
121 }
122 
123 /*---------------------------------------------------------------
124  Copy constructor
125  ---------------------------------------------------------------*/
126 void CWeightedPointsMap::copyFrom(const CPointsMap& obj)
127 {
128  CPointsMap::base_copyFrom(
129  obj); // This also does a ::resize(N) of all data fields.
130 
131  const CWeightedPointsMap* pW =
132  dynamic_cast<const CWeightedPointsMap*>(&obj);
133  if (pW)
134  {
135  pointWeight = pW->pointWeight;
136  }
137 }
138 
139 /*---------------------------------------------------------------
140  addFrom_classSpecific
141  ---------------------------------------------------------------*/
142 void CWeightedPointsMap::addFrom_classSpecific(
143  const CPointsMap& anotherMap, const size_t nPreviousPoints)
144 {
145  const size_t nOther = anotherMap.size();
146 
147  // Specific data for this class:
148  const CWeightedPointsMap* anotheMap_w =
149  dynamic_cast<const CWeightedPointsMap*>(&anotherMap);
150 
151  if (anotheMap_w)
152  {
153  for (size_t i = 0, j = nPreviousPoints; i < nOther; i++, j++)
154  pointWeight[j] = anotheMap_w->pointWeight[i];
155  }
156 }
157 
158 /*---------------------------------------------------------------
159  writeToStream
160  Implements the writing to a CStream capability of
161  CSerializable objects
162  ---------------------------------------------------------------*/
163 void CWeightedPointsMap::writeToStream(
164  mrpt::utils::CStream& out, int* version) const
165 {
166  if (version)
167  *version = 2;
168  else
169  {
170  uint32_t n = x.size();
171 
172  // First, write the number of points:
173  out << n;
174 
175  if (n > 0)
176  {
177  out.WriteBufferFixEndianness(&x[0], n);
178  out.WriteBufferFixEndianness(&y[0], n);
179  out.WriteBufferFixEndianness(&z[0], n);
180  out.WriteBufferFixEndianness(&pointWeight[0], n);
181  }
182 
183  out << genericMapParams; // v2
184  insertionOptions.writeToStream(
185  out); // version 9: insert options are saved with its own method
186  likelihoodOptions.writeToStream(out); // Added in version 5
187  }
188 }
189 
190 /*---------------------------------------------------------------
191  readFromStream
192  Implements the reading from a CStream capability of
193  CSerializable objects
194  ---------------------------------------------------------------*/
195 void CWeightedPointsMap::readFromStream(mrpt::utils::CStream& in, int version)
196 {
197  switch (version)
198  {
199  case 0:
200  case 1:
201  case 2:
202  {
203  mark_as_modified();
204 
205  // Read the number of points:
206  uint32_t n;
207  in >> n;
208 
209  this->resize(n);
210 
211  if (n > 0)
212  {
213  in.ReadBufferFixEndianness(&x[0], n);
214  in.ReadBufferFixEndianness(&y[0], n);
215  in.ReadBufferFixEndianness(&z[0], n);
216  in.ReadBufferFixEndianness(&pointWeight[0], n);
217  }
218 
219  if (version >= 1)
220  {
221  if (version >= 2)
222  in >> genericMapParams;
223  else
224  {
225  bool disableSaveAs3DObject;
226  in >> disableSaveAs3DObject;
227  genericMapParams.enableSaveAs3DObject =
228  !disableSaveAs3DObject;
229  }
230 
231  insertionOptions.readFromStream(in); // version 9: insert
232  // options are saved with
233  // its own method
234  }
235  else
236  {
237  insertionOptions = TInsertionOptions();
238  in >> insertionOptions.minDistBetweenLaserPoints >>
239  insertionOptions.addToExistingPointsMap >>
240  insertionOptions.also_interpolate >>
241  insertionOptions.disableDeletion >>
242  insertionOptions.fuseWithExisting >>
243  insertionOptions.isPlanarMap >>
244  insertionOptions.maxDistForInterpolatePoints;
245  {
246  bool disableSaveAs3DObject;
247  in >> disableSaveAs3DObject;
248  genericMapParams.enableSaveAs3DObject =
249  !disableSaveAs3DObject;
250  }
251  in >> insertionOptions.horizontalTolerance;
252  }
253 
254  likelihoodOptions.readFromStream(in); // Added in version 5
255  }
256  break;
257  default:
259  };
260 }
261 
262 /*---------------------------------------------------------------
263  Clear
264  ---------------------------------------------------------------*/
265 void CWeightedPointsMap::internal_clear()
266 {
267  // This swap() thing is the only way to really deallocate the memory.
271  vector_strong_clear(pointWeight);
272 
273  mark_as_modified();
274 }
275 
276 namespace mrpt
277 {
278 namespace maps
279 {
280 namespace detail
281 {
283 
284 template <>
286 {
287  /** Helper method fot the generic implementation of
288  * CPointsMap::loadFromRangeScan(), to be called only once before inserting
289  * points - this is the place to reserve memory in lric for extra working
290  * variables. */
292  CWeightedPointsMap& me,
294  {
295  MRPT_UNUSED_PARAM(me);
296  MRPT_UNUSED_PARAM(lric);
297  }
298  /** Helper method fot the generic implementation of
299  * CPointsMap::loadFromRangeScan(), to be called once per range data */
301  CWeightedPointsMap& me, const float gx, const float gy, const float gz,
303  {
304  MRPT_UNUSED_PARAM(me);
305  MRPT_UNUSED_PARAM(gx);
306  MRPT_UNUSED_PARAM(gy);
307  MRPT_UNUSED_PARAM(gz);
308  MRPT_UNUSED_PARAM(lric);
309  }
310  /** Helper method fot the generic implementation of
311  * CPointsMap::loadFromRangeScan(), to be called after each
312  * "{x,y,z}.push_back(...);" */
314  CWeightedPointsMap& me,
316  {
317  MRPT_UNUSED_PARAM(lric);
318  me.pointWeight.push_back(1);
319  }
320 
321  /** Helper method fot the generic implementation of
322  * CPointsMap::loadFromRangeScan(), to be called only once before inserting
323  * points - this is the place to reserve memory in lric for extra working
324  * variables. */
326  CWeightedPointsMap& me,
328  {
329  MRPT_UNUSED_PARAM(me);
330  MRPT_UNUSED_PARAM(lric);
331  }
332  /** Helper method fot the generic implementation of
333  * CPointsMap::loadFromRangeScan(), to be called once per range data */
335  CWeightedPointsMap& me, const float gx, const float gy, const float gz,
337  {
338  MRPT_UNUSED_PARAM(me);
339  MRPT_UNUSED_PARAM(gx);
340  MRPT_UNUSED_PARAM(gy);
341  MRPT_UNUSED_PARAM(gz);
342  MRPT_UNUSED_PARAM(lric);
343  }
344  /** Helper method fot the generic implementation of
345  * CPointsMap::loadFromRangeScan(), to be called after each
346  * "{x,y,z}.push_back(...);" */
348  CWeightedPointsMap& me,
350  {
351  MRPT_UNUSED_PARAM(lric);
352  me.pointWeight.push_back(1);
353  }
354  /** Helper method fot the generic implementation of
355  * CPointsMap::loadFromRangeScan(), to be called once per range data, at the
356  * end */
358  CWeightedPointsMap& me,
360  {
361  MRPT_UNUSED_PARAM(me);
362  MRPT_UNUSED_PARAM(lric);
363  }
364 };
365 }
366 }
367 }
368 
369 /** See CPointsMap::loadFromRangeScan() */
370 void CWeightedPointsMap::loadFromRangeScan(
371  const CObservation2DRangeScan& rangeScan, const CPose3D* robotPose)
372 {
374  templ_loadFromRangeScan(*this, rangeScan, robotPose);
375 }
376 
377 /** See CPointsMap::loadFromRangeScan() */
378 void CWeightedPointsMap::loadFromRangeScan(
379  const CObservation3DRangeScan& rangeScan, const CPose3D* robotPose)
380 {
382  templ_loadFromRangeScan(*this, rangeScan, robotPose);
383 }
384 
385 // ================================ PLY files import & export virtual methods
386 // ================================
387 
388 /** In a base class, reserve memory to prepare subsequent calls to
389  * PLY_import_set_vertex */
390 void CWeightedPointsMap::PLY_import_set_vertex_count(const size_t N)
391 {
392  this->setSize(N);
393 }
Virtual base for specifying the kind and parameters of one map (normally, to be inserted into mrpt::m...
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
GLdouble GLdouble z
Definition: glext.h:3872
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
GLenum GLsizei n
Definition: glext.h:5074
Declares a class derived from "CObservation" that encapsules a 3D range scan measurement, as from a time-of-flight range camera or any other RGBD sensor.
static void internal_loadFromRangeScan3D_init(CWeightedPointsMap &me, mrpt::maps::CPointsMap::TLaserRange3DInsertContext &lric)
Helper method fot the generic implementation of CPointsMap::loadFromRangeScan(), to be called only on...
STL namespace.
std::vector< uint32_t > pointWeight
The points weights.
mrpt::maps::CPointsMap::TInsertionOptions insertionOpts
Observations insertion options.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
static void internal_loadFromRangeScan2D_prepareOneRange(CWeightedPointsMap &me, const float gx, const float gy, const float gz, mrpt::maps::CPointsMap::TLaserRange2DInsertContext &lric)
Helper method fot the generic implementation of CPointsMap::loadFromRangeScan(), to be called once pe...
This class allows loading and storing values and vectors of different types from a configuration text...
With this struct options are provided to the observation insertion process.
Definition: CPointsMap.h:204
void WriteBufferFixEndianness(const T *ptr, size_t ElementCount)
Writes a sequence of elemental datatypes, taking care of reordering their bytes from the running arch...
Definition: CStream.h:159
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
A cloud of points in 2D or 3D, which can be built from a sequence of laser scans or other sensors...
Definition: CPointsMap.h:63
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
T length2length4N(T len)
Returns the smaller number >=len such that it&#39;s a multiple of 4.
Definition: bits.h:273
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
GLuint index
Definition: glext.h:4054
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
This namespace contains representation of robot actions and observations.
EIGEN_STRONG_INLINE void setSize(size_t row, size_t col)
Changes the size of matrix, maintaining its previous content as possible and padding with zeros where...
static void templ_loadFromRangeScan(Derived &obj, const mrpt::obs::CObservation2DRangeScan &rangeScan, const mrpt::poses::CPose3D *robotPose)
GLsizei const GLchar ** string
Definition: glext.h:4101
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
void loadFromConfigFile(const mrpt::utils::CConfigFileBase &source, const std::string &sectionNamePrefix) override
Load all params from a config file/source.
static void internal_loadFromRangeScan2D_postPushBack(CWeightedPointsMap &me, mrpt::maps::CPointsMap::TLaserRange2DInsertContext &lric)
Helper method fot the generic implementation of CPointsMap::loadFromRangeScan(), to be called after e...
mrpt::maps::CPointsMap::TLikelihoodOptions likelihoodOpts
Probabilistic observation likelihood options.
void vector_strong_clear(VECTOR_T &v)
Like calling a std::vector<>&#39;s clear() method, but really forcing deallocating the memory...
Definition: bits.h:265
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define MAP_DEFINITION_REGISTER(_CLASSNAME_STRINGS, _CLASSNAME_WITH_NS)
Registers one map class into TMetricMapInitializer factory.
A "CObservation"-derived class that represents a 2D range scan measurement (typically from a laser sc...
A cloud of points in 2D or 3D, which can be built from a sequence of laser scans. ...
static void internal_loadFromRangeScan3D_prepareOneRange(CWeightedPointsMap &me, const float gx, const float gy, const float gz, mrpt::maps::CPointsMap::TLaserRange3DInsertContext &lric)
Helper method fot the generic implementation of CPointsMap::loadFromRangeScan(), to be called once pe...
Declares a virtual base class for all metric maps storage classes.
Definition: CMetricMap.h:55
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
GLuint in
Definition: glext.h:7274
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
Helper struct used for internal_loadFromRangeScan3D_prepareOneRange()
Definition: CPointsMap.h:93
GLenum GLint GLint y
Definition: glext.h:3538
GLenum GLint x
Definition: glext.h:3538
unsigned __int32 uint32_t
Definition: rptypes.h:47
size_t size() const
Returns the number of stored points in the map.
Definition: CPointsMap.h:385
static void internal_loadFromRangeScan3D_postOneRange(CWeightedPointsMap &me, mrpt::maps::CPointsMap::TLaserRange3DInsertContext &lric)
Helper method fot the generic implementation of CPointsMap::loadFromRangeScan(), to be called once pe...
static void internal_loadFromRangeScan2D_init(CWeightedPointsMap &me, mrpt::maps::CPointsMap::TLaserRange2DInsertContext &lric)
Helper method fot the generic implementation of CPointsMap::loadFromRangeScan(), to be called only on...
Helper struct used for internal_loadFromRangeScan2D_prepareOneRange()
Definition: CPointsMap.h:75
static void internal_loadFromRangeScan3D_postPushBack(CWeightedPointsMap &me, mrpt::maps::CPointsMap::TLaserRange3DInsertContext &lric)
Helper method fot the generic implementation of CPointsMap::loadFromRangeScan(), to be called after e...



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019