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 ) : QObject(parent), QGLWidget(parent)
00025 {
00026 m_tfmode = 0;
00027 glInit ();
00028
00029 tfbig = false;
00030 }
00031
00032
00033 QTFCanvas::~QTFCanvas()
00034 {
00035
00036
00037 }
00038
00039 void QTFCanvas::setObjectName(const QString &name)
00040 {
00041 }
00042
00043 void QTFCanvas::setModePtr(QComboBox* cb)
00044 {
00045 m_tfmode_cb_Ptr = cb;
00046 }
00047
00048 void QTFCanvas::checkForLongClick()
00049 {
00050
00051 DWORD elapsed = GetTickCount() - clicked;
00052 VVector pos = globalmetf.getPosition();
00053
00054 if (RIGHT_PRESSED == 1 &&
00055 elapsed > 1.3f*dtClick
00056
00057 )
00058 {
00059 m_tfmode_cb_Ptr->setVisible(true);
00060 return;
00061 }
00062
00063 m_tfmode_cb_Ptr->setVisible(false);
00064 }
00065
00066 void QTFCanvas::initializeGL()
00067 {
00068 makeCurrent();
00069
00070
00071 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00072 glClearColor( 1.0, 1.0, 1.0, 1.0 );
00073 glEnable(GL_DEPTH_TEST);
00074
00075 }
00076
00077 void QTFCanvas::resizeGL( int w, int h )
00078 {
00079 makeCurrent();
00080 glViewport(0, 0, w, h);
00081 gluOrtho2D(0, w, h, 0);
00082
00083 }
00084
00085 void QTFCanvas::paintGL()
00086 {
00087
00088 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00089 glEnable(GL_DEPTH_TEST);
00090
00091
00092 glPushAttrib(GL_ALL_ATTRIB_BITS);
00093
00094 glMatrixMode(GL_PROJECTION);
00095 glPushMatrix();
00096
00097 glMatrixMode(GL_MODELVIEW);
00098 glPushMatrix();
00099
00100 glLoadIdentity();
00101
00102 if(m_tfmode == 1)
00103 m_TransferFunction.draw1d();
00104
00105 if(m_tfmode == 2)
00106 m_TransferFunction2d.draw2d();
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 glMatrixMode(GL_PROJECTION);
00138 glPopMatrix();
00139
00140 glMatrixMode(GL_MODELVIEW);
00141 glPopMatrix();
00142
00143 glPopAttrib();
00144
00145 }
00146
00147
00148 void QTFCanvas::paintEvent( QPaintEvent *e )
00149 {
00150 QGLWidget::paintEvent(e);
00151 };
00152
00153 void QTFCanvas::mousePressEvent ( QMouseEvent * e )
00154 {
00155 clicked = GetTickCount();
00156 globalmetf = getMouseEvent(e);
00157
00158 RIGHT_PRESSED = (globalmetf.getRightState());
00159
00160 };
00161
00162 void QTFCanvas::mouseReleaseEvent ( QMouseEvent * e )
00163 {
00164 m_tfmode_cb_Ptr->setVisible(false);
00165
00166
00167 m_TransferFunction.setZoomPoint(false, 0, tfbig);
00168 dragging_tfpt = -1;
00169 dragging_tfpt2d = -1;
00170 modifytriangle = -1;
00171
00172
00173 DWORD elapsed = GetTickCount() - clicked;
00174 if (elapsed < dtClick)
00175 {
00176 mouseClickEvent(globalmetf);
00177 }
00178
00179
00180 RIGHT_PRESSED = 0;
00181
00182 repaint();
00183
00184 };
00185
00186 void QTFCanvas::mouseMoveEvent ( QMouseEvent * e )
00187 {
00188 if(m_tfmode == 0)
00189 return;
00190
00191 const VMouseEvent me(getMouseEvent(e));
00192 const VVector v = me.getPosition();
00193
00194 m_TransferFunction.setZoomPoint(false, 0, tfbig);
00195
00196
00197 if(m_tfmode == 1)
00198 {
00199 if(me.getLeftState() == 1)
00200 {
00201 float x = v.getX();
00202 float y = v.getY();
00203
00204 x = (x > 1.0f) ? 1.0f : x;
00205 x = (x < -1.0f) ? -1.0f : x;
00206 y = (y > 1.0f) ? 1.0f : y;
00207 y = (y < -1.0f) ? -1.0f : y;
00208
00209 int density = (int)((x * 0.5f + 0.5f) * 4095.0f);
00210
00211 if(dragging_tfpt > -1)
00212 {
00213
00214
00215 vTransferFunctionPoint tmpPoint = m_TransferFunction.getTransferFunctionPoint(dragging_tfpt);
00216 m_TransferFunction.removeTransferFunctionPoint(dragging_tfpt);
00217
00218 int index =0;
00219 if(tfbig)
00220 {
00221 index= m_TransferFunction.transferFunctionPointInRange(density, y, THRESHOLD_DETAIL);
00222 }else
00223 {
00224 index= m_TransferFunction.transferFunctionPointInRange(density, y, THRESHOLD_OVERVIEW);
00225 }
00226
00227 if(index == -1)
00228 {
00229 tmpPoint.m_Alpha = (y + 0.8f)/1.8f;
00230 if(tmpPoint.m_Alpha < 0.0f)
00231 tmpPoint.m_Alpha = 0.0f;
00232 m_TransferFunction.addTransferFunctionPoint(density, tmpPoint);
00233 dragging_tfpt = density;
00234
00235 }
00236 else
00237 {
00238 m_TransferFunction.addTransferFunctionPoint(dragging_tfpt, tmpPoint);
00239 }
00240
00241 }
00242 else
00243 {
00244
00245 int index =0;
00246
00247 if(tfbig)
00248 {
00249 index= m_TransferFunction.transferFunctionPointInRange(density, y, THRESHOLD_DETAIL);
00250 }else
00251 {
00252 index= m_TransferFunction.transferFunctionPointInRange(density, y, THRESHOLD_OVERVIEW);
00253 }
00254
00255 dragging_tfpt = index;
00256
00257
00258
00259
00260
00261 }
00262 repaint();
00263 this->swapBuffers();
00264 return;
00265 }
00266
00267 if (me.getMiddleState() == 1)
00268 {
00269 m_TransferFunction.setZoomPoint(true, v.getX(), tfbig);
00270 repaint();
00271 this->swapBuffers();
00272 return;
00273
00274 }
00275 }
00276
00277 if(m_tfmode == 2)
00278 {
00279 if(me.getModifiers() != me.MODIFIER_CTRL && me.getLeftState() == 1)
00280 {
00281 float x = v.getX();
00282 float y = v.getY();
00283
00284 x = (x > 1.0f) ? 1.0f : x;
00285 x = (x < -1.0f) ? -1.0f : x;
00286 y = (y > 1.0f) ? 1.0f : y;
00287 y = (y < -1.0f) ? -1.0f : y;
00288
00289 float density = x * 0.5f + 0.5f;
00290 float gmag = y * 0.5f + 0.5f;
00291
00292 int tmptres = 0;
00293
00294 if(tfbig)
00295 {
00296 tmptres = THRESHOLD_DETAIL;
00297 }else
00298 {
00299 tmptres = THRESHOLD_OVERVIEW;
00300 }
00301
00302 if( dragging_tfpt2d > -1)
00303 {
00304 m_TransferFunction2d.changePointPosition(dragging_tfpt2d, x, y );
00305 m_TransferFunction.setZoomPoint(true, v.getX(), tfbig);
00306
00307 repaint();
00308 this->swapBuffers();
00309 return;
00310 }
00311 else{
00312
00313 dragging_tfpt2d = m_TransferFunction2d.findIndexTransferFunctionPoint( density, gmag, (float)tmptres/1024.0f );
00314 }
00315 }
00316
00317 if(me.getModifiers() == me.MODIFIER_CTRL )
00318 {
00319
00320 if(me.getLeftState() == 1)
00321 {
00322 float x2 = v.getX();
00323 float y2 = v.getY();
00324
00325 float x1 = globalmetf.getPosition().getX();
00326 float y1 = globalmetf.getPosition().getY();
00327
00328
00329 float offsetw = x2 - x1;
00330 float offseth = y2 - y1;
00331
00332 offsetw *= 0.5f;
00333 offseth *= 0.5f;
00334
00335 float density = x1 * 0.5f + 0.5f;
00336 float gmag = y1 * 0.5f + 0.5f;
00337
00338 int tmptres = 0;
00339
00340 if(tfbig)
00341 {
00342 tmptres = THRESHOLD_DETAIL;
00343 }else
00344 {
00345 tmptres = THRESHOLD_OVERVIEW;
00346 }
00347
00348 if(modifytriangle == -1)
00349 {
00350 modifytriangle = m_TransferFunction2d.findIndexTransferFunctionPoint( density, gmag, (float)tmptres/1024.0f );
00351 }
00352 else
00353 {
00354 m_TransferFunction2d.changeTriangleSize( modifytriangle, offsetw, offseth );
00355 }
00356
00357
00358 globalmetf = me;
00359
00360 repaint();
00361 this->swapBuffers();
00362 return;
00363 }
00364
00365 if(me.getRightState() == 1)
00366 {
00367 float x2 = v.getX();
00368
00369 float x1 = globalmetf.getPosition().getX();
00370 float y1 = globalmetf.getPosition().getY();
00371
00372 float offset = x2 - x1;
00373 offset *= 0.5f;
00374
00375 float density = x1 * 0.5f + 0.5f;
00376 float gmag = y1 * 0.5f + 0.5f;
00377
00378 int tmptres = 0;
00379
00380 if(tfbig)
00381 {
00382 tmptres = THRESHOLD_DETAIL;
00383 }else
00384 {
00385 tmptres = THRESHOLD_OVERVIEW;
00386 }
00387
00388 if(modifytriangle == -1)
00389 {
00390 modifytriangle = m_TransferFunction2d.findIndexTransferFunctionPoint( density, gmag, (float)tmptres/1024.0f );
00391 }
00392 else
00393 {
00394 m_TransferFunction2d.changeTriangleSkew( modifytriangle, offset );
00395 }
00396
00397
00398 globalmetf = me;
00399
00400 repaint();
00401 this->swapBuffers();
00402 return;
00403 }
00404
00405
00406
00407 }
00408 }
00409
00410 };
00411
00412 void QTFCanvas::mouseClickEvent(VMouseEvent me)
00413 {
00414 if(m_tfmode == 0)
00415 return;
00416
00417 const VVector v = globalmetf.getPosition();
00418
00419 if(me.getModifiers() == me.MODIFIER_ALT)return;
00420
00421 if(m_tfmode == 1)
00422 {
00423 if(globalmetf.getLeftState() == 1)
00424 {
00425 if(v.getY() >= -0.8f)
00426 {
00427 int density = (int)((v.getX() * 0.5f + 0.5f) * 4095.0f);
00428
00429
00430 int tmptres =0;
00431 if(tfbig)
00432 {
00433 tmptres = THRESHOLD_DETAIL;
00434 }else
00435 {
00436 tmptres = THRESHOLD_OVERVIEW;
00437 }
00438
00439 if(m_TransferFunction.transferFunctionPointInRange(density, v.getY(), tmptres)==-1)
00440 {
00441 QColor qcolor = QColorDialog::getColor();
00442 VVector color((float)qcolor.red()/255.0f, (float)qcolor.green()/255.0f, (float)qcolor.blue()/255.0f);
00443 float alpha = (v.getY() + 0.8f)/1.8f;
00444 if(alpha < 0.0f)
00445 alpha = 0.0f;
00446 vTransferFunctionPoint tmpPoint;
00447 tmpPoint.m_Alpha = alpha;
00448 tmpPoint.m_Color = color;
00449 m_TransferFunction.addTransferFunctionPoint(density, tmpPoint);
00450 }
00451 else
00452 {
00453
00454
00455
00456
00457 }
00458 }
00459 }
00460 }
00461
00462 if(m_tfmode == 2)
00463 {
00464 if(me.getModifiers() != me.MODIFIER_CTRL && globalmetf.getLeftState() == 1)
00465 {
00466
00467
00468 float density = (v.getX() * 0.5f + 0.5f);
00469
00470
00471 int tmptres =0;
00472 if(tfbig)
00473 {
00474 tmptres = THRESHOLD_DETAIL;
00475 }else
00476 {
00477 tmptres = THRESHOLD_OVERVIEW;
00478 }
00479 VVector result = m_TransferFunction2d.transferFunctionPointInRange(density, v.getY()*0.5 + 0.5, (float)tmptres/4095.0f);
00480
00481 if(result.getZ() == -1.0f)
00482 {
00483 QRgb qrgba = QColorDialog::getRgba(0xffffffff);
00484
00485 QColor qcolor;
00486 qcolor.setRgba(qrgba);
00487
00488 VVector color((float)qcolor.red()/255.0f, (float)qcolor.green()/255.0f, (float)qcolor.blue()/255.0f);
00489
00490 vTransferFunction2DPoint tmpPoint;
00491 tmpPoint.m_Alpha = (float)qcolor.alpha()/255.0f;
00492 tmpPoint.m_Color = color;
00493 tmpPoint.m_gmag = v.getY()*0.5f + 0.5f;
00494 tmpPoint.m_density = (float)density;
00495
00496 m_TransferFunction2d.addTransferFunctionPoint(tmpPoint);
00497
00498 repaint();
00499 }
00500 }
00501 }
00502
00503
00504 repaint();
00505
00506
00507 }
00508
00509 void QTFCanvas::mouseDoubleClickEvent ( QMouseEvent * e )
00510 {
00511 if(m_tfmode == 0)
00512 return;
00513
00514 const VMouseEvent me(getMouseEvent(e));
00515 const VVector v = me.getPosition();
00516
00517
00518
00519 if(m_tfmode == 1)
00520 {
00521 if(me.getModifiers() == me.MODIFIER_ALT && me.getLeftState() == 1)
00522 {
00523 if(tfbig)
00524 {
00525 setGeometry(0,0,381,121);
00526 hbar->setVisible(false);
00527 vbar->setVisible(false);
00528 }
00529 else
00530 {
00531 setGeometry(0,-215,1143,363);
00532 hbar->setVisible(true);
00533 vbar->setVisible(true);
00534 hbar->setValue(0);
00535 vbar->setValue(0);
00536 }
00537
00538 tfbig = !tfbig;
00539 }
00540
00541 int density = (int)((v.getX() * 0.5f + 0.5f) * 4095.0f);
00542
00543 int tmptres =0;
00544 if(tfbig)
00545 {
00546 tmptres= THRESHOLD_DETAIL;
00547 }else
00548 {
00549 tmptres= THRESHOLD_OVERVIEW;
00550 }
00551
00552 if(me.getLeftState() == 1)
00553 {
00554
00555
00556
00557
00558 int index = m_TransferFunction.transferFunctionPointInRange(density,v.getY(), tmptres);
00559
00560 if (index == -1) return;
00561
00562 QColor qcolor = QColorDialog::getColor();
00563 VVector color((float)qcolor.red()/255.0f, (float)qcolor.green()/255.0f, (float)qcolor.blue()/255.0f);
00564 float a = m_TransferFunction.getAlpha(index);
00565 m_TransferFunction.removeTransferFunctionPoint(index);
00566
00567 vTransferFunctionPoint tmpPoint;
00568 tmpPoint.m_Color = color;
00569 tmpPoint.m_Alpha = a;
00570 m_TransferFunction.addTransferFunctionPoint(index, tmpPoint);
00571
00572 }
00573 else if(me.getRightState() == 1)
00574 {
00575
00576
00577
00578
00579 if(m_TransferFunction.transferFunctionPointInRange(density,v.getY(), tmptres)==-1)
00580 {
00581
00582 }
00583 else
00584 {
00585 density = m_TransferFunction.transferFunctionPointInRange(density, v.getY(), tmptres);
00586 m_TransferFunction.removeTransferFunctionPoint(density);
00587 }
00588
00589 }
00590 }
00591
00592 if(m_tfmode == 2)
00593 {
00594
00595 int tmptres =0;
00596 if(tfbig)
00597 {
00598 tmptres= THRESHOLD_DETAIL;
00599 }else
00600 {
00601 tmptres= THRESHOLD_OVERVIEW;
00602 }
00603
00604 if(me.getModifiers() == me.MODIFIER_ALT && me.getLeftState() == 1)
00605 {
00606 if(tfbig)
00607 {
00608 setGeometry(0,0,381,121);
00609 hbar->setVisible(false);
00610 vbar->setVisible(false);
00611 }
00612 else
00613 {
00614 setGeometry(0,-242,1143,363);
00615 hbar->setVisible(true);
00616 vbar->setVisible(true);
00617 hbar->setValue(0);
00618 vbar->setValue(0);
00619 }
00620
00621 tfbig = !tfbig;
00622
00623 return;
00624 }
00625
00626 float density = v.getX() * 0.5f + 0.5f;
00627 float gmag = v.getY() * 0.5f + 0.5f;
00628
00629 if(me.getLeftState() == 1 && me.getModifiers() == me.MODIFIER_CTRL)
00630 {
00631
00632
00633 QRgb qrgba = QColorDialog::getRgba(0xffffffff);
00634
00635 QColor qcolor;
00636 qcolor.setRgba(qrgba);
00637
00638 VVector color((float)qcolor.red()/255.0f, (float)qcolor.green()/255.0f, (float)qcolor.blue()/255.0f);
00639 float alpha = (float)qcolor.alpha()/255.0f;
00640
00641 m_TransferFunction2d.changePointColor( density, gmag, color, alpha, (float)tmptres/1024.0f );
00642
00643 }
00644 else if(me.getRightState() == 1)
00645 {
00646
00647 m_TransferFunction2d.removeTransferFunctionPoint( density, gmag, (float)tmptres/1024.0f );
00648
00649 }
00650 }
00651
00652
00653 };
00654
00655 void QTFCanvas::keyPressEvent ( QKeyEvent * e )
00656 {
00657
00658
00659 };
00660
00661 void QTFCanvas::keyReleaseEvent ( QKeyEvent * e )
00662 {
00663
00664
00665
00666 };
00667
00668 const VMouseEvent QTFCanvas::getMouseEvent (QMouseEvent *e)
00669 {
00670 const VVector vecPosition( (2.0f * float(e->x()) - float(width())) / float(width()),
00671 ( float(height()) - 2.0f * float(e->y())) / float(height()),
00672 0.0f);
00673 const int iButton =
00674 ((e->button() == Qt::LeftButton) ? VMouseEvent::BUTTON_LEFT :
00675 ((e->button() == Qt::MidButton) ? VMouseEvent::BUTTON_MIDDLE :
00676 ((e->button() == Qt::RightButton) ? VMouseEvent::BUTTON_RIGHT : VMouseEvent::BUTTON_NONE)));
00677
00678 const int iStateLeft = (e->buttons () & Qt::LeftButton) ? VMouseEvent::STATE_DOWN : VMouseEvent::STATE_UP;
00679 const int iStateMiddle = (e->buttons () & Qt::MidButton) ? VMouseEvent::STATE_DOWN : VMouseEvent::STATE_UP;
00680 const int iStateRight = (e->buttons () & Qt::RightButton) ? VMouseEvent::STATE_DOWN : VMouseEvent::STATE_UP;
00681
00682 const int iModifiers = ((e->modifiers() & Qt::ShiftModifier) ? VKeyboardEvent::MODIFIER_SHIFT : 0)
00683 | ((e->modifiers() & Qt::ControlModifier) ? VKeyboardEvent::MODIFIER_CTRL : 0)
00684 | ((e->modifiers() & Qt::AltModifier) ? VKeyboardEvent::MODIFIER_ALT : 0);
00685
00686 return VMouseEvent(vecPosition,iButton,iStateLeft,iStateMiddle,iStateRight,iModifiers);
00687
00688 };
00689
00690 const VKeyboardEvent QTFCanvas::getKeyboardEvent (QKeyEvent *e)
00691 {
00692 const int iKey = e->key();
00693
00694 const int iModifiers = ((e->modifiers() & Qt::ShiftModifier) ? VKeyboardEvent::MODIFIER_SHIFT : 0)
00695 | ((e->modifiers() & Qt::ControlModifier) ? VKeyboardEvent::MODIFIER_CTRL : 0)
00696 | ((e->modifiers() & Qt::AltModifier) ? VKeyboardEvent::MODIFIER_ALT : 0);
00697
00698 return VKeyboardEvent(iKey,iModifiers);
00699
00700 };
00701
00702 void QTFCanvas::redraw()
00703 {
00704 makeCurrent();
00705 paintGL();
00706 }
00707
00708 void QTFCanvas::setHistogram(std::vector<int> *histogram, unsigned char *histogram2d)
00709 {
00710 makeCurrent();
00711 m_TransferFunction.setHistogram(histogram);
00712 m_TransferFunction2d.setHistogram2D(histogram2d);
00713 this->repaint();
00714 }
00715
00716 VTransferFunction * QTFCanvas::getTransferFunctionPtr()
00717 {
00718 return &m_TransferFunction;
00719 }
00720
00721 VTransferFunction2D * QTFCanvas::getTransferFunction2DPtr()
00722 {
00723 return &m_TransferFunction2d;
00724 }
00725
00726 void QTFCanvas::modeChanged(int index)
00727 {
00728
00729 RIGHT_PRESSED = 0;
00730
00731 m_tfmode = index;
00732 }
00733
00734 void QTFCanvas::saveTf(std::string filename)
00735 {
00736 std::string fileend = filename.substr(filename.find_last_of('.'), filename.size()-1);
00737
00738 switch(m_tfmode)
00739 {
00740 case 1:
00741
00742 if(fileend.size() == 4)
00743 {
00744 if(fileend.compare(".btf") != 0)
00745 {
00746 filename = filename.substr(0, filename.find_last_of('.'));
00747 filename += ".btf";
00748 }
00749 }
00750 else if(fileend.size() == 5)
00751 {
00752
00753 filename = filename.substr(0, filename.find_last_of('.'));
00754 filename += ".btf";
00755 }
00756 m_TransferFunction.save(filename);
00757 break;
00758 case 2:
00759 if(fileend.size() == 5)
00760 {
00761 if(fileend.compare(".bt2f") != 0)
00762 {
00763 filename = filename.substr(0, filename.find_last_of('.'));
00764 filename += ".bt2f";
00765 }
00766 }
00767 else if(fileend.size() == 4)
00768 {
00769
00770 filename = filename.substr(0, filename.find_last_of('.'));
00771 filename += ".bt2f";
00772 }
00773 m_TransferFunction2d.save(filename);
00774 break;
00775 }
00776
00777 }
00778
00779 void QTFCanvas::loadTf(std::string filename)
00780 {
00781 std::string filend = filename.substr(filename.find_last_of('.'), filename.size()-1);
00782
00783 if(filend.compare(".btf") == 0)
00784 {
00785 m_TransferFunction.load(filename);
00786 }
00787 else if(filend.compare(".bt2f") == 0)
00788 {
00789 m_TransferFunction2d.load(filename);
00790 }
00791 }