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 #include "searchState.h"
00027 #include "searchStateImpl.h"
00028
00029 #include <QString>
00030
00031 #include <Inventor/nodes/SoSeparator.h>
00032 #include <Inventor/nodes/SoCube.h>
00033 #include <Inventor/nodes/SoSphere.h>
00034 #include <Inventor/nodes/SoTransform.h>
00035 #include <Inventor/nodes/SoMaterial.h>
00036 #include <Inventor/nodes/SoGroup.h>
00037 #include "SoArrow.h"
00038
00039 #include "robot.h"
00040 #include "body.h"
00041 #include "world.h"
00042 #include "eigenGrasp.h"
00043
00044
00045 #include "debug.h"
00046
00047 SearchVariable::SearchVariable(QString name, double min, double max, double maxJump, bool circular)
00048 {
00049 mName = name;
00050 mMinVal = min; mMaxVal = max;
00051 mMaxJump = maxJump;
00052 mValue = 0;
00053 mFixed = false;
00054 mCircular = circular;
00055 }
00056
00057 SearchVariable::SearchVariable(const SearchVariable *v)
00058 {
00059 mName = v->mName;
00060 mMinVal = v->mMinVal; mMaxVal = v->mMaxVal;
00061 mMaxJump = v->mMaxJump;
00062 mValue = v->mValue;
00063 mFixed = v->mFixed;
00064 mCircular = v->mCircular;
00065 }
00066
00067
00068
00069 VariableSet::~VariableSet()
00070 {
00071 clearVariables();
00072 }
00073
00074 VariableSet::VariableSet(const VariableSet &vs)
00075 {
00076 for (int var = 0; var<vs.getNumVariables(); var++) {
00077 mVariables.push_back(new SearchVariable(vs.getVariable(var)));
00078 }
00079 for (int par=0; par<(int)vs.mParameters.size(); par++) {
00080 mParameters.push_back(SearchParameter(vs.mParameters[par]));
00081 }
00082 mHand = vs.mHand;
00083 }
00084
00085 void
00086 VariableSet::clearVariables()
00087 {
00088 for (int i=0; i < (int)mVariables.size(); i++)
00089 delete mVariables[i];
00090 mVariables.clear();
00091 }
00092
00093 void
00094 VariableSet::changeHand(const Hand *h, bool sticky)
00095 {
00096 if (sticky) {
00097 assert(mHand->getNumDOF() == h->getNumDOF());
00098 assert(mHand->getEigenGrasps()->getSize() == h->getEigenGrasps()->getSize());
00099 }
00100 mHand = h;
00101 if (!sticky) {
00102 clearVariables();
00103 createVariables();
00104 }
00105 }
00106
00107 void
00108 VariableSet::setAllConfidences(double c)
00109 {
00110 for (int i=0; i<(int)mVariables.size(); i++) {
00111 mVariables[i]->setConfidence(c);
00112 }
00113 }
00114
00115 void
00116 VariableSet::setAllFixed(bool f)
00117 {
00118 for (int i=0; i<(int)mVariables.size(); i++) {
00119 mVariables[i]->setFixed(f);
00120 }
00121 }
00122
00123 int
00124 VariableSet::getNumUsedVariables() const
00125 {
00126 int n=0;
00127 for (unsigned int i=0; i<mVariables.size(); i++)
00128 if (!mVariables[i]->isFixed()) n++;
00129 return n;
00130 }
00131
00132 SearchVariable*
00133 VariableSet::getVariable(QString name)
00134 {
00135 for (unsigned int i=0; i < mVariables.size(); i++) {
00136 if (mVariables[i]->getName() == name) return mVariables[i];
00137 }
00138 DBGP("Requested variable " << name.latin1() << " not found in search state");
00139 return NULL;
00140 }
00141
00142 const SearchVariable*
00143 VariableSet::getConstVariable(QString name) const
00144 {
00145 for (unsigned int i=0; i < mVariables.size(); i++) {
00146 if (mVariables[i]->getName() == name) return mVariables[i];
00147 }
00148 DBGP("Requested variable " << name.latin1() << " not found in search state");
00149 return NULL;
00150 }
00151
00152 double
00153 VariableSet::readVariable(QString name) const
00154 {
00155 for (unsigned int i=0; i < mVariables.size(); i++) {
00156 if (mVariables[i]->getName() == name) return mVariables[i]->getValue();
00157 }
00158 DBGP("Requested variable " << name.latin1() << " not found in search state");
00159 return 0;
00160 }
00161
00162 void
00163 VariableSet::setParameter(QString name, double value)
00164 {
00165 std::vector<SearchParameter>::iterator it;
00166 for(it = mParameters.begin(); it!=mParameters.end(); it++) {
00167 if ( it->name() == name ) break;
00168 }
00169 if (it==mParameters.end()) {
00170 DBGA("Parameter " << name.latin1() << " not found!");
00171 assert(0);
00172 return;
00173 }
00174 it->set(value);
00175 }
00176
00177 double
00178 VariableSet::getParameter (QString name) const
00179 {
00180 std::vector<SearchParameter>::const_iterator it;
00181 for(it = mParameters.begin(); it!=mParameters.end(); it++) {
00182 if ( it->name() == name ) break;
00183 }
00184 if (it==mParameters.end()) {
00185 DBGA("Parameter " << name.latin1() << " not found!");
00186 assert(0);
00187 return 0;
00188 }
00189 return it->get();
00190 }
00191
00192 void
00193 VariableSet::addParameter(QString name, double value)
00194 {
00195 std::vector<SearchParameter>::iterator it;
00196 for(it = mParameters.begin(); it!=mParameters.end(); it++) {
00197 if ( it->name() == name ) break;
00198 }
00199 if (it!=mParameters.end()) {
00200 DBGA("Parameter " << name.latin1() << " already present!");
00201 assert(0);
00202 return;
00203 }
00204 mParameters.push_back(SearchParameter(name, value));
00205 }
00206
00207 void
00208 VariableSet::removeParameter(QString name)
00209 {
00210 std::vector<SearchParameter>::iterator it;
00211 for(it = mParameters.begin(); it!=mParameters.end(); it++) {
00212 if ( it->name() == name ) break;
00213 }
00214 if (it==mParameters.end()) {
00215 DBGA("Parameter " << name.latin1() << " does not exist!");
00216 assert(0);
00217 return;
00218 }
00219 mParameters.erase(it);
00220 }
00221
00222 void
00223 VariableSet::writeToFile(FILE *fp) const
00224 {
00225 fprintf(fp,"%d ",getType());
00226 for (int i=0; i<getNumVariables(); i++)
00227 fprintf(fp,"%f ",mVariables[i]->getValue() );
00228 fprintf(fp,"\n");
00229 }
00230
00231 bool
00232 VariableSet::readFromFile(FILE *fp)
00233 {
00234
00235 int type;
00236 fscanf(fp,"%d",&type);
00237 if (type != getType()) {
00238 fprintf(stderr,"Wrong type %d in state file (%d expected)\n",type,getType());
00239 return false;
00240 }
00241
00242 char line[10000];
00243 int offset;
00244 float v;
00245 if (!fgets(line, 10000, fp)) {
00246 fprintf(stderr,"Failed to read data from file!\n");
00247 return false;
00248 }
00249
00250 offset = 0;
00251
00252 for (int i=0; i<getNumVariables(); i++) {
00253 if ( line[offset]=='\0' ) {
00254 fprintf(stderr,"Line to short to read all state variables\n");
00255 return false;
00256 }
00257 while( isspace(*(line+offset)) ) offset++;
00258 sscanf(line+offset,"%f",&v);
00259 mVariables[i]->setValue(v);
00260 while( !isspace(*(line+offset)) ) offset++;
00261 }
00262 return true;
00263 }
00264
00265 bool
00266 VariableSet::readFromArray(std::vector<double> array)
00267 {
00268 if(getNumVariables() != (int) array.size())
00269 {
00270 DBGA("size does not match" << getNumVariables() << " " << array.size());
00271 return false;
00272 }
00273 for(int i = 0; i < getNumVariables(); i ++)
00274 {
00275 mVariables[i]->setValue(array[i]);
00276 }
00277 return true;
00278 }
00279
00280
00286 double
00287 VariableSet::distance(const VariableSet *s) const
00288 {
00289 if (getNumVariables() != s->getNumVariables()) return -1;
00290 double distance = 0;
00291 for (int i=0; i<getNumVariables(); i++) {
00292 double altD;
00293 double d = readVariable(i) - s->readVariable(i);
00294
00295 if ( getVariable(i)->isCircular() ) {
00296
00297
00298 altD = fabs( readVariable(i) - getVariable(i)->mMinVal ) +
00299 fabs( s->readVariable(i) - getVariable(i)->mMaxVal );
00300 d = std::min(d, altD);
00301
00302 altD = fabs( s->readVariable(i) - getVariable(i)->mMinVal ) +
00303 fabs( readVariable(i) - getVariable(i)->mMaxVal );
00304 d = std::min(d, altD);
00305 }
00306 d = fabs(d) / getVariable(i)->getRange();
00307 distance = std::max(d, distance);
00308 }
00309 return distance;
00310 }
00311
00312 void
00313 VariableSet::print() const
00314 {
00315 fprintf(stderr,"\n");
00316 fprintf(stderr,"Type: %d\n",getType());
00317 for (int i=0; i<getNumVariables(); i++) {
00318 fprintf(stderr,"%s = %.2f; ",mVariables[i]->getName().latin1(),mVariables[i]->getValue());
00319 }
00320 fprintf(stderr,"\n");
00321 }
00322
00323 PostureState* PostureState::createInstance(StateType type, const Hand *h)
00324 {
00325 switch(type) {
00326 case POSE_EIGEN:
00327 return new PostureStateEigen(h);
00328 case POSE_DOF:
00329 return new PostureStateDOF(h);
00330 default:
00331 assert(0);
00332 return NULL;
00333 }
00334 }
00335
00336 PositionState* PositionState::createInstance(StateType type, const Hand *h)
00337 {
00338 switch(type) {
00339 case SPACE_COMPLETE:
00340 return new PositionStateComplete(h);
00341 case SPACE_ELLIPSOID:
00342 return new PositionStateEllipsoid(h);
00343 case SPACE_APPROACH:
00344 return new PositionStateApproach(h);
00345 case SPACE_AXIS_ANGLE:
00346 return new PositionStateAA(h);
00347 default:
00348 assert(0);
00349 return NULL;
00350 }
00351 }
00352
00353
00354 HandObjectState::HandObjectState(Hand *h)
00355 {
00356 init();
00357 mPosture = PostureState::createInstance(POSE_EIGEN,h);
00358 mPosition = PositionState::createInstance(SPACE_COMPLETE,h);
00359 mAttributes = new AttributeSet(h);
00360 mHand = h;
00361 }
00362
00363 void
00364 HandObjectState::init()
00365 {
00366 mRefTran = transf::IDENTITY;
00367 mTargetObject = NULL;
00368 mPosture = NULL;
00369 mPosition = NULL;
00370 mAttributes = NULL;
00371 IVRoot = NULL;
00372 IVMat = NULL;
00373 IVTran = NULL;
00374 }
00375
00376 HandObjectState::~HandObjectState()
00377 {
00378 hideVisualMarker();
00379 if (IVRoot) {
00380
00381 IVRoot->unref();
00382 }
00383 delete mPosture;
00384 delete mPosition;
00385 delete mAttributes;
00386 }
00387
00388 void HandObjectState::changeHand(Hand *h, bool sticky)
00389 {
00390 mHand = h;
00391 mPosition->changeHand(h, sticky);
00392 mPosture->changeHand(h, sticky);
00393 }
00394
00395 void HandObjectState::saveCurrentHandState()
00396 {
00397 transf newTran = mHand->getTran() * mRefTran.inverse();
00398 mPosition->setTran(newTran);
00399
00400 double *dof = new double[mHand->getNumDOF()];
00401
00402 mHand->storeDOFVals(dof);
00403
00404 mPosture->storeHandDOF(dof);
00405 delete [] dof;
00406 }
00407
00413 void HandObjectState::setPositionType(StateType type, bool sticky)
00414 {
00415 if (type == mPosition->getType()) return;
00416 PositionState *p = PositionState::createInstance(type, mHand);
00417 transf t;
00418 if (sticky) {
00419 t = mPosition->getCoreTran();
00420 }
00421 delete mPosition;
00422 mPosition = p;
00423 if (sticky) {
00424 mPosition->setTran(t);
00425 } else {
00426 mPosition->reset();
00427 }
00428 }
00429
00435 void HandObjectState::setPostureType(StateType type, bool sticky)
00436 {
00437 if (type == mPosture->getType()) return;
00438 PostureState *p = PostureState::createInstance(type, mHand);
00439 double *dof;
00440 if (sticky) {
00441 dof = new double [mHand->getNumDOF()];
00442 mPosture->getHandDOF(dof);
00443 }
00444 delete mPosture;
00445 mPosture = p;
00446 if (sticky) {
00447 mPosture->storeHandDOF(dof);
00448 delete [] dof;
00449 } else {
00450 mPosture->reset();
00451 }
00452 }
00453
00458 void HandObjectState::setRefTran(transf t, bool sticky)
00459 {
00460 transf totalTran;
00461 if (sticky) {
00462 totalTran = mPosition->getCoreTran() * mRefTran;
00463 }
00464 mRefTran = t;
00465 if (sticky) {
00466 mPosition->setTran( totalTran * mRefTran.inverse() );
00467 }
00468 }
00469
00470 bool HandObjectState::readFromFile(FILE *fp)
00471 {
00472
00473 int type; fpos_t pos;
00474 fgetpos(fp,&pos);
00475 if (!fscanf(fp,"%d",&type)) return false;
00476 DBGP("Pose type: " << type);
00477 if ( (StateType)type != POSE_DOF && (StateType)type != POSE_EIGEN ) return false;
00478 if ( type != mPosture->getType() ) {
00479 setPostureType((StateType)type);
00480 }
00481 fsetpos(fp,&pos);
00482 if ( !mPosture->readFromFile(fp) ) {
00483 DBGA("Failed");
00484 return false;
00485 }
00486 fgetpos(fp,&pos);
00487 if (!fscanf(fp,"%d",&type)) return false;
00488 DBGP("Space type: " << type);
00489 if ( (StateType)type != SPACE_COMPLETE && (StateType)type != SPACE_APPROACH &&
00490 (StateType)type != SPACE_AXIS_ANGLE && (StateType)type != SPACE_ELLIPSOID ) return false;
00491 if ( type != mPosition->getType() ) {
00492 setPositionType((StateType)type);
00493 }
00494 fsetpos(fp,&pos);
00495 if ( !mPosition->readFromFile(fp) ) return false;
00496 return true;
00497 }
00498
00499 void HandObjectState::writeToFile(FILE *fp) const
00500 {
00501 mPosture->writeToFile(fp);
00502 mPosition->writeToFile(fp);
00503 }
00504
00505 bool HandObjectState::execute(Hand *h) const
00506 {
00507 if (!h) h = mHand;
00508 else assert( h->getNumDOF() == mHand->getNumDOF());
00509
00510 if (h->setTran( mPosition->getCoreTran() * mRefTran ) == FAILURE) return false;
00511 double *dof = new double[ h->getNumDOF() ];
00512 mPosture->getHandDOF(dof);
00513
00514 h->forceDOFVals( dof );
00515 delete [] dof;
00516 return true;
00517 }
00518
00519 SearchVariable* HandObjectState::getVariable(int i)
00520 {
00521 if ( i < 0 ) assert(0);
00522 else if ( i < mPosture->getNumVariables() ) return mPosture->getVariable(i);
00523 else if ( i < getNumVariables() ) return mPosition->getVariable( i - mPosture->getNumVariables() );
00524 else assert(0);
00525 return NULL;
00526 }
00527
00528 const SearchVariable* HandObjectState::getVariable(int i) const
00529 {
00530 if ( i < mPosture->getNumVariables() ) return mPosture->getVariable(i);
00531 else if ( i < getNumVariables() ) return mPosition->getVariable( i - mPosture->getNumVariables() );
00532 else assert(0);
00533 return NULL;
00534 }
00535
00536 SearchVariable* HandObjectState::getVariable (QString name)
00537 {
00538 SearchVariable *v = mPosture->getVariable(name);
00539 if (v) return v;
00540 return mPosition->getVariable(name);
00541 }
00542
00543 const SearchVariable* HandObjectState::getConstVariable (QString name) const
00544 {
00545 const SearchVariable *v = mPosture->getConstVariable(name);
00546 if (v) return v;
00547 return mPosition->getConstVariable(name);
00548 }
00549
00550 double
00551 HandObjectState::distance(const HandObjectState *s) const
00552 {
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570 transf t1 = mHand->getApproachTran() * getTotalTran();
00571 transf t2 = mHand->getApproachTran() * s->getTotalTran();
00572
00573 vec3 dvec = t1.translation() - t2.translation();
00574 double d = dvec.len();
00575
00576 if (mTargetObject->isA("GraspableBody")) {
00577 d = d / ((GraspableBody*)mTargetObject)->getMaxRadius();
00578 } else {
00579 d = d / 200;
00580 }
00581
00582 Quaternion qvec = t1.rotation() * t2.rotation().inverse();
00583 vec3 axis; double angle;
00584 qvec.ToAngleAxis(angle,axis);
00585 double q = 0.5 * fabs(angle) / M_PI;
00586
00587 return std::max(d,q);
00588 }
00589
00590 SoSeparator*
00591 HandObjectState::getIVRoot()
00592 {
00593 if (!IVRoot) {
00594 IVRoot = new SoSeparator;
00595 IVTran = new SoTransform;
00596 IVMat = new SoMaterial;
00597 IVRoot->addChild(IVTran);
00598 IVRoot->addChild(IVMat);
00599
00600 IVRoot->ref();
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640 SoArrow *arrow = new SoArrow;
00641 arrow->height = 18;
00642 arrow->cylRadius = 1.5;
00643 arrow->coneRadius = 3.25;
00644 arrow->coneHeight = 8;
00645 SoTransform *arrowTran = new SoTransform();
00646 arrowTran->rotation.setValue(SbVec3f(1,0,0),(float)(M_PI/2.0));
00647 IVRoot->addChild(arrowTran);
00648 SoTransform *moveTran = new SoTransform();
00649 moveTran->translation.setValue(SbVec3f(0, -18, 0));
00650 IVRoot->addChild(moveTran);
00651 IVRoot->addChild(arrow);
00652
00653 }
00654
00655
00656
00657
00658 transf t = mHand->getApproachTran() * getTotalTran();
00659 t.toSoTransform(IVTran);
00660 return IVRoot;
00661 }
00662
00663 void
00664 HandObjectState::setIVMarkerColor(double r, double g, double b) const
00665 {
00666 if (!IVRoot || !IVMat) {
00667 DBGA("Attempting to set marker color, but marker not created");
00668 return;
00669 }
00670 IVMat->diffuseColor = SbColor(r,g,b);
00671 IVMat->ambientColor = SbColor(r,g,b);
00672 }
00673
00674 void
00675 HandObjectState::showVisualMarker()
00676 {
00677
00678 if (IVRoot) {
00679 int i = mHand->getWorld()->getIVRoot()->findChild( IVRoot );
00680 if (i>=0) {
00681 DBGP("Search state marker is already in world root");
00682 return;
00683 }
00684 }
00685 mHand->getWorld()->getIVRoot()->addChild( getIVRoot() );
00686 }
00687
00688 void
00689 HandObjectState::hideVisualMarker()
00690 {
00691 if (!IVRoot) return;
00692 int i = mHand->getWorld()->getIVRoot()->findChild( IVRoot );
00693 if ( i < 0 ) {
00694 DBGP("Could not find search state marker in the world root");
00695 } else {
00696 mHand->getWorld()->getIVRoot()->removeChild(i);
00697 }
00698 }
00699
00700 bool
00701 GraspPlanningState::compareStates(const GraspPlanningState *s1, const GraspPlanningState *s2)
00702 {
00703 if (s1->getEnergy() <= s2->getEnergy()) return true;
00704 return false;
00705
00706 }
00707
00708 bool
00709 GraspPlanningState::compareStatesDistances(const GraspPlanningState *s1, const GraspPlanningState *s2)
00710 {
00711 if (s1->getDistance() <= s2->getDistance()) return true;
00712 return false;
00713 }
00714
00715