Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkVector.h
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 #ifndef MITKVECTOR_H_
18 #define MITKVECTOR_H_
19 
20 #include <itkVector.h>
21 #include <vnl/vnl_vector.h>
22 #include <vnl/vnl_vector_fixed.h>
23 
24 #include "mitkArray.h"
25 #include "mitkEqual.h"
26 #include "mitkExceptionMacro.h"
27 #include "mitkNumericConstants.h"
28 
29 namespace mitk
30 {
31  template <class TCoordRep, unsigned int NVectorDimension = 3>
32  class Vector : public itk::Vector<TCoordRep, NVectorDimension>
33  {
34  public:
38  explicit Vector<TCoordRep, NVectorDimension>() : itk::Vector<TCoordRep, NVectorDimension>() {}
43  : itk::Vector<TCoordRep, NVectorDimension>(r)
44  {
45  }
46 
50  Vector<TCoordRep, NVectorDimension>(const itk::Vector<TCoordRep, NVectorDimension> &r)
51  : itk::Vector<TCoordRep, NVectorDimension>(r)
52  {
53  }
54 
60  Vector<TCoordRep, NVectorDimension>(const TCoordRep r[NVectorDimension])
61  : itk::Vector<TCoordRep, NVectorDimension>(r)
62  {
63  }
64 
68  Vector<TCoordRep, NVectorDimension>(const TCoordRep &v) : itk::Vector<TCoordRep, NVectorDimension>(v) {}
73  Vector<TCoordRep, NVectorDimension>(const vnl_vector<TCoordRep> &vnlVector)
74  : itk::Vector<TCoordRep, NVectorDimension>()
75  {
76  if (vnlVector.size() != NVectorDimension)
77  mitkThrow() << "when constructing mitk::Vector from vnl_vector: sizes didn't match: mitk::Vector "
78  << NVectorDimension << "; vnl_vector " << vnlVector.size();
79 
80  for (unsigned int var = 0; (var < NVectorDimension) && (var < vnlVector.size()); ++var)
81  {
82  this->SetElement(var, vnlVector.get(var));
83  }
84  }
85 
89  Vector<TCoordRep, NVectorDimension>(const vnl_vector_fixed<TCoordRep, NVectorDimension> &vnlVectorFixed)
90  : itk::Vector<TCoordRep, NVectorDimension>()
91  {
92  for (unsigned int var = 0; var < NVectorDimension; ++var)
93  {
94  this->SetElement(var, vnlVectorFixed[var]);
95  }
96  };
97 
104  template <typename ArrayType>
105  void FillVector(const ArrayType &array)
106  {
107  itk::FixedArray<TCoordRep, NVectorDimension> *thisP =
108  dynamic_cast<itk::FixedArray<TCoordRep, NVectorDimension> *>(this);
109  mitk::FillArray<ArrayType, TCoordRep, NVectorDimension>(*thisP, array);
110  }
111 
117  template <typename ArrayType>
118  void ToArray(ArrayType array) const
119  {
120  mitk::ToArray<ArrayType, TCoordRep, NVectorDimension>(array, *this);
121  }
122 
128  operator vnl_vector<TCoordRep>() const { return this->GetVnlVector(); }
129  }; // end mitk::Vector
130 
131  // convenience typedefs for often used mitk::Vector representations.
132 
136 
137  // other vector types used in MITK
138  typedef vnl_vector<ScalarType> VnlVector;
139 
140  // The equal methods to compare vectors for equality are below:
141 
151  template <typename TCoordRep, unsigned int NPointDimension>
152  inline bool Equal(const itk::Vector<TCoordRep, NPointDimension> &vector1,
153  const itk::Vector<TCoordRep, NPointDimension> &vector2,
154  TCoordRep eps = mitk::eps,
155  bool verbose = false)
156  {
157  bool isEqual = true;
158  typename itk::Vector<TCoordRep, NPointDimension>::VectorType diff = vector1 - vector2;
159  for (unsigned int i = 0; i < NPointDimension; i++)
160  {
161  if (DifferenceBiggerOrEqualEps(diff[i], eps))
162  {
163  isEqual = false;
164  break;
165  }
166  }
167 
168  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
169 
170  return isEqual;
171  }
172 
182  inline bool Equal(const mitk::VnlVector &vector1,
183  const mitk::VnlVector &vector2,
185  bool verbose = false)
186  {
187  bool isEqual = true;
188  mitk::VnlVector diff = vector1 - vector2;
189  for (unsigned int i = 0; i < diff.size(); i++)
190  {
191  if (DifferenceBiggerOrEqualEps(diff[i], eps))
192  {
193  isEqual = false;
194  break;
195  }
196  }
197 
198  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
199 
200  return isEqual;
201  }
202 
212  template <typename TCoordRep, unsigned int NPointDimension>
213  inline bool Equal(const vnl_vector_fixed<TCoordRep, NPointDimension> &vector1,
214  const vnl_vector_fixed<TCoordRep, NPointDimension> &vector2,
215  TCoordRep eps = mitk::eps,
216  bool verbose = false)
217  {
218  vnl_vector_fixed<TCoordRep, NPointDimension> diff = vector1 - vector2;
219  bool isEqual = true;
220  for (unsigned int i = 0; i < diff.size(); i++)
221  {
222  if (DifferenceBiggerOrEqualEps(diff[i], eps))
223  {
224  isEqual = false;
225  break;
226  }
227  }
228 
229  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
230 
231  return isEqual;
232  }
233 
234 } // end namespace mitk
235 
236 #endif /* MITKVECTOR_H_ */
void ConditionalOutputOfDifference(ElementToOutput1 elem1, ElementToOutput2 elem2, mitk::ScalarType eps, bool verbose, bool isEqual)
Definition: mitkEqual.h:42
void ToArray(ArrayType array) const
Definition: mitkVector.h:118
vnl_vector< ScalarType > VnlVector
Definition: mitkVector.h:138
double ScalarType
DataCollection - Class to facilitate loading/accessing structured data.
Vector< ScalarType, 4 > Vector4D
Definition: mitkVector.h:135
itk::Vector< float, 3 > VectorType
void FillVector(const ArrayType &array)
Definition: mitkVector.h:105
Vector< ScalarType, 2 > Vector2D
Definition: mitkVector.h:133
Vector< ScalarType, 3 > Vector3D
Definition: mitkVector.h:134
#define mitkThrow()
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.
MITKCORE_EXPORT const ScalarType eps
bool DifferenceBiggerOrEqualEps(DifferenceType diff, mitk::ScalarType epsilon=mitk::eps)
Definition: mitkEqual.h:26