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