Go to the documentation of this file.00001 #include "../include/independent_contact_regions.h"
00002 #include "../include/wrench_cone.h"
00003 #include "../include/search_zones.h"
00004 #include "../include/ows.h"
00005 #include "../include/grasp.h"
00006 #include "../include/target_object.h"
00007 #include "../include/contact_point.h"
00008 #include "../include/debug.h"
00009 #include "../include/config.h"
00010 #include "assert.h"
00011 #include <thread>
00012
00013 namespace ICR
00014 {
00015
00016 IndependentContactRegions::IndependentContactRegions() :
00017 icr_computed_(false),
00018 num_contact_regions_(0) {contact_regions_.clear();}
00019
00020 IndependentContactRegions::IndependentContactRegions(const SearchZonesPtr search_zones,const GraspPtr grasp) :
00021 search_zones_(search_zones),
00022 grasp_(grasp),
00023 icr_computed_(false),
00024 num_contact_regions_(0)
00025 {
00026 contact_regions_.clear();
00027 assert((bool)search_zones_);
00028 assert((bool)grasp_);
00029 }
00030
00031 IndependentContactRegions::IndependentContactRegions(IndependentContactRegions const& src) :
00032 search_zones_(src.search_zones_),
00033 grasp_(src.grasp_),
00034 icr_computed_(src.icr_computed_),
00035 contact_regions_(src.contact_regions_),
00036 num_contact_regions_(src.num_contact_regions_){}
00037
00038 IndependentContactRegions& IndependentContactRegions::operator=(IndependentContactRegions const& src)
00039 {
00040 if (this !=&src)
00041 {
00042 search_zones_=src.search_zones_;
00043 grasp_=src.grasp_;
00044 icr_computed_=src.icr_computed_;
00045 contact_regions_=src.contact_regions_;
00046 num_contact_regions_=src.num_contact_regions_;
00047 }
00048
00049 return *this;
00050 }
00051
00052
00053 std::ostream& operator<<(std::ostream& stream, IndependentContactRegions const& icr)
00054 {
00055 stream <<'\n'<<"IndependentContactRegions: "<<'\n'
00056 <<"Number of contact regions: " << icr.num_contact_regions_<<'\n'
00057 <<"Contact regions computed: "<<std::boolalpha<<icr.icr_computed_<<'\n';
00058 if(icr.icr_computed_)
00059 {
00060 for(uint i=0;i<icr.num_contact_regions_;i++)
00061 {
00062 stream<<"Centerpoint id's region "<<i<<": ";
00063 for(uint j=0; j < icr.contact_regions_[i]->size();j++)
00064 {
00065 stream<<(*icr.contact_regions_[i])[j]->patch_ids_.front()<<" ";
00066 }
00067 stream<<'\n';
00068 }
00069 }
00070 stream<<'\n';
00071 return stream;
00072 }
00073
00074 IndependentContactRegions::~IndependentContactRegions(){clear();}
00075
00076 void IndependentContactRegions::clear()
00077 {
00078 for(uint i=0;i<contact_regions_.size();i++)
00079 {
00080 delete contact_regions_[i];
00081 }
00082 contact_regions_.clear();
00083 num_contact_regions_ = 0;
00084 icr_computed_ = false;
00085 }
00086
00087 bool IndependentContactRegions::primitiveSearchZoneInclusionTest(PrimitiveSearchZone* prim_sz,WrenchCone const* wc)const
00088 {
00089 if ((prim_sz->satisfied_wc_ids_.array() == wc->id_).any())
00090 return true;
00091
00092
00093 bool constraints_satisfied;
00094 for(uint pw_count=0; pw_count < wc->num_primitive_wrenches_;pw_count++)
00095 {
00096 constraints_satisfied=true;
00097 for(uint hp_count=0; hp_count< prim_sz->hyperplane_ids_.size(); hp_count++)
00098 {
00099 if(search_zones_->hyperplane_normals_.row(prim_sz->hyperplane_ids_[hp_count]) * wc->cone_.col(pw_count) + search_zones_->hyperplane_offsets_(prim_sz->hyperplane_ids_[hp_count]) > 0)
00100 {
00101 constraints_satisfied=false;
00102 break;
00103 }
00104 }
00105 if(constraints_satisfied)
00106 {
00107 prim_sz->satisfied_wc_ids_.conservativeResize(prim_sz->satisfied_wc_ids_.size()+1);
00108 prim_sz->satisfied_wc_ids_(prim_sz->satisfied_wc_ids_.size()-1)=wc->id_;
00109 return true;
00110 }
00111 }
00112 return false;
00113 }
00114
00115 bool IndependentContactRegions::searchZoneInclusionTest(uint region_id,Patch const* patch)const
00116 {
00117
00118 bool psz_satisfied;
00119 for(uint psz_id=0; psz_id < search_zones_->search_zones_[region_id]->size();psz_id++)
00120 {
00121
00122
00123 psz_satisfied=false;
00124 for(ConstIndexListIterator patch_point=patch->patch_ids_.begin(); patch_point != patch->patch_ids_.end(); patch_point++)
00125 {
00126 if(primitiveSearchZoneInclusionTest((*search_zones_->search_zones_[region_id])[psz_id] ,grasp_->getFinger(region_id)->getOWS()->getWrenchCone(*patch_point)))
00127 {
00128 psz_satisfied=true;
00129 break;
00130 }
00131 }
00132 if(!psz_satisfied)
00133 return false;
00134 }
00135 return true;
00136 }
00137
00138 void IndependentContactRegions::computeContactRegion(uint region_id)
00139 {
00140 search_zones_->resetPrimitiveSearchZones(region_id);
00141 std::list<Node> nodes;
00142 VectorXui explored_cp=VectorXui::Zero(grasp_->getParentObj()->getNumCp(),1);
00143 uint init_centerpoint_id=grasp_->getFinger(region_id)->getCenterPointPatch()->patch_ids_.front();
00144
00145 contact_regions_[region_id]->reserve(grasp_->getParentObj()->getNumCp());
00146 nodes.push_back(Node(const_cast<ContactPoint*>(grasp_->getParentObj()->getContactPoint(init_centerpoint_id))));
00147 contact_regions_[region_id]->push_back(const_cast<Patch*>(grasp_->getFinger(region_id)->getCenterPointPatch()));
00148 explored_cp(init_centerpoint_id)=EXPLORED_QUALIFIED;
00149
00150 while(nodes.size() > 0)
00151 {
00152 for(ConstIndexListIterator neighbor=nodes.front().contact_point_->getNeighborItBegin(); neighbor != nodes.front().contact_point_->getNeighborItEnd(); neighbor++)
00153 {
00154 if(explored_cp(*neighbor)==NOT_EXPLORED)
00155 {
00156 if(searchZoneInclusionTest(region_id,grasp_->getFinger(region_id)->getPatch(*neighbor)))
00157 {
00158 nodes.push_back(Node(const_cast<ContactPoint*>(grasp_->getParentObj()->getContactPoint(*neighbor))));
00159 contact_regions_[region_id]->push_back(const_cast<Patch*>(grasp_->getFinger(region_id)->getPatch(*neighbor)));
00160 explored_cp(*neighbor)=EXPLORED_QUALIFIED;
00161 }
00162 else
00163 explored_cp(*neighbor)=EXPLORED_UNQUALIFIED;
00164 }
00165 }
00166 nodes.pop_front();
00167 }
00168 }
00169
00170 void IndependentContactRegions::computeICR()
00171 {
00172 assert((bool)search_zones_);
00173 assert((bool)grasp_);
00174
00175 if(icr_computed_)
00176 clear();
00177
00178 num_contact_regions_=search_zones_->num_search_zones_;
00179 contact_regions_.reserve(num_contact_regions_);
00180
00181 #ifdef MULTITHREAD_ICR_COMPUTATION
00182 std::vector<std::thread*> threads;
00183 threads.reserve(num_contact_regions_);
00184 for(uint region_id=0; region_id < num_contact_regions_; region_id++)
00185 {
00186 contact_regions_.push_back(new ContactRegion);
00187 threads.push_back(new std::thread(&IndependentContactRegions::computeContactRegion,this,region_id));
00188 }
00189 for(uint thread_id=0; thread_id < num_contact_regions_; thread_id++)
00190 {
00191 threads[thread_id]->join();
00192 delete threads[thread_id];
00193 }
00194 #else
00195 for(uint region_id=0; region_id < num_contact_regions_; region_id++)
00196 {
00197 contact_regions_.push_back(new ContactRegion);
00198 computeContactRegion(region_id);
00199 }
00200 #endif
00201
00202 icr_computed_=true;
00203 }
00204
00205 bool IndependentContactRegions::icrComputed()const{return icr_computed_;}
00206
00207 ContactRegion const* IndependentContactRegions::getContactRegion(uint id)const{return contact_regions_.at(id);}
00208
00209 uint IndependentContactRegions::getNumContactRegions()const{return num_contact_regions_;}
00210
00211 const SearchZonesPtr IndependentContactRegions::getSearchZones()const{return search_zones_;}
00212
00213 void IndependentContactRegions::setSearchZones(SearchZonesPtr sz_in)
00214 {
00215 clear();
00216 search_zones_ = sz_in;
00217 }
00218
00219 void IndependentContactRegions::setGrasp(GraspPtr g_in)
00220 {
00221 clear();
00222 grasp_ = g_in;
00223 }
00224
00225 bool IndependentContactRegions::hasInitializedGrasp() {
00226 return (grasp_ != NULL && grasp_->isInitialized());
00227 }
00228
00229 }