Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
point_representation.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id: point_representation.h 6126 2012-07-03 20:19:58Z aichim $
37  *
38  */
39 #ifndef PCL_POINT_REPRESENTATION_H_
40 #define PCL_POINT_REPRESENTATION_H_
41 
42 #include <pcl/point_types.h>
43 #include <pcl/pcl_macros.h>
44 #include <pcl/for_each_type.h>
45 
46 namespace pcl
47 {
54  template <typename PointT>
56  {
57  protected:
59  int nr_dimensions_;
61  std::vector<float> alpha_;
71  bool trivial_;
72 
73  public:
74  typedef boost::shared_ptr<PointRepresentation<PointT> > Ptr;
75  typedef boost::shared_ptr<const PointRepresentation<PointT> > ConstPtr;
76 
78  PointRepresentation () : nr_dimensions_ (0), alpha_ (0), trivial_ (false) {}
79 
81  virtual ~PointRepresentation () {}
82 
87  virtual void copyToFloatArray (const PointT &p, float *out) const = 0;
88 
96  inline bool isTrivial() const { return trivial_ && alpha_.empty (); }
97 
101  virtual bool
102  isValid (const PointT &p) const
103  {
104  bool is_valid = true;
105 
106  if (trivial_)
107  {
108  const float* temp = reinterpret_cast<const float*>(&p);
109 
110  for (int i = 0; i < nr_dimensions_; ++i)
111  {
112  if (!pcl_isfinite (temp[i]))
113  {
114  is_valid = false;
115  break;
116  }
117  }
118  }
119  else
120  {
121  float *temp = new float[nr_dimensions_];
122  copyToFloatArray (p, temp);
123 
124  for (int i = 0; i < nr_dimensions_; ++i)
125  {
126  if (!pcl_isfinite (temp[i]))
127  {
128  is_valid = false;
129  break;
130  }
131  }
132  delete [] temp;
133  }
134  return (is_valid);
135  }
136 
141  template <typename OutputType> void
142  vectorize (const PointT &p, OutputType &out) const
143  {
144  float *temp = new float[nr_dimensions_];
145  copyToFloatArray (p, temp);
146  if (alpha_.empty ())
147  {
148  for (int i = 0; i < nr_dimensions_; ++i)
149  out[i] = temp[i];
150  }
151  else
152  {
153  for (int i = 0; i < nr_dimensions_; ++i)
154  out[i] = temp[i] * alpha_[i];
155  }
156  delete [] temp;
157  }
158 
162  void
163  setRescaleValues (const float *rescale_array)
164  {
165  alpha_.resize (nr_dimensions_);
166  for (int i = 0; i < nr_dimensions_; ++i)
167  alpha_[i] = rescale_array[i];
168  }
169 
171  inline int getNumberOfDimensions () const { return (nr_dimensions_); }
172  };
173 
175 
177  template <typename PointDefault>
178  class DefaultPointRepresentation : public PointRepresentation <PointDefault>
179  {
182 
183  public:
184  // Boost shared pointers
185  typedef boost::shared_ptr<DefaultPointRepresentation<PointDefault> > Ptr;
186  typedef boost::shared_ptr<const DefaultPointRepresentation<PointDefault> > ConstPtr;
187 
189  {
190  // If point type is unknown, assume it's a struct/array of floats, and compute the number of dimensions
191  nr_dimensions_ = sizeof (PointDefault) / sizeof (float);
192  // Limit the default representation to the first 3 elements
193  if (nr_dimensions_ > 3) nr_dimensions_ = 3;
194 
195  trivial_ = true;
196  }
197 
199 
200  inline Ptr
201  makeShared () const
202  {
203  return (Ptr (new DefaultPointRepresentation<PointDefault> (*this)));
204  }
205 
206  virtual void
207  copyToFloatArray (const PointDefault &p, float * out) const
208  {
209  // If point type is unknown, treat it as a struct/array of floats
210  const float* ptr = reinterpret_cast<const float*> (&p);
211  for (int i = 0; i < nr_dimensions_; ++i)
212  out[i] = ptr[i];
213  }
214  };
215 
217 
220  template <typename PointDefault>
221  class DefaultFeatureRepresentation : public PointRepresentation <PointDefault>
222  {
223  protected:
225 
226  private:
227  struct IncrementFunctor
228  {
229  IncrementFunctor (int &n) : n_ (n)
230  {
231  n_ = 0;
232  }
233 
234  template<typename Key> inline void operator () ()
235  {
237  }
238 
239  private:
240  int &n_;
241  };
242 
243  struct NdCopyPointFunctor
244  {
245  typedef typename traits::POD<PointDefault>::type Pod;
246 
247  NdCopyPointFunctor (const PointDefault &p1, float * p2)
248  : p1_ (reinterpret_cast<const Pod&>(p1)), p2_ (p2), f_idx_ (0) { }
249 
250  template<typename Key> inline void operator() ()
251  {
252  typedef typename pcl::traits::datatype<PointDefault, Key>::type FieldT;
254  Helper<Key, FieldT, NrDims>::copyPoint (p1_, p2_, f_idx_);
255  }
256 
257  // Copy helper for scalar fields
258  template <typename Key, typename FieldT, int NrDims>
259  struct Helper
260  {
261  static void copyPoint (const Pod &p1, float * p2, int &f_idx)
262  {
263  const uint8_t * data_ptr = reinterpret_cast<const uint8_t *> (&p1) +
265  p2[f_idx++] = *reinterpret_cast<const FieldT*> (data_ptr);
266  }
267  };
268  // Copy helper for array fields
269  template <typename Key, typename FieldT, int NrDims>
270  struct Helper<Key, FieldT[NrDims], NrDims>
271  {
272  static void copyPoint (const Pod &p1, float * p2, int &f_idx)
273  {
274  const uint8_t * data_ptr = reinterpret_cast<const uint8_t *> (&p1) +
276  int nr_dims = NrDims;
277  const FieldT * array = reinterpret_cast<const FieldT *> (data_ptr);
278  for (int i = 0; i < nr_dims; ++i)
279  {
280  p2[f_idx++] = array[i];
281  }
282  }
283  };
284 
285  private:
286  const Pod &p1_;
287  float * p2_;
288  int f_idx_;
289  };
290 
291  public:
292  // Boost shared pointers
293  typedef typename boost::shared_ptr<DefaultFeatureRepresentation<PointDefault> > Ptr;
294  typedef typename boost::shared_ptr<const DefaultFeatureRepresentation<PointDefault> > ConstPtr;
296 
298  {
299  nr_dimensions_ = 0; // zero-out the nr_dimensions_ before it gets incremented
300  pcl::for_each_type <FieldList> (IncrementFunctor (nr_dimensions_));
301  }
302 
303  inline Ptr
304  makeShared () const
305  {
306  return (Ptr (new DefaultFeatureRepresentation<PointDefault> (*this)));
307  }
308 
309  virtual void
310  copyToFloatArray (const PointDefault &p, float * out) const
311  {
312  pcl::for_each_type <FieldList> (NdCopyPointFunctor (p, out));
313  }
314  };
315 
317  template <>
319  {
320  public:
322  {
323  nr_dimensions_ = 3;
324  trivial_ = true;
325  }
326 
327  virtual void
328  copyToFloatArray (const PointXYZ &p, float * out) const
329  {
330  out[0] = p.x;
331  out[1] = p.y;
332  out[2] = p.z;
333  }
334  };
335 
337  template <>
339  {
340  public:
342  {
343  nr_dimensions_ = 3;
344  trivial_ = true;
345  }
346 
347  virtual void
348  copyToFloatArray (const PointXYZI &p, float * out) const
349  {
350  out[0] = p.x;
351  out[1] = p.y;
352  out[2] = p.z;
353  // By default, p.intensity is not part of the PointXYZI vectorization
354  }
355  };
356 
358  template <>
360  {
361  public:
363  {
364  nr_dimensions_ = 3;
365  trivial_ = true;
366  }
367 
368  virtual void
369  copyToFloatArray (const PointNormal &p, float * out) const
370  {
371  out[0] = p.x;
372  out[1] = p.y;
373  out[2] = p.z;
374  }
375  };
376 
378  template <>
380  {};
381 
383  template <>
385  {};
386 
388  template <>
390  {
391  public:
393  {
394  nr_dimensions_ = 4;
395  trivial_ = true;
396  }
397 
398  virtual void
399  copyToFloatArray (const PPFSignature &p, float * out) const
400  {
401  out[0] = p.f1;
402  out[1] = p.f2;
403  out[2] = p.f3;
404  out[3] = p.f4;
405  }
406  };
407 
409  template <>
411  {};
412 
414  template <>
416  {};
417 
419  template <>
421  {};
422 
424  template <>
426  {
427  public:
429  {
430  nr_dimensions_ = 1980;
431  }
432 
433  virtual void
434  copyToFloatArray (const ShapeContext &p, float * out) const
435  {
436  for (int i = 0; i < nr_dimensions_; ++i)
437  out[i] = p.descriptor[i];
438  }
439  };
440 
442  template <>
443  class
444  PCL_DEPRECATED_CLASS (DefaultPointRepresentation, "SHOT POINT IS DEPRECATED, USE SHOT352 FOR SHAPE AND SHOT1344 FOR SHAPE+COLOR INSTEAD")
445  <SHOT>
446  : public PointRepresentation<SHOT>
447  {
448  public:
449  DefaultPointRepresentation ()
450  {
451  nr_dimensions_ = 352;
452  }
453 
454  virtual void
455  copyToFloatArray (const SHOT &p, float * out) const
456  {
457  for (int i = 0; i < nr_dimensions_; ++i)
458  out[i] = p.descriptor[i];
459  }
460  };
461 
463  template <>
465  {
466  public:
468  {
469  nr_dimensions_ = 352;
470  }
471 
472  virtual void
473  copyToFloatArray (const SHOT352 &p, float * out) const
474  {
475  for (int i = 0; i < nr_dimensions_; ++i)
476  out[i] = p.descriptor[i];
477  }
478  };
479 
481  template <>
483  {
484  public:
486  {
487  nr_dimensions_ = 1344;
488  }
489 
490  virtual void
491  copyToFloatArray (const SHOT1344 &p, float * out) const
492  {
493  for (int i = 0; i < nr_dimensions_; ++i)
494  out[i] = p.descriptor[i];
495  }
496  };
497 
498 
500 
502  template <typename PointDefault>
503  class CustomPointRepresentation : public PointRepresentation <PointDefault>
504  {
506 
507  public:
508  // Boost shared pointers
509  typedef boost::shared_ptr<CustomPointRepresentation<PointDefault> > Ptr;
510  typedef boost::shared_ptr<const CustomPointRepresentation<PointDefault> > ConstPtr;
511 
516  CustomPointRepresentation (const int max_dim = 3, const int start_dim = 0)
517  : max_dim_(max_dim), start_dim_(start_dim)
518  {
519  // If point type is unknown, assume it's a struct/array of floats, and compute the number of dimensions
520  nr_dimensions_ = static_cast<int> (sizeof (PointDefault) / sizeof (float)) - start_dim_;
521  // Limit the default representation to the first 3 elements
522  if (nr_dimensions_ > max_dim_)
523  nr_dimensions_ = max_dim_;
524  }
525 
526  inline Ptr
527  makeShared () const
528  {
529  return Ptr (new CustomPointRepresentation<PointDefault> (*this));
530  }
531 
536  virtual void
537  copyToFloatArray (const PointDefault &p, float *out) const
538  {
539  // If point type is unknown, treat it as a struct/array of floats
540  const float *ptr = (reinterpret_cast<const float*> (&p)) + start_dim_;
541  for (int i = 0; i < nr_dimensions_; ++i)
542  out[i] = ptr[i];
543  }
544 
545  protected:
547  int max_dim_;
549  int start_dim_;
550  };
551 }
552 
553 #endif // #ifndef PCL_POINT_REPRESENTATION_H_