ITK  4.6.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 Vector:public FixedArray< T, NVectorDimension >
63 {
64 public:
66  typedef Vector Self;
68 
71  typedef T ValueType;
73 
75  itkStaticConstMacro(Dimension, unsigned int, NVectorDimension);
76 
78  typedef Self VectorType;
79 
81  typedef T ComponentType;
82 
85 
87  static unsigned int GetVectorDimension() { return NVectorDimension; }
88 
90  void SetVnlVector(const vnl_vector< T > &);
91 
93  vnl_vector_ref< T > GetVnlVector(void);
94 
96  vnl_vector< T > GetVnlVector(void) const;
97 
100  itkLegacyMacro(void Set_vnl_vector(const vnl_vector< T > &));
101 
104  itkLegacyMacro(vnl_vector_ref< T > Get_vnl_vector(void));
105 
108  itkLegacyMacro(vnl_vector< T > Get_vnl_vector(void) const);
109 
112 
114  Vector(const ValueType & r);
115 
117  template< typename TVectorValueType >
121 
123  template< typename TVectorValueType >
125  {
127  return *this;
128  }
130 
131  Vector & operator=(const ValueType r[NVectorDimension]);
132 
134  template< typename Tt >
135  inline const Self & operator*=(const Tt & value)
136  {
137  for ( unsigned int i = 0; i < NVectorDimension; i++ )
138  {
139  ( *this )[i] = static_cast< ValueType >( ( *this )[i] * value );
140  }
141  return *this;
142  }
144 
146  template< typename Tt >
147  inline const Self & operator/=(const Tt & value)
148  {
149  for ( unsigned int i = 0; i < NVectorDimension; i++ )
150  {
151  ( *this )[i] = static_cast< ValueType >( ( *this )[i] / value );
152  }
153  return *this;
154  }
156 
158  const Self & operator+=(const Self & vec);
159 
161  const Self & operator-=(const Self & vec);
162 
165  Self operator-() const;
166 
168  Self operator+(const Self & vec) const;
169 
171  Self operator-(const Self & vec) const;
172 
175  ValueType operator *(const Self & vec) const;
176 
179  inline Self operator*(const ValueType & value) const
180  {
181  Self result;
182 
183  for ( unsigned int i = 0; i < NVectorDimension; i++ )
184  {
185  result[i] = static_cast< ValueType >( ( *this )[i] * value );
186  }
187  return result;
188  }
189 
192  template< typename Tt >
193  inline Self operator/(const Tt & value) const
194  {
195  Self result;
196 
197  for ( unsigned int i = 0; i < NVectorDimension; i++ )
198  {
199  result[i] = static_cast< ValueType >( ( *this )[i] / value );
200  }
201  return result;
202  }
203 
208  bool operator==(const Self & v) const
209  { return Superclass::operator==(v); }
210  bool operator!=(const Self & v) const
211  { return !operator==(v); }
213 
215  RealValueType GetNorm(void) const;
216 
218  RealValueType GetSquaredNorm(void) const;
219 
221  static unsigned int GetNumberOfComponents() { return NVectorDimension; }
222 
224  void Normalize(void);
225 
226  void SetNthComponent(int c, const ComponentType & v)
227  { this->operator[](c) = v; }
228 
231  template< typename TCoordRepB >
233  {
234  for ( unsigned int i = 0; i < NVectorDimension; i++ )
235  {
236  ( *this )[i] = static_cast< T >( pa[i] );
237  }
238  }
240 
241  template<typename TCoordRepB>
243  {
245  for (unsigned int i = 0; i < NVectorDimension; i++)
246  {
247  r[i] = static_cast<TCoordRepB> ((*this)[i]);
248  }
249  return r;
250  }
251 
252 };
253 
256 template< typename T, unsigned int NVectorDimension >
257 inline
258 Vector< T, NVectorDimension >
259 operator*(const T & scalar, const Vector< T, NVectorDimension > & v)
260 {
261  return v.operator*( scalar);
262 }
263 
265 template< typename T, unsigned int NVectorDimension >
266 std::ostream & operator<<(std::ostream & os,
267  const Vector< T, NVectorDimension > & v);
268 
270 template< typename T, unsigned int NVectorDimension >
271 std::istream & operator>>(std::istream & is,
272  Vector< T, NVectorDimension > & v);
273 
274 ITKCommon_EXPORT Vector< double, 3 > CrossProduct(const Vector< double, 3 > &,
275  const Vector< double, 3 > &);
276 
277 ITKCommon_EXPORT Vector< float, 3 > CrossProduct(const Vector< float, 3 > &,
278  const Vector< float, 3 > &);
279 
280 ITKCommon_EXPORT Vector< int, 3 > CrossProduct(const Vector< int, 3 > &,
281  const Vector< int, 3 > &);
282 } // end namespace itk
283 
284 #ifndef ITK_MANUAL_INSTANTIATION
285 #include "itkVector.hxx"
286 #endif
287 
288 #endif
ValueType operator*(const Self &vec) const
Vector & operator=(const Vector< TVectorValueType, NVectorDimension > &r)
Definition: itkVector.h:124
void Normalize(void)
const Self & operator+=(const Self &vec)
Vector(const ValueType r[Dimension])
Definition: itkVector.h:119
bool operator==(const FixedArray &r) const
Self operator-() const
std::istream & operator>>(std::istream &is, Point< T, NPointDimension > &v)
vnl_vector_ref< T > GetVnlVector(void)
Self operator*(const ValueType &value) const
Definition: itkVector.h:179
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:184
FixedArray< T, NVectorDimension > Superclass
Definition: itkVector.h:67
Simulate a standard C array with copy semnatics.
Definition: itkFixedArray.h:50
static unsigned int GetNumberOfComponents()
Definition: itkVector.h:221
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
bool operator==(const Self &v) const
Definition: itkVector.h:208
static const unsigned int Dimension
Definition: itkVector.h:75
FixedArray< T, NVectorDimension > BaseArray
Definition: itkVector.h:84
const Self & operator/=(const Tt &value)
Definition: itkVector.h:147
Self operator/(const Tt &value) const
Definition: itkVector.h:193
Self VectorType
Definition: itkVector.h:78
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:232
void SetVnlVector(const vnl_vector< T > &)
FixedArray & operator=(const FixedArray< TFixedArrayValueType, VLength > &r)
T ComponentType
Definition: itkVector.h:81
const Self & operator-=(const Self &vec)
Vector(const Vector< TVectorValueType, NVectorDimension > &r)
Definition: itkVector.h:118
RealValueType GetNorm(void) const
RealValueType GetSquaredNorm(void) const
static unsigned int GetVectorDimension()
Definition: itkVector.h:87
void SetNthComponent(int c, const ComponentType &v)
Definition: itkVector.h:226
NumericTraits< ValueType >::RealType RealValueType
Definition: itkVector.h:72
Self operator+(const Self &vec) const
bool operator!=(const Self &v) const
Definition: itkVector.h:210
const Self & operator*=(const Tt &value)
Definition: itkVector.h:135
itkLegacyMacro(void Set_vnl_vector(const vnl_vector< T > &))
Vector Self
Definition: itkVector.h:66