Guest User

Untitled

a guest
Jan 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. #include <vtkMetaImageReader.h>
  2. #include <vtkSmartPointer.h>
  3. #include "vtkMatrix4x4.h"
  4. #include "vtkImageReslice.h"
  5. #include "vtkLookupTable.h"
  6. #include "vtkImageMapToColors.h"
  7. #include "vtkImageActor.h"
  8. #include "vtkRenderer.h"
  9. #include "vtkRenderWindow.h"
  10. #include "vtkRenderWindowInteractor.h"
  11. #include "vtkInteractorStyleImage.h"
  12. #include "vtkCommand.h"
  13. #include "vtkImageData.h"
  14.  
  15. // The mouse motion callback, to turn "Slicing" on and off
  16. class vtkImageInteractionCallback : public vtkCommand
  17. {
  18. public:
  19.  
  20. static vtkImageInteractionCallback *New() {
  21. return new vtkImageInteractionCallback; };
  22.  
  23. vtkImageInteractionCallback() {
  24. this->Slicing = 0;
  25. this->ImageReslice = 0;
  26. this->Interactor = 0; };
  27.  
  28. void SetImageReslice(vtkImageReslice *reslice) {
  29. this->ImageReslice = reslice; };
  30.  
  31. vtkImageReslice *GetImageReslice() {
  32. return this->ImageReslice; };
  33.  
  34. void SetInteractor(vtkRenderWindowInteractor *interactor) {
  35. this->Interactor = interactor; };
  36.  
  37. vtkRenderWindowInteractor *GetInteractor() {
  38. return this->Interactor; };
  39.  
  40. virtual void Execute(vtkObject *, unsigned long event, void *)
  41. {
  42. vtkRenderWindowInteractor *interactor = this->GetInteractor();
  43.  
  44. int lastPos[2];
  45. interactor->GetLastEventPosition(lastPos);
  46. int currPos[2];
  47. interactor->GetEventPosition(currPos);
  48.  
  49. if (event == vtkCommand::LeftButtonPressEvent)
  50. {
  51. std::cout << "left button press event" << "n";
  52. this->Slicing = 1;
  53. }
  54. else if (event == vtkCommand::LeftButtonReleaseEvent)
  55. {
  56. this->Slicing = 0;
  57. }
  58. else if (event == vtkCommand::MouseMoveEvent)
  59. {
  60. if (this->Slicing)
  61. {
  62.  
  63. vtkImageReslice *reslice = this->ImageReslice;
  64.  
  65. // Increment slice position by deltaY of mouse
  66. int deltaY = lastPos[1] - currPos[1];
  67.  
  68. std::cout << deltaY << "n";
  69. // reslice->GetOutput()
  70. double sliceSpacing = reslice->GetOutput()->GetSpacing()[2];
  71. vtkMatrix4x4 *matrix = reslice->GetResliceAxes();
  72. // move the center point that we are slicing through
  73. double point[4];
  74. double center[4];
  75. point[0] = 0.0;
  76. point[1] = 0.0;
  77. point[2] = sliceSpacing * deltaY;
  78. point[3] = 1.0;
  79. matrix->MultiplyPoint(point, center);
  80. matrix->SetElement(0, 3, center[0]);
  81. matrix->SetElement(1, 3, center[1]);
  82. matrix->SetElement(2, 3, center[2]);
  83. interactor->Render();
  84. }
  85. else
  86. {
  87. vtkInteractorStyle *style = vtkInteractorStyle::SafeDownCast(
  88. interactor->GetInteractorStyle());
  89. if (style)
  90. {
  91. style->OnMouseMove();
  92. }
  93. }
  94. }
  95. };
  96.  
  97. private:
  98.  
  99. // Actions (slicing only, for now)
  100. int Slicing;
  101.  
  102. // Pointer to vtkImageReslice
  103. vtkImageReslice *ImageReslice;
  104.  
  105. // Pointer to the interactor
  106. vtkRenderWindowInteractor *Interactor;
  107. };
  108.  
  109. // The program entry point
  110. int main (int argc, char **argv)
  111. {
  112. if (argc < 2)
  113. {
  114. cout << "Usage: " << argv[0] << " DATADIR/headsq/quarter" << endl;
  115. return 1;
  116. }
  117.  
  118. // Start by loading some data.
  119. std::string inputFilename = argv[1];
  120.  
  121. vtkSmartPointer<vtkMetaImageReader> reader =
  122. vtkSmartPointer<vtkMetaImageReader>::New();
  123. reader->SetFileName(inputFilename.c_str());
  124. // reader->SetFilePrefix(argv[1]);
  125. reader->SetDataExtent(0, 256, 0, 256, 1, 94);
  126. reader->SetDataSpacing(0.9375, 0.9375, 1.5);
  127. reader->SetDataOrigin(0.0, 0.0, 0.0);
  128. //reader->SetDataScalarTypeToUnsignedShort();
  129. reader->UpdateWholeExtent();
  130. reader->UpdateInformation();
  131. // Calculate the center of the volume
  132. // reader->GetOutput()->UpdateInformation();
  133. int extent[6];
  134. double spacing[3];
  135. double origin[3];
  136. reader->GetOutput()->GetExtent(extent);
  137. reader->GetOutput()->GetSpacing(spacing);
  138. reader->GetOutput()->GetOrigin(origin);
  139.  
  140. double center[3];
  141. center[0] = origin[0] + spacing[0] * 0.5 * (extent[0] + extent[1]);
  142. center[1] = origin[1] + spacing[1] * 0.5 * (extent[2] + extent[3]);
  143. center[2] = origin[2] + spacing[2] * 0.5 * (extent[4] + extent[5]);
  144. std::cout << "Center:";
  145. std::cout << center[0] << "," << center[1] << "," << center[2] << "n";
  146. // Matrices for axial, coronal, sagittal, oblique view orientations
  147. static double axialElements[16] = {
  148. 1, 0, 0, 0,
  149. 0, 1, 0, 0,
  150. 0, 0, 1, 0,
  151. 0, 0, 0, 1 };
  152.  
  153. static double coronalElements[16] = {
  154. 1, 0, 0, 0,
  155. 0, 0, 1, 0,
  156. 0,-1, 0, 0,
  157. 0, 0, 0, 1 };
  158.  
  159. static double sagittalElements[16] = {
  160. 0, 0,-1, 0,
  161. 1, 0, 0, 0,
  162. 0,-1, 0, 0,
  163. 0, 0, 0, 1 };
  164.  
  165. static double obliqueElements[16] = {
  166. 1, 0, 0, 0,
  167. 0, 0.866025, -0.5, 0,
  168. 0, 0.5, 0.866025, 0,
  169. 0, 0, 0, 1 };
  170.  
  171. // Set the slice orientation
  172. vtkMatrix4x4 *resliceAxes = vtkMatrix4x4::New();
  173. resliceAxes->DeepCopy(sagittalElements);
  174. // Set the point through which to slice
  175. resliceAxes->SetElement(0, 3, center[0]);
  176. resliceAxes->SetElement(1, 3, center[1]);
  177. resliceAxes->SetElement(2, 3, center[2]);
  178.  
  179. // Extract a slice in the desired orientation
  180. vtkImageReslice *reslice = vtkImageReslice::New();
  181. reslice->SetInputConnection(reader->GetOutputPort());
  182. reslice->SetOutputDimensionality(2);
  183. reslice->SetResliceAxes(resliceAxes);
  184. reslice->SetInterpolationModeToLinear();
  185.  
  186. // Create a greyscale lookup table
  187. vtkLookupTable *table = vtkLookupTable::New();
  188. table->SetRange(0, 2000); // image intensity range
  189. table->SetValueRange(0.0, 1.0); // from black to white
  190. table->SetSaturationRange(0.0, 0.0); // no color saturation
  191. table->SetRampToLinear();
  192. table->Build();
  193.  
  194. // Map the image through the lookup table
  195. vtkImageMapToColors *color = vtkImageMapToColors::New();
  196. color->SetLookupTable(table);
  197. color->SetInputConnection(reslice->GetOutputPort());
  198.  
  199. // Display the image
  200. vtkImageActor *actor = vtkImageActor::New();
  201. actor->SetInputData(color->GetOutput());
  202.  
  203. vtkRenderer *renderer = vtkRenderer::New();
  204. renderer->AddActor(actor);
  205.  
  206. vtkRenderWindow *window = vtkRenderWindow::New();
  207. window->AddRenderer(renderer);
  208.  
  209. // Set up the interaction
  210. vtkInteractorStyleImage *imageStyle = vtkInteractorStyleImage::New();
  211. vtkRenderWindowInteractor *interactor = vtkRenderWindowInteractor::New();
  212. interactor->SetInteractorStyle(imageStyle);
  213. window->SetInteractor(interactor);
  214. window->Render();
  215.  
  216. vtkImageInteractionCallback *callback = vtkImageInteractionCallback::New();
  217. callback->SetImageReslice(reslice);
  218. callback->SetInteractor(interactor);
  219.  
  220. imageStyle->AddObserver(vtkCommand::MouseMoveEvent, callback);
  221. imageStyle->AddObserver(vtkCommand::LeftButtonPressEvent, callback);
  222. imageStyle->AddObserver(vtkCommand::LeftButtonReleaseEvent, callback);
  223.  
  224. // Start interaction
  225. // The Start() method doesn't return until the window is closed by the user
  226. interactor->Start();
  227.  
  228. std::cout << "interactor done" << "n";
  229. // Clean up
  230. callback->Delete();
  231. interactor->Delete();
  232. imageStyle->Delete();
  233. window->Delete();
  234. renderer->Delete();
  235. actor->Delete();
  236. reslice->Delete();
  237. resliceAxes->Delete();
  238. color->Delete();
  239. table->Delete();
  240. reader->Delete();
  241. }
Add Comment
Please, Sign In to add comment