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