ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkCovariantVector.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 __itkCovariantVector_h
00019 #define __itkCovariantVector_h
00020 
00021 #include "vnl/vnl_vector_ref.h"
00022 #include "itkIndent.h"
00023 #include "itkVector.h"
00024 
00025 namespace itk
00026 {
00066 template< class T, unsigned int NVectorDimension = 3 >
00067 class ITK_EXPORT CovariantVector:public FixedArray< T, NVectorDimension >
00068 {
00069 public:
00071   typedef CovariantVector                   Self;
00072   typedef FixedArray< T, NVectorDimension > Superclass;
00073 
00076   typedef T                                             ValueType;
00077   typedef typename NumericTraits< ValueType >::RealType RealValueType;
00078 
00080   typedef T ComponentType;
00081 
00083   itkStaticConstMacro(Dimension, unsigned int, NVectorDimension);
00084 
00086   typedef Self CovariantVectorType;
00087 
00089   typedef FixedArray< T, NVectorDimension > BaseArray;
00090 
00092   static unsigned int GetCovariantVectorDimension()
00093   { return NVectorDimension; }
00094 
00096   void SetVnlVector(const vnl_vector< T > &);
00097 
00099   vnl_vector_ref< T > GetVnlVector(void);
00100 
00102   vnl_vector< T > GetVnlVector(void) const;
00103 
00106   void Set_vnl_vector(const vnl_vector< T > &);
00107 
00110   vnl_vector_ref< T > Get_vnl_vector(void);
00111 
00114   vnl_vector< T > Get_vnl_vector(void) const;
00115 
00117   CovariantVector():BaseArray() {}
00118   CovariantVector(const ValueType & r);
00119 
00122   template< class TVectorValueType >
00123   CovariantVector(const CovariantVector< TVectorValueType,
00124                                          NVectorDimension > & r):BaseArray(r) {}
00125   CovariantVector(const ValueType r[Dimension]):BaseArray(r) {}
00127 
00129   template< class Tt >
00130   Self & operator=(const Tt & v)
00131   {
00132     BaseArray::operator=(v);
00133     return *this;
00134   }
00136 
00138   CovariantVector & operator=(const Self & r);
00139 
00140   CovariantVector & operator=(const ValueType r[NVectorDimension]);
00141 
00143   template< class Tt >
00144   inline const Self & operator*=(const Tt & value)
00145   {
00146     for ( unsigned int i = 0; i < NVectorDimension; i++ )
00147       {
00148       ( *this )[i] = static_cast< ValueType >( ( *this )[i] * value );
00149       }
00150     return *this;
00151   }
00153 
00155   template< class Tt >
00156   const Self & operator/=(const Tt & value)
00157   {
00158     for ( unsigned int i = 0; i < NVectorDimension; i++ )
00159       {
00160       ( *this )[i] = static_cast< ValueType >( ( *this )[i] / value );
00161       }
00162     return *this;
00163   }
00165 
00167   const Self & operator+=(const Self & vec);
00168 
00170   const Self & operator-=(const Self & vec);
00171 
00174   Self operator-() const;
00175 
00177   Self operator+(const Self & vec) const;
00178 
00180   Self operator-(const Self & vec) const;
00181 
00186   ValueType operator *(const Self & vec) const;
00187 
00190   ValueType operator *(const Vector< T, NVectorDimension > & vec) const;
00191 
00194   inline Self operator*(const ValueType & val) const
00195   {
00196     Self result;
00197 
00198     for ( unsigned int i = 0; i < NVectorDimension; i++ )
00199       {
00200       result[i] = static_cast< ValueType >( ( *this )[i] * val );
00201       }
00202     return result;
00203   }
00204 
00207   template< class Tt >
00208   inline Self operator/(const Tt & val) const
00209   {
00210     Self result;
00211 
00212     for ( unsigned int i = 0; i < NVectorDimension; i++ )
00213       {
00214       result[i] = static_cast< ValueType >( ( *this )[i] / val );
00215       }
00216     return result;
00217   }
00218 
00220   RealValueType GetNorm(void) const;
00221 
00223   static unsigned int GetNumberOfComponents() { return NVectorDimension; }
00224 
00226   void Normalize(void);
00227 
00229   RealValueType GetSquaredNorm(void) const;
00230 
00233   template< typename TCoordRepB >
00234   void CastFrom(const CovariantVector< TCoordRepB, NVectorDimension > & pa)
00235   {
00236     for ( unsigned int i = 0; i < NVectorDimension; i++ )
00237       {
00238       ( *this )[i] = static_cast< T >( pa[i] );
00239       }
00240   }
00241 };
00243 
00246 template< class T, unsigned int NVectorDimension >
00247 inline
00248 CovariantVector< T, NVectorDimension >
00249 operator*(const T & scalar, const CovariantVector< T, NVectorDimension > & v)
00250 {
00251   return v * scalar;
00252 }
00253 
00254 ITKCommon_EXPORT void CrossProduct(CovariantVector< double, 3 > &,
00255                                    const Vector< double, 3 > &,
00256                                    const Vector< double, 3 > &);
00257 
00258 ITKCommon_EXPORT void CrossProduct(CovariantVector< float, 3 > &,
00259                                    const Vector< float, 3 > &,
00260                                    const Vector< float, 3 > &);
00261 
00262 ITKCommon_EXPORT void CrossProduct(CovariantVector< int, 3 >,
00263                                    const Vector< int, 3 > &,
00264                                    const Vector< int, 3 > &);
00265 } // end namespace itk
00266 
00267 // Define instantiation macro for this template.
00268 #define ITK_TEMPLATE_CovariantVector(_, EXPORT, TypeX, TypeY)     \
00269   namespace itk                                                   \
00270   {                                                               \
00271   _( 2 ( class EXPORT CovariantVector< ITK_TEMPLATE_2 TypeX > ) ) \
00272   namespace Templates                                             \
00273   {                                                               \
00274   typedef CovariantVector< ITK_TEMPLATE_2 TypeX >                 \
00275   CovariantVector##TypeY;                                       \
00276   }                                                               \
00277   }
00278 
00279 #if ITK_TEMPLATE_EXPLICIT
00280 #include "Templates/itkCovariantVector+-.h"
00281 #endif
00282 
00283 //
00284 // Numeric traits must be included after (optionally) including the explicit
00285 // instantiations control of this class, in case the implicit instantiation
00286 // needs to be disabled.
00287 //
00288 // NumericTraits must be included before (optionally) including the .hxx file,
00289 // in case the .hxx requires to use NumericTraits.
00290 //
00291 #include "itkNumericTraitsCovariantVectorPixel.h"
00292 
00293 #if ITK_TEMPLATE_TXX
00294 #include "itkCovariantVector.hxx"
00295 #endif
00296 
00297 #endif
00298