]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/BasicFilters/DoubleFloodImageFilter.cxx
Pipeline editor added.
[cpPlugins.git] / lib / cpPlugins / Plugins / BasicFilters / DoubleFloodImageFilter.cxx
1 #include "DoubleFloodImageFilter.h"
2 #include <cpPlugins/Interface/Image.h>
3 #include <cpPlugins/Interface/BaseApplication.h>
4 #include <cpPlugins/Interface/Plugins.h>
5 #include <cpExtensions/Interaction/ImageInteractorStyle.h>
6
7 #include <vtkRenderWindowInteractor.h>
8
9 #include <itkFloodFilledImageFunctionConditionalConstIterator.h>
10 #include <itkImageFunction.h>
11
12 #ifdef cpPlugins_Interface_QT4
13 #include <QDialogButtonBox>
14
15 // -------------------------------------------------------------------------
16 cpPlugins::BasicFilters::DoubleFloodImageFilter_Dialog::
17 DoubleFloodImageFilter_Dialog(
18   QWidget* parent, DoubleFloodImageFilter* filter, Qt::WindowFlags f
19   )
20   : QDialog( parent, f | Qt::WindowStaysOnTopHint ),
21     m_Filter( filter )
22 {
23   this->m_Title = new QLabel( this );
24   this->m_Title->setText( "Execute simple vessel segmentation filter" );
25
26   this->m_MainLayout = new QGridLayout( this );
27   this->m_ToolsLayout = new QVBoxLayout( );
28   this->m_ToolsLayout->addWidget( this->m_Title );
29   this->m_MainLayout->addLayout( this->m_ToolsLayout, 0, 0, 1, 1 );
30
31   // Add buttons
32   QDialogButtonBox* bb = new QDialogButtonBox(
33     QDialogButtonBox::Cancel | QDialogButtonBox::Ok
34     );
35   QObject::connect( bb, SIGNAL( accepted( ) ), this, SLOT( accept( ) ) );
36   QObject::connect( bb, SIGNAL( rejected( ) ), this, SLOT( reject( ) ) );
37   this->m_ToolsLayout->addWidget( bb );
38 }
39
40 // -------------------------------------------------------------------------
41 cpPlugins::BasicFilters::DoubleFloodImageFilter_Dialog::
42 ~DoubleFloodImageFilter_Dialog( )
43 {
44   delete this->m_Title;
45   delete this->m_ToolsLayout;
46   delete this->m_MainLayout;
47 }
48
49 // -------------------------------------------------------------------------
50 void cpPlugins::BasicFilters::DoubleFloodImageFilter_Dialog::
51 accept( )
52 {
53   // Get interactive widget
54   if( this->m_Filter == NULL )
55     return;
56
57   typedef cpExtensions::Interaction::ImageInteractorStyle _TImageStyle;
58
59   // Activate seed widgets
60   auto iIt = this->m_Filter->m_Interactors.begin( );
61   for( ; iIt != this->m_Filter->m_Interactors.end( ); ++iIt )
62   {
63     _TImageStyle* istyle =
64       dynamic_cast< _TImageStyle* >(
65         ( *iIt )->GetInteractorStyle( )
66         );
67     if( istyle == NULL )
68       continue;
69     unsigned int nSeeds = istyle->GetNumberOfSeeds( );
70     for( unsigned int s = 0; s < nSeeds; ++s )
71     {
72       double seed[ 3 ];
73       istyle->GetSeed( s, seed );
74
75     } // rof
76
77   } // rof
78   /*
79   vtkPlaneWidget* wdg = this->m_Filter->m_PlaneWidget;
80   if( wdg == NULL )
81     return;
82
83   // Get/Set plane parameters
84   double center[ 3 ], normal[ 3 ];
85   wdg->GetCenter( center );
86   wdg->GetNormal( normal );
87
88   this->m_Filter->GetParameters( )->SetPoint( "PlaneCenter", 3, center );
89   this->m_Filter->GetParameters( )->SetVector( "PlaneNormal", 3, normal );
90
91   // Update filter
92   auto plugins = this->m_Filter->GetPlugins( );
93   if( plugins != NULL )
94   {
95     auto app = plugins->GetApplication( );
96     if( app != NULL )
97       app->UpdateActualFilter( );
98
99   } // fi
100   */
101 }
102
103 // -------------------------------------------------------------------------
104 void cpPlugins::BasicFilters::DoubleFloodImageFilter_Dialog::
105 reject( )
106 {
107   auto plugins = this->m_Filter->GetPlugins( );
108   if( plugins != NULL )
109     plugins->DeactivateFilter( );
110   this->Superclass::reject( );
111 }
112
113 #endif // cpPlugins_Interface_QT4
114
115 // -------------------------------------------------------------------------
116 cpPlugins::BasicFilters::DoubleFloodImageFilter::
117 DialogResult cpPlugins::BasicFilters::DoubleFloodImageFilter::
118 ExecConfigurationDialog( QWidget* parent )
119 {
120 #ifdef cpPlugins_Interface_QT4
121
122   typedef cpExtensions::Interaction::ImageInteractorStyle _TImageStyle;
123
124   // Activate seed widgets
125   bool at_least_one = false;
126   auto iIt = this->m_Interactors.begin( );
127   for( ; iIt != this->m_Interactors.end( ); ++iIt )
128   {
129     _TImageStyle* istyle =
130       dynamic_cast< _TImageStyle* >(
131         ( *iIt )->GetInteractorStyle( )
132         );
133     if( istyle != NULL )
134     {
135       istyle->SeedWidgetOn( );
136       at_least_one = true;
137
138     } // fi
139     
140   } // rof
141   if( !at_least_one )
142     return( Self::DialogResult_Cancel );
143
144   // Create dialog
145   this->m_Dialog = new DoubleFloodImageFilter_Dialog( NULL, this );
146   this->m_Dialog->show( );
147
148   return( Self::DialogResult_Modal );
149 #else // cpPlugins_Interface_QT4
150   return( Self::DialogResult_Cancel );
151 #endif // cpPlugins_Interface_QT4
152 }
153
154 // -------------------------------------------------------------------------
155 cpPlugins::BasicFilters::DoubleFloodImageFilter::
156 DoubleFloodImageFilter( )
157   : Superclass( )
158 {
159   this->_AddInput( "Input" );
160   this->_MakeOutput< cpPlugins::Interface::Image >( "Output" );
161
162   double seed[ 3 ] = { double( 0 ) };
163   this->m_Parameters->ConfigureAsPoint( "Seed0", 3, seed );
164   this->m_Parameters->ConfigureAsPoint( "Seed1", 3, seed );
165   this->m_Parameters->ConfigureAsReal( "Window", 0 );
166   this->m_Parameters->ConfigureAsReal( "Level", 0 );
167   this->m_Parameters->ConfigureAsUint( "InsideValue", 255 );
168   this->m_Parameters->ConfigureAsUint( "OutsideValue", 0 );
169 }
170
171 // -------------------------------------------------------------------------
172 cpPlugins::BasicFilters::DoubleFloodImageFilter::
173 ~DoubleFloodImageFilter( )
174 {
175 }
176
177 // -------------------------------------------------------------------------
178 std::string cpPlugins::BasicFilters::DoubleFloodImageFilter::
179 _GenerateData( )
180 {
181   cpPlugins::Interface::Image* image =
182     this->GetInput< cpPlugins::Interface::Image >( "Input" );
183   if( image == NULL )
184     return( "DoubleFloodImageFilter: No input image." );
185
186   itk::DataObject* itk_image = NULL;
187   std::string r = "";
188   cpPlugins_Image_Demangle_AllScalarTypes( 2, image, itk_image, r, _GD0 );
189   else cpPlugins_Image_Demangle_AllScalarTypes( 3, image, itk_image, r, _GD0 );
190   else r = "DoubleFloodImageFilter: Input image type not supported.";
191   return( r );
192 }
193
194 // -------------------------------------------------------------------------
195 template< class I >
196 std::string cpPlugins::BasicFilters::DoubleFloodImageFilter::
197 _GD0( itk::DataObject* image )
198 {
199   return(
200     this->_RealGD< I, itk::Image< unsigned char, I::ImageDimension > >(
201       image
202       )
203     );
204 }
205
206 // -------------------------------------------------------------------------
207 template< class I, class R = float >
208 class cpPlugins_BasicFilters_DoubleFloodImageFilter_Function
209   : public itk::ImageFunction< I, bool, R >
210 {
211 public:
212   typedef cpPlugins_BasicFilters_DoubleFloodImageFilter_Function Self;
213   typedef itk::ImageFunction< I, bool, R >                     Superclass;
214   typedef itk::SmartPointer< Self >                            Pointer;
215   typedef itk::SmartPointer< const Self >                      ConstPointer;
216
217   typedef typename Superclass::PointType           TPoint;
218   typedef typename Superclass::IndexType           TIndex;
219   typedef typename Superclass::ContinuousIndexType TCIndex;
220
221 public:
222   itkNewMacro( Self );
223   itkTypeMacro(
224     cpPlugins_BasicFilters_DoubleFloodImageFilter_Function,
225     itkImageFunction
226     );
227
228   itkSetMacro( Window, double );
229   itkSetMacro( Level, double );
230
231 public:
232   virtual bool Evaluate( const TPoint& point ) const
233     {
234       return( true );
235     }
236   virtual bool EvaluateAtIndex( const TIndex& index ) const
237     {
238       if( !( this->IsInsideBuffer( index ) ) )
239         return( false );
240
241       const I* image = this->GetInputImage( );
242       double w2 = this->m_Window / double( 2 );
243       double min = this->m_Level - w2;
244       double max = this->m_Level + w2;
245       unsigned char val = double( 0 );
246       double x = double( image->GetPixel( index ) );
247       double m = double( 100 ) / this->m_Window;
248       double b = ( this->m_Window - ( double( 2 ) * this->m_Level ) );
249       b *= double( 50 ) / this->m_Window;
250       if( x > min && x < max )
251         val = ( unsigned char )( ( m * x ) + b );
252
253       if( this->m_Start )
254       {
255         this->m_StartValue = val;
256         this->m_Start = false;
257         return( true );
258       }
259       else
260         return( std::abs( this->m_StartValue - val ) <= 2 );
261     }
262   virtual bool EvaluateAtContinuousIndex( const TCIndex& index ) const
263     {
264       return( true );
265     }
266
267 protected:
268   cpPlugins_BasicFilters_DoubleFloodImageFilter_Function( )
269     : Superclass( ),
270       m_Window( double( 0 ) ),
271       m_Level( double( 0 ) ),
272       m_Start( true )
273     {
274     }
275   virtual ~cpPlugins_BasicFilters_DoubleFloodImageFilter_Function( )
276     {
277     }
278
279 private:
280   // Purposely not implemented
281   cpPlugins_BasicFilters_DoubleFloodImageFilter_Function( const Self& other );
282   Self& operator=( const Self& other );
283
284 protected:
285   double m_Window;
286   double m_Level;
287   mutable unsigned char m_StartValue;
288   mutable bool m_Start;
289 };
290
291 // -------------------------------------------------------------------------
292 template< class I, class O >
293 inline std::string cpPlugins::BasicFilters::DoubleFloodImageFilter::
294 _RealGD( itk::DataObject* image )
295 {
296   /*
297   typedef typename O::PixelType _OP;
298   typedef cpPlugins_BasicFilters_DoubleFloodImageFilter_Function< I > _F;
299   typedef itk::FloodFilledImageFunctionConditionalConstIterator< I, _F > _It;
300
301   typename I::PointType pseed;
302   pseed = this->m_Parameters->GetPoint< typename I::PointType >(
303     "Seed", I::ImageDimension
304     );
305   double window = this->m_Parameters->GetReal( "Window" );
306   double level = this->m_Parameters->GetReal( "Level" );
307   _OP in_val = _OP( this->m_Parameters->GetUint( "InsideValue" ) );
308   _OP out_val = _OP( this->m_Parameters->GetUint( "OutsideValue" ) );
309
310   const I* in = dynamic_cast< const I* >( image );
311   typename I::IndexType seed;
312   in->TransformPhysicalPointToIndex( pseed, seed );
313
314   typename O::Pointer out = O::New( );
315   out->SetLargestPossibleRegion( in->GetLargestPossibleRegion( ) );
316   out->SetRequestedRegion( in->GetRequestedRegion( ) );
317   out->SetBufferedRegion( in->GetBufferedRegion( ) );
318   out->SetOrigin( in->GetOrigin( ) );
319   out->SetDirection( in->GetDirection( ) );
320   out->SetSpacing( in->GetSpacing( ) );
321   out->Allocate( );
322   out->FillBuffer( out_val );
323
324   typename _F::Pointer f = _F::New( );
325   f->SetInputImage( in );
326   f->SetWindow( window );
327   f->SetLevel( level );
328   _It i( in, f );
329   i.AddSeed( seed );
330
331   for( i.GoToBegin( ); !i.IsAtEnd( ); ++i )
332     out->SetPixel( i.GetIndex( ), in_val );
333
334   // Connect output
335   cpPlugins::Interface::Image* out_port =
336     this->GetOutput< cpPlugins::Interface::Image >( "Output" );
337   if( out_port != NULL )
338   {
339     out_port->SetITK< O >( out );
340     return( "" );
341   }
342   else
343 */
344     return( "DoubleFloodImageFilter: output not correctly created." );
345 }
346
347 // eof - $RCSfile$