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