Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkTransferFunctionTest.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 "mitkTransferFunction.h"
15 
16 #include "mitkTestingMacros.h"
17 
18 #include <iostream>
19 
28 int mitkTransferFunctionTest(int /* argc */, char * /*argv*/ [])
29 {
30  // always start with this!
31  MITK_TEST_BEGIN("TransferFunction");
32 
33  // let's create an object of our class
35 
36  // first test: did this work?
37  // using MITK_TEST_CONDITION_REQUIRED makes the test stop after failure, since
38  // it makes no sense to continue without an object.
39  MITK_TEST_CONDITION_REQUIRED(myTransferFunction.IsNotNull(), "Testing instantiation");
40 
41  /*************************************************************************/
42  // Create and set control point arrays for scalar opacity transfer function
43  mitk::TransferFunction::ControlPoints scalarOpacityPoints;
44  scalarOpacityPoints.push_back(std::make_pair(0.0, 0.0));
45  scalarOpacityPoints.push_back(std::make_pair(5.0, 0.3));
46  scalarOpacityPoints.push_back(std::make_pair(10.0, 1.0));
47  myTransferFunction->SetScalarOpacityPoints(scalarOpacityPoints);
48  MITK_TEST_CONDITION_REQUIRED(myTransferFunction->GetScalarOpacityFunction()->GetSize() == 3,
49  "Adding three point/value pairs to scalar opacity transfer function via VTK interface");
50 
51  // Create and set control point arrays for gradient opacity transfer function
52  mitk::TransferFunction::ControlPoints gradientOpacityPoints;
53  gradientOpacityPoints.push_back(std::make_pair(0.0, 0.2));
54  gradientOpacityPoints.push_back(std::make_pair(3.0, 0.7));
55  gradientOpacityPoints.push_back(std::make_pair(7.0, 0.8));
56  gradientOpacityPoints.push_back(std::make_pair(15.0, 0.9));
57  myTransferFunction->SetGradientOpacityPoints(gradientOpacityPoints);
58  MITK_TEST_CONDITION_REQUIRED(myTransferFunction->GetGradientOpacityFunction()->GetSize() == 4,
59  "Adding four point/value pairs to gradient opacity transfer function via VTK interface");
60 
61  // Create and set control point arrays for color transfer function
63  itk::RGBPixel<double> rgb0, rgb1, rgb2, rgb3;
64  rgb0[0] = 0.1f;
65  rgb0[1] = 0.3f;
66  rgb0[2] = 0.5f;
67  colorPoints.push_back(std::make_pair(2.0, rgb0));
68  rgb1[0] = 0.3;
69  rgb1[1] = 0.8;
70  rgb1[2] = 0.9;
71  colorPoints.push_back(std::make_pair(3.0, rgb1));
72  rgb2[0] = 0.6;
73  rgb2[1] = 0.5;
74  rgb2[2] = 0.4;
75  colorPoints.push_back(std::make_pair(4.0, rgb2));
76  rgb3[0] = 0.7;
77  rgb3[1] = 0.1;
78  rgb3[2] = 0.2;
79  colorPoints.push_back(std::make_pair(5.0, rgb3));
80 
81  myTransferFunction->SetRGBPoints(colorPoints);
82  MITK_TEST_CONDITION_REQUIRED(myTransferFunction->GetColorTransferFunction()->GetSize() == 4,
83  "Adding four point/value pairs to color transfer function via VTK interface");
84 
85  /*************************************************************************/
86  // Add a new point to scalar opacity transfer function
87  myTransferFunction->AddScalarOpacityPoint(3.0, 0.2);
88  MITK_TEST_CONDITION_REQUIRED(myTransferFunction->GetScalarOpacityFunction()->GetSize() == 4,
89  "Adding new point/value to scalar opacity transfer function via MITK interface");
90 
91  // Add a new point to gradient opacity transfer function
92  myTransferFunction->AddGradientOpacityPoint(19.0, 1.0);
93  MITK_TEST_CONDITION_REQUIRED(myTransferFunction->GetGradientOpacityFunction()->GetSize() == 5,
94  "Adding new point/value to gradient opacity transfer function via MITK interface");
95 
96  // Add a new point to color transfer function
97  myTransferFunction->AddRGBPoint(1.0, 0.0, 0.1, 0.2);
98  MITK_TEST_CONDITION_REQUIRED(myTransferFunction->GetColorTransferFunction()->GetSize() == 5,
99  "Adding new point/value to color transfer function via MITK interface");
100 
101  /*************************************************************************/
102  // Retrieve two points from scalar opacity transfer function
103  double v1, v2;
104  v1 = myTransferFunction->GetScalarOpacityFunction()->GetValue(3.0);
105  v2 = myTransferFunction->GetScalarOpacityFunction()->GetValue(10.0);
106  MITK_TEST_CONDITION_REQUIRED((fabs(v1 - 0.2) < 1.0e-5) && (fabs(v2 - 1.0) < 1.0e-5),
107  "Retrieving values at two points from scalar opacity transfer function");
108 
109  // Retrieve two points from gradient opacity transfer function
110  v1 = myTransferFunction->GetGradientOpacityFunction()->GetValue(0.0);
111  v2 = myTransferFunction->GetGradientOpacityFunction()->GetValue(19.0);
112  MITK_TEST_CONDITION_REQUIRED((fabs(v1 - 0.2) < 1.0e-5) && (fabs(v2 - 1.0) < 1.0e-5),
113  "Retrieving values at two points from gradient opacity transfer function");
114 
115  // Retrieve two points from color transfer function
116  double vrgb1[3], vrgb2[3];
117  myTransferFunction->GetColorTransferFunction()->GetColor(1.0, vrgb1);
118  myTransferFunction->GetColorTransferFunction()->GetColor(4.0, vrgb2);
119  std::cout << vrgb2[0] << ", " << vrgb2[1] << ", " << vrgb2[2] << std::endl;
120  MITK_TEST_CONDITION_REQUIRED((fabs(vrgb1[0] - 0.0) < 1.0e-5) && (fabs(vrgb1[1] - 0.1) < 1.0e-5) &&
121  (fabs(vrgb1[2] - 0.2) < 1.0e-5) && (fabs(vrgb2[0] - 0.6) < 1.0e-5) &&
122  (fabs(vrgb2[1] - 0.5) < 1.0e-5) && (fabs(vrgb2[2] - 0.4) < 1.0e-5),
123  "Retrieving values at two points from color transfer function");
124 
125  /*************************************************************************/
126  // Remove point from scalar opacity transfer function (should return new
127  // number of points)
128  MITK_TEST_CONDITION_REQUIRED(myTransferFunction->RemoveScalarOpacityPoint(3.0) == 1,
129  "Removing point from scalar opacity transfer function (should return point index)");
130 
131  // Remove point from scalar opacity transfer function (should return new
132  // number of points)
133  MITK_TEST_CONDITION_REQUIRED(myTransferFunction->RemoveGradientOpacityPoint(0.0) == 0,
134  "Removing point from gradient opacity transfer function (should return point index)");
135 
136  // Remove point from color transfer function (should return new
137  // number of points)
138  MITK_TEST_CONDITION_REQUIRED(myTransferFunction->RemoveRGBPoint(5.0) == 4,
139  "Removing point from color transfer function (should return point ndex)");
140 
141  /*************************************************************************/
142  // Try to remove non-existing point from scalar opacity transfer function (should return -1)
144  myTransferFunction->RemoveScalarOpacityPoint(2.5) == -1,
145  "Trying to remove non-existing point from scalar opacity transfer function (should return -1)");
146 
147  // Try to remove non-existing point from gradient opacity transfer function (should return -1)
149  myTransferFunction->RemoveGradientOpacityPoint(2.5) == -1,
150  "Trying to remove non-existing point from gradient opacity transfer function (should return -1)");
151 
152  // Try to remove non-existing point from color transfer function (should return -1)
153  MITK_TEST_CONDITION_REQUIRED(myTransferFunction->RemoveRGBPoint(2.5) == -1,
154  "Trying to remove non-existing point from color transfer function (should return -1)");
155 
156  /*************************************************************************/
157  // Retrieve copy of scalar opacity points (check for correct content and size)
158  mitk::TransferFunction::ControlPoints newScalarOpacityPoints = myTransferFunction->GetScalarOpacityPoints();
159  MITK_TEST_CONDITION_REQUIRED((newScalarOpacityPoints.size() == 3) &&
160  (fabs(newScalarOpacityPoints[2].first - 10.0) < 1.0e-5) &&
161  (fabs(newScalarOpacityPoints[2].second - 1.0) < 1.0e-5),
162  "Retrieving copy of scalar opacity points (checking for correct content and size)");
163 
164  // Retrieve copy of gradient opacity points (check for correct content and size)
165  mitk::TransferFunction::ControlPoints newGradientOpacityPoints = myTransferFunction->GetGradientOpacityPoints();
166  MITK_TEST_CONDITION_REQUIRED((newGradientOpacityPoints.size() == 4) &&
167  (fabs(newGradientOpacityPoints[3].first - 19.0) < 1.0e-5) &&
168  (fabs(newGradientOpacityPoints[3].second - 1.0) < 1.0e-5),
169  "Retrieving copy of gradient opacity points (checking for correct content and size)");
170 
171  // Retrieve copy of color transfer points (check for correct content and size)
172  mitk::TransferFunction::RGBControlPoints newRGBPoints = myTransferFunction->GetRGBPoints();
174  (newRGBPoints.size() == 4) && (fabs(newRGBPoints[3].first - 4.0) < 1.0e-5) &&
175  (fabs(newRGBPoints[3].second[0] - 0.6) < 1.0e-5) && (fabs(newRGBPoints[3].second[1] - 0.5) < 1.0e-5) &&
176  (fabs(newRGBPoints[3].second[2] - 0.4) < 1.0e-5),
177  "Retrieving copy of color transfer function points (checking for correct content and size)");
178 
179  /*************************************************************************/
180  // Clear scalar opacity points (resulting array should be empty)
181  myTransferFunction->ClearScalarOpacityPoints();
182  MITK_TEST_CONDITION_REQUIRED(myTransferFunction->GetScalarOpacityPoints().size() == 0,
183  "Clearing scalar opacity points (resulting array should be empty)");
184 
185  myTransferFunction->ClearGradientOpacityPoints();
186  MITK_TEST_CONDITION_REQUIRED(myTransferFunction->GetGradientOpacityPoints().size() == 0,
187  "Clearing gradient opacity points (resulting array should be empty)");
188 
189  myTransferFunction->ClearRGBPoints();
190  MITK_TEST_CONDITION_REQUIRED(myTransferFunction->GetRGBPoints().size() == 0,
191  "Clearing color transfer function points (resulting array should be empty)");
192 
193  /******************TRANSFERFUNCTION INITIALIZER TEST***************************/
196  mitk::TransferFunctionInitializer::New(dummyTransferFunction);
197  MITK_TEST_CONDITION_REQUIRED(transferInit->GetTransferFunction().IsNotNull(), "Testing if Transferfunction is set");
198  MITK_TEST_CONDITION_REQUIRED(transferInit->GetTransferFunction() == dummyTransferFunction,
199  "Testing if Transferfunction is the correct one");
200 
201  transferInit->SetTransferFunction(myTransferFunction);
202 
203  MITK_TEST_CONDITION_REQUIRED(transferInit->GetTransferFunction().IsNotNull(),
204  "Testing if Set Transferfunction works");
205  MITK_TEST_CONDITION_REQUIRED(transferInit->GetTransferFunction() == myTransferFunction,
206  "Testing if Set Transferfunction sets the correct one");
207 
208  const int size = 8;
209  int arPointNumbers[size][3] = {
210  {3, 1, 6}, {2, 1, 2}, {4, 1, 5}, {3, 1, 5}, {4, 1, 7}, {4, 2, 7}, {4, 1, 4}, {6, 2, 6}};
211 
212  std::string names[size] = {"SetDefaultMode",
213  "SetCtBlackWhiteMode",
214  "SetCtThoraxLargeMode",
215  "SetCtThoraxSmallMode",
216  "SetCtBoneMode",
217  "SetCtBoneGradientMode",
218  "SetCtCardiacMode",
219  "SetMrGenericMode"};
220 
221  for (int i = 0; i < size; i++)
222  {
223  transferInit->SetTransferFunctionMode(i);
224 
225  std::cout << "Punkte: " << myTransferFunction->GetScalarOpacityFunction()->GetSize() << std::endl;
226  MITK_TEST_CONDITION_REQUIRED(myTransferFunction->GetScalarOpacityFunction()->GetSize() == arPointNumbers[i][0],
227  "Testing if in " << names[i] << " the Scalar Opacity Function is set");
228  MITK_TEST_CONDITION_REQUIRED(myTransferFunction->GetGradientOpacityFunction()->GetSize() == arPointNumbers[i][1],
229  "Testing if in " << names[i] << " the Gradient Opacity Function is set");
230  MITK_TEST_CONDITION_REQUIRED(myTransferFunction->GetColorTransferFunction()->GetSize() == arPointNumbers[i][2],
231  "Testing if in " << names[i] << " the Color Transfer Function is set");
232  }
233  // always end with this!
234  MITK_TEST_END()
235 }
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
int mitkTransferFunctionTest(int, char *[])
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
std::vector< std::pair< double, itk::RGBPixel< double > > > RGBControlPoints
static Pointer New()
std::vector< std::pair< double, double > > ControlPoints
and MITK_TEST_END()