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

Review/Statistics/itkMeasurementVectorTraits.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkMeasurementVectorTraits.h,v $
00005   Language:  C++
00006   Date:      $Date: 2009-05-23 04:17:05 $
00007   Version:   $Revision: 1.3 $
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 __itkMeasurementVectorTraits_h
00018 #define __itkMeasurementVectorTraits_h
00019 
00020 #include "itkMacro.h"
00021 #include "itkArray.h"
00022 #include "itkVariableLengthVector.h"
00023 #include "itkVector.h"
00024 #include "itkFixedArray.h"
00025 #include "vnl/vnl_vector_fixed.h"
00026 #include "itkPoint.h"
00027 #include "itkRGBPixel.h"
00028 #include "itkMatrix.h"
00029 #include "itkVariableSizeMatrix.h"
00030 #include "itkNumericTraits.h"
00031 #include "itkSize.h"
00032 #include <vector>
00033 
00034 
00041 #ifdef _WIN64
00042 
00043 namespace itk { 
00044 
00049 template <>
00050 class NumericTraits< std::vector<int>::size_type > : public vcl_numeric_limits< std::vector<int>::size_type > {
00051 public:
00052   typedef std::vector<int>::size_type SelfType; 
00053   typedef SelfType                    ValueType;
00054   typedef SelfType                    PrintType;
00055   typedef SelfType                    AbsType;
00056   typedef SelfType                    AccumulateType;
00057   typedef long double                 RealType;
00058   typedef RealType                    ScalarRealType;
00059   typedef long double                 FloatType;
00060 
00061   static const SelfType ITKCommon_EXPORT Zero;
00062   static const SelfType ITKCommon_EXPORT One;
00063 
00064   static SelfType NonpositiveMin() { return min(); }
00065   static bool IsPositive(SelfType val) { return val != Zero; }
00066   static bool IsNonpositive(SelfType val) { return val == Zero; }
00067   static bool IsNegative(SelfType) { return false; }
00068   static bool IsNonnegative(SelfType) {return true; }
00069   static SelfType ZeroValue() { return Zero; }
00070 };
00071 }
00072 
00073 #endif
00074 
00075 
00076 namespace itk
00077 {
00078 namespace Statistics
00079 {
00080 
00087 class MeasurementVectorTraits 
00088 {
00089 public:
00090  
00097   //typedef typename std::vector<int>::size_type   InstanceIdentifier;
00098   typedef unsigned long  InstanceIdentifier;
00099 
00101   typedef InstanceIdentifier                                      AbsoluteFrequencyType;
00102   typedef NumericTraits< AbsoluteFrequencyType >::RealType        RelativeFrequencyType;
00103   typedef NumericTraits< AbsoluteFrequencyType >::AccumulateType  TotalAbsoluteFrequencyType;
00104   typedef NumericTraits< RelativeFrequencyType >::AccumulateType  TotalRelativeFrequencyType;
00105 
00106   typedef unsigned int MeasurementVectorLength;
00107   
00108   template<class TValueType, unsigned int VLength>
00109   static void SetLength( FixedArray< TValueType, VLength > &m, const unsigned int s )
00110     {
00111     if( s != VLength )
00112       {
00113       itkGenericExceptionMacro( << "Cannot set the size of a FixedArray of length " 
00114           << VLength << " to " << s );
00115       }
00116     m.Fill( NumericTraits< TValueType >::Zero );
00117     }
00118   
00119   template<class TValueType, unsigned int VLength>
00120   static void SetLength( FixedArray< TValueType, VLength > *m, const unsigned int s )
00121     {
00122     if( s != VLength )
00123       {
00124       itkGenericExceptionMacro( << "Cannot set the size of a FixedArray of length " 
00125           << VLength << " to " << s );
00126       }
00127     m->Fill( NumericTraits< TValueType >::Zero );
00128     }
00129   
00130   template< class TValueType >
00131   static void SetLength( Array< TValueType > & m, const unsigned int s )
00132     {
00133     m.SetSize( s );
00134     m.Fill( NumericTraits< TValueType >::Zero );
00135     }
00136   
00137   template< class TValueType >
00138   static void SetLength( Array< TValueType > * m, const unsigned int s )
00139     {
00140     m->SetSize( s );
00141     m->Fill( NumericTraits< TValueType >::Zero );
00142     }
00143 
00144   template< class TValueType >
00145   static void SetLength( VariableLengthVector< TValueType > & m, const unsigned int s )
00146     {
00147     m.SetSize( s );
00148     m.Fill( NumericTraits< TValueType >::Zero );
00149     }
00150   
00151   template< class TValueType >
00152   static void SetLength( VariableLengthVector< TValueType > * m, const unsigned int s )
00153     {
00154     m->SetSize( s );
00155     m->Fill( NumericTraits< TValueType >::Zero );
00156     }
00157 
00158   template< class TValueType >
00159   static void SetLength( std::vector< TValueType > & m, const unsigned int s )
00160     {
00161     m.resize( s );
00162     }
00163   
00164   template< class TValueType >
00165   static void SetLength( std::vector< TValueType > * m, const unsigned int s )
00166     {
00167     m->resize( s );
00168     }
00169 
00170 
00171   template< class TValueType, unsigned int VLength > 
00172   static MeasurementVectorLength 
00173                GetLength( const FixedArray< TValueType, VLength > &)
00174     { return VLength; }
00175   
00176   template< class TValueType, unsigned int VLength > 
00177   static MeasurementVectorLength 
00178                GetLength( const FixedArray< TValueType, VLength > *)
00179     { return VLength; }
00180 
00181   template< class TValueType >
00182   static MeasurementVectorLength
00183                GetLength( const Array< TValueType > &m )
00184     {return m.GetSize(); }
00185   
00186   template< class TValueType >
00187   static MeasurementVectorLength
00188                GetLength( const Array< TValueType > *m )
00189     {return m->GetSize(); }
00190 
00191   template< class TValueType >
00192   static MeasurementVectorLength
00193                GetLength( const VariableLengthVector< TValueType > &m )
00194     {return m.GetSize(); }
00195   
00196   template< class TValueType >
00197   static MeasurementVectorLength
00198                GetLength( const VariableLengthVector< TValueType > *m )
00199     {return m->GetSize(); }
00200 
00201   template< class TValueType >
00202   static MeasurementVectorLength
00203                GetLength( const std::vector< TValueType > &m )
00204     {return m.size(); }
00205   
00206   template< class TValueType >
00207   static MeasurementVectorLength
00208                GetLength( const std::vector< TValueType > *m )
00209     {return m->size(); }
00210 
00211 
00212   template< class TVectorType >
00213   static bool IsResizable( const TVectorType &  )
00214     {
00215     // Test whether the vector type is resizable or not
00216     //
00217     // If the default constructor creates a vector of
00218     // length zero, we assume that it is resizable,
00219     // otherwise that is a pretty useless measurement vector.
00220     TVectorType m;
00221     MeasurementVectorLength len = GetLength( m );
00222     return ( len == 0 );
00223     }
00224 
00225 
00226 #if !(defined(_MSC_VER) && (_MSC_VER <= 1200))
00227   template< class TValueType1, unsigned int VLength, class TValueType2, unsigned int VLength2 >
00228   static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > &, 
00229                       const FixedArray< TValueType2, VLength2 > & , const char *errMsg="Length Mismatch")
00230     {
00231     if ( VLength != VLength2 )
00232       {
00233       itkGenericExceptionMacro( << errMsg );
00234       return 0;
00235       }
00236     return 0;
00237     }
00238 
00239   template< class TValueType1, unsigned int VLength, class TValueType2, unsigned int VLength2 >
00240   static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > *, 
00241                       const FixedArray< TValueType2, VLength2 > *, const char *errMsg="Length Mismatch")
00242     {
00243     if ( VLength != VLength2 )
00244       {
00245       itkGenericExceptionMacro( << errMsg );
00246       return 0;
00247       }
00248     return 0;
00249     }
00250 #endif
00251   template< class TValueType1, class TValueType2 >
00252   static MeasurementVectorLength Assert( const Array< TValueType1 > &a, 
00253                       const Array< TValueType2 > &b, const char *errMsg="Length Mismatch")
00254     {
00255     if( b.Size() != a.Size() )
00256       {
00257       itkGenericExceptionMacro( << errMsg );
00258       return 0;
00259       }
00260     return 0;
00261     }
00262 
00263   template< class TValueType1, class TValueType2 >
00264   static MeasurementVectorLength Assert( const Array< TValueType1 > *a, 
00265                       const Array< TValueType2 > *b, const char *errMsg="Length Mismatch")
00266     {
00267     if( b->Size() != a->Size() )
00268       {
00269       itkGenericExceptionMacro( << errMsg );
00270       return 0;
00271       }
00272     return 0;
00273     }
00274 
00275 
00276   template< class TValueType1, class TValueType2 >
00277   static MeasurementVectorLength Assert( const VariableLengthVector< TValueType1 > &a, 
00278                       const VariableLengthVector< TValueType2 > &b, const char *errMsg="Length Mismatch")
00279     {
00280     if( b.Size() != a.Size() )
00281       {
00282       itkGenericExceptionMacro( << errMsg );
00283       return 0;
00284       }
00285     return 0;
00286     }
00287 
00288   template< class TValueType1, class TValueType2 >
00289   static MeasurementVectorLength Assert( const VariableLengthVector< TValueType1 > *a, 
00290                       const VariableLengthVector< TValueType2 > *b, const char *errMsg="Length Mismatch")
00291     {
00292     if( b->Size() != a->Size() )
00293       {
00294       itkGenericExceptionMacro( << errMsg );
00295       return 0;
00296       }
00297     return 0;
00298     }
00299 
00300 
00301   template< class TValueType1, class TValueType2 >
00302   static MeasurementVectorLength Assert( const std::vector< TValueType1 > &a, 
00303                       const std::vector< TValueType2 > &b, const char *errMsg="Length Mismatch")
00304     {
00305     if( b.size() != a.size() )
00306       {
00307       itkGenericExceptionMacro( << errMsg );
00308       return 0;
00309       }
00310     return 0;
00311     }
00312 
00313   template< class TValueType1, class TValueType2 >
00314   static MeasurementVectorLength Assert( const std::vector< TValueType1 > *a, 
00315                       const std::vector< TValueType2 > *b, const char *errMsg="Length Mismatch")
00316     {
00317     if( b->size() != a->size() )
00318       {
00319       itkGenericExceptionMacro( << errMsg );
00320       return 0;
00321       }
00322     return 0;
00323     }
00324 
00325 
00326   template< class TValueType1, unsigned int VLength, class TValueType2 >
00327   static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > &, 
00328                       const Array< TValueType2 > &b, const char *errMsg="Length Mismatch")
00329     {
00330     if( b.Size() == 0 )
00331       {
00332       return VLength;
00333       }
00334     if( b.Size() != 0 )
00335       {
00336       if (b.Size() != VLength)
00337         {
00338         itkGenericExceptionMacro( << errMsg );
00339         return 0;
00340         }
00341       }
00342     return 0;
00343     }
00344 
00345   template< class TValueType1, unsigned int VLength, class TValueType2 >
00346   static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > *, 
00347                       const Array< TValueType2 > *b, const char *errMsg="Length Mismatch")
00348     {
00349     if( b->Size() == 0 )
00350       {
00351       return VLength;
00352       }
00353     else if (b->Size() != VLength)
00354       {
00355       itkGenericExceptionMacro( << errMsg );
00356       return 0;
00357       }
00358     return 0;
00359     }
00360 
00361   template< class TValueType1, unsigned int VLength, class TValueType2 >
00362   static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > &, 
00363                       const VariableLengthVector< TValueType2 > &b, const char *errMsg="Length Mismatch")
00364     {
00365     if( b.Size() == 0 )
00366       {
00367       return VLength;
00368       }
00369     if( b.Size() != 0 )
00370       {
00371       if (b.Size() != VLength)
00372         {
00373         itkGenericExceptionMacro( << errMsg );
00374         return 0;
00375         }
00376       }
00377     return 0;
00378     }
00379 
00380   template< class TValueType1, unsigned int VLength, class TValueType2 >
00381   static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > *, 
00382                       const VariableLengthVector< TValueType2 > *b, const char *errMsg="Length Mismatch")
00383     {
00384     if( b->Size() == 0 )
00385       {
00386       return VLength;
00387       }
00388     else if (b->Size() != VLength)
00389       {
00390       itkGenericExceptionMacro( << errMsg );
00391       return 0;
00392       }
00393     return 0;
00394     }
00395 
00396   template< class TValueType1, unsigned int VLength, class TValueType2 >
00397   static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > &, 
00398                       const std::vector< TValueType2 > &b, const char *errMsg="Length Mismatch")
00399     {
00400     if( b.size() == 0 )
00401       {
00402       return VLength;
00403       }
00404     if( b.size() != 0 )
00405       {
00406       if (b.size() != VLength)
00407         {
00408         itkGenericExceptionMacro( << errMsg );
00409         return 0;
00410         }
00411       }
00412     return 0;
00413     }
00414 
00415   template< class TValueType1, unsigned int VLength, class TValueType2 >
00416   static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > *, 
00417                       const std::vector< TValueType2 > *b, const char *errMsg="Length Mismatch")
00418     {
00419     if( b->size() == 0 )
00420       {
00421       return VLength;
00422       }
00423     else if (b->size() != VLength)
00424       {
00425       itkGenericExceptionMacro( << errMsg );
00426       return 0;
00427       }
00428     return 0;
00429     }
00430 
00431 
00432   template< class TValueType1, unsigned int VLength>
00433   static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > &, 
00434                 const MeasurementVectorLength l, const char *errMsg="Length Mismatch")
00435     {
00436     if( l == 0 )
00437       {
00438       return VLength;
00439       }
00440     else if( l != VLength )
00441       {
00442       itkGenericExceptionMacro( << errMsg );
00443       return 0;
00444       }
00445     return 0;
00446     }
00447 
00448   template< class TValueType1, unsigned int VLength>
00449   static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > *, 
00450                const MeasurementVectorLength l, const char *errMsg="Length Mismatch")
00451     {
00452     if( l == 0 )
00453       {
00454       return VLength;
00455       }
00456     else if( l != VLength )
00457       {
00458       itkGenericExceptionMacro( << errMsg );
00459       return 0;
00460       }
00461     return 0;
00462     }
00463 
00464   template< class TValueType >
00465   static MeasurementVectorLength Assert( const Array< TValueType > &a, 
00466               const MeasurementVectorLength l, const char *errMsg="Length Mismatch")
00467     {
00468     if( ( ( l != 0 ) && ( a.Size() != l ) ) || ( a.Size() == 0 ) )
00469       {
00470       itkGenericExceptionMacro( << errMsg );
00471       }
00472     else if( l == 0 )
00473       {
00474       return a.Size();
00475       }
00476     return 0;
00477     }
00478   
00479   template< class TValueType >
00480   static MeasurementVectorLength Assert( const Array< TValueType > *a, 
00481               const MeasurementVectorLength l, const char *errMsg="Length Mismatch")
00482     {
00483     if( ( ( l != 0 ) && ( a->Size() != l )) || ( a->Size() == 0 ) )
00484       {
00485       itkGenericExceptionMacro( << errMsg );
00486       }
00487     else if( l == 0 )
00488       {
00489       return a->Size();
00490       }
00491     return 0;
00492     }
00493    
00494   template< class TValueType >
00495   static MeasurementVectorLength Assert( const VariableLengthVector< TValueType > &a, 
00496               const MeasurementVectorLength l, const char *errMsg="Length Mismatch")
00497     {
00498     if( ( ( l != 0 ) && ( a.Size() != l ) ) || ( a.Size() == 0 ) )
00499       {
00500       itkGenericExceptionMacro( << errMsg );
00501       }
00502     else if( l == 0 )
00503       {
00504       return a.Size();
00505       }
00506     return 0;
00507     }
00508   
00509   template< class TValueType >
00510   static MeasurementVectorLength Assert( const VariableLengthVector< TValueType > *a, 
00511               const MeasurementVectorLength l, const char *errMsg="Length Mismatch")
00512     {
00513     if( ( ( l != 0 ) && ( a->Size() != l ) ) || ( a->Size() == 0 ) )
00514       {
00515       itkGenericExceptionMacro( << errMsg );
00516       }
00517     else if( l == 0 )
00518       {
00519       return a->Size();
00520       }
00521     return 0;
00522     }
00523   template< class TValueType >
00524   static MeasurementVectorLength Assert( const std::vector< TValueType > &a, 
00525               const MeasurementVectorLength l, const char *errMsg="Length Mismatch")
00526     {
00527     if( ( ( l != 0 ) && ( a.size() != l ) ) || ( a.size() == 0 ) )
00528       {
00529       itkGenericExceptionMacro( << errMsg );
00530       }
00531     else if( l == 0 )
00532       {
00533       return a.size();
00534       }
00535     return 0;
00536     }
00537   
00538   template< class TValueType >
00539   static MeasurementVectorLength Assert( const std::vector< TValueType > *a, 
00540               const MeasurementVectorLength l, const char *errMsg="Length Mismatch")
00541     {
00542     if( ( ( l != 0 ) && ( a->size() != l ) ) || ( a->size() == 0 ) )
00543       {
00544       itkGenericExceptionMacro( << errMsg );
00545       }
00546     else if( l == 0 )
00547       {
00548       return a->size();
00549       }
00550     return 0;
00551     }
00552 
00553   template< class TArrayType >
00554   static void  Assign( TArrayType & m, const TArrayType & v )
00555     {
00556     m = v;
00557     }
00558  
00559   template< class TValueType, unsigned int VLength >
00560   static void  Assign( FixedArray< TValueType, VLength > & m, const TValueType & v )
00561     {
00562     m[0] = v;
00563     }
00564  
00565 };
00566 
00572 template < class TMeasurementVector >
00573 class MeasurementVectorTraitsTypes
00574 {
00575 public:
00576   typedef typename TMeasurementVector::ValueType    ValueType;
00577 };
00578 
00579 //
00580 // Visual Studio 6.0 and 7.0 are not capable of managing the next
00581 // template implementation. A macro workaround is provided.
00582 #define itkMeasurementVectorTraitsTypesMacro(T) \
00583 template <> \
00584 class MeasurementVectorTraitsTypes< std::vector< T > > \
00585 { \
00586 public: \
00587    typedef T ValueType; \
00588 };
00589 
00590 #if defined( _MSC_VER ) && ( _MSC_VER < 1310 )
00591 itkMeasurementVectorTraitsTypesMacro( char );
00592 itkMeasurementVectorTraitsTypesMacro( unsigned char );
00593 itkMeasurementVectorTraitsTypesMacro( short );
00594 itkMeasurementVectorTraitsTypesMacro( unsigned short );
00595 itkMeasurementVectorTraitsTypesMacro( int );
00596 itkMeasurementVectorTraitsTypesMacro( unsigned int );
00597 itkMeasurementVectorTraitsTypesMacro( long );
00598 itkMeasurementVectorTraitsTypesMacro( unsigned long );
00599 itkMeasurementVectorTraitsTypesMacro( float );
00600 itkMeasurementVectorTraitsTypesMacro( double );
00601 #else
00602 template< class T >
00603 class MeasurementVectorTraitsTypes< std::vector< T > >
00604 {
00605 public:
00606    typedef T ValueType;
00607 };
00608 #endif
00609 
00613 template<class TPixelType>
00614 class MeasurementVectorPixelTraits
00615 {
00616 public:
00617   /* type of the vector that matches this pixel type */
00618   typedef TPixelType      MeasurementVectorType;
00619 };
00620 
00621 
00622 template<>
00623 class MeasurementVectorPixelTraits<char>
00624 {
00625 public:
00626    typedef FixedArray< char, 1 >  MeasurementVectorType;
00627 };
00628 
00629 template<>
00630 class MeasurementVectorPixelTraits<unsigned char>
00631 {
00632 public:
00633    typedef FixedArray< unsigned char, 1 >  MeasurementVectorType;
00634 };
00635 
00636 template<>
00637 class MeasurementVectorPixelTraits<signed char>
00638 {
00639 public:
00640    typedef FixedArray< signed char, 1 >  MeasurementVectorType;
00641 };
00642 
00643 template<>
00644 class MeasurementVectorPixelTraits<unsigned short>
00645 {
00646 public:
00647    typedef FixedArray< unsigned short, 1 >  MeasurementVectorType;
00648 };
00649 
00650 template<>
00651 class MeasurementVectorPixelTraits<signed short>
00652 {
00653 public:
00654    typedef FixedArray< signed short, 1 >  MeasurementVectorType;
00655 };
00656 
00657 template<>
00658 class MeasurementVectorPixelTraits<unsigned int>
00659 {
00660 public:
00661    typedef FixedArray< unsigned int, 1 >  MeasurementVectorType;
00662 };
00663 
00664 template<>
00665 class MeasurementVectorPixelTraits<signed int>
00666 {
00667 public:
00668    typedef FixedArray< signed int, 1 >  MeasurementVectorType;
00669 };
00670 
00671 template<>
00672 class MeasurementVectorPixelTraits<unsigned long>
00673 {
00674 public:
00675    typedef FixedArray< unsigned long, 1 >  MeasurementVectorType;
00676 };
00677 
00678 template<>
00679 class MeasurementVectorPixelTraits<signed long>
00680 {
00681 public:
00682    typedef FixedArray< signed long, 1 >  MeasurementVectorType;
00683 };
00684 
00685 template<>
00686 class MeasurementVectorPixelTraits<float>
00687 {
00688 public:
00689    typedef FixedArray< float, 1 >  MeasurementVectorType;
00690 };
00691 
00692 template<>
00693 class MeasurementVectorPixelTraits<double>
00694 {
00695 public:
00696   typedef FixedArray< double, 1 >  MeasurementVectorType;
00697 };
00698 
00699 
00700 } // namespace Statistics
00701 } // namespace itk
00702 
00703 #endif  // __itkMeasurementVectorTraits_h
00704 

Generated at Thu May 28 10:46:40 2009 for ITK by doxygen 1.5.5 written by Dimitri van Heesch, © 1997-2000