Advertisement
Guest User

Untitled

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