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 "pointers.dat"
00031 #include "gwsprojection.h"
00032 #include "body.h"
00033 #include "grasp.h"
00034 #include "gws.h"
00035 #include "matvec3D.h"
00036 #include <set>
00037 #include <qwidget.h>
00038
00039 #include <Inventor/actions/SoGLRenderAction.h>
00040 #include <Inventor/nodes/SoCamera.h>
00041 #include <Inventor/nodes/SoPerspectiveCamera.h>
00042 #include <Inventor/nodes/SoCoordinate3.h>
00043 #include <Inventor/nodes/SoDirectionalLight.h>
00044 #include <Inventor/nodes/SoFaceSet.h>
00045 #include <Inventor/nodes/SoIndexedFaceSet.h>
00046 #include <Inventor/nodes/SoMaterial.h>
00047 #include <Inventor/nodes/SoRotation.h>
00048 #include <Inventor/nodes/SoScale.h>
00049 #include <Inventor/nodes/SoSeparator.h>
00050 #include <Inventor/nodes/SoShapeHints.h>
00051 #include <Inventor/nodes/SoTransform.h>
00052 #include <Inventor/nodes/SoTransformSeparator.h>
00053 #include <Inventor/Qt/SoQtRenderArea.h>
00054 #include <Inventor/Qt/viewers/SoQtExaminerViewer.h>
00055
00056 #ifdef USE_DMALLOC
00057 #include "dmalloc.h"
00058 #endif
00059
00060 #define HULLAXES_SCALE 50.0
00061
00067 GWSprojection::GWSprojection(SoQtExaminerViewer *mainViewer,GWS *g,double *c,
00068 std::set<int> whichFixed)
00069 {
00070 gws = g;
00071 GraspableBody *object = gws->getGrasp()->getObject();
00072
00073 memcpy(projCoords,c,6*sizeof(double));
00074
00075 fixedCoordIndex = whichFixed;
00076
00077 SoMaterial *mat = new SoMaterial;
00078 SoShapeHints *myHints = new SoShapeHints;
00079 myHints->shapeType = SoShapeHints::SOLID;
00080 myHints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
00081
00082 mat->diffuseColor = SbColor(0,0.8f,0);
00083 mat->ambientColor = SbColor(0,0.2f,0);
00084 mat->transparency = 0.4f;
00085
00086 hullCoords = new SoCoordinate3;
00087 hullIFS = new SoIndexedFaceSet;
00088 hullSep = new SoSeparator;
00089 hullSep->addChild(myHints);
00090 hullSep->addChild(mat);
00091 hullSep->addChild(hullCoords);
00092 hullSep->addChild(hullIFS);
00093
00094 SoInput in;
00095 in.setBuffer((void *) pointersData, (size_t) sizeof(pointersData));
00096 SoSeparator *pointers = SoDB::readAll(&in);
00097 SoSeparator *hullaxes = (SoSeparator *)pointers->getChild(3);
00098 SoScale *hasf = new SoScale;
00099 double scale = gws->getGrasp()->getMaxRadius();
00100 hasf->scaleFactor=SbVec3f(scale/HULLAXES_SCALE, scale/HULLAXES_SCALE, scale/HULLAXES_SCALE);
00101 hullaxes->insertChild(hasf,0);
00102
00103 SoRotation *lightDir = new SoRotation;
00104 lightDir->rotation.connectFrom(&mainViewer->getCamera()->orientation);
00105 SoTransformSeparator *lightSep = new SoTransformSeparator;
00106 lightSep->addChild(lightDir);
00107 lightSep->addChild(mainViewer->getHeadlight());
00108
00109 SoTransform *hullTran = new SoTransform;
00110 if (!hullTran) printf("NULL hullTran!\n");
00111
00112 if (object!=NULL) {
00113 hullTran->translation.connectFrom(&object->getIVTran()->translation);
00114 hullTran->rotation.connectFrom(&object->getIVTran()->rotation);
00115 } else {
00116 hullTran->translation = gws->getGrasp()->getCoG().toSbVec3f();
00117 }
00118
00119 sg = new SoSeparator;
00120
00121 SoPerspectiveCamera *camera = new SoPerspectiveCamera();
00122 if (!camera->position.connectFrom( &mainViewer->getCamera()->position ))
00123 fprintf(stderr,"Projection camera connection 1 failed!\n");
00124 if (!camera->orientation.connectFrom( &mainViewer->getCamera()->orientation ))
00125 fprintf(stderr,"Projection camera connection 2 failed!\n");
00126 camera->nearDistance = 5;
00127 camera->farDistance = 1000;
00128 sg->addChild(camera);
00129
00130
00131
00132 sg->addChild(lightSep);
00133 sg->addChild(hullTran);
00134 sg->addChild(hullaxes);
00135 sg->addChild(hullSep);
00136
00137 pointers->ref();
00138 pointers->unref();
00139
00140
00141 projectionViewer = new SoQtRenderArea();
00142 projectionViewer->setTransparencyType(SoGLRenderAction::SORTED_OBJECT_BLEND);
00143 projectionViewer->setBackgroundColor(SbColor(1,1,1));
00144 projectionViewer->setSceneGraph(sg);
00145 if (projectionViewer->isTopLevelShell()) printf("TOP LEVEL SHELL\n");
00146 else printf("NOT TOP LEVEL SHELL\n");
00147 projectionViewer->setWindowCloseCallback(Grasp::destroyProjection,this);
00148
00149 projectionViewer->show();
00150 setWinTitle();
00151 projWin = projectionViewer->getParentWidget();
00152
00153 update();
00154
00155 }
00156
00161 GWSprojection::~GWSprojection()
00162 {
00163 gws->getGrasp()->removeGWS(gws);
00164 if (projectionViewer->getShellWidget()) {
00165 projectionViewer->setWindowCloseCallback(NULL);
00166 delete projectionViewer->getShellWidget();
00167 }
00168 delete projectionViewer;
00169 }
00170
00178 void
00179 GWSprojection::setWinTitle()
00180 {
00181 int i;
00182 char titleStr[100],element[6];
00183
00184 sprintf(titleStr,"%s GWS projection (",gws->getType());
00185 for (i=0;i<6;i++) {
00186 if (fixedCoordIndex.find(i) == fixedCoordIndex.end())
00187 strcat(titleStr," * ");
00188 else {
00189 sprintf(element,"%4.1f",projCoords[i]);
00190 strcat(titleStr,element);
00191 }
00192
00193 if (i<5)
00194 strcat(titleStr,",");
00195 }
00196 strcat(titleStr,")");
00197
00198 projectionViewer->setTitle(titleStr);
00199 }
00200
00205 void
00206 GWSprojection::update()
00207 {
00208 int i;
00209 std::vector<position> coords;
00210 std::vector<int> indices;
00211
00212 if (gws->isForceClosure() || gws->hasPositiveVolume() )
00213 gws->projectTo3D(projCoords,fixedCoordIndex,coords,indices);
00214
00215 hullCoords->point.deleteValues(0);
00216 hullIFS->coordIndex.deleteValues(0);
00217
00218 int numCoords = coords.size();
00219 for (i=0;i<numCoords;i++) {
00220 hullCoords->point.set1Value(i,(float)coords[i].x(),(float)coords[i].y(),
00221 (float)coords[i].z());
00222 }
00223
00224 int numIndices = indices.size();
00225 for (i=0;i<numIndices;i++) {
00226 hullIFS->coordIndex.set1Value(i,indices[i]);
00227 }
00228
00229 coords.clear();
00230 indices.clear();
00231 }
00232
00236 void
00237 GWSprojection::deleteHull()
00238 {
00239 hullSep->removeChild(2);
00240 hullSep->removeChild(2);
00241 hullCoords = new SoCoordinate3;
00242 hullIFS = new SoIndexedFaceSet;
00243 hullSep->addChild(hullCoords);
00244 hullSep->addChild(hullIFS);
00245 }