]> Creatis software - cpPlugins.git/blob - plugins/Widgets/SplineWidget.cxx
1bba488aaa070f470e74a8148d017332f1b4bdbf
[cpPlugins.git] / plugins / Widgets / SplineWidget.cxx
1 #include <plugins/Widgets/SplineWidget.h>
2 #include <cpPlugins/DataObjects/Image.h>
3 #include <cpPlugins/DataObjects/Mesh.h>
4 #include <cpExtensions/Visualization/WindowLevelImageActor.h>
5
6 #include <vtkImageData.h>
7 #include <vtkRenderer.h>
8 #include <vtkSplineWidget.h>
9
10 // -------------------------------------------------------------------------
11 cpPluginsWidgets::SplineWidget::
12 SplineWidget( )
13   : Superclass( ),
14     m_Configured( false )
15 {
16   typedef cpPlugins::BaseObjects::DataObject _TData;
17   typedef cpPlugins::DataObjects::Mesh       _TMesh;
18
19   this->_ConfigureInput< _TData >( "Input", false, false );
20   this->_ConfigureOutput< _TMesh >( "Output" );
21   this->m_Contour = vtkSmartPointer< vtkPolyData >::New( );
22   this->m_Contour->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
23   this->m_Contour->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
24   this->m_Contour->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
25   this->m_Contour->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
26   this->m_Contour->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
27   this->GetOutput( "Output" )->SetVTK( this->m_Contour );
28 }
29
30 // -------------------------------------------------------------------------
31 cpPluginsWidgets::SplineWidget::
32 ~SplineWidget( )
33 {
34 }
35
36 // -------------------------------------------------------------------------
37 void cpPluginsWidgets::SplineWidget::
38 _GenerateData( )
39 {
40   typedef cpExtensions::Visualization::WindowLevelImageActor _TActor;
41
42   auto image = this->GetInputData< vtkImageData >( "Input" );
43   if( image == NULL )
44     this->_Error( "Invalid input image." );
45   if( this->m_Interactors.size( ) == 0 )
46     this->_Error( "Give at least one interactor." );
47
48   auto wdg = this->_CreateVTK< vtkSplineWidget >( );
49   if( this->m_Configured )
50   {
51     wdg->GetPolyData( this->m_Contour.GetPointer( ) );
52     wdg->Off( );
53   }
54   else
55   {
56     auto iIt = this->m_Interactors.begin( );
57     vtkRenderWindowInteractor* iren = NULL;
58     vtkRenderer* ren = NULL;
59     _TActor* actor = NULL;
60     for( ; iIt != this->m_Interactors.end( ); ++iIt )
61     {
62       auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
63       if( r != NULL )
64       {
65         auto props = r->GetViewProps( );
66         if( props != NULL )
67         {
68           props->InitTraversal( );
69           while( vtkProp* prop = props->GetNextProp( ) )
70           {
71             auto a = dynamic_cast< _TActor* >( prop );
72             if( a != NULL )
73               if( a->GetImage( ) == image )
74               {
75                 iren = *iIt;
76                 actor = a;
77                 ren = r;
78
79               } // fi
80
81           } // elihw
82
83         } // fi
84
85       } // fi
86
87     } // rof
88     if( actor == NULL || ren == NULL || iren == NULL )
89       this->_Error( "Invalid actor and/or renderer." );
90
91     // Widget configuration
92     wdg->SetCurrentRenderer( ren );
93     wdg->SetDefaultRenderer( ren );
94     wdg->SetInputData( image );
95     wdg->SetProp3D( actor );
96     wdg->SetInteractor( iren );
97     double bnds[ 6 ];
98     image->GetBounds( bnds );
99     wdg->PlaceWidget(
100       bnds[ 0 ], bnds[ 1 ],
101       bnds[ 2 ], bnds[ 3 ],
102       bnds[ 4 ], bnds[ 5 ]
103       );
104     wdg->ProjectToPlaneOn( );
105     wdg->SetProjectionNormalToZAxes( );
106     wdg->SetProjectionPosition(
107       (
108         actor->GetBounds( )[ 4 ] +
109         actor->GetBounds( )[ 5 ]
110         ) / double( 2 )
111       );
112     wdg->SetHandleSize( 0.005 );
113     wdg->SetNumberOfHandles( 3 );
114     wdg->On( );
115     this->m_Configured = true;
116
117   } // fi
118 }
119
120 // eof - $RCSfile$