ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkMeasurementVectorTraits.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 __itkMeasurementVectorTraits_h
00019 #define __itkMeasurementVectorTraits_h
00020 
00021 #include "itkVariableLengthVector.h"
00022 #include "vnl/vnl_vector_fixed.h"
00023 #include "itkRGBPixel.h"
00024 #include "itkMatrix.h"
00025 #include "itkVariableSizeMatrix.h"
00026 #include "itkNumericTraits.h"
00027 #include "itkNumericTraitsStdVector.h"
00028 #include "itkSize.h"
00029 #include <vector>
00030 
00031 namespace itk
00032 {
00033 namespace Statistics
00034 {
00041 class MeasurementVectorTraits
00042 {
00043 public:
00044 
00051   typedef IdentifierType  InstanceIdentifier;
00052 
00054   typedef InstanceIdentifier                                     AbsoluteFrequencyType;
00055   typedef NumericTraits< AbsoluteFrequencyType >::RealType       RelativeFrequencyType;
00056   typedef NumericTraits< AbsoluteFrequencyType >::AccumulateType TotalAbsoluteFrequencyType;
00057   typedef NumericTraits< RelativeFrequencyType >::AccumulateType TotalRelativeFrequencyType;
00058 
00059   typedef unsigned int MeasurementVectorLength;
00060 
00061   template< class TVectorType >
00062   static bool IsResizable(const TVectorType &)
00063   {
00064     // Test whether the vector type is resizable or not
00065     //
00066     // If the default constructor creates a vector of
00067     // length zero, we assume that it is resizable,
00068     // otherwise that is a pretty useless measurement vector.
00069     TVectorType             m;
00070     MeasurementVectorLength len = NumericTraits<TVectorType>::GetLength(m);
00071 
00072     return ( len == 0 );
00073   }
00074 
00075   template< class TValueType1, unsigned int VLength, class TValueType2, unsigned int VLength2 >
00076   static MeasurementVectorLength Assert(const FixedArray< TValueType1, VLength > &,
00077                                         const FixedArray< TValueType2, VLength2 > &,
00078                                         const char *errMsg = "Length Mismatch")
00079   {
00080     if ( VLength != VLength2 )
00081       {
00082       itkGenericExceptionMacro(<< errMsg);
00083       }
00084     return 0;
00085   }
00086 
00087   template< class TValueType1, unsigned int VLength, class TValueType2, unsigned int VLength2 >
00088   static MeasurementVectorLength Assert(const FixedArray< TValueType1, VLength > *,
00089                                         const FixedArray< TValueType2, VLength2 > *,
00090                                         const char *errMsg = "Length Mismatch")
00091   {
00092     if ( VLength != VLength2 )
00093       {
00094       itkGenericExceptionMacro(<< errMsg);
00095       }
00096     return 0;
00097   }
00098 
00099   template< class TValueType1, class TValueType2 >
00100   static MeasurementVectorLength Assert(const Array< TValueType1 > & a,
00101                                         const Array< TValueType2 > & b, const char *errMsg = "Length Mismatch")
00102   {
00103     if ( b.Size() != a.Size() )
00104       {
00105       itkGenericExceptionMacro(<< errMsg);
00106       }
00107     return 0;
00108   }
00109 
00110   template< class TValueType1, class TValueType2 >
00111   static MeasurementVectorLength Assert(const Array< TValueType1 > *a,
00112                                         const Array< TValueType2 > *b, const char *errMsg = "Length Mismatch")
00113   {
00114     if ( b->Size() != a->Size() )
00115       {
00116       itkGenericExceptionMacro(<< errMsg);
00117       }
00118     return 0;
00119   }
00120 
00121   template< class TValueType1, class TValueType2 >
00122   static MeasurementVectorLength Assert(const VariableLengthVector< TValueType1 > & a,
00123                                         const VariableLengthVector< TValueType2 > & b,
00124                                         const char *errMsg = "Length Mismatch")
00125   {
00126     if ( b.Size() != a.Size() )
00127       {
00128       itkGenericExceptionMacro(<< errMsg);
00129       }
00130     return 0;
00131   }
00132 
00133   template< class TValueType1, class TValueType2 >
00134   static MeasurementVectorLength Assert(const VariableLengthVector< TValueType1 > *a,
00135                                         const VariableLengthVector< TValueType2 > *b,
00136                                         const char *errMsg = "Length Mismatch")
00137   {
00138     if ( b->Size() != a->Size() )
00139       {
00140       itkGenericExceptionMacro(<< errMsg);
00141       }
00142     return 0;
00143   }
00144 
00145   template< class TValueType1, class TValueType2 >
00146   static MeasurementVectorLength Assert(const std::vector< TValueType1 > & a,
00147                                         const std::vector< TValueType2 > & b, const char *errMsg = "Length Mismatch")
00148   {
00149     if ( b.size() != a.size() )
00150       {
00151       itkGenericExceptionMacro(<< errMsg);
00152       }
00153     return 0;
00154   }
00155 
00156   template< class TValueType1, class TValueType2 >
00157   static MeasurementVectorLength Assert(const std::vector< TValueType1 > *a,
00158                                         const std::vector< TValueType2 > *b, const char *errMsg = "Length Mismatch")
00159   {
00160     if ( b->size() != a->size() )
00161       {
00162       itkGenericExceptionMacro(<< errMsg);
00163       }
00164     return 0;
00165   }
00166 
00167   template< class TValueType1, unsigned int VLength, class TValueType2 >
00168   static MeasurementVectorLength Assert(const FixedArray< TValueType1, VLength > &,
00169                                         const Array< TValueType2 > & b, const char *errMsg = "Length Mismatch")
00170   {
00171     if ( b.Size() == 0 )
00172       {
00173       return VLength;
00174       }
00175     if ( b.Size() != 0 )
00176       {
00177       if ( b.Size() != VLength )
00178         {
00179         itkGenericExceptionMacro(<< errMsg);
00180         }
00181       }
00182     return 0;
00183   }
00184 
00185   template< class TValueType1, unsigned int VLength, class TValueType2 >
00186   static MeasurementVectorLength Assert(const FixedArray< TValueType1, VLength > *,
00187                                         const Array< TValueType2 > *b, const char *errMsg = "Length Mismatch")
00188   {
00189     if ( b->Size() == 0 )
00190       {
00191       return VLength;
00192       }
00193     else if ( b->Size() != VLength )
00194       {
00195       itkGenericExceptionMacro(<< errMsg);
00196       }
00197     return 0;
00198   }
00199 
00200   template< class TValueType1, unsigned int VLength, class TValueType2 >
00201   static MeasurementVectorLength Assert(const FixedArray< TValueType1, VLength > &,
00202                                         const VariableLengthVector< TValueType2 > & b,
00203                                         const char *errMsg = "Length Mismatch")
00204   {
00205     if ( b.Size() == 0 )
00206       {
00207       return VLength;
00208       }
00209     if ( b.Size() != 0 )
00210       {
00211       if ( b.Size() != VLength )
00212         {
00213         itkGenericExceptionMacro(<< errMsg);
00214         }
00215       }
00216     return 0;
00217   }
00218 
00219   template< class TValueType1, unsigned int VLength, class TValueType2 >
00220   static MeasurementVectorLength Assert(const FixedArray< TValueType1, VLength > *,
00221                                         const VariableLengthVector< TValueType2 > *b,
00222                                         const char *errMsg = "Length Mismatch")
00223   {
00224     if ( b->Size() == 0 )
00225       {
00226       return VLength;
00227       }
00228     else if ( b->Size() != VLength )
00229       {
00230       itkGenericExceptionMacro(<< errMsg);
00231       }
00232     return 0;
00233   }
00234 
00235   template< class TValueType1, unsigned int VLength, class TValueType2 >
00236   static MeasurementVectorLength Assert(const FixedArray< TValueType1, VLength > &,
00237                                         const std::vector< TValueType2 > & b, const char *errMsg = "Length Mismatch")
00238   {
00239     if ( b.size() == 0 )
00240       {
00241       return VLength;
00242       }
00243     if ( b.size() != 0 )
00244       {
00245       if ( b.size() != VLength )
00246         {
00247         itkGenericExceptionMacro(<< errMsg);
00248         }
00249       }
00250     return 0;
00251   }
00252 
00253   template< class TValueType1, unsigned int VLength, class TValueType2 >
00254   static MeasurementVectorLength Assert(const FixedArray< TValueType1, VLength > *,
00255                                         const std::vector< TValueType2 > *b, const char *errMsg = "Length Mismatch")
00256   {
00257     if ( b->size() == 0 )
00258       {
00259       return VLength;
00260       }
00261     else if ( b->size() != VLength )
00262       {
00263       itkGenericExceptionMacro(<< errMsg);
00264       }
00265     return 0;
00266   }
00267 
00268   template< class TValueType1, unsigned int VLength >
00269   static MeasurementVectorLength Assert(const FixedArray< TValueType1, VLength > &,
00270                                         const MeasurementVectorLength l, const char *errMsg = "Length Mismatch")
00271   {
00272     if ( l == 0 )
00273       {
00274       return VLength;
00275       }
00276     else if ( l != VLength )
00277       {
00278       itkGenericExceptionMacro(<< errMsg);
00279       }
00280     return 0;
00281   }
00282 
00283   template< class TValueType1, unsigned int VLength >
00284   static MeasurementVectorLength Assert(const FixedArray< TValueType1, VLength > *,
00285                                         const MeasurementVectorLength l, const char *errMsg = "Length Mismatch")
00286   {
00287     if ( l == 0 )
00288       {
00289       return VLength;
00290       }
00291     else if ( l != VLength )
00292       {
00293       itkGenericExceptionMacro(<< errMsg);
00294       }
00295     return 0;
00296   }
00297 
00298   template< class TValueType >
00299   static MeasurementVectorLength Assert(const Array< TValueType > & a,
00300                                         const MeasurementVectorLength l, const char *errMsg = "Length Mismatch")
00301   {
00302     if ( ( ( l != 0 ) && ( a.Size() != l ) ) || ( a.Size() == 0 ) )
00303       {
00304       itkGenericExceptionMacro(<< errMsg);
00305       }
00306     else if ( l == 0 )
00307       {
00308       return a.Size();
00309       }
00310     return 0;
00311   }
00312 
00313   template< class TValueType >
00314   static MeasurementVectorLength Assert(const Array< TValueType > *a,
00315                                         const MeasurementVectorLength l, const char *errMsg = "Length Mismatch")
00316   {
00317     if ( ( ( l != 0 ) && ( a->Size() != l ) ) || ( a->Size() == 0 ) )
00318       {
00319       itkGenericExceptionMacro(<< errMsg);
00320       }
00321     else if ( l == 0 )
00322       {
00323       return a->Size();
00324       }
00325     return 0;
00326   }
00327 
00328   template< class TValueType >
00329   static MeasurementVectorLength Assert(const VariableLengthVector< TValueType > & a,
00330                                         const MeasurementVectorLength l, const char *errMsg = "Length Mismatch")
00331   {
00332     if ( ( ( l != 0 ) && ( a.Size() != l ) ) || ( a.Size() == 0 ) )
00333       {
00334       itkGenericExceptionMacro(<< errMsg);
00335       }
00336     else if ( l == 0 )
00337       {
00338       return a.Size();
00339       }
00340     return 0;
00341   }
00342 
00343   template< class TValueType >
00344   static MeasurementVectorLength Assert(const VariableLengthVector< TValueType > *a,
00345                                         const MeasurementVectorLength l, const char *errMsg = "Length Mismatch")
00346   {
00347     if ( ( ( l != 0 ) && ( a->Size() != l ) ) || ( a->Size() == 0 ) )
00348       {
00349       itkGenericExceptionMacro(<< errMsg);
00350       }
00351     else if ( l == 0 )
00352       {
00353       return a->Size();
00354       }
00355     return 0;
00356   }
00357 
00358   template< class TValueType >
00359   static MeasurementVectorLength Assert(const std::vector< TValueType > & a,
00360                                         const MeasurementVectorLength l, const char *errMsg = "Length Mismatch")
00361   {
00362     if ( ( ( l != 0 ) && ( a.size() != l ) ) || ( a.size() == 0 ) )
00363       {
00364       itkGenericExceptionMacro(<< errMsg);
00365       }
00366     else if ( l == 0 )
00367       {
00368       return a.size();
00369       }
00370     return 0;
00371   }
00372 
00373   template< class TValueType >
00374   static MeasurementVectorLength Assert(const std::vector< TValueType > *a,
00375                                         const MeasurementVectorLength l, const char *errMsg = "Length Mismatch")
00376   {
00377     if ( ( ( l != 0 ) && ( a->size() != l ) ) || ( a->size() == 0 ) )
00378       {
00379       itkGenericExceptionMacro(<< errMsg);
00380       }
00381     else if ( l == 0 )
00382       {
00383       return a->size();
00384       }
00385     return 0;
00386   }
00387 
00388   template< class TArrayType >
00389   static void  Assign(TArrayType & m, const TArrayType & v)
00390   {
00391     m = v;
00392   }
00393 
00394   template< class TValueType, unsigned int VLength >
00395   static void  Assign(FixedArray< TValueType, VLength > & m, const TValueType & v)
00396   {
00397     m[0] = v;
00398   }
00399 };
00400 
00407 template< class TMeasurementVector >
00408 class MeasurementVectorTraitsTypes
00409 {
00410 public:
00411   typedef typename TMeasurementVector::ValueType ValueType;
00412 };
00413 
00414 template< class T >
00415 class MeasurementVectorTraitsTypes< std::vector< T > >
00416 {
00417 public:
00418   typedef T ValueType;
00419 };
00420 
00424 template< class TPixelType >
00425 class MeasurementVectorPixelTraits
00426 {
00427 public:
00428   /* type of the vector that matches this pixel type */
00429   typedef TPixelType MeasurementVectorType;
00430 };
00431 
00437 template< >
00438 class MeasurementVectorPixelTraits< char >
00439 {
00440 public:
00441   typedef FixedArray< char, 1 > MeasurementVectorType;
00442 };
00443 
00444 template< >
00445 class MeasurementVectorPixelTraits< unsigned char >
00446 {
00447 public:
00448   typedef FixedArray< unsigned char, 1 > MeasurementVectorType;
00449 };
00450 
00451 template< >
00452 class MeasurementVectorPixelTraits< signed char >
00453 {
00454 public:
00455   typedef FixedArray< signed char, 1 > MeasurementVectorType;
00456 };
00457 
00458 template< >
00459 class MeasurementVectorPixelTraits< unsigned short >
00460 {
00461 public:
00462   typedef FixedArray< unsigned short, 1 > MeasurementVectorType;
00463 };
00464 
00465 template< >
00466 class MeasurementVectorPixelTraits< signed short >
00467 {
00468 public:
00469   typedef FixedArray< signed short, 1 > MeasurementVectorType;
00470 };
00471 
00472 template< >
00473 class MeasurementVectorPixelTraits< unsigned int >
00474 {
00475 public:
00476   typedef FixedArray< unsigned int, 1 > MeasurementVectorType;
00477 };
00478 
00479 template< >
00480 class MeasurementVectorPixelTraits< signed int >
00481 {
00482 public:
00483   typedef FixedArray< signed int, 1 > MeasurementVectorType;
00484 };
00485 
00486 template< >
00487 class MeasurementVectorPixelTraits< unsigned long >
00488 {
00489 public:
00490   typedef FixedArray< unsigned long, 1 > MeasurementVectorType;
00491 };
00492 
00493 template< >
00494 class MeasurementVectorPixelTraits< signed long >
00495 {
00496 public:
00497   typedef FixedArray< signed long, 1 > MeasurementVectorType;
00498 };
00499 
00500 template< >
00501 class MeasurementVectorPixelTraits< float >
00502 {
00503 public:
00504   typedef FixedArray< float, 1 > MeasurementVectorType;
00505 };
00506 
00507 template< >
00508 class MeasurementVectorPixelTraits< double >
00509 {
00510 public:
00511   typedef FixedArray< double, 1 > MeasurementVectorType;
00512 };
00513 
00516 } // namespace Statistics
00517 } // namespace itk
00518 
00519 #endif  // __itkMeasurementVectorTraits_h
00520