Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkViewportRenderingTest.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 // MITK
14 #include "mitkColorProperty.h"
15 #include "mitkImage.h"
19 #include "mitkPlanarFigure.h"
21 #include "mitkSurface.h"
22 #include "mitkTestingMacros.h"
24 
25 // ITK
26 #include <itkVectorContainer.h>
27 
28 // VTK
29 #include <vtkDebugLeaks.h>
30 #include <vtkRegressionTestImage.h>
31 
32 // stdlib
33 #include <cstdlib>
34 
35 int mitkViewportRenderingTest(int argc, char *argv[])
36 {
37  try
38  {
39  mitk::RenderingTestHelper openGlTest(640, 480);
40  }
41  catch (const mitk::TestNotRunException &e)
42  {
43  MITK_WARN << "Test not run: " << e.GetDescription();
44  return 77;
45  }
46 
47  // load all arguments into a datastorage, take last argument as reference rendering
48  // setup a renderwindow of fixed size X*Y
49  // render the datastorage
50  // compare rendering to reference image
51  MITK_TEST_BEGIN("mitkViewportRenderingTest")
52 
53 
54  vtkDebugLeaks::SetExitError(0);
55 
56  // enough parameters?
57  if (argc < 2)
58  {
59  MITK_TEST_OUTPUT(<< "Usage: " << std::string(*argv) << " [file1 file2 ...] outputfile")
60  MITK_TEST_OUTPUT(<< "Will render a central axial slice of all given files into outputfile")
61  exit(EXIT_FAILURE);
62  }
63 
64  double renderWindowWidth = atof(argv[1]);
65  double renderWindowHeight = atof(argv[2]);
66  double left = atof(argv[3]);
67  double bottom = atof(argv[4]);
68  double right = atof(argv[5]);
69  double top = atof(argv[6]);
70  std::string referenceFilename = argv[8 + 6];
71  argv += 6; // DO NOT attempt to read these as files, this just makes no sense
72  argc -= 6; // DO NOT attempt to read these as files, this just makes no sense
73 
74  MITK_INFO << "Testing viewport " << right - left << "x" << top - bottom << " (" << left << ", " << bottom << ") to ("
75  << right << ", " << top << ") "
76  << "in render window of size " << renderWindowWidth << "x" << renderWindowHeight << "px";
77 
78  mitk::RenderingTestHelper renderingHelper(renderWindowWidth, renderWindowHeight, argc, argv); // non-power-of-2
79 
80  // for now this test renders Sagittal
81  // renderingHelper.SetViewDirection(mitk::SliceNavigationController::Axial);
83 
84  typedef mitk::DataStorage::SetOfObjects ObjectsSet;
85 
86  ObjectsSet::ConstPointer figures =
88  for (auto iterFigures = figures->begin(); iterFigures != figures->end(); ++iterFigures)
89  {
90  (*iterFigures)->SetProperty("planarfigure.default.line.color", mitk::ColorProperty::New(1.0, 0.0, 0.0)); // red
91  (*iterFigures)->SetProperty("planarfigure.drawcontrolpoints", mitk::BoolProperty::New(false));
92  (*iterFigures)->SetProperty("planarfigure.drawname", mitk::BoolProperty::New(false));
93  (*iterFigures)->SetProperty("planarfigure.drawquantities", mitk::BoolProperty::New(true));
94  }
95 
96  ObjectsSet::ConstPointer surfaces =
98  for (auto iterSurfaces = surfaces->begin(); iterSurfaces != surfaces->end(); ++iterSurfaces)
99  {
100  (*iterSurfaces)->SetProperty("color", mitk::ColorProperty::New(0.0, 1.0, 0.0)); // green
101  }
102 
103  ObjectsSet::ConstPointer images =
104  renderingHelper.GetDataStorage()->GetSubset(mitk::TNodePredicateDataType<mitk::Image>::New());
105  for (auto iterImages = images->begin(); iterImages != images->end(); ++iterImages)
106  {
107  (*iterImages)->SetProperty("levelwindow", mitk::LevelWindowProperty::New(mitk::LevelWindow(128.0, 256.0))); // green
108  int imageWidth = dynamic_cast<mitk::Image *>((*iterImages)->GetData())->GetDimension(0);
109  int imageHeight = dynamic_cast<mitk::Image *>((*iterImages)->GetData())->GetDimension(1);
110  MITK_INFO << "Image dimension " << imageWidth << "x" << imageHeight;
111  }
112 
113  double vLeft = left / renderWindowWidth;
114  double vBottom = bottom / renderWindowHeight;
115  double vRight = right / renderWindowWidth;
116  double vTop = top / renderWindowHeight;
117  // THIS HERE IS THE ACTUAL TEST PART, all the rest is setup and decoration
118 
120  renderingHelper.GetDataStorage()->ComputeBoundingGeometry3D(images));
121 
122  renderingHelper.GetVtkRenderer()->SetViewport(vLeft, vBottom, vRight, vTop);
123  renderingHelper.SetAutomaticallyCloseRenderWindow(true); // set to false for testing the test itself
124  renderingHelper.Render();
125  // use this to generate a reference screenshot or save the file:
126  bool generateReferenceScreenshot = false;
127  if (generateReferenceScreenshot)
128  {
129  std::string tmpFilename = referenceFilename;
130  std::string::size_type slashpos = referenceFilename.find_last_of('/');
131  tmpFilename = referenceFilename.substr(slashpos + 1);
132  tmpFilename = std::string("/tmp/") + tmpFilename;
133  renderingHelper.SaveAsPNG(tmpFilename);
134  MITK_INFO << "*********************************";
135  MITK_INFO << "SAVE TO " << tmpFilename;
136  MITK_INFO << "*********************************";
137  }
138 
139  //### Usage of vtkRegressionTestImage:
140  // vtkRegressionTestImage( vtkRenderWindow )
141  // Set a vtkRenderWindow containing the desired scene.
142  // vtkRegressionTestImage automatically searches in argc and argv[]
143  // for a path a valid image with -V. If the test failed with the
144  // first image (foo.png) check if there are images of the form
145  // foo_N.png (where N=1,2,3...) and compare against them.
146  int retVal = vtkRegressionTestImageThreshold(renderingHelper.GetVtkRenderWindow(), 20.0);
147 
148  // retVal meanings: (see VTK/Rendering/vtkTesting.h)
149  // 0 = test failed
150  // 1 = test passed
151  // 2 = test not run
152  // 3 = something with vtkInteraction
153  MITK_TEST_CONDITION(retVal == 1, "VTK rendering result matches expectation");
154 
155  MITK_TEST_END();
156 }
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)
int mitkViewportRenderingTest(int argc, char *argv[])
void SetViewDirection(mitk::SliceNavigationController::ViewDirection viewDirection)
Set the view direction of the renderwindow (e.g. sagittal, coronal, axial)
ImageVectorType images
#define MITK_INFO
Definition: mitkLogMacros.h:18
vtkRenderWindow * GetVtkRenderWindow()
Getter for the vtkRenderWindow which should be used to call vtkRegressionTestImage.
static Pointer New()
static Pointer New()
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
Tests for type compatibility (dynamic_cast).
#define MITK_TEST_OUTPUT(x)
Output some text.
The LevelWindow class Class to store level/window values.
static Pointer New()
#define MITK_WARN
Definition: mitkLogMacros.h:19
void Render()
Render everything into an mitkRenderWindow. Call SetViewDirection() and SetProperty() before this met...
#define MITK_TEST_CONDITION(COND, MSG)
static RenderingManager * GetInstance()
Specialized mitk::Exception for skipped tests.
Image class for storing images.
Definition: mitkImage.h:72
itk::VectorContainer< unsigned int, DataNode::Pointer > SetOfObjects
A Container of objects that is used as a result set of GetSubset() query operations (Set of...
mitk::DataStorage::Pointer GetDataStorage()
Returns the datastorage, in order to modify the data inside a rendering test.
vtkRenderer * GetVtkRenderer()
Getter for the vtkRenderer.
void SetAutomaticallyCloseRenderWindow(bool automaticallyCloseRenderWindow)
SetStopRenderWindow Convenience method to make the renderwindow hold after rendering. Usefull for debugging.
and MITK_TEST_END()