]> Creatis software - clitk.git/blob - common/clitkFilterBase.h
add MustStop function (for interrupting a filter)
[clitk.git] / common / clitkFilterBase.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 CLITKFILTERBASE_H
20 #define CLITKFILTERBASE_H
21
22 // clitk
23 #include "clitkCommon.h"
24 #include "clitkTimer.h"
25 #include "clitkFilterMacros.txx"
26 #include "clitkLabelizeParameters.h"
27
28 // itk
29 #include "itkObject.h"
30
31 namespace clitk {
32   
33   //--------------------------------------------------------------------
34   /*
35     Convenient class to manage options from GGO gengetopt) to filter
36   */
37   //--------------------------------------------------------------------
38   class FilterBase {
39
40   public:
41     // Standard class typedefs
42     typedef FilterBase  Self;
43     
44     // Run-time type information (and related methods)
45     itkTypeMacro(FilterBase, Object);
46
47     // Needed by itkSetMacro (cannot inherit from itkObject because of
48     // multiple inheritance)
49     virtual void Modified() {} 
50     virtual bool GetDebug() const { return false; }
51
52     // To put in class that inherit from FilterBase
53 #define FILTERBASE_INIT                                                 \
54     virtual void Modified() { Superclass::Modified(); }                 \
55     virtual bool GetDebug() const { return Superclass::GetDebug(); }
56
57     // Verbose options management
58     itkSetMacro(VerboseOption, bool);
59     itkGetConstMacro(VerboseOption, bool);
60     itkBooleanMacro(VerboseOption);
61     GGO_DefineOption_Flag(verboseOption, SetVerboseOption);
62
63     // Steps management
64     itkSetMacro(NumberOfSteps, int);
65     itkGetConstMacro(NumberOfSteps, int);
66     itkSetMacro(VerboseStep, bool);
67     itkGetConstMacro(VerboseStep, bool);
68     itkBooleanMacro(VerboseStep);
69     GGO_DefineOption_Flag(verboseStep, SetVerboseStep);
70
71     itkSetMacro(WriteStep, bool);
72     itkGetConstMacro(WriteStep, bool);
73     itkBooleanMacro(WriteStep);
74     GGO_DefineOption_Flag(writeStep, SetWriteStep);
75
76     itkSetMacro(CurrentStepNumber, int);
77     itkGetConstMacro(CurrentStepNumber, int);
78     itkSetMacro(CurrentStepId, std::string);
79     itkGetConstMacro(CurrentStepId, std::string);
80     itkSetMacro(CurrentStepBaseId, std::string);
81     itkGetConstMacro(CurrentStepBaseId, std::string);
82     itkSetMacro(CurrentStepName, std::string);
83     itkGetConstMacro(CurrentStepName, std::string);
84
85     // Convenient function for verbose option
86     template<class OptionType>
87     void VerboseOption(std::string name, OptionType value);    
88     template<class OptionType>
89     void VerboseOption(std::string name, int nb, OptionType value);
90     template<class OptionType>
91     void VerboseOptionV(std::string name, int nb, OptionType * value);
92
93     // Error 
94     void SetLastError(std::string e);
95     void ResetLastError();
96     itkGetConstMacro(LastError, std::string);
97     bool HasError() { return (GetLastError() != ""); }
98     itkSetMacro(StopOnError, bool);
99     itkGetConstMacro(StopOnError, bool);
100     itkBooleanMacro(StopOnError);    
101
102     void SetWarning(std::string e);
103     itkGetConstMacro(Warning, std::string);
104     itkSetMacro(VerboseWarningOff, bool);
105     itkGetConstMacro(VerboseWarningOff, bool);
106     itkBooleanMacro(VerboseWarningOff);
107     GGO_DefineOption_Flag(verboseWarningOff, SetVerboseWarningOff);
108     
109     // Use this function to stop (when threaded)
110     void SetMustStop(bool b);
111     bool GetMustStop();
112     
113   protected:
114     FilterBase();
115     virtual ~FilterBase() {}    
116     void StartNewStep(std::string s);
117     template<class TInternalImageType>
118     void StopCurrentStep(typename TInternalImageType::Pointer p);
119     void StopCurrentStep();
120
121     bool m_VerboseOption;  
122     bool m_VerboseStep;
123     bool m_WriteStep;
124     int m_CurrentStepNumber;
125     int m_NumberOfSteps;
126     std::string m_CurrentStepId;
127     std::string m_CurrentStepBaseId;
128     std::string m_LastError;
129     std::string m_CurrentStepName;
130     bool m_StopOnError;
131     std::string m_Warning;
132     bool m_VerboseWarningOff;
133     bool m_MustStop;
134     Timer m_CurrentStepTimer;
135
136   private:
137     FilterBase(const Self&); //purposely not implemented
138     void operator=(const Self&); //purposely not implemented
139     
140   }; // end class
141   //--------------------------------------------------------------------
142
143 } // end namespace clitk
144 //--------------------------------------------------------------------
145
146 #define StartNewStepOrStop(s) StartNewStep(s); if (GetMustStop()) return;
147
148 #ifndef ITK_MANUAL_INSTANTIATION
149 #include "clitkFilterBase.txx"
150 #endif
151
152 #endif // CLITKFILTERBASE_H