ITK  5.4.0
Insight Toolkit
itkVector.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkVector_h
19 #define itkVector_h
20 
21 #include "itkFixedArray.h"
22 
23 #include "vnl/vnl_vector_ref.h" // GetVnlVector method return
24 
25 namespace itk
26 {
61 template <typename T, unsigned int VVectorDimension = 3>
62 class ITK_TEMPLATE_EXPORT Vector : public FixedArray<T, VVectorDimension>
63 {
64 public:
66  using Self = Vector;
68 
71  using ValueType = T;
73 
75  static constexpr unsigned int Dimension = VVectorDimension;
76 
78  using VectorType = Self;
79 
81  using ComponentType = T;
82 
85 
87  static unsigned int
89  {
90  return VVectorDimension;
91  }
92 
94  void
95  SetVnlVector(const vnl_vector<T> &);
96 
98  vnl_vector_ref<T>
99  GetVnlVector();
100 
102  vnl_vector<T>
103  GetVnlVector() const;
104 
107  Vector() = default;
108 
109 #if !defined(ITK_LEGACY_REMOVE)
110 
114  Vector(const ValueType & r);
115 #else
116 
119  explicit Vector(const ValueType & r);
120 
122  Vector(std::nullptr_t) = delete;
123 #endif
124 
126  template <typename TVectorValueType>
128  : BaseArray(r)
129  {}
131  : BaseArray(r)
132  {}
133  template <typename TVectorValueType>
134  Vector(const TVectorValueType r[Dimension])
135  : BaseArray(r)
136  {}
140  explicit Vector(const std::array<ValueType, VVectorDimension> & stdArray)
141  : BaseArray(stdArray)
142  {}
143 
145  template <typename TVectorValueType>
146  Vector &
148  {
149  BaseArray::operator=(r);
150  return *this;
151  }
154  Vector &
155  operator=(const ValueType r[VVectorDimension]);
156 
158  template <typename Tt>
159  inline const Self &
160  operator*=(const Tt & value)
161  {
162  for (unsigned int i = 0; i < VVectorDimension; ++i)
163  {
164  (*this)[i] = static_cast<ValueType>((*this)[i] * value);
165  }
166  return *this;
167  }
171  template <typename Tt>
172  inline const Self &
173  operator/=(const Tt & value)
174  {
175  for (unsigned int i = 0; i < VVectorDimension; ++i)
176  {
177  (*this)[i] = static_cast<ValueType>((*this)[i] / value);
178  }
179  return *this;
180  }
184  const Self &
185  operator+=(const Self & vec);
186 
188  const Self &
189  operator-=(const Self & vec);
190 
193  Self
194  operator-() const;
195 
197  Self
198  operator+(const Self & vec) const;
199 
201  Self
202  operator-(const Self & vec) const;
203 
206  ValueType operator*(const Self & other) const;
207 
210  inline Self operator*(const ValueType & value) const
211  {
212  Self result;
213 
214  for (unsigned int i = 0; i < VVectorDimension; ++i)
215  {
216  result[i] = static_cast<ValueType>((*this)[i] * value);
217  }
218  return result;
219  }
220 
223  template <typename Tt>
224  inline Self
225  operator/(const Tt & value) const
226  {
227  Self result;
228 
229  for (unsigned int i = 0; i < VVectorDimension; ++i)
230  {
231  result[i] = static_cast<ValueType>((*this)[i] / value);
232  }
233  return result;
234  }
235 
240  bool
241  operator==(const Self & v) const
242  {
243  return Superclass::operator==(v);
244  }
245 
246  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self);
247 
249  RealValueType
250  GetNorm() const;
251 
253  RealValueType
254  GetSquaredNorm() const;
255 
257  static unsigned int
259  {
260  return VVectorDimension;
261  }
262 
265  RealValueType
266  Normalize();
267 
268  void
269  SetNthComponent(int c, const ComponentType & v)
270  {
271  this->operator[](c) = v;
272  }
273 
276  template <typename TCoordRepB>
277  void
279  {
280  for (unsigned int i = 0; i < VVectorDimension; ++i)
281  {
282  (*this)[i] = static_cast<T>(pa[i]);
283  }
284  }
287  template <typename TCoordRepB>
289  {
291  for (unsigned int i = 0; i < VVectorDimension; ++i)
292  {
293  r[i] = static_cast<TCoordRepB>((*this)[i]);
294  }
295  return r;
296  }
297 };
298 
301 template <typename T, unsigned int VVectorDimension>
303 {
304  return v.operator*(scalar);
305 }
306 
308 template <typename T, unsigned int VVectorDimension>
309 std::ostream &
310 operator<<(std::ostream & os, const Vector<T, VVectorDimension> & vct);
311 
313 template <typename T, unsigned int VVectorDimension>
314 std::istream &
315 operator>>(std::istream & is, Vector<T, VVectorDimension> & vct);
316 
317 ITKCommon_EXPORT Vector<double, 3>
318  CrossProduct(const Vector<double, 3> &, const Vector<double, 3> &);
319 
320 ITKCommon_EXPORT Vector<float, 3>
321  CrossProduct(const Vector<float, 3> &, const Vector<float, 3> &);
322 
323 ITKCommon_EXPORT Vector<int, 3>
324  CrossProduct(const Vector<int, 3> &, const Vector<int, 3> &);
325 
326 
327 template <typename T, unsigned int VVectorDimension>
328 inline void
330 {
331  a.swap(b);
332 }
333 
334 
336 template <typename TValue, typename... TVariadic>
337 auto
338 MakeVector(const TValue firstValue, const TVariadic... otherValues)
339 {
340  // Assert that the other values have the same type as the first value.
341  const auto assertSameType = [](const auto value) {
342  static_assert(std::is_same<decltype(value), const TValue>::value, "Each value must have the same type!");
343  return true;
344  };
345  const bool assertions[] = { true, assertSameType(otherValues)... };
346  (void)assertions;
347  (void)assertSameType;
350  constexpr unsigned int dimension{ 1 + sizeof...(TVariadic) };
351  const std::array<TValue, dimension> stdArray{ { firstValue, otherValues... } };
352  return Vector<TValue, dimension>{ stdArray };
353 }
354 
355 } // end namespace itk
356 
357 #ifndef ITK_MANUAL_INSTANTIATION
358 # include "itkVector.hxx"
359 #endif
360 
361 #endif
itk::Vector::operator/=
const Self & operator/=(const Tt &value)
Definition: itkVector.h:173
itk::Vector::CastFrom
void CastFrom(const Vector< TCoordRepB, VVectorDimension > &pa)
Definition: itkVector.h:278
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
itk::Vector< double, Self::ImageDimension >::ComponentType
double ComponentType
Definition: itkVector.h:81
itk::Vector::operator*=
const Self & operator*=(const Tt &value)
Definition: itkVector.h:160
itk::operator*
CovariantVector< T, VVectorDimension > operator*(const T &scalar, const CovariantVector< T, VVectorDimension > &v)
Definition: itkCovariantVector.h:268
Self
AddImageFilter Self
Definition: itkAddImageFilter.h:88
itk::MakeVector
auto MakeVector(const TValue firstValue, const TVariadic... otherValues)
Definition: itkVector.h:338
itk::Vector::operator*
Self operator*(const ValueType &value) const
Definition: itkVector.h:210
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:242
itk::Vector
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
itk::operator-
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:672
itk::Vector::Vector
Vector(const std::array< ValueType, VVectorDimension > &stdArray)
Definition: itkVector.h:140
itk::Vector::Vector
Vector(const ValueType r[Dimension])
Definition: itkVector.h:130
itk::Vector::operator/
Self operator/(const Tt &value) const
Definition: itkVector.h:225
itk::Vector< double, Self::ImageDimension >::RealValueType
typename NumericTraits< ValueType >::RealType RealValueType
Definition: itkVector.h:72
itk::Vector::GetNumberOfComponents
static unsigned int GetNumberOfComponents()
Definition: itkVector.h:258
itk::Vector::SetNthComponent
void SetNthComponent(int c, const ComponentType &v)
Definition: itkVector.h:269
itk::CrossProduct
ITKCommon_EXPORT void CrossProduct(CovariantVector< double, 3 > &, const Vector< double, 3 > &, const Vector< double, 3 > &)
itkFixedArray.h
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:545
itk::FixedArray
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:53
itk::Vector::GetVectorDimension
static unsigned int GetVectorDimension()
Definition: itkVector.h:88
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Vector::operator=
Vector & operator=(const Vector< TVectorValueType, VVectorDimension > &r)
Definition: itkVector.h:147
itk::FixedArray< T, VVectorDimension >::swap
void swap(FixedArray &other)
Definition: itkFixedArray.h:416
itk::operator+
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:654
itk::Vector::Vector
Vector(const Vector< TVectorValueType, VVectorDimension > &r)
Definition: itkVector.h:127
itk::Vector::operator==
bool operator==(const Self &v) const
Definition: itkVector.h:241
itk::Vector::Vector
Vector(const TVectorValueType r[Dimension])
Definition: itkVector.h:134
itk::NumericTraits::RealType
double RealType
Definition: itkNumericTraits.h:85
AddImageFilter
Definition: itkAddImageFilter.h:80
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itk::operator>>
std::istream & operator>>(std::istream &is, Point< T, VPointDimension > &vct)
itk::FixedArray< double, VVectorDimension >::ValueType
double ValueType
Definition: itkFixedArray.h:63