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