Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkLookupTableTest.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 #include <mitkLookupTable.h>
14 
15 #include "mitkTestingMacros.h"
16 #include <mitkNumericTypes.h>
17 #include <mitkTestFixture.h>
18 
19 #include <iostream>
20 #include <vtkColorTransferFunction.h>
21 #include <vtkPiecewiseFunction.h>
22 
23 class mitkLookupTableTestSuite : public mitk::TestFixture
24 {
25  CPPUNIT_TEST_SUITE(mitkLookupTableTestSuite);
26  MITK_TEST(TestCreateLookupTable);
27  MITK_TEST(TestSetVtkLookupTable);
28  MITK_TEST(TestSetOpacity);
29  MITK_TEST(TestCreateColorTransferFunction);
30  MITK_TEST(TestCreateOpacityTransferFunction);
31  MITK_TEST(TestCreateGradientTransferFunction);
32  CPPUNIT_TEST_SUITE_END();
33 
34 private:
35  mitk::LookupTable::Pointer m_LookupTable;
36 
37 public:
38  void TestCreateLookupTable()
39  {
40  // let's create an object of our class
42  // first test: did this work?
43  // it makes no sense to continue without an object.
44  CPPUNIT_ASSERT_MESSAGE("Testing instantiation", myLookupTable.IsNotNull());
45  }
46 
47  void TestSetVtkLookupTable()
48  {
50 
51  // create a vtkLookupTable and add two values
52  vtkLookupTable *lut = vtkLookupTable::New();
53  lut->SetTableValue(0, 0.5, 0.5, 0.5, 1.0);
54  lut->SetTableValue(1, 0.5, 0.5, 0.5, 0.5);
55  lut->Build();
56 
57  myLookupTable->SetVtkLookupTable(lut);
58 
59  // check if the same lookuptable is returned
60  vtkLookupTable *lut2 = myLookupTable->GetVtkLookupTable();
61 
62  CPPUNIT_ASSERT_MESSAGE("Input and output table are not equal", lut == lut2);
63 
64  lut->Delete();
65  }
66 
67  void TestSetOpacity()
68  {
70 
71  // create a vtkLookupTable and add two values
72  vtkLookupTable *lut = vtkLookupTable::New();
73  lut->SetRange(0, 200);
74  lut->SetAlphaRange(0.0, 1.0);
75  lut->Build();
76 
77  myLookupTable->SetVtkLookupTable(lut);
78 
79  myLookupTable->ChangeOpacityForAll(0.7f);
80 
81  for (int i = 0; i < lut->GetNumberOfTableValues(); ++i)
82  {
83  CPPUNIT_ASSERT_MESSAGE("Opacity not set for all", mitk::Equal(0.7, lut->GetOpacity(i), 0.01, true));
84  }
85 
86  vtkIdType tableIndex = 10;
87  myLookupTable->ChangeOpacity(tableIndex, 1.0);
88  double rgba[4];
89  lut->GetTableValue(tableIndex, rgba);
90  CPPUNIT_ASSERT_MESSAGE("Opacity not set for value", mitk::Equal(1.0, rgba[3], 0.01, true));
91  lut->Delete();
92  }
93 
94  void TestCreateColorTransferFunction()
95  {
97 
98  // create a vtkLookupTable and add two values
99  vtkLookupTable *lut = vtkLookupTable::New();
100  lut->SetRange(0, 255);
101  lut->Build();
102 
103  myLookupTable->SetVtkLookupTable(lut);
104  vtkSmartPointer<vtkColorTransferFunction> colorTransferFunction = myLookupTable->CreateColorTransferFunction();
105 
106  CPPUNIT_ASSERT(colorTransferFunction != nullptr);
107 
108  vtkIdType numberOfTableEntries = lut->GetNumberOfTableValues();
109  double rgbaTable[4];
110  double rgbaFunction[4];
111  for (int i = 0; i < numberOfTableEntries; ++i)
112  {
113  lut->GetIndexedColor(i, rgbaTable);
114  colorTransferFunction->GetIndexedColor(i, rgbaFunction);
115  CPPUNIT_ASSERT_MESSAGE("Wrong color of transfer function",
116  mitk::Equal(rgbaTable[0], rgbaFunction[0], 0.000001, true) &&
117  mitk::Equal(rgbaTable[1], rgbaFunction[1], 0.000001, true) &&
118  mitk::Equal(rgbaTable[2], rgbaFunction[2], 0.000001, true));
119  }
120  lut->Delete();
121  }
122 
123  void TestCreateOpacityTransferFunction()
124  {
126 
127  // create a vtkLookupTable and add two values
128  vtkLookupTable *lut = vtkLookupTable::New();
129  lut->SetAlphaRange(0, 1.0);
130  lut->Build();
131 
132  myLookupTable->SetVtkLookupTable(lut);
133  vtkSmartPointer<vtkPiecewiseFunction> opacityTransferFunction = myLookupTable->CreateOpacityTransferFunction();
134  CPPUNIT_ASSERT(opacityTransferFunction != nullptr);
135 
136  int funcSize = opacityTransferFunction->GetSize();
137  auto table = new double[funcSize];
138  double rgba[4];
139  opacityTransferFunction->GetTable(0, 1, funcSize, table);
140  for (int i = 0; i < funcSize; ++i)
141  {
142  lut->GetIndexedColor(i, rgba);
143  CPPUNIT_ASSERT_MESSAGE("Wrong opacity of transfer function", mitk::Equal(table[i], rgba[3], 0.000001, true));
144  }
145  lut->Delete();
146  delete[] table;
147  }
148 
149  void TestCreateGradientTransferFunction()
150  {
152 
153  // create a vtkLookupTable and add two values
154  vtkLookupTable *lut = vtkLookupTable::New();
155  lut->SetAlphaRange(0, 0.73);
156  lut->Build();
157 
158  myLookupTable->SetVtkLookupTable(lut);
159  vtkSmartPointer<vtkPiecewiseFunction> gradientTransferFunction = myLookupTable->CreateGradientTransferFunction();
160  CPPUNIT_ASSERT(gradientTransferFunction != nullptr);
161 
162  int funcSize = gradientTransferFunction->GetSize();
163  auto table = new double[funcSize];
164  double rgba[4];
165  gradientTransferFunction->GetTable(0, 1, funcSize, table);
166  for (int i = 0; i < funcSize; ++i)
167  {
168  lut->GetIndexedColor(i, rgba);
169  CPPUNIT_ASSERT_MESSAGE("Wrong opacity of transfer function", mitk::Equal(table[i], rgba[3], 0.000001, true));
170  }
171  lut->Delete();
172  delete[] table;
173  }
174 };
175 MITK_TEST_SUITE_REGISTRATION(mitkLookupTable)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
static Pointer New()
Test fixture for parameterized tests.
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.