ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkVector.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 GetVectorDimension() { return NVectorDimension; }
88 
90  void SetVnlVector(const vnl_vector< T > &);
91 
93  vnl_vector_ref< T > GetVnlVector();
94 
96  vnl_vector< T > GetVnlVector() const;
97 
99  Vector() = default;
100  Vector(const Vector&) = default;
101  Vector(Vector&&) = default;
102  Vector & operator=(const Vector &) = default;
103  Vector & operator=(Vector &&) = default;
104  ~Vector() = default;
106 
107 #if !defined( ITK_LEGACY_FUTURE_REMOVE )
108 
112  Vector(const ValueType & r);
113 #else
114 
117  explicit Vector(const ValueType & r);
118 #endif
119 
121  template< typename TVectorValueType >
125 
127  template< typename TVectorValueType >
129  {
130  BaseArray::operator=(r);
131  return *this;
132  }
134 
135  Vector & operator=(const ValueType r[NVectorDimension]);
136 
138  template< typename Tt >
139  inline const Self & operator*=(const Tt & value)
140  {
141  for ( unsigned int i = 0; i < NVectorDimension; i++ )
142  {
143  ( *this )[i] = static_cast< ValueType >( ( *this )[i] * value );
144  }
145  return *this;
146  }
148 
150  template< typename Tt >
151  inline const Self & operator/=(const Tt & value)
152  {
153  for ( unsigned int i = 0; i < NVectorDimension; i++ )
154  {
155  ( *this )[i] = static_cast< ValueType >( ( *this )[i] / value );
156  }
157  return *this;
158  }
160 
162  const Self & operator+=(const Self & vec);
163 
165  const Self & operator-=(const Self & vec);
166 
169  Self operator-() const;
170 
172  Self operator+(const Self & vec) const;
173 
175  Self operator-(const Self & vec) const;
176 
179  ValueType operator *(const Self & vec) const;
180 
183  inline Self operator*(const ValueType & value) const
184  {
185  Self result;
186 
187  for ( unsigned int i = 0; i < NVectorDimension; i++ )
188  {
189  result[i] = static_cast< ValueType >( ( *this )[i] * value );
190  }
191  return result;
192  }
193 
196  template< typename Tt >
197  inline Self operator/(const Tt & value) const
198  {
199  Self result;
200 
201  for ( unsigned int i = 0; i < NVectorDimension; i++ )
202  {
203  result[i] = static_cast< ValueType >( ( *this )[i] / value );
204  }
205  return result;
206  }
207 
212  bool operator==(const Self & v) const
213  { return Superclass::operator==(v); }
214  bool operator!=(const Self & v) const
215  { return !operator==(v); }
217 
219  RealValueType GetNorm() const;
220 
222  RealValueType GetSquaredNorm() const;
223 
225  static unsigned int GetNumberOfComponents() { return NVectorDimension; }
226 
229  RealValueType Normalize();
230 
231  void SetNthComponent(int c, const ComponentType & v)
232  { this->operator[](c) = v; }
233 
236  template< typename TCoordRepB >
238  {
239  for ( unsigned int i = 0; i < NVectorDimension; i++ )
240  {
241  ( *this )[i] = static_cast< T >( pa[i] );
242  }
243  }
245 
246  template<typename TCoordRepB>
248  {
250  for (unsigned int i = 0; i < NVectorDimension; i++)
251  {
252  r[i] = static_cast<TCoordRepB> ((*this)[i]);
253  }
254  return r;
255  }
256 
257 };
258 
261 template< typename T, unsigned int NVectorDimension >
262 inline
263 Vector< T, NVectorDimension >
264 operator*(const T & scalar, const Vector< T, NVectorDimension > & v)
265 {
266  return v.operator*( scalar);
267 }
268 
270 template< typename T, unsigned int NVectorDimension >
271 std::ostream & operator<<(std::ostream & os,
272  const Vector< T, NVectorDimension > & v);
273 
275 template< typename T, unsigned int NVectorDimension >
276 std::istream & operator>>(std::istream & is,
277  Vector< T, NVectorDimension > & v);
278 
279 ITKCommon_EXPORT Vector< double, 3 > CrossProduct(const Vector< double, 3 > &,
280  const Vector< double, 3 > &);
281 
282 ITKCommon_EXPORT Vector< float, 3 > CrossProduct(const Vector< float, 3 > &,
283  const Vector< float, 3 > &);
284 
285 ITKCommon_EXPORT Vector< int, 3 > CrossProduct(const Vector< int, 3 > &,
286  const Vector< int, 3 > &);
287 
288 
289 template< typename T, unsigned int NVectorDimension >
291 {
292  a.swap(b);
293 }
294 
295 } // end namespace itk
296 
297 #ifndef ITK_MANUAL_INSTANTIATION
298 #include "itkVector.hxx"
299 #endif
300 
301 #endif
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:485
Vector & operator=(const Vector< TVectorValueType, NVectorDimension > &r)
Definition: itkVector.h:128
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Vector(const ValueType r[Dimension])
Definition: itkVector.h:123
Define numeric traits for std::vector.
std::istream & operator>>(std::istream &is, Point< T, NPointDimension > &v)
Self operator*(const ValueType &value) const
Definition: itkVector.h:183
CovariantVector< T, NVectorDimension > operator*(const T &scalar, const CovariantVector< T, NVectorDimension > &v)
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:188
Simulate a standard C array with copy semnatics.
Definition: itkFixedArray.h:51
static unsigned int GetNumberOfComponents()
Definition: itkVector.h:225
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:211
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
bool operator==(const Self &v) const
Definition: itkVector.h:212
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
typename NumericTraits< ValueType >::RealType RealValueType
Definition: itkVector.h:72
const Self & operator/=(const Tt &value)
Definition: itkVector.h:151
Self operator/(const Tt &value) const
Definition: itkVector.h:197
ITKCommon_EXPORT void CrossProduct(CovariantVector< double, 3 > &, const Vector< double, 3 > &, const Vector< double, 3 > &)
void CastFrom(const Vector< TCoordRepB, NVectorDimension > &pa)
Definition: itkVector.h:237
Vector(const Vector< TVectorValueType, NVectorDimension > &r)
Definition: itkVector.h:122
static unsigned int GetVectorDimension()
Definition: itkVector.h:87
void SetNthComponent(int c, const ComponentType &v)
Definition: itkVector.h:231
bool operator!=(const Self &v) const
Definition: itkVector.h:214
const Self & operator*=(const Tt &value)
Definition: itkVector.h:139