Medical Imaging Interaction Toolkit  2018.4.99-663e373e
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 (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 #ifndef MITKVECTOR_H_
14 #define MITKVECTOR_H_
15 
16 #include <itkVector.h>
17 #include <vnl/vnl_vector.h>
18 #include <vnl/vnl_vector_fixed.h>
19 
20 #include "mitkArray.h"
21 #include "mitkEqual.h"
22 #include "mitkExceptionMacro.h"
23 #include "mitkNumericConstants.h"
24 
25 namespace mitk
26 {
27  template <class TCoordRep, unsigned int NVectorDimension = 3>
28  class Vector : public itk::Vector<TCoordRep, NVectorDimension>
29  {
30  public:
34  explicit Vector<TCoordRep, NVectorDimension>() : itk::Vector<TCoordRep, NVectorDimension>() {}
39  : itk::Vector<TCoordRep, NVectorDimension>(r)
40  {
41  }
42 
46  Vector<TCoordRep, NVectorDimension>(const itk::Vector<TCoordRep, NVectorDimension> &r)
47  : itk::Vector<TCoordRep, NVectorDimension>(r)
48  {
49  }
50 
56  Vector<TCoordRep, NVectorDimension>(const TCoordRep r[NVectorDimension])
57  : itk::Vector<TCoordRep, NVectorDimension>(r)
58  {
59  }
60 
64  Vector<TCoordRep, NVectorDimension>(const TCoordRep &v) : itk::Vector<TCoordRep, NVectorDimension>(v) {}
69  Vector<TCoordRep, NVectorDimension>(const vnl_vector<TCoordRep> &vnlVector)
70  : itk::Vector<TCoordRep, NVectorDimension>()
71  {
72  if (vnlVector.size() != NVectorDimension)
73  mitkThrow() << "when constructing mitk::Vector from vnl_vector: sizes didn't match: mitk::Vector "
74  << NVectorDimension << "; vnl_vector " << vnlVector.size();
75 
76  for (unsigned int var = 0; (var < NVectorDimension) && (var < vnlVector.size()); ++var)
77  {
78  this->SetElement(var, vnlVector.get(var));
79  }
80  }
81 
85  Vector<TCoordRep, NVectorDimension>(const vnl_vector_fixed<TCoordRep, NVectorDimension> &vnlVectorFixed)
86  : itk::Vector<TCoordRep, NVectorDimension>()
87  {
88  for (unsigned int var = 0; var < NVectorDimension; ++var)
89  {
90  this->SetElement(var, vnlVectorFixed[var]);
91  }
92  };
93 
100  template <typename ArrayType>
101  void FillVector(const ArrayType &array)
102  {
103  itk::FixedArray<TCoordRep, NVectorDimension> *thisP =
104  dynamic_cast<itk::FixedArray<TCoordRep, NVectorDimension> *>(this);
105  mitk::FillArray<ArrayType, TCoordRep, NVectorDimension>(*thisP, array);
106  }
107 
113  template <typename ArrayType>
114  void ToArray(ArrayType array) const
115  {
116  mitk::ToArray<ArrayType, TCoordRep, NVectorDimension>(array, *this);
117  }
118 
124  operator vnl_vector<TCoordRep>() const { return this->GetVnlVector(); }
125  }; // end mitk::Vector
126 
127  // convenience typedefs for often used mitk::Vector representations.
128 
132 
133  // other vector types used in MITK
134  typedef vnl_vector<ScalarType> VnlVector;
135 
136  // The equal methods to compare vectors for equality are below:
137 
147  template <typename TCoordRep, unsigned int NPointDimension>
148  inline bool Equal(const itk::Vector<TCoordRep, NPointDimension> &vector1,
149  const itk::Vector<TCoordRep, NPointDimension> &vector2,
150  TCoordRep eps = mitk::eps,
151  bool verbose = false)
152  {
153  bool isEqual = true;
154  typename itk::Vector<TCoordRep, NPointDimension>::VectorType diff = vector1 - vector2;
155  for (unsigned int i = 0; i < NPointDimension; i++)
156  {
157  if (DifferenceBiggerOrEqualEps(diff[i], eps))
158  {
159  isEqual = false;
160  break;
161  }
162  }
163 
164  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
165 
166  return isEqual;
167  }
168 
178  inline bool Equal(const mitk::VnlVector &vector1,
179  const mitk::VnlVector &vector2,
181  bool verbose = false)
182  {
183  bool isEqual = true;
184  mitk::VnlVector diff = vector1 - vector2;
185  for (unsigned int i = 0; i < diff.size(); i++)
186  {
187  if (DifferenceBiggerOrEqualEps(diff[i], eps))
188  {
189  isEqual = false;
190  break;
191  }
192  }
193 
194  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
195 
196  return isEqual;
197  }
198 
208  template <typename TCoordRep, unsigned int NPointDimension>
209  inline bool Equal(const vnl_vector_fixed<TCoordRep, NPointDimension> &vector1,
210  const vnl_vector_fixed<TCoordRep, NPointDimension> &vector2,
211  TCoordRep eps = mitk::eps,
212  bool verbose = false)
213  {
214  vnl_vector_fixed<TCoordRep, NPointDimension> diff = vector1 - vector2;
215  bool isEqual = true;
216  for (unsigned int i = 0; i < diff.size(); i++)
217  {
218  if (DifferenceBiggerOrEqualEps(diff[i], eps))
219  {
220  isEqual = false;
221  break;
222  }
223  }
224 
225  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
226 
227  return isEqual;
228  }
229 
230 } // end namespace mitk
231 
232 #endif /* MITKVECTOR_H_ */
void ConditionalOutputOfDifference(ElementToOutput1 elem1, ElementToOutput2 elem2, mitk::ScalarType eps, bool verbose, bool isEqual)
Definition: mitkEqual.h:54
MITKCORE_EXPORT const ScalarType eps
vnl_vector< ScalarType > VnlVector
Definition: mitkVector.h:134
double ScalarType
DataCollection - Class to facilitate loading/accessing structured data.
Vector< ScalarType, 4 > Vector4D
Definition: mitkVector.h:131
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.
void FillVector(const ArrayType &array)
Definition: mitkVector.h:101
Vector< ScalarType, 2 > Vector2D
Definition: mitkVector.h:129
Vector< ScalarType, 3 > Vector3D
Definition: mitkVector.h:130
#define mitkThrow()
bool DifferenceBiggerOrEqualEps(DifferenceType diff, mitk::ScalarType epsilon=mitk::eps)
Definition: mitkEqual.h:38
void ToArray(ArrayType array) const
Definition: mitkVector.h:114