]> Creatis software - clitk.git/blob - segmentation/clitkExtractLymphStationsFilter.h
motion masks with and without bands
[clitk.git] / segmentation / clitkExtractLymphStationsFilter.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 CLITKEXTRACTLYMPHSTATIONSFILTER_H
20 #define CLITKEXTRACTLYMPHSTATIONSFILTER_H
21
22 // clitk
23 #include "clitkStructuresExtractionFilter.h"
24 #include "clitkLabelImageOverlapMeasureFilter.h"
25
26 // vtk
27 #include <vtkPolyData.h>
28
29 namespace clitk {
30   
31   class SupportLimitsType {
32   public:
33     std::string station_limit;
34     std::string station;
35     std::string structure_limit;
36     std::string structure;
37     double offset;
38     void Read(istream & is) {
39       is >> station_limit;
40       is >> station;
41       is >> structure_limit;
42       is >> structure;
43       std::string s;
44       is >> s;
45       offset = atof(s.c_str());
46     }
47   };
48
49   //--------------------------------------------------------------------
50   /*
51     Try to extract the LymphStations part of a thorax CT.
52     Need a set of Anatomical Features (AFDB)
53   */
54   //--------------------------------------------------------------------
55   
56   template <class TImageType>
57   class ITK_EXPORT ExtractLymphStationsFilter: 
58     public clitk::StructuresExtractionFilter<TImageType>
59   {
60
61   public:
62     /** Standard class typedefs. */
63     typedef clitk::StructuresExtractionFilter<TImageType> Superclass;
64     typedef ExtractLymphStationsFilter          Self;
65     typedef itk::SmartPointer<Self>             Pointer;
66     typedef itk::SmartPointer<const Self>       ConstPointer;
67     
68     /** Method for creation through the object factory. */
69     itkNewMacro(Self);
70     
71     /** Run-time type information (and related methods). */
72     itkTypeMacro(ExtractLymphStationsFilter, ImageToImageFilter);
73
74     /** Some convenient typedefs. */
75     typedef TImageType                       ImageType;
76     typedef typename ImageType::ConstPointer ImageConstPointer;
77     typedef typename ImageType::Pointer      ImagePointer;
78     typedef typename ImageType::RegionType   ImageRegionType; 
79     typedef typename ImageType::PixelType    ImagePixelType; 
80     typedef typename ImageType::SizeType     ImageSizeType; 
81     typedef typename ImageType::IndexType    ImageIndexType; 
82     typedef typename ImageType::PointType    ImagePointType; 
83         
84     typedef uchar MaskImagePixelType;
85     typedef itk::Image<MaskImagePixelType, 3>    MaskImageType;  
86     typedef typename MaskImageType::Pointer      MaskImagePointer;
87     typedef typename MaskImageType::RegionType   MaskImageRegionType; 
88     typedef typename MaskImageType::SizeType     MaskImageSizeType; 
89     typedef typename MaskImageType::IndexType    MaskImageIndexType; 
90     typedef typename MaskImageType::PointType    MaskImagePointType; 
91
92     typedef itk::Image<MaskImagePixelType, 2>    MaskSliceType;
93     typedef typename MaskSliceType::Pointer      MaskSlicePointer;
94     typedef typename MaskSliceType::PointType    MaskSlicePointType;
95     typedef typename MaskSliceType::RegionType   MaskSliceRegionType; 
96     typedef typename MaskSliceType::SizeType     MaskSliceSizeType; 
97     typedef typename MaskSliceType::IndexType    MaskSliceIndexType; 
98
99     /** ImageDimension constants */
100     itkStaticConstMacro(ImageDimension, unsigned int, ImageType::ImageDimension);
101     FILTERBASE_INIT;
102      
103     itkGetConstMacro(BackgroundValue, MaskImagePixelType);
104     itkGetConstMacro(ForegroundValue, MaskImagePixelType);
105     itkSetMacro(BackgroundValue, MaskImagePixelType);
106     itkSetMacro(ForegroundValue, MaskImagePixelType);
107
108     // Station 8
109     itkSetMacro(EsophagusDiltationForAnt, MaskImagePointType);
110     itkGetConstMacro(EsophagusDiltationForAnt, MaskImagePointType);
111     itkSetMacro(EsophagusDiltationForRight, MaskImagePointType);
112     itkGetConstMacro(EsophagusDiltationForRight, MaskImagePointType);
113     itkSetMacro(InjectedThresholdForS8, double);
114     itkGetConstMacro(InjectedThresholdForS8, double);
115
116     // Station 7
117     itkGetConstMacro(S7_UseMostInferiorPartOnlyFlag, bool);
118     itkSetMacro(S7_UseMostInferiorPartOnlyFlag, bool);
119     itkBooleanMacro(S7_UseMostInferiorPartOnlyFlag);
120
121     // All stations
122     bool GetComputeStation(std::string s);
123     void AddComputeStation(std::string station) ;
124     void SetFuzzyThreshold(std::string station, std::string tag, double value);
125     double GetFuzzyThreshold(std::string station, std::string tag);
126     void SetThreshold(std::string station, std::string tag, double value);
127     double GetThreshold(std::string station, std::string tag);
128     itkGetConstMacro(ForceSupportsFlag, bool);
129     itkSetMacro(ForceSupportsFlag, bool);
130     itkBooleanMacro(ForceSupportsFlag);
131
132     itkGetConstMacro(CheckSupportFlag, bool);
133     itkSetMacro(CheckSupportFlag, bool);
134     itkBooleanMacro(CheckSupportFlag);
135
136     itkGetConstMacro(SupportLimitsFilename, std::string);
137     itkSetMacro(SupportLimitsFilename, std::string);
138
139   protected:
140     ExtractLymphStationsFilter();
141     virtual ~ExtractLymphStationsFilter() {}
142     
143     virtual void GenerateOutputInformation();
144     virtual void GenerateInputRequestedRegion();
145     virtual void GenerateData();
146     
147     // To avoid repeat "this->"
148     AnatomicalFeatureDatabase * GetAFDB() { return clitk::FilterWithAnatomicalFeatureDatabaseManagement::GetAFDB(); }
149     void WriteAFDB() { clitk::FilterWithAnatomicalFeatureDatabaseManagement::WriteAFDB(); }
150     void LoadAFDB() {  clitk::FilterWithAnatomicalFeatureDatabaseManagement::LoadAFDB(); }  
151     void StartNewStep(std::string s) { clitk::FilterBase::StartNewStep(s); }
152     void StartSubStep() { clitk::FilterBase::StartSubStep(); }
153     template<class TInternalImageType>
154       void StopCurrentStep(typename TInternalImageType::Pointer p, std::string txt="") { clitk::FilterBase::StopCurrentStep<TInternalImageType>(p, txt); }
155     void StopCurrentStep() {clitk::FilterBase::StopCurrentStep(); }
156     void StopSubStep() {clitk::FilterBase::StopSubStep(); }
157
158     ImageConstPointer  m_Input;
159     MaskImagePointer   m_Mediastinum;
160     MaskImagePointer   m_Working_Support;
161     std::map<std::string, MaskImagePointer> m_ListOfStations;
162     std::map<std::string, MaskImagePointer> m_ListOfSupports;
163     MaskImagePixelType m_BackgroundValue;
164     MaskImagePixelType m_ForegroundValue;
165     std::map<std::string, bool> m_ComputeStationMap;
166     std::string m_SupportLimitsFilename;
167     std::vector<SupportLimitsType> m_ListOfSupportLimits;
168
169     bool CheckForStation(std::string station);
170     void Remove_Structures(std::string station, std::string s);
171     void WriteImageSupport(std::string support);
172     void WriteImageStation(std::string station);
173     void ComputeOverlapWithRef(std::string station);
174     void Support_SI_Limit(const std::string station_limit, const std::string station, 
175                           const std::string structure_limit, const std::string structure, 
176                           const double offset);
177     void ReadSupportLimits(std::string filename);
178
179     // Functions common to several stations
180     double FindCarina();
181     double FindApexOfTheChest();
182     double FindSuperiorBorderOfAorticArch();
183     double FindInferiorBorderOfAorticArch();
184     void FindLeftAndRightBronchi();
185     void FindLineForS7S8Separation(MaskImagePointType & A, MaskImagePointType & B);
186     MaskImagePointer FindAntPostVesselsOLD();
187     MaskImagePointer FindAntPostVessels2();
188
189     // Global parameters
190     typedef std::map<std::string, double> FuzzyThresholdByStructureType;
191     std::map<std::string, FuzzyThresholdByStructureType> m_FuzzyThreshold;    
192     typedef std::map<std::string, double> ThresholdByStructureType;
193     std::map<std::string, ThresholdByStructureType> m_Threshold;    
194
195     // Station's supports
196     void ExtractStationSupports();
197     void Support_LeftRight_S1R_S1L();
198     void Support_LeftRight_S2R_S2L();
199     void Support_LeftRight_S4R_S4L();
200     void Support_Post_S1S2S4();
201
202     MaskImagePointer LimitsWithTrachea(MaskImageType * input, 
203                                        int extremaDirection, int lineDirection, 
204                                        double offset, double maxSupPosition);
205     MaskImagePointer LimitsWithTrachea(MaskImageType * input, 
206                                        int extremaDirection, int lineDirection, 
207                                        double offset);
208     // Station 8
209     double m_DiaphragmInferiorLimit;
210     double m_OriginOfRightMiddleLobeBronchusZ;
211     double m_InjectedThresholdForS8;
212     MaskImagePointer m_Esophagus;
213     MaskImagePointType m_EsophagusDiltationForAnt;
214     MaskImagePointType m_EsophagusDiltationForRight;
215
216     void ExtractStation_8();
217     void ExtractStation_8_SetDefaultValues();
218     void ExtractStation_8_SI_Limits();
219     void ExtractStation_8_Ant_Limits();
220     void ExtractStation_8_Left_Sup_Limits();
221     void ExtractStation_8_Left_Inf_Limits();
222     void ExtractStation_8_Single_CCL_Limits();
223     void ExtractStation_8_Remove_Structures();
224
225     // Station 3P
226     void ExtractStation_3P();
227     void ExtractStation_3P_SetDefaultValues();
228     void ExtractStation_3P_LR_inf_Limits();
229     void ExtractStation_3P_LR_sup_Limits_2();
230     void ExtractStation_3P_Remove_Structures();
231     void ExtractStation_3P_LR_sup_Limits();
232
233     // Station 3A
234     void ExtractStation_3A();
235     void ExtractStation_3A_SetDefaultValues();
236     void ExtractStation_3A_Post_Left_Limits_With_Aorta_S5_Support();
237     void ExtractStation_3A_Post_Limits_With_Dilated_Aorta_S6_Support();
238     void ExtractStation_3A_AntPost_Superiorly();
239     void ExtractStation_3A_Remove_Structures();
240
241     // Station 2RL
242     void ExtractStation_2RL();
243     void ExtractStation_2RL_SetDefaultValues();
244     void ExtractStation_2RL_Ant_Limits(std::string s);
245     void ExtractStation_2RL_Remove_Structures(std::string s);
246     void ExtractStation_2RL_Cut_BrachioCephalicVein_superiorly_when_it_split();
247     vtkSmartPointer<vtkPolyData> Build3DMeshFrom2DContour(const std::vector<ImagePointType> & points);
248
249     // Station 1RL
250     void ExtractStation_1RL();
251     void ExtractStation_1RL_SetDefaultValues();
252     void ExtractStation_1RL_Ant_Limits();
253     void ExtractStation_1RL_Post_Limits();
254
255     // Station 4RL
256     void ExtractStation_4RL_SetDefaultValues();
257     void ExtractStation_4L();
258     void ExtractStation_4R();
259     void ExtractStation_S4L_S5_Limits_Aorta_LeftPulmonaryArtery(int KeepPoint);
260
261     // Station 5
262     void ExtractStation_5_SetDefaultValues();
263     void ExtractStation_5();
264     void ExtractStation_5_Limits_AscendingAorta_Ant();
265
266     // Station 6
267     void ExtractStation_6_SetDefaultValues();
268     void ExtractStation_6();
269
270
271     // Station 7
272     void ExtractStation_7();
273     void ExtractStation_7_SetDefaultValues();
274     void ExtractStation_7_SI_Limits();
275     void ExtractStation_7_RL_Interior_Limits();
276
277     void ExtractStation_7_RL_Limits_OLD();
278     void ExtractStation_7_Posterior_Limits();   
279     void ExtractStation_7_Remove_Structures();
280     bool m_S7_UseMostInferiorPartOnlyFlag;
281     bool m_ForceSupportsFlag;
282     bool m_CheckSupportFlag;
283     MaskImagePointer m_Working_Trachea;
284     MaskImagePointer m_LeftBronchus;
285     MaskImagePointer m_RightBronchus;
286     typedef std::vector<MaskImageType::PointType> ListOfPointsType;
287     ListOfPointsType  m_RightMostInLeftBronchus;
288     ListOfPointsType  m_AntMostInLeftBronchus;
289     ListOfPointsType  m_PostMostInLeftBronchus;
290     ListOfPointsType  m_LeftMostInRightBronchus;
291     ListOfPointsType  m_AntMostInRightBronchus;
292     ListOfPointsType  m_PostMostInRightBronchus;
293
294     void FindExtremaPointsInBronchus(MaskImagePointer input, 
295                                      int direction,
296                                      double distance_max_from_center_point, 
297                                      ListOfPointsType & LR, 
298                                      ListOfPointsType & Ant, 
299                                      ListOfPointsType & Post);
300   private:
301     ExtractLymphStationsFilter(const Self&); //purposely not implemented
302     void operator=(const Self&); //purposely not implemented
303     
304   }; // end class
305   //--------------------------------------------------------------------
306
307 } // end namespace clitk
308 //--------------------------------------------------------------------
309
310 #ifndef ITK_MANUAL_INSTANTIATION
311 #include "clitkExtractLymphStationsFilter.txx"
312 #include "clitkExtractLymphStation_Supports.txx"
313 #include "clitkExtractLymphStation_3P.txx"
314 #include "clitkExtractLymphStation_2RL.txx"
315 #include "clitkExtractLymphStation_3A.txx"
316 #include "clitkExtractLymphStation_4RL.txx"
317 #include "clitkExtractLymphStation_1RL.txx"
318 #include "clitkExtractLymphStation_5.txx"
319 #include "clitkExtractLymphStation_6.txx"
320
321 #include "clitkExtractLymphStation_8.txx"
322 #include "clitkExtractLymphStation_7.txx"
323 #endif
324
325 #endif