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