Medical Imaging Interaction Toolkit  2023.12.00
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
109  template <typename ArrayType>
110  void FillVector(const ArrayType &array)
111  {
112  itk::FixedArray<TCoordRep, NVectorDimension> *thisP =
113  dynamic_cast<itk::FixedArray<TCoordRep, NVectorDimension> *>(this);
114  mitk::FillArray<ArrayType, TCoordRep, NVectorDimension>(*thisP, array);
115  }
116 
122  template <typename ArrayType>
123  void ToArray(ArrayType array) const
124  {
125  mitk::ToArray<ArrayType, TCoordRep, NVectorDimension>(array, *this);
126  }
127 
133  operator vnl_vector<TCoordRep>() const { return this->GetVnlVector(); }
134  }; // end mitk::Vector
135 
136  template <class TCoordRep, unsigned int NVectorDimension>
138  {
139  j = nlohmann::json::array();
140 
141  for (size_t i = 0; i < NVectorDimension; ++i)
142  j.push_back(v[i]);
143  }
144 
145  template <class TCoordRep, unsigned int NVectorDimension>
147  {
148  for (size_t i = 0; i < NVectorDimension; ++i)
149  j.at(i).get_to(v[i]);
150  }
151 
152  // convenience typedefs for often used mitk::Vector representations.
153 
157 
158  // other vector types used in MITK
159  typedef vnl_vector<ScalarType> VnlVector;
160 
161  // The equal methods to compare vectors for equality are below:
162 
172  template <typename TCoordRep, unsigned int NPointDimension>
173  inline bool Equal(const itk::Vector<TCoordRep, NPointDimension> &vector1,
174  const itk::Vector<TCoordRep, NPointDimension> &vector2,
175  TCoordRep eps = mitk::eps,
176  bool verbose = false)
177  {
178  bool isEqual = true;
179  typename itk::Vector<TCoordRep, NPointDimension>::VectorType diff = vector1 - vector2;
180  for (unsigned int i = 0; i < NPointDimension; i++)
181  {
182  if (DifferenceBiggerOrEqualEps(diff[i], eps))
183  {
184  isEqual = false;
185  break;
186  }
187  }
188 
189  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
190 
191  return isEqual;
192  }
193 
203  inline bool Equal(const mitk::VnlVector &vector1,
204  const mitk::VnlVector &vector2,
206  bool verbose = false)
207  {
208  bool isEqual = true;
209  mitk::VnlVector diff = vector1 - vector2;
210  for (unsigned int i = 0; i < diff.size(); i++)
211  {
212  if (DifferenceBiggerOrEqualEps(diff[i], eps))
213  {
214  isEqual = false;
215  break;
216  }
217  }
218 
219  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
220 
221  return isEqual;
222  }
223 
233  template <typename TCoordRep, unsigned int NPointDimension>
234  inline bool Equal(const vnl_vector_fixed<TCoordRep, NPointDimension> &vector1,
235  const vnl_vector_fixed<TCoordRep, NPointDimension> &vector2,
236  TCoordRep eps = mitk::eps,
237  bool verbose = false)
238  {
239  vnl_vector_fixed<TCoordRep, NPointDimension> diff = vector1 - vector2;
240  bool isEqual = true;
241  for (unsigned int i = 0; i < diff.size(); i++)
242  {
243  if (DifferenceBiggerOrEqualEps(diff[i], eps))
244  {
245  isEqual = false;
246  break;
247  }
248  }
249 
250  ConditionalOutputOfDifference(vector1, vector2, eps, verbose, isEqual);
251 
252  return isEqual;
253  }
254 
255 } // end namespace mitk
256 
257 #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:156
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:154
mitk::VnlVector
vnl_vector< ScalarType > VnlVector
Definition: mitkVector.h:159
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:110
mitk::Vector3D
Vector< ScalarType, 3 > Vector3D
Definition: mitkVector.h:155
mitk::Vector::ToArray
void ToArray(ArrayType array) const
Definition: mitkVector.h:123
mitk::ScalarType
double ScalarType
Definition: mitkNumericConstants.h:20