Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The original code is from VTK's Examples from here:
- // https://github.com/Kitware/VTK/tree/master/Examples/GUI/Qt/FourPaneViewer
- // This link also includes the necessary .ui file for Qt
- // and the cmake file.
- //
- // Note: In QtVTKRenderWindows.cxx you may want to change
- // the path to the Dicom directory to an appropriate location.
- //==========================
- //QtVTKRenderWindows.h
- //==========================
- #ifndef QtVTKRenderWindows_H
- #define QtVTKRenderWindows_H
- #include "vtkSmartPointer.h"
- #include "vtkResliceImageViewer.h"
- #include "vtkImagePlaneWidget.h"
- #include "vtkDistanceWidget.h"
- #include "vtkResliceImageViewerMeasurements.h"
- #include <QMainWindow>
- // Forward Qt class declarations
- class Ui_QtVTKRenderWindows;
- class QtVTKRenderWindows : public QMainWindow
- {
- Q_OBJECT
- public:
- // Constructor/Destructor
- QtVTKRenderWindows(int argc, char *argv[]);
- ~QtVTKRenderWindows() {};
- public slots:
- virtual void resliceMode(int);
- virtual void Render();
- protected:
- vtkSmartPointer< vtkResliceImageViewer > riw[3];
- vtkSmartPointer< vtkImagePlaneWidget > planeWidget[3];
- vtkSmartPointer< vtkDistanceWidget > DistanceWidget[3];
- vtkSmartPointer< vtkResliceImageViewerMeasurements > ResliceMeasurements;
- private:
- // Designer form
- Ui_QtVTKRenderWindows *ui;
- };
- #endif // QtVTKRenderWindows_H
- //============================
- //QtVTKRenderWindows.cxx
- //============================
- #include "ui_QtVTKRenderWindows.h"
- #include "QtVTKRenderWindows.h"
- #include <vtkRenderer.h>
- #include <vtkRenderWindow.h>
- #include "vtkResliceImageViewer.h"
- #include "vtkResliceCursorLineRepresentation.h"
- #include "vtkResliceCursorThickLineRepresentation.h"
- #include "vtkResliceCursorWidget.h"
- #include "vtkResliceCursorActor.h"
- #include "vtkResliceCursorPolyDataAlgorithm.h"
- #include "vtkResliceCursor.h"
- #include "vtkDICOMImageReader.h"
- #include "vtkCellPicker.h"
- #include "vtkProperty.h"
- #include "vtkPlane.h"
- #include "vtkImageData.h"
- #include "vtkCommand.h"
- #include "vtkPlaneSource.h"
- #include "vtkLookupTable.h"
- #include "vtkImageMapToWindowLevelColors.h"
- #include "vtkInteractorStyleImage.h"
- #include "vtkImageSlabReslice.h"
- #include "vtkBoundedPlanePointPlacer.h"
- #include "vtkDistanceWidget.h"
- #include "vtkDistanceRepresentation.h"
- #include "vtkHandleRepresentation.h"
- #include "vtkResliceImageViewerMeasurements.h"
- #include "vtkDistanceRepresentation2D.h"
- #include "vtkPointHandleRepresentation3D.h"
- #include "vtkPointHandleRepresentation2D.h"
- #include <qdebug.h>
- #include <vtkPolyDataMapper.h>
- #include <vtkObjectFactory.h>
- #include <vtkActor.h>
- #include <vtkSmartPointer.h>
- #include <vtkRenderWindow.h>
- #include <vtkRenderer.h>
- #include <vtkRenderWindowInteractor.h>
- #include <vtkPolyData.h>
- #include <vtkSphereSource.h>
- #include <vtkInteractorStyleTrackballCamera.h>
- #include "QVTKWidget.h"
- #include <QKeyEvent>
- //----------------------------------------------------------------------------
- // NEW: Define interactor
- class myRenderWindowInteractor : public QVTKInteractor //vtkRenderWindowInteractor
- {
- public:
- static myRenderWindowInteractor* New(); //return new KeyPressInteractorStyle;
- vtkTypeMacro(myRenderWindowInteractor, QVTKInteractor);
- virtual void OnChar(){qDebug()<<"OnChar() event stopped";};
- virtual void OnKeyDown(){qDebug()<<"OnKeyDown() event stopped";};
- virtual void OnKeyUp(){qDebug()<<"OnKeyUp() event stopped";};
- virtual void OnKeyPress(){qDebug()<<"OnKeyPress() event stopped";};
- virtual void OnKeyRelease(){qDebug()<<"OnKeyRelease() event stopped";};
- };
- vtkStandardNewMacro(myRenderWindowInteractor);
- // Define interaction style
- class KeyPressInteractorStyle : public vtkInteractorStyle
- {
- public:
- static KeyPressInteractorStyle* New();
- vtkTypeMacro(KeyPressInteractorStyle, vtkInteractorStyle);
- virtual void OnKeyPress()
- {
- // Get the keypress
- vtkRenderWindowInteractor *rwi = this->Interactor;
- std::string key = rwi->GetKeySym();
- // Output the key that was pressed
- std::cout << "Pressed " << key << std::endl;
- qDebug()<< "Pressed " << rwi->GetKeySym();
- // Forward events
- vtkInteractorStyle::OnKeyPress();
- }
- virtual void OnChar()
- {
- qDebug()<<"keeeey";
- }
- };
- vtkStandardNewMacro(KeyPressInteractorStyle);
- class MouseInteractorStyleDoubleClick : public vtkInteractorStyleTrackballCamera
- {
- public:
- static MouseInteractorStyleDoubleClick* New();
- vtkTypeMacro(MouseInteractorStyleDoubleClick, vtkInteractorStyleTrackballCamera);
- MouseInteractorStyleDoubleClick() : NumberOfClicks(0), ResetPixelDistance(5)
- {
- this->PreviousPosition[0] = 0;
- this->PreviousPosition[1] = 0;
- }
- virtual void OnLeftButtonDown()
- {
- //std::cout << "Pressed left mouse button." << std::endl;
- this->NumberOfClicks++;
- //std::cout << "NumberOfClicks = " << this->NumberOfClicks << std::endl;
- int pickPosition[2];
- this->GetInteractor()->GetEventPosition(pickPosition);
- int xdist = pickPosition[0] - this->PreviousPosition[0];
- int ydist = pickPosition[1] - this->PreviousPosition[1];
- this->PreviousPosition[0] = pickPosition[0];
- this->PreviousPosition[1] = pickPosition[1];
- int moveDistance = (int)sqrt((double)(xdist*xdist + ydist*ydist));
- // Reset numClicks - If mouse moved further than resetPixelDistance
- if(moveDistance > this->ResetPixelDistance)
- {
- this->NumberOfClicks = 1;
- }
- if(this->NumberOfClicks == 2)
- {
- std::cout << "Double clicked." << std::endl;
- this->NumberOfClicks = 0;
- }
- // forward events
- vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
- }
- private:
- unsigned int NumberOfClicks;
- int PreviousPosition[2];
- int ResetPixelDistance;
- };
- vtkStandardNewMacro(MouseInteractorStyleDoubleClick);
- //----------------------------------------------------------------------------
- class vtkResliceCursorCallback : public vtkCommand
- {
- public:
- static vtkResliceCursorCallback *New()
- {
- return new vtkResliceCursorCallback; }
- void Execute( vtkObject *caller, unsigned long ev,
- void *callData )
- {
- if (ev == vtkCommand::KeyPressEvent)
- {
- QKeyEvent* keyPressed = static_cast<QKeyEvent*>(callData);
- qDebug()<<"key pressed: "<<keyPressed->text();
- }
- if (ev == vtkResliceCursorWidget::WindowLevelEvent ||
- ev == vtkCommand::WindowLevelEvent ||
- ev == vtkResliceCursorWidget::ResliceThicknessChangedEvent||
- ev == vtkResliceCursorWidget::ResliceAxesChangedEvent||
- ev == vtkResliceCursorWidget::ResetCursorEvent)
- {
- //do nothing
- }
- vtkImagePlaneWidget* ipw =
- dynamic_cast< vtkImagePlaneWidget* >( caller );
- if (ipw)
- {
- double* wl = static_cast<double*>( callData );
- if ( ipw == this->IPW[0] )
- {
- this->IPW[1]->SetWindowLevel(wl[0],wl[1],1);
- this->IPW[2]->SetWindowLevel(wl[0],wl[1],1);
- }
- else if( ipw == this->IPW[1] )
- {
- this->IPW[0]->SetWindowLevel(wl[0],wl[1],1);
- this->IPW[2]->SetWindowLevel(wl[0],wl[1],1);
- }
- else if (ipw == this->IPW[2])
- {
- this->IPW[0]->SetWindowLevel(wl[0],wl[1],1);
- this->IPW[1]->SetWindowLevel(wl[0],wl[1],1);
- }
- }
- vtkResliceCursorWidget *rcw = dynamic_cast<
- vtkResliceCursorWidget * >(caller);
- if (rcw)
- {
- vtkResliceCursorLineRepresentation *rep = dynamic_cast<
- vtkResliceCursorLineRepresentation * >(rcw->GetRepresentation());
- // Although the return value is not used, we keep the get calls
- // in case they had side-effects
- rep->GetResliceCursorActor()->GetCursorAlgorithm()->GetResliceCursor();
- for (int i = 0; i < 3; i++)
- {
- vtkPlaneSource *ps = static_cast< vtkPlaneSource * >(
- this->IPW[i]->GetPolyDataAlgorithm());
- ps->SetOrigin(this->RCW[i]->GetResliceCursorRepresentation()->
- GetPlaneSource()->GetOrigin());
- ps->SetPoint1(this->RCW[i]->GetResliceCursorRepresentation()->
- GetPlaneSource()->GetPoint1());
- ps->SetPoint2(this->RCW[i]->GetResliceCursorRepresentation()->
- GetPlaneSource()->GetPoint2());
- // If the reslice plane has modified, update it on the 3D widget
- this->IPW[i]->UpdatePlacement();
- }
- }
- if(ev == vtkResliceImageViewer::SliceChangedEvent)
- {
- for (int i = 0; i < 3; i++)
- {
- vtkPlaneSource *ps = static_cast< vtkPlaneSource * >(
- this->IPW[i]->GetPolyDataAlgorithm());
- ps->SetOrigin(this->RCW[i]->GetResliceCursorRepresentation()->
- GetPlaneSource()->GetOrigin());
- ps->SetPoint1(this->RCW[i]->GetResliceCursorRepresentation()->
- GetPlaneSource()->GetPoint1());
- ps->SetPoint2(this->RCW[i]->GetResliceCursorRepresentation()->
- GetPlaneSource()->GetPoint2());
- // If the reslice plane has modified, update it on the 3D widget
- this->IPW[i]->UpdatePlacement();
- }
- }
- // Render everything
- for (int i = 0; i < 3; i++)
- {
- this->RCW[i]->Render();
- }
- this->IPW[0]->GetInteractor()->GetRenderWindow()->Render();
- }
- vtkResliceCursorCallback() {}
- vtkImagePlaneWidget* IPW[3];
- vtkResliceCursorWidget *RCW[3];
- };
- QtVTKRenderWindows::QtVTKRenderWindows( int vtkNotUsed(argc), char *argv[])
- {
- this->ui = new Ui_QtVTKRenderWindows;
- this->ui->setupUi(this);
- vtkSmartPointer< vtkDICOMImageReader > reader =
- vtkSmartPointer< vtkDICOMImageReader >::New();
- //reader->SetDirectoryName(argv[1]);
- reader->SetDirectoryName("C:/Users/Panayiotis/DentalCEsse/qt_interface/build/Release/temp_dental/2/dicom");
- reader->Update();
- int imageDims[3];
- reader->GetOutput()->GetDimensions(imageDims);
- for (int i = 0; i < 3; i++)
- {
- riw[i] = vtkSmartPointer< vtkResliceImageViewer >::New();
- }
- vtkSmartPointer<myRenderWindowInteractor> myInteractor1 = vtkSmartPointer<myRenderWindowInteractor>::New();
- vtkSmartPointer<myRenderWindowInteractor> myInteractor2 = vtkSmartPointer<myRenderWindowInteractor>::New();
- vtkSmartPointer<myRenderWindowInteractor> myInteractor3 = vtkSmartPointer<myRenderWindowInteractor>::New();
- this->ui->view1->SetRenderWindow(riw[0]->GetRenderWindow());
- this->ui->view2->SetRenderWindow(riw[1]->GetRenderWindow());
- this->ui->view3->SetRenderWindow(riw[2]->GetRenderWindow());
- riw[2]->SetupInteractor(this->ui->view3->GetRenderWindow()->GetInteractor());
- riw[1]->SetupInteractor(this->ui->view2->GetRenderWindow()->GetInteractor());
- riw[0]->SetupInteractor(this->ui->view1->GetRenderWindow()->GetInteractor());
- myInteractor1->SetRenderWindow(this->ui->view1->GetRenderWindow());
- myInteractor2->SetRenderWindow(this->ui->view2->GetRenderWindow());
- myInteractor3->SetRenderWindow(this->ui->view3->GetRenderWindow());
- for (int i = 0; i < 3; i++)
- {
- // make them all share the same reslice cursor object.
- vtkResliceCursorLineRepresentation *rep =
- vtkResliceCursorLineRepresentation::SafeDownCast(
- riw[i]->GetResliceCursorWidget()->GetRepresentation());
- riw[i]->SetResliceCursor(riw[0]->GetResliceCursor());
- rep->GetResliceCursorActor()->
- GetCursorAlgorithm()->SetReslicePlaneNormal(i);
- riw[i]->SetInput(reader->GetOutput());
- riw[i]->SetSliceOrientation(i);
- riw[i]->SetResliceModeToAxisAligned();
- }
- vtkSmartPointer<vtkCellPicker> picker =
- vtkSmartPointer<vtkCellPicker>::New();
- picker->SetTolerance(0.005);
- vtkSmartPointer<vtkProperty> ipwProp =
- vtkSmartPointer<vtkProperty>::New();
- vtkSmartPointer< vtkRenderer > ren =
- vtkSmartPointer< vtkRenderer >::New();
- this->ui->view4->GetRenderWindow()->AddRenderer(ren);
- vtkRenderWindowInteractor *iren = this->ui->view4->GetInteractor();
- //commenting out the following code succesfully applies the
- //'style' to view4
- //iren->SetInteractorStyle(style);
- //style->SetCurrentRenderer(ren);
- //vtkSmartPointer<MouseInteractorStyleDoubleClick> style2 = vtkSmartPointer<MouseInteractorStyleDoubleClick>::New();
- //this->ui->view4->GetRenderWindow()->GetInteractor()->SetInteractorStyle( style2 );
- for (int i = 0; i < 3; i++)
- {
- planeWidget[i] = vtkSmartPointer<vtkImagePlaneWidget>::New();
- planeWidget[i]->SetInteractor( iren );
- planeWidget[i]->SetPicker(picker);
- planeWidget[i]->RestrictPlaneToVolumeOn();
- double color[3] = {0, 0, 0};
- color[i] = 1;
- planeWidget[i]->GetPlaneProperty()->SetColor(color);
- color[i] = 0.25;
- riw[i]->GetRenderer()->SetBackground( color );
- planeWidget[i]->SetTexturePlaneProperty(ipwProp);
- planeWidget[i]->TextureInterpolateOff();
- planeWidget[i]->SetResliceInterpolateToCubic();
- planeWidget[i]->SetInput(reader->GetOutput());
- planeWidget[i]->SetPlaneOrientation(i);
- planeWidget[i]->SetSliceIndex(imageDims[i]/2);
- planeWidget[i]->DisplayTextOn();
- planeWidget[i]->SetDefaultRenderer(ren);
- planeWidget[i]->On();
- planeWidget[i]->InteractionOn();
- planeWidget[i]->SetTextureVisibility(0);
- }
- vtkSmartPointer<vtkResliceCursorCallback> cbk =
- vtkSmartPointer<vtkResliceCursorCallback>::New();
- for (int i = 0; i < 3; i++)
- {
- cbk->IPW[i] = planeWidget[i];
- cbk->RCW[i] = riw[i]->GetResliceCursorWidget();
- riw[i]->GetResliceCursorWidget()->AddObserver(
- vtkResliceCursorWidget::ResliceAxesChangedEvent, cbk );
- riw[i]->GetResliceCursorWidget()->AddObserver(
- vtkResliceCursorWidget::WindowLevelEvent, cbk );
- riw[i]->GetResliceCursorWidget()->AddObserver(
- vtkResliceCursorWidget::ResliceThicknessChangedEvent, cbk );
- riw[i]->GetResliceCursorWidget()->AddObserver(
- vtkResliceCursorWidget::ResetCursorEvent, cbk );
- riw[i]->GetInteractorStyle()->AddObserver(
- vtkCommand::WindowLevelEvent, cbk );
- riw[i]->AddObserver(vtkResliceImageViewer::SliceChangedEvent,cbk);
- // Make them all share the same color map.
- riw[i]->SetLookupTable(riw[0]->GetLookupTable());
- planeWidget[i]->GetColorMap()->SetLookupTable(riw[0]->GetLookupTable());
- planeWidget[i]->SetColorMap(riw[i]->GetResliceCursorWidget()->GetResliceCursorRepresentation()->GetColorMap());
- }
- //riw[0]->GetRenderWindow()->GetInteractor()->AddObserver(vtkCommand::KeyPressEvent,cbk);
- resliceMode(1);
- this->ui->resliceModeCheckBox->setChecked(true);
- vtkSmartPointer<KeyPressInteractorStyle> style1 = vtkSmartPointer<KeyPressInteractorStyle>::New();
- vtkSmartPointer<KeyPressInteractorStyle> style2 = vtkSmartPointer<KeyPressInteractorStyle>::New();
- vtkSmartPointer<KeyPressInteractorStyle> style3 = vtkSmartPointer<KeyPressInteractorStyle>::New();
- vtkSmartPointer<KeyPressInteractorStyle> style4 = vtkSmartPointer<KeyPressInteractorStyle>::New();
- this->ui->view1->GetRenderWindow()->GetInteractor()->SetInteractorStyle(style1);
- this->ui->view2->GetRenderWindow()->GetInteractor()->SetInteractorStyle(style2);
- this->ui->view3->GetRenderWindow()->GetInteractor()->SetInteractorStyle(style3);
- //this->ui->view4->GetRenderWindow()->GetInteractor()->SetInteractorStyle(style4); //don't need it on 4th view
- this->ui->view1->show();
- this->ui->view2->show();
- this->ui->view3->show();
- // Set up action signals and slots
- this->ui->thickModeCheckBox->setEnabled(0);
- this->ui->resliceModeCheckBox->setEnabled(0);
- this->ui->blendModeGroupBox->setEnabled(0);
- this->ui->resetButton->setEnabled(0);
- this->ui->AddDistance1Button->setEnabled(0);
- };
- void QtVTKRenderWindows::resliceMode(int mode)
- {
- for (int i = 0; i < 3; i++)
- {
- riw[i]->SetResliceMode(mode ? 1 : 0);
- riw[i]->GetRenderer()->ResetCamera();
- riw[i]->Render();
- }
- }
- void QtVTKRenderWindows::Render()
- {
- for (int i = 0; i < 3; i++)
- {
- riw[i]->Render();
- }
- this->ui->view3->GetRenderWindow()->Render();
- }
- //==================================
- //QtVTKRenderWindowsApp.cxx
- //==================================
- #include <QApplication>
- #include "QtVTKRenderWindows.h"
- int main( int argc, char** argv )
- {
- // QT Stuff
- QApplication app( argc, argv );
- QtVTKRenderWindows myQtVTKRenderWindows(argc, argv);
- myQtVTKRenderWindows.show();
- return app.exec();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement