search_zones.cpp
Go to the documentation of this file.
00001 #include "../include/search_zones.h"
00002 #include "../include/debug.h"
00003 #include "../include/grasp.h"
00004 #include "assert.h"
00005 
00006 namespace ICR
00007 {
00008 //-------------------------------------------------------------------
00009 //-------------------------------------------------------------------
00010 PrimitiveSearchZone::PrimitiveSearchZone(){hyperplane_ids_.clear();}
00011 //-------------------------------------------------------------------
00012 std::ostream& operator<<(std::ostream& stream, PrimitiveSearchZone const& psz)
00013 {
00014   stream <<'\n'<<"PRIMITIVE SEARCH ZONE: "<<'\n'
00015          <<"Hyperplane id's: ";
00016        
00017     for(uint i=0;i<psz.hyperplane_ids_.size();i++)
00018         stream<<psz.hyperplane_ids_[i]<<" ";
00019 
00020     stream<<'\n'<<'\n';
00021 
00022   return stream;
00023 }
00024 //-------------------------------------------------------------------
00025 //-------------------------------------------------------------------
00026 SearchZones::SearchZones() : tws_(NULL),num_search_zones_(0),search_zones_computed_(false)
00027 {
00028   search_zones_.clear();
00029 }
00030 //-------------------------------------------------------------------
00031 SearchZones::SearchZones(const GraspPtr grasp) : grasp_(grasp), tws_(NULL),num_search_zones_(0),search_zones_computed_(false)
00032 {
00033   assert(grasp_->getGWS()->containsOrigin());
00034   search_zones_.clear();
00035 }
00036 //-------------------------------------------------------------------
00037 SearchZones::SearchZones(SearchZones const& src) :  grasp_(src.grasp_), tws_(src.tws_), search_zones_(src.search_zones_),
00038                                                       num_search_zones_(src.num_search_zones_), search_zones_computed_(src.search_zones_computed_),map_vertex2finger_(src.map_vertex2finger_),
00039                                                       hyperplane_normals_(src.hyperplane_normals_),hyperplane_offsets_(src.hyperplane_offsets_){}
00040 //-------------------------------------------------------------------
00041 SearchZones& SearchZones::operator=(SearchZones const& src)
00042 {
00043   if (this !=&src)
00044     {
00045       grasp_=src.grasp_;
00046       tws_=src.tws_;
00047       search_zones_=src.search_zones_;
00048       num_search_zones_=src.num_search_zones_;
00049       search_zones_computed_=src.search_zones_computed_;
00050       map_vertex2finger_=src.map_vertex2finger_;
00051       hyperplane_normals_=src.hyperplane_normals_;
00052       hyperplane_offsets_=src.hyperplane_offsets_;
00053     }
00054   return *this;
00055 }
00056 //-------------------------------------------------------------------
00057 std::ostream& operator<<(std::ostream& stream,SearchZones const& sz)
00058 {
00059   stream <<'\n'<<"SEARCH ZONES: "<<'\n'
00060          <<"Number of patch search zones: " << sz.num_search_zones_<<'\n'
00061          <<"Search zones computed: "<<std::boolalpha<<sz.search_zones_computed_<<'\n'
00062          <<"Vertex-finger map: "<<sz.map_vertex2finger_<<'\n';
00063 
00064   return stream;
00065 }
00066 //-------------------------------------------------------------------
00067 SearchZones::~SearchZones(){clear();}
00068 //-------------------------------------------------------------------
00069 SearchZone const* SearchZones::getSearchZone(uint finger_id)const{return search_zones_.at(finger_id);}
00070 //-------------------------------------------------------------------
00071 uint SearchZones::getNumSearchZones()const{return num_search_zones_;}
00072 //-------------------------------------------------------------------
00073 bool SearchZones::searchZonesComputed()const{return search_zones_computed_;}
00074 //-------------------------------------------------------------------
00075 void SearchZones::computeShiftedHyperplanes(double alpha)
00076 {
00077   double offset=grasp_->getGWS()->getOcInsphereRadius()*alpha;
00078   tws_=new SphericalWrenchSpace(6,offset);
00079   uint num_hyperplanes=grasp_->getGWS()->num_facets_;
00080   hyperplane_normals_.resize(num_hyperplanes,6);
00081   hyperplane_offsets_.resize(num_hyperplanes);
00082   facetT* curr_f=grasp_->getGWS()->conv_hull_.beginFacet().getFacetT();
00083  
00084   hyperplane_offsets_.fill(offset); 
00085      
00086   for(uint hp_id=0; hp_id < num_hyperplanes;hp_id++)
00087     {
00088       assert(offset <= (-curr_f->offset)); //Make sure that GWSinit contains the TWS and the hyperplanes are shifted inwards
00089       //The direction of the normal is switched to inward-pointing in order to be consistent with the positive offset
00090       hyperplane_normals_.row(hp_id)=(-Eigen::Map<Eigen::Matrix<double,1,6> >(curr_f->normal));
00091       curr_f=curr_f->next;
00092     }
00093 }
00094 //-------------------------------------------------------------------
00095 void SearchZones::addShiftedPrimitiveSearchZone(uint finger_id,vertexT const* curr_vtx)
00096 {
00097   setT *neighbor_facets=curr_vtx->neighbors;
00098   PrimitiveSearchZone* p_search_zone = new PrimitiveSearchZone();
00099   p_search_zone->hyperplane_ids_.reserve(qh_setsize(neighbor_facets));
00100 
00101   for(uint i=0; i < (uint)qh_setsize(neighbor_facets); i++)
00102     p_search_zone->hyperplane_ids_.push_back(((facetT*)neighbor_facets->e[i].p)->id);
00103 
00104   search_zones_[finger_id]->push_back(p_search_zone);
00105 }
00106 //-------------------------------------------------------------------
00107 void SearchZones::clear()
00108 {
00109   delete tws_;
00110   tws_=NULL;
00111   for(uint finger_id=0;finger_id < num_search_zones_;finger_id++)
00112     {
00113       for(uint prim_sz_id=0; prim_sz_id < search_zones_[finger_id]->size(); prim_sz_id++)
00114         delete (*search_zones_[finger_id])[prim_sz_id];            
00115 
00116       delete search_zones_[finger_id];
00117     }
00118   search_zones_.clear();
00119   num_search_zones_ = 0;
00120   search_zones_computed_ = false;
00121 
00122 }
00123 //-------------------------------------------------------------------
00124 void SearchZones::initializeSearchZones()
00125 {
00126   if(search_zones_computed_) //Clear up possible previously computed search zones
00127      clear();
00128 
00129   num_search_zones_=grasp_->getNumFingers();
00130   search_zones_.reserve(num_search_zones_);
00131   map_vertex2finger_.resize(1,num_search_zones_);
00132 
00133   for(uint finger_id=0; finger_id < num_search_zones_; finger_id++)
00134     {
00135       search_zones_.push_back(new SearchZone());
00136       //reserve for the maximum number of primitive search zones for each finger (i.e all wrenches are vertexes of the gws and span a primitive search zone)
00137       search_zones_[finger_id]->reserve(grasp_->getFinger(finger_id)->getCenterPointPatch()->patch_ids_.size()
00138                                        *grasp_->getFinger(finger_id)->getContactModel()->getLimitSurface()->getNumPrimitiveWrenches());
00139 
00140       //the elements of map_vertex2finger_ describe the relation between a vertex-index from the grasp wrench space to the finger this vertex belongs.
00141       //I.e. if the value of vertex id <= than map_vertex2finger_(0), then the vertex is a wrench belonging to the first finger, if vertex id <= than 
00142       //map_vertex2finger_(1) the vertex belongs to the second finger and so on. E.g, a 3-fingered grasp comprising patches with 2 contact points each and 10 
00143       //primitive wrenches in each contact point would have map_vertex2finger_=[19 39 59] 
00144       map_vertex2finger_(finger_id)=grasp_->getFinger(finger_id)->getCenterPointPatch()->patch_ids_.size()
00145                             *grasp_->getFinger(finger_id)->getContactModel()->getLimitSurface()->getNumPrimitiveWrenches()-1;
00146       if (finger_id > 0)
00147         map_vertex2finger_(finger_id)=map_vertex2finger_(finger_id)+map_vertex2finger_(finger_id-1)+1;
00148     }
00149 }
00150 //--------------------------------------------------------------------
00151 void SearchZones::computeShiftedSearchZones(double alpha)
00152 {
00153   assert(alpha > 0 && alpha <= 1);
00154   assert(grasp_->getGWS()->containsOrigin());
00155   initializeSearchZones();
00156   computeShiftedHyperplanes(alpha);
00157 
00158   //iterate through the vertices of the GWS and create the appropriate Primitive Search Zone for each 
00159   //vertex and push it back on the search zone of its corresponding finger
00160   orgQhull::QhullVertex curr_vtx=grasp_->getGWS()->conv_hull_.beginVertex();
00161 
00162   for (uint vtx_count=0; vtx_count < grasp_->getGWS()->num_vtx_;vtx_count++)
00163     {
00164       //determine to which finger the current vertex belongs and create an appropriate primitive search zone and push it on the list for the corresponding
00165       //finger
00166       for(uint finger_id=0; finger_id < num_search_zones_; finger_id++)
00167         {
00168           if((uint)curr_vtx.point().id() <= map_vertex2finger_(finger_id))
00169             {
00170               addShiftedPrimitiveSearchZone(finger_id,curr_vtx.getVertexT());
00171               break;
00172             }
00173         }
00174 
00175       curr_vtx=curr_vtx.next();
00176     }
00177   search_zones_computed_=true;
00178 }
00179 //--------------------------------------------------------------------
00180 Eigen::Matrix<double,Eigen::Dynamic,6> const* SearchZones::getHyperplaneNormals()const{return &hyperplane_normals_;}
00181 //--------------------------------------------------------------------
00182 Eigen::VectorXd const* SearchZones::getHyperplaneOffsets()const{return &hyperplane_offsets_;}
00183 //--------------------------------------------------------------------
00184 const GraspPtr SearchZones::getGrasp()const{return grasp_;}
00185 //--------------------------------------------------------------------
00186 WrenchSpace const* SearchZones::getTWS()const
00187 {
00188   assert(search_zones_computed_);
00189   return tws_;
00190 }
00191 //--------------------------------------------------------------------
00192 void SearchZones::resetPrimitiveSearchZones(uint sz_id)
00193 {
00194   for(uint psz_id=0; psz_id < search_zones_[sz_id]->size(); psz_id++)
00195     (*search_zones_[sz_id])[psz_id]->satisfied_wc_ids_.resize(0);  
00196 }
00197 //--------------------------------------------------------------------
00198 void SearchZones::resetSearchZones()
00199 {
00200   for(uint sz_id=0;sz_id < num_search_zones_; sz_id++)
00201     resetPrimitiveSearchZones(sz_id);
00202 }
00203 //--------------------------------------------------------------------
00204 //--------------------------------------------------------------------
00205 }//namespace ICR


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:33:19