Medical Imaging Interaction Toolkit  2026.06.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 #include <nlohmann/json.hpp>
26 
27 namespace mitk
28 {
37  template <class TCoordRep, unsigned int NVectorDimension = 3>
38  class Vector : public itk::Vector<TCoordRep, NVectorDimension>
39  {
40  public:
42  explicit Vector() : itk::Vector<TCoordRep, NVectorDimension>() {}
43 
46  : itk::Vector<TCoordRep, NVectorDimension>(r)
47  {
48  }
49 
52  {
53  itk::Vector<TCoordRep, NVectorDimension>::operator=(r);
54  return *this;
55  }
56 
60  Vector(const itk::Vector<TCoordRep, NVectorDimension> &r)
61  : itk::Vector<TCoordRep, NVectorDimension>(r)
62  {
63  }
64 
70  Vector(const TCoordRep r[NVectorDimension])
71  : itk::Vector<TCoordRep, NVectorDimension>(r)
72  {
73  }
74 
78  Vector(const TCoordRep &v) : itk::Vector<TCoordRep, NVectorDimension>(v) {}
83  Vector(const vnl_vector<TCoordRep> &vnlVector)
84  : itk::Vector<TCoordRep, NVectorDimension>()
85  {
86  if (vnlVector.size() != NVectorDimension)
87  mitkThrow() << "when constructing mitk::Vector from vnl_vector: sizes didn't match: mitk::Vector "
88  << NVectorDimension << "; vnl_vector " << vnlVector.size();
89 
90  for (unsigned int var = 0; (var < NVectorDimension) && (var < vnlVector.size()); ++var)
91  {
92  this->SetElement(var, vnlVector.get(var));
93  }
94  }
95 
99  Vector(const vnl_vector_fixed<TCoordRep, NVectorDimension> &vnlVectorFixed)
100  : itk::Vector<TCoordRep, NVectorDimension>()
101  {
102  for (unsigned int var = 0; var < NVectorDimension; ++var)
103  {
104  this->SetElement(var, vnlVectorFixed[var]);
105  }
106  }
107 
108  template <typename... Args,
109  typename = std::enable_if_t<(sizeof...(Args) == NVectorDimension)>>
110  explicit Vector(Args... args)
111  {
112  size_t i = 0;
113  ((this->GetDataPointer()[i++] = static_cast<TCoordRep>(args)), ...);
114  }
115 
123  template <typename ArrayType>
124  void FillVector(const ArrayType &array)
125  {
126  itk::FixedArray<TCoordRep, NVectorDimension> *thisP =
127  dynamic_cast<itk::FixedArray<TCoordRep, NVectorDimension> *>(this);
128  mitk::FillArray<ArrayType, TCoordRep, NVectorDimension>(*thisP, array);
129  }
130 
136  template <typename ArrayType>
137  void ToArray(ArrayType array) const
138  {
139  mitk::ToArray<ArrayType, TCoordRep, NVectorDimension>(array, *this);
140  }
141 
148  operator vnl_vector<TCoordRep>() const { return this->GetVnlVector(); }
149  }; // end mitk::Vector
150 
152  template <class TCoordRep, unsigned int NVectorDimension>
154  {
155  j = nlohmann::json::array();
156 
157  for (size_t i = 0; i < NVectorDimension; ++i)
158  j.push_back(v[i]);
159  }
160 
162  template <class TCoordRep, unsigned int NVectorDimension>
164  {
165  for (size_t i = 0; i < NVectorDimension; ++i)
166  j.at(i).get_to(v[i]);
167  }
168 
169  // convenience typedefs for often used mitk::Vector representations.
170 
174 
175  // other vector types used in MITK
176  typedef vnl_vector<ScalarType> VnlVector;
177 
178  // The equal methods to compare vectors for equality are below:
179 
190  template <typename TCoordRep, unsigned int NPointDimension>
191  inline bool Equal(const itk::Vector<TCoordRep, NPointDimension> &vector1,
192  const itk::Vector<TCoordRep, NPointDimension> &vector2,
193  TCoordRep eps = mitk::eps,
194  bool verbose = false)
195  {
196  bool isEqual = true;
197  typename itk::Vector<TCoordRep, NPointDimension>::VectorType diff = vector1 - vector2;
198  for (unsigned int i = 0; i < NPointDimension; i++)
199  {
200  if (DifferenceBiggerOrEqualEps(diff[i], eps))
201  {
202  isEqual = false;
203  break;
204  }
205  }
206 
207  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
208 
209  return isEqual;
210  }
211 
222  inline bool Equal(const mitk::VnlVector &vector1,
223  const mitk::VnlVector &vector2,
225  bool verbose = false)
226  {
227  bool isEqual = true;
228  mitk::VnlVector diff = vector1 - vector2;
229  for (unsigned int i = 0; i < diff.size(); i++)
230  {
231  if (DifferenceBiggerOrEqualEps(diff[i], eps))
232  {
233  isEqual = false;
234  break;
235  }
236  }
237 
238  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
239 
240  return isEqual;
241  }
242 
253  template <typename TCoordRep, unsigned int NPointDimension>
254  inline bool Equal(const vnl_vector_fixed<TCoordRep, NPointDimension> &vector1,
255  const vnl_vector_fixed<TCoordRep, NPointDimension> &vector2,
256  TCoordRep eps = mitk::eps,
257  bool verbose = false)
258  {
259  vnl_vector_fixed<TCoordRep, NPointDimension> diff = vector1 - vector2;
260  bool isEqual = true;
261  for (unsigned int i = 0; i < diff.size(); i++)
262  {
263  if (DifferenceBiggerOrEqualEps(diff[i], eps))
264  {
265  isEqual = false;
266  break;
267  }
268  }
269 
270  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
271 
272  return isEqual;
273  }
274 
275 } // end namespace mitk
276 
277 #endif
MITK vector type extending itk::Vector with additional constructors and conversions.
Definition: mitkVector.h:39
Vector(const mitk::Vector< TCoordRep, NVectorDimension > &r)
Copy constructor.
Definition: mitkVector.h:45
Vector(Args... args)
Definition: mitkVector.h:110
Vector< TCoordRep, NVectorDimension > & operator=(const Vector< TCoordRep, NVectorDimension > &r)
Definition: mitkVector.h:51
void FillVector(const ArrayType &array)
Copy elements from an array into this vector.
Definition: mitkVector.h:124
Vector(const itk::Vector< TCoordRep, NVectorDimension > &r)
Constructor to convert from itk::Vector to mitk::Vector.
Definition: mitkVector.h:60
Vector()
Default constructor.
Definition: mitkVector.h:42
Vector(const TCoordRep r[NVectorDimension])
Constructor to convert an array to mitk::Vector.
Definition: mitkVector.h:70
void ToArray(ArrayType array) const
Copy the values stored in this vector into an array.
Definition: mitkVector.h:137
Vector(const vnl_vector_fixed< TCoordRep, NVectorDimension > &vnlVectorFixed)
Constructor for vnl_vector_fixed.
Definition: mitkVector.h:99
Vector(const vnl_vector< TCoordRep > &vnlVector)
Constructor for vnl_vectors.
Definition: mitkVector.h:83
Vector(const TCoordRep &v)
Definition: mitkVector.h:78
#define mitkThrow()
Throw a generic mitk::Exception with source location information.
nlohmann::json json
Find image slices visible on a given plane.
MITKCORE_EXPORT const ScalarType eps
Epsilon value for floating point comparison (vnl_math::eps * 100).
void from_json(const nlohmann::json &, GenericLookupTable< T > &)
Deserialize a GenericLookupTable from JSON.
Vector< ScalarType, 2 > Vector2D
Definition: mitkVector.h:171
void ConditionalOutputOfDifference(ElementToOutput1 elem1, ElementToOutput2 elem2, mitk::ScalarType eps, bool verbose, bool isEqual)
Conditionally output the difference between two elements to the log.
Definition: mitkEqual.h:50
Vector< ScalarType, 4 > Vector4D
Definition: mitkVector.h:173
Vector< ScalarType, 3 > Vector3D
Definition: mitkVector.h:172
void to_json(nlohmann::json &j, const GenericLookupTable< T > &t)
Serialize a GenericLookupTable to JSON.
bool DifferenceBiggerOrEqualEps(DifferenceType diff, mitk::ScalarType epsilon=mitk::eps)
Check if the absolute difference is bigger than or equal to a given epsilon.
Definition: mitkEqual.h:32
vnl_vector< ScalarType > VnlVector
Definition: mitkVector.h:176
double ScalarType
Scalar type used throughout MITK for geometric computations.
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.