Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 (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 <mitkIOUtil.h>
18 #include <mitkTestFixture.h>
19 #include <mitkTestingMacros.h>
20 
21 // VTK
22 #include <vtkDoubleArray.h>
23 #include <vtkPointData.h>
24 #include <vtkPolyData.h>
25 
29 class mitkSurfaceVtkMapper2D3DTestSuite : public mitk::TestFixture
30 {
31  CPPUNIT_TEST_SUITE(mitkSurfaceVtkMapper2D3DTestSuite);
32  MITK_TEST(RenderLUT2D);
33  MITK_TEST(RenderLUT3D);
34  CPPUNIT_TEST_SUITE_END();
35 
36 private:
38  mitk::RenderingTestHelper m_RenderingTestHelper;
39  std::vector<std::string> m_CommandlineArgs;
40  std::string m_PathToBall;
41 
42 public:
48  mitkSurfaceVtkMapper2D3DTestSuite() : m_RenderingTestHelper(300, 300) {}
53  void setUp()
54  {
55  m_RenderingTestHelper = mitk::RenderingTestHelper(300, 300);
56 
57  m_PathToBall = GetTestDataFilePath("ball.stl");
58 
59  // Build a command line for the vtkTesting::Test method.
60  // See VTK documentation and RenderingTestHelper for more information.
61  // Use the following command line option to save the difference image
62  // and the test image in some tmp folder
63  // m_CommandlineArgs.push_back("-T");
64  // m_CommandlineArgs.push_back("/path/to/save/tmp/difference/images/");
65  m_CommandlineArgs.push_back("-V");
66  }
67 
68  void tearDown() {}
69  // Helper method to prepare a DataNode holding a mitk::Surface
70  // for rendering of point scalars via a lookup table (LUT).
71  void PrepareSurfaceRenderingWithLUT(mitk::DataNode &node)
72  {
73  mitk::Surface::Pointer surface = dynamic_cast<mitk::Surface *>(node.GetData());
74  CPPUNIT_ASSERT(surface);
75 
76  vtkPolyData *polydata = surface->GetVtkPolyData();
77  CPPUNIT_ASSERT(polydata);
78 
79  // Build lookup table entries, associate to points of vtkPolyData
80  vtkSmartPointer<vtkDoubleArray> data_array = vtkSmartPointer<vtkDoubleArray>::New();
81  data_array->Initialize();
82  data_array->SetName("Funny LUT entries");
83  data_array->SetNumberOfComponents(1);
84  auto num_points = polydata->GetNumberOfPoints(); // initialize all points in polydata
85  data_array->SetNumberOfTuples(num_points);
86 
87  for (unsigned int index = 0; index != num_points; ++index)
88  {
89  // just assign values 0..4 to points
90  // (value 0 for point idx 0..249, value 1 for idx 250..499, etc.)
91  data_array->SetValue(index, (index / 250) % 5); // values 0 .. 4
92  }
93 
94  polydata->GetPointData()->SetScalars(data_array);
95 
97  node.SetProperty("LookupTable", mitk::LookupTableProperty::New(mitk_lut));
98 
99  node.SetBoolProperty("scalar visibility", true);
100  node.SetBoolProperty("color mode", true);
101  node.SetFloatProperty("ScalarsRangeMinimum", 0);
102  node.SetFloatProperty("ScalarsRangeMaximum", 3);
103 
104  // build the lut
105  vtkSmartPointer<vtkLookupTable> vtk_lut = mitk_lut->GetVtkLookupTable();
106  if (vtk_lut == nullptr)
107  {
108  vtk_lut = vtkSmartPointer<vtkLookupTable>::New();
109  mitk_lut->SetVtkLookupTable(vtk_lut);
110  }
111 
112  // Define the lookup table.
113  vtk_lut->SetTableRange(0, 3);
114  vtk_lut->SetNumberOfTableValues(4);
115 
116  vtk_lut->SetTableValue(0, 1, 0, 0);
117  vtk_lut->SetTableValue(1, 0, 1, 0);
118  vtk_lut->SetTableValue(2, 0, 0, 1);
119  vtk_lut->SetTableValue(3, 1, 1, 0);
120 
121  vtk_lut->Build();
122  }
123 
124  void RenderLUT2D()
125  {
127  node->SetData(mitk::IOUtil::Load(m_PathToBall)[0]);
128  PrepareSurfaceRenderingWithLUT(*node);
129  m_RenderingTestHelper.AddNodeToStorage(node);
130 
131  // reference screenshot for this test
132  m_CommandlineArgs.push_back(GetTestDataFilePath("RenderingTestData/ReferenceScreenshots/ballLUT2D_300x300.png"));
133  // Convert vector of strings to argc/argv
134  mitk::RenderingTestHelper::ArgcHelperClass arg(m_CommandlineArgs);
136  CPPUNIT_ASSERT(m_RenderingTestHelper.CompareRenderWindowAgainstReference(arg.GetArgc(), arg.GetArgv()) == true);
137  }
138 
139  void RenderLUT3D()
140  {
142  node->SetData(mitk::IOUtil::Load(m_PathToBall)[0]);
143  PrepareSurfaceRenderingWithLUT(*node);
144  m_RenderingTestHelper.AddNodeToStorage(node);
145 
146  // reference screenshot for this test
147  m_CommandlineArgs.push_back(GetTestDataFilePath("RenderingTestData/ReferenceScreenshots/ballLUT3D_300x300.png"));
148  // Convert vector of strings to argc/argv
149  mitk::RenderingTestHelper::ArgcHelperClass arg(m_CommandlineArgs);
150  m_RenderingTestHelper.SetMapperIDToRender3D();
151  CPPUNIT_ASSERT(m_RenderingTestHelper.CompareRenderWindowAgainstReference(arg.GetArgc(), arg.GetArgv(), 50.0) == true);
152  // m_RenderingTestHelper.SaveReferenceScreenShot("c:/dev/ballLUT3D_300x300.png");
153  }
154 };
155 
156 MITK_TEST_SUITE_REGISTRATION(mitkSurfaceVtkMapper2D3D)
Class for storing surfaces (vtkPolyData).
Definition: mitkSurface.h:28
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.
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 SetProperty(const std::string &propertyKey, BaseProperty *property, const std::string &contextName="", bool fallBackOnDefaultContext=false) override
Add new or change existent property.
void SetFloatProperty(const char *propertyKey, float floatValue, const mitk::BaseRenderer *renderer=nullptr)
Convenience method for setting float properties (instances of FloatProperty)
BaseData * GetData() const
Get the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
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...
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()
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
Class for nodes of the DataTree.
Definition: mitkDataNode.h:57