1 /* =======================================================================
2 * @author: Leonardo Florez-Valencia
3 * @email: florez-l@javeriana.edu.co
4 * =======================================================================
7 #include <ivq/VTK/ImageSlicePointPlacer.h>
9 #include <vtkBoundedPlanePointPlacer.h>
10 #include <vtkImageData.h>
11 #include <vtkImageMapper3D.h>
12 #include <vtkImageSlice.h>
13 #include <vtkImageSliceMapper.h>
16 // -------------------------------------------------------------------------
18 ivq::VTK::ImageSlicePointPlacer, ImageSlice, vtkImageSlice
21 // -------------------------------------------------------------------------
22 ivq::VTK::ImageSlicePointPlacer::
23 Self* ivq::VTK::ImageSlicePointPlacer::
26 return( new Self( ) );
29 // -------------------------------------------------------------------------
30 int ivq::VTK::ImageSlicePointPlacer::
33 double displayPos[ 2 ],
35 double worldOrient[ 9 ]
38 if( !this->UpdateInternalState( ) )
41 this->Placer->ComputeWorldPosition(
42 ren, displayPos, worldPos, worldOrient
47 // -------------------------------------------------------------------------
48 int ivq::VTK::ImageSlicePointPlacer::
51 double displayPos[ 2 ],
52 double refWorldPos[ 2 ],
54 double worldOrient[ 9 ]
57 if( !this->UpdateInternalState( ) )
60 this->Placer->ComputeWorldPosition(
61 ren, displayPos, refWorldPos, worldPos, worldOrient
66 // -------------------------------------------------------------------------
67 int ivq::VTK::ImageSlicePointPlacer::
68 ValidateWorldPosition( double worldPos[ 3 ] )
70 if( !this->UpdateInternalState( ) )
72 return( this->Placer->ValidateWorldPosition( worldPos ) );
75 // -------------------------------------------------------------------------
76 int ivq::VTK::ImageSlicePointPlacer::
77 ValidateWorldPosition( double worldPos[ 3 ], double worldOrient[ 9 ] )
79 if( !this->UpdateInternalState( ) )
81 return( this->Placer->ValidateWorldPosition( worldPos, worldOrient ) );
84 // -------------------------------------------------------------------------
85 int ivq::VTK::ImageSlicePointPlacer::
87 vtkRenderer* ren, double worldPos[ 3 ], double worldOrient[ 9 ]
90 if( !this->UpdateInternalState( ) )
92 return( this->Placer->UpdateWorldPosition( ren, worldPos, worldOrient ) );
95 // -------------------------------------------------------------------------
96 int ivq::VTK::ImageSlicePointPlacer::
97 UpdateInternalState( )
99 if( !this->ImageSlice )
102 vtkImageData* input = this->ImageSlice->GetMapper( )->GetInput( );
107 input->GetSpacing( spacing );
110 input->GetOrigin( origin );
113 this->ImageSlice->GetBounds( b );
114 if( this->Bounds[ 0 ] != VTK_DOUBLE_MAX)
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 ];
125 int displayExtent[ 6 ] = { 0, 1, 0, 1, 0, 1 };
126 input->GetExtent( displayExtent );
127 vtkImageSliceMapper* mapper =
128 vtkImageSliceMapper::SafeDownCast( this->ImageSlice->GetMapper( ) );
131 int ori = mapper->GetOrientation( );
132 displayExtent[ ( ori << 1 ) + 1 ] =
133 displayExtent[ ori << 1 ] =
134 mapper->GetSliceNumber( );
140 if( displayExtent[ 0 ] == displayExtent[ 1 ] )
142 axis = vtkBoundedPlanePointPlacer::XAxis;
143 position = origin[ 0 ] + displayExtent[ 0 ] * spacing[ 0 ];
145 else if( displayExtent[ 2 ] == displayExtent[ 3 ] )
147 axis = vtkBoundedPlanePointPlacer::YAxis;
148 position = origin[ 1 ] + displayExtent[ 2 ] * spacing[ 1 ];
150 else if( displayExtent[ 4 ] == displayExtent[ 5 ] )
152 axis = vtkBoundedPlanePointPlacer::ZAxis;
153 position = origin[ 2 ] + displayExtent[ 4 ] * spacing[ 2 ];
157 vtkErrorMacro( "Incorrect display extent in Image Slice" );
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 ]
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 ];
180 this->Placer->SetProjectionNormal( axis );
181 this->Placer->SetProjectionPosition( position );
183 this->Placer->RemoveAllBoundingPlanes( );
186 if( axis != vtkBoundedPlanePointPlacer::XAxis )
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 );
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 );
202 if( axis != vtkBoundedPlanePointPlacer::YAxis )
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 );
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 );
218 if( axis != vtkBoundedPlanePointPlacer::ZAxis )
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 );
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 );
240 // -------------------------------------------------------------------------
241 void ivq::VTK::ImageSlicePointPlacer::
242 SetWorldTolerance( double tol )
245 ( tol < 0.0 )? 0.0: ( ( tol > VTK_DOUBLE_MAX )? VTK_DOUBLE_MAX: tol );
246 if( this->WorldTolerance != t )
248 this->WorldTolerance = t;
249 this->Placer->SetWorldTolerance( tol );
255 // -------------------------------------------------------------------------
256 ivq::VTK::ImageSlicePointPlacer::
257 ImageSlicePointPlacer( )
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;
272 // -------------------------------------------------------------------------
273 ivq::VTK::ImageSlicePointPlacer::
274 ~ImageSlicePointPlacer( )
276 this->Placer->Delete( );
277 this->SetImageSlice( NULL );