Advertisement
DarkArbiter

Untitled

Sep 18th, 2023
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.39 KB | None | 0 0
  1. #include <QVTKOpenGLNativeWidget.h>
  2. #include <vtkActor.h>
  3. #include <vtkDataSetMapper.h>
  4. #include <vtkDoubleArray.h>
  5. #include <vtkGenericOpenGLRenderWindow.h>
  6. #include <vtkPointData.h>
  7. #include <vtkProperty.h>
  8. #include <vtkRenderer.h>
  9. #include <vtkSphereSource.h>
  10. #include <vtkVolume.h>
  11. #include <vtkVolumeProperty.h>
  12. #include <vtkSmartVolumeMapper.h>
  13. #include <vtkOpenGLGPUVolumeRayCastMapper.h>
  14. #include <vtkImageReslice.h>
  15. #include <vtkImageMapper3D.h>
  16. #include <vtkImageActor.h>
  17. #include <vtkLookupTable.h>
  18. #include <vtkImageMapToColors.h>
  19.  
  20. #include <QApplication>
  21. #include <QDockWidget>
  22. #include <QGridLayout>
  23. #include <QLabel>
  24. #include <QMainWindow>
  25. #include <QPointer>
  26. #include <QPushButton>
  27. #include <QVBoxLayout>
  28. #include <QDebug>
  29. #include <QSlider>
  30. #include <QRadioButton>
  31.  
  32. #include <cmath>
  33. #include <cstdlib>
  34. #include <random>
  35.  
  36. #include <iostream>
  37. #include <fstream>
  38. #include <sstream>
  39.  
  40. #include <vtkImageData.h>
  41. #include <vtkSmartPointer.h>
  42. #include <vtkActor.h>
  43. #include <vtkRenderer.h>
  44. #include <vtkNamedColors.h>
  45. #include <vtkColorTransferFunction.h>
  46. #include <vtkPiecewiseFunction.h>
  47.  
  48. using namespace std;
  49.  
  50.  
  51. int main(int argc, char* argv[])
  52. {
  53.     QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());
  54.     QApplication app(argc, argv);
  55.  
  56.     QMainWindow mainWindow;
  57.     mainWindow.resize(1200, 900);
  58.  
  59.     QGridLayout* gridLayout = new QGridLayout();
  60.     QWidget* centralWidget = new QWidget(&mainWindow);
  61.     centralWidget->setLayout(gridLayout);
  62.     mainWindow.setCentralWidget(centralWidget);
  63.  
  64.     QDockWidget controlDock;
  65.     mainWindow.addDockWidget(Qt::LeftDockWidgetArea, &controlDock);
  66.  
  67.     QLabel controlDockTitle("");
  68.     controlDock.setTitleBarWidget(&controlDockTitle);
  69.  
  70.     QPointer<QVBoxLayout> dockLayout = new QVBoxLayout();
  71.     QWidget layoutContainer;
  72.     layoutContainer.setLayout(dockLayout);
  73.     controlDock.setWidget(&layoutContainer);
  74.  
  75.     QPointer<QVTKOpenGLNativeWidget> ctRenderWidget = new QVTKOpenGLNativeWidget();
  76.     QPointer<QVTKOpenGLNativeWidget> mrRenderWidget = new QVTKOpenGLNativeWidget();
  77.     QPointer<QVTKOpenGLNativeWidget> ctMrRenderWidget = new QVTKOpenGLNativeWidget();
  78.  
  79.     vtkNew<vtkGenericOpenGLRenderWindow> ctRenderWindow;
  80.     vtkNew<vtkGenericOpenGLRenderWindow> mrRenderWindow;
  81.     vtkNew<vtkGenericOpenGLRenderWindow> ctMrRenderWindow;
  82.     ctRenderWidget->setRenderWindow(ctRenderWindow.Get());
  83.     mrRenderWidget->setRenderWindow(mrRenderWindow.Get());
  84.     ctMrRenderWidget->setRenderWindow(ctMrRenderWindow.Get());
  85.  
  86.     QSlider slider(Qt::Vertical);
  87.     dockLayout->addWidget(&slider);
  88.  
  89.     slider.setMinimum(0);
  90.     slider.setMaximum(141);
  91.     slider.setValue(0);
  92.  
  93.     // File headers
  94.     string CTinput = "D:/adobiiii/mr-ct/CT.rdata"; ///////////////change later
  95.     string MRinput = "D:/adobiiii/mr-ct/MR.rdata"; ///////////////change later
  96.     ifstream MRstream(MRinput + ".header");
  97.     ifstream CTstream(CTinput + ".header");
  98.     string line;
  99.  
  100.     std::ifstream MRfin(MRinput, ios::in | ios::binary | ios::ate);
  101.     std::ifstream CTfin(CTinput, ios::in | ios::binary | ios::ate);
  102.  
  103.     //size CT
  104.     int CTsize = CTfin.tellg();
  105.     cout << CTsize;
  106.     CTfin.seekg(0, ios::beg);
  107.  
  108.     char *CTbuffer = new char[CTsize];
  109.     CTfin.read(CTbuffer, CTsize);
  110.     CTfin.close();
  111.      
  112.     //size MR
  113.     int MRsize = MRfin.tellg();
  114.     cout << MRsize;
  115.     MRfin.seekg(0, ios::beg);
  116.  
  117.     char *MRbuffer = new char[MRsize];
  118.     MRfin.read(MRbuffer, MRsize);
  119.     MRfin.close();
  120.  
  121.  
  122.     //Setup allocator for input file MR
  123.     vtkSmartPointer<vtkImageData> MRimageData = vtkSmartPointer<vtkImageData>::New();
  124.     MRimageData->SetDimensions(512, 512, 192);
  125.     MRimageData->SetSpacing(0.351562, 0.351562, 0.7);
  126.     MRimageData->SetOrigin(0, 0, 0);
  127.     MRimageData->AllocateScalars(VTK_UNSIGNED_SHORT, 1);
  128.  
  129.     //Setup allocator for input file CT
  130.     vtkSmartPointer<vtkImageData> CTimageData = vtkSmartPointer<vtkImageData>::New();
  131.     CTimageData->SetDimensions(512, 512, 247);
  132.     CTimageData->SetSpacing(0.447266, 0.447266, 0.625);
  133.     CTimageData->SetOrigin(0, 0, 0);
  134.     CTimageData->AllocateScalars(VTK_UNSIGNED_SHORT, 1);
  135.  
  136.     //Copy image data MR
  137.     unsigned char* MRimageDataPointer = static_cast<unsigned char*>(MRimageData->GetScalarPointer());
  138.     std::memcpy(MRimageDataPointer, MRbuffer, MRsize);
  139.     //Copy image data CT
  140.     unsigned char* CTimageDataPointer = static_cast<unsigned char*>(CTimageData->GetScalarPointer());
  141.     std::memcpy(CTimageDataPointer, CTbuffer, CTsize);
  142.  
  143.     //Mapping data MR
  144.     vtkNew<vtkOpenGLGPUVolumeRayCastMapper> MRvolumeMapper;
  145.     MRvolumeMapper->SetInputData(MRimageData);
  146.  
  147.     //Mapping data CT
  148.     vtkNew<vtkOpenGLGPUVolumeRayCastMapper> CTvolumeMapper;
  149.     CTvolumeMapper->SetInputData(CTimageData);
  150.  
  151.     //Volumes properties MR
  152.     vtkNew<vtkVolumeProperty> MRvolumeProperty;
  153.     MRvolumeProperty->SetInterpolationTypeToLinear();
  154.  
  155.     vtkNew<vtkVolume> MRvolume;
  156.     MRvolume->SetMapper(MRvolumeMapper);
  157.     MRvolume->SetProperty(MRvolumeProperty);
  158.  
  159.     vtkNew<vtkImageReslice> MRreslice;
  160.     MRreslice->SetInputData(MRimageData);
  161.     MRreslice->SetOutputDimensionality(2);
  162.     MRreslice->SetResliceAxesDirectionCosines(1, 0, 0, 0, 1, 0, 0, 0, 1);
  163.     MRreslice->SetInterpolationModeToLinear();
  164.  
  165.     MRreslice->SetResliceAxesOrigin(0, 0, 1);
  166.  
  167.     //Volumes properties CT
  168.     vtkNew<vtkVolumeProperty> CTvolumeProperty;
  169.     CTvolumeProperty->SetInterpolationTypeToLinear();
  170.  
  171.     vtkNew<vtkVolume> CTvolume;
  172.     CTvolume->SetMapper(CTvolumeMapper);
  173.     CTvolume->SetProperty(CTvolumeProperty);
  174.  
  175.     vtkNew<vtkImageReslice> CTreslice;
  176.     CTreslice->SetInputData(CTimageData);
  177.     CTreslice->SetOutputDimensionality(2);
  178.     CTreslice->SetResliceAxesDirectionCosines(1, 0, 0, 0, 1, 0, 0, 0, 1);
  179.     CTreslice->SetInterpolationModeToLinear();
  180.  
  181.     CTreslice->SetResliceAxesOrigin(0, 0, 1);
  182.  
  183.     // Create a vtkLookupTable for color mapping MR
  184.     vtkNew<vtkLookupTable> MRcolorTable;
  185.     MRcolorTable->SetTableRange(0, 256); // Zakres danych medycznych
  186.     MRcolorTable->SetHueRange(0.7, 0.7);
  187.     MRcolorTable->SetSaturationRange(1.0, 1.0); // Nasycenie kolorów
  188.     MRcolorTable->SetValueRange(0.2, 1.0); // Jasność kolorów
  189.     MRcolorTable->SetAlphaRange(1.0, 1.0); // Kolor nie jest przezroczysty
  190.     MRcolorTable->SetNumberOfColors(256); // Liczba kolorów w mapie (dostosuj według potrzeb)
  191.     // Tworzenie mapy kolorów
  192.     for (int i = 0; i < 256; i++) {
  193.         double val = static_cast<double>(i) / 255.0;
  194.         MRcolorTable->SetTableValue(i, val, val, val * 0.8, 1.0); // Ustaw niebieski kolor
  195.     }
  196.  
  197.     // Create a vtkLookupTable for color mapping CT
  198.     vtkNew<vtkLookupTable> CTcolorTable;
  199.     CTcolorTable->SetTableRange(0, 10000); // Zakres danych medycznych
  200.     CTcolorTable->SetHueRange(0.0, 0.0);
  201.     CTcolorTable->SetSaturationRange(0.0, 0.0); // Nasycenie kolorów
  202.     CTcolorTable->SetValueRange(0.0, 1.0); // Jasność kolorów
  203.     CTcolorTable->SetAlphaRange(1.0, 1.0); // Kolor nie jest przezroczysty
  204.    
  205.     MRcolorTable->Build();
  206.     CTcolorTable->Build();
  207.  
  208.     vtkNew<vtkImageMapToColors> MRcolorMapper;
  209.     MRcolorMapper->SetInputConnection(MRreslice->GetOutputPort());
  210.     MRcolorMapper->SetLookupTable(MRcolorTable.Get());
  211.  
  212.     vtkNew<vtkImageMapToColors> CTcolorMapper;
  213.     CTcolorMapper->SetInputConnection(CTreslice->GetOutputPort());
  214.     CTcolorMapper->SetLookupTable(CTcolorTable.Get());
  215.  
  216.     // Create a vtkImageActor and set it up to display the slice with colors
  217.     vtkNew<vtkImageActor> MRimageActor;
  218.     vtkNew<vtkImageActor> CTimageActor;
  219.     vtkNew<vtkImageActor> MRCTimageActor;
  220.     MRimageActor->GetMapper()->SetInputConnection(MRcolorMapper->GetOutputPort());
  221.     CTimageActor->GetMapper()->SetInputConnection(CTcolorMapper->GetOutputPort());
  222.     MRCTimageActor->GetMapper()->SetInputConnection(MRcolorMapper->GetOutputPort());
  223.  
  224.     vtkNew<vtkRenderer> MRrenderer;
  225.     vtkNew<vtkRenderer> CTrenderer;
  226.     vtkNew<vtkRenderer> MRCTrenderer;
  227.     mrRenderWindow->AddRenderer(MRrenderer);
  228.     MRrenderer->AddActor(MRimageActor);
  229.     ctRenderWindow->AddRenderer(CTrenderer);
  230.     CTrenderer->AddActor(CTimageActor);
  231.  
  232.     // Dodaj widżety do siatki w głównym oknie
  233.     gridLayout->addWidget(ctRenderWidget, 0, 0);
  234.     gridLayout->addWidget(mrRenderWidget, 0, 1);
  235.     gridLayout->addWidget(ctMrRenderWidget, 0, 2);
  236.  
  237.     mainWindow.show();
  238.     MRrenderer->Render();
  239.     CTrenderer->Render();
  240.  
  241.  
  242.     QObject::connect(&slider, &QSlider::valueChanged, [&](int value) {
  243.         MRreslice->SetResliceAxesOrigin(0, 0, value);
  244.         CTreslice->SetResliceAxesOrigin(0, 0, value);
  245.         MRrenderer->GetRenderWindow()->Render();
  246.         CTrenderer->GetRenderWindow()->Render();
  247.         });
  248.  
  249.     return app.exec();
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement