Medical Imaging Interaction Toolkit  2018.4.99-9c019225
Medical Imaging Interaction Toolkit
mitkRenderingTestHelper.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 // VTK
14 #include <vtkCamera.h>
15 #include <vtkOpenGLRenderWindow.h>
16 #include <vtkPNGWriter.h>
17 #include <vtkRenderLargeImage.h>
18 #include <vtkRenderWindow.h>
19 #include <vtkRenderWindowInteractor.h>
20 
21 // MITK
23 #include <mitkRenderWindow.h>
27 #include <mitkException.h>
29 #include <mitkTestingMacros.h>
30 
31 #if defined _MSC_VER
32 #if _MSC_VER >= 1700
33 #define RESIZE_WORKAROUND
34 #endif
35 #endif
36 
37 #ifdef RESIZE_WORKAROUND
38 #include "vtkWin32OpenGLRenderWindow.h"
39 #endif
40 
41 // VTK Testing to compare the rendered image pixel-wise against a reference screen shot
42 #include "vtkTesting.h"
43 
45  int height,
46  AntiAliasing antiAliasing)
47  : m_AutomaticallyCloseRenderWindow(true)
48 {
49  this->Initialize(width, height, antiAliasing);
50 }
51 
53  int width, int height, int argc, char *argv[], AntiAliasing antiAliasing)
55 {
56  this->Initialize(width, height, antiAliasing);
57  this->SetInputFileNames(argc, argv);
58 }
59 
60 void mitk::RenderingTestHelper::Initialize(int width, int height, AntiAliasing antiAliasing)
61 {
63 
64  mitk::UIDGenerator uidGen = mitk::UIDGenerator("UnnamedRenderer_", 8);
65  m_RenderWindow = mitk::RenderWindow::New(nullptr, uidGen.GetUID().c_str());
66 
67  auto renderWindow = m_RenderWindow->GetVtkRenderWindow();
68 
69  if (0 == renderWindow->SupportsOpenGL())
70  {
71  auto openGLRenderWindow = dynamic_cast<vtkOpenGLRenderWindow*>(renderWindow);
72 
73  auto message = nullptr != openGLRenderWindow
74  ? openGLRenderWindow->GetOpenGLSupportMessage()
75  : std::string("No details available.");
76 
77  mitkThrowException(mitk::TestNotRunException) << "OpenGL not supported: " << message;
78  }
79 
81 
82  m_RenderWindow->GetRenderer()->SetDataStorage(m_DataStorage);
83  this->SetMapperIDToRender2D();
84  this->GetVtkRenderWindow()->SetSize(width, height);
85 
86 #ifdef RESIZE_WORKAROUND
87 
88  HWND hWnd = static_cast<vtkWin32OpenGLRenderWindow *>(this->GetVtkRenderWindow())->GetWindowId();
89 
90  RECT r;
91  r.left = 10;
92  r.top = 10;
93  r.right = r.left + width;
94  r.bottom = r.top + height;
95 
96  LONG style = GetWindowLong(hWnd, GWL_STYLE);
97  AdjustWindowRect(&r, style, FALSE);
98 
99  MITK_INFO << "WANTED:";
100  MITK_INFO << r.right - r.left;
101  MITK_INFO << r.bottom - r.top;
102 
103  RECT rect;
104  if (GetWindowRect(hWnd, &rect))
105  {
106  int width = rect.right - rect.left;
107  int height = rect.bottom - rect.top;
108 
109  MITK_INFO << "ACTUAL:";
110  MITK_INFO << width;
111  MITK_INFO << height;
112  }
113 
114  SetWindowPos(hWnd, HWND_TOP, 0, 0, r.right - r.left, r.bottom - r.top, SWP_NOZORDER);
115 
116  GetWindowRect(hWnd, &rect);
117 
118  int width2 = rect.right - rect.left;
119  int height2 = rect.bottom - rect.top;
120 
121  MITK_INFO << "ACTUAL2:";
122  MITK_INFO << width2;
123  MITK_INFO << height2;
124 
125  SetWindowPos(hWnd, HWND_TOP, 0, 0, 2 * (r.right - r.left) - width2, 2 * (r.bottom - r.top) - height2, SWP_NOZORDER);
126 
127 #endif
128 
129  m_RenderWindow->GetRenderer()->Resize(width, height);
130 }
131 
133 {
134 }
135 
137 {
138  m_RenderWindow->GetRenderer()->SetMapperID(id);
139 }
140 
142 {
145  this->GetDataStorage()->ComputeBoundingGeometry3D(this->GetDataStorage()->GetAll()));
146 }
147 
149 {
151 }
152 
154 {
155  // if the datastorage is initialized and at least 1 image is loaded render it
156  if (m_DataStorage.IsNotNull() || m_DataStorage->GetAll()->Size() >= 1)
157  {
158  // Prepare the VTK camera before rendering.
159  m_RenderWindow->GetRenderer()->PrepareRender();
160 
161  this->GetVtkRenderWindow()->Render();
162  this->GetVtkRenderWindow()->WaitForCompletion();
164  {
165  // Use interaction to stop the test
166  this->GetVtkRenderWindow()->GetInteractor()->Start();
167  }
168  }
169  else
170  {
171  MITK_ERROR << "No images loaded in data storage!";
172  }
173 }
174 
176 {
177  return m_DataStorage;
178 }
179 
181 {
182  // i is set 1, because 0 is the testname as string
183  // parse parameters
184  for (int i = 1; i < argc; ++i)
185  {
186  // add everything to a list but -T and -V
187  std::string tmp = argv[i];
188  if ((tmp.compare("-T")) && (tmp.compare("-V")))
189  {
190  this->AddToStorage(tmp);
191  }
192  else
193  {
194  break;
195  }
196  }
197 }
198 
200 {
201  mitk::BaseRenderer::GetInstance(m_RenderWindow->GetVtkRenderWindow())
202  ->GetSliceNavigationController()
203  ->SetDefaultViewDirection(viewDirection);
205  m_DataStorage->ComputeBoundingGeometry3D(m_DataStorage->GetAll()));
206 }
207 
209 {
210  mitk::SliceNavigationController::Pointer sliceNavigationController =
211  mitk::BaseRenderer::GetInstance(m_RenderWindow->GetVtkRenderWindow())->GetSliceNavigationController();
212  sliceNavigationController->ReorientSlices(origin, rotation);
213 }
214 
216 {
217  return m_RenderWindow->GetRenderer()->GetVtkRenderer();
218 }
219 
220 void mitk::RenderingTestHelper::SetImageProperty(const char *propertyKey, mitk::BaseProperty *property)
221 {
222  this->m_DataStorage->GetNode(mitk::NodePredicateDataType::New("Image"))->SetProperty(propertyKey, property);
223 }
224 
226 {
227  return m_RenderWindow->GetVtkRenderWindow();
228 }
229 
230 bool mitk::RenderingTestHelper::CompareRenderWindowAgainstReference(int argc, char *argv[], double threshold)
231 {
232  this->Render();
233  // retVal meanings: (see VTK/Rendering/vtkTesting.h)
234  // 0 = test failed
235  // 1 = test passed
236  // 2 = test not run
237  // 3 = something with vtkInteraction
238  if (vtkTesting::Test(argc, argv, this->GetVtkRenderWindow(), threshold) == 1)
239  return true;
240  else
241  return false;
242 }
243 
244 // method to save a screenshot of the renderwindow (e.g. create a reference screenshot)
245 void mitk::RenderingTestHelper::SaveAsPNG(std::string fileName)
246 {
247  vtkSmartPointer<vtkRenderer> renderer = this->GetVtkRenderer();
248  bool doubleBuffering(renderer->GetRenderWindow()->GetDoubleBuffer());
249  renderer->GetRenderWindow()->DoubleBufferOff();
250 
251  vtkSmartPointer<vtkRenderLargeImage> magnifier = vtkSmartPointer<vtkRenderLargeImage>::New();
252  magnifier->SetInput(renderer);
253  magnifier->SetMagnification(1);
254 
255  vtkSmartPointer<vtkImageWriter> fileWriter = vtkSmartPointer<vtkPNGWriter>::New();
256  fileWriter->SetInputConnection(magnifier->GetOutputPort());
257  fileWriter->SetFileName(fileName.c_str());
258 
259  fileWriter->Write();
260  renderer->GetRenderWindow()->SetDoubleBuffer(doubleBuffering);
261 }
262 
263 void mitk::RenderingTestHelper::SetAutomaticallyCloseRenderWindow(bool automaticallyCloseRenderWindow)
264 {
265  m_AutomaticallyCloseRenderWindow = automaticallyCloseRenderWindow;
266 }
267 
269 {
270  this->SaveAsPNG(fileName);
271 }
272 
273 void mitk::RenderingTestHelper::AddToStorage(const std::string &filename)
274 {
275  try
276  {
277  mitk::IOUtil::Load(filename, *m_DataStorage.GetPointer());
279  m_DataStorage->ComputeBoundingGeometry3D(m_DataStorage->GetAll()));
280  }
281  catch ( const itk::ExceptionObject &e )
282  {
283  MITK_ERROR << "Failed loading test data '" << filename << "': " << e.what();
284  }
285 }
286 
288 {
289  this->m_DataStorage->Add(node);
291  m_DataStorage->ComputeBoundingGeometry3D(m_DataStorage->GetAll()));
292 }
void SaveAsPNG(std::string fileName)
Method can be used to save a screenshot (e.g. reference screenshot as a .png file.
virtual bool InitializeViews(const BaseGeometry *geometry, RequestType type=REQUEST_UPDATE_ALL, bool preserveRoughOrientationInWorldSpace=false)
void Initialize(int width, int height, AntiAliasing antiAliasing=AntiAliasing::None)
Initialize Internal method to initialize the renderwindow and set the datastorage.
void SetViewDirection(mitk::SliceNavigationController::ViewDirection viewDirection)
Set the view direction of the renderwindow (e.g. sagittal, coronal, axial)
Generated unique IDs.
static BaseRenderer * GetInstance(vtkRenderWindow *renWin)
#define MITK_INFO
Definition: mitkLogMacros.h:18
vtkRenderWindow * GetVtkRenderWindow()
Getter for the vtkRenderWindow which should be used to call vtkRegressionTestImage.
#define MITK_ERROR
Definition: mitkLogMacros.h:20
void SetMapperID(mitk::BaseRenderer::StandardMapperSlot id)
SetMapperID Change between Standard2D and 3D mappers.
void SetAntiAliasing(AntiAliasing antiAliasing)
mitk::DataStorage::Pointer m_DataStorage
void SetMapperIDToRender3D()
SetMapperIDToRender3D Convenience method to render in a 3D renderwindow.
static Matrix3D rotation
void SaveReferenceScreenShot(std::string fileName)
SaveReferenceScreenShot Convenience method to save a reference screen shot.
static Pointer New()
void SetInputFileNames(int argc, char *argv[])
This method tries to parse the given argv for files (e.g. images) and load them into a member datasto...
bool CompareRenderWindowAgainstReference(int argc, char *argv[], double threshold=10.0)
CompareRenderWindowAgainstReference Convenience method to compare the image rendered in the internal ...
void SetImageProperty(const char *propertyKey, mitk::BaseProperty *property)
This method set the property of the member datastorage.
void ReorientSlices(mitk::Point3D origin, mitk::Vector3D rotation)
Reorient the slice (e.g. rotation and translation like the swivel mode).
Abstract base class for properties.
void Render()
Render everything into an mitkRenderWindow. Call SetViewDirection() and SetProperty() before this met...
static RenderingManager * GetInstance()
ViewDirection
Possible view directions, Original will uses the PlaneGeometry instances in a SlicedGeometry3D provid...
Specialized mitk::Exception for skipped tests.
mitk::RenderWindow::Pointer m_RenderWindow
void AddToStorage(const std::string &filename)
This method tries to load the given file into a member datastorage, in order to render it...
RenderingTestHelper(int width, int height, int argc, char *argv[], AntiAliasing antiAliasing=AntiAliasing::None)
Generate a rendering test helper object including a render window of the size width * height (in pixe...
mitk::DataStorage::Pointer GetDataStorage()
Returns the datastorage, in order to modify the data inside a rendering test.
static Pointer New(const char *_arg)
#define mitkThrowException(classname)
vtkRenderer * GetVtkRenderer()
Getter for the vtkRenderer.
void SetAutomaticallyCloseRenderWindow(bool automaticallyCloseRenderWindow)
SetStopRenderWindow Convenience method to make the renderwindow hold after rendering. Usefull for debugging.
void SetMapperIDToRender2D()
SetMapperIDToRender2D Convenience method to render in a 2D renderwindow.
void AddNodeToStorage(mitk::DataNode::Pointer node)
AddNodeToStorage Add a node to the datastorage and perform a reinit which is necessary for rendering...
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage, const ReaderOptionsFunctorBase *optionsCallback=nullptr)
Load a file into the given DataStorage.
Definition: mitkIOUtil.cpp:489