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