From: Leonardo Florez-Valencia Date: Mon, 16 Feb 2015 23:41:44 +0000 (-0500) Subject: Some more debug... almost there X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=972dd44060dfff40ba3daab899c6365445f6de7e;p=FrontAlgorithms.git Some more debug... almost there --- diff --git a/appli/examples/example_ImageAlgorithmRegionGrow_MultipleThresholds.cxx b/appli/examples/example_ImageAlgorithmRegionGrow_MultipleThresholds.cxx index 1f4bf3f..ff82103 100644 --- a/appli/examples/example_ImageAlgorithmRegionGrow_MultipleThresholds.cxx +++ b/appli/examples/example_ImageAlgorithmRegionGrow_MultipleThresholds.cxx @@ -74,43 +74,42 @@ int main( int argc, char* argv[] ) vtk_image->SetInput( input_image ); vtk_image->Update( ); - /* TODO - vtkSmartPointer< vtkSphereSource > seed = - vtkSmartPointer< vtkSphereSource >::New( ); - seed->SetCenter( seed_pnt[ 0 ], seed_pnt[ 1 ], seed_pnt[ 2 ] ); - seed->SetRadius( min_spac * double( 5 ) ); - seed->Update( ); - */ - fpa::VTK::ImageMPR view; view.SetBackground( 0.3, 0.2, 0.8 ); view.SetSize( 800, 800 ); view.SetImage( vtk_image->GetOutput( ) ); - // TODO: view.AddPolyData( seed->GetOutput( ), 1, 0, 0 ); - view.Start( ); + + // Wait for a seed to be given + while( view.GetNumberOfSeeds( ) == 0 ) + view.Start( ); + + // Convert seed + double seed[ 3 ]; + view.GetSeed( 0, seed ); + TImage::PointType seed_pnt; + seed_pnt[ 0 ] = seed[ 0 ]; + seed_pnt[ 1 ] = seed[ 1 ]; + seed_pnt[ 2 ] = seed[ 2 ]; + TImage::IndexType seed_idx; + input_image->TransformPhysicalPointToIndex( seed_pnt, seed_idx ); // Configure observer - /* TODO - TObserver::Pointer obs = TObserver::New( ); - obs->SetImage( input_image, view.GetWindow( ) ); - - // Configure algorithm - TFrontAlgorithm::Pointer algorithm = TFrontAlgorithm::New( ); - algorithm->AddThresholds( thr_0, thr_1, n_samples ); - - TImage::IndexType seed_idx; - input_image->TransformPhysicalPointToIndex( seed_pnt, seed_idx ); - algorithm->AddSeed( seed_idx, 0 ); - - algorithm->AddObserver( itk::AnyEvent( ), obs ); - algorithm->ThrowEventsOn( ); - algorithm->SetInput( input_image ); - algorithm->SetNeighborhoodOrder( 1 ); - algorithm->SetDerivativeThreshold( double( 3 ) ); - algorithm->Update( ); - - view.Start( ); - */ + TObserver::Pointer obs = TObserver::New( ); + obs->SetImage( input_image, view.GetWindow( ) ); + + // Configure algorithm + TFrontAlgorithm::Pointer algorithm = TFrontAlgorithm::New( ); + algorithm->AddThresholds( thr_0, thr_1, n_samples ); + algorithm->AddSeed( seed_idx, 0 ); + algorithm->AddObserver( itk::AnyEvent( ), obs ); + algorithm->ThrowEventsOn( ); + algorithm->SetInput( input_image ); + algorithm->SetNeighborhoodOrder( 1 ); + algorithm->SetDerivativeThreshold( double( 3 ) ); + algorithm->Update( ); + + // Let some interaction and close program + view.Start( ); return( 0 ); } diff --git a/lib/fpa/Base/RegionGrowWithMultipleCriteria.hxx b/lib/fpa/Base/RegionGrowWithMultipleCriteria.hxx index 24b3308..5ec51ec 100644 --- a/lib/fpa/Base/RegionGrowWithMultipleCriteria.hxx +++ b/lib/fpa/Base/RegionGrowWithMultipleCriteria.hxx @@ -47,7 +47,7 @@ template< class V, class R, class VV, class VC, class B > void fpa::Base::RegionGrowWithMultipleCriteria< V, R, VV, VC, B >:: _BeforeLoop( ) { - this->_BeforeLoop( ); + this->Superclass::_BeforeLoop( ); this->m_ActualFunction = this->m_Functions.begin( ); } diff --git a/lib/fpa/Image/Functors/RegionGrowThresholdFunction.h b/lib/fpa/Image/Functors/RegionGrowThresholdFunction.h index 44cccc2..6da586d 100644 --- a/lib/fpa/Image/Functors/RegionGrowThresholdFunction.h +++ b/lib/fpa/Image/Functors/RegionGrowThresholdFunction.h @@ -53,9 +53,12 @@ namespace fpa { */ TPixel v = img->GetPixel( idx ); + std::cout + << v << " " + << this->m_LowerThreshold << " " + << this->m_UpperThreshold << std::endl; return( - this->m_LowerThreshold <= v && - v <= this->m_UpperThreshold + this->m_LowerThreshold <= v && v < this->m_UpperThreshold ); // TODO: } // fi diff --git a/lib/fpa/Image/RegionGrowWithMultipleThresholds.h b/lib/fpa/Image/RegionGrowWithMultipleThresholds.h index 41fba37..b4c3e4e 100644 --- a/lib/fpa/Image/RegionGrowWithMultipleThresholds.h +++ b/lib/fpa/Image/RegionGrowWithMultipleThresholds.h @@ -2,7 +2,9 @@ #define __FPA__IMAGE__REGIONGROWWITHMULTIPLETHRESHOLDS__H__ #include +#include #include +#include namespace fpa { @@ -28,6 +30,9 @@ namespace fpa typedef typename Superclass::TMembershipFunction TMembershipFunction; typedef typename Superclass::TFunctions TFunctions; + typedef std::set< TPixel > TThresholds; + typedef fpa::Image::Functors::RegionGrowThresholdFunction< I > TFunction; + protected: typedef typename TBaseAlgorithm::_TNode _TNode; @@ -59,6 +64,7 @@ namespace fpa void operator=( const Self& ); // Not impl. protected: + TThresholds m_Thresholds; double m_DerivativeThreshold; THistogram m_Histogram; }; diff --git a/lib/fpa/Image/RegionGrowWithMultipleThresholds.hxx b/lib/fpa/Image/RegionGrowWithMultipleThresholds.hxx index 0969b0c..a5b2075 100644 --- a/lib/fpa/Image/RegionGrowWithMultipleThresholds.hxx +++ b/lib/fpa/Image/RegionGrowWithMultipleThresholds.hxx @@ -2,43 +2,14 @@ #define __FPA__IMAGE__REGIONGROWWITHMULTIPLETHRESHOLDS__HXX__ #include -#include // ------------------------------------------------------------------------- template< class I > void fpa::Image::RegionGrowWithMultipleThresholds< I >:: AddThreshold( const TPixel& v ) { - typedef - fpa::Image::Functors::RegionGrowThresholdFunction< I > - TFunction; - typename TFunction::Pointer function = TFunction::New( ); - - if( this->GetNumberOfMembershipFunctions( ) > 0 ) - { - } - else - function->SetLowerThreshold( std::numeric_limits< TPixel >::min( ) ); - std::cout - << typeid( TPixel ).name( ) << " <<<----->>> " - << function->GetLowerThreshold( ) - << std::endl; - std::exit( 1 ); - function->SetUpperThreshold( v ); - this->AddMembershipFunction( function ); - - /* TODO - this->m_Histogram[ v ] = 0; - - TFunction* function = - dynamic_cast< TFunction* >( this->GetMembershipFunction( ) ); - if( function != NULL ) - { - function->SetLowerThreshold( this->m_Histogram.begin( )->first ); - - } // fi - this->Modified( ); - */ + this->m_Thresholds.insert( v ); + this->Modified( ); } // ------------------------------------------------------------------------- @@ -80,7 +51,9 @@ bool fpa::Image::RegionGrowWithMultipleThresholds< I >:: _UpdateResult( _TNode& n ) { bool ret = this->Superclass::_UpdateResult( n ); + std::cout << "Image:UpdateResult " << ret << std::endl; + /* TODO if( ret ) { @@ -110,18 +83,22 @@ template< class I > void fpa::Image::RegionGrowWithMultipleThresholds< I >:: _BeforeLoop( ) { - std::cout << "**1" << std::endl; const I* img = this->GetInput( ); - std::cout << "**2" << std::endl; - typename TFunctions::iterator fIt = this->m_Functions.begin( ); - for( ; fIt != this->m_Functions.end( ); ++fIt ) + + this->ClearMembershipFunctions( ); + typename TThresholds::const_iterator tIt = this->m_Thresholds.begin( ); + typename TThresholds::const_iterator prev_tIt = tIt; + for( ++tIt; tIt != this->m_Thresholds.end( ); ++tIt, ++prev_tIt ) { - TMembershipFunction* f = - dynamic_cast< TMembershipFunction* >( fIt->GetPointer( ) ); - if( f != NULL ) - f->SetInputImage( this->GetInput( ) ); + typename TFunction::Pointer function = TFunction::New( ); + function->SetInputImage( img ); + function->SetLowerThreshold( *prev_tIt ); + function->SetUpperThreshold( *tIt ); + this->AddMembershipFunction( function ); + std::cout << *prev_tIt << " " << *tIt << std::endl; } // rof + this->Superclass::_BeforeLoop( ); } diff --git a/lib/fpa/VTK/ImageMPR.cxx b/lib/fpa/VTK/ImageMPR.cxx index b990900..fc5883e 100644 --- a/lib/fpa/VTK/ImageMPR.cxx +++ b/lib/fpa/VTK/ImageMPR.cxx @@ -2,58 +2,65 @@ #include #include -#include -class vtkUpdateCallback +#include + +/** + */ +class fpa_VTK_InteractionModeCallback : public vtkCommand { public: - static vtkUpdateCallback *New() + static fpa_VTK_InteractionModeCallback* New( ) { - return new vtkUpdateCallback; + return( new fpa_VTK_InteractionModeCallback ); } - virtual void Execute(vtkObject *caller, unsigned long eventId, void* - arguments) + virtual void Execute( + vtkObject* caller, unsigned long eventId, void* arguments ) { - vtkRenderWindowInteractor* iren = - static_cast< vtkRenderWindowInteractor* >( caller ); - if( caller == NULL ) - return; - - switch( iren->GetKeyCode( ) ) + if( eventId == vtkCommand::KeyPressEvent ) { - case 'x': - case 'X': - { - bool eSeed = ( this->SeedWidget->GetProcessEvents( ) == 1 ); - if( eSeed ) + vtkRenderWindowInteractor* iren = + static_cast< vtkRenderWindowInteractor* >( caller ); + if( caller == NULL ) + return; + + switch( iren->GetKeyCode( ) ) { - this->SeedWidget->ProcessEventsOff( ); - this->WidgetX->InteractionOn( ); - this->WidgetY->InteractionOn( ); - this->WidgetZ->InteractionOn( ); - } - else + case 'x': + case 'X': { - this->SeedWidget->ProcessEventsOn( ); - this->WidgetX->InteractionOff( ); - this->WidgetY->InteractionOff( ); - this->WidgetZ->InteractionOff( ); - - } // fi - } - break; - default: + bool eSeed = ( this->SeedWidget->GetProcessEvents( ) == 1 ); + if( eSeed ) + { + this->SeedWidget->ProcessEventsOff( ); + this->WidgetX->InteractionOn( ); + this->WidgetY->InteractionOn( ); + this->WidgetZ->InteractionOn( ); + } + else + { + this->SeedWidget->ProcessEventsOn( ); + this->WidgetX->InteractionOff( ); + this->WidgetY->InteractionOff( ); + this->WidgetZ->InteractionOff( ); + + } // fi + } break; + default: + break; + + } // hctiws - } // hctiws + } // fi } public: vtkImagePlaneWidget* WidgetX; vtkImagePlaneWidget* WidgetY; vtkImagePlaneWidget* WidgetZ; - vtkSeedWidget2* SeedWidget; + vtkSeedWidget* SeedWidget; }; // ------------------------------------------------------------------------- @@ -70,40 +77,36 @@ ImageMPR( ) this->m_Renderer = vtkSmartPointer< vtkRenderer >::New( ); this->m_Window = vtkSmartPointer< vtkRenderWindow >::New( ); this->m_Interactor = vtkSmartPointer< vtkRenderWindowInteractor >::New( ); + this->m_SeedHandleRepresentation = + vtkSmartPointer< vtkPointHandleRepresentation3D >::New( ); + this->m_SeedRepresentation = + vtkSmartPointer< vtkSeedRepresentation >::New( ); + this->m_SeedWidget = vtkSmartPointer::New(); - - - - - - handlePointRep3D = - vtkSmartPointer::New(); - //handlePointRep3D->AllOn(); - handlePointRep3D->GetProperty()->SetColor(1.,0.,1.); - seedRep = - vtkSmartPointer::New(); - seedRep->SetHandleRepresentation(handlePointRep3D); - seedWidget = vtkSmartPointer::New(); - - seedWidget->SetRepresentation(seedRep); + this->m_SeedHandleRepresentation->GetProperty()->SetColor( 1, 0, 1 ); + this->m_SeedRepresentation-> + SetHandleRepresentation( this->m_SeedHandleRepresentation ); + this->m_SeedWidget->SetRepresentation( this->m_SeedRepresentation ); this->m_WidgetX->KeyPressActivationOff( ); this->m_WidgetY->KeyPressActivationOff( ); this->m_WidgetZ->KeyPressActivationOff( ); - seedWidget->KeyPressActivationOff( ); - - // this->m_Interactor->GetPickingManager( )->AddPicker( this->m_Picker ); - - - vtkUpdateCallback* onUpdate = vtkUpdateCallback::New(); - onUpdate->WidgetX = this->m_WidgetX; - onUpdate->WidgetY = this->m_WidgetY; - onUpdate->WidgetZ = this->m_WidgetZ; - onUpdate->SeedWidget = this->seedWidget; - - this->m_Interactor->AddObserver(vtkCommand::KeyPressEvent, onUpdate); + this->m_SeedWidget->KeyPressActivationOff( ); + + vtkSmartPointer< fpa_VTK_InteractionModeCallback > int_cb = + vtkSmartPointer< fpa_VTK_InteractionModeCallback >::New( ); + int_cb->WidgetX = this->m_WidgetX; + int_cb->WidgetY = this->m_WidgetY; + int_cb->WidgetZ = this->m_WidgetZ; + int_cb->SeedWidget = this->m_SeedWidget; + this->m_Interactor->AddObserver( vtkCommand::KeyPressEvent, int_cb ); + + vtkSmartPointer< fpa::VTK::SeedWidgetCorrector > corr_cb = + vtkSmartPointer< fpa::VTK::SeedWidgetCorrector >::New( ); + corr_cb->Configure( this->m_Picker ); + this->m_SeedWidget->AddObserver( vtkCommand::PlacePointEvent, corr_cb ); } - + // ------------------------------------------------------------------------- fpa::VTK::ImageMPR:: ~ImageMPR( ) @@ -181,16 +184,18 @@ SetImage( vtkImageData* image ) this->m_WidgetX->SetInteractor( this->m_Interactor ); this->m_WidgetY->SetInteractor( this->m_Interactor ); this->m_WidgetZ->SetInteractor( this->m_Interactor ); - seedWidget->SetInteractor(this->m_Interactor); + this->m_SeedWidget->SetInteractor( this->m_Interactor ); this->m_WidgetX->On( ); this->m_WidgetY->On( ); this->m_WidgetZ->On( ); - seedWidget->On( ); - seedWidget->ProcessEventsOff( ); + this->m_SeedWidget->On( ); + this->m_SeedWidget->ProcessEventsOff( ); this->m_Interactor->SetPicker( this->m_Picker ); - this->m_Interactor->GetPickingManager( )->AddPicker( this->m_Picker ); - this->m_Interactor->GetPickingManager( )->EnabledOn( ); + /* + this->m_Interactor->GetPickingManager( )->AddPicker( this->m_Picker ); + this->m_Interactor->GetPickingManager( )->EnabledOn( ); + */ } // ------------------------------------------------------------------------- @@ -223,6 +228,27 @@ AddPolyData( vtkPolyData* pd, double r, double g, double b ) this->m_Renderer->AddActor( this->m_Actors[ i ] ); } +// ------------------------------------------------------------------------- +unsigned int fpa::VTK::ImageMPR:: +GetNumberOfSeeds( ) const +{ + return( this->m_SeedRepresentation->GetNumberOfSeeds( ) ); +} + +// ------------------------------------------------------------------------- +void fpa::VTK::ImageMPR:: +GetSeed( int n, double* s ) const +{ + vtkHandleWidget* hWdg = this->m_SeedWidget->GetSeed( n ); + if( hWdg == NULL ) + return; + vtkHandleRepresentation* hRep = + dynamic_cast< vtkHandleRepresentation* >( hWdg->GetRepresentation( ) ); + if( hRep == NULL ) + return; + hRep->GetWorldPosition( s ); +} + // ------------------------------------------------------------------------- vtkRenderWindow* fpa::VTK::ImageMPR:: GetWindow( ) const diff --git a/lib/fpa/VTK/ImageMPR.h b/lib/fpa/VTK/ImageMPR.h index 6664d31..ba82b95 100644 --- a/lib/fpa/VTK/ImageMPR.h +++ b/lib/fpa/VTK/ImageMPR.h @@ -5,57 +5,22 @@ #include #include +#include #include #include #include +#include #include #include #include #include #include #include +#include +#include #include #include -#include -#include -#include -#include - -class vtkSeedWidget2 - : public vtkSeedWidget -{ -public: - // Description: - // Instantiate this class. - static vtkSeedWidget2 *New() - { - return new vtkSeedWidget2; - } - - // Description: - // Standard methods for a VTK class. - vtkTypeMacro(vtkSeedWidget2, vtkSeedWidget); - - virtual vtkPickingManager* GetPickingManager( ) - { - return( this->Superclass::GetPickingManager( ) ); - } - -protected: - vtkSeedWidget2() - : Superclass( ) - { - } - virtual ~vtkSeedWidget2() - { - } - -private: - vtkSeedWidget2(const vtkSeedWidget2&); //Not implemented - void operator=(const vtkSeedWidget2&); //Not implemented -}; - namespace fpa { namespace VTK @@ -74,34 +39,36 @@ namespace fpa void AddPolyData( vtkPolyData* pd, double r, double g, double b ); + unsigned int GetNumberOfSeeds( ) const; + void GetSeed( int n, double* s ) const; + vtkRenderWindow* GetWindow( ) const; vtkRenderer* GetRenderer( ) const; void Start( ); protected: - vtkSmartPointer< vtkImageData > m_Image; - vtkSmartPointer< vtkOutlineSource > m_Outline; - vtkSmartPointer< vtkPolyDataMapper > m_OutlineMapper; - vtkSmartPointer< vtkActor > m_OutlineActor; - vtkSmartPointer< vtkCellPicker > m_Picker; - vtkSmartPointer< vtkImagePlaneWidget > m_WidgetX; - vtkSmartPointer< vtkImagePlaneWidget > m_WidgetY; - vtkSmartPointer< vtkImagePlaneWidget > m_WidgetZ; - vtkSmartPointer< vtkRenderer > m_Renderer; - vtkSmartPointer< vtkRenderWindow > m_Window; + vtkSmartPointer< vtkImageData > m_Image; + vtkSmartPointer< vtkOutlineSource > m_Outline; + vtkSmartPointer< vtkPolyDataMapper > m_OutlineMapper; + vtkSmartPointer< vtkActor > m_OutlineActor; + vtkSmartPointer< vtkCellPicker > m_Picker; + vtkSmartPointer< vtkImagePlaneWidget > m_WidgetX; + vtkSmartPointer< vtkImagePlaneWidget > m_WidgetY; + vtkSmartPointer< vtkImagePlaneWidget > m_WidgetZ; + vtkSmartPointer< vtkRenderer > m_Renderer; + vtkSmartPointer< vtkRenderWindow > m_Window; vtkSmartPointer< vtkRenderWindowInteractor > m_Interactor; - std::vector< vtkSmartPointer< vtkPolyData > > m_PolyDatas; + std::vector< vtkSmartPointer< vtkPolyData > > m_PolyDatas; std::vector< vtkSmartPointer< vtkPolyDataMapper > > m_Mappers; - std::vector< vtkSmartPointer< vtkActor > > m_Actors; - + std::vector< vtkSmartPointer< vtkActor > > m_Actors; // Create the widget and its representation - vtkSmartPointer handlePointRep3D; - vtkSmartPointer seedRep; - vtkSmartPointer seedWidget; - + vtkSmartPointer< vtkPointHandleRepresentation3D > + m_SeedHandleRepresentation; + vtkSmartPointer< vtkSeedRepresentation > m_SeedRepresentation; + vtkSmartPointer< vtkSeedWidget > m_SeedWidget; }; } // ecapseman diff --git a/lib/fpa/VTK/SeedWidgetCorrector.cxx b/lib/fpa/VTK/SeedWidgetCorrector.cxx new file mode 100644 index 0000000..93f32a1 --- /dev/null +++ b/lib/fpa/VTK/SeedWidgetCorrector.cxx @@ -0,0 +1,79 @@ +#include + +#include +#include + +#include +#include +#include + +// ------------------------------------------------------------------------- +fpa::VTK::SeedWidgetCorrector* +fpa::VTK::SeedWidgetCorrector:: +New( ) +{ + return( new fpa::VTK::SeedWidgetCorrector ); +} + +// ------------------------------------------------------------------------- +void fpa::VTK::SeedWidgetCorrector:: +Configure( vtkCellPicker* picker ) +{ + this->m_Picker = picker; +} + +// ------------------------------------------------------------------------- +void fpa::VTK::SeedWidgetCorrector:: +Execute( vtkObject* caller, unsigned long eventId, void* arguments ) +{ + vtkSeedWidget* wdg = dynamic_cast< vtkSeedWidget* >( caller ); + if( wdg == NULL || this->m_Picker == NULL ) + return; + + if( eventId == vtkCommand::PlacePointEvent ) + { + // Get newly added handle and its associated objects + int hId = *( reinterpret_cast< int* >( arguments ) ); + vtkHandleWidget* hWdg = wdg->GetSeed( hId ); + if( hWdg == NULL ) + return; + vtkHandleRepresentation* hRep = + dynamic_cast< vtkHandleRepresentation* >( hWdg->GetRepresentation( ) ); + if( hRep == NULL ) + return; + + // Get clicked point and 3D scene + double p[ 3 ]; + hRep->GetDisplayPosition( p ); + vtkRenderWindowInteractor* iren = wdg->GetInteractor( ); + if( iren == NULL ) + return; + vtkRenderer* ren = + iren->FindPokedRenderer( int( p[ 0 ] ), int( p[ 1 ] ) ); + if( ren == NULL ) + return; + + // Correct point with given picker + double q[ 3 ]; + this->m_Picker->Pick( p[ 0 ], p[ 1 ], p[ 2 ], ren ); + this->m_Picker->GetPickPosition( q ); + hRep->SetWorldPosition( q ); + + } // fi +} + +// ------------------------------------------------------------------------- +fpa::VTK::SeedWidgetCorrector:: +SeedWidgetCorrector( ) + : vtkCommand( ), + m_Picker( NULL ) +{ +} + +// ------------------------------------------------------------------------- +fpa::VTK::SeedWidgetCorrector:: +~SeedWidgetCorrector( ) +{ +} + +// eof - $RCSfile$ diff --git a/lib/fpa/VTK/SeedWidgetCorrector.h b/lib/fpa/VTK/SeedWidgetCorrector.h new file mode 100644 index 0000000..426a75a --- /dev/null +++ b/lib/fpa/VTK/SeedWidgetCorrector.h @@ -0,0 +1,44 @@ +#ifndef __FPA__VTK__SEEDWIDGETCORRECTOR__H__ +#define __FPA__VTK__SEEDWIDGETCORRECTOR__H__ + +#include +#include + +class vtkCellPicker; +class vtkSeedWidget; + +namespace fpa +{ + namespace VTK + { + /** + */ + class FrontAlgorithms_EXPORT SeedWidgetCorrector + : public vtkCommand + { + public: + static SeedWidgetCorrector* New( ); + + void Configure( vtkCellPicker* picker ); + + virtual void Execute( + vtkObject* caller, + unsigned long eventId, + void* arguments + ); + + protected: + SeedWidgetCorrector( ); + virtual ~SeedWidgetCorrector( ); + + protected: + vtkCellPicker* m_Picker; + }; + + } // ecapseman + +} // ecapseman + +#endif // __FPA__VTK__SEEDWIDGETCORRECTOR__H__ + +// eof - $RCSfile$