Go to the documentation of this file.00001 #include "../include/grasp.h"
00002 #include "../include/ows.h"
00003 #include "../include/debug.h"
00004 #include <iostream>
00005 #include "assert.h"
00006
00007 namespace ICR
00008 {
00009
00010
00011 Grasp::Grasp() : initialized_(false) , gws_(NULL),num_fingers_(0),num_grasp_wrenches_(0){}
00012
00013 Grasp::Grasp(Grasp const& src) : fingers_(src.fingers_), initialized_(src.initialized_),
00014 obj_(src.obj_),gws_(src.gws_),num_fingers_(src.num_fingers_),num_grasp_wrenches_(src.num_grasp_wrenches_) {}
00015
00016 Grasp& Grasp::operator=(Grasp const& src)
00017 {
00018 if (this !=&src)
00019 {
00020 fingers_=src.fingers_;
00021 initialized_=src.initialized_;
00022 obj_=src.obj_;
00023 gws_=src.gws_;
00024 num_fingers_=src.num_fingers_;
00025 num_grasp_wrenches_=src.num_grasp_wrenches_;
00026 }
00027 return *this;
00028 }
00029
00030 std::ostream& operator<<(std::ostream& stream, Grasp const& grasp)
00031 {
00032 stream <<'\n'<<"GRASP: "<<'\n'
00033 <<"Number of fingers: " << grasp.num_fingers_ <<'\n'
00034 <<"Number of grasp wrenches: " << grasp.num_grasp_wrenches_ <<'\n'
00035 <<"Is initialized: "<<std::boolalpha<<grasp.initialized_<<'\n'<<'\n';
00036
00037 return stream;
00038 }
00039
00040 Grasp::~Grasp(){clear();}
00041
00042 void Grasp::clear()
00043 {
00044 delete gws_;
00045 for(uint i=0; i < num_fingers_; i++)
00046 delete fingers_[i];
00047
00048 fingers_.clear();
00049 }
00050
00051 void Grasp::computeGWS()
00052 {
00053 double* wrenches=new double[num_grasp_wrenches_*gws_->getDimension()];
00054 double* wrench_it=wrenches;
00055 uint wrench_cone_cardinality=0;
00056
00057
00058 for(uint i=0;i<num_fingers_;i++)
00059 {
00060 wrench_cone_cardinality=fingers_[i]->getOWS()->getLimitSurface()->getNumPrimitiveWrenches();
00061
00062
00063 for(ConstIndexListIterator curr_pt = fingers_[i]->getCenterPointPatch()->patch_ids_.begin(); curr_pt != fingers_[i]->getCenterPointPatch()->patch_ids_.end();curr_pt++)
00064 {
00065 Eigen::Map<Matrix6Xd >(wrench_it,6,wrench_cone_cardinality)=*(fingers_[i]->getOWS()->getWrenchCone(*curr_pt)->getWrenches());
00066 wrench_it+=gws_->getDimension()*wrench_cone_cardinality;
00067 }
00068 }
00069
00070 gws_->computeConvexHull(wrenches,num_grasp_wrenches_);
00071 delete[] wrenches;
00072 }
00073
00074 PatchListPtr Grasp::computePatches(Finger* new_finger)
00075 {
00076 PatchListPtr patches;
00077 if(new_finger->getContactModel()->getModelType()==Single_Point)
00078 {
00079 if( fingers_.size() > 0)
00080 {
00081 patches=fingers_[0]->getPatches();
00082 return patches;
00083 }
00084
00085 patches=PatchListPtr(new std::vector<Patch* >);
00086 patches->reserve(obj_->getNumCp());
00087 for(uint centerpoint_id=0; centerpoint_id < obj_->getNumCp();centerpoint_id++)
00088 patches->push_back(new Patch(centerpoint_id));
00089 }
00090 else if(new_finger->getContactModel()->getModelType()==Multi_Point)
00091 {
00092 for(uint id=0; id < fingers_.size(); id++)
00093 {
00094 if(dynamic_cast<MultiPointContactModel*>(fingers_[id]->getContactModel())->getInclusionRule() == dynamic_cast<MultiPointContactModel*>(new_finger->getContactModel())->getInclusionRule())
00095 {
00096 patches=fingers_[id]->getPatches();
00097 return patches;
00098 }
00099 }
00100 patches=PatchListPtr(new std::vector<Patch* >);
00101 patches->reserve(obj_->getNumCp());
00102 for(uint centerpoint_id=0; centerpoint_id < obj_->getNumCp();centerpoint_id++)
00103 (*patches.get())[centerpoint_id]= new Patch(centerpoint_id,*obj_,*dynamic_cast<MultiPointContactModel*>(new_finger->getContactModel())->getInclusionRule());
00104 }
00105 else
00106 {
00107 std::cout<<"Error in Grasp::computePatches - Invalid contact model type. Exiting..."<<std::endl;
00108 exit(1);
00109 }
00110 return patches;
00111 }
00112
00113 OWSPtr Grasp::computeOWS(Finger const* new_finger)
00114 {
00115 OWSPtr ows;
00116 for(uint id=0; id < fingers_.size(); id++)
00117 {
00118 if( *fingers_[id]->getContactModel()->getLimitSurface() == *new_finger->getContactModel()->getLimitSurface())
00119 {
00120 ows=fingers_[id]->getOWS();
00121 return ows;
00122 }
00123 }
00124
00125 ows=OWSPtr(new OWS());
00126 ows->init(*obj_,*new_finger->getContactModel()->getLimitSurface());
00127 return ows;
00128 }
00129
00130 void Grasp::addFinger(FingerParameters const& param, uint centerpoint_id)
00131 {
00132 Finger* new_finger=new Finger(param);
00133
00134
00135 new_finger->setName(param.getName());
00136 new_finger->init(centerpoint_id,computePatches(new_finger),computeOWS(new_finger));
00137 num_grasp_wrenches_+=(new_finger->getCenterPointPatch()->patch_ids_.size())*(new_finger->getContactModel()->getLimitSurface()->getNumPrimitiveWrenches());
00138 fingers_.push_back(new_finger);
00139 }
00140
00141 Finger const* Grasp::getFinger(uint id) const {return fingers_.at(id);}
00142
00143 uint Grasp::getNumFingers()const{return num_fingers_;}
00144
00145 void Grasp::init(FParamList const& f_param_list,const TargetObjectPtr obj,VectorXui const& centerpoint_ids)
00146 {
00147 assert(f_param_list.size()>0);
00148 assert(f_param_list.size()==(uint)centerpoint_ids.size());
00149 assert((bool)obj);
00150
00151 if(initialized_)
00152 clear();
00153
00154 gws_=new DiscreteWrenchSpace();
00155
00156 num_fingers_=f_param_list.size();
00157 for (uint i=0;i < num_fingers_;i++)
00158 assert(centerpoint_ids(i) < obj->getNumCp());
00159
00160 obj_=obj;
00161 for (uint i=0; i< num_fingers_;i++)
00162 addFinger(f_param_list[i],(uint)centerpoint_ids(i));
00163
00164 computeGWS();
00165 initialized_=true;
00166 }
00167
00168 void Grasp::setCenterPointId(uint finger_id,uint centerpoint_id)
00169 {
00170
00171 assert(initialized_);
00172 assert(centerpoint_id <= obj_->getNumCp()-1);
00173 num_grasp_wrenches_-=(fingers_.at(finger_id)->getCenterPointPatch()->patch_ids_.size())*(fingers_.at(finger_id)->getContactModel()->getLimitSurface()->getNumPrimitiveWrenches());
00174 fingers_.at(finger_id)->setCenterPointId(centerpoint_id);
00175 num_grasp_wrenches_+=(fingers_.at(finger_id)->getCenterPointPatch()->patch_ids_.size())*(fingers_.at(finger_id)->getContactModel()->getLimitSurface()->getNumPrimitiveWrenches());
00176 delete gws_;
00177 gws_ = new DiscreteWrenchSpace();
00178 computeGWS();
00179 }
00180
00181 void Grasp::setCenterPointIds(VectorXui const& centerpoint_ids)
00182 {
00183
00184 assert(initialized_);
00185 assert(centerpoint_ids.size()==(int)num_fingers_);
00186
00187 for(uint finger_id=0; finger_id < num_fingers_; finger_id++)
00188 {
00189 assert(centerpoint_ids(finger_id) <= obj_->getNumCp()-1);
00190 num_grasp_wrenches_-=(fingers_.at(finger_id)->getCenterPointPatch()->patch_ids_.size())*(fingers_.at(finger_id)->getContactModel()->getLimitSurface()->getNumPrimitiveWrenches());
00191 fingers_.at(finger_id)->setCenterPointId(centerpoint_ids(finger_id));
00192 num_grasp_wrenches_+=(fingers_.at(finger_id)->getCenterPointPatch()->patch_ids_.size())*(fingers_.at(finger_id)->getContactModel()->getLimitSurface()->getNumPrimitiveWrenches());
00193 }
00194 delete gws_;
00195 gws_ = new DiscreteWrenchSpace();
00196 computeGWS();
00197 }
00198
00199 bool Grasp::isInitialized()const{return initialized_;}
00200
00201 const TargetObjectPtr Grasp::getParentObj()const{return obj_;}
00202
00203 DiscreteWrenchSpace const* Grasp::getGWS()const{return gws_;}
00204
00205
00206 }