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 <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 = vtkSmartPointer<vtkLookupTable>::New();
47 //------------------------------------------------------------------------------
50 //------------------------------------------------------------------------------
51 vvBinaryImageOverlayActor::~vvBinaryImageOverlayActor()
53 mImageActorList.clear();
55 //------------------------------------------------------------------------------
58 //------------------------------------------------------------------------------
59 void vvBinaryImageOverlayActor::RemoveActors()
61 for (unsigned int i = 0; i < mImageActorList.size(); i++) {
63 if (mSlicer != NULL) {
64 if (mSlicer->GetRenderer() != 0) {
65 if (mImageActorList[i] != 0) {
66 mSlicer->GetRenderer()->RemoveActor(mImageActorList[i]);
73 //------------------------------------------------------------------------------
76 //------------------------------------------------------------------------------
77 void vvBinaryImageOverlayActor::SetColor(double r, double g, double b)
83 //------------------------------------------------------------------------------
86 //------------------------------------------------------------------------------
87 void vvBinaryImageOverlayActor::SetSlicer(vvSlicer * slicer)
91 //------------------------------------------------------------------------------
94 //------------------------------------------------------------------------------
95 void vvBinaryImageOverlayActor::Initialize(bool IsVisible)
98 std::cerr << "ERROR. Please use setSlicer before setSlicer in vvBinaryImageOverlayActor." << std::endl;
103 std::cerr << "ERROR. Please use setImage before setSlicer in vvBinaryImageOverlayActor." << std::endl;
106 // Create an actor for each time slice
107 for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
108 // how many intensity ?
109 vtkSmartPointer<vtkImageMapToRGBA> mOverlayMapper = vtkSmartPointer<vtkImageMapToRGBA>::New();
110 mOverlayMapper->SetInput(mImage->GetVTKImages()[0]); // DS TODO : to change if it is 4D !!!
113 mImage->GetVTKImages()[0]->GetScalarRange(range);
114 int n = range[1]-range[0]+1;
115 mColorLUT->SetRange(range[0],range[1]);
116 mColorLUT->SetNumberOfTableValues(n);
118 // Mode BG -> all is color except BG
120 for(int i=0; i<n; i++) {
121 mColorLUT->SetTableValue(i, mColor[0], mColor[1], mColor[2], mAlpha);
123 mColorLUT->SetTableValue(mBackgroundValue, 0, 0, 0, 0.0);
126 // Mode FG -> all is BG, except FG which is color
127 for(int i=0; i<n; i++) {
128 mColorLUT->SetTableValue(i, 0, 0, 0, 0.0);
130 mColorLUT->SetTableValue(mForegroundValue, mColor[0], mColor[1], mColor[2], mAlpha);
133 mOverlayMapper->SetLookupTable(mColorLUT);
135 vtkSmartPointer<vtkImageActor> mOverlayActor = vtkSmartPointer<vtkImageActor>::New();
136 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
137 mOverlayActor->SetPickable(0);
138 mOverlayActor->SetVisibility(IsVisible);
139 //mOverlayActor->SetOpacity(1.0);
141 mMapperList.push_back(mOverlayMapper);
142 mImageActorList.push_back(mOverlayActor);
143 mSlicer->GetRenderer()->AddActor(mOverlayActor);
146 //------------------------------------------------------------------------------
149 //------------------------------------------------------------------------------
150 void vvBinaryImageOverlayActor::SetOpacity(double d)
154 //------------------------------------------------------------------------------
157 //------------------------------------------------------------------------------
158 // void vvBinaryImageOverlayActor::UpdateOpacity(double d) {
160 // mColorLUT->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
161 // for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
162 // // how many intensity ?
163 // vtkImageMapToRGBA * mOverlayMapper = mMapperList[numImage];
164 // mOverlayMapper->SetLookupTable(mColorLUT);
166 // vtkImageActor * mOverlayActor = mImageActorList[numImage];
167 // mOverlayActor->SetInput(mOverlayMapper->GetOutput());
170 //------------------------------------------------------------------------------
173 //------------------------------------------------------------------------------
174 // void vvBinaryImageOverlayActor::SetColor(double r, double v, double b) {
178 // // mColorLUT->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
179 // // for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
180 // // // how many intensity ?
181 // // vtkImageMapToRGBA * mOverlayMapper = mMapperList[numImage];
182 // // mOverlayMapper->SetLookupTable(mColorLUT);
184 // // vtkImageActor * mOverlayActor = mImageActorList[numImage];
185 // // mOverlayActor->SetInput(mOverlayMapper->GetOutput());
188 //------------------------------------------------------------------------------
191 //------------------------------------------------------------------------------
192 void vvBinaryImageOverlayActor::SetImage(vvImage::Pointer image, double bg, bool modeBG)
196 mBackgroundValue = bg;
200 mForegroundValue = bg;
204 //------------------------------------------------------------------------------
207 //------------------------------------------------------------------------------
208 void vvBinaryImageOverlayActor::HideActors()
210 if (!mSlicer) return;
211 mSlice = mSlicer->GetSlice();
212 for(unsigned int i=0; i<mImageActorList.size(); i++) {
213 mImageActorList[i]->VisibilityOff();
215 // Caller MUST call Render
216 // mSlicer->Render();
218 //------------------------------------------------------------------------------
221 //------------------------------------------------------------------------------
222 void vvBinaryImageOverlayActor::ShowActors()
224 if (!mSlicer) return;
225 mSlice = mSlicer->GetSlice();
226 mTSlice = mSlicer->GetTSlice();
227 // for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
228 mImageActorList[mTSlice]->VisibilityOn();
229 UpdateSlice(0, mSlice);
231 // Caller MUST call Render
234 //------------------------------------------------------------------------------
237 //------------------------------------------------------------------------------
238 void vvBinaryImageOverlayActor::UpdateColor()
240 mColorLUT->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
241 for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
242 // how many intensity ?
243 vtkImageMapToRGBA * mOverlayMapper = mMapperList[numImage];
244 mOverlayMapper->SetLookupTable(mColorLUT);
246 vtkImageActor * mOverlayActor = mImageActorList[numImage];
247 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
250 //------------------------------------------------------------------------------
252 //------------------------------------------------------------------------------
253 void vvBinaryImageOverlayActor::UpdateSlice(int slicer, int slice, bool force)
255 if (!mSlicer) return;
258 if (mPreviousSlice == mSlicer->GetSlice()) {
259 if (mPreviousTSlice == mSlicer->GetTSlice()) {
260 return; // Nothing to do
266 mSlice = mSlicer->GetSlice();
267 mTSlice = mSlicer->GetTSlice();
270 int * imageExtent = mSlicer->GetExtent();
271 int orientation = mSlicer->GetOrientation();
273 ComputeExtent(orientation, mSlice, imageExtent, maskExtent);
274 ComputeExtent(maskExtent, maskExtent, mSlicer->GetImage()->GetFirstVTKImageData(), mImage->GetFirstVTKImageData());
275 mSlicer->ClipDisplayedExtent(maskExtent, mMapperList[mTSlice]->GetInput()->GetWholeExtent());
276 SetDisplayExtentAndCameraPosition(orientation, mSlice, maskExtent, mImageActorList[mTSlice], mDepth);
278 // set previous slice
279 mPreviousTSlice = mSlicer->GetTSlice();
280 mPreviousSlice = mSlicer->GetSlice();
282 //------------------------------------------------------------------------------
285 //------------------------------------------------------------------------------
286 void vvBinaryImageOverlayActor::ComputeExtent(int orientation,
291 switch (orientation) {
292 case vtkImageViewer2::SLICE_ORIENTATION_XY:
293 for(int i=0; i<4; i++) outExtent[i] = inExtent[i];
294 outExtent[4] = outExtent[5] = slice;
296 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
297 for(int i=0; i<2; i++) outExtent[i] = inExtent[i];
298 for(int i=4; i<6; i++) outExtent[i] = inExtent[i];
299 outExtent[2] = outExtent[3] = slice;
301 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
302 for(int i=2; i<6; i++) outExtent[i] = inExtent[i];
303 outExtent[0] = outExtent[1] = slice;
307 //------------------------------------------------------------------------------
310 //----------------------------------------------------------------------------
311 void vvBinaryImageOverlayActor::ComputeExtent(int * inExtent, int * outExtent, vtkImageData * image, vtkImageData * overlay)
313 outExtent[0] = (int)lrint(((image->GetOrigin()[0] + inExtent[0]*image->GetSpacing()[0]) - overlay->GetOrigin()[0]) / overlay->GetSpacing()[0]);
314 outExtent[1] = (int)lrint(((image->GetOrigin()[0] + inExtent[1]*image->GetSpacing()[0]) - overlay->GetOrigin()[0]) / overlay->GetSpacing()[0]);
315 outExtent[2] = (int)lrint(((image->GetOrigin()[1] + inExtent[2]*image->GetSpacing()[1]) - overlay->GetOrigin()[1]) / overlay->GetSpacing()[1]);
316 outExtent[3] = (int)lrint(((image->GetOrigin()[1] + inExtent[3]*image->GetSpacing()[1]) - overlay->GetOrigin()[1]) / overlay->GetSpacing()[1]);
317 outExtent[4] = (int)lrint(((image->GetOrigin()[2] + inExtent[4]*image->GetSpacing()[2]) - overlay->GetOrigin()[2]) / overlay->GetSpacing()[2]);
318 outExtent[5] = (int)lrint(((image->GetOrigin()[2] + inExtent[5]*image->GetSpacing()[2]) - overlay->GetOrigin()[2]) / overlay->GetSpacing()[2]);
320 //----------------------------------------------------------------------------
323 //------------------------------------------------------------------------------
324 void vvBinaryImageOverlayActor::SetDisplayExtentAndCameraPosition(int orientation,
327 vtkImageActor * actor,
331 Error according to camera orientation
335 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_XY) {
336 //if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2] > slice)
337 actor->SetPosition(0,0, -position);
339 //actor->SetPosition(0,0, position);
341 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_XZ) {
342 //if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1] > slice)
343 // actor->SetPosition(0,position,0);
345 actor->SetPosition(0,-position,0);
347 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_YZ) {
348 //if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0] > slice)
349 // actor->SetPosition(position,0, 0);
351 actor->SetPosition(-position,0, 0);
353 actor->SetDisplayExtent(extent);
355 //------------------------------------------------------------------------------