Medical Imaging Interaction Toolkit  2024.12.99-0da743f6
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 {
29  template <class TCoordRep, unsigned int NVectorDimension = 3>
30  class Vector : public itk::Vector<TCoordRep, NVectorDimension>
31  {
32  public:
36  explicit Vector<TCoordRep, NVectorDimension>() : itk::Vector<TCoordRep, NVectorDimension>() {}
41  : itk::Vector<TCoordRep, NVectorDimension>(r)
42  {
43  }
44 
47  {
48  itk::Vector<TCoordRep, NVectorDimension>::operator=(r);
49  return *this;
50  }
51 
55  Vector<TCoordRep, NVectorDimension>(const itk::Vector<TCoordRep, NVectorDimension> &r)
56  : itk::Vector<TCoordRep, NVectorDimension>(r)
57  {
58  }
59 
65  Vector<TCoordRep, NVectorDimension>(const TCoordRep r[NVectorDimension])
66  : itk::Vector<TCoordRep, NVectorDimension>(r)
67  {
68  }
69 
73  Vector<TCoordRep, NVectorDimension>(const TCoordRep &v) : itk::Vector<TCoordRep, NVectorDimension>(v) {}
78  Vector<TCoordRep, NVectorDimension>(const vnl_vector<TCoordRep> &vnlVector)
79  : itk::Vector<TCoordRep, NVectorDimension>()
80  {
81  if (vnlVector.size() != NVectorDimension)
82  mitkThrow() << "when constructing mitk::Vector from vnl_vector: sizes didn't match: mitk::Vector "
83  << NVectorDimension << "; vnl_vector " << vnlVector.size();
84 
85  for (unsigned int var = 0; (var < NVectorDimension) && (var < vnlVector.size()); ++var)
86  {
87  this->SetElement(var, vnlVector.get(var));
88  }
89  }
90 
94  Vector<TCoordRep, NVectorDimension>(const vnl_vector_fixed<TCoordRep, NVectorDimension> &vnlVectorFixed)
95  : itk::Vector<TCoordRep, NVectorDimension>()
96  {
97  for (unsigned int var = 0; var < NVectorDimension; ++var)
98  {
99  this->SetElement(var, vnlVectorFixed[var]);
100  }
101  }
102 
103  template <typename... Args,
104  typename = std::enable_if_t<(sizeof...(Args) == NVectorDimension)>>
105  explicit Vector(Args... args)
106  {
107  size_t i = 0;
108  ((this->GetDataPointer()[i++] = static_cast<TCoordRep>(args)), ...);
109  }
110 
117  template <typename ArrayType>
118  void FillVector(const ArrayType &array)
119  {
120  itk::FixedArray<TCoordRep, NVectorDimension> *thisP =
121  dynamic_cast<itk::FixedArray<TCoordRep, NVectorDimension> *>(this);
122  mitk::FillArray<ArrayType, TCoordRep, NVectorDimension>(*thisP, array);
123  }
124 
130  template <typename ArrayType>
131  void ToArray(ArrayType array) const
132  {
133  mitk::ToArray<ArrayType, TCoordRep, NVectorDimension>(array, *this);
134  }
135 
141  operator vnl_vector<TCoordRep>() const { return this->GetVnlVector(); }
142  }; // end mitk::Vector
143 
144  template <class TCoordRep, unsigned int NVectorDimension>
146  {
147  j = nlohmann::json::array();
148 
149  for (size_t i = 0; i < NVectorDimension; ++i)
150  j.push_back(v[i]);
151  }
152 
153  template <class TCoordRep, unsigned int NVectorDimension>
155  {
156  for (size_t i = 0; i < NVectorDimension; ++i)
157  j.at(i).get_to(v[i]);
158  }
159 
160  // convenience typedefs for often used mitk::Vector representations.
161 
165 
166  // other vector types used in MITK
167  typedef vnl_vector<ScalarType> VnlVector;
168 
169  // The equal methods to compare vectors for equality are below:
170 
180  template <typename TCoordRep, unsigned int NPointDimension>
181  inline bool Equal(const itk::Vector<TCoordRep, NPointDimension> &vector1,
182  const itk::Vector<TCoordRep, NPointDimension> &vector2,
183  TCoordRep eps = mitk::eps,
184  bool verbose = false)
185  {
186  bool isEqual = true;
187  typename itk::Vector<TCoordRep, NPointDimension>::VectorType diff = vector1 - vector2;
188  for (unsigned int i = 0; i < NPointDimension; i++)
189  {
190  if (DifferenceBiggerOrEqualEps(diff[i], eps))
191  {
192  isEqual = false;
193  break;
194  }
195  }
196 
197  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
198 
199  return isEqual;
200  }
201 
211  inline bool Equal(const mitk::VnlVector &vector1,
212  const mitk::VnlVector &vector2,
214  bool verbose = false)
215  {
216  bool isEqual = true;
217  mitk::VnlVector diff = vector1 - vector2;
218  for (unsigned int i = 0; i < diff.size(); i++)
219  {
220  if (DifferenceBiggerOrEqualEps(diff[i], eps))
221  {
222  isEqual = false;
223  break;
224  }
225  }
226 
227  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
228 
229  return isEqual;
230  }
231 
241  template <typename TCoordRep, unsigned int NPointDimension>
242  inline bool Equal(const vnl_vector_fixed<TCoordRep, NPointDimension> &vector1,
243  const vnl_vector_fixed<TCoordRep, NPointDimension> &vector2,
244  TCoordRep eps = mitk::eps,
245  bool verbose = false)
246  {
247  vnl_vector_fixed<TCoordRep, NPointDimension> diff = vector1 - vector2;
248  bool isEqual = true;
249  for (unsigned int i = 0; i < diff.size(); i++)
250  {
251  if (DifferenceBiggerOrEqualEps(diff[i], eps))
252  {
253  isEqual = false;
254  break;
255  }
256  }
257 
258  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
259 
260  return isEqual;
261  }
262 
263 } // end namespace mitk
264 
265 #endif
mitk::eps
const MITKCORE_EXPORT ScalarType eps
mitk::to_json
void to_json(nlohmann::json &j, const GenericLookupTable< T > &t)
Definition: mitkGenericLookupTable.h:98
mitk::Vector4D
Vector< ScalarType, 4 > Vector4D
Definition: mitkVector.h:164
mitk::Vector::Vector
Vector(Args... args)
Definition: mitkVector.h:105
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::from_json
void from_json(const nlohmann::json &, GenericLookupTable< T > &)
Definition: mitkGenericLookupTable.h:104
mitk::Vector::operator=
Vector< TCoordRep, NVectorDimension > & operator=(const Vector< TCoordRep, NVectorDimension > &r)
Definition: mitkVector.h:46
mitk::Vector2D
Vector< ScalarType, 2 > Vector2D
Definition: mitkVector.h:162
mitk::VnlVector
vnl_vector< ScalarType > VnlVector
Definition: mitkVector.h:167
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitkArray.h
mitkExceptionMacro.h
json
nlohmann::json json
Definition: mitkModelTestFixture.h:29
mitk::Vector
Definition: mitkVector.h:30
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:118
mitk::Vector3D
Vector< ScalarType, 3 > Vector3D
Definition: mitkVector.h:163
mitk::Vector::ToArray
void ToArray(ArrayType array) const
Definition: mitkVector.h:131
mitk::ScalarType
double ScalarType
Definition: mitkNumericConstants.h:20