]> Creatis software - clitk.git/blob - common/clitkCommonGenericFilter.h
952c90b0df3c2902811bad65c5412bd5bf339aac
[clitk.git] / common / clitkCommonGenericFilter.h
1 #ifndef CLITKCOMMONGENERICFILTER_H
2 #define CLITKCOMMONGENERICFILTER_H
3 #include "clitkCommon.h"
4
5 /*--------------------------------------------------------------------
6   DISCLAIMER : I obviously know how to make this mess much clearer and
7   shorter, but I fear its being too long to put in this margin ...
8   D.S.
9   -------------------------------------------------------------------- */
10
11 namespace clitk {
12   
13   //--------------------------------------------------------------------
14   template<class FilterType>
15   class GenericFilterFunctorBase {
16   public:
17     GenericFilterFunctorBase(FilterType * f) { mFilter = f; }
18     virtual void Execute()= 0;
19     FilterType * mFilter;
20   };
21  
22   //--------------------------------------------------------------------
23   template<class FilterType, class InputImageType>
24   class GenericFilterFunctorWithDimAndPixelType: public GenericFilterFunctorBase<FilterType> {
25   public:
26     GenericFilterFunctorWithDimAndPixelType(FilterType * f): GenericFilterFunctorBase<FilterType>(f) {}
27     virtual void Execute() {       
28       GenericFilterFunctorBase<FilterType>::mFilter->template UpdateWithInputImageType<InputImageType>();
29     }
30   };
31
32   //--------------------------------------------------------------------
33   template<class FilterType>
34   class ImageTypesManager  {
35   public:
36     typedef std::map<std::string, GenericFilterFunctorBase<FilterType>*> MapOfPixelTypeToFunctionType;
37     typedef std::map<unsigned int, MapOfPixelTypeToFunctionType> MapOfImageComponentsToFunctionType;
38     typedef std::map<unsigned int, MapOfImageComponentsToFunctionType> MapOfImageDimensionToFunctionType;
39     MapOfImageDimensionToFunctionType mMapOfImageTypeToFunction;
40
41     ImageTypesManager(FilterType * f) { mFilter = f;  }
42     virtual void DoIt(int dim, int ncomp, std::string pixelname) {
43       // std::cout << "ImageTypesManager DoIt " << dim << " " << pixelname << std::endl;
44       if (mMapOfImageTypeToFunction[dim][ncomp][pixelname])
45         mMapOfImageTypeToFunction[dim][ncomp][pixelname]->Execute();
46     }
47     template<unsigned int Dim, unsigned int NComp, class PixelType>
48     void AddNewDimensionAndPixelType() {
49         typedef itk::Image<itk::Vector<PixelType,NComp>,Dim> InputImageType;
50         mMapOfImageTypeToFunction[Dim][NComp][ GetTypeAsString<PixelType>() ] = 
51             new GenericFilterFunctorWithDimAndPixelType<FilterType, InputImageType>(mFilter);
52     }
53     /// Specialization for NComp == 1
54     template<unsigned int Dim, class PixelType>
55     void AddNewDimensionAndPixelType() {
56         typedef itk::Image<PixelType,Dim> InputImageType;
57         mMapOfImageTypeToFunction[Dim][1][ GetTypeAsString<PixelType>() ] = 
58             new GenericFilterFunctorWithDimAndPixelType<FilterType, InputImageType>(mFilter);
59     }
60     FilterType * mFilter;
61   };
62   //--------------------------------------------------------------------
63
64 } // end namespace
65
66 #endif // end CLITKCOMMONGENERICFILTER_H