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