Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.42 KB | None | 0 0
  1. // Derived from VTK/Examples/Cxx/Medical4.cxx
  2. // This example reads a volume dataset and displays it via volume rendering.
  3. //
  4.  
  5. #include <vtkCamera.h>
  6. #include <vtkColorTransferFunction.h>
  7. #include <vtkFixedPointVolumeRayCastMapper.h>
  8. #include <vtkMetaImageReader.h>
  9. #include <vtkNamedColors.h>
  10. #include <vtkPiecewiseFunction.h>
  11. #include <vtkRenderer.h>
  12. #include <vtkRenderWindow.h>
  13. #include <vtkRenderWindowInteractor.h>
  14. #include <vtkSmartPointer.h>
  15. #include <vtkVolume.h>
  16. #include <vtkVolumeProperty.h>
  17. #include <vtkInteractorStyleTrackballCamera.h>
  18. #include <vtkCallbackCommand.h>
  19. #include <vtkCommand.h>
  20. #include "vtkVolume16Reader.h"
  21.  
  22. #include <array>
  23.  
  24. int step = 5;
  25.  
  26. double aneurism[4] = { 1, 1, 1, 256 };
  27. double head[4] = { 3.2, 3.2, 1.5, 93 };
  28. double frog[4] = { 1, 1, 2.2, 44 };
  29.  
  30. vtkSmartPointer<vtkColorTransferFunction>volumeColor;
  31. vtkSmartPointer<vtkPiecewiseFunction> volumeScalarOpacity;
  32. //vtkSmartPointer<vtkPiecewiseFunction> volumeGradientOpacity;
  33. vtkSmartPointer<vtkVolumeProperty> volumeProperty;
  34. vtkSmartPointer<vtkRenderWindow> renWin;
  35.  
  36. void KeypressCallbackFunction(
  37. vtkObject* caller,
  38. long unsigned int eventId,
  39. void* clientData,
  40. void* callData);
  41.  
  42. int main (int argc, char *argv[])
  43. {
  44. if (argc < 2)
  45. {
  46. cout << "Usage: " << argv[0] << "file.mhd" << endl;
  47. return EXIT_FAILURE;
  48. }
  49.  
  50. vtkSmartPointer<vtkNamedColors> colors =
  51. vtkSmartPointer<vtkNamedColors>::New();
  52.  
  53. std::array<unsigned char , 4> bkg{{51, 77, 102, 255}};
  54. colors->SetColor("BkgColor", bkg.data());
  55.  
  56. // Create the renderer, the render window, and the interactor. The renderer
  57. // draws into the render window, the interactor enables mouse- and
  58. // keyboard-based interaction with the scene.
  59. vtkSmartPointer<vtkRenderer> ren =
  60. vtkSmartPointer<vtkRenderer>::New();
  61. renWin = vtkSmartPointer<vtkRenderWindow>::New();
  62. renWin->AddRenderer(ren);
  63. vtkSmartPointer<vtkRenderWindowInteractor> iren =
  64. vtkSmartPointer<vtkRenderWindowInteractor>::New();
  65. iren->SetRenderWindow(renWin);
  66.  
  67. vtkSmartPointer<vtkCallbackCommand> keypressCallback =
  68. vtkSmartPointer<vtkCallbackCommand>::New();
  69. keypressCallback->SetCallback(KeypressCallbackFunction);
  70. iren->AddObserver(vtkCommand::KeyPressEvent, keypressCallback);
  71.  
  72. // The following reader is used to read a series of 2D slices (images)
  73. // that compose the volume. The slice dimensions are set, and the
  74. // pixel spacing. The data Endianness must also be specified. The reader
  75. // uses the FilePrefix in combination with the slice number to construct
  76. // filenames using the format FilePrefix.%d. (In this case the FilePrefix
  77. // is the root name of the file: quarter.)
  78. // vtkSmartPointer<vtkMetaImageReader> reader =
  79. // vtkSmartPointer<vtkMetaImageReader>::New();
  80. // reader->SetFileName(argv[1]);
  81.  
  82. // vtkVolumeReader16 reads in the head CT data set.
  83. vtkSmartPointer<vtkVolume16Reader> reader =
  84. vtkSmartPointer<vtkVolume16Reader>::New();
  85. // reader = vtkVolume16Reader::New();
  86. reader->SetDataDimensions(256, 256);
  87. reader->SetImageRange(1, aneurism[3]);
  88. reader->SetDataByteOrderToLittleEndian();
  89. reader->SetFilePrefix("../data/aneurism/aneurism");
  90. reader->SetDataSpacing(aneurism[0], aneurism[1], aneurism[2]);
  91.  
  92. // The volume will be displayed by ray-cast alpha compositing.
  93. // A ray-cast mapper is needed to do the ray-casting.
  94. vtkSmartPointer<vtkFixedPointVolumeRayCastMapper> volumeMapper =
  95. vtkSmartPointer<vtkFixedPointVolumeRayCastMapper>::New();
  96. volumeMapper->SetInputConnection(reader->GetOutputPort());
  97.  
  98. // The color transfer function maps voxel intensities to colors.
  99. // It is modality-specific, and often anatomy-specific as well.
  100. // The goal is to one color for flesh (between 500 and 1000)
  101. // and another color for bone (1150 and over).
  102. volumeColor = vtkSmartPointer<vtkColorTransferFunction>::New();
  103. volumeColor->AddRGBPoint(0, 0.0, 0.0, 0.0);
  104. volumeColor->AddRGBPoint(50, 0.4, 0.2, 0.1);
  105. volumeColor->AddRGBPoint(100, 0.8, 0.4, 0.2);
  106. volumeColor->AddRGBPoint(150, 1.0, 0.6, 0.3);
  107. volumeColor->AddRGBPoint(200, 1.0, 1.0, 0.4);
  108.  
  109. // The opacity transfer function is used to control the opacity
  110. // of different tissue types.
  111. volumeScalarOpacity = vtkSmartPointer<vtkPiecewiseFunction>::New();
  112. volumeScalarOpacity->AddPoint(0, 0.00);
  113. volumeScalarOpacity->AddPoint(50, 0.00);
  114. volumeScalarOpacity->AddPoint(100, 0.30);
  115. volumeScalarOpacity->AddPoint(150, 0.50);
  116. volumeScalarOpacity->AddPoint(200, 0.90);
  117.  
  118. // The gradient opacity function is used to decrease the opacity
  119. // in the "flat" regions of the volume while maintaining the opacity
  120. // at the boundaries between tissue types. The gradient is measured
  121. // as the amount by which the intensity changes over unit distance.
  122. // For most medical data, the unit distance is 1mm.
  123. /* volumeGradientOpacity = vtkSmartPointer<vtkPiecewiseFunction>::New();
  124. volumeGradientOpacity->AddPoint(0, 0.0);
  125. volumeGradientOpacity->AddPoint(90, 0.5);
  126. volumeGradientOpacity->AddPoint(100, 1.0);
  127. */
  128. // The VolumeProperty attaches the color and opacity functions to the
  129. // volume, and sets other volume properties. The interpolation should
  130. // be set to linear to do a high-quality rendering. The ShadeOn option
  131. // turns on directional lighting, which will usually enhance the
  132. // appearance of the volume and make it look more "3D". However,
  133. // the quality of the shading depends on how accurately the gradient
  134. // of the volume can be calculated, and for noisy data the gradient
  135. // estimation will be very poor. The impact of the shading can be
  136. // decreased by increasing the Ambient coefficient while decreasing
  137. // the Diffuse and Specular coefficient. To increase the impact
  138. // of shading, decrease the Ambient and increase the Diffuse and Specular.
  139. volumeProperty = vtkSmartPointer<vtkVolumeProperty>::New();
  140. volumeProperty->SetColor(volumeColor);
  141. volumeProperty->SetScalarOpacity(volumeScalarOpacity);
  142. // volumeProperty->SetGradientOpacity(volumeGradientOpacity);
  143. volumeProperty->SetInterpolationTypeToLinear();
  144. volumeProperty->ShadeOn();
  145. volumeProperty->SetAmbient(0.4);
  146. volumeProperty->SetDiffuse(0.6);
  147. volumeProperty->SetSpecular(0.2);
  148.  
  149. // The vtkVolume is a vtkProp3D (like a vtkActor) and controls the position
  150. // and orientation of the volume in world coordinates.
  151. vtkSmartPointer<vtkVolume> volume =
  152. vtkSmartPointer<vtkVolume>::New();
  153. volume->SetMapper(volumeMapper);
  154. volume->SetProperty(volumeProperty);
  155.  
  156. // Finally, add the volume to the renderer
  157. ren->AddViewProp(volume);
  158.  
  159. // Set up an initial view of the volume. The focal point will be the
  160. // center of the volume, and the camera position will be 400mm to the
  161. // patient's left (which is our right).
  162. vtkCamera *camera = ren->GetActiveCamera();
  163. double *c = volume->GetCenter();
  164. camera->SetViewUp (0, 0, -1);
  165. camera->SetPosition (c[0], c[1] - 400, c[2]);
  166. camera->SetFocalPoint (c[0], c[1], c[2]);
  167. camera->Azimuth(30.0);
  168. camera->Elevation(30.0);
  169.  
  170. // Set a background color for the renderer
  171. ren->SetBackground(colors->GetColor3d("BkgColor").GetData());
  172.  
  173. // Increase the size of the render window
  174. renWin->SetSize(640, 480);
  175.  
  176. // Interact with the data.
  177. iren->Start();
  178.  
  179. return EXIT_SUCCESS;
  180. }
  181.  
  182. void KeypressCallbackFunction(vtkObject* caller, long unsigned int vtkNotUsed(eventId), void* vtkNotUsed(clientData), void* vtkNotUsed(callData))
  183. {
  184. std::cout << "Keypress callback" << std::endl;
  185.  
  186. vtkRenderWindowInteractor *iren =
  187. static_cast<vtkRenderWindowInteractor*>(caller);
  188.  
  189. std::cout << "Pressed: " << iren->GetKeySym() << std::endl;
  190. std::string key = iren->GetKeySym();
  191.  
  192. if (key == "Up") {
  193. // volumeColor->RemoveAllPoints();
  194. // volumeScalarOpacity->RemoveAllPoints();
  195.  
  196. step++;
  197. int isoValue_ = 0;
  198. double stepValue = (double)1 / step;
  199.  
  200. for (int i = 0; i < step; i++) {
  201. double r = stepValue * 2 * i;
  202. double b = stepValue / 2 * i;
  203. if (r > 1.0) r = 1.0;
  204. volumeScalarOpacity->AddPoint(isoValue_, stepValue * i);
  205. // volumeGradientOpacity->AddPoint(isoValue_, stepValue * i);
  206. volumeColor->AddRGBPoint(isoValue_, r, stepValue * i, b);
  207. isoValue_ += 50;
  208. }
  209. volumeProperty->SetColor(volumeColor);
  210. volumeProperty->SetScalarOpacity(volumeScalarOpacity);
  211. volumeProperty->ShadeOn();
  212. volumeProperty->SetInterpolationTypeToLinear();
  213. // volumeProperty->SetGradientOpacity(volumeGradientOpacity);
  214. renWin->Render();
  215. }
  216. else if (key == "Down") {
  217. // volumeColor->RemoveAllPoints();
  218. // volumeScalarOpacity->RemoveAllPoints();
  219. // volumeGradientOpacity->RemoveAllPoints();
  220.  
  221. if (step > 0) {
  222. step--;
  223. // volumeScalarOpacity->RemoveAllPoints();
  224. // volumeGradientOpacity->RemoveAllPoints();
  225. // volumeColor->RemoveAllPoints();
  226. int isoValue_ = 0;
  227. double stepValue = (double)1 / step;
  228.  
  229. for (int i = 0; i < step; i++) {
  230. double r = stepValue * 2 * i;
  231. double b = stepValue / 2 * i;
  232. if (r > 1.0) r = 1.0;
  233. volumeScalarOpacity->AddPoint(isoValue_, stepValue * i);
  234. // volumeGradientOpacity->AddPoint(isoValue_, stepValue * i);
  235. volumeColor->AddRGBPoint(isoValue_, r, stepValue * i, b);
  236. isoValue_ += 50;
  237. }
  238. volumeProperty->SetColor(volumeColor);
  239. volumeProperty->SetScalarOpacity(volumeScalarOpacity);
  240. volumeProperty->ShadeOn();
  241. volumeProperty->SetInterpolationTypeToLinear();
  242. // volumeProperty->SetGradientOpacity(volumeGradientOpacity);
  243. renWin->Render();
  244. }
  245. }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement