1 #include <cpExtensions/Visualization/ImageSliceActors.h>
6 #include <vtkAlgorithmOutput.h>
8 #include <vtkCellArray.h>
9 #include <vtkImageData.h>
10 #include <vtkImageProperty.h>
11 #include <vtkPoints.h>
12 #include <vtkProperty.h>
13 #include <vtkRenderer.h>
14 #include <vtkRendererCollection.h>
15 #include <vtkRenderWindow.h>
16 #include <vtkRenderWindowInteractor.h>
17 #include <vtkTextProperty.h>
19 // -------------------------------------------------------------------------
20 double cpExtensions::Visualization::ImageSliceActors::
21 m_PlaneColors[ 3 ][ 3 ] =
28 // -------------------------------------------------------------------------
29 cpExtensions::Visualization::ImageSliceActors*
30 cpExtensions::Visualization::ImageSliceActors::
33 return( new Self( ) );
36 // -------------------------------------------------------------------------
37 void cpExtensions::Visualization::ImageSliceActors::
40 this->m_ImageMapper->SetOrientation( axis );
41 this->m_BlenderMapper->SetOrientation( axis );
42 this->m_ImageMapper->Update( );
43 this->m_BlenderMapper->Update( );
44 this->SetSliceNumber( this->GetSliceNumberMinValue( ) );
45 this->m_ImageActor->Modified( );
46 this->m_BlenderActor->Modified( );
50 // -------------------------------------------------------------------------
51 void cpExtensions::Visualization::ImageSliceActors::
52 SetInputConnection( vtkAlgorithmOutput* aout )
54 // Get input vtkImageData
58 // Create mapper and actors
59 this->m_ImageMapper->SetInputConnection( aout );
60 this->_ConfigureInputImage( );
63 // -------------------------------------------------------------------------
64 void cpExtensions::Visualization::ImageSliceActors::
65 SetInputImage( vtkImageData* data )
67 // Get input vtkImageData
71 // Create mapper and actors
72 this->m_ImageMapper->SetInputData( data );
73 this->_ConfigureInputImage( );
76 // -------------------------------------------------------------------------
77 int cpExtensions::Visualization::ImageSliceActors::
79 vtkAlgorithmOutput* aout,
80 const double& r, const double& g, const double& b
85 this->m_Blender->AddInputConnection( aout );
86 this->_ConfigureBinaryImage( r, g, b );
87 return( this->m_Blender->GetNumberOfInputs( ) - 1 );
90 // -------------------------------------------------------------------------
91 int cpExtensions::Visualization::ImageSliceActors::
94 const double& r, const double& g, const double& b
99 this->m_Blender->AddInputData( data );
100 this->_ConfigureBinaryImage( r, g, b );
101 return( this->m_Blender->GetNumberOfInputs( ) - 1 );
104 // -------------------------------------------------------------------------
105 void cpExtensions::Visualization::ImageSliceActors::
108 // Unbind from container
109 this->RemoveAllItems( );
111 // Filters and mappers
112 this->m_ImageMapper = vtkSmartPointer< vtkImageSliceMapper >::New( );
113 this->m_ImageActor = vtkSmartPointer< vtkImageActor >::New( );
114 this->m_Blender = vtkSmartPointer< TBlender >::New( );
115 this->m_BlenderMapper = vtkSmartPointer< vtkImageSliceMapper >::New( );
116 this->m_BlenderLUT = vtkSmartPointer< vtkLookupTable >::New( );
117 this->m_BlenderActor = vtkSmartPointer< vtkImageActor >::New( );
119 this->m_ImageActor->SetMapper( this->m_ImageMapper );
120 this->m_BlenderMapper->
121 SetInputConnection( this->m_Blender->GetOutputPort( ) );
123 this->m_BlenderLUT->SetNumberOfTableValues( 1 );
124 this->m_BlenderLUT->SetTableRange( 0, 1 );
125 this->m_BlenderLUT->SetTableValue( 0, 0, 0, 0, 0 );
126 this->m_BlenderLUT->Build( );
128 this->m_BlenderActor->SetMapper( this->m_BlenderMapper );
129 this->m_BlenderActor->GetProperty( )->SetLookupTable( this->m_BlenderLUT );
130 this->m_BlenderActor->GetProperty( )->UseLookupTableScalarRangeOn( );
133 vtkSmartPointer< vtkPoints > cursor_points =
134 vtkSmartPointer< vtkPoints >::New( );
135 vtkSmartPointer< vtkCellArray > cursor_lines =
136 vtkSmartPointer< vtkCellArray >::New( );
137 cursor_points->InsertNextPoint( 0, 0, 0 );
138 cursor_points->InsertNextPoint( 0, 0, 0 );
139 cursor_points->InsertNextPoint( 0, 0, 0 );
140 cursor_points->InsertNextPoint( 0, 0, 0 );
141 cursor_lines->InsertNextCell( 2 );
142 cursor_lines->InsertCellPoint( 0 );
143 cursor_lines->InsertCellPoint( 1 );
144 cursor_lines->InsertNextCell( 2 );
145 cursor_lines->InsertCellPoint( 2 );
146 cursor_lines->InsertCellPoint( 3 );
148 this->m_Cursor = vtkSmartPointer< vtkPolyData >::New( );
149 this->m_CursorMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
150 this->m_CursorActor = vtkSmartPointer< vtkActor >::New( );
152 this->m_Cursor->SetPoints( cursor_points );
153 this->m_Cursor->SetLines( cursor_lines );
154 this->m_CursorMapper->SetInputData( this->m_Cursor );
155 this->m_CursorActor->SetMapper( this->m_CursorMapper );
156 this->m_CursorActor->GetProperty( )->SetColor( 1, 1, 0 );
157 this->m_CursorActor->GetProperty( )->SetLineWidth( 2 );
159 // Plane intersections
160 this->m_Axis1 = vtkSmartPointer< vtkPolyData >::New( );
161 this->m_Axis1Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
162 this->m_Axis1Actor = vtkSmartPointer< vtkActor >::New( );
164 vtkSmartPointer< vtkPoints > h_points =
165 vtkSmartPointer< vtkPoints >::New( );
166 vtkSmartPointer< vtkCellArray > h_lines =
167 vtkSmartPointer< vtkCellArray >::New( );
168 h_points->InsertNextPoint( 0, 0, 0 );
169 h_points->InsertNextPoint( 0, 0, 0 );
170 h_lines->InsertNextCell( 2 );
171 h_lines->InsertCellPoint( 0 );
172 h_lines->InsertCellPoint( 1 );
173 this->m_Axis1->SetPoints( h_points );
174 this->m_Axis1->SetLines( h_lines );
175 this->m_Axis1Mapper->SetInputData( this->m_Axis1 );
176 this->m_Axis1Actor->SetMapper( this->m_Axis1Mapper );
178 this->m_Axis2 = vtkSmartPointer< vtkPolyData >::New( );
179 this->m_Axis2Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
180 this->m_Axis2Actor = vtkSmartPointer< vtkActor >::New( );
182 vtkSmartPointer< vtkPoints > v_points =
183 vtkSmartPointer< vtkPoints >::New( );
184 vtkSmartPointer< vtkCellArray > v_lines =
185 vtkSmartPointer< vtkCellArray >::New( );
186 v_points->InsertNextPoint( 0, 0, 0 );
187 v_points->InsertNextPoint( 0, 0, 0 );
188 v_lines->InsertNextCell( 2 );
189 v_lines->InsertCellPoint( 0 );
190 v_lines->InsertCellPoint( 1 );
191 this->m_Axis2->SetPoints( v_points );
192 this->m_Axis2->SetLines( v_lines );
193 this->m_Axis2Mapper->SetInputData( this->m_Axis2 );
194 this->m_Axis2Actor->SetMapper( this->m_Axis2Mapper );
197 this->m_Plane = vtkSmartPointer< vtkPolyData >::New( );
198 this->m_PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
199 this->m_PlaneActor = vtkSmartPointer< vtkActor >::New( );
201 vtkSmartPointer< vtkPoints > plane_points =
202 vtkSmartPointer< vtkPoints >::New( );
203 vtkSmartPointer< vtkCellArray > plane_lines =
204 vtkSmartPointer< vtkCellArray >::New( );
205 plane_points->InsertNextPoint( 0, 0, 0 );
206 plane_points->InsertNextPoint( 0, 1, 0 );
207 plane_points->InsertNextPoint( 1, 1, 0 );
208 plane_points->InsertNextPoint( 1, 0, 0 );
209 plane_lines->InsertNextCell( 5 );
210 plane_lines->InsertCellPoint( 0 );
211 plane_lines->InsertCellPoint( 1 );
212 plane_lines->InsertCellPoint( 2 );
213 plane_lines->InsertCellPoint( 3 );
214 plane_lines->InsertCellPoint( 0 );
215 this->m_Plane->SetPoints( plane_points );
216 this->m_Plane->SetLines( plane_lines );
218 this->m_PlaneMapper->SetInputData( this->m_Plane );
219 this->m_PlaneActor->SetMapper( this->m_PlaneMapper );
222 this->m_TextActor = vtkSmartPointer< vtkTextActor >::New( );
223 this->m_TextBuffer[ 0 ] = '\0';
224 this->m_TextActor->SetTextScaleModeToNone( );
225 vtkTextProperty* textprop = this->m_TextActor->GetTextProperty( );
226 textprop->SetColor( 1, 1, 0 );
227 textprop->SetFontFamilyToCourier( );
228 textprop->SetFontSize( 18 );
229 textprop->BoldOff( );
230 textprop->ItalicOff( );
231 textprop->ShadowOff( );
232 textprop->SetJustificationToLeft( );
233 textprop->SetVerticalJustificationToBottom( );
234 vtkCoordinate* coord = this->m_TextActor->GetPositionCoordinate( );
235 coord->SetCoordinateSystemToNormalizedViewport( );
236 coord->SetValue( 0.01, 0.01 );
239 // -------------------------------------------------------------------------
240 void cpExtensions::Visualization::ImageSliceActors::
241 AssociateSlice( Self* slice )
243 this->m_AssociatedSlices.push_back( slice );
247 // -------------------------------------------------------------------------
248 vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
251 return( this->m_Style.GetPointer( ) );
254 // -------------------------------------------------------------------------
255 const vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
258 return( this->m_Style.GetPointer( ) );
261 // -------------------------------------------------------------------------
262 vtkImageData* cpExtensions::Visualization::ImageSliceActors::
265 if( this->m_ImageMapper.GetPointer( ) != NULL )
266 return( this->m_ImageMapper->GetInput( ) );
271 // -------------------------------------------------------------------------
272 const vtkImageData* cpExtensions::Visualization::ImageSliceActors::
273 GetInputImage( ) const
275 if( this->m_ImageMapper.GetPointer( ) != NULL )
276 return( this->m_ImageMapper->GetInput( ) );
281 // -------------------------------------------------------------------------
282 void cpExtensions::Visualization::ImageSliceActors::
283 PushActorsInto( vtkRenderWindow* window, bool force_style )
285 this->m_Window = window;
288 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
289 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
290 if( rwi == NULL || renderer == NULL )
294 if( this->m_Style.GetPointer( ) != NULL && force_style )
295 rwi->SetInteractorStyle( this->m_Style );
298 renderer->AddViewProp( this->m_ImageActor );
299 renderer->AddViewProp( this->m_BlenderActor );
302 renderer->AddViewProp( this->m_CursorActor );
303 renderer->AddViewProp( this->m_PlaneActor );
304 renderer->AddViewProp( this->m_TextActor );
305 renderer->AddViewProp( this->m_Axis1Actor );
306 renderer->AddViewProp( this->m_Axis2Actor );
311 vtkCamera* camera = renderer->GetActiveCamera( );
312 if( camera != NULL && force_style )
314 // Parallel projections are better when displaying 2D images
315 int axis = this->GetAxis( );
316 camera->ParallelProjectionOn( );
317 camera->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
320 camera->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
321 camera->SetViewUp ( double( 0 ), double( 0 ), double( 1 ) );
325 camera->SetPosition( double( 0 ), double( -1 ), double( 0 ) );
326 camera->SetViewUp ( double( 0 ), double( 0 ), double( 1 ) );
328 else // if( axis == 2 )
330 camera->SetPosition( double( 0 ), double( 0 ), double( -1 ) );
331 camera->SetViewUp ( double( 0 ), double( -1 ), double( 0 ) );
336 renderer->ResetCamera( );
339 // -------------------------------------------------------------------------
340 void cpExtensions::Visualization::ImageSliceActors::
341 PopActorsFrom( vtkRenderWindow* window )
343 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
344 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
345 if( renderer != NULL )
347 renderer->RemoveViewProp( this->m_ImageActor );
348 renderer->RemoveViewProp( this->m_BlenderActor );
349 renderer->RemoveViewProp( this->m_CursorActor );
350 renderer->RemoveViewProp( this->m_PlaneActor );
351 renderer->RemoveViewProp( this->m_TextActor );
352 renderer->RemoveViewProp( this->m_Axis1Actor );
353 renderer->RemoveViewProp( this->m_Axis2Actor );
358 // -------------------------------------------------------------------------
359 unsigned int cpExtensions::Visualization::ImageSliceActors::
360 GetNumberOfImages( ) const
362 return( this->m_Blender->GetNumberOfInputs( ) );
365 // -------------------------------------------------------------------------
366 vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
369 return( this->m_ImageActor );
372 // -------------------------------------------------------------------------
373 const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
374 GetImageActor( ) const
376 return( this->m_ImageActor );
379 // -------------------------------------------------------------------------
380 vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
383 return( this->m_BlenderActor );
386 // -------------------------------------------------------------------------
387 const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
388 GetBinaryActor( ) const
390 return( this->m_BlenderActor );
393 // -------------------------------------------------------------------------
394 vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
397 return( this->m_TextActor );
400 // -------------------------------------------------------------------------
401 const vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
402 GetTextActor( ) const
404 return( this->m_TextActor );
407 // -------------------------------------------------------------------------
408 vtkActor* cpExtensions::Visualization::ImageSliceActors::
411 return( this->m_PlaneActor );
414 // -------------------------------------------------------------------------
415 const vtkActor* cpExtensions::Visualization::ImageSliceActors::
416 GetPlaneActor( ) const
418 return( this->m_PlaneActor );
421 // -------------------------------------------------------------------------
422 vtkPlane* cpExtensions::Visualization::ImageSliceActors::
425 if( this->m_ImageMapper.GetPointer( ) != NULL )
426 return( this->m_ImageMapper->GetSlicePlane( ) );
431 // -------------------------------------------------------------------------
432 const vtkPlane* cpExtensions::Visualization::ImageSliceActors::
433 GetPlaneFunction( ) const
435 if( this->m_ImageMapper.GetPointer( ) != NULL )
436 return( this->m_ImageMapper->GetSlicePlane( ) );
441 // -------------------------------------------------------------------------
442 void cpExtensions::Visualization::ImageSliceActors::
443 SetInterpolate( bool v )
445 if( this->m_Interpolate != v )
447 this->m_ImageActor->SetInterpolate( v );
448 this->m_BlenderActor->SetInterpolate( v );
449 this->m_Interpolate = v;
455 // -------------------------------------------------------------------------
456 void cpExtensions::Visualization::ImageSliceActors::
459 this->SetInterpolate( true );
462 // -------------------------------------------------------------------------
463 void cpExtensions::Visualization::ImageSliceActors::
466 this->SetInterpolate( false );
469 // -------------------------------------------------------------------------
470 double* cpExtensions::Visualization::ImageSliceActors::
471 GetDisplayBounds( ) const
473 if( this->m_ImageActor.GetPointer( ) != NULL )
474 return( this->m_ImageActor->GetDisplayBounds( ) );
479 // -------------------------------------------------------------------------
480 void cpExtensions::Visualization::ImageSliceActors::
481 GetDisplayBounds( double bounds[ 6 ] ) const
483 if( this->m_ImageActor.GetPointer( ) == NULL )
485 bounds[ 0 ] = bounds[ 2 ] = bounds[ 4 ] = double( 0 );
486 bounds[ 1 ] = bounds[ 3 ] = bounds[ 5 ] = double( -1 );
489 this->m_ImageActor->GetDisplayBounds( bounds );
492 // -------------------------------------------------------------------------
493 void cpExtensions::Visualization::ImageSliceActors::
496 if( this->m_ImageMapper.GetPointer( ) != NULL )
500 this->m_VisibleBounds[ 0 ],
501 this->m_VisibleBounds[ 2 ],
502 this->m_VisibleBounds[ 4 ]
504 this->SetCursor( pos );
508 vtkPoints* points = this->m_Cursor->GetPoints( );
509 points->SetPoint( 0, 0, 0, 0 );
510 points->SetPoint( 1, 0, 0, 0 );
511 points->SetPoint( 2, 0, 0, 0 );
512 points->SetPoint( 3, 0, 0, 0 );
513 this->m_Cursor->Modified( );
514 this->m_CursorMapper->Modified( );
515 this->m_CursorActor->Modified( );
520 // -------------------------------------------------------------------------
521 void cpExtensions::Visualization::ImageSliceActors::
522 SetCursor( double pos[ 3 ] )
524 if( this->m_ImageMapper.GetPointer( ) == NULL )
528 int a0 = this->GetAxis( );
529 int a1 = ( a0 + 1 ) % 3;
530 int a2 = ( a0 + 2 ) % 3;
536 double* bounds = this->m_VisibleBounds;
537 double p0[ 3 ], p1[ 3 ], p2[ 3 ], p3[ 3 ];
539 p0[ a2 ] = p1[ a2 ] = pos[ a2 ];
540 p2[ a1 ] = p3[ a1 ] = pos[ a1 ];
541 p0[ a0 ] = p1[ a0 ] = p2[ a0 ] = p3[ a0 ] = bounds[ ma0 ];
542 p0[ a1 ] = bounds[ ma1 ];
543 p1[ a1 ] = bounds[ ma1 + 1 ];
544 p2[ a2 ] = bounds[ ma2 ];
545 p3[ a2 ] = bounds[ ma2 + 1 ];
547 vtkPoints* points = this->m_Cursor->GetPoints( );
548 points->SetPoint( 0, p0 );
549 points->SetPoint( 1, p1 );
550 points->SetPoint( 2, p2 );
551 points->SetPoint( 3, p3 );
552 this->m_Cursor->Modified( );
553 this->m_CursorMapper->Modified( );
554 this->m_CursorActor->Modified( );
557 // -------------------------------------------------------------------------
558 void cpExtensions::Visualization::ImageSliceActors::
561 if( this->m_ImageMapper.GetPointer( ) != NULL )
565 this->m_VisibleBounds[ 0 ],
566 this->m_VisibleBounds[ 2 ],
567 this->m_VisibleBounds[ 4 ]
569 this->SetAxesCursor( pos );
573 vtkPoints* points = this->m_Axis1->GetPoints( );
574 points->SetPoint( 0, 0, 0, 0 );
575 points->SetPoint( 1, 0, 0, 0 );
576 this->m_Axis1->Modified( );
577 this->m_Axis1Mapper->Modified( );
578 this->m_Axis1Actor->Modified( );
580 points = this->m_Axis2->GetPoints( );
581 points->SetPoint( 0, 0, 0, 0 );
582 points->SetPoint( 1, 0, 0, 0 );
583 this->m_Axis2->Modified( );
584 this->m_Axis2Mapper->Modified( );
585 this->m_Axis2Actor->Modified( );
590 // -------------------------------------------------------------------------
591 void cpExtensions::Visualization::ImageSliceActors::
592 SetAxesCursor( double pos[ 3 ] )
594 if( this->m_ImageMapper.GetPointer( ) == NULL )
598 int a0 = this->GetAxis( );
599 int a1 = ( a0 + 1 ) % 3;
600 int a2 = ( a0 + 2 ) % 3;
606 double* bounds = this->m_VisibleBounds;
607 double p0[ 3 ], p1[ 3 ], p2[ 3 ], p3[ 3 ];
609 p0[ a2 ] = p1[ a2 ] = pos[ a2 ];
610 p2[ a1 ] = p3[ a1 ] = pos[ a1 ];
611 p0[ a0 ] = p1[ a0 ] = p2[ a0 ] = p3[ a0 ] = bounds[ ma0 ];
612 p0[ a1 ] = bounds[ ma1 ];
613 p1[ a1 ] = bounds[ ma1 + 1 ];
614 p2[ a2 ] = bounds[ ma2 ];
615 p3[ a2 ] = bounds[ ma2 + 1 ];
617 vtkPoints* points1 = this->m_Axis1->GetPoints( );
618 points1->SetPoint( 0, p2 );
619 points1->SetPoint( 1, p3 );
621 vtkPoints* points2 = this->m_Axis2->GetPoints( );
622 points2->SetPoint( 0, p0 );
623 points2->SetPoint( 1, p1 );
625 this->m_Axis1->Modified( );
626 this->m_Axis1Mapper->Modified( );
627 this->m_Axis1Actor->Modified( );
629 this->m_Axis2->Modified( );
630 this->m_Axis2Mapper->Modified( );
631 this->m_Axis2Actor->Modified( );
634 // -------------------------------------------------------------------------
635 double cpExtensions::Visualization::ImageSliceActors::
636 GetMinWindow( ) const
638 return( this->m_WLRange[ 0 ] );
641 // -------------------------------------------------------------------------
642 double cpExtensions::Visualization::ImageSliceActors::
643 GetMaxWindow( ) const
645 return( this->m_WLRange[ 1 ] );
648 // -------------------------------------------------------------------------
649 double cpExtensions::Visualization::ImageSliceActors::
652 return( this->m_WLRange[ 2 ] );
655 // -------------------------------------------------------------------------
656 double cpExtensions::Visualization::ImageSliceActors::
659 return( this->m_WLRange[ 3 ] );
662 // -------------------------------------------------------------------------
663 double cpExtensions::Visualization::ImageSliceActors::
666 if( this->m_ImageActor.GetPointer( ) != NULL )
667 return( this->m_ImageActor->GetProperty( )->GetColorWindow( ) );
669 return( double( 0 ) );
672 // -------------------------------------------------------------------------
673 double cpExtensions::Visualization::ImageSliceActors::
676 if( this->m_ImageActor.GetPointer( ) != NULL )
677 return( this->m_ImageActor->GetProperty( )->GetColorLevel( ) );
679 return( double( 0 ) );
682 // -------------------------------------------------------------------------
683 void cpExtensions::Visualization::ImageSliceActors::
684 SetWindow( double w )
686 if( this->m_ImageActor.GetPointer( ) == NULL )
688 double v = ( w < this->m_WLRange[ 0 ] )? this->m_WLRange[ 0 ]: w;
689 v = ( v > this->m_WLRange[ 1 ] )? this->m_WLRange[ 1 ]: v;
690 this->m_ImageActor->GetProperty( )->SetColorWindow( v );
693 // -------------------------------------------------------------------------
694 void cpExtensions::Visualization::ImageSliceActors::
697 if( this->m_ImageActor.GetPointer( ) == NULL )
699 double v = ( l < this->m_WLRange[ 2 ] )? this->m_WLRange[ 2 ]: l;
700 v = ( v > this->m_WLRange[ 3 ] )? this->m_WLRange[ 3 ]: v;
701 this->m_ImageActor->GetProperty( )->SetColorLevel( v );
704 // -------------------------------------------------------------------------
705 void cpExtensions::Visualization::ImageSliceActors::
706 SetWindowLevel( double w, double l )
708 if( this->m_ImageActor.GetPointer( ) == NULL )
710 double a = ( w < this->m_WLRange[ 0 ] )? this->m_WLRange[ 0 ]: w;
711 a = ( a > this->m_WLRange[ 1 ] )? this->m_WLRange[ 1 ]: a;
712 double b = ( l < this->m_WLRange[ 2 ] )? this->m_WLRange[ 2 ]: l;
713 b = ( b > this->m_WLRange[ 3 ] )? this->m_WLRange[ 3 ]: b;
714 this->m_ImageActor->GetProperty( )->SetColorWindow( a );
715 this->m_ImageActor->GetProperty( )->SetColorLevel( b );
716 this->UpdateText( a, b );
719 // -------------------------------------------------------------------------
720 void cpExtensions::Visualization::ImageSliceActors::
723 this->SetWindowLevel(
724 this->m_WLRange[ 1 ] * double( 0.5 ),
725 ( this->m_WLRange[ 3 ] + this->m_WLRange[ 2 ] ) * double( 0.5 )
729 // -------------------------------------------------------------------------
730 int cpExtensions::Visualization::ImageSliceActors::
733 if( this->m_ImageMapper.GetPointer( ) != NULL )
734 return( this->m_ImageMapper->GetOrientation( ) );
739 // -------------------------------------------------------------------------
740 int cpExtensions::Visualization::ImageSliceActors::
741 GetSliceNumber( ) const
743 if( this->m_ImageMapper.GetPointer( ) != NULL )
744 return( this->m_ImageMapper->GetSliceNumber( ) );
749 // -------------------------------------------------------------------------
750 int cpExtensions::Visualization::ImageSliceActors::
751 GetSliceNumberMinValue( ) const
753 if( this->m_ImageMapper.GetPointer( ) != NULL )
754 return( this->m_ImageMapper->GetSliceNumberMinValue( ) );
759 // -------------------------------------------------------------------------
760 int cpExtensions::Visualization::ImageSliceActors::
761 GetSliceNumberMaxValue( ) const
763 if( this->m_ImageMapper.GetPointer( ) != NULL )
764 return( this->m_ImageMapper->GetSliceNumberMaxValue( ) );
769 // -------------------------------------------------------------------------
770 void cpExtensions::Visualization::ImageSliceActors::
771 SetSliceNumber( const int& slice )
773 if( this->m_ImageMapper.GetPointer( ) == NULL )
776 int axis = this->GetAxis( );
777 double prev_pos = this->m_VisibleBounds[ axis << 1 ];
779 // Update mappers and display bounds
780 this->m_ImageMapper->SetSliceNumber( slice );
781 this->m_BlenderMapper->SetSliceNumber( slice );
782 this->m_ImageMapper->Modified( );
783 this->m_BlenderMapper->Modified( );
784 this->m_ImageActor->Modified( );
785 this->m_BlenderActor->Modified( );
787 // Update display extent (this isn't done automatically)
788 this->m_ImageMapper->GetInput( )->GetExtent( this->m_VisibleExtent );
789 this->m_VisibleExtent[ axis << 1 ] = slice;
790 this->m_VisibleExtent[ ( axis << 1 ) + 1 ] = slice;
791 this->m_ImageActor->SetDisplayExtent( this->m_VisibleExtent );
793 // Prepare plane data
794 this->m_ImageMapper->GetBounds( this->m_VisibleBounds );
798 this->m_VisibleBounds[ 0 ],
799 this->m_VisibleBounds[ 2 ],
800 this->m_VisibleBounds[ 4 ]
803 this->m_VisibleBounds[ 1 ],
804 this->m_VisibleBounds[ 3 ],
805 this->m_VisibleBounds[ 5 ]
810 vtkPlane* plane = this->m_ImageMapper->GetSlicePlane( );
811 plane->GeneralizedProjectPoint( x0[ 0 ], p0[ 0 ] );
812 plane->GeneralizedProjectPoint( x0[ 1 ], p0[ 1 ] );
814 this->m_VisibleBounds[ 0 ] = p0[ 0 ][ 0 ];
815 this->m_VisibleBounds[ 1 ] = p0[ 1 ][ 0 ];
816 this->m_VisibleBounds[ 2 ] = p0[ 0 ][ 1 ];
817 this->m_VisibleBounds[ 3 ] = p0[ 1 ][ 1 ];
818 this->m_VisibleBounds[ 4 ] = p0[ 0 ][ 2 ];
819 this->m_VisibleBounds[ 5 ] = p0[ 1 ][ 2 ];
820 double* bnds = this->m_VisibleBounds;
822 // Configure visualization and implicit plane orientation
823 this->m_PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
824 this->m_PlaneActor->GetProperty( )->SetLineWidth( 3 );
825 vtkPoints* plane_points = this->m_Plane->GetPoints( );
826 plane_points->SetPoint( 0, bnds[ 0 ], bnds[ 2 ], bnds[ 4 ] );
827 plane_points->SetPoint( 2, bnds[ 1 ], bnds[ 3 ], bnds[ 5 ] );
828 if( axis == 0 || axis == 2 ) // YZ, x-normal
830 plane_points->SetPoint( 1, bnds[ 1 ], bnds[ 2 ], bnds[ 5 ] );
831 plane_points->SetPoint( 3, bnds[ 0 ], bnds[ 3 ], bnds[ 4 ] );
833 else if( axis == 1 ) // ZX, y-normal
835 plane_points->SetPoint( 1, bnds[ 0 ], bnds[ 2 ], bnds[ 5 ] );
836 plane_points->SetPoint( 3, bnds[ 1 ], bnds[ 3 ], bnds[ 4 ] );
841 this->m_PlaneActor->GetProperty( )->
842 SetColor( this->m_PlaneColors[ axis ] );
843 this->m_Axis1Actor->GetProperty( )->
844 SetColor( this->m_PlaneColors[ ( axis + 1 ) % 3 ] );
845 this->m_Axis2Actor->GetProperty( )->
846 SetColor( this->m_PlaneColors[ ( axis + 2 ) % 3 ] );
848 this->m_Plane->Modified( );
849 this->m_PlaneMapper->Modified( );
850 this->m_PlaneActor->Modified( );
852 this->m_Axis1->Modified( );
853 this->m_Axis1Mapper->Modified( );
854 this->m_Axis1Actor->Modified( );
856 this->m_Axis2->Modified( );
857 this->m_Axis2Mapper->Modified( );
858 this->m_Axis2Actor->Modified( );
863 // Update camera position
864 if( this->m_Window == NULL )
866 vtkRenderer* renderer =
867 this->m_Window->GetRenderers( )->GetFirstRenderer( );
868 if( renderer == NULL )
870 vtkCamera* camera = renderer->GetActiveCamera( );
874 camera->GetPosition( cam_pos );
875 cam_pos[ axis ] += this->m_VisibleBounds[ axis << 1 ] - prev_pos;
876 camera->SetPosition( cam_pos );
879 // -------------------------------------------------------------------------
880 void cpExtensions::Visualization::ImageSliceActors::
881 SetSlice( double* pos )
883 vtkImageData* image = this->GetInputImage( );
889 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
890 this->SetSliceNumber( ijk[ this->GetAxis( ) ] );
893 // -------------------------------------------------------------------------
894 void cpExtensions::Visualization::ImageSliceActors::
897 if( this->m_ImageMapper.GetPointer( ) != NULL )
900 int axId = this->m_ImageMapper->GetOrientation( );
901 if ( axId == 0 ) axis = 'X';
902 else if( axId == 1 ) axis = 'Y';
903 else if( axId == 2 ) axis = 'Z';
906 this->m_TextBuffer, "Axis: %c (%d)",
907 axis, this->m_ImageMapper->GetSliceNumber( )
911 this->m_TextBuffer[ 0 ] = '\0';
912 this->m_TextActor->SetInput( this->m_TextBuffer );
913 this->m_TextActor->Modified( );
917 // -------------------------------------------------------------------------
918 void cpExtensions::Visualization::ImageSliceActors::
919 UpdateText( double pos[ 3 ] )
921 if( this->m_ImageMapper.GetPointer( ) != NULL )
924 int axId = this->m_ImageMapper->GetOrientation( );
925 if ( axId == 0 ) axis = 'X';
926 else if( axId == 1 ) axis = 'Y';
927 else if( axId == 2 ) axis = 'Z';
928 int slice = this->GetSliceNumber( );
930 vtkImageData* image = this->GetInputImage( );
933 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
937 image->GetExtent( ext );
939 ext[ 0 ] <= ijk[ 0 ] && ijk[ 0 ] <= ext[ 1 ] &&
940 ext[ 2 ] <= ijk[ 1 ] && ijk[ 1 ] <= ext[ 3 ] &&
941 ext[ 4 ] <= ijk[ 2 ] && ijk[ 2 ] <= ext[ 5 ]
944 int nScl = image->GetNumberOfScalarComponents( );
945 std::stringstream str;
949 << "," << ijk[ 2 ] << "]=(";
951 image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
952 for( int n = 1; n < nScl; ++n )
955 << image->GetScalarComponentAsFloat(
956 ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n
960 this->m_TextBuffer, "Axis: %c (%d)\nPixel %s",
961 axis, slice, str.str( ).c_str( )
967 this->m_TextBuffer[ 0 ] = '\0';
968 this->m_TextActor->SetInput( this->m_TextBuffer );
969 this->m_TextActor->Modified( );
973 // -------------------------------------------------------------------------
974 void cpExtensions::Visualization::ImageSliceActors::
975 UpdateText( const double& w, const double& l )
977 if( this->m_ImageMapper.GetPointer( ) != NULL )
980 int axId = this->m_ImageMapper->GetOrientation( );
981 if ( axId == 0 ) axis = 'X';
982 else if( axId == 1 ) axis = 'Y';
983 else if( axId == 2 ) axis = 'Z';
986 this->m_TextBuffer, "Axis: %c (%d)\nW/L (%.2f/%.2f)",
987 axis, this->m_ImageMapper->GetSliceNumber( ), w, l
991 this->m_TextBuffer[ 0 ] = '\0';
992 this->m_TextActor->SetInput( this->m_TextBuffer );
993 this->m_TextActor->Modified( );
997 // -------------------------------------------------------------------------
998 void cpExtensions::Visualization::ImageSliceActors::
1001 if( this->m_Window == NULL )
1003 vtkRenderer* renderer =
1004 this->m_Window->GetRenderers( )->GetFirstRenderer( );
1005 if( renderer == NULL )
1007 renderer->SetAllocatedRenderTime( 1e-3 );
1008 this->m_Window->Render( );
1011 // -------------------------------------------------------------------------
1012 void cpExtensions::Visualization::ImageSliceActors::
1015 if( this->m_Window == NULL )
1017 vtkRenderer* renderer =
1018 this->m_Window->GetRenderers( )->GetFirstRenderer( );
1019 if( renderer == NULL )
1021 renderer->ResetCamera( );
1024 // -------------------------------------------------------------------------
1025 cpExtensions::Visualization::ImageSliceActors::
1029 m_Interpolate( false )
1032 this->_ConfigureStyle( );
1035 // -------------------------------------------------------------------------
1036 cpExtensions::Visualization::ImageSliceActors::
1037 ~ImageSliceActors( )
1041 // -------------------------------------------------------------------------
1042 void cpExtensions::Visualization::ImageSliceActors::
1045 // Connect this view with a controller
1046 this->m_Style = vtkSmartPointer< TStyle >::New( );
1047 this->m_Style->SetAssociatedObject( this );
1048 this->m_Style->AddMouseMoveCommand( Self::_MouseMoveCommand, this );
1049 this->m_Style->AddMouseClickCommand( Self::_MouseClickCommand, this );
1050 this->m_Style->AddMouseWheelCommand( Self::_MouseWheelCommand, this );
1051 this->m_Style->AddKeyCommand( Self::_KeyCommand, this );
1052 this->m_Style->AddEnterCommand( Self::_EnterCommand, this );
1053 this->m_Style->AddLeaveCommand( Self::_LeaveCommand, this );
1056 // -------------------------------------------------------------------------
1057 void cpExtensions::Visualization::ImageSliceActors::
1058 _ConfigureInputImage( )
1060 this->m_ImageMapper->SetOrientation( 0 );
1061 this->m_ImageMapper->Update( );
1064 this->m_ImageActor->SetInterpolate( this->m_Interpolate );
1065 this->m_ImageActor->Modified( );
1067 if( this->m_Style.GetPointer( ) != NULL )
1068 this->m_Style->AssociateImageActor( this->m_ImageActor );
1069 this->AddItem( this->m_ImageActor );
1071 this->SetSliceNumber( this->GetSliceNumberMinValue( ) );
1072 this->ResetCursor( );
1076 this->ResetCursor( );
1077 this->ResetAxesCursor( );
1079 // Update window/level ranges
1080 vtkImageData* data = this->GetInputImage( );
1084 data->GetScalarRange( r );
1085 this->m_WLRange[ 0 ] = double( 0 );
1086 this->m_WLRange[ 1 ] = r[ 1 ] - r[ 0 ];
1087 this->m_WLRange[ 2 ] = r[ 0 ];
1088 this->m_WLRange[ 3 ] = r[ 1 ];
1089 this->ResetWindowLevel( );
1091 // Configure blender
1092 this->m_BlenderBase = vtkSmartPointer< vtkImageData >::New( );
1093 this->m_BlenderBase->ShallowCopy( data );
1094 this->m_BlenderBase->AllocateScalars( VTK_UNSIGNED_CHAR, 1 );
1096 this->m_BlenderBase->GetScalarPointer( ), 0,
1097 this->m_BlenderBase->GetActualMemorySize( )
1099 this->m_Blender->AddInputData( this->m_BlenderBase );
1104 // -------------------------------------------------------------------------
1105 void cpExtensions::Visualization::ImageSliceActors::
1106 _ConfigureBinaryImage( const double& r, const double& g, const double& b )
1108 unsigned int nValues = this->m_BlenderLUT->GetNumberOfTableValues( );
1109 this->m_BlenderLUT->SetNumberOfTableValues( nValues + 1 );
1110 this->m_BlenderLUT->SetTableRange( 0, nValues );
1111 this->m_BlenderLUT->SetTableValue( nValues, r, g, b, 0.5 );
1112 this->m_BlenderLUT->Build( );
1114 this->m_BlenderLUT->Modified( );
1115 this->m_Blender->Modified( );
1116 this->m_Blender->Update( );
1118 this->m_BlenderMapper->Modified( );
1119 this->m_BlenderActor->Modified( );
1122 // -------------------------------------------------------------------------
1123 void cpExtensions::Visualization::ImageSliceActors::
1125 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1126 bool alt, bool ctr, bool sft
1129 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1130 if( actors == NULL )
1133 if( btn == TStyle::ButtonID_None )
1135 // Just show the pixel information
1136 actors->SetCursor( pos );
1137 actors->UpdateText( pos );
1140 else if( btn == TStyle::ButtonID_Left )
1142 if( !alt && ctr && !sft )
1144 // Show axes in current renderer
1145 actors->SetAxesCursor( pos );
1148 // Interactively move slices
1149 auto i = actors->m_SlicesCommands.begin( );
1150 for( ; i != actors->m_SlicesCommands.end( ); ++i )
1151 i->first( pos, actors->GetAxis( ), i->second );
1155 else if( btn == TStyle::ButtonID_Right )
1157 if( !alt && !ctr && sft )
1159 // Change image window level
1161 actors->m_ImageMapper->GetBounds( bounds );
1163 int a0 = actors->GetAxis( );
1164 int a1 = ( a0 + 1 ) % 3;
1165 int a2 = ( a0 + 2 ) % 3;
1166 double dx = pos[ a1 ] - actors->m_StartWindowLevelPos[ a1 ];
1167 double dy = pos[ a2 ] - actors->m_StartWindowLevelPos[ a2 ];
1168 dx /= bounds[ ( a1 << 1 ) + 1 ] - bounds[ a1 << 1 ];
1169 dy /= bounds[ ( a2 << 1 ) + 1 ] - bounds[ a2 << 1 ];
1171 dx *= actors->m_StartWindowLevel[ 0 ];
1172 dy *= actors->m_StartWindowLevel[ 1 ];
1173 dx += actors->m_StartWindowLevel[ 0 ];
1174 dy += actors->m_StartWindowLevel[ 1 ];
1175 actors->SetWindowLevel( dx, dy );
1178 // Associate objects
1179 auto i = actors->m_WindowLevelCommands.begin( );
1180 for( ; i != actors->m_WindowLevelCommands.end( ); ++i )
1181 i->first( dx, dy, i->second );
1188 // -------------------------------------------------------------------------
1189 void cpExtensions::Visualization::ImageSliceActors::
1191 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1192 bool alt, bool ctr, bool sft
1195 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1196 if( actors == NULL )
1199 actors->m_StartWindowLevelPos[ 0 ] = pos[ 0 ];
1200 actors->m_StartWindowLevelPos[ 1 ] = pos[ 1 ];
1201 actors->m_StartWindowLevelPos[ 2 ] = pos[ 2 ];
1202 actors->m_StartWindowLevel[ 0 ] = actors->GetWindow( );
1203 actors->m_StartWindowLevel[ 1 ] = actors->GetLevel( );
1206 // -------------------------------------------------------------------------
1207 void cpExtensions::Visualization::ImageSliceActors::
1209 void* data, const int& dir,
1210 bool alt, bool ctr, bool sft
1213 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1214 if( actors == NULL )
1219 int slice = actors->GetSliceNumber( ) + ( dir * ( ( sft )? 10: 1 ) );
1220 if( slice < actors->GetSliceNumberMinValue( ) )
1221 slice = actors->GetSliceNumberMinValue( );
1222 if( slice > actors->GetSliceNumberMaxValue( ) )
1223 slice = actors->GetSliceNumberMaxValue( );
1224 actors->SetSliceNumber( slice );
1227 auto a = actors->m_AssociatedSlices.begin( );
1228 for( ; a != actors->m_AssociatedSlices.end( ); ++a )
1229 ( *a )->SetSliceNumber( slice );
1231 // Associate objects
1232 auto i = actors->m_RenderCommands.begin( );
1233 for( ; i != actors->m_RenderCommands.end( ); ++i )
1234 i->first( i->second );
1239 // -------------------------------------------------------------------------
1240 void cpExtensions::Visualization::ImageSliceActors::
1241 _KeyCommand( void* data, const char& key )
1243 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1244 if( actors == NULL )
1251 actors->ResetCamera( );
1254 // Associate objects
1255 auto i = actors->m_RenderCommands.begin( );
1256 for( ; i != actors->m_RenderCommands.end( ); ++i )
1257 i->first( i->second );
1262 actors->ResetWindowLevel( );
1265 // Associate objects
1266 auto i = actors->m_RenderCommands.begin( );
1267 for( ; i != actors->m_RenderCommands.end( ); ++i )
1268 i->first( i->second );
1276 // -------------------------------------------------------------------------
1277 void cpExtensions::Visualization::ImageSliceActors::
1278 _EnterCommand( void* data )
1280 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1281 if( actors == NULL )
1284 actors->ResetCursor( );
1285 actors->m_CursorActor->VisibilityOn( );
1289 // -------------------------------------------------------------------------
1290 void cpExtensions::Visualization::ImageSliceActors::
1291 _LeaveCommand( void* data )
1293 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1294 if( actors == NULL )
1297 actors->ResetCursor( );
1298 actors->m_CursorActor->VisibilityOff( );