]> Creatis software - clitk.git/blob - common/clitkCommonGenericFilter.h
- correct crop 2D bug
[clitk.git] / common / clitkCommonGenericFilter.h
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18
19 #ifndef CLITKCOMMONGENERICFILTER_H
20 #define CLITKCOMMONGENERICFILTER_H
21
22 #include "clitkCommon.h"
23
24 /*--------------------------------------------------------------------
25   DISCLAIMER : I obviously know how to make this mess much clearer and
26   shorter, but I fear its being too long to put in this margin ...
27   D.S.
28   -------------------------------------------------------------------- */
29
30 namespace clitk {
31   
32   //--------------------------------------------------------------------
33   template<class FilterType>
34   class GenericFilterFunctorBase {
35   public:
36     GenericFilterFunctorBase(FilterType * f) { mFilter = f; }
37     virtual ~GenericFilterFunctorBase() { delete mFilter; }
38     virtual void Execute()= 0;
39     FilterType * mFilter;
40   };
41  
42   //--------------------------------------------------------------------
43   template<class FilterType, class InputImageType>
44   class GenericFilterFunctorWithDimAndPixelType: public GenericFilterFunctorBase<FilterType> {
45   public:
46     GenericFilterFunctorWithDimAndPixelType(FilterType * f): GenericFilterFunctorBase<FilterType>(f) {}
47     virtual void Execute() {       
48       GenericFilterFunctorBase<FilterType>::mFilter->template UpdateWithInputImageType<InputImageType>();
49     }
50   };
51
52   //--------------------------------------------------------------------
53   template<class FilterType>
54   class ImageTypesManager  {
55   public:
56     typedef std::map<std::string, GenericFilterFunctorBase<FilterType>*> MapOfPixelTypeToFunctionType;
57     typedef std::map<unsigned int, MapOfPixelTypeToFunctionType> MapOfImageComponentsToFunctionType;
58     typedef std::map<unsigned int, MapOfImageComponentsToFunctionType> MapOfImageDimensionToFunctionType;
59     MapOfImageDimensionToFunctionType mMapOfImageTypeToFunction;
60
61     ImageTypesManager(FilterType * f) { mFilter = f;  }
62     virtual void DoIt(int dim, int ncomp, std::string pixelname) {
63       // std::cout << "ImageTypesManager DoIt " << dim << " " << pixelname << std::endl;
64       if (mMapOfImageTypeToFunction[dim][ncomp][pixelname])
65         mMapOfImageTypeToFunction[dim][ncomp][pixelname]->Execute();
66     }
67     template<unsigned int Dim, unsigned int NComp, class PixelType>
68     void AddNewDimensionAndPixelType() {
69         typedef itk::Image<itk::Vector<PixelType,NComp>,Dim> InputImageType;
70         mMapOfImageTypeToFunction[Dim][NComp][ GetTypeAsString<PixelType>() ] = 
71             new GenericFilterFunctorWithDimAndPixelType<FilterType, InputImageType>(mFilter);
72     }
73     /// Specialization for NComp == 1
74     template<unsigned int Dim, class PixelType>
75     void AddNewDimensionAndPixelType() {
76         typedef itk::Image<PixelType,Dim> InputImageType;
77         mMapOfImageTypeToFunction[Dim][1][ GetTypeAsString<PixelType>() ] = 
78             new GenericFilterFunctorWithDimAndPixelType<FilterType, InputImageType>(mFilter);
79     }
80     FilterType * mFilter;
81   };
82   //--------------------------------------------------------------------
83
84 } // end namespace
85
86 #endif // end CLITKCOMMONGENERICFILTER_H