Point Cloud Library (PCL)  1.14.1-dev
model_library.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, 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  *
37  */
38 
39 #pragma once
40 
41 #include "auxiliary.h"
42 #include <pcl/recognition/ransac_based/voxel_structure.h>
43 #include <pcl/recognition/ransac_based/orr_octree.h>
44 #include <pcl/common/random.h>
45 #include <pcl/pcl_exports.h>
46 #include <pcl/point_cloud.h>
47 #include <pcl/point_types.h>
48 #include <ctime>
49 #include <string>
50 #include <list>
51 #include <map>
52 
53 namespace pcl
54 {
55  namespace recognition
56  {
58  {
59  public:
62 
63  /** \brief Stores some information about the model. */
64  class Model
65  {
66  public:
67  Model (const PointCloudIn& points, const PointCloudN& normals, float voxel_size, const std::string& object_name,
68  float frac_of_points_for_registration, void* user_data = nullptr)
69  : obj_name_(object_name),
70  user_data_ (user_data)
71  {
72  octree_.build (points, voxel_size, &normals);
73 
74  const std::vector<ORROctree::Node*>& full_leaves = octree_.getFullLeaves ();
75  if ( full_leaves.empty () )
76  return;
77 
78  // Initialize
79  auto it = full_leaves.begin ();
80  const float *p = (*it)->getData ()->getPoint ();
81  aux::copy3 (p, octree_center_of_mass_);
82  bounds_of_octree_points_[0] = bounds_of_octree_points_[1] = p[0];
83  bounds_of_octree_points_[2] = bounds_of_octree_points_[3] = p[1];
84  bounds_of_octree_points_[4] = bounds_of_octree_points_[5] = p[2];
85 
86  // Compute both the bounds and the center of mass of the octree points
87  for ( ++it ; it != full_leaves.end () ; ++it )
88  {
89  aux::add3 (octree_center_of_mass_, (*it)->getData ()->getPoint ());
90  aux::expandBoundingBoxToContainPoint (bounds_of_octree_points_, (*it)->getData ()->getPoint ());
91  }
92 
93  int num_octree_points = static_cast<int> (full_leaves.size ());
94  // Finalize the center of mass computation
95  aux::mult3 (octree_center_of_mass_, 1.0f/static_cast<float> (num_octree_points));
96 
97  int num_points_for_registration = static_cast<int> (static_cast<float> (num_octree_points)*frac_of_points_for_registration);
98  points_for_registration_.resize (static_cast<std::size_t> (num_points_for_registration));
99 
100  // Prepare for random point sampling
101  std::vector<int> ids (num_octree_points);
102  for ( int i = 0 ; i < num_octree_points ; ++i )
103  ids[i] = i;
104 
105  // The random generator
106  pcl::common::UniformGenerator<int> randgen (0, num_octree_points - 1, static_cast<std::uint32_t> (time (nullptr)));
107 
108  // Randomly sample some points from the octree
109  for ( int i = 0 ; i < num_points_for_registration ; ++i )
110  {
111  // Choose a random position within the array of ids
112  randgen.setParameters (0, static_cast<int> (ids.size ()) - 1);
113  int rand_pos = randgen.run ();
114 
115  // Copy the randomly selected octree point
116  aux::copy3 (octree_.getFullLeaves ()[ids[rand_pos]]->getData ()->getPoint (), points_for_registration_[i]);
117 
118  // Delete the selected id
119  ids.erase (ids.begin() + rand_pos);
120  }
121  }
122 
123  virtual ~Model () = default;
124 
125  inline const std::string&
126  getObjectName () const
127  {
128  return (obj_name_);
129  }
130 
131  inline const ORROctree&
132  getOctree () const
133  {
134  return (octree_);
135  }
136 
137  inline void*
138  getUserData () const
139  {
140  return (user_data_);
141  }
142 
143  inline const float*
145  {
146  return (octree_center_of_mass_);
147  }
148 
149  inline const float*
151  {
152  return (bounds_of_octree_points_);
153  }
154 
155  inline const PointCloudIn&
157  {
158  return (points_for_registration_);
159  }
160 
161  protected:
162  const std::string obj_name_;
164  float octree_center_of_mass_[3];
165  float bounds_of_octree_points_[6];
167  void* user_data_;
168  };
169 
170  using node_data_pair_list = std::list<std::pair<const ORROctree::Node::Data*, const ORROctree::Node::Data*> >;
171  using HashTableCell = std::map<const Model*, node_data_pair_list>;
173 
174  public:
175  /** \brief This class is used by 'ObjRecRANSAC' to maintain the object models to be recognized. Normally, you do not need to use
176  * this class directly. */
177  ModelLibrary (float pair_width, float voxel_size, float max_coplanarity_angle = 3.0f*AUX_DEG_TO_RADIANS/*3 degrees*/);
178  virtual ~ModelLibrary ()
179  {
180  this->clear();
181  }
182 
183  /** \brief Removes all models from the library and clears the hash table. */
184  void
186 
187  /** \brief This is a threshold. The larger the value the more point pairs will be considered as co-planar and will
188  * be ignored in the off-line model pre-processing and in the online recognition phases. This makes sense only if
189  * "ignore co-planar points" is on. Call this method before calling addModel. */
190  inline void
191  setMaxCoplanarityAngleDegrees (float max_coplanarity_angle_degrees)
192  {
193  max_coplanarity_angle_ = max_coplanarity_angle_degrees*AUX_DEG_TO_RADIANS;
194  }
195 
196  /** \brief Call this method in order NOT to add co-planar point pairs to the hash table. The default behavior
197  * is ignoring co-planar points on. */
198  inline void
200  {
201  ignore_coplanar_opps_ = true;
202  }
203 
204  /** \brief Call this method in order to add all point pairs (co-planar as well) to the hash table. The default
205  * behavior is ignoring co-planar points on. */
206  inline void
208  {
209  ignore_coplanar_opps_ = false;
210  }
211 
212  /** \brief Adds a model to the hash table.
213  *
214  * \param[in] points represents the model to be added.
215  * \param[in] normals are the normals at the model points.
216  * \param[in] object_name is the unique name of the object to be added.
217  * \param[in] frac_of_points_for_registration is the number of points used for fast ICP registration prior to hypothesis testing
218  * \param[in] user_data is a pointer to some data (can be NULL)
219  *
220  * Returns true if model successfully added and false otherwise (e.g., if object_name is not unique). */
221  bool
222  addModel (const PointCloudIn& points, const PointCloudN& normals, const std::string& object_name,
223  float frac_of_points_for_registration, void* user_data = nullptr);
224 
225  /** \brief Returns the hash table built by this instance. */
226  inline const HashTable&
227  getHashTable () const
228  {
229  return (hash_table_);
230  }
231 
232  inline const Model*
233  getModel (const std::string& name) const
234  {
235  auto it = models_.find (name);
236  if ( it != models_.end () )
237  return (it->second);
238 
239  return (nullptr);
240  }
241 
242  inline const std::map<std::string,Model*>&
243  getModels () const
244  {
245  return (models_);
246  }
247 
248  protected:
249  /** \brief Removes all models from the library and destroys the hash table. This method should be called upon destroying this object. */
250  void
251  clear ();
252 
253  /** \brief Returns true if the oriented point pair was added to the hash table and false otherwise. */
254  bool
255  addToHashTable (Model* model, const ORROctree::Node::Data* data1, const ORROctree::Node::Data* data2);
256 
257  protected:
258  float pair_width_;
259  float voxel_size_;
261  bool ignore_coplanar_opps_{true};
262 
263  std::map<std::string,Model*> models_;
265  int num_of_cells_[3];
266  };
267  } // namespace recognition
268 } // namespace pcl
UniformGenerator class generates a random number from range [min, max] at each run picked according t...
Definition: random.h:79
void setParameters(T min, T max, std::uint32_t seed=-1)
Set the uniform number generator parameters.
Definition: random.hpp:84
Stores some information about the model.
Definition: model_library.h:65
const std::string & getObjectName() const
const ORROctree & getOctree() const
const float * getOctreeCenterOfMass() const
const float * getBoundsOfOctreePoints() const
Model(const PointCloudIn &points, const PointCloudN &normals, float voxel_size, const std::string &object_name, float frac_of_points_for_registration, void *user_data=nullptr)
Definition: model_library.h:67
const PointCloudIn & getPointsForRegistration() const
const std::map< std::string, Model * > & getModels() const
void clear()
Removes all models from the library and destroys the hash table.
void removeAllModels()
Removes all models from the library and clears the hash table.
bool addModel(const PointCloudIn &points, const PointCloudN &normals, const std::string &object_name, float frac_of_points_for_registration, void *user_data=nullptr)
Adds a model to the hash table.
ModelLibrary(float pair_width, float voxel_size, float max_coplanarity_angle=3.0f *AUX_DEG_TO_RADIANS)
This class is used by 'ObjRecRANSAC' to maintain the object models to be recognized.
void setMaxCoplanarityAngleDegrees(float max_coplanarity_angle_degrees)
This is a threshold.
void ignoreCoplanarPointPairsOff()
Call this method in order to add all point pairs (co-planar as well) to the hash table.
bool addToHashTable(Model *model, const ORROctree::Node::Data *data1, const ORROctree::Node::Data *data2)
Returns true if the oriented point pair was added to the hash table and false otherwise.
std::map< const Model *, node_data_pair_list > HashTableCell
const Model * getModel(const std::string &name) const
const HashTable & getHashTable() const
Returns the hash table built by this instance.
std::list< std::pair< const ORROctree::Node::Data *, const ORROctree::Node::Data * > > node_data_pair_list
void ignoreCoplanarPointPairsOn()
Call this method in order NOT to add co-planar point pairs to the hash table.
std::map< std::string, Model * > models_
That's a very specialized and simple octree class.
Definition: orr_octree.h:69
Defines all the PCL implemented PointT point type structures.
void expandBoundingBoxToContainPoint(T bbox[6], const T p[3])
Expands the bounding box 'bbox' such that it contains the point 'p'.
Definition: auxiliary.h:95
void mult3(T *v, T scalar)
v = scalar*v.
Definition: auxiliary.h:221
void add3(T a[3], const T b[3])
a += b
Definition: auxiliary.h:150
void copy3(const T src[3], T dst[3])
dst = src
Definition: auxiliary.h:116
#define PCL_EXPORTS
Definition: pcl_macros.h:323
CloudGenerator class generates a point cloud using some random number generator.