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