Point Cloud Library (PCL)  1.11.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  std::vector<ORROctree::Node*>::const_iterator 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 ()
124  {
125  }
126 
127  inline const std::string&
128  getObjectName () const
129  {
130  return (obj_name_);
131  }
132 
133  inline const ORROctree&
134  getOctree () const
135  {
136  return (octree_);
137  }
138 
139  inline void*
140  getUserData () const
141  {
142  return (user_data_);
143  }
144 
145  inline const float*
147  {
148  return (octree_center_of_mass_);
149  }
150 
151  inline const float*
153  {
154  return (bounds_of_octree_points_);
155  }
156 
157  inline const PointCloudIn&
159  {
160  return (points_for_registration_);
161  }
162 
163  protected:
164  const std::string obj_name_;
166  float octree_center_of_mass_[3];
167  float bounds_of_octree_points_[6];
169  void* user_data_;
170  };
171 
172  using node_data_pair_list = std::list<std::pair<const ORROctree::Node::Data*, const ORROctree::Node::Data*> >;
173  using HashTableCell = std::map<const Model*, node_data_pair_list>;
175 
176  public:
177  /** \brief This class is used by 'ObjRecRANSAC' to maintain the object models to be recognized. Normally, you do not need to use
178  * this class directly. */
179  ModelLibrary (float pair_width, float voxel_size, float max_coplanarity_angle = 3.0f*AUX_DEG_TO_RADIANS/*3 degrees*/);
180  virtual ~ModelLibrary ()
181  {
182  this->clear();
183  }
184 
185  /** \brief Removes all models from the library and clears the hash table. */
186  void
187  removeAllModels ();
188 
189  /** \brief This is a threshold. The larger the value the more point pairs will be considered as co-planar and will
190  * be ignored in the off-line model pre-processing and in the online recognition phases. This makes sense only if
191  * "ignore co-planar points" is on. Call this method before calling addModel. */
192  inline void
193  setMaxCoplanarityAngleDegrees (float max_coplanarity_angle_degrees)
194  {
195  max_coplanarity_angle_ = max_coplanarity_angle_degrees*AUX_DEG_TO_RADIANS;
196  }
197 
198  /** \brief Call this method in order NOT to add co-planar point pairs to the hash table. The default behavior
199  * is ignoring co-planar points on. */
200  inline void
202  {
203  ignore_coplanar_opps_ = true;
204  }
205 
206  /** \brief Call this method in order to add all point pairs (co-planar as well) to the hash table. The default
207  * behavior is ignoring co-planar points on. */
208  inline void
210  {
211  ignore_coplanar_opps_ = false;
212  }
213 
214  /** \brief Adds a model to the hash table.
215  *
216  * \param[in] points represents the model to be added.
217  * \param[in] normals are the normals at the model points.
218  * \param[in] object_name is the unique name of the object to be added.
219  * \param[in] frac_of_points_for_registration is the number of points used for fast ICP registration prior to hypothesis testing
220  * \param[in] user_data is a pointer to some data (can be NULL)
221  *
222  * Returns true if model successfully added and false otherwise (e.g., if object_name is not unique). */
223  bool
224  addModel (const PointCloudIn& points, const PointCloudN& normals, const std::string& object_name,
225  float frac_of_points_for_registration, void* user_data = nullptr);
226 
227  /** \brief Returns the hash table built by this instance. */
228  inline const HashTable&
229  getHashTable () const
230  {
231  return (hash_table_);
232  }
233 
234  inline const Model*
235  getModel (const std::string& name) const
236  {
237  std::map<std::string,Model*>::const_iterator it = models_.find (name);
238  if ( it != models_.end () )
239  return (it->second);
240 
241  return (nullptr);
242  }
243 
244  inline const std::map<std::string,Model*>&
245  getModels () const
246  {
247  return (models_);
248  }
249 
250  protected:
251  /** \brief Removes all models from the library and destroys the hash table. This method should be called upon destroying this object. */
252  void
253  clear ();
254 
255  /** \brief Returns true if the oriented point pair was added to the hash table and false otherwise. */
256  bool
257  addToHashTable (Model* model, const ORROctree::Node::Data* data1, const ORROctree::Node::Data* data2);
258 
259  protected:
260  float pair_width_;
261  float voxel_size_;
264 
265  std::map<std::string,Model*> models_;
267  int num_of_cells_[3];
268  };
269  } // namespace recognition
270 } // namespace pcl
pcl
Definition: convolution.h:46
point_types.h
pcl::recognition::ModelLibrary::Model::getOctree
const ORROctree & getOctree() const
Definition: model_library.h:134
pcl::recognition::ModelLibrary::ignore_coplanar_opps_
bool ignore_coplanar_opps_
Definition: model_library.h:263
pcl::recognition::ModelLibrary::getModels
const std::map< std::string, Model * > & getModels() const
Definition: model_library.h:245
pcl::recognition::ModelLibrary::Model::octree_
ORROctree octree_
Definition: model_library.h:165
pcl::recognition::aux::mult3
void mult3(T *v, T scalar)
v = scalar*v.
Definition: auxiliary.h:221
pcl::recognition::ORROctree::Node::Data
Definition: orr_octree.h:78
pcl::recognition::ModelLibrary::Model::user_data_
void * user_data_
Definition: model_library.h:169
pcl::recognition::VoxelStructure< HashTableCell, float >
pcl::recognition::ModelLibrary::HashTableCell
std::map< const Model *, node_data_pair_list > HashTableCell
Definition: model_library.h:173
pcl::recognition::ModelLibrary::Model::Model
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
pcl::recognition::ModelLibrary::node_data_pair_list
std::list< std::pair< const ORROctree::Node::Data *, const ORROctree::Node::Data * > > node_data_pair_list
Definition: model_library.h:172
pcl::recognition::ModelLibrary::models_
std::map< std::string, Model * > models_
Definition: model_library.h:265
pcl::PointCloud< pcl::PointXYZ >
pcl::recognition::ModelLibrary::Model::getObjectName
const std::string & getObjectName() const
Definition: model_library.h:128
pcl::recognition::ModelLibrary
Definition: model_library.h:57
random.h
CloudGenerator class generates a point cloud using some randoom number generator.
pcl::recognition::aux::expandBoundingBoxToContainPoint
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
pcl::recognition::ModelLibrary::getModel
const Model * getModel(const std::string &name) const
Definition: model_library.h:235
pcl::common::UniformGenerator
UniformGenerator class generates a random number from range [min, max] at each run picked according t...
Definition: random.h:78
pcl::recognition::ModelLibrary::getHashTable
const HashTable & getHashTable() const
Returns the hash table built by this instance.
Definition: model_library.h:229
pcl::recognition::ModelLibrary::Model::getPointsForRegistration
const PointCloudIn & getPointsForRegistration() const
Definition: model_library.h:158
pcl::recognition::ModelLibrary::voxel_size_
float voxel_size_
Definition: model_library.h:261
pcl::recognition::ModelLibrary::Model::obj_name_
const std::string obj_name_
Definition: model_library.h:164
pcl::recognition::ModelLibrary::max_coplanarity_angle_
float max_coplanarity_angle_
Definition: model_library.h:262
pcl::recognition::ModelLibrary::hash_table_
HashTable hash_table_
Definition: model_library.h:266
pcl::recognition::ModelLibrary::ignoreCoplanarPointPairsOff
void ignoreCoplanarPointPairsOff()
Call this method in order to add all point pairs (co-planar as well) to the hash table.
Definition: model_library.h:209
pcl::common::UniformGenerator::setParameters
void setParameters(T min, T max, std::uint32_t seed=-1)
Set the uniform number generator parameters.
Definition: random.hpp:84
pcl::recognition::ModelLibrary::Model::getOctreeCenterOfMass
const float * getOctreeCenterOfMass() const
Definition: model_library.h:146
pcl::recognition::ORROctree
That's a very specialized and simple octree class.
Definition: orr_octree.h:68
pcl::recognition::ModelLibrary::ignoreCoplanarPointPairsOn
void ignoreCoplanarPointPairsOn()
Call this method in order NOT to add co-planar point pairs to the hash table.
Definition: model_library.h:201
pcl::recognition::ModelLibrary::Model::getUserData
void * getUserData() const
Definition: model_library.h:140
pcl::recognition::ModelLibrary::pair_width_
float pair_width_
Definition: model_library.h:260
pcl::recognition::ModelLibrary::Model
Stores some information about the model.
Definition: model_library.h:64
pcl::recognition::aux::copy3
void copy3(const T src[3], T dst[3])
dst = src
Definition: auxiliary.h:116
pcl::recognition::ModelLibrary::Model::points_for_registration_
PointCloudIn points_for_registration_
Definition: model_library.h:168
pcl::recognition::ModelLibrary::setMaxCoplanarityAngleDegrees
void setMaxCoplanarityAngleDegrees(float max_coplanarity_angle_degrees)
This is a threshold.
Definition: model_library.h:193
pcl::common::UniformGenerator::run
T run()
Definition: random.h:132
pcl::recognition::ModelLibrary::~ModelLibrary
virtual ~ModelLibrary()
Definition: model_library.h:180
pcl::recognition::aux::add3
void add3(T a[3], const T b[3])
a += b
Definition: auxiliary.h:150
pcl::recognition::ModelLibrary::Model::getBoundsOfOctreePoints
const float * getBoundsOfOctreePoints() const
Definition: model_library.h:152
pcl::recognition::ModelLibrary::Model::~Model
virtual ~Model()
Definition: model_library.h:123
PCL_EXPORTS
#define PCL_EXPORTS
Definition: pcl_macros.h:323