Eigene Dateien/FlowVis/src/QTFCanvas.cpp

Go to the documentation of this file.
00001 #include "glew.h"
00002 #include "QTFCanvas.h"
00003 #include "icon.img"
00004 
00005 const int THRESHOLD_DETAIL = 8;
00006 const int THRESHOLD_OVERVIEW = 25;
00007 
00008 const DWORD dtClick = 250;
00009 DWORD clicked = 0;
00010 int dragging_tfpt = -1;
00011 int dragging_tfpt2d = -1;
00012 int modifytriangle = -1;
00013 
00014 VMouseEvent globalmetf = VMouseEvent();
00015 
00016 int RIGHT_PRESSED = 0;
00017 
00018 bool firstframeh = true;
00019 bool firstframew = true;
00020 bool firstframes = true;
00021 
00022 
00023 
00024 QTFCanvas::QTFCanvas(QWidget* parent /*= 0*/) : QObject(parent), QGLWidget(parent)
00025 {
00026         glInit ();
00027         //initIcon();
00028         tfbig = false;
00029 
00030         m_TFVector.resize( 6 );
00031         /*m_TFVector.push_back( VTransferFunction() );
00032         m_TFVector.push_back( VTransferFunction() );
00033         m_TFVector.push_back( VTransferFunction() );*/
00034 
00035         m_TransferFunction = &( m_TFVector[ 0 ] );
00036 }
00037 
00038 
00039 QTFCanvas::~QTFCanvas()
00040 {
00041         //glDeleteTextures(1, &icon_texturehandle);
00042 
00043         m_TFVector.clear();
00044         
00045 }
00046 
00047 void QTFCanvas::setObjectName(const QString &name)
00048 {
00049 }
00050 
00051 void QTFCanvas::setActiveTF( int index )
00052 {
00053 
00054         switch ( index )
00055         {
00056                 case 1:
00057                 {
00058                         m_TransferFunction = &( m_TFVector[ 0 ] );
00059                         break;
00060                 }
00061                 case 2:
00062                 {
00063                         m_TransferFunction = &( m_TFVector[ 1 ] );
00064                         break;
00065                 }
00066                 case 3:
00067                 {
00068                         m_TransferFunction = &( m_TFVector[ 2 ] );
00069                         break;
00070                 }
00071                 case 4:
00072                 {
00073                         m_TransferFunction = &( m_TFVector[ 3 ] );
00074                         break;
00075                 }
00076                 case 5:
00077                 {
00078                         m_TransferFunction = &( m_TFVector[ 4 ] );
00079                         break;
00080                 }
00081                 case 6:
00082                 {
00083                         m_TransferFunction = &( m_TFVector[ 5 ] );
00084                         break;
00085                 }
00086                 default :
00087                 {
00088                         m_TransferFunction = &( m_TFVector[ 0 ] );
00089                         break;
00090                 }
00091         
00092         }
00093 
00094         repaint();
00095 }
00096 
00097 void QTFCanvas::initializeGL()
00098 {
00099         /*this*/makeCurrent();
00100     // Set up the rendering context, define display lists etc.:
00101     //...
00102         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00103     glClearColor( 1.0, 1.0, 1.0, 1.0 );
00104     glEnable(GL_DEPTH_TEST);
00105     //...
00106 }
00107 
00108 void QTFCanvas::resizeGL( int w, int h )
00109 {
00110         /*this*/makeCurrent();
00111     glViewport(0, 0, w, h);
00112         gluOrtho2D(0, w, h, 0);
00113 
00114 }
00115 
00116 void QTFCanvas::paintGL()
00117 {
00118         //makeCurrent();
00119 
00120         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00121         glEnable(GL_DEPTH_TEST);
00122 
00123     // draw the scene:
00124     glPushAttrib(GL_ALL_ATTRIB_BITS);
00125         
00126         glMatrixMode(GL_PROJECTION);
00127         glPushMatrix();
00128 
00129         glMatrixMode(GL_MODELVIEW);
00130         glPushMatrix();
00131 
00132         glLoadIdentity();       // Reset The Current Modelview Matrix
00133         
00134 
00135         m_TransferFunction->draw1d();
00136 
00137         /*glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00138         glGenTextures(1, &icon_texturehandle);
00139 
00140         glEnable(GL_TEXTURE_2D);
00141         glActiveTextureARB(GL_TEXTURE0_ARB);
00142         glBindTexture(GL_TEXTURE_2D,icon_texturehandle);
00143         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
00144         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
00145         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
00146         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
00147 
00148         glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,16,16,0,GL_RGBA,GL_UNSIGNED_BYTE,vcImage);
00149         glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00150 
00151         float x1 = 1.0f - 16.0f/391.0f;
00152         float x2 = 1.0f;
00153         float y1 = 1.0f - 16.0f/121.0f;
00154         float y2 = 1.0f;
00155 
00156         glBegin(GL_QUADS);
00157                 glTexCoord2f(0.0, 1.0); glVertex3f(x1,y1, -0.05f);
00158                 glTexCoord2f(1.0, 1.0); glVertex3f(x2,y1, -0.05f);
00159                 glTexCoord2f(1.0, 0.0); glVertex3f(x2,y2, -0.05f);
00160                 glTexCoord2f(0.0, 0.0); glVertex3f(x1,y2, -0.05f);
00161         glEnd();
00162 
00163         glBindTexture(GL_TEXTURE_2D, 0);
00164         glDisable(GL_TEXTURE_2D);*/
00165 
00166         glMatrixMode(GL_PROJECTION);
00167         glPopMatrix();
00168 
00169         glMatrixMode(GL_MODELVIEW);
00170         glPopMatrix();
00171 
00172         glPopAttrib();
00173         
00174 }
00175 
00176 
00177 void QTFCanvas::paintEvent( QPaintEvent *e )
00178 {
00179         QGLWidget::paintEvent(e);
00180 };
00181 
00182 void QTFCanvas::mousePressEvent ( QMouseEvent * e )
00183 {               
00184         clicked = GetTickCount();
00185         globalmetf  = getMouseEvent(e);
00186 
00187         RIGHT_PRESSED = (globalmetf.getRightState());
00188         
00189 };
00190 
00191 void QTFCanvas::mouseReleaseEvent ( QMouseEvent * e )
00192 {
00193 
00194         //const VMouseEvent me(getMouseEvent(e));
00195         m_TransferFunction->setZoomPoint(false, 0, tfbig);
00196         dragging_tfpt = -1;
00197         dragging_tfpt2d = -1;
00198         modifytriangle = -1;
00199 
00200         //handle mouse click
00201         DWORD elapsed = GetTickCount() - clicked;
00202         if (elapsed < dtClick)
00203         {
00204                 mouseClickEvent(globalmetf);
00205         }
00206         
00207         //clicked = 0;
00208         RIGHT_PRESSED = 0;
00209 
00210         repaint();
00211         
00212 };
00213         
00214 void QTFCanvas::mouseMoveEvent ( QMouseEvent * e )
00215 {
00216         
00217         const VMouseEvent me(getMouseEvent(e));
00218         const VVector v = me.getPosition();
00219 
00220         m_TransferFunction->setZoomPoint(false, 0, tfbig);
00221 
00222         if(me.getLeftState() == 1)
00223         {
00224                 float x = v.getX();
00225                 float y = v.getY();
00226 
00227                 x = (x >  1.0f) ?  1.0f : x;
00228                 x = (x < -1.0f) ? -1.0f : x;
00229                 y = (y >  1.0f) ?  1.0f : y;
00230                 y = (y < -1.0f) ? -1.0f : y;
00231 
00232                 int density = (int)((x * 0.5f + 0.5f) * 4095.0f);
00233                 
00234                 if(dragging_tfpt > -1)
00235                 {
00236 
00237                         //drag a tf point also in x-y direction, not only alpha
00238                         vTransferFunctionPoint tmpPoint = m_TransferFunction->getTransferFunctionPoint(dragging_tfpt);
00239                         m_TransferFunction->removeTransferFunctionPoint(dragging_tfpt);
00240 
00241                         int index =0;
00242                         if(tfbig)
00243                         {
00244                                  index= m_TransferFunction->transferFunctionPointInRange(density, y, THRESHOLD_DETAIL);
00245                         }else
00246                         {
00247                                 index= m_TransferFunction->transferFunctionPointInRange(density, y, THRESHOLD_OVERVIEW);
00248                         }
00249 
00250                         if(index == -1)
00251                         {
00252                                 tmpPoint.m_Alpha = (y + 0.8f)/1.8f;
00253                                 if(tmpPoint.m_Alpha < 0.0f)
00254                                         tmpPoint.m_Alpha = 0.0f;
00255                                 m_TransferFunction->addTransferFunctionPoint(density, tmpPoint);
00256                                 dragging_tfpt = density;
00257                                 
00258                         }
00259                         else
00260                         {
00261                                 m_TransferFunction->addTransferFunctionPoint(dragging_tfpt, tmpPoint);
00262                         }
00263                         
00264                         }
00265                 else //if (dragging_tfpt == -1)
00266                 {
00267 
00268                         int index =0;
00269 
00270                         if(tfbig)
00271                         {
00272                                  index= m_TransferFunction->transferFunctionPointInRange(density, y, THRESHOLD_DETAIL);
00273                         }else
00274                         {
00275                                 index= m_TransferFunction->transferFunctionPointInRange(density, y, THRESHOLD_OVERVIEW);
00276                         }
00277 
00278                         dragging_tfpt = index;
00279                         /*density = m_TransferFunction.transferFunctionPointInRange(density, v.getY(), THRESHOLD);
00280                         vTransferFunctionPoint tmpPoint = m_TransferFunction.getTransferFunctionPoint(density);
00281                         tmpPoint.m_Alpha = v.getY() * 0.5f + 0.5f;
00282                         m_TransferFunction.addTransferFunctionPoint(density, tmpPoint);*/
00283 
00284                 }
00285                 repaint();
00286                 this->swapBuffers();
00287                 return;
00288         }
00289         
00290         if (me.getMiddleState() == 1)
00291         {
00292                 m_TransferFunction->setZoomPoint(true, v.getX(), tfbig);
00293                 repaint();
00294                 this->swapBuffers();
00295                 return;
00296                 
00297         }
00298         
00299 
00300 };
00301 
00302 void QTFCanvas::mouseClickEvent(VMouseEvent me)
00303 {
00304 
00305         const VVector v = globalmetf.getPosition();
00306 
00307         if(me.getModifiers() == me.MODIFIER_ALT)return;
00308 
00309         
00310         if(globalmetf.getLeftState() == 1)
00311         {
00312                 if(v.getY() >= -0.8f)
00313                 {
00314                         int density = (int)((v.getX() * 0.5f + 0.5f) * 4095.0f);
00315 
00316                         //add new point
00317                         int tmptres =0;
00318                         if(tfbig)
00319                         {
00320                                  tmptres = THRESHOLD_DETAIL;
00321                         }else
00322                         {
00323                                  tmptres = THRESHOLD_OVERVIEW;
00324                         }
00325 
00326                         if(m_TransferFunction->transferFunctionPointInRange(density, v.getY(), tmptres)==-1)
00327                         {
00328                                 QColor qcolor = QColorDialog::getColor();
00329                                 VVector color((float)qcolor.red()/255.0f, (float)qcolor.green()/255.0f, (float)qcolor.blue()/255.0f);
00330                                 float alpha = (v.getY() + 0.8f)/1.8f;
00331                                 if(alpha < 0.0f)
00332                                         alpha = 0.0f;
00333                                 vTransferFunctionPoint tmpPoint;
00334                                 tmpPoint.m_Alpha = alpha;
00335                                 tmpPoint.m_Color = color;
00336                                 m_TransferFunction->addTransferFunctionPoint(density, tmpPoint);
00337                         }
00338                         else
00339                         {
00340                                 /*density = m_TransferFunction.transferFunctionPointInRange(density, v.getY(), THRESHOLD);
00341                                 vTransferFunctionPoint tmpPoint = m_TransferFunction.getTransferFunctionPoint(density);
00342                                 tmpPoint.m_Alpha = v.getY() * 0.5f + 0.5f;
00343                                 m_TransferFunction.addTransferFunctionPoint(density, tmpPoint);*/
00344                         }
00345                 }
00346         }       
00347         
00348         
00349 
00350         repaint();
00351 
00352 
00353 }
00354 
00355 void QTFCanvas::mouseDoubleClickEvent ( QMouseEvent * e )
00356 {
00357 
00358         const VMouseEvent me(getMouseEvent(e));
00359         const VVector v = me.getPosition();
00360 
00361         if(me.getModifiers() == me.MODIFIER_ALT && me.getLeftState() == 1)
00362         {
00363                 if(tfbig)
00364                 {
00365                         setGeometry(0,0,381,121);
00366                         hbar->setVisible(false);
00367                         vbar->setVisible(false);
00368                 }
00369                 else
00370                 {
00371                         setGeometry(0,-215,1143,363);
00372                         hbar->setVisible(true);
00373                         vbar->setVisible(true);
00374                         hbar->setValue(0);
00375                         vbar->setValue(0);
00376                 }
00377                 
00378                 tfbig = !tfbig;
00379         }
00380         
00381         int density = (int)((v.getX() * 0.5f + 0.5f) * 4095.0f);
00382 
00383         int tmptres =0;
00384         if(tfbig)
00385         {
00386                  tmptres= THRESHOLD_DETAIL;
00387         }else
00388         {
00389                  tmptres= THRESHOLD_OVERVIEW;
00390         }
00391 
00392         if(me.getLeftState() == 1)
00393         {
00394                 //doubleclick with left mouse
00395                 //detect if there is a TF Point in range
00396                 //if Y, call the QDialog to change its color
00397                 
00398                 int index = m_TransferFunction->transferFunctionPointInRange(density,v.getY(), tmptres);
00399 
00400                 if (index == -1) return;
00401 
00402                 QColor qcolor = QColorDialog::getColor();
00403                 VVector color((float)qcolor.red()/255.0f, (float)qcolor.green()/255.0f, (float)qcolor.blue()/255.0f);
00404                 float a = m_TransferFunction->getAlpha(index);
00405                 m_TransferFunction->removeTransferFunctionPoint(index);
00406 
00407                 vTransferFunctionPoint tmpPoint;
00408                 tmpPoint.m_Color = color;
00409                 tmpPoint.m_Alpha = a;
00410                 m_TransferFunction->addTransferFunctionPoint(index, tmpPoint);
00411 
00412         }
00413         else if(me.getRightState() == 1)
00414         {
00415                 //doubleclick with right mouse
00416                 //detect if there is a TF Point in range
00417                 //delete this TF Pt fromt the list
00418                 
00419                 if(m_TransferFunction->transferFunctionPointInRange(density,v.getY(), tmptres)==-1)
00420                 {
00421                         
00422                 }
00423                 else
00424                 {
00425                         density = m_TransferFunction->transferFunctionPointInRange(density, v.getY(), tmptres);
00426                         m_TransferFunction->removeTransferFunctionPoint(density);
00427                 }
00428                 
00429         }
00430         
00431 };
00432 
00433 void QTFCanvas::keyPressEvent ( QKeyEvent * e )
00434 {
00435         //const VMouseEvent me(getMouseEvent(e));
00436 
00437 };
00438 
00439 void QTFCanvas::keyReleaseEvent ( QKeyEvent * e )
00440 {
00441         //const VMouseEvent me(getMouseEvent(e));
00442         
00443 
00444 };
00445 
00446 const VMouseEvent QTFCanvas::getMouseEvent (QMouseEvent *e)
00447 {
00448         const VVector vecPosition( (2.0f * float(e->x()) - float(width()))  / float(width()),
00449                                       ( float(height()) - 2.0f * float(e->y())) / float(height()),
00450                                                            0.0f);
00451         const int iButton = 
00452                                 ((e->button() == Qt::LeftButton)        ? VMouseEvent::BUTTON_LEFT       : 
00453                                 ((e->button() == Qt::MidButton)         ? VMouseEvent::BUTTON_MIDDLE : 
00454                                 ((e->button() == Qt::RightButton)       ? VMouseEvent::BUTTON_RIGHT      : VMouseEvent::BUTTON_NONE)));
00455 
00456         const int iStateLeft    = (e->buttons () & Qt::LeftButton)      ? VMouseEvent::STATE_DOWN : VMouseEvent::STATE_UP;
00457         const int iStateMiddle  = (e->buttons () & Qt::MidButton)       ? VMouseEvent::STATE_DOWN : VMouseEvent::STATE_UP;
00458         const int iStateRight   = (e->buttons () & Qt::RightButton) ? VMouseEvent::STATE_DOWN : VMouseEvent::STATE_UP;
00459 
00460         const int iModifiers =   ((e->modifiers()   & Qt::ShiftModifier)   ? VKeyboardEvent::MODIFIER_SHIFT : 0) 
00461                                                    | ((e->modifiers()   & Qt::ControlModifier) ? VKeyboardEvent::MODIFIER_CTRL  : 0) 
00462                                                    | ((e->modifiers()   & Qt::AltModifier)     ? VKeyboardEvent::MODIFIER_ALT  : 0);
00463 
00464         return VMouseEvent(vecPosition,iButton,iStateLeft,iStateMiddle,iStateRight,iModifiers);
00465 
00466 };
00467 
00468 const VKeyboardEvent QTFCanvas::getKeyboardEvent (QKeyEvent *e)
00469 {
00470         const int iKey = e->key();
00471 
00472         const int iModifiers =    ((e->modifiers() & Qt::ShiftModifier)         ? VKeyboardEvent::MODIFIER_SHIFT : 0) 
00473                                                         | ((e->modifiers() & Qt::ControlModifier)       ? VKeyboardEvent::MODIFIER_CTRL  : 0) 
00474                                                         | ((e->modifiers() & Qt::AltModifier)           ? VKeyboardEvent::MODIFIER_ALT   : 0);
00475 
00476         return VKeyboardEvent(iKey,iModifiers);
00477 
00478 };
00479 
00480 void QTFCanvas::redraw()
00481 {
00482         /*this*/makeCurrent();
00483         paintGL();
00484 }
00485 
00486 void QTFCanvas::setHistogram(std::vector<int> *histogram, unsigned char *histogram2d)
00487 {
00488         /*this*/makeCurrent();
00489         m_TransferFunction->setHistogram(histogram);
00490         //m_TransferFunction2d.setHistogram2D(histogram2d);
00491         this->repaint();
00492 }
00493 
00494 VTransferFunction * QTFCanvas::getTransferFunctionPtr()
00495 {
00496         return m_TransferFunction;
00497 }
00498 
00499 VTransferFunction * QTFCanvas::getTransferFunctionPtr( int index )
00500 {
00501         return &(m_TFVector[index - 1]);
00502 }
00503 
00504 
00505 void QTFCanvas::saveTf(std::string filename)
00506 {
00507         std::string fileend = filename.substr(filename.find_last_of('.'), filename.size()-1);
00508 
00509         
00510         if(fileend.size() == 4)
00511         {
00512                 if(fileend.compare(".btf") != 0)
00513                 {
00514                         filename = filename.substr(0, filename.find_last_of('.'));
00515                         filename += ".btf";
00516                 }
00517         }
00518         m_TransferFunction->save(filename);
00519         
00520 
00521 }
00522 
00523 void QTFCanvas::loadTf(std::string filename)
00524 {
00525         std::string filend = filename.substr(filename.find_last_of('.'), filename.size()-1);
00526 
00527         if(filend.compare(".btf") == 0)
00528         {
00529                 m_TransferFunction->load(filename);
00530         }
00531 }

Generated on Mon Jan 21 01:15:15 2008 for FlowVis by  doxygen 1.5.4