finger.cpp
Go to the documentation of this file.
00001 #include "../include/finger.h"
00002 #include "../include/contact_model.h"
00003 #include "../include/ows.h"
00004 #include "../include/debug.h"
00005 #include "../include/config.h"
00006 #include "assert.h"
00007 
00008 namespace ICR
00009 {
00010 //--------------------------------------------------------------------------
00011 //--------------------------------------------------------------------------
00012 InclusionRule::InclusionRule() : 
00013   rule_parameter_(DEFAULT_INCLUSION_RULE_PARAMETER),
00014   rule_type_(DEFAULT_INCLUSION_RULE_TYPE),
00015   filter_inside_points_(DEFAULT_INCLUSION_RULE_FILTER_INSIDE_POINTS) {}
00016 //------------------------------------------------------
00017   InclusionRule::InclusionRule(double rp_in, RuleType rt_in, bool filter_in) : 
00018     filter_inside_points_(filter_in) 
00019   {
00020     if (rp_in < 0) {
00021       rule_parameter_ = DEFAULT_INCLUSION_RULE_PARAMETER;
00022     } else {
00023       rule_parameter_ = rp_in;
00024     }
00025 
00026     if (rt_in != Undefined_RT || rt_in != Sphere) {
00027       rule_type_ = DEFAULT_INCLUSION_RULE_TYPE;
00028     } else {
00029       rule_type_ = rt_in;
00030     }
00031   }
00032 //------------------------------------------------------
00033 bool InclusionRule::inclusionTest(ContactPoint const* center_point, ContactPoint const* test_point)const
00034 {
00035   if (rule_type_==Sphere)
00036     {
00037      if( ((*center_point->getVertex())-(*test_point->getVertex())).norm() <= rule_parameter_ )
00038         return true;
00039     }
00040   else
00041     {
00042       std::cout<<"Error in InclusionRule::inclusionTest: Invalid rule type. Exiting..."<<std::endl;
00043       exit(1);
00044     }
00045 
00046   return false;
00047 }
00048 //--------------------------------------------------------------------------
00049 std::ostream& operator<<(std::ostream &stream,InclusionRule const& inclusion_rule)
00050 {
00051   std::string rule_type;
00052 
00053  if (inclusion_rule.rule_type_==Sphere) rule_type="Sphere";
00054   else if (inclusion_rule.rule_type_==Undefined_RT)  rule_type="Undefined";
00055   else rule_type="Warning in InclusionRule: Invalid rule type!";
00056 
00057   stream <<'\n'<<"INCLUSION RULE: "<<'\n'
00058          <<"Inclusion rule type: "<<rule_type<<'\n'
00059          <<"Inclusion rule parameter: "<<inclusion_rule.rule_parameter_<<'\n'
00060          <<"Points inside a patch are filtered out: "<<std::boolalpha<<inclusion_rule.filter_inside_points_<<'\n'<<'\n';
00061 
00062   return stream;
00063 }
00064 //--------------------------------------------------------------------------
00065 //--------------------------------------------------------------------------
00066 Node::Node() : contact_point_(NULL), inside_patch_(true){}
00067 //--------------------------------------------------------------------------
00068 Node::Node(ContactPoint* contact_point) : contact_point_(contact_point), inside_patch_(true){}
00069 //--------------------------------------------------------------------------
00070 std::ostream& operator<<(std::ostream& stream, Node const& node)
00071 {
00072   stream <<'\n'<<"NODE: "<<'\n';
00073 
00074   if(node.contact_point_ != NULL)
00075     {
00076       stream <<"Associated contact point id: "<<node.contact_point_->getId()<<'\n'
00077              <<"Is inside patch: "<<std::boolalpha<<node.inside_patch_<<'\n';
00078     }
00079   else
00080     stream <<"Contact point pointer not set"<<'\n';
00081 
00082   stream<<'\n';   
00083 
00084   return stream;
00085 }
00086 //--------------------------------------------------------------------------
00087 //--------------------------------------------------------------------------
00088 Patch::Patch(){}
00089 //--------------------------------------------------------------------------
00090 Patch::Patch(uint centerpoint_id){patch_ids_.push_back(centerpoint_id);}
00091 //--------------------------------------------------------------------------
00092 Patch::Patch(uint centerpoint_id,TargetObject const& obj, InclusionRule const& rule)
00093 {
00094   //Breadth first exploration of the mesh with the centerpoint of the patch as root node. The criterion to qualify a
00095   //point for inclusion in the patch is that the InclusionRule::inclusionTest is satisfied. The vector 'nodes' acts as a FIFO
00096   //list for the search; explored_cp(i) states if contact point i was already explored during the search and whether it qualified 
00097   //for inclusion in the patch or not. If the filter_inside_points_ flag of the LimitSuface is set, the patch is filtered so that it
00098   //contains only centerpoint and border points, not the points inside the patch. The criterion for a point to be on the border of the 
00099   //patch is, that not all of its neighbors qualified for patch inclusion.
00100 
00101   std::list<Node> nodes;
00102   VectorXui explored_cp=VectorXui::Zero(obj.getNumCp(),1); //set all entries to NOT_EXPLORED
00103 
00104   nodes.push_back(Node(const_cast<ContactPoint*>(obj.getContactPoint(centerpoint_id)))); //push the node corresponding to the centerpoint of the patch
00105   explored_cp(centerpoint_id)=EXPLORED_QUALIFIED;
00106  
00107   while(nodes.size() > 0)
00108     {   
00109       for(ConstIndexListIterator neighbor=nodes.front().contact_point_->getNeighborItBegin(); neighbor != nodes.front().contact_point_->getNeighborItEnd(); neighbor++)      
00110         {
00111           if(explored_cp(*neighbor)==NOT_EXPLORED)
00112             {         
00113               if(rule.inclusionTest(obj.getContactPoint(centerpoint_id) , obj.getContactPoint(*neighbor)))
00114                 {
00115                   nodes.push_back(Node(const_cast<ContactPoint*>(obj.getContactPoint(*neighbor))));
00116                   explored_cp(*neighbor)=EXPLORED_QUALIFIED;
00117                 }
00118               else
00119                 {
00120                  //If not all neighbors of the investigated node qualify for patch inclusion, the contact point associated with the node
00121                  //is a border point of the patch.
00122                   nodes.front().inside_patch_=false; 
00123                   explored_cp(*neighbor)=EXPLORED_UNQUALIFIED;
00124                 }
00125             }
00126           //The same as above, not all neighbors of the current node qualify for inclusion, thus the contact point is on the border
00127           else if(explored_cp(*neighbor)==EXPLORED_UNQUALIFIED)
00128             nodes.front().inside_patch_=false;
00129         }
00130 
00131       if(rule.filter_inside_points_)//Filtering of points inside the patch, if the according flag is set
00132         {
00133           if((!nodes.front().inside_patch_) | (nodes.front().contact_point_->getId()==centerpoint_id))//The centerpoint always qualifies for patch inclusion
00134             patch_ids_.push_back(nodes.front().contact_point_->getId());
00135         }
00136       else
00137           patch_ids_.push_back(nodes.front().contact_point_->getId());
00138 
00139       nodes.pop_front();
00140     }
00141 }
00142 //--------------------------------------------------------------------------
00143 std::ostream& operator<<(std::ostream& stream,Patch const& patch)
00144 {
00145   stream <<'\n'<<"PATCH: "<<'\n'
00146          <<"Number of contact points in the patch: "<< patch.patch_ids_.size() <<'\n'
00147          <<"Patch id's: ";
00148 
00149   for(ConstIndexListIterator it=patch.patch_ids_.begin(); it != patch.patch_ids_.end(); it++)
00150     stream<< *it <<" ";
00151 
00152   stream<<'\n'<<'\n';
00153 
00154   return stream;
00155 }
00156 //---------------------------------------------------------------
00157 //---------------------------------------------------------------
00158 FingerParameters::FingerParameters() :
00159   name_("Default finger"),
00160   force_magnitude_(DEFAULT_FORCE_MAGNITUDE), 
00161   disc_(DEFAULT_DISC), 
00162   mu_0_(DEFAULT_MU_0),  
00163   mu_T_(DEFAULT_MU_T),
00164   contact_type_(DEFAULT_CONTACT_TYPE),
00165   model_type_(DEFAULT_CONTACT_MODEL_TYPE),
00166   inclusion_rule_(InclusionRule(DEFAULT_INCLUSION_RULE_PARAMETER)) {}
00167 //--------------------------------------------------------------------------
00168 FingerParameters::FingerParameters(FingerParameters const& src) : 
00169   name_(src.name_),
00170   force_magnitude_(src.force_magnitude_), 
00171   disc_(src.disc_),
00172   mu_0_(src.mu_0_), 
00173   mu_T_(src.mu_T_), 
00174   contact_type_(src.contact_type_),
00175   model_type_(src.model_type_), 
00176   inclusion_rule_(src.inclusion_rule_) {}
00177 //--------------------------------------------------------------------------
00178 FingerParameters::FingerParameters(string name, 
00179                                    double force_magnitude_in,
00180                                    uint disc_in,
00181                                    double mu_0_in, 
00182                                    double mu_T_in, 
00183                                    ContactType contact_type_in, 
00184                                    ModelType model_type_in, 
00185                                    double radius_in) {
00186   name_ = name;
00187   if (force_magnitude_in <= 0) {
00188     force_magnitude_ = DEFAULT_FORCE_MAGNITUDE;
00189   } else {
00190       force_magnitude_ = force_magnitude_in;
00191   }
00192   
00193   if (disc_in < 4) {
00194     disc_ = DEFAULT_DISC;
00195   } else {
00196     disc_ = disc_in;
00197   }
00198   if(mu_0_in <= 0) {
00199     mu_0_ = DEFAULT_MU_0;
00200   } else {
00201     mu_0_ = mu_0_in;
00202   }
00203   if(mu_T_in <= 0) {
00204     mu_T_ = DEFAULT_MU_T;
00205   } else {
00206     mu_T_ = mu_T_in;
00207   }
00208   if(contact_type_in != Undefined_CT || contact_type_in != Frictional ||
00209      contact_type_in != Frictional || contact_type_in != Soft_Finger) {
00210     contact_type_ = DEFAULT_CONTACT_TYPE;
00211   } else {
00212     contact_type_ = contact_type_in;
00213   }
00214   if(model_type_in != Undefined_MT || model_type_in != Single_Point || model_type_in != Multi_Point) {
00215     model_type_ = DEFAULT_CONTACT_MODEL_TYPE;
00216   } else {
00217     model_type_ = model_type_in;
00218   }
00219   if (radius_in < 0) {
00220     inclusion_rule_ = InclusionRule(DEFAULT_INCLUSION_RULE_PARAMETER);
00221   } else {
00222     inclusion_rule_ = InclusionRule(radius_in);
00223   }
00224 }
00225 //--------------------------------------------------------------------------
00226 FingerParameters& FingerParameters::operator=(FingerParameters const& src)
00227 {
00228   if (this !=&src)
00229     {
00230       name_ = src.name_;
00231       force_magnitude_=src.force_magnitude_;
00232       disc_=src.disc_;
00233       mu_0_=src.mu_0_;
00234       mu_T_=src.mu_T_;
00235       contact_type_=src.contact_type_;
00236       model_type_=src.model_type_;
00237       inclusion_rule_=src.inclusion_rule_;
00238     }
00239   return *this;
00240 }
00241 //--------------------------------------------------------------------------
00242 std::ostream& operator<<(std::ostream &stream,FingerParameters const& param)
00243 {
00244   std::string contact_type;
00245   std::string model_type;
00246   std::string rule_type;
00247 
00248     if (param.contact_type_==Frictionless) contact_type="Frictionless";
00249   else if (param.contact_type_==Frictional)   contact_type="Frictional";
00250   else if (param.contact_type_==Soft_Finger)  contact_type="Soft Finger";
00251   else if (param.contact_type_==Undefined_CT)  contact_type="Undefined";
00252   else contact_type="Warning in FingerParameters: Invalid contact type!";
00253 
00254      if (param.model_type_==Single_Point) model_type="Single Point";
00255   else if (param.model_type_==Multi_Point)   model_type="Multi Point";
00256   else if (param.model_type_==Undefined_MT)  model_type="Undefined";
00257   else model_type="Warning in FingerParameters: Invalid model type!";
00258 
00259  if (param.inclusion_rule_.rule_type_==Sphere) rule_type="Sphere";
00260   else if (param.inclusion_rule_.rule_type_==Undefined_RT)  rule_type="Undefined";
00261   else rule_type="Warning in FingerParameters: Invalid rule type!";
00262 
00263   stream <<'\n'<<"FINGER PARAMETERS: "<<'\n'
00264          <<"Name: " << param.name_ << '\n'
00265          <<"Force magnitude: " << param.force_magnitude_ <<'\n'
00266          <<"Discretization: "<<param.disc_<<'\n'
00267          <<"Mu_0: "<<param.mu_0_<<'\n'
00268          <<"Mu_T: "<<param.mu_T_<<'\n'
00269          <<"Contact type: "<<contact_type<<'\n'
00270          <<"Model type: "<<model_type<<'\n'
00271          <<"Inclusion rule type: "<<rule_type<<'\n'
00272          <<"Inclusion rule parameter: "<<param.inclusion_rule_.rule_parameter_<<'\n'<< std::endl;
00273 
00274   return stream;
00275 }        
00276 //--------------------------------------------------------------------------                   
00277 FingerParameters::~FingerParameters(){}
00278 //--------------------------------------------------------------------------
00279 void FingerParameters::setFrictionlessContact(double force_magnitude)
00280 {
00281   assert(force_magnitude_ > 0);
00282   force_magnitude_=force_magnitude;
00283   contact_type_=Frictionless;
00284 }
00285 //--------------------------------------------------------------------------
00286 void FingerParameters::setFrictionalContact(double force_magnitude,uint disc,double mu_0)
00287 {
00288   assert(force_magnitude_ > 0);
00289   assert(disc > 0);
00290   assert(mu_0 > 0);
00291   force_magnitude_=force_magnitude;
00292   disc_=disc;
00293   mu_0_=mu_0;
00294   contact_type_=Frictional;
00295 }
00296 //--------------------------------------------------------------------------
00297 void FingerParameters::setSoftFingerContact(double force_magnitude,uint disc,double mu_0,double mu_T)
00298 {
00299   assert(force_magnitude_ > 0);
00300   assert(disc > 0);
00301   assert(mu_0 > 0);
00302   assert(mu_T > 0);
00303   force_magnitude_=force_magnitude;
00304   disc_=disc;
00305   mu_0_=mu_0;
00306   mu_T_=mu_T;
00307   contact_type_=Soft_Finger;
00308 }
00309 
00310 void FingerParameters::setContactType(ContactType contact_type_in) {
00311   contact_type_ = contact_type_in;
00312 }
00313 
00314 void FingerParameters::setContactType(string &contact_type_in) {
00315   if (contact_type_in.compare("Frictionless") == 0) {
00316     contact_type_ = Frictionless;
00317   } else if (contact_type_in.compare("Frictional") == 0) {
00318     contact_type_ = Frictional;
00319   } else if (contact_type_in.compare("Soft_Finger") == 0) {
00320     contact_type_ = Soft_Finger;
00321   } else {
00322     contact_type_ = Undefined_CT;
00323   } 
00324 }
00325 
00326 //--------------------------------------------------------------------------
00327 void FingerParameters::setContactModelType(ModelType model_type){model_type_=model_type;}
00328 
00329 void FingerParameters::setContactModelType(std::string &model_type_in){
00330   if (model_type_in.compare("Single_Point") == 0) {
00331     model_type_=Single_Point;
00332   } else if (model_type_in.compare("Multi_Point") == 0) {
00333     model_type_=Single_Point;
00334   } else {
00335     model_type_=Undefined_MT;
00336   }
00337 }
00338 //--------------------------------------------------------------------------
00339 void FingerParameters::setInclusionRule(InclusionRule const& inclusion_rule){inclusion_rule_=inclusion_rule;}
00340 //--------------------------------------------------------------------------
00341 void FingerParameters::setInclusionRuleType(RuleType rule_type){inclusion_rule_.rule_type_=rule_type;}
00342 
00343 void FingerParameters::setInclusionRuleType(std::string &rule_type_in) {
00344   if (rule_type_in.compare("Sphere") == 0) {
00345     inclusion_rule_.rule_type_=Sphere;
00346   } else {
00347     inclusion_rule_.rule_type_=Undefined_RT;
00348   }
00349 }
00350 
00351 //--------------------------------------------------------------------------
00352 void FingerParameters::setInclusionRuleParameter(double rule_parameter){inclusion_rule_.rule_parameter_=rule_parameter;}
00353 //--------------------------------------------------------------------------
00354 void FingerParameters::setInclusionRuleFilterPatch(bool filter_inside_points){inclusion_rule_.filter_inside_points_=filter_inside_points;}
00355 //--------------------------------------------------------------------------
00356 double FingerParameters::getForceMagnitude()const{return force_magnitude_;}
00357 //--------------------------------------------------------------------------
00358 uint FingerParameters::getDisc()const{return disc_;}
00359 //--------------------------------------------------------------------------
00360 std::string FingerParameters::getName()const{return name_;}
00361 //--------------------------------------------------------------------------
00362 double FingerParameters::getMu0()const{return mu_0_;}
00363 //--------------------------------------------------------------------------
00364 double FingerParameters::getMuT()const{return mu_T_;}
00365 //-------------------------------------------------------------------
00366 ContactType FingerParameters::getContactType()const{return contact_type_;}
00367 //-------------------------------------------------------------------
00368 ModelType FingerParameters::getModelType()const{return model_type_;}
00369 //-------------------------------------------------------------------
00370 InclusionRule const* FingerParameters::getInclusionRule()const{return &inclusion_rule_;}
00371 //-------------------------------------------------------------------
00372 RuleType FingerParameters::getInclusionRuleType()const{return inclusion_rule_.rule_type_;}
00373 //-------------------------------------------------------------------
00374 uint FingerParameters::getInclusionRuleParameter()const{return inclusion_rule_.rule_parameter_;}
00375 //-------------------------------------------------------------------
00376 bool FingerParameters:: getInclusionRuleFilterPatch()const{return inclusion_rule_.filter_inside_points_;}
00377 //-------------------------------------------------------------------
00378 //-------------------------------------------------------------------
00379 Finger::Finger() : c_model_(NULL), centerpoint_id_(0), initialized_(false), name_("default")
00380 {
00381   FingerParameters default_param;
00382 
00383   if (default_param.getModelType()==Single_Point)
00384     c_model_=new PointContactModel(default_param); 
00385   else if (default_param.getModelType()==Multi_Point)
00386     c_model_=new MultiPointContactModel(default_param); 
00387   else
00388     {
00389       std::cout<<"Error in Finger: Invalid contact model type. exiting..."<<std::endl;
00390       exit(1);
00391     }
00392 }
00393 //--------------------------------------------------------------------------
00394 Finger::Finger(FingerParameters const& param) : 
00395   c_model_(NULL), 
00396   centerpoint_id_(0), 
00397   initialized_(false)
00398 {
00399   if (param.getModelType()==Single_Point)
00400     c_model_=new PointContactModel(param); 
00401   else if (param.getModelType()==Multi_Point)
00402     c_model_=new MultiPointContactModel(param); 
00403   else
00404     {
00405       std::cout<<"Error in Finger: Invalid contact model type. exiting..."<<std::endl;
00406       exit(1);
00407     }
00408 }
00409 //--------------------------------------------------------------------------
00410 Finger::Finger(Finger const& src) : c_model_(src.c_model_), ows_(src.ows_), patches_(src.patches_),
00411                                     centerpoint_id_(src.centerpoint_id_), initialized_(src.initialized_),name_("default"){}
00412 //--------------------------------------------------------------------------
00413 Finger& Finger::operator=(const Finger& src)              
00414 {
00415   if (this !=&src)
00416     {
00417       c_model_=src.c_model_;
00418       ows_=src.ows_;
00419       patches_=src.patches_;
00420       centerpoint_id_=src.centerpoint_id_;
00421       initialized_=src.initialized_;
00422       name_=src.name_;
00423     }
00424   return *this;
00425 }
00426 //--------------------------------------------------------------------------
00427 std::ostream& operator<<(std::ostream& stream,Finger const& finger)
00428 {
00429   stream <<'\n'<<"FINGER: "<<'\n'
00430          <<"Is initialized: "<<std::boolalpha<<finger.initialized_<<'\n'
00431          <<"Centerpoint id: "<<finger.centerpoint_id_<<'\n'<<'\n';
00432 
00433   return stream;
00434 }
00435 //--------------------------------------------------------------------------
00436 Finger::~Finger()
00437 {
00438   delete c_model_;
00439   if (patches_.use_count()==1) //The last shared pointer to a patch list has to clean up the allocated patches
00440      for(uint i=0; i < patches_->size();i++)
00441        delete (*patches_.get())[i];
00442 }
00443 //--------------------------------------------------------------------------
00444 Patch const* Finger::getCenterPointPatch()const{return (*patches_.get())[centerpoint_id_];}
00445 //--------------------------------------------------------------------------
00446 Patch const* Finger::getPatch(uint id)const{return (*patches_.get())[id];}
00447 //--------------------------------------------------------------------------
00448 const OWSPtr Finger::getOWS()const{return ows_;}
00449 //--------------------------------------------------------------------------
00450 string Finger::getName()const{return name_;}
00451 //--------------------------------------------------------------------------
00452 void Finger::setName(std::string const & name){name_=name;}
00453 //--------------------------------------------------------------------------
00454 PointContactModel const* Finger::getContactModel()const{return c_model_;}
00455 //--------------------------------------------------------------------------
00456 PointContactModel* Finger::getContactModel(){return c_model_;}
00457 //--------------------------------------------------------------------------
00458 const PatchListPtr Finger::getPatches()const{return patches_;}
00459 //--------------------------------------------------------------------------
00460 uint Finger::getCenterPointId()const{return centerpoint_id_;}
00461 //--------------------------------------------------------------------------
00462 bool Finger::isInitialized()const{return initialized_;}
00463 //--------------------------------------------------------------------------
00464 void Finger::setCenterPointId(uint centerpoint_id)
00465 {
00466   assert(initialized_);
00467   centerpoint_id_=centerpoint_id;
00468 }
00469 //--------------------------------------------------------------------------
00470 void Finger::init(uint centerpoint_id, const PatchListPtr patches, const OWSPtr ows)
00471 {
00472   assert((bool)patches);
00473   assert((bool)ows);
00474   centerpoint_id_=centerpoint_id;
00475   patches_=patches;
00476   ows_=ows;
00477   initialized_=true;
00478 }
00479 //--------------------------------------------------------------------------
00480 //--------------------------------------------------------------------------
00481 }//namespace ICR
00482 


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:40