Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkSurfaceVtkMapper2D3DTest.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 <mitkIOUtil.h>
22 #include <mitkTestFixture.h>
23 #include <mitkTestingMacros.h>
24 
25 // VTK
26 #include <vtkDoubleArray.h>
27 #include <vtkPointData.h>
28 #include <vtkPolyData.h>
29 
33 class mitkSurfaceVtkMapper2D3DTestSuite : public mitk::TestFixture
34 {
35  CPPUNIT_TEST_SUITE(mitkSurfaceVtkMapper2D3DTestSuite);
36  MITK_TEST(RenderLUT2D);
37 #ifndef WIN32
38  /* MITK dartclient Win7_VS2012Express_x86_Release refuses to
39  pass this test. Locally, a Windows machine (VS2013, 64bit)
40  passed without problems. */
41  MITK_TEST(RenderLUT3D);
42 #endif
43  CPPUNIT_TEST_SUITE_END();
44 
45 private:
47  mitk::RenderingTestHelper m_RenderingTestHelper;
48  std::vector<std::string> m_CommandlineArgs;
49  std::string m_PathToBall;
50 
51 public:
57  mitkSurfaceVtkMapper2D3DTestSuite() : m_RenderingTestHelper(300, 300) {}
62  void setUp()
63  {
64  m_RenderingTestHelper = mitk::RenderingTestHelper(300, 300);
65 
66  m_PathToBall = GetTestDataFilePath("ball.stl");
67 
68  // Build a command line for the vtkTesting::Test method.
69  // See VTK documentation and RenderingTestHelper for more information.
70  // Use the following command line option to save the difference image
71  // and the test image in some tmp folder
72  // m_CommandlineArgs.push_back("-T");
73  // m_CommandlineArgs.push_back("/path/to/save/tmp/difference/images/");
74  m_CommandlineArgs.push_back("-V");
75  }
76 
77  void tearDown() {}
78  // Helper method to prepare a DataNode holding a mitk::Surface
79  // for rendering of point scalars via a lookup table (LUT).
80  void PrepareSurfaceRenderingWithLUT(mitk::DataNode &node)
81  {
82  mitk::Surface::Pointer surface = dynamic_cast<mitk::Surface *>(node.GetData());
83  CPPUNIT_ASSERT(surface);
84 
85  vtkPolyData *polydata = surface->GetVtkPolyData();
86  CPPUNIT_ASSERT(polydata);
87 
88  // Build lookup table entries, associate to points of vtkPolyData
89  vtkSmartPointer<vtkDoubleArray> data_array = vtkSmartPointer<vtkDoubleArray>::New();
90  data_array->Initialize();
91  data_array->SetName("Funny LUT entries");
92  data_array->SetNumberOfComponents(1);
93  auto num_points = polydata->GetNumberOfPoints(); // initialize all points in polydata
94  data_array->SetNumberOfTuples(num_points);
95 
96  for (unsigned int index = 0; index != num_points; ++index)
97  {
98  // just assign values 0..4 to points
99  // (value 0 for point idx 0..249, value 1 for idx 250..499, etc.)
100  data_array->SetValue(index, (index / 250) % 5); // values 0 .. 4
101  }
102 
103  polydata->GetPointData()->SetScalars(data_array);
104 
106  node.SetProperty("LookupTable", mitk::LookupTableProperty::New(mitk_lut));
107 
108  node.SetBoolProperty("scalar visibility", true);
109  node.SetBoolProperty("color mode", true);
110  node.SetFloatProperty("ScalarsRangeMinimum", 0);
111  node.SetFloatProperty("ScalarsRangeMaximum", 3);
112 
113  // build the lut
114  vtkSmartPointer<vtkLookupTable> vtk_lut = mitk_lut->GetVtkLookupTable();
115  if (vtk_lut == nullptr)
116  {
118  mitk_lut->SetVtkLookupTable(vtk_lut);
119  }
120 
121  // Define the lookup table.
122  vtk_lut->SetTableRange(0, 3);
123  vtk_lut->SetNumberOfTableValues(4);
124 
125  vtk_lut->SetTableValue(0, 1, 0, 0);
126  vtk_lut->SetTableValue(1, 0, 1, 0);
127  vtk_lut->SetTableValue(2, 0, 0, 1);
128  vtk_lut->SetTableValue(3, 1, 1, 0);
129 
130  vtk_lut->Build();
131  }
132 
133  void RenderLUT2D()
134  {
136  node->SetData(mitk::IOUtil::Load(m_PathToBall)[0]);
137  PrepareSurfaceRenderingWithLUT(*node);
138  m_RenderingTestHelper.AddNodeToStorage(node);
139 
140  // reference screenshot for this test
141  m_CommandlineArgs.push_back(GetTestDataFilePath("RenderingTestData/ReferenceScreenshots/ballLUT2D_300x300.png"));
142  // Convert vector of strings to argc/argv
143  mitk::RenderingTestHelper::ArgcHelperClass arg(m_CommandlineArgs);
145  CPPUNIT_ASSERT(m_RenderingTestHelper.CompareRenderWindowAgainstReference(arg.GetArgc(), arg.GetArgv()) == true);
146  }
147 
148  void RenderLUT3D()
149  {
151  node->SetData(mitk::IOUtil::Load(m_PathToBall)[0]);
152  PrepareSurfaceRenderingWithLUT(*node);
153  m_RenderingTestHelper.AddNodeToStorage(node);
154 
155  // reference screenshot for this test
156  m_CommandlineArgs.push_back(GetTestDataFilePath("RenderingTestData/ReferenceScreenshots/ballLUT3D_300x300.png"));
157  // Convert vector of strings to argc/argv
158  mitk::RenderingTestHelper::ArgcHelperClass arg(m_CommandlineArgs);
159  m_RenderingTestHelper.SetMapperIDToRender3D();
160  CPPUNIT_ASSERT(m_RenderingTestHelper.CompareRenderWindowAgainstReference(arg.GetArgc(), arg.GetArgv()) == true);
161  // m_RenderingTestHelper.SaveReferenceScreenShot("c:/dev/ballLUT3D_300x300.png");
162  }
163 };
164 
165 MITK_TEST_SUITE_REGISTRATION(mitkSurfaceVtkMapper2D3D)
Class for storing surfaces (vtkPolyData).
Definition: mitkSurface.h:32
void SetViewDirection(mitk::SliceNavigationController::ViewDirection viewDirection)
Set the view direction of the renderwindow (e.g. sagittal, coronal, axial)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
void SetMapperIDToRender3D()
SetMapperIDToRender3D Convenience method to render in a 3D renderwindow.
BaseData * GetData() const
Get the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
static std::string GetTestDataFilePath(const std::string &testData)
Get the absolute path for test data.
bool CompareRenderWindowAgainstReference(int argc, char *argv[], double threshold=10.0)
CompareRenderWindowAgainstReference Convenience method to compare the image rendered in the internal ...
void SetFloatProperty(const char *propertyKey, float floatValue, const mitk::BaseRenderer *renderer=nullptr)
Convenience method for setting float properties (instances of FloatProperty)
static Pointer New()
static Pointer New()
Test fixture for parameterized tests.
void SetBoolProperty(const char *propertyKey, bool boolValue, const mitk::BaseRenderer *renderer=nullptr)
Convenience method for setting boolean properties (instances of BoolProperty)
The ArgcHelperClass class is a convinience class to convert a vector of strings to the standard c++ a...
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage)
Load a file into the given DataStorage.
Definition: mitkIOUtil.cpp:483
void SetProperty(const char *propertyKey, BaseProperty *property, const mitk::BaseRenderer *renderer=nullptr)
Set the property (instance of BaseProperty) with key propertyKey in the PropertyList of the renderer ...
void AddNodeToStorage(mitk::DataNode::Pointer node)
AddNodeToStorage Add a node to the datastorage and perform a reinit which is necessary for rendering...
static Pointer New()
Class for nodes of the DataTree.
Definition: mitkDataNode.h:66
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.