Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkVector.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkVector.h,v $
00005   Language:  C++
00006   Date:      $Date: 2008-05-12 21:11:21 $
00007   Version:   $Revision: 1.82 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================*/
00017 #ifndef __itkVector_h
00018 #define __itkVector_h
00019 
00020 #include "itkFixedArray.h"
00021 
00022 #include "itkNumericTraits.h"   // RealValueType type
00023 #include <vnl/vnl_vector_ref.h> // Get_vnl_vector method return
00024 
00025 
00026 namespace itk
00027 {
00028 
00057 template<class T, unsigned int NVectorDimension=3>
00058 class Vector : public FixedArray<T,NVectorDimension>
00059 {
00060 public:
00062   typedef Vector  Self;
00063   typedef FixedArray<T,NVectorDimension>  Superclass;
00064 
00067   typedef T ValueType;
00068   typedef typename NumericTraits< ValueType >::RealType   RealValueType;
00069 
00071   itkStaticConstMacro(Dimension, unsigned int, NVectorDimension);
00072 
00074   typedef Self VectorType;
00075 
00077   typedef T ComponentType;
00078 
00080   typedef FixedArray<T, NVectorDimension>                BaseArray;
00081 
00083   static unsigned int GetVectorDimension() 
00084     { return NVectorDimension; }  
00085 
00087   void SetVnlVector( const vnl_vector<T> & );
00088 
00090   vnl_vector_ref<T> GetVnlVector( void );
00091 
00093   vnl_vector<T> GetVnlVector( void ) const;
00094 
00095 
00098   void Set_vnl_vector( const vnl_vector<T> & );
00099 
00102   vnl_vector_ref<T> Get_vnl_vector( void );
00103 
00106   vnl_vector<T> Get_vnl_vector( void ) const;
00107 
00109   Vector(): BaseArray() { }
00110   Vector(const ValueType& r);
00111 
00113   template< class TVectorValueType >
00114   Vector(const Vector< TVectorValueType, NVectorDimension>& r): BaseArray(r) {}
00115   Vector(const ValueType r[Dimension]): BaseArray(r) {}  
00117 
00119   template< class TVectorValueType >
00120   Vector& operator= (const Vector< TVectorValueType, NVectorDimension> & r)
00121     {
00122     BaseArray::operator=(r);
00123     return *this;
00124     }
00126 
00127   Vector& operator= (const ValueType r[NVectorDimension]);
00128     
00130   template< class Tt > inline const Self& operator*=(const Tt &value)
00131     {
00132     for( unsigned int i=0; i<NVectorDimension; i++)
00133       {
00134       (*this)[i] = static_cast< ValueType >((*this)[i] * value);
00135       }
00136     return *this;
00137     }
00139 
00141   template< class Tt > inline const Self& operator/=(const Tt &value)
00142     {
00143     for( unsigned int i=0; i<NVectorDimension; i++)
00144       {
00145       (*this)[i] = static_cast< ValueType >((*this)[i] / value);
00146       }
00147     return *this;
00148     }
00150 
00152   const Self& operator+=(const Self &vec);
00153 
00155   const Self& operator-=(const Self &vec);
00156 
00159   Self operator-() const;
00160 
00162   Self operator+(const Self &vec) const;
00163 
00165   Self operator-(const Self &vec) const;
00166 
00169   ValueType operator*(const Self &vec) const;
00170 
00173   inline Self operator*(const ValueType& value) const
00174     {
00175     Self result;
00176     for( unsigned int i=0; i<NVectorDimension; i++) 
00177       {
00178       result[i] = static_cast< ValueType >((*this)[i] * value);
00179       }
00180     return result;
00181     }
00183 
00186   template< class Tt > inline Self operator/(const Tt& value) const
00187     {
00188     Self result;
00189     for( unsigned int i=0; i<NVectorDimension; i++) 
00190       {
00191       result[i] = static_cast< ValueType >((*this)[i] / value);
00192       }
00193     return result;
00194     }
00196 
00201   bool operator==(const Self& v) const
00202     { return Superclass::operator==(v); }
00203   bool operator!=(const Self& v) const
00204     { return !operator==(v); }
00206 
00208   RealValueType GetNorm( void ) const;
00209 
00211   RealValueType GetSquaredNorm( void ) const; 
00212 
00214   static unsigned int GetNumberOfComponents(){ return NVectorDimension;}
00215 
00217   void Normalize(void);
00218 
00219   void SetNthComponent(int c, const ComponentType& v)  
00220     {  this->operator[](c) = v; }
00221   
00224   template < typename TCoordRepB >
00225   void CastFrom( const Vector<TCoordRepB,NVectorDimension> & pa )
00226   {
00227     for(unsigned int i=0; i<NVectorDimension; i++ )
00228       {
00229       (*this)[i] = static_cast<T>( pa[i] );
00230       }
00231   }
00233 
00234 };
00235 
00238 template< class T, unsigned int NVectorDimension >
00239 inline
00240 Vector<T,NVectorDimension>
00241 operator*(const T &scalar, const  Vector<T,NVectorDimension> & v)
00242 {
00243   return v * scalar;
00244 }
00245 
00246 template< class T, unsigned int NVectorDimension >  
00247 std::ostream& operator<<(std::ostream& os, 
00248                                     const Vector<T,NVectorDimension> & v); 
00249 
00250 template< class T, unsigned int NVectorDimension >  
00251 std::istream& operator>>(std::istream& is, 
00252                                     Vector<T,NVectorDimension> & v); 
00253 
00254 ITKCommon_EXPORT Vector<double,3> CrossProduct( const Vector<double,3> &,
00255                                           const Vector<double,3> &  );
00256 
00257 ITKCommon_EXPORT Vector<float,3> CrossProduct( const Vector<float,3> &,
00258                                          const Vector<float,3> &  );
00259 
00260 ITKCommon_EXPORT Vector<int,3> CrossProduct( const Vector<int,3> &,
00261                                        const Vector<int,3> &  );
00262 
00263 } // end namespace itk
00264 
00265 
00266 // Define instantiation macro for this template.
00267 #define ITK_TEMPLATE_Vector(_, EXPORT, x, y) namespace itk { \
00268   _(2(class EXPORT Vector< ITK_TEMPLATE_2 x >)) \
00269   _(1(EXPORT std::ostream& operator<<(std::ostream&, \
00270                                       const Vector< ITK_TEMPLATE_2 x >&))) \
00271   _(1(EXPORT std::istream& operator>>(std::istream&, \
00272                                       Vector< ITK_TEMPLATE_2 x >&))) \
00273   namespace Templates { typedef Vector< ITK_TEMPLATE_2 x > Vector##y; } \
00274   }
00275 
00276 #if ITK_TEMPLATE_EXPLICIT
00277 # include "Templates/itkVector+-.h"
00278 #endif
00279 
00280 //
00281 // Numeric traits must be included after (optionally) including the explicit
00282 // instantiations control of this class, in case the implicit instantiation
00283 // needs to be disabled. 
00284 //
00285 // NumericTraits must be included before (optionally) including the .txx file,
00286 // in case the .txx requires to use NumericTraits.
00287 //
00288 #include "itkNumericTraitsVectorPixel.h"
00289 
00290 #if ITK_TEMPLATE_TXX
00291 # include "itkVector.txx"
00292 #endif
00293 
00294 
00295 #endif
00296 

Generated at Tue Jul 29 23:11:28 2008 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000