Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QVTKOpenGLNativeWidget.h>
- #include <vtkActor.h>
- #include <vtkDataSetMapper.h>
- #include <vtkDoubleArray.h>
- #include <vtkGenericOpenGLRenderWindow.h>
- #include <vtkPointData.h>
- #include <vtkProperty.h>
- #include <vtkRenderer.h>
- #include <vtkSphereSource.h>
- #include <vtkVolume.h>
- #include <vtkVolumeProperty.h>
- #include <vtkSmartVolumeMapper.h>
- #include <vtkOpenGLGPUVolumeRayCastMapper.h>
- #include <vtkImageReslice.h>
- #include <vtkImageMapper3D.h>
- #include <vtkImageActor.h>
- #include <vtkLookupTable.h>
- #include <vtkImageMapToColors.h>
- #include <QApplication>
- #include <QDockWidget>
- #include <QGridLayout>
- #include <QLabel>
- #include <QMainWindow>
- #include <QPointer>
- #include <QPushButton>
- #include <QVBoxLayout>
- #include <QDebug>
- #include <QSlider>
- #include <QRadioButton>
- #include <cmath>
- #include <cstdlib>
- #include <random>
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <vtkImageData.h>
- #include <vtkSmartPointer.h>
- #include <vtkActor.h>
- #include <vtkRenderer.h>
- #include <vtkNamedColors.h>
- #include <vtkColorTransferFunction.h>
- #include <vtkPiecewiseFunction.h>
- using namespace std;
- int main(int argc, char* argv[])
- {
- QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());
- QApplication app(argc, argv);
- QMainWindow mainWindow;
- mainWindow.resize(1200, 900);
- QGridLayout* gridLayout = new QGridLayout();
- QWidget* centralWidget = new QWidget(&mainWindow);
- centralWidget->setLayout(gridLayout);
- mainWindow.setCentralWidget(centralWidget);
- QDockWidget controlDock;
- mainWindow.addDockWidget(Qt::LeftDockWidgetArea, &controlDock);
- QLabel controlDockTitle("");
- controlDock.setTitleBarWidget(&controlDockTitle);
- QPointer<QVBoxLayout> dockLayout = new QVBoxLayout();
- QWidget layoutContainer;
- layoutContainer.setLayout(dockLayout);
- controlDock.setWidget(&layoutContainer);
- QPointer<QVTKOpenGLNativeWidget> ctRenderWidget = new QVTKOpenGLNativeWidget();
- QPointer<QVTKOpenGLNativeWidget> mrRenderWidget = new QVTKOpenGLNativeWidget();
- QPointer<QVTKOpenGLNativeWidget> ctMrRenderWidget = new QVTKOpenGLNativeWidget();
- vtkNew<vtkGenericOpenGLRenderWindow> ctRenderWindow;
- vtkNew<vtkGenericOpenGLRenderWindow> mrRenderWindow;
- vtkNew<vtkGenericOpenGLRenderWindow> ctMrRenderWindow;
- ctRenderWidget->setRenderWindow(ctRenderWindow.Get());
- mrRenderWidget->setRenderWindow(mrRenderWindow.Get());
- ctMrRenderWidget->setRenderWindow(ctMrRenderWindow.Get());
- QSlider slider(Qt::Vertical);
- dockLayout->addWidget(&slider);
- slider.setMinimum(0);
- slider.setMaximum(141);
- slider.setValue(0);
- // File headers
- string CTinput = "D:/adobiiii/mr-ct/CT.rdata"; ///////////////change later
- string MRinput = "D:/adobiiii/mr-ct/MR.rdata"; ///////////////change later
- ifstream MRstream(MRinput + ".header");
- ifstream CTstream(CTinput + ".header");
- string line;
- std::ifstream MRfin(MRinput, ios::in | ios::binary | ios::ate);
- std::ifstream CTfin(CTinput, ios::in | ios::binary | ios::ate);
- //size CT
- int CTsize = CTfin.tellg();
- cout << CTsize;
- CTfin.seekg(0, ios::beg);
- char *CTbuffer = new char[CTsize];
- CTfin.read(CTbuffer, CTsize);
- CTfin.close();
- //size MR
- int MRsize = MRfin.tellg();
- cout << MRsize;
- MRfin.seekg(0, ios::beg);
- char *MRbuffer = new char[MRsize];
- MRfin.read(MRbuffer, MRsize);
- MRfin.close();
- //Setup allocator for input file MR
- vtkSmartPointer<vtkImageData> MRimageData = vtkSmartPointer<vtkImageData>::New();
- MRimageData->SetDimensions(512, 512, 192);
- MRimageData->SetSpacing(0.351562, 0.351562, 0.7);
- MRimageData->SetOrigin(0, 0, 0);
- MRimageData->AllocateScalars(VTK_UNSIGNED_SHORT, 1);
- //Setup allocator for input file CT
- vtkSmartPointer<vtkImageData> CTimageData = vtkSmartPointer<vtkImageData>::New();
- CTimageData->SetDimensions(512, 512, 247);
- CTimageData->SetSpacing(0.447266, 0.447266, 0.625);
- CTimageData->SetOrigin(0, 0, 0);
- CTimageData->AllocateScalars(VTK_UNSIGNED_SHORT, 1);
- //Copy image data MR
- unsigned char* MRimageDataPointer = static_cast<unsigned char*>(MRimageData->GetScalarPointer());
- std::memcpy(MRimageDataPointer, MRbuffer, MRsize);
- //Copy image data CT
- unsigned char* CTimageDataPointer = static_cast<unsigned char*>(CTimageData->GetScalarPointer());
- std::memcpy(CTimageDataPointer, CTbuffer, CTsize);
- //Mapping data MR
- vtkNew<vtkOpenGLGPUVolumeRayCastMapper> MRvolumeMapper;
- MRvolumeMapper->SetInputData(MRimageData);
- //Mapping data CT
- vtkNew<vtkOpenGLGPUVolumeRayCastMapper> CTvolumeMapper;
- CTvolumeMapper->SetInputData(CTimageData);
- //Volumes properties MR
- vtkNew<vtkVolumeProperty> MRvolumeProperty;
- MRvolumeProperty->SetInterpolationTypeToLinear();
- vtkNew<vtkVolume> MRvolume;
- MRvolume->SetMapper(MRvolumeMapper);
- MRvolume->SetProperty(MRvolumeProperty);
- vtkNew<vtkImageReslice> MRreslice;
- MRreslice->SetInputData(MRimageData);
- MRreslice->SetOutputDimensionality(2);
- MRreslice->SetResliceAxesDirectionCosines(1, 0, 0, 0, 1, 0, 0, 0, 1);
- MRreslice->SetInterpolationModeToLinear();
- MRreslice->SetResliceAxesOrigin(0, 0, 1);
- //Volumes properties CT
- vtkNew<vtkVolumeProperty> CTvolumeProperty;
- CTvolumeProperty->SetInterpolationTypeToLinear();
- vtkNew<vtkVolume> CTvolume;
- CTvolume->SetMapper(CTvolumeMapper);
- CTvolume->SetProperty(CTvolumeProperty);
- vtkNew<vtkImageReslice> CTreslice;
- CTreslice->SetInputData(CTimageData);
- CTreslice->SetOutputDimensionality(2);
- CTreslice->SetResliceAxesDirectionCosines(1, 0, 0, 0, 1, 0, 0, 0, 1);
- CTreslice->SetInterpolationModeToLinear();
- CTreslice->SetResliceAxesOrigin(0, 0, 1);
- // Create a vtkLookupTable for color mapping MR
- vtkNew<vtkLookupTable> MRcolorTable;
- MRcolorTable->SetTableRange(0, 256); // Zakres danych medycznych
- MRcolorTable->SetHueRange(0.7, 0.7);
- MRcolorTable->SetSaturationRange(1.0, 1.0); // Nasycenie kolorów
- MRcolorTable->SetValueRange(0.2, 1.0); // Jasność kolorów
- MRcolorTable->SetAlphaRange(1.0, 1.0); // Kolor nie jest przezroczysty
- MRcolorTable->SetNumberOfColors(256); // Liczba kolorów w mapie (dostosuj według potrzeb)
- // Tworzenie mapy kolorów
- for (int i = 0; i < 256; i++) {
- double val = static_cast<double>(i) / 255.0;
- MRcolorTable->SetTableValue(i, val, val, val * 0.8, 1.0); // Ustaw niebieski kolor
- }
- // Create a vtkLookupTable for color mapping CT
- vtkNew<vtkLookupTable> CTcolorTable;
- CTcolorTable->SetTableRange(0, 10000); // Zakres danych medycznych
- CTcolorTable->SetHueRange(0.0, 0.0);
- CTcolorTable->SetSaturationRange(0.0, 0.0); // Nasycenie kolorów
- CTcolorTable->SetValueRange(0.0, 1.0); // Jasność kolorów
- CTcolorTable->SetAlphaRange(1.0, 1.0); // Kolor nie jest przezroczysty
- MRcolorTable->Build();
- CTcolorTable->Build();
- vtkNew<vtkImageMapToColors> MRcolorMapper;
- MRcolorMapper->SetInputConnection(MRreslice->GetOutputPort());
- MRcolorMapper->SetLookupTable(MRcolorTable.Get());
- vtkNew<vtkImageMapToColors> CTcolorMapper;
- CTcolorMapper->SetInputConnection(CTreslice->GetOutputPort());
- CTcolorMapper->SetLookupTable(CTcolorTable.Get());
- // Create a vtkImageActor and set it up to display the slice with colors
- vtkNew<vtkImageActor> MRimageActor;
- vtkNew<vtkImageActor> CTimageActor;
- vtkNew<vtkImageActor> MRCTimageActor;
- MRimageActor->GetMapper()->SetInputConnection(MRcolorMapper->GetOutputPort());
- CTimageActor->GetMapper()->SetInputConnection(CTcolorMapper->GetOutputPort());
- MRCTimageActor->GetMapper()->SetInputConnection(MRcolorMapper->GetOutputPort());
- vtkNew<vtkRenderer> MRrenderer;
- vtkNew<vtkRenderer> CTrenderer;
- vtkNew<vtkRenderer> MRCTrenderer;
- mrRenderWindow->AddRenderer(MRrenderer);
- MRrenderer->AddActor(MRimageActor);
- ctRenderWindow->AddRenderer(CTrenderer);
- CTrenderer->AddActor(CTimageActor);
- // Dodaj widżety do siatki w głównym oknie
- gridLayout->addWidget(ctRenderWidget, 0, 0);
- gridLayout->addWidget(mrRenderWidget, 0, 1);
- gridLayout->addWidget(ctMrRenderWidget, 0, 2);
- mainWindow.show();
- MRrenderer->Render();
- CTrenderer->Render();
- QObject::connect(&slider, &QSlider::valueChanged, [&](int value) {
- MRreslice->SetResliceAxesOrigin(0, 0, value);
- CTreslice->SetResliceAxesOrigin(0, 0, value);
- MRrenderer->GetRenderWindow()->Render();
- CTrenderer->GetRenderWindow()->Render();
- });
- return app.exec();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement