Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <pcl/recognition/ransac_based/model_library.h>
00041 #include <pcl/recognition/ransac_based/obj_rec_ransac.h>
00042 #include <pcl/kdtree/kdtree_flann.h>
00043 #include <pcl/kdtree/impl/kdtree_flann.hpp>
00044 #include <pcl/console/print.h>
00045 #include <cmath>
00046
00047 using namespace std;
00048 using namespace pcl;
00049 using namespace console;
00050 using namespace pcl::recognition;
00051
00052
00053
00054 ModelLibrary::ModelLibrary (float pair_width, float voxel_size, float max_coplanarity_angle)
00055 : pair_width_ (pair_width),
00056 voxel_size_ (voxel_size),
00057 max_coplanarity_angle_ (max_coplanarity_angle),
00058 ignore_coplanar_opps_ (true)
00059 {
00060 num_of_cells_[0] = 60;
00061 num_of_cells_[1] = 60;
00062 num_of_cells_[2] = 60;
00063
00064
00065 float eps = 0.000001f;
00066 float bounds[6] = {-eps, static_cast<float> (M_PI) + eps,
00067 -eps, static_cast<float> (M_PI) + eps,
00068 -eps, static_cast<float> (M_PI) + eps};
00069
00070 hash_table_.build (bounds, num_of_cells_);
00071 }
00072
00073
00074
00075 void
00076 ModelLibrary::clear ()
00077 {
00078 this->removeAllModels ();
00079
00080 num_of_cells_[0] = num_of_cells_[1] = num_of_cells_[2] = 0;
00081 hash_table_.clear ();
00082 }
00083
00084
00085
00086 void
00087 ModelLibrary::removeAllModels ()
00088 {
00089
00090 for ( map<string,Model*>::iterator it = models_.begin() ; it != models_.end() ; ++it )
00091 delete it->second;
00092 models_.clear();
00093
00094
00095 HashTableCell* cells = hash_table_.getVoxels();
00096 int num_bins = num_of_cells_[0]*num_of_cells_[1]*num_of_cells_[2];
00097
00098
00099 for ( int i = 0 ; i < num_bins ; ++i )
00100 cells[i].clear();
00101 }
00102
00103
00104
00105 bool
00106 ModelLibrary::addModel (const PointCloudIn& points, const PointCloudN& normals, const std::string& object_name,
00107 float frac_of_points_for_registration, void* user_data)
00108 {
00109 #ifdef OBJ_REC_RANSAC_VERBOSE
00110 printf("ModelLibrary::%s(): begin [%s]\n", __func__, object_name.c_str ());
00111 #endif
00112
00113
00114 pair<map<string,Model*>::iterator, bool> result = models_.insert (pair<string,Model*> (object_name, static_cast<Model*> (NULL)));
00115
00116
00117 if (!result.second)
00118 {
00119 print_error ("'%s' already exists in the model library.\n", object_name.c_str ());
00120 return (false);
00121 }
00122
00123
00124 Model* new_model = new Model (points, normals, voxel_size_, object_name, frac_of_points_for_registration, user_data);
00125 result.first->second = new_model;
00126
00127 const ORROctree& octree = new_model->getOctree ();
00128 const vector<ORROctree::Node*> &full_leaves = octree.getFullLeaves ();
00129 list<ORROctree::Node*> inter_leaves;
00130 int num_of_pairs = 0;
00131
00132
00133 for ( vector<ORROctree::Node*>::const_iterator leaf1 = full_leaves.begin () ; leaf1 != full_leaves.end () ; ++leaf1 )
00134 {
00135 const ORROctree::Node::Data* node_data1 = (*leaf1)->getData ();
00136
00137
00138 inter_leaves.clear ();
00139 octree.getFullLeavesIntersectedBySphere (node_data1->getPoint (), pair_width_, inter_leaves);
00140
00141 for ( list<ORROctree::Node*>::iterator leaf2 = inter_leaves.begin () ; leaf2 != inter_leaves.end () ; ++leaf2 )
00142 {
00143
00144 if ( this->addToHashTable(new_model, node_data1, (*leaf2)->getData ()) )
00145 ++num_of_pairs;
00146 }
00147 }
00148
00149 #ifdef OBJ_REC_RANSAC_VERBOSE
00150 printf("ModelLibrary::%s(): end [%i oriented point pairs]\n", __func__, num_of_pairs);
00151 #endif
00152
00153 return (true);
00154 }
00155
00156
00157
00158 bool
00159 ModelLibrary::addToHashTable (Model* model, const ORROctree::Node::Data* data1, const ORROctree::Node::Data* data2)
00160 {
00161 float key[3];
00162
00163
00164 ObjRecRANSAC::compute_oriented_point_pair_signature (
00165 data1->getPoint (), data1->getNormal (),
00166 data2->getPoint (), data2->getNormal (), key);
00167
00168 if ( ignore_coplanar_opps_ )
00169 {
00170
00171
00172 if ( std::fabs (key[0]-AUX_HALF_PI) < max_coplanarity_angle_ && key[2] < max_coplanarity_angle_ )
00173 return (false);
00174 }
00175
00176
00177 HashTableCell* cell = hash_table_.getVoxel (key);
00178
00179
00180 (*cell)[model].push_back (std::pair<const ORROctree::Node::Data*, const ORROctree::Node::Data*> (data1, data2));
00181
00182 return (true);
00183 }
00184
00185