]> Creatis software - clitk.git/blob - common/clitkCommonGenericFilter.h
almost finished the work on multiple components
[clitk.git] / common / clitkCommonGenericFilter.h
1 /*=========================================================================
2
3   Program:   clitk
4   Module:    $RCSfile: clitkCommonGenericFilter.h,v $
5   Language:  C++
6   Date:      $Date: 2010/02/09 14:19:28 $
7   Version:   $Revision: 1.3 $
8   Author :   Joel Schaerer <joel.schaerer@creatis.insa-lyon.fr>
9              David Sarrut <david.sarrut@creatis.insa-lyon.fr>
10
11   Copyright (C) 2008
12   Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
13   CREATIS-LRMN http://www.creatis.insa-lyon.fr
14
15   This program is free software: you can redistribute it and/or modify
16   it under the terms of the GNU General Public License as published by
17   the Free Software Foundation, version 3 of the License.
18
19   This program is distributed in the hope that it will be useful,
20   but WITHOUT ANY WARRANTY; without even the implied warranty of
21   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22   GNU General Public License for more details.
23
24   You should have received a copy of the GNU General Public License
25   along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
27   =========================================================================*/
28
29 #ifndef CLITKCOMMONGENERICFILTER_H
30 #define CLITKCOMMONGENERICFILTER_H
31
32 // clitk include
33 #include "clitkCommon.h"
34
35 /*--------------------------------------------------------------------
36   DISCLAIMER : I obviously know how to make this mess much clearer and
37   shorter, but I fear its being too long to put in this margin ...
38   D.S.
39   -------------------------------------------------------------------- */
40
41 namespace clitk {
42   
43   //--------------------------------------------------------------------
44   template<class FilterType>
45   class GenericFilterFunctorBase {
46   public:
47     GenericFilterFunctorBase(FilterType * f) { mFilter = f; }
48     virtual void Execute()= 0;
49     FilterType * mFilter;
50   };
51  
52   //--------------------------------------------------------------------
53   template<class FilterType, class InputImageType>
54   class GenericFilterFunctorWithDimAndPixelType: public GenericFilterFunctorBase<FilterType> {
55   public:
56     GenericFilterFunctorWithDimAndPixelType(FilterType * f): GenericFilterFunctorBase<FilterType>(f) {}
57     virtual void Execute() {       
58       GenericFilterFunctorBase<FilterType>::mFilter->template UpdateWithInputImageType<InputImageType>();
59     }
60   };
61
62   //--------------------------------------------------------------------
63   template<class FilterType>
64   class ImageTypesManager  {
65   public:
66     typedef std::map<std::string, GenericFilterFunctorBase<FilterType>*> MapOfPixelTypeToFunctionType;
67     typedef std::map<unsigned int, MapOfPixelTypeToFunctionType> MapOfImageComponentsToFunctionType;
68     typedef std::map<unsigned int, MapOfImageComponentsToFunctionType> MapOfImageDimensionToFunctionType;
69     MapOfImageDimensionToFunctionType mMapOfImageTypeToFunction;
70
71     ImageTypesManager(FilterType * f) { mFilter = f;  }
72     virtual void DoIt(int dim, int ncomp, std::string pixelname) {
73       // std::cout << "ImageTypesManager DoIt " << dim << " " << pixelname << std::endl;
74       if (mMapOfImageTypeToFunction[dim][ncomp][pixelname])
75         mMapOfImageTypeToFunction[dim][ncomp][pixelname]->Execute();
76     }
77     template<unsigned int Dim, unsigned int NComp, class PixelType>
78     void AddNewDimensionAndPixelType() {
79         typedef itk::Image<itk::Vector<PixelType,NComp>,Dim> InputImageType;
80         mMapOfImageTypeToFunction[Dim][NComp][ GetTypeAsString<PixelType>() ] = 
81             new GenericFilterFunctorWithDimAndPixelType<FilterType, InputImageType>(mFilter);
82     }
83     /// Specialization for NComp == 1
84     template<unsigned int Dim, class PixelType>
85     void AddNewDimensionAndPixelType() {
86         typedef itk::Image<PixelType,Dim> InputImageType;
87         mMapOfImageTypeToFunction[Dim][1][ GetTypeAsString<PixelType>() ] = 
88             new GenericFilterFunctorWithDimAndPixelType<FilterType, InputImageType>(mFilter);
89     }
90     FilterType * mFilter;
91   };
92   //--------------------------------------------------------------------
93
94 } // end namespace
95
96 #endif // end CLITKCOMMONGENERICFILTER_H