1 #include <cpExtensions/Visualization/ImageViewerActors.h>
2 #include <cpExtensions/Visualization/CursorActors.h>
3 #include <vtkImageData.h>
4 #include <vtkProperty.h>
5 #include <vtkRenderWindow.h>
6 #include <vtkTextActor.h>
7 #include <vtkTextProperty.h>
10 // -------------------------------------------------------------------------
11 cpExtensions::Visualization::ImageViewerActors::
12 Self* cpExtensions::Visualization::ImageViewerActors::
15 return( new Self( ) );
18 // -------------------------------------------------------------------------
19 void cpExtensions::Visualization::ImageViewerActors::
20 SetImage( vtkImageData* image, int orientation )
22 this->Superclass::SetImage( image, orientation );
24 this->m_Cursor = vtkSmartPointer< CursorActors >::New( );
25 this->m_Cursor->SetImageBounds( image->GetBounds( ) );
26 this->m_Cursor->SetImageOrientation( orientation );
27 this->m_Cursor->GetProperty( 0 )->SetColor( 1, 1, 0 );
28 this->m_Cursor->GetProperty( 1 )->SetColor( 1, 1, 0 );
29 this->m_Cursor->GetProperty( 0 )->SetLineWidth( 1.5 );
30 this->m_Cursor->GetProperty( 1 )->SetLineWidth( 1.5 );
31 this->m_Cursor->VisibilityOff( );
33 this->m_SuperCursor = vtkSmartPointer< CursorActors >::New( );
34 this->m_SuperCursor->SetImageBounds( image->GetBounds( ) );
35 this->m_SuperCursor->SetImageOrientation( orientation );
36 this->m_SuperCursor->GetProperty( 0 )->SetColor( 0, 1, 1 );
37 this->m_SuperCursor->GetProperty( 1 )->SetColor( 0, 1, 1 );
38 this->m_SuperCursor->GetProperty( 0 )->SetLineWidth( 1.5 );
39 this->m_SuperCursor->GetProperty( 1 )->SetLineWidth( 1.5 );
40 this->m_SuperCursor->VisibilityOff( );
43 this->m_TextBuffer[ 0 ] = '\0';
44 this->m_Text = vtkSmartPointer< vtkTextActor >::New( );
45 this->m_Text->SetTextScaleModeToNone( );
46 auto textprop = this->m_Text->GetTextProperty( );
47 textprop->SetColor( 1, 1, 0 );
48 textprop->SetFontFamilyToCourier( );
49 textprop->SetFontSize( 18 );
51 textprop->ItalicOff( );
52 textprop->ShadowOff( );
53 textprop->SetJustificationToLeft( );
54 textprop->SetVerticalJustificationToBottom( );
55 auto coord = this->m_Text->GetPositionCoordinate( );
56 coord->SetCoordinateSystemToNormalizedViewport( );
57 coord->SetValue( 0.01, 0.05 );
58 this->m_Text->VisibilityOff( );
61 this->m_Cursor->InitTraversal( );
62 while( vtkProp* prop = this->m_Cursor->GetNextProp( ) )
63 this->AddItem( prop );
64 this->m_SuperCursor->InitTraversal( );
65 while( vtkProp* prop = this->m_SuperCursor->GetNextProp( ) )
66 this->AddItem( prop );
67 this->AddItem( this->m_Text );
70 // -------------------------------------------------------------------------
71 void cpExtensions::Visualization::ImageViewerActors::
72 SetCursor( double* pos, bool neg )
74 this->m_Cursor->SetCursor( pos, neg );
75 this->_ShowText( pos );
76 this->m_Cursor->VisibilityOn( );
77 this->m_Text->VisibilityOn( );
80 // -------------------------------------------------------------------------
81 void cpExtensions::Visualization::ImageViewerActors::
82 SetSuperCursor( double* pos, bool neg )
84 this->SetCursor( pos, neg );
87 // -------------------------------------------------------------------------
88 void cpExtensions::Visualization::ImageViewerActors::
91 this->m_Cursor->VisibilityOff( );
92 this->m_Text->VisibilityOff( );
95 // -------------------------------------------------------------------------
96 cpExtensions::Visualization::ImageViewerActors::
102 // -------------------------------------------------------------------------
103 cpExtensions::Visualization::ImageViewerActors::
104 ~ImageViewerActors( )
108 // -------------------------------------------------------------------------
109 void cpExtensions::Visualization::ImageViewerActors::
110 _CorrectPosition( double* pos, int* ijk )
112 auto image = this->GetImage( );
116 // Approximate image index
118 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
120 // Manually correct index
122 image->GetExtent( ext );
123 for( int i = 0; i < 3; ++i )
125 if( ijk[ i ] < ext[ i << 1 ] )
126 ijk[ i ] = ext[ i << 1 ];
127 if( ext[ ( i << 1 ) + 1 ] < ijk[ i ] )
128 ijk[ i ] = ext[ ( i << 1 ) + 1 ];
132 // Get real coordinates
133 int o = this->GetOrientation( );
134 ijk[ o ] = this->GetSliceNumber( );
135 image->GetPoint( image->ComputePointId( ijk ), pos );
138 // -------------------------------------------------------------------------
139 void cpExtensions::Visualization::ImageViewerActors::
140 _ShowText( double* pos )
142 auto image = this->GetImage( );
147 this->_CorrectPosition( pos, ijk );
148 int o = this->GetOrientation( );
150 std::stringstream buffer;
151 buffer << "Axis: " << char( 'X' + char( o ) ) << std::endl;
152 int nScl = image->GetNumberOfScalarComponents( );
154 << "Pixel: [" << ijk[ 0 ]
156 << "," << ijk[ 2 ] << "]=("
157 << image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
158 for( int n = 1; n < nScl; ++n )
161 << image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n );
163 cpExtensions_SPRINTF( this->m_TextBuffer, 1024, buffer.str( ).c_str( ) );
164 this->m_Text->SetInput( this->m_TextBuffer );
165 this->m_Text->VisibilityOn( );
166 this->m_Text->Modified( );