]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Visualization/ImageSliceActors.cxx
...
[cpPlugins.git] / lib / cpExtensions / Visualization / ImageSliceActors.cxx
1 #include <cpExtensions/Visualization/ImageSliceActors.h>
2
3 #include <vtkAlgorithmOutput.h>
4 #include <vtkCellArray.h>
5 #include <vtkImageData.h>
6 #include <vtkInformation.h>
7 #include <vtkPlane.h>
8 #include <vtkPoints.h>
9 #include <vtkProperty.h>
10 #include <vtkStreamingDemandDrivenPipeline.h>
11 #include <vtkTextProperty.h>
12
13 // -------------------------------------------------------------------------
14 cpExtensions::Visualization::ImageSliceActors*
15 cpExtensions::Visualization::ImageSliceActors::
16 New( )
17 {
18   return( new Self( ) );
19 }
20
21 // -------------------------------------------------------------------------
22 void cpExtensions::Visualization::ImageSliceActors::
23 SetInputConnection( vtkAlgorithmOutput* aout, int axis )
24 {
25   this->SliceMapper->SetInputConnection( aout );
26   this->SliceMapper->SetOrientation( axis );
27   this->SliceMapper->Update( );
28   this->SetSliceNumber( this->SliceMapper->GetSliceNumber( ) );
29   this->ImageActor->SetMapper( this->SliceMapper );
30   this->ImageActor->Modified( );
31   this->Modified( );
32 }
33
34 // -------------------------------------------------------------------------
35 void cpExtensions::Visualization::ImageSliceActors::
36 SetInputData( vtkImageData* data, int axis )
37 {
38   this->SliceMapper->SetInputData( data );
39   this->SliceMapper->SetOrientation( axis );
40   this->SliceMapper->Update( );
41   this->SetSliceNumber( this->SliceMapper->GetSliceNumber( ) );
42   this->ImageActor->SetMapper( this->SliceMapper );
43   this->ImageActor->Modified( );
44   this->Modified( );
45 }
46
47 // -------------------------------------------------------------------------
48 double* cpExtensions::Visualization::ImageSliceActors::
49 GetDisplayBounds( ) const
50 {
51   return( this->ImageActor->GetDisplayBounds( ) );
52 }
53
54 // -------------------------------------------------------------------------
55 void cpExtensions::Visualization::ImageSliceActors::
56 GetDisplayBounds( double bounds[ 6 ] ) const
57 {
58   this->ImageActor->GetDisplayBounds( bounds );
59 }
60
61 // -------------------------------------------------------------------------
62 int cpExtensions::Visualization::ImageSliceActors::
63 GetAxis( ) const
64 {
65   return( this->SliceMapper->GetOrientation( ) );
66 }
67
68 // -------------------------------------------------------------------------
69 int cpExtensions::Visualization::ImageSliceActors::
70 GetSliceNumber( ) const
71 {
72   return( this->SliceMapper->GetSliceNumber( ) );
73 }
74
75 // -------------------------------------------------------------------------
76 int cpExtensions::Visualization::ImageSliceActors::
77 GetSliceNumberMinValue( ) const
78 {
79   return( this->SliceMapper->GetSliceNumberMinValue( ) );
80 }
81
82 // -------------------------------------------------------------------------
83 int cpExtensions::Visualization::ImageSliceActors::
84 GetSliceNumberMaxValue( ) const
85 {
86   return( this->SliceMapper->GetSliceNumberMaxValue( ) );
87 }
88
89 // -------------------------------------------------------------------------
90 void cpExtensions::Visualization::ImageSliceActors::
91 SetSliceNumber( const int& slice )
92 {
93   this->SliceMapper->SetSliceNumber( slice );
94   this->SliceMapper->Update( );
95
96   // Compute plane
97   vtkAlgorithm* algo = this->SliceMapper->GetInputAlgorithm( );
98   vtkInformation* info = algo->GetOutputInformation( 0 );
99   int ext[ 6 ];
100   double ori[ 3 ], spac[ 3 ], pos[ 3 ];
101   info->Get( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT( ), ext );
102   info->Get( vtkDataObject::ORIGIN( ), ori );
103   info->Get( vtkDataObject::SPACING( ), spac );
104   this->SliceMapper->GetSlicePlane( )->GetOrigin( pos );
105
106   // Prevent obscuring voxels by offsetting the plane geometry
107   double xbnds[ ] =
108     {
109       ori[ 0 ] + ( spac[ 0 ] * double( ext[ 0 ] ) ),
110       ori[ 0 ] + ( spac[ 0 ] * double( ext[ 1 ] ) )
111     };
112   double ybnds[ ] =
113     {
114       ori[ 1 ] + ( spac[ 1 ] * double( ext[ 2 ] ) ),
115       ori[ 1 ] + ( spac[ 1 ] * double( ext[ 3 ] ) )
116     };
117   double zbnds[ ] =
118     {
119       ori[ 2 ] + ( spac[ 2 ] * double( ext[ 4 ] ) ),
120       ori[ 2 ] + ( spac[ 2 ] * double( ext[ 5 ] ) )
121     };
122
123   if( spac[ 0 ] < double( 0 ) )
124   {
125     double t = xbnds[ 0 ];
126     xbnds[ 0 ] = xbnds[ 1 ];
127     xbnds[ 1 ] = t;
128
129   } // fi
130   if( spac[ 1 ] < double( 0 ) )
131   {
132     double t = ybnds[ 0 ];
133     ybnds[ 0 ] = ybnds[ 1 ];
134     ybnds[ 1 ] = t;
135
136   } // fi
137   if( spac[ 2 ] < double( 0 ) )
138   {
139     double t = zbnds[ 0 ];
140     zbnds[ 0 ] = zbnds[ 1 ];
141     zbnds[ 1 ] = t;
142
143   } // fi
144
145   int axis = this->SliceMapper->GetOrientation( );
146   this->PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
147   this->PlaneActor->GetProperty( )->SetLineWidth( 2 );
148   vtkPoints* plane_points = this->PlaneSource->GetPoints( );
149   if( axis == 0 ) // YZ, x-normal
150   {
151     plane_points->SetPoint( 0, pos[ 0 ], ybnds[ 0 ], zbnds[ 0 ] );
152     plane_points->SetPoint( 1, pos[ 0 ], ybnds[ 1 ], zbnds[ 0 ] );
153     plane_points->SetPoint( 2, pos[ 0 ], ybnds[ 1 ], zbnds[ 1 ] );
154     plane_points->SetPoint( 3, pos[ 0 ], ybnds[ 0 ], zbnds[ 1 ] );
155     this->PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
156   }
157   else if( axis == 1 ) // ZX, y-normal
158   {
159     plane_points->SetPoint( 0, xbnds[ 0 ], pos[ 1 ], zbnds[ 0 ] );
160     plane_points->SetPoint( 1, xbnds[ 0 ], pos[ 1 ], zbnds[ 1 ] );
161     plane_points->SetPoint( 2, xbnds[ 1 ], pos[ 1 ], zbnds[ 1 ] );
162     plane_points->SetPoint( 3, xbnds[ 1 ], pos[ 1 ], zbnds[ 0 ] );
163     this->PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
164   }
165   else // XY, z-normal
166   {
167     plane_points->SetPoint( 0, xbnds[ 0 ], ybnds[ 0 ], pos[ 2 ] );
168     plane_points->SetPoint( 1, xbnds[ 1 ], ybnds[ 0 ], pos[ 2 ] );
169     plane_points->SetPoint( 2, xbnds[ 1 ], ybnds[ 1 ], pos[ 2 ] );
170     plane_points->SetPoint( 3, xbnds[ 0 ], ybnds[ 1 ], pos[ 2 ] );
171     this->PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
172
173   } // fi
174   this->PlaneSource->Modified( );
175   this->PlaneMapper->Modified( );
176   this->PlaneActor->Modified( );
177   this->Modified( );
178 }
179
180 // -------------------------------------------------------------------------
181 void cpExtensions::Visualization::ImageSliceActors::
182 UpdateText( )
183 {
184   char axis;
185   int axId = this->SliceMapper->GetOrientation( );
186   if     ( axId == 0 ) axis = 'X';
187   else if( axId == 1 ) axis = 'Y';
188   else if( axId == 2 ) axis = 'Z';
189
190   std::sprintf(
191     this->TextBuffer, "Axis: %c (%d)",
192     axis, this->SliceMapper->GetSliceNumber( )
193     );
194   this->TextActor->SetInput( this->TextBuffer );
195   this->TextActor->Modified( );
196   this->Modified( );
197 }
198
199 // -------------------------------------------------------------------------
200 cpExtensions::Visualization::ImageSliceActors::
201 ImageSliceActors( )
202   : Superclass( )
203 {
204   this->SliceMapper = vtkSmartPointer< vtkImageSliceMapper >::New( );
205   this->PlaneSource = vtkSmartPointer< vtkPolyData >::New( );
206   this->PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
207   this->ImageActor  = vtkSmartPointer< vtkImageActor >::New( );
208   this->TextActor   = vtkSmartPointer< vtkTextActor >::New( );
209   this->PlaneActor  = vtkSmartPointer< vtkActor >::New( );
210
211   this->ImageActorIndex = this->GetNumberOfItems( );
212   this->TextActorIndex = this->ImageActorIndex + 1;
213   this->PlaneActorIndex = this->ImageActorIndex + 2;
214   this->AddItem( this->ImageActor );
215   this->AddItem( this->TextActor );
216   this->AddItem( this->PlaneActor );
217
218   // Configuration
219   vtkSmartPointer< vtkPoints > plane_points =
220     vtkSmartPointer< vtkPoints >::New( );
221   vtkSmartPointer< vtkCellArray > plane_lines =
222     vtkSmartPointer< vtkCellArray >::New( );
223
224   plane_points->InsertNextPoint( 0, 0, 0 );
225   plane_points->InsertNextPoint( 0, 1, 0 );
226   plane_points->InsertNextPoint( 1, 1, 0 );
227   plane_points->InsertNextPoint( 1, 0, 0 );
228   plane_lines->InsertNextCell( 5 );
229   plane_lines->InsertCellPoint( 0 );
230   plane_lines->InsertCellPoint( 1 );
231   plane_lines->InsertCellPoint( 2 );
232   plane_lines->InsertCellPoint( 3 );
233   plane_lines->InsertCellPoint( 0 );
234   this->PlaneSource->SetPoints( plane_points );
235   this->PlaneSource->SetLines( plane_lines );
236
237   this->PlaneMapper->SetInputData( this->PlaneSource );
238   this->PlaneActor->SetMapper( this->PlaneMapper );
239
240   this->TextActor->SetTextScaleModeToNone( );
241   vtkTextProperty* textprop = this->TextActor->GetTextProperty( );
242   textprop->SetColor( 1, 1, 1 );
243   textprop->SetFontFamilyToCourier( );
244   textprop->SetFontSize( 18 );
245   textprop->BoldOff( );
246   textprop->ItalicOff( );
247   textprop->ShadowOff( );
248   textprop->SetJustificationToLeft( );
249   textprop->SetVerticalJustificationToBottom( );
250   vtkCoordinate* coord = this->TextActor->GetPositionCoordinate( );
251   coord->SetCoordinateSystemToNormalizedViewport( );
252   coord->SetValue( 0.01, 0.01 );
253 }
254
255 // -------------------------------------------------------------------------
256 cpExtensions::Visualization::ImageSliceActors::
257 ~ImageSliceActors( )
258 {
259 }
260
261 // eof - $RCSfile$