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 operator*(const Self & other) const;
208 
211  inline Self operator*(const ValueType & value) const
212  {
213  Self result;
214 
215  for (unsigned int i = 0; i < VVectorDimension; ++i)
216  {
217  result[i] = static_cast<ValueType>((*this)[i] * value);
218  }
219  return result;
220  }
221 
224  template <typename Tt>
225  inline Self
226  operator/(const Tt & value) const
227  {
228  Self result;
229 
230  for (unsigned int i = 0; i < VVectorDimension; ++i)
231  {
232  result[i] = static_cast<ValueType>((*this)[i] / value);
233  }
234  return result;
235  }
236 
241  bool
242  operator==(const Self & v) const
243  {
244  return Superclass::operator==(v);
245  }
246 
247  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self);
248 
250  RealValueType
251  GetNorm() const;
252 
254  RealValueType
255  GetSquaredNorm() const;
256 
258  static unsigned int
260  {
261  return VVectorDimension;
262  }
263 
266  RealValueType
267  Normalize();
268 
269  void
270  SetNthComponent(int c, const ComponentType & v)
271  {
272  this->operator[](c) = v;
273  }
274 
277  template <typename TCoordRepB>
278  void
280  {
281  for (unsigned int i = 0; i < VVectorDimension; ++i)
282  {
283  (*this)[i] = static_cast<T>(pa[i]);
284  }
285  }
288  template <typename TCoordRepB>
290  {
292  for (unsigned int i = 0; i < VVectorDimension; ++i)
293  {
294  r[i] = static_cast<TCoordRepB>((*this)[i]);
295  }
296  return r;
297  }
298 };
299 
302 template <typename T, unsigned int VVectorDimension>
304 {
305  return v.operator*(scalar);
306 }
307 
309 template <typename T, unsigned int VVectorDimension>
310 std::ostream &
311 operator<<(std::ostream & os, const Vector<T, VVectorDimension> & vct);
312 
314 template <typename T, unsigned int VVectorDimension>
315 std::istream &
316 operator>>(std::istream & is, Vector<T, VVectorDimension> & vct);
317 
318 ITKCommon_EXPORT Vector<double, 3>
319  CrossProduct(const Vector<double, 3> &, const Vector<double, 3> &);
320 
321 ITKCommon_EXPORT Vector<float, 3>
322  CrossProduct(const Vector<float, 3> &, const Vector<float, 3> &);
323 
324 ITKCommon_EXPORT Vector<int, 3>
325  CrossProduct(const Vector<int, 3> &, const Vector<int, 3> &);
326 
327 
328 template <typename T, unsigned int VVectorDimension>
329 inline void
331 {
332  a.swap(b);
333 }
334 
335 
337 template <typename TValue, typename... TVariadic>
338 auto
339 MakeVector(const TValue firstValue, const TVariadic... otherValues)
340 {
341  static_assert(std::conjunction_v<std::is_same<TVariadic, TValue>...>,
342  "The other values should have the same type as the first value.");
343 
344  constexpr unsigned int dimension{ 1 + sizeof...(TVariadic) };
345  const std::array<TValue, dimension> stdArray{ { firstValue, otherValues... } };
346  return Vector<TValue, dimension>{ stdArray };
347 }
348 
349 } // end namespace itk
350 
351 #ifndef ITK_MANUAL_INSTANTIATION
352 # include "itkVector.hxx"
353 #endif
354 
355 #endif
itk::Vector::operator/=
const Self & operator/=(const Tt &value)
Definition: itkVector.h:174
itk::Vector::CastFrom
void CastFrom(const Vector< TCoordRepB, VVectorDimension > &pa)
Definition: itkVector.h:279
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:161
itk::operator*
CovariantVector< T, VVectorDimension > operator*(const T &scalar, const CovariantVector< T, VVectorDimension > &v)
Definition: itkCovariantVector.h:268
Self
AddImageFilter Self
Definition: itkAddImageFilter.h:89
itk::MakeVector
auto MakeVector(const TValue firstValue, const TVariadic... otherValues)
Definition: itkVector.h:339
itk::Vector::operator*
Self operator*(const ValueType &value) const
Definition: itkVector.h:211
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::Vector::operator/
Self operator/(const Tt &value) const
Definition: itkVector.h:226
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:259
itk::Vector::SetNthComponent
void SetNthComponent(int c, const ComponentType &v)
Definition: itkVector.h:270
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:543
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: itkAnnulusOperator.h:24
itk::Vector::operator=
Vector & operator=(const Vector< TVectorValueType, VVectorDimension > &r)
Definition: itkVector.h:148
itk::FixedArray< T, VVectorDimension >::swap
void swap(FixedArray &other)
Definition: itkFixedArray.h:425
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:242
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