Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkVectorTypeConversionTest.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 <iostream>
14 
15 #include "itkVector.h"
16 
17 #include "mitkTestFixture.h"
18 #include "mitkTestingMacros.h"
19 
20 #include "vnl/vnl_math.h"
21 #include <vnl/vnl_vector_fixed_ref.h>
22 
23 #include "mitkNumericConstants.h"
24 #include "mitkPoint.h"
25 #include "mitkVector.h"
26 
27 using namespace mitk;
28 
29 class mitkVectorTypeConversionTestSuite : public mitk::TestFixture
30 
31 {
32  CPPUNIT_TEST_SUITE(mitkVectorTypeConversionTestSuite);
33 
34  MITK_TEST(Point2Vector);
35 
36  MITK_TEST(Pod2Mitk);
37  MITK_TEST(Mitk2Pod);
38 
39  MITK_TEST(OneElement2Mitk);
40 
41  MITK_TEST(Itk2Mitk);
42  MITK_TEST(Mitk2Itk);
43 
44  MITK_TEST(Vnlfixed2Mitk);
45  MITK_TEST(Mitk2Vnlfixed);
46 
47  MITK_TEST(Vnl2Mitk);
48  MITK_TEST(Mitk2Vnl);
49  MITK_TEST(Vnl2Mitk_WrongVnlVectorSize);
50 
51  MITK_TEST(ToArray_DifferentType);
52  MITK_TEST(Fill_DifferentType);
53 
54  CPPUNIT_TEST_SUITE_END();
55 
56 private:
67  ScalarType originalValues[3];
68  ScalarType valuesToCopy[3];
69 
70  float epsDouble2Float;
71 
83  template <typename T1, typename T2>
84  void TestForEquality(const T1 &v1,
85  const T2 &v2,
86  const std::string &v1Name,
87  const std::string &v2Name,
88  const ScalarType &eps = mitk::eps) const
89  {
90  CPPUNIT_ASSERT_EQUAL_MESSAGE(
91  "\nAssigning " + v2Name + " to " + v1Name + ":\n both are equal", true, EqualArray(v1, v2, 3, eps));
92  }
93 
94 public:
95  void setUp(void) override
96  {
97  FillVector3D(originalValues, 1.123456789987, 2.789456321456, 3.123654789987456);
98  FillVector3D(valuesToCopy, 4.654789123321, 5.987456789321, 6.321654987789546);
99 
100  epsDouble2Float = vnl_math::float_eps * 10.0;
101  }
102 
103  void tearDown(void) override {}
104  void Pod2Mitk(void)
105  {
106  mitk::Vector3D vector3D = valuesToCopy;
107 
108  TestForEquality(vector3D, valuesToCopy, "mitk::Vector3D", "double POD");
109  }
110 
111  void Mitk2Pod(void)
112  {
113  ScalarType podArray[3];
114  mitk::Vector3D vector3D = valuesToCopy;
115 
116  vector3D.ToArray(podArray);
117 
118  TestForEquality(podArray, vector3D, "double POD", "mitk::Vector3D");
119  }
120 
121  void OneElement2Mitk(void)
122  {
123  double twos[] = {2.0, 2.0, 2.0};
124  mitk::Vector<double, 3> vector3D(2.0);
125 
126  CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE("\n one values initializes all elements to this value",
127  EqualArray(vector3D, twos, 3));
128  }
129 
130  void Itk2Mitk(void)
131  {
132  itk::Vector<ScalarType, 3> itkVector = valuesToCopy;
133 
134  Vector3D vector3D = itkVector;
135 
136  TestForEquality(vector3D, itkVector, "mitk::Vector3D", "itk::Vector");
137  }
138 
139  void Mitk2Itk(void)
140  {
141  Vector3D vector3D = valuesToCopy;
142 
143  itk::Vector<ScalarType, 3> itkVector = vector3D;
144 
145  TestForEquality(itkVector, vector3D, "itk::Vector", "mitk::Vector3D");
146  }
147 
148  void Vnlfixed2Mitk(void)
149  {
150  vnl_vector_fixed<ScalarType, 3> vnlVectorFixed(valuesToCopy);
151 
152  mitk::Vector3D vector3D = vnlVectorFixed;
153 
154  TestForEquality(vector3D, vnlVectorFixed, "mitk::Vector3D", "vnl_vector_fixed<ScalarType>");
155  }
156 
157  void Mitk2Vnlfixed(void)
158  {
159  vnl_vector_fixed<ScalarType, 3> vnlVectorFixed(originalValues);
160  mitk::Vector3D vector3D = valuesToCopy;
161 
162  vnlVectorFixed = vector3D;
163 
164  TestForEquality(vnlVectorFixed, vector3D, "vnl_vector_fixed<ScalarType>", "mitk::Vector3D");
165  }
166 
167  void Vnl2Mitk(void)
168  {
169  vnl_vector<ScalarType> vnlVector(3);
170  vnlVector.set(valuesToCopy);
171 
172  mitk::Vector3D vector3D = vnlVector;
173 
174  TestForEquality(vector3D, vnlVector, "mitk::Vector3D", "vnl_vector<ScalarType>");
175  }
176 
177  void Mitk2Vnl(void)
178  {
179  vnl_vector<ScalarType> vnlVector(3);
180  vnlVector.set(originalValues);
181  mitk::Vector3D vector3D = valuesToCopy;
182 
183  vnlVector = vector3D;
184 
185  TestForEquality(vnlVector, vector3D, "vnl_vector<ScalarType>", "mitk::Vector3D");
186  }
187 
191  void Vnl2Mitk_WrongVnlVectorSize()
192  {
193  ScalarType largerValuesToCopy[] = {4.12345678910, 5.10987654321, 6.123456789132456, 7.123456987789456};
194  mitk::Vector3D vector3D = originalValues;
195  vnl_vector<ScalarType> vnlVector(4);
196  vnlVector.set(largerValuesToCopy);
197 
198  CPPUNIT_ASSERT_THROW(vector3D = vnlVector, mitk::Exception);
199  }
200 
201  void ToArray_DifferentType(void)
202  {
203  float podArray[3];
204  for (int var = 0; var < 3; ++var)
205  {
206  podArray[var] = originalValues[var];
207  }
208  mitk::Vector3D vector3D = valuesToCopy;
209 
210  vector3D.ToArray(podArray);
211 
212  TestForEquality(podArray, vector3D, "float POD", "mitk::Vector3D", epsDouble2Float);
213  }
214 
215  void Fill_DifferentType(void)
216  {
217  mitk::Vector3D vector3D = originalValues;
218  float podArray[3];
219  for (int var = 0; var < 3; ++var)
220  {
221  podArray[var] = valuesToCopy[var];
222  }
223 
224  vector3D.FillVector(podArray);
225 
226  TestForEquality(vector3D, podArray, "mitk::Vector3D", "float POD", epsDouble2Float);
227  }
228 
229  void Point2Vector()
230  {
231  mitk::Point3D point3D = originalValues;
232 
233  mitk::Vector3D vector3D = point3D.GetVectorFromOrigin();
234 
235  TestForEquality(point3D, vector3D, "mitk::Point3D", "mitk::Vector3D");
236  }
237 };
238 
239 MITK_TEST_SUITE_REGISTRATION(mitkVectorTypeConversion)
bool EqualArray(TArrayType1 &arrayType1, TArrayType2 &arrayType2, int size, ScalarType eps=mitk::eps, bool verbose=false)
Definition: mitkArray.h:128
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
double ScalarType
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
DataCollection - Class to facilitate loading/accessing structured data.
void FillVector3D(Tout &out, mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
Definition: mitkArray.h:106
void FillVector(const ArrayType &array)
Definition: mitkVector.h:101
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
Test fixture for parameterized tests.
MITKCORE_EXPORT const ScalarType eps
void ToArray(ArrayType array) const
Definition: mitkVector.h:114