Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkVtkPropRendererTest.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "vtkActor.h"
18 #include "vtkPolyDataMapper.h"
19 #include "vtkProperty.h"
20 #include "vtkRenderWindow.h"
21 #include "vtkRenderWindowInteractor.h"
22 #include "vtkRenderer.h"
23 #include "vtkSphereSource.h"
24 #include "vtkUnsignedCharArray.h"
25 
26 #include <mitkImage.h>
27 #include <mitkPicFileReader.h>
28 
29 #include <mitkDataStorage.h>
30 #include <mitkLevelWindow.h>
32 
33 #include <mitkVtkLayerController.h>
34 #include <mitkVtkPropRenderer.h>
35 #include <vtkMitkRenderProp.h>
36 
37 #include <itksys/SystemTools.hxx>
38 
39 #include <fstream>
40 int mitkVtkPropRendererTest(int argc, char *argv[])
41 {
42  // independently read header of pic file
43  mitkIpPicDescriptor *picheader = NULL;
44  if (argc >= 1)
45  {
46  if (itksys::SystemTools::LowerCase(itksys::SystemTools::GetFilenameExtension(argv[1])).find(".pic") !=
47  std::string::npos)
48  picheader = mitkIpPicGetHeader(argv[1], NULL);
49  }
50  if (picheader == NULL)
51  {
52  std::cout << "file not found/not a pic-file - test not applied [PASSED]" << std::endl;
53  std::cout << "[TEST DONE]" << std::endl;
54  return EXIT_SUCCESS;
55  }
56 
57  static long int sum_orig_Pic3D_pic_gz = 14685408;
58 
59  mitkIpPicGetTags(argv[1], picheader);
60 
61  // Read pic-Image from file
62  std::cout << "Reading image: ";
64  reader->SetFileName(argv[1]);
65  reader->Update();
66  std::cout << "[PASSED]" << std::endl;
67 
68  mitk::Image::Pointer image = reader->GetOutput();
69 
70  std::cout << "Creating node: ";
72  node->SetData(image);
73  std::cout << "[PASSED]" << std::endl;
74 
75  std::cout << "Creating DataStorage: ";
77  std::cout << "[PASSED]" << std::endl;
78 
79  std::cout << "Adding node via DataStorage: ";
80  ds->Add(node);
81  std::cout << "[PASSED]" << std::endl;
82 
83  std::cout << "Adding level-window property: ";
85  mitk::LevelWindow levelwindow;
86  levelwindow.SetAuto(image);
87  levWinProp->SetLevelWindow(levelwindow);
88  node->GetPropertyList()->SetProperty("levelwindow", levWinProp);
89  std::cout << "[PASSED]" << std::endl;
90 
91  std::cout << "Creating a vtk sphere: ";
92  vtkSphereSource *sphere = vtkSphereSource::New();
93  sphere->SetRadius(1.0);
94  sphere->SetThetaResolution(18);
95  sphere->SetPhiResolution(18);
96 
97  vtkPolyDataMapper *map = vtkPolyDataMapper::New();
98  map->SetInput(sphere->GetOutput());
99  sphere->Delete();
100 
101  vtkActor *aSphere = vtkActor::New();
102  aSphere->SetMapper(map);
103  map->Delete();
104  aSphere->GetProperty()->SetColor(0, 0, 1); // sphere color blue
105  std::cout << "[PASSED]" << std::endl;
106 
107  std::cout << "Creating a renderer for the sphere: ";
108  vtkRenderer *sphereRenderer = vtkRenderer::New();
109  sphereRenderer->AddActor(aSphere);
110  aSphere->Delete();
111  // sphereRenderer->SetBackground(1,1,1); // Background color white
112  std::cout << "[PASSED]" << std::endl;
113 
114  std::cout << "Creating vtkRenderWindow and VtkPropRenderer: ";
115  vtkRenderWindow *renderWindow = vtkRenderWindow::New();
116  mitk::VtkPropRenderer::Pointer propRenderer = mitk::VtkPropRenderer::New("the renderer", renderWindow);
117  // propRenderer->SetMapperID(2);
118  std::cout << "[PASSED]" << std::endl;
119 
120  // renderWindow->AddRenderer(sphereRenderer);
121  // renderWindow->SetErase(0);
122 
123  std::cout << "BaseRenderer::SetData(iterator): ";
124  propRenderer->SetDataStorage(ds);
125  std::cout << "[PASSED]" << std::endl;
126 
127  std::cout << "Creating vtkMitkRenderProp and connecting it to the VtkPropRenderer: ";
129  renderProp->SetPropRenderer(propRenderer);
130  propRenderer->GetVtkRenderer()->AddViewProp(renderProp);
131  std::cout << "[PASSED]" << std::endl;
132 
133  std::cout << "Inserting the sphere into the foreground of the VtkLayerController: ";
134  mitk::VtkLayerController::GetInstance(renderWindow)->InsertForegroundRenderer(sphereRenderer, true);
135  std::cout << "[PASSED]" << std::endl;
136 
137  // mouse interaction for debugging
138  // vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
139  // iren->SetRenderWindow(renderWindow);
140 
141  std::cout << "Setting and getting size of renderWindow: ";
142  renderWindow->SetSize(400, 400);
143  int *size = renderWindow->GetSize();
144  std::cout << "[PASSED]" << std::endl;
145 
146  std::cout << "Do the rendering: ";
147  renderWindow->Render();
148  std::cout << "[PASSED]" << std::endl;
149 
150  // iren->Start();
151 
152  std::cout << "Testing to pick a world position: ";
153  mitk::Point2D p;
154  mitk::Point3D p_mm;
155  p[0] = 10;
156  p[1] = 10;
157  propRenderer->PickWorldPoint(p, p_mm);
158  std::cout << "returned world position: " << p_mm << "\n";
159 
160  std::cout << "Creating vtkUnsignedCharArray: ";
161  vtkUnsignedCharArray *vtkImage = vtkUnsignedCharArray::New();
162  std::cout << "[PASSED]" << std::endl;
163 
164  cout << "Reading image from renderWindow" << std::endl;
165  renderWindow->GetRGBACharPixelData(0, 0, size[0] - 1, size[1] - 1, 0, vtkImage);
166  cout << "Read " << size[0] * size[1] << " data points\n";
167 
168  cout << "Computing sum of all RGBA values..\n";
169  long int sum_now = 0;
170  for (int i = 0; i < size[0] * size[1]; i++)
171  sum_now += vtkImage->GetValue(i);
172  std::cout << "Sum of all RGBA values: " << sum_now << "\n";
173  std::cout << "Sum should be: " << sum_orig_Pic3D_pic_gz << "\n";
174 
175  // std::string Pic3d_pic_gz_str("Pic3D.pic.gz");
176  // std::cout << "pic3d " << Pic3d_pic_gz_str << "\n";
177  // std::cout << "argv " << argv_str << "\n";
178  // std::cout << "find " << (int) argv_str.find("Pic3D.pic.gz") << "\n";
179  // std::cout << "size " << argv_str.size() << "\n";
180 
181  // if(argv_str.size() - ((int) argv_str.find("Pic3D.pic.gz")) == 12)
182  //{
183  // std::cout << "Input image is Pic3D.pic.gz\n";
184  // std::cout << "Sum should be: " << sum_orig_Pic3D_pic_gz << "\n";
185  // if(sum_orig_Pic3D_pic_gz!=sum_now)
186  // {
187  // std::cout<<"[FAILED]"<<std::endl;
188  // return EXIT_FAILURE;
189  // }
190  // std::cout<<"[PASSED]"<<std::endl;
191  //}
192  // else
193  //{
194  // std::cout<<"Unknown image, comparison test skipped"<<std::endl;
195  //}
196 
197  propRenderer->GetVtkRenderer()->RemoveViewProp(renderProp);
198  renderProp->Delete();
199  propRenderer = NULL;
200  sphereRenderer->Delete();
201 
202  renderWindow->Delete();
203 
204  vtkImage->Delete();
205  ds = NULL;
206 
207  std::cout << "[TEST DONE]" << std::endl;
208  return EXIT_SUCCESS;
209 }
void SetPropRenderer(mitk::VtkPropRenderer::Pointer propRenderer)
itk::SmartPointer< Self > Pointer
mitkIpPicDescriptor * mitkIpPicGetHeader(const char *picfile_name, mitkIpPicDescriptor *pic)
void InsertForegroundRenderer(vtkSmartPointer< vtkRenderer > renderer, bool forceAbsoluteForeground)
static Pointer New()
vtkMitkRenderProp
mitkIpPicDescriptor * mitkIpPicGetTags(const char *picfile_name, mitkIpPicDescriptor *pic)
The LevelWindow class Class to store level/window values.
static VtkLayerController * GetInstance(vtkSmartPointer< vtkRenderWindow > renWin)
static Pointer New()
void SetAuto(const Image *image, bool tryPicTags=true, bool guessByCentralSlice=true, unsigned selectedComponent=0)
sets level/window to optimize the contrast of the given Image
int mitkVtkPropRendererTest(int argc, char *argv[])
static vtkMitkRenderProp * New()
static Pointer New(const char *_arga, vtkRenderWindow *_argb, mitk::RenderingManager *_argc)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.