]> Creatis software - cpPlugins.git/blob - lib/ivq/VTK/ImageSlicePointPlacer.cxx
...
[cpPlugins.git] / lib / ivq / VTK / ImageSlicePointPlacer.cxx
1 /* =======================================================================
2  * @author: Leonardo Florez-Valencia
3  * @email: florez-l@javeriana.edu.co
4  * =======================================================================
5  */
6
7 #include <ivq/VTK/ImageSlicePointPlacer.h>
8
9 #include <vtkBoundedPlanePointPlacer.h>
10 #include <vtkImageData.h>
11 #include <vtkImageMapper3D.h>
12 #include <vtkImageSlice.h>
13 #include <vtkImageSliceMapper.h>
14 #include <vtkPlane.h>
15
16 // -------------------------------------------------------------------------
17 vtkCxxSetObjectMacro(
18   ivq::VTK::ImageSlicePointPlacer, ImageSlice, vtkImageSlice
19   );
20
21 // -------------------------------------------------------------------------
22 ivq::VTK::ImageSlicePointPlacer::
23 Self* ivq::VTK::ImageSlicePointPlacer::
24 New( )
25 {
26   return( new Self( ) );
27 }
28
29 // -------------------------------------------------------------------------
30 int ivq::VTK::ImageSlicePointPlacer::
31 ComputeWorldPosition(
32   vtkRenderer* ren,
33   double displayPos[ 2 ],
34   double worldPos[ 3 ],
35   double worldOrient[ 9 ]
36   )
37 {
38   if( !this->UpdateInternalState( ) )
39     return( 0 );
40   return(
41     this->Placer->ComputeWorldPosition(
42       ren, displayPos, worldPos, worldOrient
43       )
44     );
45 }
46
47 // -------------------------------------------------------------------------
48 int ivq::VTK::ImageSlicePointPlacer::
49 ComputeWorldPosition(
50   vtkRenderer* ren,
51   double displayPos[ 2 ],
52   double refWorldPos[ 2 ],
53   double worldPos[ 3 ],
54   double worldOrient[ 9 ]
55   )
56 {
57   if( !this->UpdateInternalState( ) )
58     return( 0 );
59   return(
60     this->Placer->ComputeWorldPosition(
61       ren, displayPos, refWorldPos, worldPos, worldOrient
62       )
63     );
64 }
65
66 // -------------------------------------------------------------------------
67 int ivq::VTK::ImageSlicePointPlacer::
68 ValidateWorldPosition( double worldPos[ 3 ] )
69 {
70   if( !this->UpdateInternalState( ) )
71     return( 0 );
72   return( this->Placer->ValidateWorldPosition( worldPos ) );
73 }
74
75 // -------------------------------------------------------------------------
76 int ivq::VTK::ImageSlicePointPlacer::
77 ValidateWorldPosition( double worldPos[ 3 ], double worldOrient[ 9 ] )
78 {
79   if( !this->UpdateInternalState( ) )
80     return( 0 );
81   return( this->Placer->ValidateWorldPosition( worldPos, worldOrient ) );
82 }
83
84 // -------------------------------------------------------------------------
85 int ivq::VTK::ImageSlicePointPlacer::
86 UpdateWorldPosition(
87   vtkRenderer* ren, double worldPos[ 3 ], double worldOrient[ 9 ]
88   )
89 {
90   if( !this->UpdateInternalState( ) )
91     return( 0 );
92   return( this->Placer->UpdateWorldPosition( ren, worldPos, worldOrient ) );
93 }
94
95 // -------------------------------------------------------------------------
96 int ivq::VTK::ImageSlicePointPlacer::
97 UpdateInternalState( )
98 {
99   if( !this->ImageSlice )
100     return( 0 );
101
102   vtkImageData* input = this->ImageSlice->GetMapper( )->GetInput( );
103   if( !input )
104     return( 0 );
105
106   double spacing[ 3 ];
107   input->GetSpacing( spacing );
108
109   double origin[ 3 ];
110   input->GetOrigin( origin );
111
112   double b[ 6 ];
113   this->ImageSlice->GetBounds( b );
114   if( this->Bounds[ 0 ] != VTK_DOUBLE_MAX)
115   {
116     b[ 0 ] = ( b[ 0 ] < this->Bounds[ 0 ] )? this->Bounds[ 0 ] : b[ 0 ];
117     b[ 1 ] = ( b[ 1 ] > this->Bounds[ 1 ] )? this->Bounds[ 1 ] : b[ 1 ];
118     b[ 2 ] = ( b[ 2 ] < this->Bounds[ 2 ] )? this->Bounds[ 2 ] : b[ 2 ];
119     b[ 3 ] = ( b[ 3 ] > this->Bounds[ 3 ] )? this->Bounds[ 3 ] : b[ 3 ];
120     b[ 4 ] = ( b[ 4 ] < this->Bounds[ 4 ] )? this->Bounds[ 4 ] : b[ 4 ];
121     b[ 5 ] = ( b[ 5 ] > this->Bounds[ 5 ] )? this->Bounds[ 5 ] : b[ 5 ];
122
123   } // fi
124
125   int displayExtent[ 6 ] = { 0, 1, 0, 1, 0, 1 };
126   input->GetExtent( displayExtent );
127   vtkImageSliceMapper* mapper =
128     vtkImageSliceMapper::SafeDownCast( this->ImageSlice->GetMapper( ) );
129   if( mapper != NULL )
130   {
131     int ori = mapper->GetOrientation( );
132     displayExtent[ ( ori << 1 ) + 1 ] =
133       displayExtent[ ori << 1 ] =
134       mapper->GetSliceNumber( );
135
136   } // fi
137
138   int axis;
139   double position;
140   if( displayExtent[ 0 ] == displayExtent[ 1 ] )
141   {
142     axis = vtkBoundedPlanePointPlacer::XAxis;
143     position = origin[ 0 ] + displayExtent[ 0 ] * spacing[ 0 ];
144   }
145   else if( displayExtent[ 2 ] == displayExtent[ 3 ] )
146   {
147     axis = vtkBoundedPlanePointPlacer::YAxis;
148     position = origin[ 1 ] + displayExtent[ 2 ] * spacing[ 1 ];
149   }
150   else if( displayExtent[ 4 ] == displayExtent[ 5 ] )
151   {
152     axis = vtkBoundedPlanePointPlacer::ZAxis;
153     position = origin[ 2 ] + displayExtent[ 4 ] * spacing[ 2 ];
154   }
155   else
156   {
157     vtkErrorMacro( "Incorrect display extent in Image Slice" );
158     return( 0 );
159
160   } // fi
161
162   if(
163     axis != this->Placer->GetProjectionNormal( ) ||
164     position != this->Placer->GetProjectionPosition( ) ||
165     b[ 0 ] != this->SavedBounds[ 0 ] ||
166     b[ 1 ] != this->SavedBounds[ 1 ] ||
167     b[ 2 ] != this->SavedBounds[ 2 ] ||
168     b[ 3 ] != this->SavedBounds[ 3 ] ||
169     b[ 4 ] != this->SavedBounds[ 4 ] ||
170     b[ 5 ] != this->SavedBounds[ 5 ]
171     )
172   {
173     this->SavedBounds[ 0 ] = b[ 0 ];
174     this->SavedBounds[ 1 ] = b[ 1 ];
175     this->SavedBounds[ 2 ] = b[ 2 ];
176     this->SavedBounds[ 3 ] = b[ 3 ];
177     this->SavedBounds[ 4 ] = b[ 4 ];
178     this->SavedBounds[ 5 ] = b[ 5 ];
179
180     this->Placer->SetProjectionNormal( axis );
181     this->Placer->SetProjectionPosition( position );
182
183     this->Placer->RemoveAllBoundingPlanes( );
184
185     vtkPlane* plane;
186     if( axis != vtkBoundedPlanePointPlacer::XAxis )
187     {
188       plane = vtkPlane::New( );
189       plane->SetOrigin( b[ 0 ], b[ 2 ], b[ 4 ] );
190       plane->SetNormal( 1.0, 0.0, 0.0 );
191       this->Placer->AddBoundingPlane( plane );
192       plane->Delete( );
193
194       plane = vtkPlane::New();
195       plane->SetOrigin( b[ 1 ], b[ 3 ], b[ 5 ] );
196       plane->SetNormal( -1.0, 0.0, 0.0 );
197       this->Placer->AddBoundingPlane( plane );
198       plane->Delete( );
199
200     } // fi
201
202     if( axis != vtkBoundedPlanePointPlacer::YAxis )
203     {
204       plane = vtkPlane::New( );
205       plane->SetOrigin( b[ 0 ], b[ 2 ], b[ 4 ] );
206       plane->SetNormal( 0.0, 1.0, 0.0 );
207       this->Placer->AddBoundingPlane( plane );
208       plane->Delete( );
209
210       plane = vtkPlane::New( );
211       plane->SetOrigin( b[ 1 ], b[ 3 ], b[ 5 ] );
212       plane->SetNormal( 0.0, -1.0, 0.0 );
213       this->Placer->AddBoundingPlane( plane );
214       plane->Delete( );
215
216     } // fi
217
218     if( axis != vtkBoundedPlanePointPlacer::ZAxis )
219     {
220       plane = vtkPlane::New( );
221       plane->SetOrigin( b[ 0 ], b[ 2 ], b[ 4 ] );
222       plane->SetNormal( 0.0, 0.0, 1.0 );
223       this->Placer->AddBoundingPlane( plane );
224       plane->Delete( );
225
226       plane = vtkPlane::New( );
227       plane->SetOrigin( b[ 1 ], b[ 3 ], b[ 5 ] );
228       plane->SetNormal( 0.0, 0.0, -1.0 );
229       this->Placer->AddBoundingPlane( plane );
230       plane->Delete( );
231
232     } // fi
233
234     this->Modified( );
235
236   } // fi
237   return( 1 );
238 }
239
240 // -------------------------------------------------------------------------
241 void ivq::VTK::ImageSlicePointPlacer::
242 SetWorldTolerance( double tol )
243 {
244   double t =
245     ( tol < 0.0 )? 0.0: ( ( tol > VTK_DOUBLE_MAX )? VTK_DOUBLE_MAX: tol );
246   if( this->WorldTolerance != t )
247   {
248     this->WorldTolerance = t;
249     this->Placer->SetWorldTolerance( tol );
250     this->Modified( );
251
252   } // fi
253 }
254
255 // -------------------------------------------------------------------------
256 ivq::VTK::ImageSlicePointPlacer::
257 ImageSlicePointPlacer( )
258   : Superclass( )
259 {
260   this->Placer = vtkBoundedPlanePointPlacer::New( );
261   this->ImageSlice = NULL;
262   this->SavedBounds[ 0 ] = 0.0;
263   this->SavedBounds[ 1 ] = 0.0;
264   this->SavedBounds[ 2 ] = 0.0;
265   this->SavedBounds[ 3 ] = 0.0;
266   this->SavedBounds[ 4 ] = 0.0;
267   this->SavedBounds[ 5 ] = 0.0;
268   this->Bounds[ 0 ] = this->Bounds[ 2 ] = this->Bounds[ 4 ] = VTK_DOUBLE_MAX;
269   this->Bounds[ 1 ] = this->Bounds[ 3 ] = this->Bounds[ 5 ] = VTK_DOUBLE_MIN;
270 }
271
272 // -------------------------------------------------------------------------
273 ivq::VTK::ImageSlicePointPlacer::
274 ~ImageSlicePointPlacer( )
275 {
276   this->Placer->Delete( );
277   this->SetImageSlice( NULL );
278 }
279
280 // eof - $RCSfile$