Advertisement
nebk96

Untitled

Apr 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.20 KB | None | 0 0
  1. // Derived from VTK/Examples/Cxx/Medical2.cxx
  2. // This example reads a volume dataset, extracts two isosurfaces that
  3. // represent the skin and bone, and then displays them.
  4. //
  5.  
  6. #include <vtkActor.h>
  7. #include <vtkCamera.h>
  8. #include <vtkMarchingCubes.h>
  9. #include <vtkMetaImageReader.h>
  10. #include <vtkNamedColors.h>
  11. #include <vtkOutlineFilter.h>
  12. #include <vtkPolyDataMapper.h>
  13. #include <vtkPolyDataMapper.h>
  14. #include <vtkProperty.h>
  15. #include <vtkRenderer.h>
  16. #include <vtkRenderWindow.h>
  17. #include <vtkRenderWindowInteractor.h>
  18. #include <vtkSmartPointer.h>
  19. #include <vtkStripper.h>
  20. #include <vtkInteractorStyleTrackballCamera.h>
  21. #include <vtkCallbackCommand.h>
  22. #include <vtkCommand.h>
  23. #include<iostream>
  24.  
  25. #include <array>
  26. using namespace std;
  27.  
  28. int isoValueBone = 1150;
  29. int isoValueSkin = 500;
  30. vtkSmartPointer<vtkMarchingCubes> boneExtractor;
  31. vtkSmartPointer<vtkMarchingCubes> skinExtractor;
  32. vtkSmartPointer<vtkRenderWindow> renWin;
  33.  
  34. //resource : https://vtk.org/Wiki/VTK/Examples/Cxx/Interaction/KeypressObserver
  35. void KeypressCallbackFunction(vtkObject* caller, long unsigned int vtkNotUsed(eventId), void* vtkNotUsed(clientData), void* vtkNotUsed(callData))
  36. {
  37.     std::cout << "Keypress callback" << std::endl;
  38.  
  39.     vtkRenderWindowInteractor *iren =
  40.     static_cast<vtkRenderWindowInteractor*>(caller);
  41.    
  42.     string key = iren->GetKeySym();
  43.     cout << "Pressed: " << iren->GetKeySym() << std::endl;
  44.     if (key == "Up") {
  45.         isoValueSkin = isoValueSkin + 50;
  46.         cout << isoValueSkin << endl;
  47.         cout << iren->GetKeySym();
  48.         skinExtractor->SetValue(0, isoValueSkin);
  49.         renWin->Render();
  50.     }
  51.     else if (key == "Down") {
  52.         isoValueSkin = isoValueSkin - 50;
  53.         cout << isoValueSkin << endl;
  54.         cout << iren->GetKeySym();
  55.         skinExtractor->SetValue(0, isoValueSkin);
  56.         renWin->Render();
  57.     }
  58.  
  59.     else if (key == "Left") {
  60.         isoValueBone = isoValueBone + 50;
  61.         cout << isoValueSkin << endl;
  62.         cout << iren->GetKeySym();
  63.         boneExtractor->SetValue(0, isoValueBone);
  64.         renWin->Render();
  65.     }
  66.     else if (key == "Right") {
  67.         isoValueSkin = isoValueSkin - 50;
  68.         cout << isoValueSkin << endl;
  69.         cout << iren->GetKeySym();
  70.         boneExtractor->SetValue(0, isoValueBone);
  71.         renWin->Render();
  72.     }
  73.    
  74. }
  75.  
  76. int main (int argc, char *argv[])
  77. {
  78.   if (argc < 2)
  79.   {
  80.     cout << "Usage: " << argv[0] << " file.mhd" << endl;
  81.     return EXIT_FAILURE;
  82.   }
  83.  
  84.   vtkSmartPointer<vtkNamedColors> colors =
  85.     vtkSmartPointer<vtkNamedColors>::New();
  86.  
  87.   // Set the colors.
  88.   std::array<unsigned char , 4> skinColor{{255, 125, 64}};
  89.     colors->SetColor("SkinColor", skinColor.data());
  90.   std::array<unsigned char , 4> bkg{{51, 77, 102, 255}};
  91.     colors->SetColor("BkgColor", bkg.data());
  92.  
  93.   // Create the renderer, the render window, and the interactor. The renderer
  94.   // draws into the render window, the interactor enables mouse- and
  95.   // keyboard-based interaction with the data within the render window.
  96.   //
  97.  
  98.     vtkSmartPointer<vtkRenderer> aRenderer =
  99.     vtkSmartPointer<vtkRenderer>::New();
  100.    
  101.  
  102.     renWin = vtkRenderWindow::New();
  103.     vtkSmartPointer<vtkRenderWindow>::New();
  104.   renWin->AddRenderer(aRenderer);
  105.  
  106.   vtkSmartPointer<vtkRenderWindowInteractor> iren =
  107.     vtkSmartPointer<vtkRenderWindowInteractor>::New();
  108.   iren->SetRenderWindow(renWin);
  109.  
  110.   vtkSmartPointer<vtkCallbackCommand> keypressCallback =
  111.       vtkSmartPointer<vtkCallbackCommand>::New();
  112.   keypressCallback->SetCallback(KeypressCallbackFunction);
  113.   iren->AddObserver(vtkCommand::KeyPressEvent, keypressCallback);
  114.  
  115.   // The following reader is used to read a series of 2D slices (images)
  116.   // that compose the volume. The slice dimensions are set, and the
  117.   // pixel spacing. The data Endianness must also be specified. The reader
  118.   // uses the FilePrefix in combination with the slice number to construct
  119.   // filenames using the format FilePrefix.%d. (In this case the FilePrefix
  120.   // is the root name of the file: quarter.)
  121.   vtkSmartPointer<vtkMetaImageReader> reader =
  122.     vtkSmartPointer<vtkMetaImageReader>::New();
  123.   reader->SetFileName (argv[1]);
  124.  
  125.  
  126.  
  127.   // An isosurface, or contour value of 500 is known to correspond to the
  128.   // skin of the patient.
  129.   // The triangle stripper is used to create triangle strips from the
  130.   // isosurface; these render much faster on many systems.
  131.   skinExtractor = vtkSmartPointer<vtkMarchingCubes>::New();
  132.   skinExtractor->SetInputConnection(reader->GetOutputPort());
  133.   skinExtractor->SetValue(0, isoValueSkin);
  134.  
  135.   vtkSmartPointer<vtkStripper> skinStripper =
  136.     vtkSmartPointer<vtkStripper>::New();
  137.   skinStripper->SetInputConnection(skinExtractor->GetOutputPort());
  138.  
  139.   vtkSmartPointer<vtkPolyDataMapper> skinMapper =
  140.     vtkSmartPointer<vtkPolyDataMapper>::New();
  141.   skinMapper->SetInputConnection(skinStripper->GetOutputPort());
  142.   skinMapper->ScalarVisibilityOff();
  143.  
  144.   vtkSmartPointer<vtkActor> skin =
  145.     vtkSmartPointer<vtkActor>::New();
  146.   skin->SetMapper(skinMapper);
  147.   skin->GetProperty()->SetDiffuseColor(colors->GetColor3d("SkinColor").GetData());
  148.   skin->GetProperty()->SetSpecular(.3);
  149.   skin->GetProperty()->SetSpecularPower(20);
  150.   skin->GetProperty()->SetOpacity(.5);
  151.  
  152.   // An isosurface, or contour value of 1150 is known to correspond to the
  153.   // bone of the patient.
  154.   // The triangle stripper is used to create triangle strips from the
  155.   // isosurface; these render much faster on may systems.
  156.  boneExtractor = vtkSmartPointer<vtkMarchingCubes>::New();
  157.   boneExtractor->SetInputConnection(reader->GetOutputPort());
  158.   boneExtractor->SetValue(0, isoValueBone);
  159.  
  160.   vtkSmartPointer<vtkStripper> boneStripper =
  161.     vtkSmartPointer<vtkStripper>::New();
  162.   boneStripper->SetInputConnection(boneExtractor->GetOutputPort());
  163.  
  164.   vtkSmartPointer<vtkPolyDataMapper> boneMapper =
  165.     vtkSmartPointer<vtkPolyDataMapper>::New();
  166.   boneMapper->SetInputConnection(boneStripper->GetOutputPort());
  167.   boneMapper->ScalarVisibilityOff();
  168.  
  169.   vtkSmartPointer<vtkActor> bone =
  170.     vtkSmartPointer<vtkActor>::New();
  171.   bone->SetMapper(boneMapper);
  172.   bone->GetProperty()->SetDiffuseColor(colors->GetColor3d("Ivory").GetData());
  173.  
  174.   // An outline provides context around the data.
  175.   //
  176.   vtkSmartPointer<vtkOutlineFilter> outlineData =
  177.     vtkSmartPointer<vtkOutlineFilter>::New();
  178.   outlineData->SetInputConnection(reader->GetOutputPort());
  179.  
  180.   vtkSmartPointer<vtkPolyDataMapper> mapOutline =
  181.     vtkSmartPointer<vtkPolyDataMapper>::New();
  182.   mapOutline->SetInputConnection(outlineData->GetOutputPort());
  183.  
  184.   vtkSmartPointer<vtkActor> outline =
  185.     vtkSmartPointer<vtkActor>::New();
  186.   outline->SetMapper(mapOutline);
  187.   outline->GetProperty()->SetColor(colors->GetColor3d("Black").GetData());
  188.  
  189.   // It is convenient to create an initial view of the data. The FocalPoint
  190.   // and Position form a vector direction. Later on (ResetCamera() method)
  191.   // this vector is used to position the camera to look at the data in
  192.   // this direction.
  193.   vtkSmartPointer<vtkCamera> aCamera = vtkSmartPointer<vtkCamera>::New();
  194.   aCamera->SetViewUp (0, 0, -1);
  195.   aCamera->SetPosition (0, -1, 0);
  196.   aCamera->SetFocalPoint (0, 0, 0);
  197.   aCamera->ComputeViewPlaneNormal();
  198.   aCamera->Azimuth(30.0);
  199.   aCamera->Elevation(30.0);
  200.  
  201.   // Actors are added to the renderer. An initial camera view is created.
  202.   // The Dolly() method moves the camera towards the FocalPoint,
  203.   // thereby enlarging the image.
  204.   aRenderer->AddActor(outline);
  205.   aRenderer->AddActor(skin);
  206.   aRenderer->AddActor(bone);
  207.   aRenderer->SetActiveCamera(aCamera);
  208.   aRenderer->ResetCamera ();
  209.   aCamera->Dolly(1.5);
  210.  
  211.   // Set a background color for the renderer and set the size of the
  212.   // render window (expressed in pixels).
  213.   aRenderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
  214.   renWin->SetSize(640, 480);
  215.  
  216.   // Note that when camera movement occurs (as it does in the Dolly()
  217.   // method), the clipping planes often need adjusting. Clipping planes
  218.   // consist of two planes: near and far along the view direction. The
  219.   // near plane clips out objects in front of the plane; the far plane
  220.   // clips out objects behind the plane. This way only what is drawn
  221.   // between the planes is actually rendered.
  222.   aRenderer->ResetCameraClippingRange ();
  223.  
  224.   // Initialize the event loop and then start it.
  225.   renWin->Render();
  226.   iren->Initialize();
  227.   iren->Start();
  228.  
  229.   return EXIT_SUCCESS;
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement