ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkVector.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkVector_h
00019 #define __itkVector_h
00020 
00021 #include "itkFixedArray.h"
00022 
00023 #include "vnl/vnl_vector_ref.h" // Get_vnl_vector method return
00024 
00025 namespace itk
00026 {
00061 template< class T, unsigned int NVectorDimension = 3 >
00062 class Vector:public FixedArray< T, NVectorDimension >
00063 {
00064 public:
00066   typedef Vector                            Self;
00067   typedef FixedArray< T, NVectorDimension > Superclass;
00068 
00071   typedef T                                             ValueType;
00072   typedef typename NumericTraits< ValueType >::RealType RealValueType;
00073 
00075   itkStaticConstMacro(Dimension, unsigned int, NVectorDimension);
00076 
00078   typedef Self VectorType;
00079 
00081   typedef T ComponentType;
00082 
00084   typedef FixedArray< T, NVectorDimension > BaseArray;
00085 
00087   static unsigned int GetVectorDimension() { return NVectorDimension; }
00088 
00090   void SetVnlVector(const vnl_vector< T > &);
00091 
00093   vnl_vector_ref< T > GetVnlVector(void);
00094 
00096   vnl_vector< T > GetVnlVector(void) const;
00097 
00100   void Set_vnl_vector(const vnl_vector< T > &);
00101 
00104   vnl_vector_ref< T > Get_vnl_vector(void);
00105 
00108   vnl_vector< T > Get_vnl_vector(void) const;
00109 
00111   Vector():BaseArray() {}
00112   Vector(const ValueType & r);
00113 
00115   template< class TVectorValueType >
00116   Vector(const Vector< TVectorValueType, NVectorDimension > & r):BaseArray(r) {}
00117   Vector(const ValueType r[Dimension]):BaseArray(r) {}
00119 
00121   template< class TVectorValueType >
00122   Vector & operator=(const Vector< TVectorValueType, NVectorDimension > & r)
00123   {
00124     BaseArray::operator=(r);
00125     return *this;
00126   }
00128 
00129   Vector & operator=(const ValueType r[NVectorDimension]);
00130 
00132   template< class Tt >
00133   inline const Self & operator*=(const Tt & value)
00134   {
00135     for ( unsigned int i = 0; i < NVectorDimension; i++ )
00136       {
00137       ( *this )[i] = static_cast< ValueType >( ( *this )[i] * value );
00138       }
00139     return *this;
00140   }
00142 
00144   template< class Tt >
00145   inline const Self & operator/=(const Tt & value)
00146   {
00147     for ( unsigned int i = 0; i < NVectorDimension; i++ )
00148       {
00149       ( *this )[i] = static_cast< ValueType >( ( *this )[i] / value );
00150       }
00151     return *this;
00152   }
00154 
00156   const Self & operator+=(const Self & vec);
00157 
00159   const Self & operator-=(const Self & vec);
00160 
00163   Self operator-() const;
00164 
00166   Self operator+(const Self & vec) const;
00167 
00169   Self operator-(const Self & vec) const;
00170 
00173   ValueType operator *(const Self & vec) const;
00174 
00177   inline Self operator*(const ValueType & value) const
00178   {
00179     Self result;
00180 
00181     for ( unsigned int i = 0; i < NVectorDimension; i++ )
00182       {
00183       result[i] = static_cast< ValueType >( ( *this )[i] * value );
00184       }
00185     return result;
00186   }
00187 
00190   template< class Tt >
00191   inline Self operator/(const Tt & value) const
00192   {
00193     Self result;
00194 
00195     for ( unsigned int i = 0; i < NVectorDimension; i++ )
00196       {
00197       result[i] = static_cast< ValueType >( ( *this )[i] / value );
00198       }
00199     return result;
00200   }
00201 
00206   bool operator==(const Self & v) const
00207   { return Superclass::operator==(v); }
00208   bool operator!=(const Self & v) const
00209   { return !operator==(v); }
00211 
00213   RealValueType GetNorm(void) const;
00214 
00216   RealValueType GetSquaredNorm(void) const;
00217 
00219   static unsigned int GetNumberOfComponents() { return NVectorDimension; }
00220 
00222   void Normalize(void);
00223 
00224   void SetNthComponent(int c, const ComponentType & v)
00225   {  this->operator[](c) = v; }
00226 
00229   template< typename TCoordRepB >
00230   void CastFrom(const Vector< TCoordRepB, NVectorDimension > & pa)
00231   {
00232     for ( unsigned int i = 0; i < NVectorDimension; i++ )
00233       {
00234       ( *this )[i] = static_cast< T >( pa[i] );
00235       }
00236   }
00238 
00239   template<typename TCoordRepB>
00240   operator Vector< TCoordRepB, NVectorDimension >()
00241   {
00242     Vector<TCoordRepB, NVectorDimension> r;
00243     for (unsigned int i = 0; i < NVectorDimension; i++)
00244     {
00245       r[i] = static_cast<TCoordRepB> ((*this)[i]);
00246     }
00247     return r;
00248   }
00249 
00250 };
00251 
00254 template< class T, unsigned int NVectorDimension >
00255 inline
00256 Vector< T, NVectorDimension >
00257 operator*(const T & scalar, const Vector< T, NVectorDimension > & v)
00258 {
00259   return v * scalar;
00260 }
00261 
00262 template< class T, unsigned int NVectorDimension >
00263 std::ostream & operator<<(std::ostream & os,
00264                           const Vector< T, NVectorDimension > & v);
00265 
00266 template< class T, unsigned int NVectorDimension >
00267 std::istream & operator>>(std::istream & is,
00268                           Vector< T, NVectorDimension > & v);
00269 
00270 ITKCommon_EXPORT Vector< double, 3 > CrossProduct(const Vector< double, 3 > &,
00271                                                   const Vector< double, 3 > &);
00272 
00273 ITKCommon_EXPORT Vector< float, 3 > CrossProduct(const Vector< float, 3 > &,
00274                                                  const Vector< float, 3 > &);
00275 
00276 ITKCommon_EXPORT Vector< int, 3 > CrossProduct(const Vector< int, 3 > &,
00277                                                const Vector< int, 3 > &);
00278 } // end namespace itk
00279 
00280 // Define instantiation macro for this template.
00281 #define ITK_TEMPLATE_Vector(_, EXPORT, TypeX, TypeY)                                  \
00282   namespace itk                                                                       \
00283   {                                                                                   \
00284   _( 2 ( class EXPORT Vector< ITK_TEMPLATE_2 TypeX > ) )                              \
00285   _( 1 ( EXPORT std::ostream & operator<<(std::ostream &,                             \
00286                                           const Vector< ITK_TEMPLATE_2 TypeX > &) ) ) \
00287   _( 1 ( EXPORT std::istream & operator>>(std::istream &,                             \
00288                                           Vector< ITK_TEMPLATE_2 TypeX > &) ) )       \
00289   namespace Templates                                                                 \
00290   {                                                                                   \
00291   typedef Vector< ITK_TEMPLATE_2 TypeX > Vector##TypeY;                             \
00292   }                                                                                   \
00293   }
00294 
00295 #if ITK_TEMPLATE_EXPLICIT
00296 #include "Templates/itkVector+-.h"
00297 #endif
00298 
00299 //
00300 // Numeric traits must be included after (optionally) including the explicit
00301 // instantiations control of this class, in case the implicit instantiation
00302 // needs to be disabled.
00303 //
00304 // NumericTraits must be included before (optionally) including the .hxx file,
00305 // in case the .hxx requires to use NumericTraits.
00306 //
00307 #include "itkNumericTraitsVectorPixel.h"
00308 
00309 #if ITK_TEMPLATE_TXX
00310 #include "itkVector.hxx"
00311 #endif
00312 
00313 #endif
00314