Advertisement
Guest User

Problem in subclassing vtkInteractorStyle in FourPainView

a guest
Sep 11th, 2013
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.38 KB | None | 0 0
  1. // The original code is from VTK's Examples from here:
  2. // https://github.com/Kitware/VTK/tree/master/Examples/GUI/Qt/FourPaneViewer
  3. // This link also includes the necessary .ui file for Qt
  4. // and the cmake file.
  5. //
  6. // Note: In QtVTKRenderWindows.cxx you may want to change
  7. // the path to the Dicom directory to an appropriate location.
  8.  
  9. //==========================
  10. //QtVTKRenderWindows.h
  11. //==========================
  12.  
  13. #ifndef QtVTKRenderWindows_H
  14. #define QtVTKRenderWindows_H
  15.  
  16. #include "vtkSmartPointer.h"
  17. #include "vtkResliceImageViewer.h"
  18. #include "vtkImagePlaneWidget.h"
  19. #include "vtkDistanceWidget.h"
  20. #include "vtkResliceImageViewerMeasurements.h"
  21. #include <QMainWindow>
  22.  
  23. // Forward Qt class declarations
  24. class Ui_QtVTKRenderWindows;
  25.  
  26. class QtVTKRenderWindows : public QMainWindow
  27. {
  28.   Q_OBJECT
  29. public:
  30.  
  31.   // Constructor/Destructor
  32.   QtVTKRenderWindows(int argc, char *argv[]);
  33.   ~QtVTKRenderWindows() {};
  34.  
  35. public slots:
  36.   virtual void resliceMode(int);
  37.   virtual void Render();
  38.  
  39. protected:
  40.   vtkSmartPointer< vtkResliceImageViewer > riw[3];
  41.   vtkSmartPointer< vtkImagePlaneWidget > planeWidget[3];
  42.   vtkSmartPointer< vtkDistanceWidget > DistanceWidget[3];
  43.   vtkSmartPointer< vtkResliceImageViewerMeasurements > ResliceMeasurements;
  44.  
  45. private:
  46.  
  47.   // Designer form
  48.   Ui_QtVTKRenderWindows *ui;
  49. };
  50.  
  51. #endif // QtVTKRenderWindows_H
  52.  
  53. //============================
  54. //QtVTKRenderWindows.cxx
  55. //============================
  56.  
  57. #include "ui_QtVTKRenderWindows.h"
  58. #include "QtVTKRenderWindows.h"
  59.  
  60. #include <vtkRenderer.h>
  61. #include <vtkRenderWindow.h>
  62. #include "vtkResliceImageViewer.h"
  63. #include "vtkResliceCursorLineRepresentation.h"
  64. #include "vtkResliceCursorThickLineRepresentation.h"
  65. #include "vtkResliceCursorWidget.h"
  66. #include "vtkResliceCursorActor.h"
  67. #include "vtkResliceCursorPolyDataAlgorithm.h"
  68. #include "vtkResliceCursor.h"
  69. #include "vtkDICOMImageReader.h"
  70. #include "vtkCellPicker.h"
  71. #include "vtkProperty.h"
  72. #include "vtkPlane.h"
  73. #include "vtkImageData.h"
  74. #include "vtkCommand.h"
  75. #include "vtkPlaneSource.h"
  76. #include "vtkLookupTable.h"
  77. #include "vtkImageMapToWindowLevelColors.h"
  78. #include "vtkInteractorStyleImage.h"
  79. #include "vtkImageSlabReslice.h"
  80. #include "vtkBoundedPlanePointPlacer.h"
  81. #include "vtkDistanceWidget.h"
  82. #include "vtkDistanceRepresentation.h"
  83. #include "vtkHandleRepresentation.h"
  84. #include "vtkResliceImageViewerMeasurements.h"
  85. #include "vtkDistanceRepresentation2D.h"
  86. #include "vtkPointHandleRepresentation3D.h"
  87. #include "vtkPointHandleRepresentation2D.h"
  88. #include <qdebug.h>
  89.  
  90. #include <vtkPolyDataMapper.h>
  91. #include <vtkObjectFactory.h>
  92. #include <vtkActor.h>
  93. #include <vtkSmartPointer.h>
  94. #include <vtkRenderWindow.h>
  95. #include <vtkRenderer.h>
  96. #include <vtkRenderWindowInteractor.h>
  97. #include <vtkPolyData.h>
  98. #include <vtkSphereSource.h>
  99. #include <vtkInteractorStyleTrackballCamera.h>
  100. #include "QVTKWidget.h"
  101. #include <QKeyEvent>
  102.  
  103. // Define interaction style
  104. class KeyPressInteractorStyle : public vtkInteractorStyle
  105. {
  106. public:
  107.     static KeyPressInteractorStyle* New();
  108.     vtkTypeMacro(KeyPressInteractorStyle, vtkInteractorStyle);
  109.  
  110.     virtual void OnKeyPress()
  111.     {
  112.         // Get the keypress
  113.         vtkRenderWindowInteractor *rwi = this->Interactor;
  114.         std::string key = rwi->GetKeySym();
  115.  
  116.         // Output the key that was pressed
  117.         std::cout << "Pressed " << key << std::endl;
  118.         qDebug()<< "Pressed " << rwi->GetKeySym();
  119.  
  120.         // Forward events
  121.         vtkInteractorStyle::OnKeyPress();
  122.     }
  123.  
  124.     virtual void OnChar()
  125.     {
  126.  
  127.         qDebug()<<"keeeey";
  128.     }
  129.  
  130. };
  131. vtkStandardNewMacro(KeyPressInteractorStyle);
  132.  
  133.  
  134. //----------------------------------------------------------------------------
  135. class vtkResliceCursorCallback : public vtkCommand
  136. {
  137. public:
  138.  
  139.     static vtkResliceCursorCallback *New()
  140.     {
  141.         return new vtkResliceCursorCallback; }
  142.  
  143.     void Execute( vtkObject *caller, unsigned long ev,
  144.         void *callData )
  145.     {
  146.  
  147.         if (ev == vtkCommand::KeyPressEvent)
  148.         {
  149.             QKeyEvent* keyPressed = static_cast<QKeyEvent*>(callData);
  150.            
  151.             qDebug()<<"key pressed: "<<keyPressed->text();
  152.         }
  153.  
  154.         if (ev == vtkResliceCursorWidget::WindowLevelEvent ||
  155.             ev == vtkCommand::WindowLevelEvent ||
  156.             ev == vtkResliceCursorWidget::ResliceThicknessChangedEvent||
  157.             ev == vtkResliceCursorWidget::ResliceAxesChangedEvent||
  158.             ev == vtkResliceCursorWidget::ResetCursorEvent)
  159.         {
  160.             //do nothing
  161.         }
  162.  
  163.         vtkImagePlaneWidget* ipw =
  164.             dynamic_cast< vtkImagePlaneWidget* >( caller );
  165.         if (ipw)
  166.         {
  167.             double* wl = static_cast<double*>( callData );
  168.  
  169.             if ( ipw == this->IPW[0] )
  170.             {
  171.  
  172.                 this->IPW[1]->SetWindowLevel(wl[0],wl[1],1);
  173.                 this->IPW[2]->SetWindowLevel(wl[0],wl[1],1);
  174.  
  175.             }
  176.             else if( ipw == this->IPW[1] )
  177.             {
  178.                 this->IPW[0]->SetWindowLevel(wl[0],wl[1],1);
  179.                 this->IPW[2]->SetWindowLevel(wl[0],wl[1],1);
  180.             }
  181.             else if (ipw == this->IPW[2])
  182.             {
  183.                 this->IPW[0]->SetWindowLevel(wl[0],wl[1],1);
  184.                 this->IPW[1]->SetWindowLevel(wl[0],wl[1],1);
  185.             }
  186.         }
  187.  
  188.         vtkResliceCursorWidget *rcw = dynamic_cast<
  189.             vtkResliceCursorWidget * >(caller);
  190.         if (rcw)
  191.         {
  192.             vtkResliceCursorLineRepresentation *rep = dynamic_cast<
  193.                 vtkResliceCursorLineRepresentation * >(rcw->GetRepresentation());
  194.             // Although the return value is not used, we keep the get calls
  195.             // in case they had side-effects
  196.             rep->GetResliceCursorActor()->GetCursorAlgorithm()->GetResliceCursor();
  197.             for (int i = 0; i < 3; i++)
  198.             {
  199.                 vtkPlaneSource *ps = static_cast< vtkPlaneSource * >(
  200.                     this->IPW[i]->GetPolyDataAlgorithm());
  201.                 ps->SetOrigin(this->RCW[i]->GetResliceCursorRepresentation()->
  202.                     GetPlaneSource()->GetOrigin());
  203.                 ps->SetPoint1(this->RCW[i]->GetResliceCursorRepresentation()->
  204.                     GetPlaneSource()->GetPoint1());
  205.                 ps->SetPoint2(this->RCW[i]->GetResliceCursorRepresentation()->
  206.                     GetPlaneSource()->GetPoint2());
  207.  
  208.                 // If the reslice plane has modified, update it on the 3D widget
  209.                 this->IPW[i]->UpdatePlacement();
  210.             }
  211.         }
  212.  
  213.         if(ev == vtkResliceImageViewer::SliceChangedEvent)
  214.         {
  215.             for (int i = 0; i < 3; i++)
  216.             {
  217.                 vtkPlaneSource *ps = static_cast< vtkPlaneSource * >(
  218.                     this->IPW[i]->GetPolyDataAlgorithm());
  219.                 ps->SetOrigin(this->RCW[i]->GetResliceCursorRepresentation()->
  220.                     GetPlaneSource()->GetOrigin());
  221.                 ps->SetPoint1(this->RCW[i]->GetResliceCursorRepresentation()->
  222.                     GetPlaneSource()->GetPoint1());
  223.                 ps->SetPoint2(this->RCW[i]->GetResliceCursorRepresentation()->
  224.                     GetPlaneSource()->GetPoint2());
  225.  
  226.                 // If the reslice plane has modified, update it on the 3D widget
  227.                 this->IPW[i]->UpdatePlacement();
  228.             }
  229.         }
  230.  
  231.         // Render everything
  232.         for (int i = 0; i < 3; i++)
  233.         {
  234.             this->RCW[i]->Render();
  235.         }
  236.         this->IPW[0]->GetInteractor()->GetRenderWindow()->Render();
  237.     }
  238.  
  239.     vtkResliceCursorCallback() {}
  240.     vtkImagePlaneWidget* IPW[3];
  241.     vtkResliceCursorWidget *RCW[3];
  242. };
  243.  
  244.  
  245. QtVTKRenderWindows::QtVTKRenderWindows( int vtkNotUsed(argc), char *argv[])
  246. {
  247.     this->ui = new Ui_QtVTKRenderWindows;
  248.     this->ui->setupUi(this);
  249.  
  250.     vtkSmartPointer< vtkDICOMImageReader > reader =
  251.         vtkSmartPointer< vtkDICOMImageReader >::New();
  252.     //reader->SetDirectoryName(argv[1]);
  253.     reader->SetDirectoryName("C:/Users/Panayiotis/DentalCEsse/qt_interface/build/Release/temp_dental/2/dicom");
  254.     reader->Update();
  255.     int imageDims[3];
  256.     reader->GetOutput()->GetDimensions(imageDims);
  257.  
  258.  
  259.     for (int i = 0; i < 3; i++)
  260.     {
  261.         riw[i] = vtkSmartPointer< vtkResliceImageViewer >::New();
  262.     }
  263.  
  264.     vtkSmartPointer<KeyPressInteractorStyle> style = vtkSmartPointer<KeyPressInteractorStyle>::New();
  265.  
  266.     this->ui->view1->GetInteractor()->SetInteractorStyle(style);
  267.     //this->ui->view1->GetRenderWindow()->GetInteractor()->SetInteractorStyle(style);
  268.     //this->ui->view1->GetRenderWindow()->AddRenderer(riw[0]->GetRenderer());
  269.     style->SetCurrentRenderer(riw[0]->GetRenderer());
  270.  
  271.     this->ui->view1->SetRenderWindow(riw[0]->GetRenderWindow());
  272.     riw[0]->SetupInteractor(
  273.         this->ui->view1->GetRenderWindow()->GetInteractor());
  274.    
  275.     this->ui->view2->SetRenderWindow(riw[1]->GetRenderWindow());
  276.     riw[1]->SetupInteractor(
  277.         this->ui->view2->GetRenderWindow()->GetInteractor());
  278.  
  279.     this->ui->view3->SetRenderWindow(riw[2]->GetRenderWindow());
  280.     riw[2]->SetupInteractor(
  281.         this->ui->view3->GetRenderWindow()->GetInteractor());
  282.  
  283.     for (int i = 0; i < 3; i++)
  284.     {
  285.         // make them all share the same reslice cursor object.
  286.         vtkResliceCursorLineRepresentation *rep =
  287.             vtkResliceCursorLineRepresentation::SafeDownCast(
  288.             riw[i]->GetResliceCursorWidget()->GetRepresentation());
  289.         riw[i]->SetResliceCursor(riw[0]->GetResliceCursor());
  290.  
  291.         rep->GetResliceCursorActor()->
  292.             GetCursorAlgorithm()->SetReslicePlaneNormal(i);
  293.  
  294.         riw[i]->SetInput(reader->GetOutput());
  295.         riw[i]->SetSliceOrientation(i);
  296.         riw[i]->SetResliceModeToAxisAligned();
  297.     }
  298.  
  299.     vtkSmartPointer<vtkCellPicker> picker =
  300.         vtkSmartPointer<vtkCellPicker>::New();
  301.     picker->SetTolerance(0.005);
  302.  
  303.     vtkSmartPointer<vtkProperty> ipwProp =
  304.         vtkSmartPointer<vtkProperty>::New();
  305.  
  306.     vtkSmartPointer< vtkRenderer > ren =
  307.         vtkSmartPointer< vtkRenderer >::New();
  308.  
  309.     this->ui->view4->GetRenderWindow()->AddRenderer(ren);
  310.     vtkRenderWindowInteractor *iren = this->ui->view4->GetInteractor();
  311.  
  312.     //commenting out the following code succesfully applies the
  313.     //'style' to view4
  314.     //iren->SetInteractorStyle(style);
  315.     //style->SetCurrentRenderer(ren);
  316.  
  317.     for (int i = 0; i < 3; i++)
  318.     {
  319.         planeWidget[i] = vtkSmartPointer<vtkImagePlaneWidget>::New();
  320.         planeWidget[i]->SetInteractor( iren );
  321.         planeWidget[i]->SetPicker(picker);
  322.         planeWidget[i]->RestrictPlaneToVolumeOn();
  323.         double color[3] = {0, 0, 0};
  324.         color[i] = 1;
  325.         planeWidget[i]->GetPlaneProperty()->SetColor(color);
  326.  
  327.         color[i] = 0.25;
  328.         riw[i]->GetRenderer()->SetBackground( color );
  329.  
  330.         planeWidget[i]->SetTexturePlaneProperty(ipwProp);
  331.         planeWidget[i]->TextureInterpolateOff();
  332.         planeWidget[i]->SetResliceInterpolateToCubic();
  333.         planeWidget[i]->SetInput(reader->GetOutput());
  334.         planeWidget[i]->SetPlaneOrientation(i);
  335.         planeWidget[i]->SetSliceIndex(imageDims[i]/2);
  336.         planeWidget[i]->DisplayTextOn();
  337.         planeWidget[i]->SetDefaultRenderer(ren);
  338.         planeWidget[i]->On();
  339.         planeWidget[i]->InteractionOn();
  340.         planeWidget[i]->SetTextureVisibility(0);
  341.     }
  342.  
  343.     vtkSmartPointer<vtkResliceCursorCallback> cbk =
  344.         vtkSmartPointer<vtkResliceCursorCallback>::New();
  345.  
  346.     for (int i = 0; i < 3; i++)
  347.     {
  348.         cbk->IPW[i] = planeWidget[i];
  349.         cbk->RCW[i] = riw[i]->GetResliceCursorWidget();
  350.         riw[i]->GetResliceCursorWidget()->AddObserver(
  351.             vtkResliceCursorWidget::ResliceAxesChangedEvent, cbk );
  352.  
  353.         riw[i]->GetResliceCursorWidget()->AddObserver(
  354.             vtkResliceCursorWidget::WindowLevelEvent, cbk );
  355.  
  356.         riw[i]->GetResliceCursorWidget()->AddObserver(
  357.             vtkResliceCursorWidget::ResliceThicknessChangedEvent, cbk );
  358.  
  359.         riw[i]->GetResliceCursorWidget()->AddObserver(
  360.             vtkResliceCursorWidget::ResetCursorEvent, cbk );
  361.  
  362.         riw[i]->GetInteractorStyle()->AddObserver(
  363.             vtkCommand::WindowLevelEvent, cbk );
  364.  
  365.         riw[i]->AddObserver(vtkResliceImageViewer::SliceChangedEvent,cbk);
  366.  
  367.         // Make them all share the same color map.
  368.         riw[i]->SetLookupTable(riw[0]->GetLookupTable());
  369.         planeWidget[i]->GetColorMap()->SetLookupTable(riw[0]->GetLookupTable());
  370.         planeWidget[i]->SetColorMap(riw[i]->GetResliceCursorWidget()->GetResliceCursorRepresentation()->GetColorMap());
  371.  
  372.     }
  373.  
  374.     //riw[0]->GetRenderWindow()->GetInteractor()->AddObserver(vtkCommand::KeyPressEvent,cbk);
  375.  
  376.     resliceMode(1);
  377.     this->ui->resliceModeCheckBox->setChecked(true);
  378.  
  379.     this->ui->view1->show();
  380.     this->ui->view2->show();
  381.     this->ui->view3->show();
  382.  
  383.     // Set up action signals and slots
  384.     this->ui->thickModeCheckBox->setEnabled(0);
  385.     this->ui->resliceModeCheckBox->setEnabled(0);
  386.  
  387.     this->ui->blendModeGroupBox->setEnabled(0);
  388.     this->ui->resetButton->setEnabled(0);
  389.     this->ui->AddDistance1Button->setEnabled(0);
  390. };
  391.  
  392. void QtVTKRenderWindows::resliceMode(int mode)
  393. {
  394.     for (int i = 0; i < 3; i++)
  395.     {
  396.         riw[i]->SetResliceMode(mode ? 1 : 0);
  397.         riw[i]->GetRenderer()->ResetCamera();
  398.         riw[i]->Render();
  399.     }
  400. }
  401.  
  402. void QtVTKRenderWindows::Render()
  403. {
  404.     for (int i = 0; i < 3; i++)
  405.     {
  406.         riw[i]->Render();
  407.     }
  408.     this->ui->view3->GetRenderWindow()->Render();
  409. }
  410.  
  411. //==================================
  412. //QtVTKRenderWindowsApp.cxx
  413. //==================================
  414.  
  415. #include <QApplication>
  416. #include "QtVTKRenderWindows.h"
  417.  
  418. int main( int argc, char** argv )
  419. {
  420.   // QT Stuff
  421.   QApplication app( argc, argv );
  422.  
  423.   QtVTKRenderWindows myQtVTKRenderWindows(argc, argv);
  424.   myQtVTKRenderWindows.show();
  425.  
  426.   return app.exec();
  427. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement