ITK  5.2.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  * http://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 NVectorDimension = 3>
62 class ITK_TEMPLATE_EXPORT Vector : public FixedArray<T, NVectorDimension>
63 {
64 public:
66  using Self = Vector;
68 
71  using ValueType = T;
73 
75  static constexpr unsigned int Dimension = NVectorDimension;
76 
78  using VectorType = Self;
79 
81  using ComponentType = T;
82 
85 
87  static unsigned int
89  {
90  return NVectorDimension;
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 
106  Vector() = default;
107  Vector(const Vector &) = default;
108  Vector(Vector &&) = default;
109  Vector &
110  operator=(const Vector &) = default;
111  Vector &
112  operator=(Vector &&) = default;
113  ~Vector() = default;
115 
116 #if !defined(ITK_LEGACY_FUTURE_REMOVE)
117 
121  Vector(const ValueType & r);
122 #else
123 
126  explicit Vector(const ValueType & r);
127 #endif
128 
130  template <typename TVectorValueType>
132  : BaseArray(r)
133  {}
135  : BaseArray(r)
136  {}
137  template <typename TVectorValueType>
138  Vector(const TVectorValueType r[Dimension])
139  : BaseArray(r)
140  {}
142 
144  template <typename TVectorValueType>
145  Vector &
147  {
148  BaseArray::operator=(r);
149  return *this;
150  }
152 
153  Vector &
154  operator=(const ValueType r[NVectorDimension]);
155 
157  template <typename Tt>
158  inline const Self &
159  operator*=(const Tt & value)
160  {
161  for (unsigned int i = 0; i < NVectorDimension; i++)
162  {
163  (*this)[i] = static_cast<ValueType>((*this)[i] * value);
164  }
165  return *this;
166  }
168 
170  template <typename Tt>
171  inline const Self &
172  operator/=(const Tt & value)
173  {
174  for (unsigned int i = 0; i < NVectorDimension; i++)
175  {
176  (*this)[i] = static_cast<ValueType>((*this)[i] / value);
177  }
178  return *this;
179  }
181 
183  const Self &
184  operator+=(const Self & vec);
185 
187  const Self &
188  operator-=(const Self & vec);
189 
192  Self
193  operator-() const;
194 
196  Self
197  operator+(const Self & vec) const;
198 
200  Self
201  operator-(const Self & vec) const;
202 
205  ValueType operator*(const Self & vec) const;
206 
209  inline Self operator*(const ValueType & value) const
210  {
211  Self result;
212 
213  for (unsigned int i = 0; i < NVectorDimension; i++)
214  {
215  result[i] = static_cast<ValueType>((*this)[i] * value);
216  }
217  return result;
218  }
219 
222  template <typename Tt>
223  inline Self
224  operator/(const Tt & value) const
225  {
226  Self result;
227 
228  for (unsigned int i = 0; i < NVectorDimension; i++)
229  {
230  result[i] = static_cast<ValueType>((*this)[i] / value);
231  }
232  return result;
233  }
234 
239  bool
240  operator==(const Self & v) const
241  {
242  return Superclass::operator==(v);
243  }
244  bool
245  operator!=(const Self & v) const
246  {
247  return !operator==(v);
248  }
250 
252  RealValueType
253  GetNorm() const;
254 
256  RealValueType
257  GetSquaredNorm() const;
258 
260  static unsigned int
262  {
263  return NVectorDimension;
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 TCoordRepB>
280  void
282  {
283  for (unsigned int i = 0; i < NVectorDimension; i++)
284  {
285  (*this)[i] = static_cast<T>(pa[i]);
286  }
287  }
289 
290  template <typename TCoordRepB>
292  {
294  for (unsigned int i = 0; i < NVectorDimension; i++)
295  {
296  r[i] = static_cast<TCoordRepB>((*this)[i]);
297  }
298  return r;
299  }
300 };
301 
304 template <typename T, unsigned int NVectorDimension>
306 {
307  return v.operator*(scalar);
308 }
309 
311 template <typename T, unsigned int NVectorDimension>
312 std::ostream &
313 operator<<(std::ostream & os, const Vector<T, NVectorDimension> & v);
314 
316 template <typename T, unsigned int NVectorDimension>
317 std::istream &
318 operator>>(std::istream & is, Vector<T, NVectorDimension> & v);
319 
320 ITKCommon_EXPORT Vector<double, 3>
321  CrossProduct(const Vector<double, 3> &, const Vector<double, 3> &);
322 
323 ITKCommon_EXPORT Vector<float, 3>
324  CrossProduct(const Vector<float, 3> &, const Vector<float, 3> &);
325 
326 ITKCommon_EXPORT Vector<int, 3>
327  CrossProduct(const Vector<int, 3> &, const Vector<int, 3> &);
328 
329 
330 template <typename T, unsigned int NVectorDimension>
331 inline void
333 {
334  a.swap(b);
335 }
336 
337 } // end namespace itk
338 
339 #ifndef ITK_MANUAL_INSTANTIATION
340 # include "itkVector.hxx"
341 #endif
342 
343 #endif
itk::Vector::Vector
Vector(const Vector< TVectorValueType, NVectorDimension > &r)
Definition: itkVector.h:131
itk::Vector::operator/
Self operator/(const Tt &value) const
Definition: itkVector.h:224
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:213
itk::Vector< double, Self::ImageDimension >::ComponentType
double ComponentType
Definition: itkVector.h:81
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:239
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:678
itk::Vector::operator*
Self operator*(const ValueType &value) const
Definition: itkVector.h:209
itk::Vector::Vector
Vector(const ValueType r[Dimension])
Definition: itkVector.h:134
itk::Vector::operator*=
const Self & operator*=(const Tt &value)
Definition: itkVector.h:159
itk::Vector::Vector
Vector(const TVectorValueType r[Dimension])
Definition: itkVector.h:138
itk::Vector::GetNumberOfComponents
static unsigned int GetNumberOfComponents()
Definition: itkVector.h:261
itk::CrossProduct
ITKCommon_EXPORT void CrossProduct(CovariantVector< double, 3 > &, const Vector< double, 3 > &, const Vector< double, 3 > &)
itkFixedArray.h
itk::Vector::operator=
Vector & operator=(const Vector< TVectorValueType, NVectorDimension > &r)
Definition: itkVector.h:146
itk::Vector::CastFrom
void CastFrom(const Vector< TCoordRepB, NVectorDimension > &pa)
Definition: itkVector.h:281
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:532
itk::FixedArray
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:52
itk::operator*
CovariantVector< T, NVectorDimension > operator*(const T &scalar, const CovariantVector< T, NVectorDimension > &v)
Definition: itkCovariantVector.h:275
itk::Vector::SetNthComponent
void SetNthComponent(int c, const ComponentType &v)
Definition: itkVector.h:272
itk::Vector< double, Self::ImageDimension >::RealValueType
typename NumericTraits< ValueType >::RealType RealValueType
Definition: itkVector.h:72
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==
bool operator==(const Self &v) const
Definition: itkVector.h:240
itk::FixedArray< T, NVectorDimension >::swap
void swap(FixedArray &other)
Definition: itkFixedArray.h:441
itk::operator+
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:660
itk::operator>>
std::istream & operator>>(std::istream &is, Point< T, NPointDimension > &v)
itk::NumericTraits::RealType
double RealType
Definition: itkNumericTraits.h:84
itk::Vector::operator!=
bool operator!=(const Self &v) const
Definition: itkVector.h:245
itk::Vector::operator/=
const Self & operator/=(const Tt &value)
Definition: itkVector.h:172
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itk::FixedArray< double, NVectorDimension >::ValueType
double ValueType
Definition: itkFixedArray.h:62