Medical Imaging Interaction Toolkit  2023.04.00
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 
45  {
46  itk::Vector<TCoordRep, NVectorDimension>::operator=(r);
47  return *this;
48  }
49 
53  Vector<TCoordRep, NVectorDimension>(const itk::Vector<TCoordRep, NVectorDimension> &r)
54  : itk::Vector<TCoordRep, NVectorDimension>(r)
55  {
56  }
57 
63  Vector<TCoordRep, NVectorDimension>(const TCoordRep r[NVectorDimension])
64  : itk::Vector<TCoordRep, NVectorDimension>(r)
65  {
66  }
67 
71  Vector<TCoordRep, NVectorDimension>(const TCoordRep &v) : itk::Vector<TCoordRep, NVectorDimension>(v) {}
76  Vector<TCoordRep, NVectorDimension>(const vnl_vector<TCoordRep> &vnlVector)
77  : itk::Vector<TCoordRep, NVectorDimension>()
78  {
79  if (vnlVector.size() != NVectorDimension)
80  mitkThrow() << "when constructing mitk::Vector from vnl_vector: sizes didn't match: mitk::Vector "
81  << NVectorDimension << "; vnl_vector " << vnlVector.size();
82 
83  for (unsigned int var = 0; (var < NVectorDimension) && (var < vnlVector.size()); ++var)
84  {
85  this->SetElement(var, vnlVector.get(var));
86  }
87  }
88 
92  Vector<TCoordRep, NVectorDimension>(const vnl_vector_fixed<TCoordRep, NVectorDimension> &vnlVectorFixed)
93  : itk::Vector<TCoordRep, NVectorDimension>()
94  {
95  for (unsigned int var = 0; var < NVectorDimension; ++var)
96  {
97  this->SetElement(var, vnlVectorFixed[var]);
98  }
99  };
100 
107  template <typename ArrayType>
108  void FillVector(const ArrayType &array)
109  {
110  itk::FixedArray<TCoordRep, NVectorDimension> *thisP =
111  dynamic_cast<itk::FixedArray<TCoordRep, NVectorDimension> *>(this);
112  mitk::FillArray<ArrayType, TCoordRep, NVectorDimension>(*thisP, array);
113  }
114 
120  template <typename ArrayType>
121  void ToArray(ArrayType array) const
122  {
123  mitk::ToArray<ArrayType, TCoordRep, NVectorDimension>(array, *this);
124  }
125 
131  operator vnl_vector<TCoordRep>() const { return this->GetVnlVector(); }
132  }; // end mitk::Vector
133 
134  // convenience typedefs for often used mitk::Vector representations.
135 
139 
140  // other vector types used in MITK
141  typedef vnl_vector<ScalarType> VnlVector;
142 
143  // The equal methods to compare vectors for equality are below:
144 
154  template <typename TCoordRep, unsigned int NPointDimension>
155  inline bool Equal(const itk::Vector<TCoordRep, NPointDimension> &vector1,
156  const itk::Vector<TCoordRep, NPointDimension> &vector2,
157  TCoordRep eps = mitk::eps,
158  bool verbose = false)
159  {
160  bool isEqual = true;
161  typename itk::Vector<TCoordRep, NPointDimension>::VectorType diff = vector1 - vector2;
162  for (unsigned int i = 0; i < NPointDimension; i++)
163  {
164  if (DifferenceBiggerOrEqualEps(diff[i], eps))
165  {
166  isEqual = false;
167  break;
168  }
169  }
170 
171  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
172 
173  return isEqual;
174  }
175 
185  inline bool Equal(const mitk::VnlVector &vector1,
186  const mitk::VnlVector &vector2,
188  bool verbose = false)
189  {
190  bool isEqual = true;
191  mitk::VnlVector diff = vector1 - vector2;
192  for (unsigned int i = 0; i < diff.size(); i++)
193  {
194  if (DifferenceBiggerOrEqualEps(diff[i], eps))
195  {
196  isEqual = false;
197  break;
198  }
199  }
200 
201  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
202 
203  return isEqual;
204  }
205 
215  template <typename TCoordRep, unsigned int NPointDimension>
216  inline bool Equal(const vnl_vector_fixed<TCoordRep, NPointDimension> &vector1,
217  const vnl_vector_fixed<TCoordRep, NPointDimension> &vector2,
218  TCoordRep eps = mitk::eps,
219  bool verbose = false)
220  {
221  vnl_vector_fixed<TCoordRep, NPointDimension> diff = vector1 - vector2;
222  bool isEqual = true;
223  for (unsigned int i = 0; i < diff.size(); i++)
224  {
225  if (DifferenceBiggerOrEqualEps(diff[i], eps))
226  {
227  isEqual = false;
228  break;
229  }
230  }
231 
232  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
233 
234  return isEqual;
235  }
236 
237 } // end namespace mitk
238 
239 #endif
mitk::eps
const MITKCORE_EXPORT ScalarType eps
mitk::Vector4D
Vector< ScalarType, 4 > Vector4D
Definition: mitkVector.h:138
mitk::Equal
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.
mitk::Vector::operator=
Vector< TCoordRep, NVectorDimension > & operator=(const Vector< TCoordRep, NVectorDimension > &r)
Definition: mitkVector.h:44
mitk::Vector2D
Vector< ScalarType, 2 > Vector2D
Definition: mitkVector.h:136
mitk::VnlVector
vnl_vector< ScalarType > VnlVector
Definition: mitkVector.h:141
mitk
DataCollection - Class to facilitate loading/accessing structured data.
Definition: RenderingTests.dox:1
mitkArray.h
mitkExceptionMacro.h
mitk::Vector
Definition: mitkVector.h:28
mitkThrow
#define mitkThrow()
Definition: mitkExceptionMacro.h:27
mitkNumericConstants.h
mitkEqual.h
itk
SET FUNCTIONS.
Definition: itkIntelligentBinaryClosingFilter.h:30
mitk::DifferenceBiggerOrEqualEps
bool DifferenceBiggerOrEqualEps(DifferenceType diff, mitk::ScalarType epsilon=mitk::eps)
Definition: mitkEqual.h:32
mitk::ConditionalOutputOfDifference
void ConditionalOutputOfDifference(ElementToOutput1 elem1, ElementToOutput2 elem2, mitk::ScalarType eps, bool verbose, bool isEqual)
Definition: mitkEqual.h:48
mitk::Vector::FillVector
void FillVector(const ArrayType &array)
Definition: mitkVector.h:108
mitk::Vector3D
Vector< ScalarType, 3 > Vector3D
Definition: mitkVector.h:137
mitk::Vector::ToArray
void ToArray(ArrayType array) const
Definition: mitkVector.h:121
mitk::ScalarType
double ScalarType
Definition: mitkNumericConstants.h:20