1 #include <cpExtensions/Visualization/ImageSliceActors.h>
5 #include <vtkImageData.h>
6 #include <vtkImageProperty.h>
8 #include <vtkProperty.h>
9 #include <vtkRenderer.h>
10 #include <vtkRenderWindow.h>
11 #include <vtkTextProperty.h>
13 // -------------------------------------------------------------------------
14 cpExtensions::Visualization::ImageSliceActors::
15 Self* cpExtensions::Visualization::ImageSliceActors::
18 return( new Self( ) );
21 // -------------------------------------------------------------------------
22 void cpExtensions::Visualization::ImageSliceActors::
25 this->RemoveAllItems( );
28 this->m_Mapper = vtkSmartPointer< vtkImageSliceMapper >::New( );
29 this->m_Actor = vtkSmartPointer< vtkImageActor >::New( );
30 this->m_Actor->SetMapper( this->m_Mapper );
33 this->m_Cursor.Create( );
34 this->m_Cursor.Actor->GetProperty( )->SetColor( 1, 1, 0 );
37 this->m_TextBuffer[ 0 ] = '\0';
38 this->m_TextActor = vtkSmartPointer< vtkTextActor >::New( );
39 this->m_TextActor->SetTextScaleModeToNone( );
40 vtkTextProperty* textprop = this->m_TextActor->GetTextProperty( );
41 textprop->SetColor( 1, 1, 0 );
42 textprop->SetFontFamilyToCourier( );
43 textprop->SetFontSize( 18 );
45 textprop->ItalicOff( );
46 textprop->ShadowOff( );
47 textprop->SetJustificationToLeft( );
48 textprop->SetVerticalJustificationToBottom( );
49 vtkCoordinate* coord = this->m_TextActor->GetPositionCoordinate( );
50 coord->SetCoordinateSystemToNormalizedViewport( );
51 coord->SetValue( 0.01, 0.05 );
54 vtkSmartPointer< TStyle > st = vtkSmartPointer< TStyle >::New( );
55 st->SetAssociatedObject( this );
56 st->AddMouseMoveCommand( Self::_MouseMoveCommand, this );
57 st->AddMouseClickCommand( Self::_MouseClickCommand, this );
58 st->AddMouseWheelCommand( Self::_MouseWheelCommand, this );
59 st->AddKeyCommand( Self::_KeyCommand, this );
60 st->AddEnterCommand( Self::_EnterCommand, this );
61 st->AddLeaveCommand( Self::_LeaveCommand, this );
65 // -------------------------------------------------------------------------
66 void cpExtensions::Visualization::ImageSliceActors::
67 SetInputConnection( vtkAlgorithmOutput* aout, int orientation )
71 this->m_Mapper->SetInputConnection( aout );
72 this->_ConfigureInput( orientation );
75 // -------------------------------------------------------------------------
76 void cpExtensions::Visualization::ImageSliceActors::
77 SetInputData( vtkImageData* data, int orientation )
81 this->m_Mapper->SetInputData( data );
82 this->_ConfigureInput( orientation );
85 // -------------------------------------------------------------------------
86 void cpExtensions::Visualization::ImageSliceActors::
87 SetInputActor( vtkProp* actor, int orientation )
89 auto real_actor = dynamic_cast< vtkImageActor* >( actor );
90 if( real_actor == NULL )
93 dynamic_cast< vtkImageSliceMapper* >( real_actor->GetMapper( ) );
94 if( real_mapper == NULL )
96 this->m_Actor = real_actor;
97 this->m_Mapper = real_mapper;
98 this->_ConfigureInput( orientation );
101 // -------------------------------------------------------------------------
102 vtkImageData* cpExtensions::Visualization::ImageSliceActors::
105 if( this->m_Mapper.GetPointer( ) != NULL )
106 return( this->m_Mapper->GetInput( ) );
111 // -------------------------------------------------------------------------
112 vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
115 return( this->m_Actor.GetPointer( ) );
118 // -------------------------------------------------------------------------
119 const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
120 GetImageActor( ) const
122 return( this->m_Actor.GetPointer( ) );
125 // -------------------------------------------------------------------------
126 void cpExtensions::Visualization::ImageSliceActors::
127 AddMesh( vtkPolyData* mesh )
129 SourceActor< vtkCutter > a;
131 a.Source->SetInputData( mesh );
132 a.Source->SetCutFunction( this->m_Mapper->GetSlicePlane( ) );
133 a.Source->SetValue( 0, 0 );
134 a.Source->GenerateTrianglesOff( );
137 this->m_Meshes[ mesh ] = a;
138 this->AddItem( a.Actor );
140 auto ren = this->m_Style->GetCurrentRenderer( );
142 if( ren->HasViewProp( this->m_Actor ) )
143 ren->AddViewProp( a.Actor );
146 // -------------------------------------------------------------------------
147 void cpExtensions::Visualization::ImageSliceActors::
148 AssociateSlice( Self* slice )
150 this->m_AssociatedSlices.push_back( slice );
154 // -------------------------------------------------------------------------
155 void cpExtensions::Visualization::ImageSliceActors::
156 CleanAssociatedSlices( )
158 this->m_AssociatedSlices.clear( );
162 // -------------------------------------------------------------------------
163 cpExtensions::Interaction::ImageInteractorStyle*
164 cpExtensions::Visualization::ImageSliceActors::
167 return( dynamic_cast< TStyle* >( this->m_Style.GetPointer( ) ) );
170 // -------------------------------------------------------------------------
171 void cpExtensions::Visualization::ImageSliceActors::
172 SetStyle( vtkInteractorStyle* st )
178 // -------------------------------------------------------------------------
179 void cpExtensions::Visualization::ImageSliceActors::
180 PushInto( vtkRenderer* ren )
182 this->InitTraversal( );
184 while( ( prop = this->GetNextProp( ) ) != NULL )
185 ren->AddViewProp( prop );
188 // -------------------------------------------------------------------------
189 void cpExtensions::Visualization::ImageSliceActors::
190 PopFrom( vtkRenderer* ren )
192 this->InitTraversal( );
194 while( ( prop = this->GetNextProp( ) ) != NULL )
195 ren->RemoveViewProp( prop );
198 // -------------------------------------------------------------------------
199 long cpExtensions::Visualization::ImageSliceActors::
200 GetSliceNumber( ) const
202 if( this->m_Mapper.GetPointer( ) != NULL )
203 return( this->m_Mapper->GetSliceNumber( ) );
208 // -------------------------------------------------------------------------
209 void cpExtensions::Visualization::ImageSliceActors::
210 SetSliceNumber( long slice )
212 if( this->m_Mapper.GetPointer( ) == NULL )
216 int a = this->m_Mapper->GetOrientation( );
220 this->m_Mapper->GetInput( )->GetExtent( ext );
222 rs = ( rs > ext[ a << 1 ] )? rs: ext[ a << 1 ];
223 rs = ( rs < ext[ ( a << 1 ) + 1 ] )? rs: ext[ ( a << 1 ) + 1 ];
226 this->m_Mapper->SetSliceNumber( rs );
228 // Update display extent (this isn't done automatically)
229 ext[ a << 1 ] = ext[ ( a << 1 ) + 1 ] = rs;
230 this->m_Actor->SetDisplayExtent( ext );
232 this->m_Mapper->Modified( );
233 this->m_Actor->Modified( );
234 this->_ConfigureCursor( );
238 // -------------------------------------------------------------------------
239 void cpExtensions::Visualization::ImageSliceActors::
240 ShowPixelText( double* pos )
242 if( this->m_Mapper.GetPointer( ) != NULL )
245 int axId = this->m_Mapper->GetOrientation( );
246 if ( axId == 0 ) axis = 'X';
247 else if( axId == 1 ) axis = 'Y';
248 else if( axId == 2 ) axis = 'Z';
249 long slice = this->GetSliceNumber( );
251 vtkImageData* image = this->m_Actor->GetInput( );
254 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
258 image->GetExtent( ext );
260 ext[ 0 ] <= ijk[ 0 ] && ijk[ 0 ] <= ext[ 1 ] &&
261 ext[ 2 ] <= ijk[ 1 ] && ijk[ 1 ] <= ext[ 3 ] &&
262 ext[ 4 ] <= ijk[ 2 ] && ijk[ 2 ] <= ext[ 5 ]
265 int nScl = image->GetNumberOfScalarComponents( );
266 std::stringstream str;
270 << "," << ijk[ 2 ] << "]=(";
272 image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
273 for( int n = 1; n < nScl; ++n )
276 << image->GetScalarComponentAsFloat(
277 ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n
283 this->m_TextBuffer, MAX_TEXT_BUFFER, "Axis: %c (%d)\nPixel %s",
284 axis, slice, str.str( ).c_str( )
286 #else // defined(WIN32)
288 this->m_TextBuffer, "Axis: %c (%ld)\nPixel %s",
289 axis, slice, str.str( ).c_str( )
291 #endif // defined(WIN32)
295 this->m_TextBuffer[ 0 ] = '\0';
296 this->m_TextActor->SetInput( this->m_TextBuffer );
297 this->m_TextActor->Modified( );
301 // -------------------------------------------------------------------------
302 void cpExtensions::Visualization::ImageSliceActors::
303 GetScalarRange( double r[ 2 ] ) const
305 r[ 0 ] = this->m_ScalarRange[ 0 ];
306 r[ 1 ] = this->m_ScalarRange[ 1 ];
309 // -------------------------------------------------------------------------
310 void cpExtensions::Visualization::ImageSliceActors::
311 SetScalarRange( const double& a, const double& b )
313 this->m_ManualScalarRange = true;
314 this->m_ScalarRange[ 0 ] = a;
315 this->m_ScalarRange[ 1 ] = b;
316 this->SetWindowLevel( 1, 0.5 );
319 // -------------------------------------------------------------------------
320 void cpExtensions::Visualization::ImageSliceActors::
323 auto image = this->m_Actor->GetInput( );
327 image->GetScalarRange( r );
328 this->SetScalarRange( r[ 0 ], r[ 1 ] );
331 this->m_ManualScalarRange = false;
334 // -------------------------------------------------------------------------
335 void cpExtensions::Visualization::ImageSliceActors::
336 SetWindowLevel( const double& w, const double& l )
338 static const double _0 = double( 0 );
339 static const double _1 = double( 1 );
341 double rw = ( w < _0 )? _0: ( ( w > _1 )? _1: w );
342 double rl = ( l < _0 )? _0: ( ( l > _1 )? _1: l );
344 double d = this->m_ScalarRange[ 1 ] - this->m_ScalarRange[ 0 ];
347 rl += this->m_ScalarRange[ 0 ];
349 if( this->m_Actor.GetPointer( ) != NULL )
351 this->m_Actor->GetProperty( )->SetColorWindow( rw );
352 this->m_Actor->GetProperty( )->SetColorLevel( rl );
358 // -------------------------------------------------------------------------
359 double cpExtensions::Visualization::ImageSliceActors::
362 if( this->m_Actor.GetPointer( ) != NULL )
363 return( this->m_Actor->GetProperty( )->GetColorWindow( ) );
365 return( double( 0 ) );
368 // -------------------------------------------------------------------------
369 double cpExtensions::Visualization::ImageSliceActors::
372 if( this->m_Actor.GetPointer( ) != NULL )
373 return( this->m_Actor->GetProperty( )->GetColorLevel( ) );
375 return( double( 0 ) );
378 // -------------------------------------------------------------------------
379 void cpExtensions::Visualization::ImageSliceActors::
382 auto ren = this->m_Style->GetCurrentRenderer( );
385 auto win = ren->GetRenderWindow( );
391 // -------------------------------------------------------------------------
392 cpExtensions::Visualization::ImageSliceActors::
395 m_ManualScalarRange( false )
400 // -------------------------------------------------------------------------
401 cpExtensions::Visualization::ImageSliceActors::
406 // -------------------------------------------------------------------------
407 void cpExtensions::Visualization::ImageSliceActors::
408 _ConfigureInput( int orientation )
410 // Prepare main image
411 this->m_Mapper->SetOrientation( orientation );
412 this->m_Mapper->Update( );
413 this->m_Actor->GetProperty( )->SetOpacity( 0.8 );
414 this->m_Actor->Modified( );
415 auto st = this->GetStyle( );
417 st->AssociateImageActor( this->m_Actor );
418 this->AddItem( this->m_Actor );
420 // Prepare main cursor
421 this->_ConfigureCursor( );
422 this->AddItem( this->m_Cursor.Actor );
424 // Prepare window/level
425 if( !( this->m_ManualScalarRange ) )
426 this->UnsetScalarRange( );
428 // Add all other actors
429 this->AddItem( this->m_TextActor );
431 // Put it in the first slice
432 auto image = this->m_Mapper->GetInput( );
434 image->GetExtent( ext );
435 this->SetSliceNumber( ext[ orientation << 1 ] );
438 if( this->m_Style.GetPointer( ) != NULL )
439 this->AddItem( this->m_Actor );
441 this->SetSliceNumber( this->GetSliceNumberMinValue( ) );
442 this->ResetCursor( );
446 this->ResetCursor( );
447 this->ResetAxesCursor( );
449 // Update window/level ranges
450 vtkImageData* data = this->GetInputImage( );
454 data->GetScalarRange( r );
455 this->m_WLRange[ 0 ] = double( 0 );
456 this->m_WLRange[ 1 ] = r[ 1 ] - r[ 0 ];
457 this->m_WLRange[ 2 ] = r[ 0 ];
458 this->m_WLRange[ 3 ] = r[ 1 ];
459 this->ResetWindowLevel( );
462 this->m_BlenderBase = vtkSmartPointer< vtkImageData >::New( );
463 this->m_BlenderBase->ShallowCopy( data );
464 this->m_BlenderBase->AllocateScalars( VTK_UNSIGNED_CHAR, 1 );
466 this->m_BlenderBase->GetScalarPointer( ), 0,
467 this->m_BlenderBase->GetActualMemorySize( )
469 this->m_Blender->AddInputData( this->m_BlenderBase );
475 // -------------------------------------------------------------------------
476 void cpExtensions::Visualization::ImageSliceActors::
479 int a = this->m_Mapper->GetOrientation( );
481 this->m_Actor->GetBounds( bounds );
482 bounds[ a << 1 ] += 1e-5;
483 bounds[ ( a << 1 ) + 1 ] += 1e-5;
485 this->m_Cursor.Source->SetModelBounds( bounds );
486 this->m_Cursor.Source->AllOn( );
487 this->m_Cursor.Actor->GetProperty( )->SetOpacity( 1 );
488 this->m_Cursor.Actor->GetProperty( )->SetLineWidth( 3 );
489 this->m_Cursor.Modified( );
492 // -------------------------------------------------------------------------
493 void cpExtensions::Visualization::ImageSliceActors::
496 const TStyle::ButtonID& btn,
497 int* idx, double* pos,
498 bool alt, bool ctr, bool sft
501 ImageSliceActors* self = reinterpret_cast< ImageSliceActors* >( data );
505 if( btn == TStyle::ButtonID_None )
507 self->ShowPixelText( pos );
508 self->m_Cursor.Source->SetFocalPoint( pos );
509 self->m_Cursor.Source->Modified( );
510 self->m_Cursor.Modified( );
513 else if( btn == TStyle::ButtonID_Right )
515 if( !alt && !ctr && sft )
517 int a0 = self->m_Mapper->GetOrientation( );
518 int a1 = ( a0 + 1 ) % 3;
519 int a2 = ( a0 + 2 ) % 3;
520 double dx = pos[ a1 ] - self->m_StartMouseEvent[ a1 ];
521 double dy = pos[ a2 ] - self->m_StartMouseEvent[ a2 ];
523 self->m_Actor->GetBounds( bounds );
524 dx /= bounds[ ( a1 << 1 ) + 1 ] - bounds[ a1 << 1 ];
525 dy /= bounds[ ( a2 << 1 ) + 1 ] - bounds[ a2 << 1 ];
526 double w = self->m_StartWindow + dx;
527 double l = self->m_StartLevel + dy;
529 self->SetWindowLevel( w, l );
532 auto aIt = self->m_AssociatedSlices.begin( );
533 aIt != self->m_AssociatedSlices.end( );
537 ( *aIt )->SetWindowLevel( w, l );
547 // -------------------------------------------------------------------------
548 void cpExtensions::Visualization::ImageSliceActors::
551 const TStyle::ButtonID& btn,
552 int* idx, double* pos,
553 bool alt, bool ctr, bool sft
556 ImageSliceActors* self = reinterpret_cast< ImageSliceActors* >( data );
560 self->m_StartMouseEvent[ 0 ] = pos[ 0 ];
561 self->m_StartMouseEvent[ 1 ] = pos[ 1 ];
562 self->m_StartMouseEvent[ 2 ] = pos[ 2 ];
564 if( btn == TStyle::ButtonID_Right )
566 if( !alt && !ctr && sft )
569 self->GetScalarRange( r );
570 double d = r[ 1 ] - r[ 0 ];
571 self->m_StartWindow = self->GetWindow( ) / d;
572 self->m_StartLevel = ( self->GetLevel( ) - r[ 0 ] ) / d;
579 // -------------------------------------------------------------------------
580 void cpExtensions::Visualization::ImageSliceActors::
583 const int& dir, bool alt, bool ctr, bool sft
586 ImageSliceActors* self = reinterpret_cast< ImageSliceActors* >( data );
589 auto ren = self->m_Style->GetCurrentRenderer( );
592 auto cam = ren->GetActiveCamera( );
595 auto win = ren->GetRenderWindow( );
599 // Get previous values
600 int a = self->m_Mapper->GetOrientation( );
602 self->m_Actor->GetBounds( bounds );
603 double prev_pos = bounds[ a << 1 ];
606 long slice = self->GetSliceNumber( ) + dir * ( ( sft )? 10: 1 );
607 self->SetSliceNumber( slice );
608 self->m_Actor->GetBounds( bounds );
612 cam->GetPosition( cam_pos );
613 cam_pos[ a ] += bounds[ a << 1 ] - prev_pos;
614 cam->SetPosition( cam_pos );
617 self->ShowPixelText( self->m_Cursor.Source->GetFocalPoint( ) );
622 auto aIt = self->m_AssociatedSlices.begin( );
623 aIt != self->m_AssociatedSlices.end( );
627 if( ( *aIt )->m_Mapper->GetOrientation( ) == a )
629 ( *aIt )->SetSliceNumber( slice );
637 // -------------------------------------------------------------------------
638 void cpExtensions::Visualization::ImageSliceActors::
646 // -------------------------------------------------------------------------
647 void cpExtensions::Visualization::ImageSliceActors::
648 _EnterCommand( void* data )
652 // -------------------------------------------------------------------------
653 void cpExtensions::Visualization::ImageSliceActors::
654 _LeaveCommand( void* data )