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
00030 #include <map>
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <math.h>
00034 #include <exception>
00035 #include <stdexcept>
00036 #include <typeinfo>
00037
00038 #include <q3listbox.h>
00039 #include <QApplication>
00040 #include <QThread>
00041
00042 #include <Inventor/SoDB.h>
00043 #include <Inventor/SoInput.h>
00044 #include <Inventor/SoPickedPoint.h>
00045 #include <Inventor/SoOffscreenRenderer.h>
00046 #include <Inventor/actions/SoGetBoundingBoxAction.h>
00047 #include <Inventor/actions/SoGetMatrixAction.h>
00048 #include <Inventor/actions/SoGLRenderAction.h>
00049 #include <Inventor/actions/SoBoxHighlightRenderAction.h>
00050 #include <Inventor/actions/SoSearchAction.h>
00051 #include <Inventor/actions/SoWriteAction.h>
00052 #include <Inventor/draggers/SoHandleBoxDragger.h>
00053 #include <Inventor/draggers/SoRotateDiscDragger.h>
00054 #include <Inventor/draggers/SoCenterballDragger.h>
00055 #include <Inventor/draggers/SoTranslate1Dragger.h>
00056 #include <Inventor/events/SoMouseButtonEvent.h>
00057 #include <Inventor/events/SoKeyboardEvent.h>
00058 #include <Inventor/fields/SoSFTime.h>
00059 #include <Inventor/manips/SoHandleBoxManip.h>
00060 #include <Inventor/manips/SoCenterballManip.h>
00061 #include <Inventor/nodekits/SoWrapperKit.h>
00062 #include <Inventor/nodes/SoBaseColor.h>
00063 #include <Inventor/nodes/SoBlinker.h>
00064 #include <Inventor/nodes/SoColorIndex.h>
00065 #include <Inventor/nodes/SoCoordinate3.h>
00066 #include <Inventor/nodes/SoCube.h>
00067 #include <Inventor/nodes/SoCylinder.h>
00068 #include <Inventor/nodes/SoDirectionalLight.h>
00069 #include <Inventor/nodes/SoDrawStyle.h>
00070 #include <Inventor/nodes/SoEventCallback.h>
00071 #include <Inventor/nodes/SoFont.h>
00072 #include <Inventor/nodes/SoGroup.h>
00073 #include <Inventor/nodes/SoIndexedFaceSet.h>
00074 #include <Inventor/nodes/SoLight.h>
00075 #include <Inventor/nodes/SoLightModel.h>
00076 #include <Inventor/nodes/SoMaterial.h>
00077 #include <Inventor/nodes/SoOrthographicCamera.h>
00078 #include <Inventor/nodes/SoPerspectiveCamera.h>
00079 #include <Inventor/nodes/SoPickStyle.h>
00080 #include <Inventor/nodes/SoRotation.h>
00081 #include <Inventor/nodes/SoSelection.h>
00082 #include <Inventor/nodes/SoSphere.h>
00083 #include <Inventor/nodes/SoFaceSet.h>
00084 #include <Inventor/nodes/SoTransformSeparator.h>
00085 #include <Inventor/nodes/SoTranslation.h>
00086 #include <Inventor/nodes/SoText2.h>
00087 #include <Inventor/nodes/SoFile.h>
00088 #include <Inventor/sensors/SoIdleSensor.h>
00089 #include <Inventor/sensors/SoNodeSensor.h>
00090 #include <Inventor/SoSceneManager.h>
00091 #include <Inventor/Qt/SoQt.h>
00092
00093 #include "pointers.dat"
00094 #include "ivmgr.h"
00095 #include "SoArrow.h"
00096 #include "SoTorquePointer.h"
00097 #include "SoComplexShape.h"
00098 #include "robot.h"
00099 #include "joint.h"
00100 #include "humanHand.h"
00101 #include "body.h"
00102 #include "contact.h"
00103 #include "grasp.h"
00104 #include "world.h"
00105 #include "mainWindow.h"
00106 #include "matvec3D.h"
00107
00108 #include "graspitGUI.h"
00109
00110
00111 #include "debug.h"
00112
00113 #ifdef USE_DMALLOC
00114 include "dmalloc.h"
00115 #endif
00116
00117 #define FORCE_SCALE 5.0
00118
00119 #ifdef GRASPITDBG
00120 extern FILE *debugfile;
00121 #endif
00122
00124
00142 struct DraggerInfo {
00144 WorldElement *selectedElement;
00145
00147 DOF *dof;
00148
00150 SoSeparator *draggerSep;
00151
00153 SoDragger *dragger;
00154
00156 SoTransform *draggerTran;
00157
00159 SbVec3f lastTran;
00160
00162 SbRotation lastRot;
00163
00165 SbVec3f lastCent;
00166
00168 vec3 centerballTransl;
00169
00171 double lastVal;
00172 };
00173
00175 std::list<DraggerInfo *> draggerInfoList;
00176
00177
00178 void
00179 StereoViewer::computeSeekFinalOrientation()
00180 {
00181 float f = getCamera()->focalDistance.getValue();
00182 float sb = mFocalPlane / f;
00183 getCamera()->setBalanceAdjustment( sb );
00184 }
00185
00186
00187 void StereoViewer::setStereo(bool s)
00188 {
00189 if (s) {
00190 stereoOn = true;
00191 setStereoType( SoQtViewer::STEREO_QUADBUFFER );
00192 getCamera()->setStereoAdjustment(100.0);
00193 setStereoOffset(10.0);
00194 computeSeekFinalOrientation();
00195 } else {
00196 stereoOn = false;
00197 setStereoType( SoQtViewer::STEREO_NONE );
00198 }
00199 }
00200
00201 StereoViewer::StereoViewer(QWidget *parent) : SoQtExaminerViewer(parent)
00202 {
00203 stereoOn = false;
00204 mFocalPlane = 200.0;
00205 }
00206
00207 IVmgr *IVmgr::ivmgr = 0;
00208
00209
00219 IVmgr::IVmgr(QWidget *parent, const char *name, Qt::WFlags f) :
00220 QWidget(parent,name,f)
00221 {
00222 ivmgr = this;
00223 camerafp = NULL;
00224 CtrlDown = FALSE;
00225 ShiftDown = FALSE;
00226 dynForceMat = NULL;
00227
00228 currTool = TRANSLATE_TOOL;
00229
00230 #ifdef GRASPITDBG
00231 printf("Starting Inventor...\n");
00232 #endif
00233
00234
00235 world = new World(NULL,"mainWorld", this);
00236 setupPointers();
00237
00238
00239 myViewer = new StereoViewer(parent);
00240
00241
00242
00243 sceneRoot = new SoSeparator;
00244 sceneRoot->ref();
00245
00246
00247 draggerRoot = new SoSeparator;
00248 sceneRoot->addChild(draggerRoot);
00249
00250
00251 SoEventCallback *keyEventNode = new SoEventCallback;
00252 keyEventNode->addEventCallback(SoKeyboardEvent::getClassTypeId(), keyPressedCB,NULL);
00253 sceneRoot->addChild(keyEventNode);
00254
00255
00256 SoEventCallback *mouseEventCB = new SoEventCallback;
00257 mouseEventCB->addEventCallback(SoMouseButtonEvent::getClassTypeId(), shiftOrCtrlDownCB);
00258 sceneRoot->addChild(mouseEventCB);
00259
00260
00261 junk = new SoSeparator; junk->ref();
00262
00263
00264 selectionRoot = new SoSelection;
00265 sceneRoot->addChild(selectionRoot);
00266
00267
00268 selectionRoot->addSelectionCallback(selectionCB, NULL);
00269 selectionRoot->addDeselectionCallback(deselectionCB, NULL);
00270 selectionRoot->setPickFilterCallback(pickFilterCB, NULL);
00271 selectionRoot->addChild(world->getIVRoot());
00272
00273 wireFrameRoot = new SoSeparator;
00274 sceneRoot->addChild(wireFrameRoot);
00275
00276
00277
00278
00279
00280 myViewer->show();
00281 myViewer->setSceneGraph(sceneRoot);
00282 myViewer->setTransparencyType(SoGLRenderAction::DELAYED_BLEND);
00283 myViewer->setBackgroundColor(SbColor(1,1,1));
00284
00285 myViewer->viewAll();
00286 mDBMgr = NULL;
00287 mDBMgr = NULL;
00288 }
00289
00291 void
00292 IVmgr::setStereoWindow(QWidget *parent)
00293 {
00294 StereoViewer *sViewer = new StereoViewer(parent);
00295 sViewer->show();
00296 sViewer->setSceneGraph(sceneRoot);
00297 sViewer->setTransparencyType(SoGLRenderAction::DELAYED_BLEND);
00298 sViewer->setBackgroundColor(SbColor(1,1,1));
00299
00300 sViewer->viewAll();
00301 sViewer->setDecoration(false);
00302 }
00303
00308 IVmgr::~IVmgr()
00309 {
00310 if (camerafp) fclose(camerafp);
00311 std::cout << "deleting IVmgr" << std::endl;
00312 selectionRoot->deselectAll();
00313 delete world;
00314 junk->unref();
00315 if (dynForceMat) dynForceMat->unref();
00316 pointers->unref();
00317 sceneRoot->unref();
00318 delete myViewer;
00319 }
00320
00324 void
00325 IVmgr::emptyWorld()
00326 {
00327 selectionRoot->deselectAll();
00328 selectionRoot->removeChild(world->getIVRoot());
00329 delete world;
00330 world = new World(NULL, "MainWorld", this);
00331
00332
00333 selectionRoot->addChild(world->getIVRoot());
00334 }
00335
00339 void IVmgr::setTool(ToolType newTool)
00340 {
00341 selectionRoot->deselectAll();
00342 currTool = newTool;
00343 }
00344
00350 void
00351 IVmgr::setupPointers()
00352 {
00353 SoInput in;
00354 in.setBuffer((void *) pointersData, (size_t) sizeof(pointersData));
00355 pointers = SoDB::readAll(&in);
00356
00357 pointers->ref();
00358 }
00359
00363 void
00364 IVmgr::beginMainLoop()
00365 {
00366
00367 SoQt::mainLoop();
00368 }
00369
00374 void
00375 IVmgr::drawUnbalancedForces()
00376 {
00377 for (int b=0; b<world->getNumGB(); b++) {
00378 drawBodyWrench( world->getGB(b), world->getGB(b)->getExtWrenchAcc() );
00379 }
00380 }
00381
00388 void
00389 IVmgr::drawWorstCaseWrenches()
00390 {
00391 Grasp *grasp;
00392 double minWrench[6];
00393
00394 for (int h=0;h<world->getNumHands();h++) {
00395 grasp = world->getHand(h)->getGrasp();
00396 if (!grasp->getObject()) continue;
00397
00398
00399 grasp->getMinWrench(minWrench);
00400
00401
00402 double forceScale = minWrench[0]*minWrench[0] + minWrench[1]*minWrench[1] + minWrench[2]*minWrench[2];
00403 forceScale = sqrt(forceScale);
00404 double torqueScale = minWrench[3]*minWrench[3] + minWrench[4]*minWrench[4] + minWrench[5]*minWrench[5];
00405 torqueScale = sqrt(torqueScale);
00406 double scale = 5.0 / std::max(forceScale, torqueScale);
00407
00408
00409 for (int i=0; i<6; i++) {
00410 minWrench[i] = -scale * minWrench[i];
00411 }
00412
00413 drawBodyWrench( grasp->getObject(), minWrench );
00414 }
00415 }
00416
00417 void
00418 IVmgr::drawBodyWrench(GraspableBody *body, const double *wrench)
00419 {
00420 SoSeparator *fSep = NULL;
00421 SoSeparator *tSep = NULL;
00422 body->getIVWorstCase()->removeAllChildren();
00423
00424 SbVec3f forceDir(wrench[0],wrench[1],wrench[2]);
00425 if (forceDir.length() > 0.0) {
00426 SoArrow *forcePtr = new SoArrow;
00427 forcePtr->height = forceDir.length()*FORCE_SCALE;
00428 if (forceDir.length()*FORCE_SCALE > 200.0) {
00429 DBGP("Too long.");
00430 forcePtr->height = 200.0;
00431 }
00432
00433 SoRotation *fRot = new SoRotation;
00434 fRot->rotation.setValue(SbRotation(SbVec3f(0,1,0),forceDir));
00435
00436 fSep = new SoSeparator;
00437 fSep->addChild(fRot);
00438 fSep->addChild(forcePtr);
00439 }
00440
00441 SbVec3f torqueDir(wrench[3],wrench[4],wrench[5]);
00442 if (torqueDir.length() > 0.0) {
00443 SoTorquePointer *torquePtr = new SoTorquePointer;
00444 torquePtr->height = torqueDir.length()*FORCE_SCALE;
00445 if (torqueDir.length()*FORCE_SCALE > 200.0) {
00446 DBGP("Too long.");
00447 torquePtr->height = 200.0;
00448 }
00449
00450 SoRotation *tRot = new SoRotation;
00451 tRot->rotation.setValue(SbRotation(SbVec3f(0,1,0),torqueDir));
00452
00453 tSep = new SoSeparator;
00454 tSep->addChild(tRot);
00455 tSep->addChild(torquePtr);
00456 }
00457
00458 if (fSep || tSep) {
00459 SoMaterial *mat = new SoMaterial;
00460 mat->diffuseColor = SbColor(0.8f,0,0.8f);
00461 mat->ambientColor = SbColor(0.2f,0,0.2f);
00462 mat->emissiveColor = SbColor(0.4f,0,0.4f);
00463
00464 SoSeparator *pointerRoot = new SoSeparator;
00465 pointerRoot->addChild(mat);
00466 if (fSep) pointerRoot->addChild(fSep);
00467 if (tSep) pointerRoot->addChild(tSep);
00468
00469
00470 body->getIVWorstCase()->addChild(pointerRoot);
00471 }
00472 }
00473
00482 void
00483 IVmgr::drawDynamicForces()
00484 {
00485 std::list<Contact *> contactList;
00486 std::list<Contact *>::iterator cp;
00487 SbVec3f forceVec;
00488 double *contactForce;
00489
00490 if (!dynForceMat) {
00491 dynForceMat = new SoMaterial;
00492 dynForceMat->diffuseColor = SbColor(0.8f,0.8f,0);
00493 dynForceMat->ambientColor = SbColor(0.2f,0.2f,0);
00494
00495 dynForceMat->ref();
00496 }
00497
00498 int numContacts=0;
00499 if (!world->dynamicsAreOn()) {
00500
00501 for (int b=0; b<world->getNumGB(); b++) {
00502 int sz = world->getGB(b)->getContacts().size();
00503 numContacts += sz;
00504 int numChildren = world->getGB(b)->getIVContactIndicators()->getNumChildren();
00505 for (int i=numChildren-1; i>=sz; i--){
00506 world->getGB(b)->getIVContactIndicators()->removeChild(i);
00507 }
00508 }
00509 contactForceBlinkerVec.clear();
00510 if (numContacts == 0) return;
00511 contactForceBlinkerVec.reserve(numContacts);
00512 }
00513
00514 for (int b=0; b<world->getNumGB(); b++) {
00515 contactList = world->getGB(b)->getContacts();
00516 for (cp=contactList.begin();cp!=contactList.end();cp++) {
00517 contactForce = (*cp)->getDynamicContactWrench();
00518 forceVec.setValue(contactForce[0],contactForce[1],contactForce[2]);
00519 forceVec *= FORCE_SCALE;
00520
00521 if (forceVec.length() > 200) {
00522 forceVec *= 200.0 / forceVec.length();
00523 }
00524 SoArrow *arrow = new SoArrow;
00525 arrow->height = forceVec.length();
00526 arrow->cylRadius = 0.25;
00527 arrow->coneRadius = 0.5;
00528 if (arrow->height.getValue() < arrow->coneHeight.getValue()) {
00529 arrow->coneHeight = arrow->height.getValue() / 2.0;
00530 }
00531 SoTransform *tran = new SoTransform;
00532 (*cp)->getContactFrame().toSoTransform(tran);
00533 SoRotation *rot = new SoRotation;
00534 rot->rotation.setValue(SbRotation(SbVec3f(0,1,0),forceVec));
00535
00536 SoSeparator *ptr = new SoSeparator;
00537 ptr->addChild(tran);
00538 ptr->addChild(rot);
00539 ptr->addChild(dynForceMat);
00540 ptr->addChild(arrow);
00541
00542 int lastChild = world->getGB(b)->getIVContactIndicators()->getNumChildren();
00543 if (world->dynamicsAreOn()) {
00544 world->getGB(b)->getIVContactIndicators()->insertChild(ptr, lastChild);
00545 } else {
00546 contactForceBlinkerVec.push_back(new SoBlinker);
00547 contactForceBlinkerVec.back()->addChild(ptr);
00548 contactForceBlinkerVec.back()->on = false;
00549 contactForceBlinkerVec.back()->whichChild = 0;
00550 world->getGB(b)->getIVContactIndicators()->insertChild(
00551 contactForceBlinkerVec.back(),lastChild);
00552 }
00553 }
00554 }
00555 }
00556
00561 void
00562 IVmgr::hilightObjContact(int contactNum)
00563 {
00564 if ((int)contactForceBlinkerVec.size() > contactNum) {
00565 contactForceBlinkerVec[contactNum]->on = true;
00566 } else {
00567 DBGA("Highlight blinker " << contactNum << " requested, but only "
00568 << contactForceBlinkerVec.size() << " present.");
00569 }
00570 }
00571
00576 void
00577 IVmgr::unhilightObjContact(int contactNum)
00578 {
00579 if ((int)contactForceBlinkerVec.size() > contactNum) {
00580 contactForceBlinkerVec[contactNum]->on = false;
00581 contactForceBlinkerVec[contactNum]->whichChild = 0;
00582 } else {
00583 DBGA("Unhighlight blinker " << contactNum << " requested, but only "
00584 << contactForceBlinkerVec.size() << " present.");
00585 }
00586 }
00587
00598 void
00599 IVmgr::transRot(DraggerInfo *dInfo)
00600 {
00601
00602
00603
00604 SoCenterballDragger *myCenterball;
00605 SoHandleBoxDragger *myHandleBox;
00606 bool translating;
00607
00608 Quaternion origRotation,desiredRotation;
00609 vec3 origTranslation,desiredTranslation, center,scale;
00610 transf newTran;
00611
00612
00613 if (dInfo->dragger->isOfType(SoHandleBoxDragger::getClassTypeId())) {
00614 myHandleBox = (SoHandleBoxDragger *)dInfo->dragger;
00615 translating = true;
00616 } else {
00617 myCenterball = (SoCenterballDragger *)dInfo->dragger;
00618 translating = false;
00619 }
00620
00621
00622
00623
00624 if (!translating) {
00625 SbVec3f centerTran = myCenterball->center.getValue() - dInfo->lastCent;
00626 if (centerTran.length() > 1.0e-3) {
00627 DBGP("RECENTERING");
00628 DBGP("current center: " << myCenterball->center.getValue()[0] << " " <<
00629 myCenterball->center.getValue()[1] << " " <<
00630 myCenterball->center.getValue()[2]);
00631 DBGP("last center: " << dInfo->lastCent[0] << " " <<
00632 dInfo->lastCent[1] << " " <<
00633 dInfo->lastCent[2]);
00634 scale.set(dInfo->draggerTran->scaleFactor.getValue());
00635 center.set(myCenterball->center.getValue());
00636 center*=scale[0];
00637 transf recenterTran = translate_transf(center)*
00638 dInfo->selectedElement->getTran() * translate_transf(-center);
00639 dInfo->centerballTransl = recenterTran.translation();
00640 dInfo->lastCent = myCenterball->center.getValue();
00641 return;
00642 }
00643 }
00644
00645
00646
00647
00648
00649
00650 origTranslation = dInfo->selectedElement->getTran().translation();
00651 origRotation = dInfo->selectedElement->getTran().rotation();
00652
00653
00654
00655 SbVec3f temp = dInfo->lastTran;
00656 if (translating) {
00657 if ((myHandleBox->translation.getValue() - dInfo->lastTran).length()<0.00001) {
00658 myHandleBox->translation.setValue(dInfo->lastTran);
00659
00660 return;
00661 }
00662 desiredTranslation.set(myHandleBox->translation.getValue());
00663 scale.set(dInfo->draggerTran->scaleFactor.getValue());
00664 desiredTranslation[0] *= scale[0];
00665 desiredTranslation[1] *= scale[1];
00666 desiredTranslation[2] *= scale[2];
00667
00668 desiredTranslation = origRotation*desiredTranslation;
00669
00670 DBGP("desired Translation: " << desiredTranslation);
00671
00672
00673 newTran = transf(origRotation,desiredTranslation);
00674 } else {
00675 if (myCenterball->rotation.getValue().equals(dInfo->lastRot,0.00001f)) {
00676 myCenterball->rotation.setValue(dInfo->lastRot);
00677
00678 return;
00679 }
00680 desiredRotation.set(myCenterball->rotation.getValue());
00681 scale.set(dInfo->draggerTran->scaleFactor.getValue());
00682 center.set(myCenterball->center.getValue());
00683 center*=scale[0];
00684
00685
00686 newTran = translate_transf(-center) * transf(desiredRotation,vec3::ZERO) *
00687 translate_transf(center) * translate_transf(dInfo->centerballTransl);
00688 }
00689 dInfo->selectedElement->moveTo(newTran,50*Contact::THRESHOLD,M_PI/36.0);
00690 world->updateGrasps();
00691
00692 DBGP("new pos: "<<dInfo->selectedElement->getTran());
00693 if (translating) {
00694 SbVec3f newPos = (origRotation.inverse() * dInfo->selectedElement->getTran().translation()).toSbVec3f();
00695 newPos[0]/=scale[0];
00696 newPos[1]/=scale[1];
00697 newPos[2]/=scale[2];
00698 dInfo->lastTran = newPos;
00699 myHandleBox->translation.setValue(newPos);
00700 } else {
00701 SbRotation newRot = dInfo->selectedElement->getTran().rotation().toSbRotation();
00702 dInfo->lastRot = newRot;
00703 myCenterball->rotation.setValue(newRot);
00704 #ifdef GRASPITDBG
00705 SbVec3f ax;
00706 float ang;
00707 newRot.getValue(ax,ang);
00708 desiredRotation.set(myCenterball->rotation.getValue());
00709 std::cout << "setting ball to: "<<desiredRotation<<std::endl;
00710 #endif
00711 }
00712
00713
00714 }
00715
00716 void
00717 IVmgr::revoluteJointClicked(DraggerInfo *dInfo)
00718 {
00719 SbVec3f axis;
00720 float angle;
00721 SoRotateDiscDragger *dragger = (SoRotateDiscDragger *)dInfo->dragger;
00722 dragger->rotation.getValue(axis,angle);
00723 dInfo->lastVal = angle;
00724
00725 Robot *robot = (Robot *) dInfo->selectedElement;
00726 robot->emitUserInteractionStart();
00727 }
00728
00729 void
00730 IVmgr::revoluteJointFinished(DraggerInfo *dInfo)
00731 {
00732 Robot *robot = (Robot *) dInfo->selectedElement;
00733 robot->emitUserInteractionEnd();
00734 }
00735
00745 void
00746 IVmgr::revoluteJointChanged(DraggerInfo *dInfo)
00747 {
00748 SbVec3f axis;
00749 float angle;
00750 double desiredAngle;
00751 Robot *robot = (Robot *)dInfo->selectedElement;
00752 SoRotateDiscDragger *dragger = (SoRotateDiscDragger *)dInfo->dragger;
00753 DOF *dof = dInfo->dof;
00754 double *dofVals= new double[robot->getNumDOF()];
00755 double *stepBy = new double[robot->getNumDOF()];
00756 int d;
00757
00758
00759 #ifdef GRASPITDBG
00760 printf("in jointChangedCB\n");
00761 #endif
00762
00763 SbBool enabled = dragger->enableValueChangedCallbacks(FALSE);
00764
00765 dragger->rotation.getValue(axis,angle);
00766
00767 if (fabs(angle - dInfo->lastVal) < 0.00001) {
00768 dragger->rotation.setValue(axis,dInfo->lastVal);
00769 if (enabled) dragger->enableValueChangedCallbacks(TRUE);
00770 return;
00771 }
00772
00773
00774
00775 double relAngle = (double)axis[2] * (angle - dInfo->lastVal);
00776 if (relAngle > M_PI) relAngle -= 2 * M_PI;
00777 if (relAngle <= -M_PI) relAngle += 2 * M_PI;
00778 desiredAngle = dof->getVal() + (double)(relAngle);
00779 dInfo->lastVal = angle;
00780
00781 #ifdef GRASPITDBG
00782 printf("axis[2]: %f angle: %f\n",axis[2],angle);
00783 #endif
00784
00785
00786
00787
00788 if (desiredAngle > dof->getMax())
00789 desiredAngle = dof->getMax();
00790 else if (desiredAngle < dof->getMin())
00791 desiredAngle = dof->getMin();
00792
00793 robot->getDOFVals(dofVals);
00794
00795 #ifdef GRASPITDBG
00796 printf("desiredDOFVal: %.1f deg -- %15.15lf rad\n",desiredAngle * 180.0 / M_PI, desiredAngle);
00797 #endif
00798
00799 dofVals[dof->getDOFNum()] = desiredAngle;
00800 for(d=0;d<robot->getNumDOF();d++) {
00801
00802 stepBy[d] = M_PI/36.0;
00803 #if 0
00804 printf("dof %d, to: %lf\n",d,dofVals[d]);
00805 #endif
00806 }
00807 stepBy[dof->getDOFNum()] = M_PI/36.0;
00808
00809
00810 robot->moveDOFToContacts(dofVals,stepBy,true);
00811
00812 robot->emitConfigChange();
00813 world->updateGrasps();
00814
00815 #ifdef GRASPITDBG
00816 printf("newDOFVal: %15.15lf\n",dof->getVal());
00817 #endif
00818
00819 angle = (float) dof->getVal();
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829 angle = dof->getVal() / axis[2];
00830
00831 #ifdef GRASPITDBG
00832 printf("setting axis[2]: %f angle: %f dof val: %f\n",axis[2],angle,dof->getVal());
00833 #endif
00834
00835 dragger->rotation.setValue(axis,angle);
00836
00837 if (enabled)
00838 dragger->enableValueChangedCallbacks(TRUE);
00839
00840 delete [] dofVals;
00841 delete [] stepBy;
00842 }
00843
00853 void
00854 IVmgr::prismaticJointChanged(DraggerInfo *dInfo)
00855 {
00856 SbVec3f transl(0,0,0);
00857 double desiredTransl;
00858 SoTranslate1Dragger *dragger = (SoTranslate1Dragger *)dInfo->dragger;
00859 Robot *robot = (Robot *)dInfo->selectedElement;
00860 DOF *dof = dInfo->dof;
00861 double *dofVals = new double[robot->getNumDOF()];
00862 double *stepBy = new double[robot->getNumDOF()];
00863
00864 SbBool enabled = dragger->enableValueChangedCallbacks(FALSE);
00865
00866 float scale=robot->getDOFDraggerScale(dof->getDOFNum());
00867
00868
00869 desiredTransl = (double) (dragger->translation.getValue()[0] * scale);
00870
00871 if (desiredTransl > dof->getMax()) {
00872 desiredTransl = dof->getMax();
00873 }
00874 else if (desiredTransl < dof->getMin()) {
00875 desiredTransl = dof->getMin();
00876 }
00877
00878 robot->getDOFVals(dofVals);
00879
00880 #ifdef GRASPITDBG
00881 printf("desiredDOFVal: %le\n",desiredTransl);
00882 #endif
00883
00884 dofVals[dof->getDOFNum()] = desiredTransl;
00885 for(int d=0;d<robot->getNumDOF();d++) stepBy[d]=0.0;
00886 stepBy[dof->getDOFNum()] = 50*Contact::THRESHOLD;
00887
00888
00889 robot->moveDOFToContacts(dofVals,stepBy,true);
00890
00891 robot->emitConfigChange();
00892 world->updateGrasps();
00893
00894 transl[0] = (float) dof->getVal()/scale;
00895 dragger->translation.setValue(transl);
00896
00897 if (enabled)
00898 dragger->enableValueChangedCallbacks(TRUE);
00899
00900 delete [] dofVals;
00901 delete [] stepBy;
00902 }
00903
00911 void
00912 IVmgr::makeCenterball(WorldElement *selectedElement,Body *surroundMe)
00913 {
00914 SoCenterballDragger *myCenterball = new SoCenterballDragger;
00915 SoSeparator *sep = new SoSeparator;
00916 SoTransform *draggerTran = new SoTransform;
00917
00918
00919 SoGetBoundingBoxAction *bba = new SoGetBoundingBoxAction(myViewer->getViewportRegion());
00920 bba->apply(surroundMe->getIVGeomRoot());
00921 float maxRad = (bba->getBoundingBox().getMax() - bba->getBoundingBox().getMin()).length();
00922 delete bba;
00923
00924 maxRad /= 2.0;
00925
00926 draggerTran->translation = selectedElement->getTran().translation().toSbVec3f();
00927 draggerTran->scaleFactor.setValue(maxRad,maxRad,maxRad);
00928 myCenterball->rotation = selectedElement->getTran().rotation().toSbRotation();
00929
00930 sep->addChild(draggerTran);
00931 sep->addChild(myCenterball);
00932 draggerRoot->addChild(sep);
00933
00934
00935 DraggerInfo *dInfo = new DraggerInfo;
00936 dInfo->draggerSep = sep;
00937 dInfo->dragger = myCenterball;
00938 dInfo->draggerTran = draggerTran;
00939 dInfo->selectedElement = selectedElement;
00940 dInfo->lastCent = myCenterball->center.getValue();
00941 DBGP("lastCent " << dInfo->lastCent[0] << " "<<dInfo->lastCent[1]<<" "<<dInfo->lastCent[2]);
00942
00943 dInfo->centerballTransl = selectedElement->getTran().translation();
00944 draggerInfoList.push_back(dInfo);
00945 DBGP("NUM DRAGGER INFOS "<<draggerInfoList.size());
00946
00947
00948
00949
00950
00951
00952 myCenterball->addMotionCallback(transRotCB,dInfo);
00953 }
00954
00962 void
00963 IVmgr::makeHandleBox(WorldElement *selectedElement,Body *surroundMe)
00964 {
00965 SoSeparator *sep = new SoSeparator;
00966 draggerRoot->addChild(sep);
00967
00968
00969 SoHandleBoxDragger *myHandleBox = new SoHandleBoxDragger;
00970 myHandleBox->setPart("extruder1",new SoSeparator);
00971 myHandleBox->setPart("extruder2",new SoSeparator);
00972 myHandleBox->setPart("extruder3",new SoSeparator);
00973 myHandleBox->setPart("extruder4",new SoSeparator);
00974 myHandleBox->setPart("extruder5",new SoSeparator);
00975 myHandleBox->setPart("extruder6",new SoSeparator);
00976 myHandleBox->setPart("uniform1",new SoSeparator);
00977 myHandleBox->setPart("uniform2",new SoSeparator);
00978 myHandleBox->setPart("uniform3",new SoSeparator);
00979 myHandleBox->setPart("uniform4",new SoSeparator);
00980 myHandleBox->setPart("uniform5",new SoSeparator);
00981 myHandleBox->setPart("uniform6",new SoSeparator);
00982 myHandleBox->setPart("uniform7",new SoSeparator);
00983 myHandleBox->setPart("uniform8",new SoSeparator);
00984 myHandleBox->setPart("extruder1Active",new SoSeparator);
00985 myHandleBox->setPart("extruder2Active",new SoSeparator);
00986 myHandleBox->setPart("extruder3Active",new SoSeparator);
00987 myHandleBox->setPart("extruder4Active",new SoSeparator);
00988 myHandleBox->setPart("extruder5Active",new SoSeparator);
00989 myHandleBox->setPart("extruder6Active",new SoSeparator);
00990 myHandleBox->setPart("uniform1Active",new SoSeparator);
00991 myHandleBox->setPart("uniform2Active",new SoSeparator);
00992 myHandleBox->setPart("uniform3Active",new SoSeparator);
00993 myHandleBox->setPart("uniform4Active",new SoSeparator);
00994 myHandleBox->setPart("uniform5Active",new SoSeparator);
00995 myHandleBox->setPart("uniform6Active",new SoSeparator);
00996 myHandleBox->setPart("uniform7Active",new SoSeparator);
00997 myHandleBox->setPart("uniform8Active",new SoSeparator);
00998
00999
01000 SoGetBoundingBoxAction *bba = new SoGetBoundingBoxAction(myViewer->getViewportRegion());
01001 bba->apply(surroundMe->getIVGeomRoot());
01002 SbVec3f bbmin,bbmax;
01003 bba->getBoundingBox().getBounds(bbmin,bbmax);
01004 delete bba;
01005
01006
01007 SbVec3f scale = ((bbmax - bbmin)/1.9f);
01008 double maxScale = MAX(MAX(scale[0],scale[1]),scale[2]);
01009 if (maxScale == 0.0) {
01010 DBGP( "0 volume bounding box!");
01011 return;
01012 }
01013
01014
01015 for (int i=0;i<3;i++) {
01016 if (scale[i]/maxScale < .01) {
01017 scale[i] = 0.01 * maxScale;
01018 }
01019 }
01020
01021 SbRotation rot = selectedElement->getTran().rotation().toSbRotation();
01022
01023 SbVec3f transl =
01024 (selectedElement->getTran().rotation().inverse()*
01025 selectedElement->getTran().translation()).toSbVec3f();
01026
01027 transl[0] = transl[0] / scale[0];
01028 transl[1] = transl[1] / scale[1];
01029 transl[2] = transl[2] / scale[2];
01030
01031
01032 SbVec3f offsetTransl;
01033 rot.multVec((bbmax+bbmin)/2.0f,offsetTransl);
01034
01035 SoTransform *draggerTran = new SoTransform;
01036
01037 myHandleBox->translation= transl;
01038 draggerTran->scaleFactor.setValue(scale);
01039 draggerTran->rotation = rot;
01040 draggerTran->translation = offsetTransl;
01041
01042 sep->addChild(draggerTran);
01043 sep->addChild(myHandleBox);
01044
01045 DraggerInfo *dInfo = new DraggerInfo;
01046 dInfo->draggerSep = sep;
01047 dInfo->dragger = myHandleBox;
01048 dInfo->draggerTran = draggerTran;
01049 dInfo->selectedElement = selectedElement;
01050 draggerInfoList.push_back(dInfo);
01051
01052 DBGP("Callback added");
01053
01054
01055
01056
01057
01058 myHandleBox->addMotionCallback(transRotCB,dInfo);
01059 }
01060
01069 void
01070 IVmgr::makeJointDraggers(Robot *robot,KinematicChain *chain)
01071 {
01072 int j,d;
01073 int *activeDOFs = new int[robot->getNumDOF()];
01074 SoSeparator *jointDraggerSep = new SoSeparator;
01075 DraggerInfo *dInfo;
01076 bool firstDragger=true;
01077
01078
01079 jointDraggerSep->addChild(robot->getBase()->getIVTran());
01080 jointDraggerSep->addChild(chain->getIVTran());
01081
01082 for (d=0;d<robot->getNumDOF();d++)
01083 activeDOFs[d] = -1;
01084
01085 for (j=chain->getNumJoints()-1;j>=0;j--)
01086 activeDOFs[chain->getJoint(j)->getDOFNum()] = j;
01087
01088 for(d=0;d<robot->getNumDOF();d++) {
01089 if ((j=activeDOFs[d]) != -1) {
01090 SoSeparator *sep = new SoSeparator;
01091 dInfo = new DraggerInfo;
01092 dInfo->selectedElement = robot;
01093 dInfo->dof = robot->getDOF(d);
01094
01095
01096
01097
01098
01099 if (firstDragger) {
01100 dInfo->draggerSep = jointDraggerSep;
01101 firstDragger=false;
01102 }
01103 else
01104 dInfo->draggerSep = NULL;
01105
01106
01107
01108
01109 if (chain->getJoint(j)->getType() == REVOLUTE) {
01110 SoScale *dialSize = new SoScale;
01111 SoRotateDiscDragger *myDisc = new SoRotateDiscDragger;
01112
01113 float scale = robot->getDOFDraggerScale(d);
01114 dialSize->scaleFactor.setValue(SbVec3f(scale,scale,scale));
01115 SoTranslation *dTrans = new SoTranslation;
01116 dTrans->translation.setValue(0,0,chain->getJoint(j)->getDH()->getD());
01117
01118
01119 sep->addChild(dialSize);
01120 sep->addChild(myDisc);
01121
01122 myDisc->rotation.setValue(SbVec3f(0.0,0.0,1.0),
01123 (float) robot->getDOF(d)->getVal());
01124
01125 myDisc->addStartCallback(revoluteJointClickedCB,dInfo);
01126 myDisc->addValueChangedCallback(revoluteJointChangedCB,dInfo);
01127 myDisc->addFinishCallback(revoluteJointFinishedCB,dInfo);
01128 dInfo->dragger = myDisc;
01129 }
01130 else {
01131 SoTransform *arrowTran = new SoTransform;
01132 SoTranslate1Dragger *myArrow = new SoTranslate1Dragger;
01133 SoBaseColor *arrowBC = new SoBaseColor;
01134 float scale = robot->getDOFDraggerScale(d);
01135 arrowBC->rgb.setValue(1,1,1);
01136 arrowTran->scaleFactor.setValue(SbVec3f(scale,scale,scale));
01137 arrowTran->rotation.setValue(SbVec3f(0,1,0),(float)-M_PI/2.0f);
01138 arrowTran->translation.setValue(SbVec3f(0,-scale,0));
01139 sep->addChild(arrowBC);
01140 sep->addChild(arrowTran);
01141 sep->addChild(myArrow);
01142
01143 myArrow->translation.setValue(SbVec3f((float)robot->getDOF(d)->getVal()/scale,0,0));
01144 dInfo->lastVal = (float) robot->getDOF(d)->getVal()/scale;
01145 myArrow->addValueChangedCallback(prismaticJointChangedCB,dInfo);
01146 dInfo->dragger = myArrow;
01147 }
01148
01149 chain->getJoint(j)->setDraggerAttached(true);
01150
01151 jointDraggerSep->addChild(sep);
01152 jointDraggerSep->addChild(chain->getJoint(j)->getIVTran());
01153 draggerInfoList.push_back(dInfo);
01154 }
01155 }
01156 draggerRoot->addChild(jointDraggerSep);
01157
01158 #ifdef GRASPITDBG
01159 std::cout << "NUM DRAGGER INFOS "<<draggerInfoList.size()<<std::endl;
01160 #endif
01161 delete activeDOFs;
01162 }
01163
01169 void
01170 IVmgr::drawWireFrame(SoSeparator *elementRoot)
01171 {
01172 SoLightModel *lm = new SoLightModel;
01173 lm->model = SoLightModel::BASE_COLOR;
01174
01175 SoDrawStyle *ds = new SoDrawStyle;
01176 ds->style = SoDrawStyle::LINES;
01177 ds->setOverride(TRUE);
01178
01179 SoBaseColor *bc = new SoBaseColor;
01180 bc->rgb.setValue(1,1,1);
01181 bc->setOverride(TRUE);
01182
01183 SoSeparator *sep = new SoSeparator;
01184 sep->addChild(lm);
01185 sep->addChild(ds);
01186 sep->addChild(bc);
01187 sep->addChild(elementRoot);
01188
01189 wireFrameRoot->addChild(sep);
01190 }
01191
01202 SoPath *
01203 IVmgr::pickFilter(const SoPickedPoint *pick)
01204 {
01205 int i,l,r,f,b;
01206 SoPath *p = pick->getPath();
01207 Robot *robot;
01208
01209 if (p->getTail()->isOfType(SoDragger::getClassTypeId())||
01210 p->getTail()->isOfType(SoTransformManip::getClassTypeId()))
01211 {
01212 DBGP("dragger or manip picked");
01213 SoPath *newP = new SoPath(sceneRoot);
01214 return newP;
01215 }
01216
01217 for (i=p->getLength() - 1; i>=0; i--) {
01218 SoNode *n = p->getNode(i);
01219 for (r=0;r<world->getNumRobots(); r++) {
01220 robot = world->getRobot(r);
01221
01222 if (currTool != SELECT_TOOL) {
01223 for (f=0;f<robot->getNumChains();f++)
01224 if (n == robot->getChain(f)->getIVRoot())
01225 return p->copy(0,i+1);
01226
01227 if (n == robot->getBase()->getIVRoot())
01228 return p->copy(0,i);
01229
01230 if (robot->inherits("HumanHand"))
01231 {
01232 for (f=0; f<((HumanHand*)robot)->getNumTendons(); f++)
01233 if ( n == ((HumanHand*)robot)->getTendon(f)->getIVRoot() )
01234 return p->copy(0,i+1);
01235 }
01236
01237 }
01238 else {
01239 if (n == robot->getBase()->getIVRoot() && world->isSelected(robot->getBase()))
01240 return p->copy(0,i);
01241
01242 for (f=0;f<robot->getNumChains();f++)
01243 for (l=0;l<robot->getChain(f)->getNumLinks();l++)
01244 if (n == robot->getChain(f)->getLink(l)->getIVRoot()&& world->isSelected(robot->getChain(f)->getLink(l)))
01245 return p->copy(0,i-1);
01246
01247 if (robot->inherits("HumanHand"))
01248 {
01249 for (f=0; f<((HumanHand*)robot)->getNumTendons(); f++)
01250 if ( n == ((HumanHand*)robot)->getTendon(f)->getIVRoot() )
01251 return p->copy(0,i+1);
01252 }
01253
01254 }
01255 }
01256 }
01257
01258 for (i=p->getLength() - 1; i>=0; i--) {
01259 SoNode *n = p->getNode(i);
01260 for(b=0;b<world->getNumBodies();b++)
01261 if (n == world->getBody(b)->getIVRoot()) {
01262 #ifdef GRAPSITDBG
01263 printf("pickfilter: body %s picked\n",world->getBody(b)->getName());
01264 #endif
01265 return p->copy(0,i+1);
01266 }
01267 }
01268
01269 return NULL;
01270 }
01271
01284 void
01285 IVmgr::handleSelection(SoPath *p)
01286 {
01287 int r,b,f;
01288 bool selectionFound=false;
01289 Robot *robot;
01290
01291 #ifdef GRASPITDBG
01292 printf("in selectionCB: \n");
01293 fprintf(stderr,"in selectionCB: \n");
01294
01295 fprintf(stderr,"*************NUM SELECTED %d*****************\n",selectionRoot->getNumSelected());
01296 #endif
01297
01298 if (p->getTail()->isOfType(SoDragger::getClassTypeId())) {
01299 #ifdef GRASPITDBG
01300 printf("selection path tail is dragger\n");
01301 #endif
01302 return;
01303 }
01304 else if (p->getTail() == sceneRoot) {
01305 #ifdef GRASPITDBG
01306 printf("selection path tail is sceneRoot\n");
01307 #endif
01308 return;
01309 }
01310 else {
01311 for (r=0;r<world->getNumRobots();r++) {
01312 robot = world->getRobot(r);
01313
01314 if (p->getTail() == robot->getIVRoot()) {
01315 #ifdef GRASPITDBG
01316 printf("robot selected\n");
01317 #endif
01318 selectionFound = true;
01319 if (currTool == ROTATE_TOOL)
01320 makeCenterball(robot,robot->getBase());
01321 else if (currTool == TRANSLATE_TOOL)
01322 makeHandleBox(robot,robot->getBase());
01323 else if (currTool == SELECT_TOOL) {
01324 world->selectElement(robot);
01325 drawWireFrame(robot->getIVRoot());
01326 }
01327 }
01328 else {
01329 for (f=0;f<robot->getNumChains();f++)
01330 if (p->getTail() == robot->getChain(f)->getIVRoot()) {
01331 selectionFound = true;
01332 makeJointDraggers(robot,robot->getChain(f));
01333 }
01334 if ( robot->inherits("HumanHand") )
01335 {
01336 for (f=0; f< ((HumanHand*)robot)->getNumTendons(); f++)
01337 if (p->getTail() == ((HumanHand*)robot)->getTendon(f)->getIVRoot())
01338 {
01339 selectionFound = true;
01340 world->selectTendon( ((HumanHand*)robot)->getTendon(f));
01341 }
01342 }
01343 }
01344 }
01345 if (!selectionFound) {
01346
01347 for (b=0;b<world->getNumBodies();b++)
01348 if (p->getTail() == world->getBody(b)->getIVRoot()) {
01349
01350 printf("body %s selected\n",world->getBody(b)->getName().latin1());
01351
01352 selectionFound = true;
01353 if (currTool == ROTATE_TOOL)
01354 makeCenterball(world->getBody(b),world->getBody(b));
01355 else if (currTool == TRANSLATE_TOOL)
01356 makeHandleBox(world->getBody(b),world->getBody(b));
01357 else if (currTool == SELECT_TOOL) {
01358 world->selectElement(world->getBody(b));
01359 drawWireFrame(world->getBody(b)->getIVRoot());
01360 }
01361 break;
01362 }
01363 }
01364
01365 }
01366 }
01367
01374 void
01375 IVmgr::handleDeselection(SoPath *p)
01376 {
01377 int r,b,f,j;
01378 Robot *robot;
01379 WorldElement *deselectedElement=NULL;
01380 std::list<DraggerInfo *>::iterator dp;
01381
01382 #ifdef GRASPITDBG
01383 printf("in deselectionCB: \n");
01384 #endif
01385
01386
01387 #ifdef GRASPITDBG
01388 if (p->getTail()->isOfType(SoDragger::getClassTypeId()))
01389 printf("deselecting dragger\n");
01390 #endif
01391
01392 for (r=0;r<world->getNumRobots() && !deselectedElement;r++) {
01393 robot = world->getRobot(r);
01394
01395
01396 if (p->getTail() == robot->getIVRoot()) {
01397 deselectedElement = robot;
01398 } else {
01399
01400 for (f=0;f<robot->getNumChains() && !deselectedElement;f++) {
01401 if (p->getTail() == robot->getChain(f)->getIVRoot()) {
01402 DBGP("deselecting chain " << f);
01403 for (j=0;j<robot->getChain(f)->getNumJoints();j++)
01404 robot->getChain(f)->getJoint(j)->setDraggerAttached(false);
01405 deselectedElement = robot;
01406 }
01407 }
01408 if ( robot->inherits("HumanHand") ){
01409 for (f=0; f< ((HumanHand*)robot)->getNumTendons(); f++)
01410 if (p->getTail() == ((HumanHand*)robot)->getTendon(f)->getIVRoot()) {
01411 deselectedElement = robot;
01412
01413
01414
01415 }
01416 }
01417
01418 }
01419 }
01420
01421 for (b=0;b<world->getNumBodies() && !deselectedElement;b++)
01422 if (p->getTail() == world->getBody(b)->getIVRoot()) {
01423 DBGP("deselecting body " << world->getBody(b)->getName().latin1());
01424 deselectedElement = world->getBody(b);
01425 }
01426
01427 if (deselectedElement) {
01428 if (currTool == SELECT_TOOL) {
01429 world->deselectElement(deselectedElement);
01430 for (b=0;b<wireFrameRoot->getNumChildren();b++) {
01431 SoSeparator *wfSep = (SoSeparator *)wireFrameRoot->getChild(b);
01432 if (wfSep->getChild(wfSep->getNumChildren()-1) == deselectedElement->getIVRoot()) {
01433 wireFrameRoot->removeChild(b);
01434 break;
01435 }
01436 }
01437 }
01438 else {
01439 dp=draggerInfoList.begin();
01440 while (dp!=draggerInfoList.end()) {
01441 if ((*dp)->selectedElement == deselectedElement) {
01442 if ((*dp)->draggerSep) {
01443 draggerRoot->removeChild((*dp)->draggerSep);
01444 }
01445 delete *dp;
01446 dp = draggerInfoList.erase(dp);
01447 DBGP("removing dragger info");
01448 } else {
01449 dp++;
01450 }
01451 }
01452 }
01453 }
01454 }
01455
01463 void
01464 IVmgr::deleteSelections()
01465 {
01466 int i,r,b;
01467
01468
01469 for (i=selectionRoot->getNumSelected()-1;i>=0;i--) {
01470 for (r=0;r<world->getNumRobots();r++)
01471 if (selectionRoot->getPath(i)->getTail() ==
01472 world->getRobot(r)->getIVRoot()) {
01473 selectionRoot->deselect(i);
01474 world->destroyElement(world->getRobot(r));
01475 break;
01476 }
01477 }
01478
01479
01480
01481
01482
01483 for (i=selectionRoot->getNumSelected()-1;i>=0;i--) {
01484 for (b=0;b<world->getNumBodies();b++)
01485 if (selectionRoot->getPath(i)->getTail() ==
01486 world->getBody(b)->getIVRoot()) {
01487 if (!world->getBody(b)->inherits("Link")) {
01488 selectionRoot->deselect(i);
01489 world->destroyElement(world->getBody(b));
01490 }
01491 break;
01492 }
01493 }
01494 }
01495
01496 void
01497 IVmgr::deselectBody(Body *b)
01498 {
01499 int i;
01500 for (i=selectionRoot->getNumSelected()-1;i>=0;i--) {
01501 if (selectionRoot->getPath(i)->getTail() == b->getIVRoot()) {
01502 if (!b->inherits("Link")) {
01503 selectionRoot->deselect(i);
01504 }
01505 break;
01506 }
01507 }
01508 }
01515 void
01516 IVmgr::saveImage(QString filename)
01517 {
01518 SoQtRenderArea *renderArea;
01519 SoNode *sg;
01520 const SbColor white(1, 1, 1);
01521
01522 SoGLRenderAction *glRend;
01523 SoOffscreenRenderer *myRenderer;
01524
01525 renderArea = myViewer;
01526 sg = sceneRoot;
01527
01528 glRend = new SoGLRenderAction(renderArea->getViewportRegion());
01529 glRend->setSmoothing(TRUE);
01530 glRend->setNumPasses(5);
01531 glRend->setTransparencyType(SoGLRenderAction::SORTED_OBJECT_SORTED_TRIANGLE_BLEND);
01532
01533 myRenderer = new SoOffscreenRenderer(glRend);
01534 myRenderer->setBackgroundColor(white);
01535
01536 #ifdef GRASPITDBG
01537 if (myRenderer->isWriteSupported("jpg"))
01538 std::cout << " supports jpg" << std::endl;
01539 else
01540 std::cout << "no jpg support" << std::endl;
01541 #endif
01542
01543 int numtypes = myRenderer->getNumWriteFiletypes();
01544 SbList<SbName> extList;
01545 SbString fullname;
01546 SbString desc;
01547 for (int i=0;i<numtypes;i++) {
01548 myRenderer ->getWriteFiletypeInfo(i,extList,fullname,desc);
01549 #ifdef GRASPITDBG
01550 std::cout <<std::endl;
01551 for (int j=0;j<extList.getLength();j++)
01552 std::cout << extList[j].getString() <<" ";
01553 std::cout <<std::endl<<fullname.getString()<<std::endl<<desc.getString()<<std::endl;
01554
01555 #endif
01556 }
01557
01558
01559 SoSeparator *renderRoot = new SoSeparator;
01560 renderRoot->ref();
01561 renderRoot->addChild(myViewer->getCamera());
01562 SoTransformSeparator *lightSep = new SoTransformSeparator;
01563 SoRotation *lightDir = new SoRotation;
01564 lightDir->rotation.connectFrom(&myViewer->getCamera()->orientation);
01565 lightSep->addChild(lightDir);
01566 lightSep->addChild(myViewer->getHeadlight());
01567
01568 renderRoot->addChild(lightSep);
01569 renderRoot->addChild(sg);
01570
01571 myRenderer->render(renderRoot);
01572
01573 SbBool result;
01574 result = myRenderer->writeToFile(SbString(filename.latin1()),
01575 SbName(filename.section('.',-1)));
01576
01577 #ifdef GRASPITDBG
01578 if (result)
01579 fprintf(stderr,"saved\n");
01580 else
01581 fprintf(stderr,"not saved\n");
01582 #endif
01583
01584 renderRoot->unref();
01585 delete myRenderer;
01586 }
01587
01595 void
01596 IVmgr::saveImageSequence(const char *fileStr)
01597 {
01598 imgSeqStr = fileStr;
01599 imgSeqCounter = 1;
01600 connect(world,SIGNAL(dynamicStepTaken()),this,SLOT(saveNextImage()));
01601
01602 }
01603
01608 void
01609 IVmgr::saveNextImage()
01610 {
01611 saveImage(QString(imgSeqStr).
01612 arg(world->getWorldTime(),0,'f',4));
01613 }
01614
01622 int
01623 IVmgr::saveCameraPositions(const char *filename)
01624 {
01625 if (!(camerafp=fopen(filename,"w"))) return FAILURE;
01626 connect(world,SIGNAL(dynamicStepTaken()),this,SLOT(saveCameraPos()));
01627 return SUCCESS;
01628 }
01629
01636 int
01637 IVmgr::useSavedCameraPositions(const char *filename)
01638 {
01639 if (!(camerafp=fopen(filename,"r"))) return FAILURE;
01640 connect(world,SIGNAL(dynamicStepTaken()),this,SLOT(restoreCameraPos()));
01641 return SUCCESS;
01642 }
01643
01648 void
01649 IVmgr::saveCameraPos()
01650 {
01651 float x,y,z;
01652 float q1,q2,q3,q4;
01653
01654 myViewer->getCamera()->position.getValue().getValue(x,y,z);
01655 myViewer->getCamera()->orientation.getValue().getValue(q1,q2,q3,q4);
01656 fprintf(camerafp,"%f %f %f %f %f %f %f\n",x,y,z,q1,q2,q3,q4);
01657 }
01658
01663 void
01664 IVmgr::restoreCameraPos()
01665 {
01666 float x,y,z;
01667 float q1,q2,q3,q4;
01668
01669 fscanf(camerafp,"%f %f %f %f %f %f %f\n",&x,&y,&z,&q1,&q2,&q3,&q4);
01670 myViewer->getCamera()->position.setValue(x,y,z);
01671 myViewer->getCamera()->orientation.setValue(q1,q2,q3,q4);
01672 }
01673
01674 void
01675 IVmgr::setCamera(double px, double py, double pz, double q1, double q2, double q3, double q4, double fd)
01676 {
01677 myViewer->getCamera()->position.setValue(px,py,pz);
01678 myViewer->getCamera()->orientation.setValue(q1,q2,q3,q4);
01679 myViewer->getCamera()->focalDistance.setValue(fd);
01680 }
01681
01682 void
01683 IVmgr::getCamera(float &px, float &py, float &pz, float &q1, float &q2, float &q3, float &q4, float &fd)
01684 {
01685 myViewer->getCamera()->position.getValue().getValue(px,py,pz);
01686 myViewer->getCamera()->orientation.getValue().getValue(q1,q2,q3,q4);
01687 fd = myViewer->getCamera()->focalDistance.getValue();
01688 }
01689
01690 void
01691 IVmgr::setCameraTransf(transf tr)
01692 {
01693 const vec3 t = tr.translation();
01694 const Quaternion q = tr.rotation();
01695 myViewer->getCamera()->position.setValue( t.x(), t.y(), t.z() );
01696 myViewer->getCamera()->orientation.setValue( q.x, q.y, q.z, q.w );
01697 }
01698
01699 transf
01700 IVmgr::getCameraTransf()
01701 {
01702 transf tr;
01703 float px, py, pz, qx, qy, qz, qw;
01704 myViewer->getCamera()->position.getValue().getValue(px,py,pz);
01705 myViewer->getCamera()->orientation.getValue().getValue(qx,qy,qz,qw);
01706 Quaternion q = Quaternion(qw,qx,qy,qz);
01707 tr.set( q, vec3(px,py,pz) );
01708 return tr;
01709 }
01710
01717 void
01718 IVmgr::keyPressed(SoEventCallback *eventCB)
01719 {
01720 const SoEvent *event = eventCB->getEvent();
01721
01722 if (SO_KEY_RELEASE_EVENT(event,SPACE)) {
01723 if (world->dynamicsAreOn()) world->turnOffDynamics();
01724 else world->turnOnDynamics();
01725 }
01726
01727
01728 if (SoKeyboardEvent::isKeyReleaseEvent(event, SoKeyboardEvent::KEY_DELETE)) {
01729 if (currTool == SELECT_TOOL)
01730 ivmgr->deleteSelections();
01731 }
01732
01733 if (SO_KEY_RELEASE_EVENT(event,G)) {
01734 if (world->getCurrentHand()) {
01735 fprintf(stderr,"Autograsp!\n");
01736 world->getCurrentHand()->approachToContact(30);
01737 world->getCurrentHand()->autoGrasp(true);
01738 world->updateGrasps();
01739 }
01740 }
01741
01742 if (SO_KEY_RELEASE_EVENT(event,R)) {
01743 world->getCurrentHand()->restoreState();
01744 }
01745
01746 if (SO_KEY_RELEASE_EVENT(event,C)) {
01747 Hand *h = world->getCurrentHand();
01748 Hand *clone = new Hand(world, "Hand clone");
01749 clone->cloneFrom(h);
01750 clone->setRenderGeometry(false);
01751 clone->showVirtualContacts(false);
01752 world->addRobot(clone);
01753 world->toggleCollisions(false, h, clone);
01754 clone->setTran( h->getTran() );
01755 }
01756
01757 if (SO_KEY_RELEASE_EVENT(event,S)) {
01758 world->getCurrentHand()->saveState();
01759
01760
01761
01762
01763
01764
01765 }
01766
01767 if (SoKeyboardEvent::isKeyReleaseEvent(event,SoKeyboardEvent::PAD_ADD)) {
01768 if (myViewer->isStereoOn()) {
01769 myViewer->mFocalPlane += 50.0;
01770 myViewer->setStereo(true);
01771 }
01772 }
01773 if (SoKeyboardEvent::isKeyReleaseEvent(event,SoKeyboardEvent::PAD_SUBTRACT)) {
01774 if (myViewer->isStereoOn()) {
01775 myViewer->mFocalPlane -= 50.0;
01776 myViewer->setStereo(true);
01777 }
01778 }
01779
01780 static bool left = true;
01781
01782 static SoSeparator *indicators = NULL;
01783 if (SO_KEY_RELEASE_EVENT(event,V)) {
01784 fprintf(stderr,"Distance test\n");
01785 if (!indicators) {
01786 indicators = new SoSeparator();
01787 world->getIVRoot()->addChild(indicators);
01788 }
01789 indicators->removeAllChildren();
01790 if (world->getNumGB() < 2) return;
01791 Body *b1 = world->getGB(0);
01792 Body *b2 = world->getGB(1);
01793 position p1, p2;
01794 world->getDist(b1, b2, p1, p2);
01795 p1 = p1 * b1->getTran();
01796 p2 = p2 * b2->getTran();
01797
01798
01799
01800
01801
01802 SoSphere *sphere = new SoSphere();
01803 sphere->radius = 2;
01804
01805 SoSeparator *sep1 = new SoSeparator();
01806 SoTransform *tran1 = new SoTransform();
01807 tran1->translation.setValue(p1.x(), p1.y(), p1.z());
01808 sep1->addChild(tran1);
01809 sep1->addChild(sphere);
01810 indicators->addChild(sep1);
01811
01812 SoSeparator *sep2 = new SoSeparator();
01813 SoTransform *tran2 = new SoTransform();
01814 tran2->translation.setValue(p2.x(), p2.y(), p2.z());
01815 sep2->addChild(tran2);
01816 sep2->addChild(sphere);
01817 indicators->addChild(sep2);
01818 }
01819
01820 }
01821
01822
01823 void
01824 IVmgr::keyPressedCB(void *,SoEventCallback *eventCB)
01825 {
01826 ivmgr->keyPressed(eventCB);
01827 }
01828
01829 void
01830 IVmgr::transRotCB(void *dInfo,SoDragger *dragger)
01831 {
01832 ((DraggerInfo *)dInfo)->dragger = dragger;
01833 ivmgr->transRot((DraggerInfo *)dInfo);
01834 }
01835
01836 void
01837 IVmgr::revoluteJointChangedCB(void *dInfo,SoDragger *dragger)
01838 {
01839 ((DraggerInfo *)dInfo)->dragger = dragger;
01840 ivmgr->revoluteJointChanged((DraggerInfo *)dInfo);
01841 }
01842
01843 void
01844 IVmgr::revoluteJointClickedCB(void *dInfo,SoDragger *dragger)
01845 {
01846 ((DraggerInfo *)dInfo)->dragger = dragger;
01847 ivmgr->revoluteJointClicked((DraggerInfo *)dInfo);
01848 }
01849
01850 void
01851 IVmgr::revoluteJointFinishedCB(void *dInfo,SoDragger *dragger)
01852 {
01853 ((DraggerInfo *)dInfo)->dragger = dragger;
01854 ivmgr->revoluteJointFinished((DraggerInfo *)dInfo);
01855 }
01856
01857 void
01858 IVmgr::prismaticJointChangedCB(void *dInfo,SoDragger *dragger)
01859 {
01860 ((DraggerInfo *)dInfo)->dragger = dragger;
01861 ivmgr->prismaticJointChanged((DraggerInfo *)dInfo);
01862 }
01863
01864 void
01865 IVmgr::shiftOrCtrlDownCB(void *,SoEventCallback *eventCB)
01866 {
01867 const SoEvent *event = eventCB->getEvent();
01868 if (SO_MOUSE_RELEASE_EVENT(event,BUTTON1))
01869 ivmgr->setCtrlDown(event->wasCtrlDown());
01870 if (SO_MOUSE_PRESS_EVENT(event,BUTTON1))
01871 ivmgr->setShiftDown(event->wasShiftDown());
01872 }
01873
01874 SoPath*
01875 IVmgr::pickFilterCB(void *,const SoPickedPoint *pick)
01876 {
01877 return ivmgr->pickFilter(pick);
01878 }
01879
01880 void
01881 IVmgr::selectionCB(void *,SoPath *path)
01882 {
01883 ivmgr->handleSelection(path);
01884 }
01885
01886 void
01887 IVmgr::deselectionCB(void *,SoPath *path)
01888 {
01889 ivmgr->handleDeselection(path);
01890 }
01891
01892 void IVmgr::setStereo(bool s)
01893 {
01894 myViewer->setStereo(s);
01895 }
01896
01897 void IVmgr::flipStereo()
01898 {
01899 }