]> Creatis software - clitk.git/blob - vv/vvBinaryImageOverlayActor.cxx
- display binary image as overlay
[clitk.git] / vv / vvBinaryImageOverlayActor.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ======================================================================-====*/
18
19 #include "vvBinaryImageOverlayActor.h"
20 #include "vvImage.h"
21 #include <vtkImageActor.h>
22 #include <vtkCamera.h>
23 #include <vtkRenderer.h>
24 #include <vtkMarchingSquares.h>
25 #include <vtkImageClip.h>
26 #include <vtkImageData.h>
27 #include <vtkPolyDataMapper.h>
28 #include <vtkProperty.h>
29 #include <vtkImageMapToRGBA.h>
30 #include <vtkLookupTable.h>
31
32 //------------------------------------------------------------------------------
33 vvBinaryImageOverlayActor::vvBinaryImageOverlayActor() {
34   mTSlice = -1;
35   mSlice = 0;
36   mColor.resize(3);
37   mAlpha = 0.6;
38   mImage = 0;
39   mSlicer = 0;
40 }
41 //------------------------------------------------------------------------------
42
43
44 //------------------------------------------------------------------------------
45 vvBinaryImageOverlayActor::~vvBinaryImageOverlayActor() {
46   for (unsigned int i = 0; i < mSlicer->GetImage()->GetVTKImages().size(); i++) {
47     mSlicer->GetRenderer()->RemoveActor(mImageActorList[i]);
48   }
49   mImageActorList.clear();
50   mMapperList.clear();
51 }
52 //------------------------------------------------------------------------------
53
54
55 //------------------------------------------------------------------------------
56 void vvBinaryImageOverlayActor::setColor(double r, double g, double b) {
57   mColor[0] = r;
58   mColor[1] = g;
59   mColor[2] = b;
60 }
61 //------------------------------------------------------------------------------
62
63
64 //------------------------------------------------------------------------------
65 void vvBinaryImageOverlayActor::setSlicer(vvSlicer * slicer) {
66   mSlicer = slicer;
67 }
68 //------------------------------------------------------------------------------
69
70
71 //------------------------------------------------------------------------------
72 void vvBinaryImageOverlayActor::initialize() {
73   if (!mSlicer) {
74     std::cerr << "ERROR. Please use setSlicer before setSlicer in vvBinaryImageOverlayActor." << std::endl;
75     exit(0);
76   }
77   
78   if (!mImage) {
79     std::cerr << "ERROR. Please use setImage before setSlicer in vvBinaryImageOverlayActor." << std::endl;
80     exit(0);
81   }
82   // Create an actor for each time slice
83   for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
84     DD(numImage);
85
86     // how many intensity ? 
87     
88
89     vtkImageMapToRGBA * mOverlayMapper = vtkImageMapToRGBA::New();
90     mOverlayMapper->SetInput(mImage->GetVTKImages()[0]); // DS TODO : to change if it is 4D !!!
91     vtkLookupTable * lut = vtkLookupTable::New();
92     DD(lut->IsOpaque ());
93     lut->SetRange(0,1);
94     lut->SetNumberOfTableValues(2);
95     lut->SetTableValue(mBackgroundValue, 0, 0, 0, 0.0);   // BG
96     lut->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
97     DD(mColor[0]);
98     mOverlayMapper->SetLookupTable(lut);
99     
100     vtkImageActor * mOverlayActor = vtkImageActor::New();
101     mOverlayActor->SetInput(mOverlayMapper->GetOutput());
102     mOverlayActor->SetPickable(0);
103     mOverlayActor->SetVisibility(true);
104     //mOverlayActor->SetOpacity(1.0);
105
106     mMapperList.push_back(mOverlayMapper);
107     mImageActorList.push_back(mOverlayActor);
108     mSlicer->GetRenderer()->AddActor(mOverlayActor);  
109   }
110 }
111 //------------------------------------------------------------------------------
112
113
114 //------------------------------------------------------------------------------
115 void vvBinaryImageOverlayActor::setImage(vvImage::Pointer image, double bg) {
116   mImage = image;
117   mBackgroundValue = bg;
118 }
119 //------------------------------------------------------------------------------
120
121
122 //------------------------------------------------------------------------------
123 void vvBinaryImageOverlayActor::hideActors() {
124   if (!mSlicer) return;
125   mSlice = mSlicer->GetSlice();
126   for(unsigned int i=0; i<mImageActorList.size(); i++) {
127     mImageActorList[i]->VisibilityOff();
128   }
129 }
130 //------------------------------------------------------------------------------
131
132   
133 //------------------------------------------------------------------------------
134 void vvBinaryImageOverlayActor::showActors() {
135   if (!mSlicer) return;
136   mSlice = mSlicer->GetSlice();
137   mTSlice = mSlicer->GetTSlice();
138   //  for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
139   mImageActorList[mTSlice]->VisibilityOn();
140   update(0, mSlice);
141   //}
142 }
143 //------------------------------------------------------------------------------
144
145   
146 //------------------------------------------------------------------------------
147 void vvBinaryImageOverlayActor::update(int slicer, int slice) {
148   if (!mSlicer) return;
149
150   if (mPreviousSlice == mSlicer->GetSlice()) {
151     if (mPreviousTSlice == mSlicer->GetTSlice()) {
152       //DD("=========== NOTHING");
153       return; // Nothing to do
154     }
155   }
156
157   // Get current slice
158   mSlice = mSlicer->GetSlice();
159   mTSlice = mSlicer->GetTSlice();
160
161   // Update extent
162   int * imageExtent = mSlicer->GetExtent();
163   int orientation = mSlicer->GetOrientation();
164   int maskExtent[6];
165   ComputeExtent(orientation, mSlice, imageExtent, maskExtent);
166   //ComputeExtent(imageExtent, maskExtent, mSlicer->GetImage()->GetFirstVTKImageData(), mImage->GetFirstVTKImageData());
167   ComputeExtent(maskExtent, maskExtent, mSlicer->GetImage()->GetFirstVTKImageData(), mImage->GetFirstVTKImageData());
168   // std::cout << "maskExtent = " << maskExtent[0] << " " << maskExtent[1] << " " << maskExtent[2] << " " 
169   //        << maskExtent[3] << " " << maskExtent[4] << " " << maskExtent[5] << std::endl;
170   mSlicer->ClipDisplayedExtent(maskExtent, mMapperList[mTSlice]->GetInput()->GetWholeExtent());
171   // std::cout << "maskExtent = " << maskExtent[0] << " " << maskExtent[1] << " " << maskExtent[2] << " " 
172   //        << maskExtent[3] << " " << maskExtent[4] << " " << maskExtent[5] << std::endl;
173   SetDisplayExtentAndCameraPosition(orientation, mSlice, maskExtent, mImageActorList[mTSlice], 0.0);
174   
175   // set previous slice
176   mPreviousTSlice = mSlicer->GetTSlice();
177   mPreviousSlice  = mSlicer->GetSlice();
178 }
179 //------------------------------------------------------------------------------
180
181
182 //------------------------------------------------------------------------------
183 void vvBinaryImageOverlayActor::ComputeExtent(int orientation, 
184                                               int slice, 
185                                               int * inExtent,
186                                               int * outExtent) {
187   //  std::cout << "InExtent = " << inExtent[0] << " " << inExtent[1] << " " << inExtent[2] << " " 
188   //        << inExtent[3] << " " << inExtent[4] << " " << inExtent[5] << std::endl;
189   //  DD(orientation);
190   switch (orientation) {
191   case vtkImageViewer2::SLICE_ORIENTATION_XY:
192     for(int i=0; i<4; i++) outExtent[i] = inExtent[i];
193     outExtent[4] = outExtent[5] = slice; 
194     break;
195   case vtkImageViewer2::SLICE_ORIENTATION_XZ:
196     for(int i=0; i<2; i++) outExtent[i] = inExtent[i];
197     for(int i=4; i<6; i++) outExtent[i] = inExtent[i];
198     outExtent[2] = outExtent[3] = slice; 
199     break;
200   case vtkImageViewer2::SLICE_ORIENTATION_YZ:
201     for(int i=2; i<6; i++) outExtent[i] = inExtent[i];
202     outExtent[0] = outExtent[1] = slice; 
203     break;
204   }
205   // std::cout << "OutExtent = " << outExtent[0] << " " << outExtent[1] << " " << outExtent[2] << " " 
206   //        << outExtent[3] << " " << outExtent[4] << " " << outExtent[5] << std::endl;
207 }
208 //------------------------------------------------------------------------------
209
210
211 //----------------------------------------------------------------------------
212 void vvBinaryImageOverlayActor::ComputeExtent(int * inExtent, int * outExtent, vtkImageData * image, vtkImageData * overlay)
213 {
214   outExtent[0] = (( image->GetOrigin()[0] + inExtent[0]*image->GetSpacing()[0] ) - overlay->GetOrigin()[0]) /
215     overlay->GetSpacing()[0];
216   outExtent[1] = (( image->GetOrigin()[0] + inExtent[1]*image->GetSpacing()[0] ) - overlay->GetOrigin()[0]) /
217     overlay->GetSpacing()[0];
218   outExtent[2] = (( image->GetOrigin()[1] + inExtent[2]*image->GetSpacing()[1] ) - overlay->GetOrigin()[1]) /
219     overlay->GetSpacing()[1];
220   outExtent[3] = (( image->GetOrigin()[1] + inExtent[3]*image->GetSpacing()[1] ) - overlay->GetOrigin()[1]) /
221     overlay->GetSpacing()[1];
222   outExtent[4] = (( image->GetOrigin()[2] + inExtent[4]*image->GetSpacing()[2] ) - overlay->GetOrigin()[2]) /
223     overlay->GetSpacing()[2];
224   outExtent[5] = (( image->GetOrigin()[2] + inExtent[5]*image->GetSpacing()[2] ) - overlay->GetOrigin()[2]) /
225     overlay->GetSpacing()[2];
226 }
227 //----------------------------------------------------------------------------
228
229
230 //------------------------------------------------------------------------------
231 void vvBinaryImageOverlayActor::SetDisplayExtentAndCameraPosition(int orientation, 
232                                                                   int slice, 
233                                                                   int * extent, 
234                                                                   vtkImageActor * actor, 
235                                                                   double position) {
236   actor->SetDisplayExtent(extent);
237   
238   // Set position
239   if (orientation == vtkImageViewer2::SLICE_ORIENTATION_XY) {
240     if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2] > slice)
241       actor->SetPosition(0,0, position);
242     else
243       actor->SetPosition(0,0, -position);    
244   }
245   if (orientation == vtkImageViewer2::SLICE_ORIENTATION_XZ) {
246     if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1] > slice)
247       actor->SetPosition(0,position,0);
248     else
249       actor->SetPosition(0,-position,0);    
250   }
251   if (orientation == vtkImageViewer2::SLICE_ORIENTATION_YZ) {
252     if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0] > slice)
253       actor->SetPosition(position,0, 0);
254     else
255       actor->SetPosition(-position,0, 0);    
256   }
257   
258 }
259 //------------------------------------------------------------------------------
260
261
262
263