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://www.centreleonberard.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 <vtkVersion.h>
22 #include <vtkStreamingDemandDrivenPipeline.h>
23 #include <vtkInformation.h>
24 #include <vtkImageActor.h>
25 #include <vtkCamera.h>
26 #include <vtkRenderer.h>
27 #include <vtkMarchingSquares.h>
28 #include <vtkImageClip.h>
29 #include <vtkImageData.h>
30 #include <vtkPolyDataMapper.h>
31 #include <vtkProperty.h>
32 #include <vtkImageMapToRGBA.h>
33 #include <vtkLookupTable.h>
34 #include <vtkImageMapper3D.h>
36 //------------------------------------------------------------------------------
37 vvBinaryImageOverlayActor::vvBinaryImageOverlayActor()
45 mColorLUT = vtkSmartPointer<vtkLookupTable>::New();
51 //------------------------------------------------------------------------------
54 //------------------------------------------------------------------------------
55 vvBinaryImageOverlayActor::~vvBinaryImageOverlayActor()
57 mImageActorList.clear();
59 //------------------------------------------------------------------------------
62 //------------------------------------------------------------------------------
63 void vvBinaryImageOverlayActor::RemoveActors()
65 for (unsigned int i = 0; i < mImageActorList.size(); i++) {
67 if (mSlicer != NULL) {
68 if (mSlicer->GetRenderer() != 0) {
69 if (mImageActorList[i] != 0) {
70 mSlicer->GetRenderer()->RemoveActor(mImageActorList[i]);
77 //------------------------------------------------------------------------------
80 //------------------------------------------------------------------------------
81 void vvBinaryImageOverlayActor::SetColor(double r, double g, double b)
87 //------------------------------------------------------------------------------
90 //------------------------------------------------------------------------------
91 void vvBinaryImageOverlayActor::SetSlicer(vvSlicer * slicer)
95 //------------------------------------------------------------------------------
98 //------------------------------------------------------------------------------
99 void vvBinaryImageOverlayActor::Initialize(bool IsVisible)
102 std::cerr << "ERROR. Please use setSlicer before setSlicer in vvBinaryImageOverlayActor." << std::endl;
107 std::cerr << "ERROR. Please use setImage before setSlicer in vvBinaryImageOverlayActor." << std::endl;
110 // Create an actor for each time slice
111 for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
112 // how many intensity ?
113 vtkSmartPointer<vtkImageMapToRGBA> mOverlayMapper = vtkSmartPointer<vtkImageMapToRGBA>::New();
114 if (mImage->IsTimeSequence())
116 #if VTK_MAJOR_VERSION <= 5
117 mOverlayMapper->SetInput(mImage->GetVTKImages()[numImage]);
119 mOverlayMapper->SetInputData(mImage->GetVTKImages()[numImage]);
123 #if VTK_MAJOR_VERSION <= 5
124 mOverlayMapper->SetInput(mImage->GetVTKImages()[0]);
126 mOverlayMapper->SetInputData(mImage->GetVTKImages()[0]);
131 if (mImage->IsTimeSequence())
133 mImage->GetVTKImages()[numImage]->GetScalarRange(range);
136 mImage->GetVTKImages()[0]->GetScalarRange(range);
138 int n = range[1]-range[0]+1;
139 mColorLUT->SetRange(range[0],range[1]);
140 mColorLUT->SetNumberOfTableValues(n);
142 // Mode BG -> all is color except BG
144 for(int i=0; i<n; i++) {
145 mColorLUT->SetTableValue(i, mColor[0], mColor[1], mColor[2], mAlpha);
147 mColorLUT->SetTableValue(mBackgroundValue, 0, 0, 0, 0.0);
150 // Mode FG -> all is BG, except FG which is color
151 for(int i=0; i<n; i++) {
152 mColorLUT->SetTableValue(i, 0, 0, 0, 0.0);
154 mColorLUT->SetTableValue(mForegroundValue, mColor[0], mColor[1], mColor[2], mAlpha);
157 mOverlayMapper->SetLookupTable(mColorLUT);
159 vtkSmartPointer<vtkImageActor> mOverlayActor = vtkSmartPointer<vtkImageActor>::New();
160 #if VTK_MAJOR_VERSION <= 5
161 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
163 mOverlayActor->GetMapper()->SetInputConnection(mOverlayMapper->GetOutputPort());
165 mOverlayActor->SetPickable(0);
166 mOverlayActor->SetVisibility(IsVisible);
167 //mOverlayActor->SetOpacity(1.0);
169 // FIXME : by default overlay is not interpolated.
170 // mOverlayActor->SetInterpolate(mSlicer->GetImageActor()->GetInterpolate());
171 mOverlayActor->InterpolateOff();
173 mMapperList.push_back(mOverlayMapper);
174 mImageActorList.push_back(mOverlayActor);
175 mSlicer->GetRenderer()->AddActor(mImageActorList[numImage]);
178 //------------------------------------------------------------------------------
181 //------------------------------------------------------------------------------
182 void vvBinaryImageOverlayActor::SetOpacity(double d)
186 //------------------------------------------------------------------------------
189 //------------------------------------------------------------------------------
190 // void vvBinaryImageOverlayActor::UpdateOpacity(double d) {
192 // mColorLUT->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
193 // for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
194 // // how many intensity ?
195 // vtkImageMapToRGBA * mOverlayMapper = mMapperList[numImage];
196 // mOverlayMapper->SetLookupTable(mColorLUT);
198 // vtkImageActor * mOverlayActor = mImageActorList[numImage];
199 // mOverlayActor->SetInput(mOverlayMapper->GetOutput());
202 //------------------------------------------------------------------------------
205 //------------------------------------------------------------------------------
206 // void vvBinaryImageOverlayActor::SetColor(double r, double v, double b) {
210 // // mColorLUT->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
211 // // for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
212 // // // how many intensity ?
213 // // vtkImageMapToRGBA * mOverlayMapper = mMapperList[numImage];
214 // // mOverlayMapper->SetLookupTable(mColorLUT);
216 // // vtkImageActor * mOverlayActor = mImageActorList[numImage];
217 // // mOverlayActor->SetInput(mOverlayMapper->GetOutput());
220 //------------------------------------------------------------------------------
223 //------------------------------------------------------------------------------
224 void vvBinaryImageOverlayActor::SetImage(vvImage::Pointer image, double bg, bool modeBG)
228 mBackgroundValue = bg;
232 mForegroundValue = bg;
236 //------------------------------------------------------------------------------
239 //------------------------------------------------------------------------------
240 void vvBinaryImageOverlayActor::HideActors()
242 if (!mSlicer) return;
243 mSlice = mSlicer->GetSlice();
244 for(unsigned int i=0; i<mImageActorList.size(); i++) {
245 mImageActorList[i]->VisibilityOff();
247 // Caller MUST call Render
248 // mSlicer->Render();
250 //------------------------------------------------------------------------------
253 //------------------------------------------------------------------------------
254 void vvBinaryImageOverlayActor::ShowActors()
256 if (!mSlicer) return;
257 mSlice = mSlicer->GetSlice();
258 mTSlice = mSlicer->GetTSlice();
259 mImageActorList[mTSlice]->VisibilityOn();
260 UpdateSlice(0, mSlice);
261 // Caller MUST call Render
264 //------------------------------------------------------------------------------
267 //------------------------------------------------------------------------------
268 void vvBinaryImageOverlayActor::UpdateColor()
270 mColorLUT->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
271 for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
272 // how many intensity ?
273 vtkImageMapToRGBA * mOverlayMapper = mMapperList[numImage];
274 mOverlayMapper->SetLookupTable(mColorLUT);
276 vtkImageActor * mOverlayActor = mImageActorList[numImage];
277 #if VTK_MAJOR_VERSION <= 5
278 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
280 mOverlayActor->SetInputData(mOverlayMapper->GetOutput());
284 //------------------------------------------------------------------------------
286 //------------------------------------------------------------------------------
287 void vvBinaryImageOverlayActor::UpdateSlice(int slicer, int slice, bool force)
289 if (!mSlicer) return;
292 if (mPreviousSlice == mSlicer->GetSlice()) {
293 if (mPreviousTSlice == mSlicer->GetTSlice()) {
294 return; // Nothing to do
300 mSlice = mSlicer->GetSlice();
301 mTSlice = mSlicer->GetTSlice();
304 int * imageExtent = mSlicer->GetExtent();
305 int orientation = mSlicer->GetOrientation();
307 ComputeExtent(orientation, mSlice, imageExtent, maskExtent);
308 ComputeExtent(maskExtent, maskExtent, mSlicer->GetImage()->GetVTKImages()[mTSlice], mImage->GetVTKImages()[mTSlice]);
309 #if VTK_MAJOR_VERSION <= 5
310 mSlicer->ClipDisplayedExtent(maskExtent, mMapperList[mTSlice]->GetInput()->GetWholeExtent());
312 mSlicer->ClipDisplayedExtent(maskExtent, mMapperList[mTSlice]->GetInput()->GetInformation()->Get(vtkDataObject::DATA_EXTENT()));
315 mImageActorList[mTSlice]->VisibilityOn();
316 SetDisplayExtentAndCameraPosition(orientation, mSlice, maskExtent, mImageActorList[mTSlice], mDepth);
318 // set previous slice
319 mPreviousTSlice = mSlicer->GetTSlice();
320 mPreviousSlice = mSlicer->GetSlice();
322 //------------------------------------------------------------------------------
325 //------------------------------------------------------------------------------
326 void vvBinaryImageOverlayActor::ComputeExtent(int orientation,
331 switch (orientation) {
332 case vtkImageViewer2::SLICE_ORIENTATION_XY:
333 for(int i=0; i<4; i++) outExtent[i] = inExtent[i];
334 outExtent[4] = outExtent[5] = slice;
336 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
337 for(int i=0; i<2; i++) outExtent[i] = inExtent[i];
338 for(int i=4; i<6; i++) outExtent[i] = inExtent[i];
339 outExtent[2] = outExtent[3] = slice;
341 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
342 for(int i=2; i<6; i++) outExtent[i] = inExtent[i];
343 outExtent[0] = outExtent[1] = slice;
347 //------------------------------------------------------------------------------
350 //----------------------------------------------------------------------------
351 void vvBinaryImageOverlayActor::ComputeExtent(int * inExtent, int * outExtent, vtkImageData * image, vtkImageData * overlay)
353 for(int i=0; i<3; i++) {
354 double a = (image->GetOrigin()[i] + inExtent[i*2]*image->GetSpacing()[i] -
355 overlay->GetOrigin()[i]) / overlay->GetSpacing()[i];
356 double b = (image->GetOrigin()[i] + inExtent[i*2+1]*image->GetSpacing()[i] -
357 overlay->GetOrigin()[i]) / overlay->GetSpacing()[i];
358 outExtent[i*2] = lrint(a);
359 outExtent[i*2+1] = lrint(b);
362 /* // FIXME (original)
363 outExtent[0] = (int)lrint(((image->GetOrigin()[0] + inExtent[0]*image->GetSpacing()[0]) - overlay->GetOrigin()[0]) / overlay->GetSpacing()[0]);
364 outExtent[1] = (int)lrint(((image->GetOrigin()[0] + inExtent[1]*image->GetSpacing()[0]) - overlay->GetOrigin()[0]) / overlay->GetSpacing()[0]);
365 outExtent[2] = (int)lrint(((image->GetOrigin()[1] + inExtent[2]*image->GetSpacing()[1]) - overlay->GetOrigin()[1]) / overlay->GetSpacing()[1]);
366 outExtent[3] = (int)lrint(((image->GetOrigin()[1] + inExtent[3]*image->GetSpacing()[1]) - overlay->GetOrigin()[1]) / overlay->GetSpacing()[1]);
367 outExtent[4] = (int)lrint(((image->GetOrigin()[2] + inExtent[4]*image->GetSpacing()[2]) - overlay->GetOrigin()[2]) / overlay->GetSpacing()[2]);
368 outExtent[5] = (int)lrint(((image->GetOrigin()[2] + inExtent[5]*image->GetSpacing()[2]) - overlay->GetOrigin()[2]) / overlay->GetSpacing()[2]);
371 //----------------------------------------------------------------------------
374 //------------------------------------------------------------------------------
375 void vvBinaryImageOverlayActor::SetDisplayExtentAndCameraPosition(int orientation,
378 vtkImageActor * actor,
382 Error according to camera orientation
386 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_XY) {
387 //if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2] > slice)
388 actor->SetPosition(0,0, -position);
390 //actor->SetPosition(0,0, position);
392 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_XZ) {
393 //if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1] > slice)
394 // actor->SetPosition(0,position,0);
396 actor->SetPosition(0,-position,0);
398 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_YZ) {
399 //if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0] > slice)
400 // actor->SetPosition(position,0, 0);
402 actor->SetPosition(-position,0, 0);
404 actor->SetDisplayExtent(extent);
406 //------------------------------------------------------------------------------