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