]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Visualization/ImageViewerActors.cxx
d01551bf9ee8a56943c95f5255b570cea7ca3800
[cpPlugins.git] / lib / cpExtensions / Visualization / ImageViewerActors.cxx
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>
9 #include <sstream>
10
11 // -------------------------------------------------------------------------
12 cpExtensions::Visualization::ImageViewerActors::
13 Self* cpExtensions::Visualization::ImageViewerActors::
14 New( )
15 {
16   return( new Self( ) );
17 }
18
19 // -------------------------------------------------------------------------
20 void cpExtensions::Visualization::ImageViewerActors::
21 SetImage( vtkImageData* image, int orientation )
22 {
23   this->Superclass::SetImage( image, orientation );
24
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( );
33
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( );
42
43   // Prepare text
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 );
51   textprop->BoldOff( );
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( );
60
61   // Update actor list
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 );
69 }
70
71 // -------------------------------------------------------------------------
72 void cpExtensions::Visualization::ImageViewerActors::
73 SetCursor( double* pos, bool neg )
74 {
75   this->m_Cursor->SetCursor( pos, neg );
76   this->_ShowText( pos );
77   this->m_Cursor->VisibilityOn( );
78   this->m_Text->VisibilityOn( );
79 }
80
81 // -------------------------------------------------------------------------
82 void cpExtensions::Visualization::ImageViewerActors::
83 SetSuperCursor( double* pos, bool neg )
84 {
85   this->SetCursor( pos, neg );
86 }
87
88 // -------------------------------------------------------------------------
89 void cpExtensions::Visualization::ImageViewerActors::
90 HideViewerActors( )
91 {
92   this->m_Cursor->VisibilityOff( );
93   this->m_Text->VisibilityOff( );
94 }
95
96 // -------------------------------------------------------------------------
97 cpExtensions::Visualization::ImageViewerActors::
98 ImageViewerActors( )
99   : Superclass( )
100 {
101 }
102
103 // -------------------------------------------------------------------------
104 cpExtensions::Visualization::ImageViewerActors::
105 ~ImageViewerActors( )
106 {
107 }
108
109 // -------------------------------------------------------------------------
110 void cpExtensions::Visualization::ImageViewerActors::
111 _CorrectPosition( double* pos, int* ijk )
112 {
113   auto image = this->GetImage( );
114   if( image == NULL )
115     return;
116
117   // Approximate image index
118   double pcoords[ 3 ];
119   image->ComputeStructuredCoordinates( pos, ijk, pcoords );
120
121   // Manually correct index
122   int ext[ 6 ];
123   image->GetExtent( ext );
124   for( int i = 0; i < 3; ++i )
125   {
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 ];
130
131   } // rof
132
133   // Get real coordinates
134   int o = this->GetOrientation( );
135   ijk[ o ] = this->GetSliceNumber( );
136   image->GetPoint( image->ComputePointId( ijk ), pos );
137 }
138
139 // -------------------------------------------------------------------------
140 void cpExtensions::Visualization::ImageViewerActors::
141 _ShowText( double* pos )
142 {
143   auto image = this->GetImage( );
144   if( image == NULL )
145     return;
146
147   int ijk[ 3 ];
148   this->_CorrectPosition( pos, ijk );
149   int o = this->GetOrientation( );
150
151   std::stringstream buffer;
152   buffer << "Axis: " << char( 'X' + char( o ) ) << std::endl;
153   int nScl = image->GetNumberOfScalarComponents( );
154   buffer
155     << "Pixel: [" << ijk[ 0 ]
156     << "," << ijk[ 1 ]
157     << "," << ijk[ 2 ] << "]=("
158     << image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
159   for( int n = 1; n < nScl; ++n )
160     buffer
161       << " : "
162       << image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n );
163   buffer << ")";
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( );
168 }
169
170 // eof - $RCSfile$