1 #include <cpExtensions/Interaction/ImageSlicePointPlacer.h>
2 #include <cpExtensions/Visualization/ImageSliceMapper.h>
3 #include <vtkBoundedPlanePointPlacer.h>
4 #include <vtkImageData.h>
5 #include <vtkImageMapper3D.h>
6 #include <vtkImageSlice.h>
9 // -------------------------------------------------------------------------
11 cpExtensions::Interaction::ImageSlicePointPlacer, ImageSlice, vtkImageSlice
14 // -------------------------------------------------------------------------
15 cpExtensions::Interaction::ImageSlicePointPlacer::
16 Self* cpExtensions::Interaction::ImageSlicePointPlacer::
19 return( new Self( ) );
22 // -------------------------------------------------------------------------
23 int cpExtensions::Interaction::ImageSlicePointPlacer::
26 double displayPos[ 2 ],
28 double worldOrient[ 9 ]
31 if( !this->UpdateInternalState( ) )
34 this->Placer->ComputeWorldPosition(
35 ren, displayPos, worldPos, worldOrient
40 // -------------------------------------------------------------------------
41 int cpExtensions::Interaction::ImageSlicePointPlacer::
44 double displayPos[ 2 ],
45 double refWorldPos[ 2 ],
47 double worldOrient[ 9 ]
50 if( !this->UpdateInternalState( ) )
53 this->Placer->ComputeWorldPosition(
54 ren, displayPos, refWorldPos, worldPos, worldOrient
59 // -------------------------------------------------------------------------
60 int cpExtensions::Interaction::ImageSlicePointPlacer::
61 ValidateWorldPosition( double worldPos[ 3 ] )
63 if( !this->UpdateInternalState( ) )
65 return( this->Placer->ValidateWorldPosition( worldPos ) );
68 // -------------------------------------------------------------------------
69 int cpExtensions::Interaction::ImageSlicePointPlacer::
70 ValidateWorldPosition( double worldPos[ 3 ], double worldOrient[ 9 ] )
72 if( !this->UpdateInternalState( ) )
74 return( this->Placer->ValidateWorldPosition( worldPos, worldOrient ) );
77 // -------------------------------------------------------------------------
78 int cpExtensions::Interaction::ImageSlicePointPlacer::
80 vtkRenderer* ren, double worldPos[ 3 ], double worldOrient[ 9 ]
83 if( !this->UpdateInternalState( ) )
85 return( this->Placer->UpdateWorldPosition( ren, worldPos, worldOrient ) );
88 // -------------------------------------------------------------------------
89 int cpExtensions::Interaction::ImageSlicePointPlacer::
90 UpdateInternalState( )
92 if( !this->ImageSlice )
95 vtkImageData* input = this->ImageSlice->GetMapper( )->GetInput( );
100 input->GetSpacing( spacing );
103 input->GetOrigin( origin );
106 this->ImageSlice->GetBounds( b );
107 if( this->Bounds[ 0 ] != VTK_DOUBLE_MAX)
109 b[ 0 ] = ( b[ 0 ] < this->Bounds[ 0 ] )? this->Bounds[ 0 ] : b[ 0 ];
110 b[ 1 ] = ( b[ 1 ] > this->Bounds[ 1 ] )? this->Bounds[ 1 ] : b[ 1 ];
111 b[ 2 ] = ( b[ 2 ] < this->Bounds[ 2 ] )? this->Bounds[ 2 ] : b[ 2 ];
112 b[ 3 ] = ( b[ 3 ] > this->Bounds[ 3 ] )? this->Bounds[ 3 ] : b[ 3 ];
113 b[ 4 ] = ( b[ 4 ] < this->Bounds[ 4 ] )? this->Bounds[ 4 ] : b[ 4 ];
114 b[ 5 ] = ( b[ 5 ] > this->Bounds[ 5 ] )? this->Bounds[ 5 ] : b[ 5 ];
118 int displayExtent[ 6 ] = { 0, 1, 0, 1, 0, 1 };
119 input->GetExtent( displayExtent );
121 dynamic_cast< cpExtensions::Visualization::ImageSliceMapper* >(
122 this->ImageSlice->GetMapper( )
126 auto ori = mapper->GetOrientation( );
127 displayExtent[ ( ori << 1 ) + 1 ] =
128 displayExtent[ ori << 1 ] =
129 mapper->GetSliceNumber( );
135 if( displayExtent[ 0 ] == displayExtent[ 1 ] )
137 axis = vtkBoundedPlanePointPlacer::XAxis;
138 position = origin[ 0 ] + displayExtent[ 0 ] * spacing[ 0 ];
140 else if( displayExtent[ 2 ] == displayExtent[ 3 ] )
142 axis = vtkBoundedPlanePointPlacer::YAxis;
143 position = origin[ 1 ] + displayExtent[ 2 ] * spacing[ 1 ];
145 else if( displayExtent[ 4 ] == displayExtent[ 5 ] )
147 axis = vtkBoundedPlanePointPlacer::ZAxis;
148 position = origin[ 2 ] + displayExtent[ 4 ] * spacing[ 2 ];
152 vtkErrorMacro( "Incorrect display extent in Image Slice" );
158 axis != this->Placer->GetProjectionNormal( ) ||
159 position != this->Placer->GetProjectionPosition( ) ||
160 b[ 0 ] != this->SavedBounds[ 0 ] ||
161 b[ 1 ] != this->SavedBounds[ 1 ] ||
162 b[ 2 ] != this->SavedBounds[ 2 ] ||
163 b[ 3 ] != this->SavedBounds[ 3 ] ||
164 b[ 4 ] != this->SavedBounds[ 4 ] ||
165 b[ 5 ] != this->SavedBounds[ 5 ]
168 this->SavedBounds[ 0 ] = b[ 0 ];
169 this->SavedBounds[ 1 ] = b[ 1 ];
170 this->SavedBounds[ 2 ] = b[ 2 ];
171 this->SavedBounds[ 3 ] = b[ 3 ];
172 this->SavedBounds[ 4 ] = b[ 4 ];
173 this->SavedBounds[ 5 ] = b[ 5 ];
175 this->Placer->SetProjectionNormal( axis );
176 this->Placer->SetProjectionPosition( position );
178 this->Placer->RemoveAllBoundingPlanes( );
181 if( axis != vtkBoundedPlanePointPlacer::XAxis )
183 plane = vtkPlane::New( );
184 plane->SetOrigin( b[ 0 ], b[ 2 ], b[ 4 ] );
185 plane->SetNormal( 1.0, 0.0, 0.0 );
186 this->Placer->AddBoundingPlane( plane );
189 plane = vtkPlane::New();
190 plane->SetOrigin( b[ 1 ], b[ 3 ], b[ 5 ] );
191 plane->SetNormal( -1.0, 0.0, 0.0 );
192 this->Placer->AddBoundingPlane( plane );
197 if( axis != vtkBoundedPlanePointPlacer::YAxis )
199 plane = vtkPlane::New( );
200 plane->SetOrigin( b[ 0 ], b[ 2 ], b[ 4 ] );
201 plane->SetNormal( 0.0, 1.0, 0.0 );
202 this->Placer->AddBoundingPlane( plane );
205 plane = vtkPlane::New( );
206 plane->SetOrigin( b[ 1 ], b[ 3 ], b[ 5 ] );
207 plane->SetNormal( 0.0, -1.0, 0.0 );
208 this->Placer->AddBoundingPlane( plane );
213 if( axis != vtkBoundedPlanePointPlacer::ZAxis )
215 plane = vtkPlane::New( );
216 plane->SetOrigin( b[ 0 ], b[ 2 ], b[ 4 ] );
217 plane->SetNormal( 0.0, 0.0, 1.0 );
218 this->Placer->AddBoundingPlane( plane );
221 plane = vtkPlane::New( );
222 plane->SetOrigin( b[ 1 ], b[ 3 ], b[ 5 ] );
223 plane->SetNormal( 0.0, 0.0, -1.0 );
224 this->Placer->AddBoundingPlane( plane );
235 // -------------------------------------------------------------------------
236 void cpExtensions::Interaction::ImageSlicePointPlacer::
237 SetWorldTolerance( double tol )
240 ( tol < 0.0 )? 0.0: ( ( tol > VTK_DOUBLE_MAX )? VTK_DOUBLE_MAX: tol );
241 if( this->WorldTolerance != t )
243 this->WorldTolerance = t;
244 this->Placer->SetWorldTolerance( tol );
250 // -------------------------------------------------------------------------
251 cpExtensions::Interaction::ImageSlicePointPlacer::
252 ImageSlicePointPlacer( )
255 this->Placer = vtkBoundedPlanePointPlacer::New( );
256 this->ImageSlice = NULL;
257 this->SavedBounds[ 0 ] = 0.0;
258 this->SavedBounds[ 1 ] = 0.0;
259 this->SavedBounds[ 2 ] = 0.0;
260 this->SavedBounds[ 3 ] = 0.0;
261 this->SavedBounds[ 4 ] = 0.0;
262 this->SavedBounds[ 5 ] = 0.0;
263 this->Bounds[ 0 ] = this->Bounds[ 2 ] = this->Bounds[ 4 ] = VTK_DOUBLE_MAX;
264 this->Bounds[ 1 ] = this->Bounds[ 3 ] = this->Bounds[ 5 ] = VTK_DOUBLE_MIN;
267 // -------------------------------------------------------------------------
268 cpExtensions::Interaction::ImageSlicePointPlacer::
269 ~ImageSlicePointPlacer( )
271 this->Placer->Delete( );
272 this->SetImageSlice( NULL );