Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkVectorTest.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 <mitkNumericTypes.h>
18 #include <mitkTestingMacros.h>
19 
20 #include <vnl/vnl_vector_fixed.txx>
21 
22 int mitkVectorTest(int /*argc*/, char * /*argv*/ [])
23 {
24  MITK_TEST_BEGIN("mitkVector");
25  // test itk vector equality methods
26  itk::Vector<mitk::ScalarType, 3> itkVector_1;
27  itkVector_1[0] = 4.6;
28  itkVector_1[1] = 9.76543;
29  itkVector_1[2] = 746.09;
30 
31  itk::Vector<mitk::ScalarType, 3> itkVector_2;
32  itk::Vector<mitk::ScalarType, 3> itkVector_3;
33  for (int i = 0; i < 3; i++)
34  {
35  itkVector_2[i] = itkVector_1[i] - mitk::eps * 1.1;
36  itkVector_3[i] = itkVector_1[i] - mitk::eps * 0.9;
37  }
38 
39  MITK_TEST_CONDITION(mitk::Equal(itkVector_1, itkVector_1),
40  "Test vector equality using the same vector with mitk::eps");
42  !mitk::Equal(itkVector_1, itkVector_2),
43  "Test vector equality using different vectors with an element-wise difference greater than mitk::eps");
44  MITK_TEST_CONDITION(mitk::Equal(itkVector_1, itkVector_2, mitk::eps * 1.2),
45  "Vectors are equal for higher epsilon tolerance ( 1.2 * mitk::eps )");
47  mitk::Equal(itkVector_1, itkVector_3),
48  "Test vector equality using different vectors with an element-wise difference less than mitk::eps");
49  // test itk point equality methods
50  itk::Point<mitk::ScalarType, 3> itkPoint_1;
51  itk::Point<mitk::ScalarType, 3> itkPoint_2;
52  itk::Point<mitk::ScalarType, 3> itkPoint_3;
53  for (int i = 0; i < 3; i++)
54  {
55  itkPoint_1[i] = itkVector_1[i];
56  itkPoint_2[i] = itkVector_2[i];
57  itkPoint_3[i] = itkVector_3[i];
58  }
59  MITK_TEST_CONDITION(mitk::Equal(itkPoint_1, itkPoint_1), "Test point equality using the same point with mitk::eps");
61  !mitk::Equal(itkPoint_1, itkPoint_2),
62  "Test point equality using different points with an element-wise difference greater than mitk::eps");
63  MITK_TEST_CONDITION(mitk::Equal(itkPoint_1, itkPoint_2, mitk::eps * 1.2),
64  "Points are equal for higher epsilon tolerance ( 1.2 * mitk::eps )");
65  MITK_TEST_CONDITION(mitk::Equal(itkPoint_1, itkPoint_3),
66  "Test point equality using different points with an element-wise difference less than mitk::eps");
67  // test mitk vnl vector equality methods
68  mitk::VnlVector mitk_vnl_vector_1(3);
69  mitk::VnlVector mitk_vnl_vector_2(3);
70  mitk::VnlVector mitk_vnl_vector_3(3);
71  for (int i = 0; i < 3; i++)
72  {
73  mitk_vnl_vector_1.put(i, itkVector_1[i]);
74  mitk_vnl_vector_2.put(i, itkVector_2[i]);
75  mitk_vnl_vector_3.put(i, itkVector_1[i]);
76  }
77 
78  MITK_TEST_CONDITION(mitk::Equal(mitk_vnl_vector_1, mitk_vnl_vector_1),
79  "Test mitk vnl vector equality using the same mitk vnl vector with mitk::eps");
80  MITK_TEST_CONDITION(!mitk::Equal(mitk_vnl_vector_1, mitk_vnl_vector_2),
81  "Test mitk vnl vector equality using different mitk vnl vectors with an element-wise difference "
82  "greater than mitk::eps");
83  MITK_TEST_CONDITION(mitk::Equal(mitk_vnl_vector_1, mitk_vnl_vector_2, mitk::eps * 1.2),
84  "Vnl vectors are equal for higher epsilon tolerance ( 1.2 * mitk::eps )");
85  MITK_TEST_CONDITION(mitk::Equal(mitk_vnl_vector_1, mitk_vnl_vector_3),
86  "Test mitk vnl vector equality using "
87  "different mitk vnl vectors with an "
88  "element-wise difference less than mitk::eps");
89 
90  // test vnl_vector equality method
91  typedef mitk::ScalarType VnlValueType;
92  vnl_vector_fixed<VnlValueType, 7> vnlVector_1;
93  vnlVector_1[3] = 56.98;
94  vnlVector_1[4] = 22.32;
95  vnlVector_1[5] = 1.00;
96  vnlVector_1[6] = 746.09;
97  vnl_vector_fixed<VnlValueType, 7> vnlVector_2;
98  vnl_vector_fixed<VnlValueType, 7> vnlVector_3;
99  for (int i = 0; i < 7; i++)
100  {
101  if (i < 3)
102  {
103  vnlVector_1.put(i, itkVector_1[i]);
104  }
105 
106  vnlVector_2[i] = vnlVector_1[i] - mitk::eps * 1.1f;
107  vnlVector_3[i] = vnlVector_1[i] - mitk::eps * 0.9f;
108  }
109 
110  MITK_TEST_CONDITION((mitk::Equal<VnlValueType, 7>(vnlVector_1, vnlVector_1)), "vnl_fixed : v_1 == v_1 ");
111  // the v_2 is constructed so that the equality test fails for mitk::eps, the norm of the difference between the
112  // vectors is 7 * eps/6.9
113  MITK_TEST_CONDITION(!(mitk::Equal<VnlValueType, 7>(vnlVector_1, vnlVector_2)),
114  "vnl_fixed : v_1 != v_2 with mitk::eps ");
115  // increase the epsilon value used for testing equality - should now pass ( 1.2 * mitk::eps > 7 * mitk::eps/6.9 )
116  MITK_TEST_CONDITION((mitk::Equal<VnlValueType, 7>(vnlVector_1, vnlVector_2, mitk::eps * 1.2f)),
117  "vnl_fixed : v_1 == v_2 with eps = 1.2 * mitk::eps ");
118  MITK_TEST_CONDITION((mitk::Equal<VnlValueType, 7>(vnlVector_1, vnlVector_3, mitk::eps)),
119  "vnl_fixed : v_1 == v_3 with eps = 0.8 * mitk::eps ");
120  MITK_TEST_CONDITION(!(mitk::Equal<VnlValueType, 7>(vnlVector_1, vnlVector_3, mitk::eps * 0.8f)),
121  "vnl_fixed : v_1 != v_3 with eps = 0.8 * mitk::eps ");
122 
123  // test scalar equality method
124  mitk::ScalarType scalar1 = 0.5689;
125  mitk::ScalarType scalar2 = scalar1 + mitk::eps * 1.01;
126  mitk::ScalarType scalar3 = scalar1;
127  mitk::ScalarType scalar4 = scalar1 + mitk::eps * 0.95;
128  MITK_TEST_CONDITION(mitk::Equal(scalar1, scalar1), "Test scalar equality using the same scalar with mitk::eps");
129  MITK_TEST_CONDITION(!mitk::Equal(scalar1, scalar2),
130  "Test scalar equality using the different scalars with a difference greater than mitk::eps");
131  MITK_TEST_CONDITION(mitk::Equal(scalar1, scalar3),
132  "Test scalar equality using the different scalars with a difference equal to mitk::eps");
133  MITK_TEST_CONDITION(mitk::Equal(scalar1, scalar4),
134  "Test scalar equality using the different scalars with a difference less than mitk::eps");
135 
136  // test matrix equality methods
137  vnl_matrix_fixed<mitk::ScalarType, 3, 3> vnlMatrix3x3_1;
138  vnlMatrix3x3_1(0, 0) = 1.1;
139  vnlMatrix3x3_1(0, 1) = 0.4;
140  vnlMatrix3x3_1(0, 2) = 5.3;
141  vnlMatrix3x3_1(1, 0) = 2.7;
142  vnlMatrix3x3_1(1, 1) = 3578.56418;
143  vnlMatrix3x3_1(1, 2) = 123.56;
144  vnlMatrix3x3_1(2, 0) = 546.89;
145  vnlMatrix3x3_1(2, 1) = 0.0001;
146  vnlMatrix3x3_1(2, 2) = 1.0;
147  vnl_matrix_fixed<mitk::ScalarType, 3, 3> vnlMatrix3x3_2;
148  vnlMatrix3x3_2(0, 0) = 1.1000009;
149  vnlMatrix3x3_2(0, 1) = 0.4000009;
150  vnlMatrix3x3_2(0, 2) = 5.3000009;
151  vnlMatrix3x3_2(1, 0) = 2.7000009;
152  vnlMatrix3x3_2(1, 1) = 3578.5641809;
153  vnlMatrix3x3_2(1, 2) = 123.5600009;
154  vnlMatrix3x3_2(2, 0) = 546.8900009;
155  vnlMatrix3x3_2(2, 1) = 0.0001009;
156  vnlMatrix3x3_2(2, 2) = 1.0000009;
157 
158  mitk::ScalarType epsilon = 0.000001;
159  MITK_TEST_CONDITION(mitk::MatrixEqualElementWise(vnlMatrix3x3_1, vnlMatrix3x3_1, mitk::eps),
160  "Test for matrix equality with given epsilon=mitk::eps and exactly the same matrix elements");
161  MITK_TEST_CONDITION(!mitk::MatrixEqualElementWise(vnlMatrix3x3_1, vnlMatrix3x3_2, 0.0),
162  "Test for matrix equality with given epsilon=0.0 and slightly different matrix elements");
163  MITK_TEST_CONDITION(mitk::MatrixEqualElementWise(vnlMatrix3x3_1, vnlMatrix3x3_2, epsilon),
164  "Test for matrix equality with given epsilon and slightly different matrix elements");
165  MITK_TEST_CONDITION(!mitk::MatrixEqualRMS(vnlMatrix3x3_1, vnlMatrix3x3_2, 0.0),
166  "Test for matrix equality with given epsilon=0.0 and slightly different matrix elements");
167  MITK_TEST_CONDITION(mitk::MatrixEqualRMS(vnlMatrix3x3_1, vnlMatrix3x3_2, epsilon),
168  "Test for matrix equality with given epsilon and slightly different matrix elements");
169 
170  MITK_TEST_END();
171 }
bool MatrixEqualElementWise(const vnl_matrix_fixed< TCoordRep, NRows, NCols > &matrix1, const vnl_matrix_fixed< TCoordRep, NRows, NCols > &matrix2, mitk::ScalarType epsilon=mitk::eps)
Check for element-wise matrix equality with a user defined accuracy.
Definition: mitkMatrix.h:144
vnl_vector< ScalarType > VnlVector
Definition: mitkVector.h:138
double ScalarType
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
#define MITK_TEST_CONDITION(COND, MSG)
bool MatrixEqualRMS(const vnl_matrix_fixed< TCoordRep, NRows, NCols > &matrix1, const vnl_matrix_fixed< TCoordRep, NRows, NCols > &matrix2, mitk::ScalarType epsilon=mitk::eps)
Check for matrix equality with a user defined accuracy. As an equality metric the root mean squared e...
Definition: mitkMatrix.h:100
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.
int mitkVectorTest(int, char *[])
MITKCORE_EXPORT const ScalarType eps
and MITK_TEST_END()