/home/ann2.ext/paccaulf/projsem2/ContourActif/viewer.cpp

Aller à la documentation de ce fichier.
00001 #include <qmessagebox.h>
00002 #include <qimage.h>
00003 #include <q3filedialog.h>
00004 #include <list>
00005 #if QT_VERSION < 0x040000
00006 # include <qpopupmenu.h>
00007 #else
00008 # include <qmenu.h>
00009 #endif
00010 #include <qcursor.h>
00011 #include <qmap.h>
00012 #include <iostream> 
00013 #include <fstream>
00014 
00015 #include <vector>
00016 
00017 #include "viewer.h"
00018 
00019 using namespace qglviewer;
00020 using namespace std;
00021 
00022 Viewer::Viewer(QWidget *parent, const char *name) : QGLViewer(parent,name),m(NULL),drawMesh_(false)
00023 {
00024 };
00025 
00026 
00027 Viewer::~Viewer()
00028 {
00029         delete m;
00030 };
00031 
00032 
00033 
00034 // ajoute un nom 
00035 void Viewer::drawWithNames()
00036 {
00037         glPushMatrix();
00038         glPushName(1);
00039         //affichage du mesh
00040         glMultMatrixd(manipulatedFrame()->matrix());
00041 #if QT_VERSION < 0x040000
00042         setHandlerKeyboardModifiers(QGLViewer::FRAME, Qt::AltButton);
00043         setHandlerKeyboardModifiers(QGLViewer::CAMERA,  Qt::NoButton);
00044         setHandlerKeyboardModifiers(QGLViewer::FRAME, Qt::ControlButton);
00045 #else
00046         setHandlerKeyboardModifiers(QGLViewer::FRAME, Qt::AltModifier);
00047         setHandlerKeyboardModifiers(QGLViewer::CAMERA,  Qt::NoModifier);
00048         setHandlerKeyboardModifiers(QGLViewer::FRAME, Qt::ControlModifier);
00049 #endif      
00050         
00051         if (drawMesh_) 
00052           {
00053                 m->draw(smooth_, true, invertNormals_); // glColor3f(0.0,0.5,0.8) (bleu turquoise)
00054                 if (drawEdges_) m->draw_edges(); // glColor3f(0.0,0.0,0.0) (noir)
00055           }
00056         
00057         glPopName();
00058         
00059         glPopMatrix();
00060 }
00061 
00062 void Viewer::postSelection(const QPoint& point)
00063 {
00064         // Compute orig and dir, used to draw a representation of the intersecting line
00065         camera()->convertClickToLine(point, orig, dir);
00066         
00067         // Find the selectedPoint coordinates, using camera()->pointUnderPixel().
00068         bool found;
00069         selectedPoint = camera()->pointUnderPixel(point, found);
00070         
00071         Vec selectedPointO = selectedPoint - 0.01*dir; // Small offset to make point clearly visible.
00072                                                                                                    // Note that "found" is different from (selectedObjectId()>=0) because of the size of the select region.
00073                                                                                                    //màj de dir du mesh
00074                                                                                                    //m->dir = Mesh::Point(dir[0], dir[1], dir[2]) ;
00075                                                                                                    //std::cout<<"m->dir = Mesh::Point(dir[0], dir[1], dir[2]) ;"<<std::endl ;
00076         
00077         // Distinction suivant si on a clique dans le vide ou non
00078         std::cout<<"selectedName == "<<selectedName()<<std::endl ;
00079         if (selectedName() == -1) 
00080           {
00081                 // on a clique dans le vide
00082                 std::cout<<"No object selected under pixel "
00083                <<point.x()<<","
00084                <<point.y()<<std::endl;
00085           }
00086         else 
00087           {
00088                 // Attention, on veut le point 3D situe sous le pixel 2D de l'ecran, mais DANS LES COORDONNEES relatives au maillage
00089                 Vec selectedPointOnMesh = manipulatedFrame()->coordinatesOf(selectedPoint) ;
00090                 // on a clique sur un objet (le maillage, donc)
00091                 /*std::cout<<"objet numero "<<selectedName()
00092                 <<" selected under pixel "
00093                 <<selectedPointOnMesh[0] <<","
00094                 <<selectedPointOnMesh[1] <<","
00095                 <<selectedPointOnMesh[2] <<std::endl;*/
00096                 
00097                 //le sommet le plus proche de la ou on a clique
00098                 Mesh::Vertex_handle nV = m->nearestVertexOnFacet(Mesh::Point(selectedPointOnMesh[0],
00099                                                                                                                                          selectedPointOnMesh[1],
00100                                                                                                                                          selectedPointOnMesh[2])
00101                                                                                                                  );
00102                 
00103                 // sauvegarde de ce sommet
00104                 m->_snake_vertex.push_back(nV) ;
00105                 
00106                 
00107                 // la construction du snake se fait ici, en plusieurs passages
00108                 // *prevVertex = avant dernier sommet clique par l utilisateur 
00109                 std::vector<Mesh::Vertex_handle>::reverse_iterator prevVertex = m->_snake_vertex.rbegin();
00110                 if (m->_snake_vertex.size() !=1 ) 
00111                   {
00112                         // il y a un avant-dernier sommet dans le snake
00113                         prevVertex++ ;
00114                         
00115                         // chemtemp = (presque) plus court chemin de prevVertex au dernier sommet clique par l'utilisateur
00116                         Mesh::Vertex_chemin chemtemp ;
00117                         
00118                         m->approx_shortest_path(chemtemp,*prevVertex,nV) ;
00119                         
00120                         // chemtemp est ajoute au snake total
00121                         m->_snake_total.push_back(chemtemp) ;
00122                   }    
00123                 drawUserSnake_ = true ; // on a au moins un sommet, on peut donc commencer a afficher le snake.
00124           }
00125 }
00126 
00127 
00128 
00129 
00130 // principale procedure d affichage
00131 void Viewer::draw()
00132 {
00133         if (meshMode_)
00134           {
00135                 // M O D E   A F F I C H A G E   D U   M E S H
00136                 // Save the current model view matrix (not needed here in fact)
00137                 glPushMatrix();
00138                 // Multiply matrix to get in the frame coordinate system.
00139                 glMultMatrixd(manipulatedFrame()->matrix());
00140 #if QT_VERSION < 0x040000
00141                 setHandlerKeyboardModifiers(QGLViewer::FRAME,  Qt::AltButton);
00142                 setHandlerKeyboardModifiers(QGLViewer::CAMERA, Qt::NoButton);
00143                 setHandlerKeyboardModifiers(QGLViewer::FRAME,  Qt::ControlButton);
00144 #else
00145                 setHandlerKeyboardModifiers(QGLViewer::FRAME,  Qt::AltModifier);
00146                 setHandlerKeyboardModifiers(QGLViewer::CAMERA, Qt::NoModifier);
00147                 setHandlerKeyboardModifiers(QGLViewer::FRAME,  Qt::ControlModifier);
00148 #endif      
00149                 
00150                 if (drawMesh_) {
00151                         
00152                         m->draw(smooth_, true, invertNormals_); // glColor3f(0.0,0.5,0.8) (bleu turquoise)
00153                         if (drawEdges_) m->draw_edges(); // glColor3f(0.0,0.0,0.0) (noir)
00154                 }
00155                 
00156                 if (drawUserSnake_)
00157                         m->drawUserSnake() ;
00158                 
00159                 if (drawLocalRegion_)
00160                         m->drawLocalRegion() ;
00161                 
00162                 // Restore the original (world) coordinate system
00163                 glPopMatrix();
00164           }
00165         else
00166           {
00167                 // M O D E  A F F I C H A G E   L O C A L   R E G I O N
00168                 // Save the current model view matrix (not needed here in fact)
00169                 glPushMatrix();
00170                 // Multiply matrix to get in the frame coordinate system.
00171                 glMultMatrixd(manipulatedFrame()->matrix());
00172 #if QT_VERSION < 0x040000
00173                 setHandlerKeyboardModifiers(QGLViewer::FRAME,  Qt::AltButton);
00174                 setHandlerKeyboardModifiers(QGLViewer::CAMERA, Qt::NoButton);
00175                 setHandlerKeyboardModifiers(QGLViewer::FRAME,  Qt::ControlButton);
00176 #else
00177                 setHandlerKeyboardModifiers(QGLViewer::FRAME,  Qt::AltModifier);
00178                 setHandlerKeyboardModifiers(QGLViewer::CAMERA, Qt::NoModifier);
00179                 setHandlerKeyboardModifiers(QGLViewer::FRAME,  Qt::ControlModifier);
00180 #endif      
00181                 
00182                 m->draw2dRegion() ;
00183                 
00184                 // Restore the original (world) coordinate system
00185                 glPopMatrix();          
00186           }
00187         
00188 };
00189 
00190 
00191 //   D i s p l a y   e v e n t s
00192 
00193 Viewer::DisplayMode Viewer::dispMode() const
00194 {
00195         return displayMode_;
00196 };
00197 
00198 
00199 void Viewer::dispMode(DisplayMode displaymode)
00200 {       
00201         displayMode_ = displaymode;
00202         glFlush();
00203 };
00204 
00205 
00206 void Viewer::toggleDrawEdges()
00207 {
00208         drawEdges_ = !drawEdges_;
00209         updateGL();
00210 }
00211 
00212 void Viewer::toggleInvertNormals()
00213 {
00214         invertNormals_ = !invertNormals_;
00215         updateGL();
00216 }
00217 
00218 void Viewer::toggleDisplayRep()
00219 {
00220         if (displayMode_ == SURF) {
00221                 dispMode(WIRE);
00222                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00223         }
00224         else {
00225                 if (displayMode_ == WIRE) {
00226                         dispMode(PTS);
00227                         glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
00228                 }
00229                 else {
00230                         dispMode(SURF);
00231                         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00232                 }
00233         }
00234         updateGL();
00235 }
00236 
00237 void Viewer::toggleSmoothShading()
00238 {
00239         smooth_ = !smooth_;
00240         updateGL();
00241 }
00242 
00243 void Viewer::init()
00244 {
00245         glClearColor(1.0,1.0,1.0,1.0);
00246         glDisable(GL_CULL_FACE);
00247         
00248 #ifdef GL_RESCALE_NORMAL  // OpenGL 1.2 Only...
00249         glEnable(GL_RESCALE_NORMAL);
00250 #endif
00251         // Add a manipulated frame to the viewer.
00252         setManipulatedFrame(new ManipulatedFrame());
00253         
00254         // Enable GL textures
00255         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00256         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
00257         // Nice texture coordinate interpolation
00258         glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
00259         
00260         drawEdges_ = false;
00261         invertNormals_ = false;
00262         smooth_ = true;
00263         dispMode(SURF);
00264         meshMode_ = true;       
00265         // Add custom key description (see keyPressEvent)
00266         setKeyDescription(Qt::Key_E, "Toggles the display of edges.");
00267         setKeyDescription(Qt::Key_N, "Inverts the face normals.");
00268         setKeyDescription(Qt::Key_R, "Sets surface, wireframe or points representation.");
00269         setKeyDescription(Qt::Key_X, "Toggles smooth shading.");
00270         setKeyDescription(Qt::Key_Return, "Saves a screenshot.");
00271         setKeyDescription(Qt::CTRL+Qt::Key_O, "Opens a file.");
00272         setKeyDescription(Qt::CTRL+Qt::Key_S, "Saves scene in a file.");
00273         setKeyDescription(Qt::CTRL+Qt::Key_C, "Starts the feature detection process.");
00274         setKeyDescription(Qt::Key_L, "switches display between the original mesh and the 2d parameterized region");
00275         
00276         setMouseBinding(Qt::LeftButton, SELECT);
00277         
00278 };
00279 
00280 void Viewer::changeMesh(const char* filename)
00281 {
00282         double scene_center[3], scene_radius;
00283         
00284         drawMesh_ = false;
00285         
00286         delete m;
00287         
00288         //reinitialisation des booleens
00289         drawUserSnake_=false ;
00290         drawLocalRegion_=false ;
00291         
00292         m = new Mesh(filename);
00293         if(m->isValid()) 
00294           {
00295                 Vec vec_scene_center(scene_center[0],scene_center[1],scene_center[2]); 
00296                 drawMesh_ = true;
00297                 m->BoundingBox(scene_center,&scene_radius);
00298                 camera()->setSceneCenter(vec_scene_center);
00299                 camera()->setSceneRadius(scene_radius*2);
00300                 showEntireScene();
00301           }
00302         else 
00303           {
00304                 QMessageBox::warning( this, "Error", m->message(), "OK", 0, 0, 0, 1 );
00305                 m = NULL;
00306           }
00307         
00308         glFlush();
00309         m->_snake_total.clear() ;
00310 };
00311 
00312 void Viewer::saveMesh(const char* filename)
00313 {
00314         m->saveToFile(filename);
00315 };
00316 
00317 void Viewer::mousePressEvent(QMouseEvent* e)
00318 {
00319 #if QT_VERSION < 0x040000
00320         if ((e->button() == Qt::RightButton) && (e->state() == Qt::NoButton))
00321 #else
00322                 if ((e->button() == Qt::RightButton) && (e->modifiers() == Qt::NoButton))
00323 #endif
00324                   {
00325 #if QT_VERSION < 0x040000
00326                         QPopupMenu menu( this );
00327                         menu.insertItem("Camera positions");
00328                         menu.insertSeparator();
00329                         QMap<int, int> menuMap;
00330 #else
00331                         QMenu menu( this );
00332                         menu.addAction("Camera positions");
00333                         menu.addSeparator();
00334                         QMap<QAction*, int> menuMap;
00335 #endif
00336                         
00337                         bool atLeastOne = false;
00338                         // We only test the 20 first indexes. This is a limitation.
00339                         for (unsigned short i=0; i<20; ++i)
00340                                 if (camera()->keyFrameInterpolator(i))
00341                                   {
00342                                         atLeastOne = true;
00343                                         QString text;
00344                                         if (camera()->keyFrameInterpolator(i)->numberOfKeyFrames() == 1)
00345                                                 text = "Position "+QString::number(i);
00346                                         else
00347                                                 text = "Path "+QString::number(i);
00348                                         
00349 #if QT_VERSION < 0x040000
00350                                         menuMap[menu.insertItem(text)] = i;
00351 #else
00352                                         menuMap[menu.addAction(text)] = i;
00353 #endif
00354                                   }
00355                                         
00356                                         if (!atLeastOne)
00357                                           {
00358 #if QT_VERSION < 0x040000
00359                                                 menu.insertItem("No position defined");
00360                                                 menu.insertItem("Use to Alt+Fx to define one");
00361 #else
00362                                                 menu.addAction("No position defined");
00363                                                 menu.addAction("Use to Alt+Fx to define one");
00364 #endif
00365                                           }
00366                                         
00367 #if QT_VERSION < 0x040000
00368                                         menu.setMouseTracking(true);
00369                         int select = menu.exec(e->globalPos());
00370                         
00371                         if (atLeastOne && select != -1)
00372                                 camera()->playPath(menuMap[select]);
00373 #else
00374                         QAction* action = menu.exec(e->globalPos());
00375                         
00376                         if (atLeastOne && action)
00377                                 camera()->playPath(menuMap[action]);
00378 #endif
00379                   }
00380                         else
00381                                 QGLViewer::mousePressEvent(e);
00382 }
00383 
00384 QString Viewer::helpString() const
00385 {
00386         QString text("<h2>Interface projets image</h2>");
00387         return text;
00388 };
00389 
00390 // operations a effectuer une fois le snake termine
00391 void Viewer::computeDetection() 
00392 {
00393         std::cout<<std::endl ;
00394         std::cout<<"DEMARRAGE DE LA DETECTION DE FEATURE"<<std::endl
00395            <<std::endl;
00396         std::cout<<"calcul region locale..." ;
00397         m->computeLocalRegion() ;
00398         std::cout<<"fait"<<std::endl ;
00399         drawLocalRegion_=true ;
00400         updateGL() ;
00401 }
00402 
00403 // bascule le mode affichage Mesh/Local Region   
00404 void Viewer::switch2d3d()
00405 {
00406         if (drawLocalRegion_)
00407           {
00408                 meshMode_ = !meshMode_ ;
00409                 updateGL() ;
00410           }
00411         else
00412                 std::cout << "***Lancer d'abord le calcul de la local region !" << std::endl ;
00413 }
00414 

Généré le Thu Jun 15 18:48:52 2006 pour Projet Image 2006 - Vincent Vidal, Florent Paccault - par  doxygen 1.4.7