1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
19 #include "vvBinaryImageOverlayActor.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>
32 //------------------------------------------------------------------------------
33 vvBinaryImageOverlayActor::vvBinaryImageOverlayActor()
41 mColorLUT = vtkLookupTable::New();
46 //------------------------------------------------------------------------------
49 //------------------------------------------------------------------------------
50 vvBinaryImageOverlayActor::~vvBinaryImageOverlayActor()
52 for (unsigned int i = 0; i < mSlicer->GetImage()->GetVTKImages().size(); i++) {
53 mSlicer->GetRenderer()->RemoveActor(mImageActorList[i]);
55 mImageActorList.clear();
58 //------------------------------------------------------------------------------
61 //------------------------------------------------------------------------------
62 void vvBinaryImageOverlayActor::SetColor(double r, double g, double b)
68 //------------------------------------------------------------------------------
71 //------------------------------------------------------------------------------
72 void vvBinaryImageOverlayActor::SetSlicer(vvSlicer * slicer)
76 //------------------------------------------------------------------------------
79 //------------------------------------------------------------------------------
80 void vvBinaryImageOverlayActor::Initialize()
83 std::cerr << "ERROR. Please use setSlicer before setSlicer in vvBinaryImageOverlayActor." << std::endl;
88 std::cerr << "ERROR. Please use setImage before setSlicer in vvBinaryImageOverlayActor." << std::endl;
91 // Create an actor for each time slice
92 for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
93 // how many intensity ?
94 vtkImageMapToRGBA * mOverlayMapper = vtkImageMapToRGBA::New();
95 mOverlayMapper->SetInput(mImage->GetVTKImages()[0]); // DS TODO : to change if it is 4D !!!
97 // DD("change to nb of intensity");
99 mImage->GetVTKImages()[0]->GetScalarRange(range);
102 int n = range[1]-range[0]+1;
103 mColorLUT->SetRange(range[0],range[1]);
104 mColorLUT->SetNumberOfTableValues(n);
106 // Mode BG -> all is color except BG
108 for(int i=0; i<n; i++) {
109 mColorLUT->SetTableValue(i, mColor[0], mColor[1], mColor[2], mAlpha);
111 mColorLUT->SetTableValue(mBackgroundValue, 0, 0, 0, 0.0);
114 // Mode FG -> all is BG, except FG which is color
115 for(int i=0; i<n; i++) {
116 mColorLUT->SetTableValue(i, 0, 0, 0, 0.0);
118 mColorLUT->SetTableValue(mForegroundValue, mColor[0], mColor[1], mColor[2], mAlpha);
121 mOverlayMapper->SetLookupTable(mColorLUT);
123 vtkImageActor * mOverlayActor = vtkImageActor::New();
124 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
125 mOverlayActor->SetPickable(0);
126 mOverlayActor->SetVisibility(true);
127 //mOverlayActor->SetOpacity(1.0);
129 mMapperList.push_back(mOverlayMapper);
130 mImageActorList.push_back(mOverlayActor);
131 mSlicer->GetRenderer()->AddActor(mOverlayActor);
134 //------------------------------------------------------------------------------
137 //------------------------------------------------------------------------------
138 void vvBinaryImageOverlayActor::SetOpacity(double d)
142 //------------------------------------------------------------------------------
145 //------------------------------------------------------------------------------
146 // void vvBinaryImageOverlayActor::UpdateOpacity(double d) {
148 // mColorLUT->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
149 // for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
150 // // how many intensity ?
151 // vtkImageMapToRGBA * mOverlayMapper = mMapperList[numImage];
152 // mOverlayMapper->SetLookupTable(mColorLUT);
154 // vtkImageActor * mOverlayActor = mImageActorList[numImage];
155 // mOverlayActor->SetInput(mOverlayMapper->GetOutput());
158 //------------------------------------------------------------------------------
161 //------------------------------------------------------------------------------
162 // void vvBinaryImageOverlayActor::SetColor(double r, double v, double b) {
166 // // mColorLUT->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
167 // // for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
168 // // // how many intensity ?
169 // // vtkImageMapToRGBA * mOverlayMapper = mMapperList[numImage];
170 // // mOverlayMapper->SetLookupTable(mColorLUT);
172 // // vtkImageActor * mOverlayActor = mImageActorList[numImage];
173 // // mOverlayActor->SetInput(mOverlayMapper->GetOutput());
176 //------------------------------------------------------------------------------
179 //------------------------------------------------------------------------------
180 void vvBinaryImageOverlayActor::SetImage(vvImage::Pointer image, double bg, bool modeBG)
182 // DD("vvBinaryImageOverlayActor::SetImage");
186 mBackgroundValue = bg;
190 mForegroundValue = bg;
194 //------------------------------------------------------------------------------
197 //------------------------------------------------------------------------------
198 void vvBinaryImageOverlayActor::HideActors()
200 if (!mSlicer) return;
201 mSlice = mSlicer->GetSlice();
202 for(unsigned int i=0; i<mImageActorList.size(); i++) {
203 mImageActorList[i]->VisibilityOff();
205 // Caller MUST call Render
206 // mSlicer->Render();
208 //------------------------------------------------------------------------------
211 //------------------------------------------------------------------------------
212 void vvBinaryImageOverlayActor::ShowActors()
214 if (!mSlicer) return;
215 mSlice = mSlicer->GetSlice();
216 mTSlice = mSlicer->GetTSlice();
217 // for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
218 mImageActorList[mTSlice]->VisibilityOn();
219 UpdateSlice(0, mSlice);
221 // Caller MUST call Render
224 //------------------------------------------------------------------------------
227 //------------------------------------------------------------------------------
228 void vvBinaryImageOverlayActor::UpdateColor()
230 mColorLUT->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
231 for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
232 // how many intensity ?
233 vtkImageMapToRGBA * mOverlayMapper = mMapperList[numImage];
234 mOverlayMapper->SetLookupTable(mColorLUT);
236 vtkImageActor * mOverlayActor = mImageActorList[numImage];
237 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
240 //------------------------------------------------------------------------------
242 //------------------------------------------------------------------------------
243 void vvBinaryImageOverlayActor::UpdateSlice(int slicer, int slice)
245 if (!mSlicer) return;
247 if (mPreviousSlice == mSlicer->GetSlice()) {
248 if (mPreviousTSlice == mSlicer->GetTSlice()) {
249 //DD("=========== NOTHING");
250 return; // Nothing to do
255 mSlice = mSlicer->GetSlice();
256 mTSlice = mSlicer->GetTSlice();
259 int * imageExtent = mSlicer->GetExtent();
260 int orientation = mSlicer->GetOrientation();
262 ComputeExtent(orientation, mSlice, imageExtent, maskExtent);
263 //ComputeExtent(imageExtent, maskExtent, mSlicer->GetImage()->GetFirstVTKImageData(), mImage->GetFirstVTKImageData());
264 ComputeExtent(maskExtent, maskExtent, mSlicer->GetImage()->GetFirstVTKImageData(), mImage->GetFirstVTKImageData());
265 // std::cout << "maskExtent = " << maskExtent[0] << " " << maskExtent[1] << " " << maskExtent[2] << " "
266 // << maskExtent[3] << " " << maskExtent[4] << " " << maskExtent[5] << std::endl;
267 mSlicer->ClipDisplayedExtent(maskExtent, mMapperList[mTSlice]->GetInput()->GetWholeExtent());
268 // std::cout << "maskExtent = " << maskExtent[0] << " " << maskExtent[1] << " " << maskExtent[2] << " "
269 // << maskExtent[3] << " " << maskExtent[4] << " " << maskExtent[5] << std::endl;
270 SetDisplayExtentAndCameraPosition(orientation, mSlice, maskExtent, mImageActorList[mTSlice], 0.0);
272 // set previous slice
273 mPreviousTSlice = mSlicer->GetTSlice();
274 mPreviousSlice = mSlicer->GetSlice();
276 //------------------------------------------------------------------------------
279 //------------------------------------------------------------------------------
280 void vvBinaryImageOverlayActor::ComputeExtent(int orientation,
285 // std::cout << "InExtent = " << inExtent[0] << " " << inExtent[1] << " " << inExtent[2] << " "
286 // << inExtent[3] << " " << inExtent[4] << " " << inExtent[5] << std::endl;
288 switch (orientation) {
289 case vtkImageViewer2::SLICE_ORIENTATION_XY:
290 for(int i=0; i<4; i++) outExtent[i] = inExtent[i];
291 outExtent[4] = outExtent[5] = slice;
293 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
294 for(int i=0; i<2; i++) outExtent[i] = inExtent[i];
295 for(int i=4; i<6; i++) outExtent[i] = inExtent[i];
296 outExtent[2] = outExtent[3] = slice;
298 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
299 for(int i=2; i<6; i++) outExtent[i] = inExtent[i];
300 outExtent[0] = outExtent[1] = slice;
303 // std::cout << "OutExtent = " << outExtent[0] << " " << outExtent[1] << " " << outExtent[2] << " "
304 // << outExtent[3] << " " << outExtent[4] << " " << outExtent[5] << std::endl;
306 //------------------------------------------------------------------------------
309 //----------------------------------------------------------------------------
310 void vvBinaryImageOverlayActor::ComputeExtent(int * inExtent, int * outExtent, vtkImageData * image, vtkImageData * overlay)
312 outExtent[0] = (( image->GetOrigin()[0] + inExtent[0]*image->GetSpacing()[0] ) - overlay->GetOrigin()[0]) /
313 overlay->GetSpacing()[0];
314 outExtent[1] = (( image->GetOrigin()[0] + inExtent[1]*image->GetSpacing()[0] ) - overlay->GetOrigin()[0]) /
315 overlay->GetSpacing()[0];
316 outExtent[2] = (( image->GetOrigin()[1] + inExtent[2]*image->GetSpacing()[1] ) - overlay->GetOrigin()[1]) /
317 overlay->GetSpacing()[1];
318 outExtent[3] = (( image->GetOrigin()[1] + inExtent[3]*image->GetSpacing()[1] ) - overlay->GetOrigin()[1]) /
319 overlay->GetSpacing()[1];
320 outExtent[4] = (( image->GetOrigin()[2] + inExtent[4]*image->GetSpacing()[2] ) - overlay->GetOrigin()[2]) /
321 overlay->GetSpacing()[2];
322 outExtent[5] = (( image->GetOrigin()[2] + inExtent[5]*image->GetSpacing()[2] ) - overlay->GetOrigin()[2]) /
323 overlay->GetSpacing()[2];
325 //----------------------------------------------------------------------------
328 //------------------------------------------------------------------------------
329 void vvBinaryImageOverlayActor::SetDisplayExtentAndCameraPosition(int orientation,
332 vtkImageActor * actor,
336 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_XY) {
337 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2] > slice)
338 actor->SetPosition(0,0, position);
340 actor->SetPosition(0,0, -position);
342 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_XZ) {
343 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1] > slice)
344 actor->SetPosition(0,position,0);
346 actor->SetPosition(0,-position,0);
348 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_YZ) {
349 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0] > slice)
350 actor->SetPosition(position,0, 0);
352 actor->SetPosition(-position,0, 0);
354 actor->SetDisplayExtent(extent);
356 //------------------------------------------------------------------------------