00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00035 namespace itk
00036 {
00037 namespace Statistics
00038 {
00039
00046 class MeasurementVectorTraits
00047 {
00048 public:
00049
00056 typedef size_t InstanceIdentifier;
00057
00059 typedef InstanceIdentifier AbsoluteFrequencyType;
00060 typedef NumericTraits< AbsoluteFrequencyType >::RealType RelativeFrequencyType;
00061 typedef NumericTraits< AbsoluteFrequencyType >::AccumulateType TotalAbsoluteFrequencyType;
00062 typedef NumericTraits< RelativeFrequencyType >::AccumulateType TotalRelativeFrequencyType;
00063
00064 typedef unsigned int MeasurementVectorLength;
00065
00066 template<class TValueType, unsigned int VLength>
00067 static void SetLength( FixedArray< TValueType, VLength > &m, const unsigned int s )
00068 {
00069 if( s != VLength )
00070 {
00071 itkGenericExceptionMacro( << "Cannot set the size of a FixedArray of length "
00072 << VLength << " to " << s );
00073 }
00074 m.Fill( NumericTraits< TValueType >::Zero );
00075 }
00076
00077 template<class TValueType, unsigned int VLength>
00078 static void SetLength( FixedArray< TValueType, VLength > *m, const unsigned int s )
00079 {
00080 if( s != VLength )
00081 {
00082 itkGenericExceptionMacro( << "Cannot set the size of a FixedArray of length "
00083 << VLength << " to " << s );
00084 }
00085 m->Fill( NumericTraits< TValueType >::Zero );
00086 }
00087
00088 template< class TValueType >
00089 static void SetLength( Array< TValueType > & m, const unsigned int s )
00090 {
00091 m.SetSize( s );
00092 m.Fill( NumericTraits< TValueType >::Zero );
00093 }
00094
00095 template< class TValueType >
00096 static void SetLength( Array< TValueType > * m, const unsigned int s )
00097 {
00098 m->SetSize( s );
00099 m->Fill( NumericTraits< TValueType >::Zero );
00100 }
00101
00102 template< class TValueType >
00103 static void SetLength( VariableLengthVector< TValueType > & m, const unsigned int s )
00104 {
00105 m.SetSize( s );
00106 m.Fill( NumericTraits< TValueType >::Zero );
00107 }
00108
00109 template< class TValueType >
00110 static void SetLength( VariableLengthVector< TValueType > * m, const unsigned int s )
00111 {
00112 m->SetSize( s );
00113 m->Fill( NumericTraits< TValueType >::Zero );
00114 }
00115
00116 template< class TValueType >
00117 static void SetLength( std::vector< TValueType > & m, const unsigned int s )
00118 {
00119 m.resize( s );
00120 }
00121
00122 template< class TValueType >
00123 static void SetLength( std::vector< TValueType > * m, const unsigned int s )
00124 {
00125 m->resize( s );
00126 }
00127
00128
00129 template< class TValueType, unsigned int VLength >
00130 static MeasurementVectorLength
00131 GetLength( const FixedArray< TValueType, VLength > &)
00132 { return VLength; }
00133
00134 template< class TValueType, unsigned int VLength >
00135 static MeasurementVectorLength
00136 GetLength( const FixedArray< TValueType, VLength > *)
00137 { return VLength; }
00138
00139 template< class TValueType >
00140 static MeasurementVectorLength
00141 GetLength( const Array< TValueType > &m )
00142 {return m.GetSize(); }
00143
00144 template< class TValueType >
00145 static MeasurementVectorLength
00146 GetLength( const Array< TValueType > *m )
00147 {return m->GetSize(); }
00148
00149 template< class TValueType >
00150 static MeasurementVectorLength
00151 GetLength( const VariableLengthVector< TValueType > &m )
00152 {return m.GetSize(); }
00153
00154 template< class TValueType >
00155 static MeasurementVectorLength
00156 GetLength( const VariableLengthVector< TValueType > *m )
00157 {return m->GetSize(); }
00158
00159 template< class TValueType >
00160 static MeasurementVectorLength
00161 GetLength( const std::vector< TValueType > &m )
00162 {return m.size(); }
00163
00164 template< class TValueType >
00165 static MeasurementVectorLength
00166 GetLength( const std::vector< TValueType > *m )
00167 {return m->size(); }
00168
00169
00170 template< class TVectorType >
00171 static bool IsResizable( const TVectorType & )
00172 {
00173
00174
00175
00176
00177
00178 TVectorType m;
00179 MeasurementVectorLength len = GetLength( m );
00180 return ( len == 0 );
00181 }
00182
00183
00184 #if !(defined(_MSC_VER) && (_MSC_VER <= 1200))
00185 template< class TValueType1, unsigned int VLength, class TValueType2, unsigned int VLength2 >
00186 static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > &,
00187 const FixedArray< TValueType2, VLength2 > & , const char *errMsg="Length Mismatch")
00188 {
00189 if ( VLength != VLength2 )
00190 {
00191 itkGenericExceptionMacro( << errMsg );
00192 return 0;
00193 }
00194 return 0;
00195 }
00196
00197 template< class TValueType1, unsigned int VLength, class TValueType2, unsigned int VLength2 >
00198 static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > *,
00199 const FixedArray< TValueType2, VLength2 > *, const char *errMsg="Length Mismatch")
00200 {
00201 if ( VLength != VLength2 )
00202 {
00203 itkGenericExceptionMacro( << errMsg );
00204 return 0;
00205 }
00206 return 0;
00207 }
00208 #endif
00209 template< class TValueType1, class TValueType2 >
00210 static MeasurementVectorLength Assert( const Array< TValueType1 > &a,
00211 const Array< TValueType2 > &b, const char *errMsg="Length Mismatch")
00212 {
00213 if( b.Size() != a.Size() )
00214 {
00215 itkGenericExceptionMacro( << errMsg );
00216 return 0;
00217 }
00218 return 0;
00219 }
00220
00221 template< class TValueType1, class TValueType2 >
00222 static MeasurementVectorLength Assert( const Array< TValueType1 > *a,
00223 const Array< TValueType2 > *b, const char *errMsg="Length Mismatch")
00224 {
00225 if( b->Size() != a->Size() )
00226 {
00227 itkGenericExceptionMacro( << errMsg );
00228 return 0;
00229 }
00230 return 0;
00231 }
00232
00233
00234 template< class TValueType1, class TValueType2 >
00235 static MeasurementVectorLength Assert( const VariableLengthVector< TValueType1 > &a,
00236 const VariableLengthVector< TValueType2 > &b, const char *errMsg="Length Mismatch")
00237 {
00238 if( b.Size() != a.Size() )
00239 {
00240 itkGenericExceptionMacro( << errMsg );
00241 return 0;
00242 }
00243 return 0;
00244 }
00245
00246 template< class TValueType1, class TValueType2 >
00247 static MeasurementVectorLength Assert( const VariableLengthVector< TValueType1 > *a,
00248 const VariableLengthVector< TValueType2 > *b, const char *errMsg="Length Mismatch")
00249 {
00250 if( b->Size() != a->Size() )
00251 {
00252 itkGenericExceptionMacro( << errMsg );
00253 return 0;
00254 }
00255 return 0;
00256 }
00257
00258
00259 template< class TValueType1, class TValueType2 >
00260 static MeasurementVectorLength Assert( const std::vector< TValueType1 > &a,
00261 const std::vector< TValueType2 > &b, const char *errMsg="Length Mismatch")
00262 {
00263 if( b.size() != a.size() )
00264 {
00265 itkGenericExceptionMacro( << errMsg );
00266 return 0;
00267 }
00268 return 0;
00269 }
00270
00271 template< class TValueType1, class TValueType2 >
00272 static MeasurementVectorLength Assert( const std::vector< TValueType1 > *a,
00273 const std::vector< TValueType2 > *b, const char *errMsg="Length Mismatch")
00274 {
00275 if( b->size() != a->size() )
00276 {
00277 itkGenericExceptionMacro( << errMsg );
00278 return 0;
00279 }
00280 return 0;
00281 }
00282
00283
00284 template< class TValueType1, unsigned int VLength, class TValueType2 >
00285 static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > &,
00286 const Array< TValueType2 > &b, const char *errMsg="Length Mismatch")
00287 {
00288 if( b.Size() == 0 )
00289 {
00290 return VLength;
00291 }
00292 if( b.Size() != 0 )
00293 {
00294 if (b.Size() != VLength)
00295 {
00296 itkGenericExceptionMacro( << errMsg );
00297 return 0;
00298 }
00299 }
00300 return 0;
00301 }
00302
00303 template< class TValueType1, unsigned int VLength, class TValueType2 >
00304 static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > *,
00305 const Array< TValueType2 > *b, const char *errMsg="Length Mismatch")
00306 {
00307 if( b->Size() == 0 )
00308 {
00309 return VLength;
00310 }
00311 else if (b->Size() != VLength)
00312 {
00313 itkGenericExceptionMacro( << errMsg );
00314 return 0;
00315 }
00316 return 0;
00317 }
00318
00319 template< class TValueType1, unsigned int VLength, class TValueType2 >
00320 static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > &,
00321 const VariableLengthVector< TValueType2 > &b, const char *errMsg="Length Mismatch")
00322 {
00323 if( b.Size() == 0 )
00324 {
00325 return VLength;
00326 }
00327 if( b.Size() != 0 )
00328 {
00329 if (b.Size() != VLength)
00330 {
00331 itkGenericExceptionMacro( << errMsg );
00332 return 0;
00333 }
00334 }
00335 return 0;
00336 }
00337
00338 template< class TValueType1, unsigned int VLength, class TValueType2 >
00339 static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > *,
00340 const VariableLengthVector< TValueType2 > *b, const char *errMsg="Length Mismatch")
00341 {
00342 if( b->Size() == 0 )
00343 {
00344 return VLength;
00345 }
00346 else if (b->Size() != VLength)
00347 {
00348 itkGenericExceptionMacro( << errMsg );
00349 return 0;
00350 }
00351 return 0;
00352 }
00353
00354 template< class TValueType1, unsigned int VLength, class TValueType2 >
00355 static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > &,
00356 const std::vector< TValueType2 > &b, const char *errMsg="Length Mismatch")
00357 {
00358 if( b.size() == 0 )
00359 {
00360 return VLength;
00361 }
00362 if( b.size() != 0 )
00363 {
00364 if (b.size() != VLength)
00365 {
00366 itkGenericExceptionMacro( << errMsg );
00367 return 0;
00368 }
00369 }
00370 return 0;
00371 }
00372
00373 template< class TValueType1, unsigned int VLength, class TValueType2 >
00374 static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > *,
00375 const std::vector< TValueType2 > *b, const char *errMsg="Length Mismatch")
00376 {
00377 if( b->size() == 0 )
00378 {
00379 return VLength;
00380 }
00381 else if (b->size() != VLength)
00382 {
00383 itkGenericExceptionMacro( << errMsg );
00384 return 0;
00385 }
00386 return 0;
00387 }
00388
00389
00390 template< class TValueType1, unsigned int VLength>
00391 static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > &,
00392 const MeasurementVectorLength l, const char *errMsg="Length Mismatch")
00393 {
00394 if( l == 0 )
00395 {
00396 return VLength;
00397 }
00398 else if( l != VLength )
00399 {
00400 itkGenericExceptionMacro( << errMsg );
00401 return 0;
00402 }
00403 return 0;
00404 }
00405
00406 template< class TValueType1, unsigned int VLength>
00407 static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > *,
00408 const MeasurementVectorLength l, const char *errMsg="Length Mismatch")
00409 {
00410 if( l == 0 )
00411 {
00412 return VLength;
00413 }
00414 else if( l != VLength )
00415 {
00416 itkGenericExceptionMacro( << errMsg );
00417 return 0;
00418 }
00419 return 0;
00420 }
00421
00422 template< class TValueType >
00423 static MeasurementVectorLength Assert( const Array< TValueType > &a,
00424 const MeasurementVectorLength l, const char *errMsg="Length Mismatch")
00425 {
00426 if( ( ( l != 0 ) && ( a.Size() != l ) ) || ( a.Size() == 0 ) )
00427 {
00428 itkGenericExceptionMacro( << errMsg );
00429 }
00430 else if( l == 0 )
00431 {
00432 return a.Size();
00433 }
00434 return 0;
00435 }
00436
00437 template< class TValueType >
00438 static MeasurementVectorLength Assert( const Array< TValueType > *a,
00439 const MeasurementVectorLength l, const char *errMsg="Length Mismatch")
00440 {
00441 if( ( ( l != 0 ) && ( a->Size() != l )) || ( a->Size() == 0 ) )
00442 {
00443 itkGenericExceptionMacro( << errMsg );
00444 }
00445 else if( l == 0 )
00446 {
00447 return a->Size();
00448 }
00449 return 0;
00450 }
00451
00452 template< class TValueType >
00453 static MeasurementVectorLength Assert( const VariableLengthVector< TValueType > &a,
00454 const MeasurementVectorLength l, const char *errMsg="Length Mismatch")
00455 {
00456 if( ( ( l != 0 ) && ( a.Size() != l ) ) || ( a.Size() == 0 ) )
00457 {
00458 itkGenericExceptionMacro( << errMsg );
00459 }
00460 else if( l == 0 )
00461 {
00462 return a.Size();
00463 }
00464 return 0;
00465 }
00466
00467 template< class TValueType >
00468 static MeasurementVectorLength Assert( const VariableLengthVector< TValueType > *a,
00469 const MeasurementVectorLength l, const char *errMsg="Length Mismatch")
00470 {
00471 if( ( ( l != 0 ) && ( a->Size() != l ) ) || ( a->Size() == 0 ) )
00472 {
00473 itkGenericExceptionMacro( << errMsg );
00474 }
00475 else if( l == 0 )
00476 {
00477 return a->Size();
00478 }
00479 return 0;
00480 }
00481 template< class TValueType >
00482 static MeasurementVectorLength Assert( const std::vector< TValueType > &a,
00483 const MeasurementVectorLength l, const char *errMsg="Length Mismatch")
00484 {
00485 if( ( ( l != 0 ) && ( a.size() != l ) ) || ( a.size() == 0 ) )
00486 {
00487 itkGenericExceptionMacro( << errMsg );
00488 }
00489 else if( l == 0 )
00490 {
00491 return a.size();
00492 }
00493 return 0;
00494 }
00495
00496 template< class TValueType >
00497 static MeasurementVectorLength Assert( const std::vector< TValueType > *a,
00498 const MeasurementVectorLength l, const char *errMsg="Length Mismatch")
00499 {
00500 if( ( ( l != 0 ) && ( a->size() != l ) ) || ( a->size() == 0 ) )
00501 {
00502 itkGenericExceptionMacro( << errMsg );
00503 }
00504 else if( l == 0 )
00505 {
00506 return a->size();
00507 }
00508 return 0;
00509 }
00510
00511 template< class TArrayType >
00512 static void Assign( TArrayType & m, const TArrayType & v )
00513 {
00514 m = v;
00515 }
00516
00517 template< class TValueType, unsigned int VLength >
00518 static void Assign( FixedArray< TValueType, VLength > & m, const TValueType & v )
00519 {
00520 m[0] = v;
00521 }
00522
00523 };
00524
00530 template < class TMeasurementVector >
00531 class MeasurementVectorTraitsTypes
00532 {
00533 public:
00534 typedef typename TMeasurementVector::ValueType ValueType;
00535 };
00536
00537
00538
00539
00540 #define itkMeasurementVectorTraitsTypesMacro(T) \
00541 template <> \
00542 class MeasurementVectorTraitsTypes< std::vector< T > > \
00543 { \
00544 public: \
00545 typedef T ValueType; \
00546 };
00547
00548 #if defined( _MSC_VER ) && ( _MSC_VER < 1310 )
00549 itkMeasurementVectorTraitsTypesMacro( char );
00550 itkMeasurementVectorTraitsTypesMacro( unsigned char );
00551 itkMeasurementVectorTraitsTypesMacro( short );
00552 itkMeasurementVectorTraitsTypesMacro( unsigned short );
00553 itkMeasurementVectorTraitsTypesMacro( int );
00554 itkMeasurementVectorTraitsTypesMacro( unsigned int );
00555 itkMeasurementVectorTraitsTypesMacro( long );
00556 itkMeasurementVectorTraitsTypesMacro( unsigned long );
00557 itkMeasurementVectorTraitsTypesMacro( float );
00558 itkMeasurementVectorTraitsTypesMacro( double );
00559 #else
00560 template< class T >
00561 class MeasurementVectorTraitsTypes< std::vector< T > >
00562 {
00563 public:
00564 typedef T ValueType;
00565 };
00566 #endif
00567
00571 template<class TPixelType>
00572 class MeasurementVectorPixelTraits
00573 {
00574 public:
00575
00576 typedef TPixelType MeasurementVectorType;
00577 };
00578
00579
00580 template<>
00581 class MeasurementVectorPixelTraits<char>
00582 {
00583 public:
00584 typedef FixedArray< char, 1 > MeasurementVectorType;
00585 };
00586
00587 template<>
00588 class MeasurementVectorPixelTraits<unsigned char>
00589 {
00590 public:
00591 typedef FixedArray< unsigned char, 1 > MeasurementVectorType;
00592 };
00593
00594 template<>
00595 class MeasurementVectorPixelTraits<signed char>
00596 {
00597 public:
00598 typedef FixedArray< signed char, 1 > MeasurementVectorType;
00599 };
00600
00601 template<>
00602 class MeasurementVectorPixelTraits<unsigned short>
00603 {
00604 public:
00605 typedef FixedArray< unsigned short, 1 > MeasurementVectorType;
00606 };
00607
00608 template<>
00609 class MeasurementVectorPixelTraits<signed short>
00610 {
00611 public:
00612 typedef FixedArray< signed short, 1 > MeasurementVectorType;
00613 };
00614
00615 template<>
00616 class MeasurementVectorPixelTraits<unsigned int>
00617 {
00618 public:
00619 typedef FixedArray< unsigned int, 1 > MeasurementVectorType;
00620 };
00621
00622 template<>
00623 class MeasurementVectorPixelTraits<signed int>
00624 {
00625 public:
00626 typedef FixedArray< signed int, 1 > MeasurementVectorType;
00627 };
00628
00629 template<>
00630 class MeasurementVectorPixelTraits<unsigned long>
00631 {
00632 public:
00633 typedef FixedArray< unsigned long, 1 > MeasurementVectorType;
00634 };
00635
00636 template<>
00637 class MeasurementVectorPixelTraits<signed long>
00638 {
00639 public:
00640 typedef FixedArray< signed long, 1 > MeasurementVectorType;
00641 };
00642
00643 template<>
00644 class MeasurementVectorPixelTraits<float>
00645 {
00646 public:
00647 typedef FixedArray< float, 1 > MeasurementVectorType;
00648 };
00649
00650 template<>
00651 class MeasurementVectorPixelTraits<double>
00652 {
00653 public:
00654 typedef FixedArray< double, 1 > MeasurementVectorType;
00655 };
00656
00657
00658 }
00659 }
00660
00661 #endif // __itkMeasurementVectorTraits_h
00662