1 #include <cpExtensions/Visualization/ImageViewerActors.h>
2 #include <cpExtensions/Visualization/CursorActors.h>
3 #include <cpExtensions/Utility.h>
4 #include <vtkImageData.h>
5 #include <vtkProperty.h>
6 #include <vtkRenderWindow.h>
7 #include <vtkTextActor.h>
8 #include <vtkTextProperty.h>
11 // -------------------------------------------------------------------------
12 cpExtensions::Visualization::ImageViewerActors::
13 Self* cpExtensions::Visualization::ImageViewerActors::
16 return( new Self( ) );
19 // -------------------------------------------------------------------------
20 void cpExtensions::Visualization::ImageViewerActors::
21 SetImage( vtkImageData* image, int orientation )
23 this->Superclass::SetImage( image, orientation );
25 this->m_Cursor = vtkSmartPointer< CursorActors >::New( );
26 this->m_Cursor->SetImageBounds( image->GetBounds( ) );
27 this->m_Cursor->SetImageOrientation( orientation );
28 this->m_Cursor->GetProperty( 0 )->SetColor( 1, 1, 0 );
29 this->m_Cursor->GetProperty( 1 )->SetColor( 1, 1, 0 );
30 this->m_Cursor->GetProperty( 0 )->SetLineWidth( 1.5 );
31 this->m_Cursor->GetProperty( 1 )->SetLineWidth( 1.5 );
32 this->m_Cursor->VisibilityOff( );
34 this->m_SuperCursor = vtkSmartPointer< CursorActors >::New( );
35 this->m_SuperCursor->SetImageBounds( image->GetBounds( ) );
36 this->m_SuperCursor->SetImageOrientation( orientation );
37 this->m_SuperCursor->GetProperty( 0 )->SetColor( 0, 1, 1 );
38 this->m_SuperCursor->GetProperty( 1 )->SetColor( 0, 1, 1 );
39 this->m_SuperCursor->GetProperty( 0 )->SetLineWidth( 1.5 );
40 this->m_SuperCursor->GetProperty( 1 )->SetLineWidth( 1.5 );
41 this->m_SuperCursor->VisibilityOff( );
44 this->m_TextBuffer[ 0 ] = '\0';
45 this->m_Text = vtkSmartPointer< vtkTextActor >::New( );
46 this->m_Text->SetTextScaleModeToNone( );
47 auto textprop = this->m_Text->GetTextProperty( );
48 textprop->SetColor( 1, 1, 0 );
49 textprop->SetFontFamilyToCourier( );
50 textprop->SetFontSize( 18 );
52 textprop->ItalicOff( );
53 textprop->ShadowOff( );
54 textprop->SetJustificationToLeft( );
55 textprop->SetVerticalJustificationToBottom( );
56 auto coord = this->m_Text->GetPositionCoordinate( );
57 coord->SetCoordinateSystemToNormalizedViewport( );
58 coord->SetValue( 0.01, 0.05 );
59 this->m_Text->VisibilityOff( );
62 this->m_Cursor->InitTraversal( );
63 while( vtkProp* prop = this->m_Cursor->GetNextProp( ) )
64 this->AddItem( prop );
65 this->m_SuperCursor->InitTraversal( );
66 while( vtkProp* prop = this->m_SuperCursor->GetNextProp( ) )
67 this->AddItem( prop );
68 this->AddItem( this->m_Text );
71 // -------------------------------------------------------------------------
72 void cpExtensions::Visualization::ImageViewerActors::
73 SetCursor( double* pos, bool neg )
75 this->m_Cursor->SetCursor( pos, neg );
76 this->_ShowText( pos );
77 this->m_Cursor->VisibilityOn( );
78 this->m_Text->VisibilityOn( );
81 // -------------------------------------------------------------------------
82 void cpExtensions::Visualization::ImageViewerActors::
83 SetSuperCursor( double* pos, bool neg )
85 this->SetCursor( pos, neg );
88 // -------------------------------------------------------------------------
89 void cpExtensions::Visualization::ImageViewerActors::
92 this->m_Cursor->VisibilityOff( );
93 this->m_Text->VisibilityOff( );
96 // -------------------------------------------------------------------------
97 cpExtensions::Visualization::ImageViewerActors::
103 // -------------------------------------------------------------------------
104 cpExtensions::Visualization::ImageViewerActors::
105 ~ImageViewerActors( )
109 // -------------------------------------------------------------------------
110 void cpExtensions::Visualization::ImageViewerActors::
111 _CorrectPosition( double* pos, int* ijk )
113 auto image = this->GetImage( );
117 // Approximate image index
119 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
121 // Manually correct index
123 image->GetExtent( ext );
124 for( int i = 0; i < 3; ++i )
126 if( ijk[ i ] < ext[ i << 1 ] )
127 ijk[ i ] = ext[ i << 1 ];
128 if( ext[ ( i << 1 ) + 1 ] < ijk[ i ] )
129 ijk[ i ] = ext[ ( i << 1 ) + 1 ];
133 // Get real coordinates
134 int o = this->GetOrientation( );
135 ijk[ o ] = this->GetSliceNumber( );
136 image->GetPoint( image->ComputePointId( ijk ), pos );
139 // -------------------------------------------------------------------------
140 void cpExtensions::Visualization::ImageViewerActors::
141 _ShowText( double* pos )
143 auto image = this->GetImage( );
148 this->_CorrectPosition( pos, ijk );
149 int o = this->GetOrientation( );
151 std::stringstream buffer;
152 buffer << "Axis: " << char( 'X' + char( o ) ) << std::endl;
153 int nScl = image->GetNumberOfScalarComponents( );
155 << "Pixel: [" << ijk[ 0 ]
157 << "," << ijk[ 2 ] << "]=("
158 << image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
159 for( int n = 1; n < nScl; ++n )
162 << image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n );
164 cpExtensions_SPRINTF( this->m_TextBuffer, 1024, buffer.str( ).c_str( ) );
165 this->m_Text->SetInput( this->m_TextBuffer );
166 this->m_Text->VisibilityOn( );
167 this->m_Text->Modified( );