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