ITK  6.0.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 
95  void
96  SetVnlVector(const vnl_vector<T> &);
97 
99  vnl_vector_ref<T>
100  GetVnlVector();
101 
103  vnl_vector<T>
104  GetVnlVector() const;
105 
108  Vector() = default;
109 
110 #if !defined(ITK_LEGACY_REMOVE)
111 
115  Vector(const ValueType & r);
116 #else
117 
120  explicit Vector(const ValueType & r);
121 
123  Vector(std::nullptr_t) = delete;
124 #endif
125 
127  template <typename TVectorValueType>
129  : BaseArray(r)
130  {}
132  : BaseArray(r)
133  {}
134  template <typename TVectorValueType>
135  Vector(const TVectorValueType r[Dimension])
136  : BaseArray(r)
137  {}
141  explicit Vector(const std::array<ValueType, VVectorDimension> & stdArray)
142  : BaseArray(stdArray)
143  {}
144 
146  template <typename TVectorValueType>
147  Vector &
149  {
150  BaseArray::operator=(r);
151  return *this;
152  }
155  Vector &
156  operator=(const ValueType r[VVectorDimension]);
157 
159  template <typename Tt>
160  inline const Self &
161  operator*=(const Tt & value)
162  {
163  for (unsigned int i = 0; i < VVectorDimension; ++i)
164  {
165  (*this)[i] = static_cast<ValueType>((*this)[i] * value);
166  }
167  return *this;
168  }
172  template <typename Tt>
173  inline const Self &
174  operator/=(const Tt & value)
175  {
176  for (unsigned int i = 0; i < VVectorDimension; ++i)
177  {
178  (*this)[i] = static_cast<ValueType>((*this)[i] / value);
179  }
180  return *this;
181  }
185  const Self &
186  operator+=(const Self & vec);
187 
189  const Self &
190  operator-=(const Self & vec);
191 
194  Self
195  operator-() const;
196 
198  Self
199  operator+(const Self & vec) const;
200 
202  Self
203  operator-(const Self & vec) const;
204 
207  ValueType
208  operator*(const Self & other) const;
209 
212  inline Self
213  operator*(const ValueType & value) const
214  {
215  Self result;
216 
217  for (unsigned int i = 0; i < VVectorDimension; ++i)
218  {
219  result[i] = static_cast<ValueType>((*this)[i] * value);
220  }
221  return result;
222  }
223 
226  template <typename Tt>
227  inline Self
228  operator/(const Tt & value) const
229  {
230  Self result;
231 
232  for (unsigned int i = 0; i < VVectorDimension; ++i)
233  {
234  result[i] = static_cast<ValueType>((*this)[i] / value);
235  }
236  return result;
237  }
238 
243  bool
244  operator==(const Self & v) const
245  {
246  return Superclass::operator==(v);
247  }
248 
249  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self);
250 
252  RealValueType
253  GetNorm() const;
254 
256  RealValueType
257  GetSquaredNorm() const;
258 
260  static unsigned int
262  {
263  return VVectorDimension;
264  }
265 
268  RealValueType
269  Normalize();
270 
271  void
272  SetNthComponent(int c, const ComponentType & v)
273  {
274  this->operator[](c) = v;
275  }
276 
279  template <typename TCoordinateB>
280  void
282  {
283  for (unsigned int i = 0; i < VVectorDimension; ++i)
284  {
285  (*this)[i] = static_cast<T>(pa[i]);
286  }
287  }
290  template <typename TCoordinateB>
292  {
294  for (unsigned int i = 0; i < VVectorDimension; ++i)
295  {
296  r[i] = static_cast<TCoordinateB>((*this)[i]);
297  }
298  return r;
299  }
300 };
301 
304 template <typename T, unsigned int VVectorDimension>
305 inline Vector<T, VVectorDimension>
306 operator*(const T & scalar, const Vector<T, VVectorDimension> & v)
307 {
308  return v.operator*(scalar);
309 }
310 
312 template <typename T, unsigned int VVectorDimension>
313 std::ostream &
314 operator<<(std::ostream & os, const Vector<T, VVectorDimension> & vct);
315 
317 template <typename T, unsigned int VVectorDimension>
318 std::istream &
319 operator>>(std::istream & is, Vector<T, VVectorDimension> & vct);
320 
321 ITKCommon_EXPORT Vector<double, 3>
322  CrossProduct(const Vector<double, 3> &, const Vector<double, 3> &);
323 
324 ITKCommon_EXPORT Vector<float, 3>
325  CrossProduct(const Vector<float, 3> &, const Vector<float, 3> &);
326 
327 ITKCommon_EXPORT Vector<int, 3>
328  CrossProduct(const Vector<int, 3> &, const Vector<int, 3> &);
329 
330 
331 template <typename T, unsigned int VVectorDimension>
332 inline void
334 {
335  a.swap(b);
336 }
337 
338 
340 template <typename TValue, typename... TVariadic>
341 auto
342 MakeVector(const TValue firstValue, const TVariadic... otherValues)
343 {
344  static_assert(std::conjunction_v<std::is_same<TVariadic, TValue>...>,
345  "The other values should have the same type as the first value.");
346 
347  constexpr unsigned int dimension{ 1 + sizeof...(TVariadic) };
348  const std::array<TValue, dimension> stdArray{ { firstValue, otherValues... } };
349  return Vector<TValue, dimension>{ stdArray };
350 }
351 
352 } // end namespace itk
353 
354 #ifndef ITK_MANUAL_INSTANTIATION
355 # include "itkVector.hxx"
356 #endif
357 
358 #endif
itk::Vector::operator/=
const Self & operator/=(const Tt &value)
Definition: itkVector.h:174
itk::Vector< double, Self::ImageDimension >::ComponentType
double ComponentType
Definition: itkVector.h:81
itk::Vector::operator*=
const Self & operator*=(const Tt &value)
Definition: itkVector.h:161
itk::operator*
CovariantVector< T, VVectorDimension > operator*(const T &scalar, const CovariantVector< T, VVectorDimension > &v)
Definition: itkCovariantVector.h:272
Self
AddImageFilter Self
Definition: itkAddImageFilter.h:89
itk::MakeVector
auto MakeVector(const TValue firstValue, const TVariadic... otherValues)
Definition: itkVector.h:342
itk::Vector::operator*
Self operator*(const ValueType &value) const
Definition: itkVector.h:213
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:141
itk::Vector::Vector
Vector(const ValueType r[Dimension])
Definition: itkVector.h:131
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::Vector::operator/
Self operator/(const Tt &value) const
Definition: itkVector.h:228
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:261
itk::Vector::SetNthComponent
void SetNthComponent(int c, const ComponentType &v)
Definition: itkVector.h:272
itk::CrossProduct
ITKCommon_EXPORT void CrossProduct(CovariantVector< double, 3 > &, const Vector< double, 3 > &, const Vector< double, 3 > &)
itk::Vector::CastFrom
void CastFrom(const Vector< TCoordinateB, VVectorDimension > &pa)
Definition: itkVector.h:281
itkFixedArray.h
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:552
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::swap
void swap(Array< T > &a, Array< T > &b) noexcept
Definition: itkArray.h:242
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::Vector::operator=
Vector & operator=(const Vector< TVectorValueType, VVectorDimension > &r)
Definition: itkVector.h:148
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:128
itk::Vector::operator==
bool operator==(const Self &v) const
Definition: itkVector.h:244
itk::Vector::Vector
Vector(const TVectorValueType r[Dimension])
Definition: itkVector.h:135
itk::NumericTraits::RealType
double RealType
Definition: itkNumericTraits.h:86
AddImageFilter
Definition: itkAddImageFilter.h:81
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