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 = vtkSmartPointer<vtkLookupTable>::New();
46 //------------------------------------------------------------------------------
49 //------------------------------------------------------------------------------
50 vvBinaryImageOverlayActor::~vvBinaryImageOverlayActor()
52 for (unsigned int i = 0; i < mImageActorList.size(); i++) {
53 mSlicer->GetRenderer()->RemoveActor(mImageActorList[i]);
56 //------------------------------------------------------------------------------
59 //------------------------------------------------------------------------------
60 void vvBinaryImageOverlayActor::SetColor(double r, double g, double b)
66 //------------------------------------------------------------------------------
69 //------------------------------------------------------------------------------
70 void vvBinaryImageOverlayActor::SetSlicer(vvSlicer * slicer)
74 //------------------------------------------------------------------------------
77 //------------------------------------------------------------------------------
78 void vvBinaryImageOverlayActor::Initialize(bool IsVisible)
81 std::cerr << "ERROR. Please use setSlicer before setSlicer in vvBinaryImageOverlayActor." << std::endl;
86 std::cerr << "ERROR. Please use setImage before setSlicer in vvBinaryImageOverlayActor." << std::endl;
89 // Create an actor for each time slice
90 for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
91 // how many intensity ?
92 vtkSmartPointer<vtkImageMapToRGBA> mOverlayMapper = vtkSmartPointer<vtkImageMapToRGBA>::New();
93 mOverlayMapper->SetInput(mImage->GetVTKImages()[0]); // DS TODO : to change if it is 4D !!!
96 mImage->GetVTKImages()[0]->GetScalarRange(range);
97 int n = range[1]-range[0]+1;
98 mColorLUT->SetRange(range[0],range[1]);
99 mColorLUT->SetNumberOfTableValues(n);
101 // Mode BG -> all is color except BG
103 for(int i=0; i<n; i++) {
104 mColorLUT->SetTableValue(i, mColor[0], mColor[1], mColor[2], mAlpha);
106 mColorLUT->SetTableValue(mBackgroundValue, 0, 0, 0, 0.0);
109 // Mode FG -> all is BG, except FG which is color
110 for(int i=0; i<n; i++) {
111 mColorLUT->SetTableValue(i, 0, 0, 0, 0.0);
113 mColorLUT->SetTableValue(mForegroundValue, mColor[0], mColor[1], mColor[2], mAlpha);
116 mOverlayMapper->SetLookupTable(mColorLUT);
118 vtkSmartPointer<vtkImageActor> mOverlayActor = vtkSmartPointer<vtkImageActor>::New();
119 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
120 mOverlayActor->SetPickable(0);
121 mOverlayActor->SetVisibility(IsVisible);
122 //mOverlayActor->SetOpacity(1.0);
124 mMapperList.push_back(mOverlayMapper);
125 mImageActorList.push_back(mOverlayActor);
126 mSlicer->GetRenderer()->AddActor(mOverlayActor);
129 //------------------------------------------------------------------------------
132 //------------------------------------------------------------------------------
133 void vvBinaryImageOverlayActor::SetOpacity(double d)
137 //------------------------------------------------------------------------------
140 //------------------------------------------------------------------------------
141 // void vvBinaryImageOverlayActor::UpdateOpacity(double d) {
143 // mColorLUT->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
144 // for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
145 // // how many intensity ?
146 // vtkImageMapToRGBA * mOverlayMapper = mMapperList[numImage];
147 // mOverlayMapper->SetLookupTable(mColorLUT);
149 // vtkImageActor * mOverlayActor = mImageActorList[numImage];
150 // mOverlayActor->SetInput(mOverlayMapper->GetOutput());
153 //------------------------------------------------------------------------------
156 //------------------------------------------------------------------------------
157 // void vvBinaryImageOverlayActor::SetColor(double r, double v, double b) {
161 // // mColorLUT->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
162 // // for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
163 // // // how many intensity ?
164 // // vtkImageMapToRGBA * mOverlayMapper = mMapperList[numImage];
165 // // mOverlayMapper->SetLookupTable(mColorLUT);
167 // // vtkImageActor * mOverlayActor = mImageActorList[numImage];
168 // // mOverlayActor->SetInput(mOverlayMapper->GetOutput());
171 //------------------------------------------------------------------------------
174 //------------------------------------------------------------------------------
175 void vvBinaryImageOverlayActor::SetImage(vvImage * image, double bg, bool modeBG)
179 mBackgroundValue = bg;
183 mForegroundValue = bg;
187 //------------------------------------------------------------------------------
190 //------------------------------------------------------------------------------
191 void vvBinaryImageOverlayActor::HideActors()
193 if (!mSlicer) return;
194 mSlice = mSlicer->GetSlice();
195 for(unsigned int i=0; i<mImageActorList.size(); i++) {
196 mImageActorList[i]->VisibilityOff();
198 // Caller MUST call Render
199 // mSlicer->Render();
201 //------------------------------------------------------------------------------
204 //------------------------------------------------------------------------------
205 void vvBinaryImageOverlayActor::ShowActors()
207 if (!mSlicer) return;
208 mSlice = mSlicer->GetSlice();
209 mTSlice = mSlicer->GetTSlice();
210 // for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
211 mImageActorList[mTSlice]->VisibilityOn();
212 UpdateSlice(0, mSlice);
214 // Caller MUST call Render
217 //------------------------------------------------------------------------------
220 //------------------------------------------------------------------------------
221 void vvBinaryImageOverlayActor::UpdateColor()
223 mColorLUT->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
224 for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
225 // how many intensity ?
226 vtkImageMapToRGBA * mOverlayMapper = mMapperList[numImage];
227 mOverlayMapper->SetLookupTable(mColorLUT);
229 vtkImageActor * mOverlayActor = mImageActorList[numImage];
230 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
233 //------------------------------------------------------------------------------
235 //------------------------------------------------------------------------------
236 void vvBinaryImageOverlayActor::UpdateSlice(int slicer, int slice)
238 if (!mSlicer) return;
240 if (mPreviousSlice == mSlicer->GetSlice()) {
241 if (mPreviousTSlice == mSlicer->GetTSlice()) {
242 //DD("=========== NOTHING");
243 return; // Nothing to do
248 mSlice = mSlicer->GetSlice();
249 mTSlice = mSlicer->GetTSlice();
252 int * imageExtent = mSlicer->GetExtent();
253 int orientation = mSlicer->GetOrientation();
255 ComputeExtent(orientation, mSlice, imageExtent, maskExtent);
256 //ComputeExtent(imageExtent, maskExtent, mSlicer->GetImage()->GetFirstVTKImageData(), mImage->GetFirstVTKImageData());
257 ComputeExtent(maskExtent, maskExtent, mSlicer->GetImage()->GetFirstVTKImageData(), mImage->GetFirstVTKImageData());
258 // std::cout << "maskExtent = " << maskExtent[0] << " " << maskExtent[1] << " " << maskExtent[2] << " "
259 // << maskExtent[3] << " " << maskExtent[4] << " " << maskExtent[5] << std::endl;
260 mSlicer->ClipDisplayedExtent(maskExtent, mMapperList[mTSlice]->GetInput()->GetWholeExtent());
261 // std::cout << "maskExtent = " << maskExtent[0] << " " << maskExtent[1] << " " << maskExtent[2] << " "
262 // << maskExtent[3] << " " << maskExtent[4] << " " << maskExtent[5] << std::endl;
263 SetDisplayExtentAndCameraPosition(orientation, mSlice, maskExtent, mImageActorList[mTSlice], 0.0);
265 // set previous slice
266 mPreviousTSlice = mSlicer->GetTSlice();
267 mPreviousSlice = mSlicer->GetSlice();
269 //------------------------------------------------------------------------------
272 //------------------------------------------------------------------------------
273 void vvBinaryImageOverlayActor::ComputeExtent(int orientation,
278 switch (orientation) {
279 case vtkImageViewer2::SLICE_ORIENTATION_XY:
280 for(int i=0; i<4; i++) outExtent[i] = inExtent[i];
281 outExtent[4] = outExtent[5] = slice;
283 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
284 for(int i=0; i<2; i++) outExtent[i] = inExtent[i];
285 for(int i=4; i<6; i++) outExtent[i] = inExtent[i];
286 outExtent[2] = outExtent[3] = slice;
288 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
289 for(int i=2; i<6; i++) outExtent[i] = inExtent[i];
290 outExtent[0] = outExtent[1] = slice;
294 //------------------------------------------------------------------------------
297 //----------------------------------------------------------------------------
298 void vvBinaryImageOverlayActor::ComputeExtent(int * inExtent, int * outExtent, vtkImageData * image, vtkImageData * overlay)
300 outExtent[0] = (( image->GetOrigin()[0] + inExtent[0]*image->GetSpacing()[0] ) - overlay->GetOrigin()[0]) /
301 overlay->GetSpacing()[0];
302 outExtent[1] = (( image->GetOrigin()[0] + inExtent[1]*image->GetSpacing()[0] ) - overlay->GetOrigin()[0]) /
303 overlay->GetSpacing()[0];
304 outExtent[2] = (( image->GetOrigin()[1] + inExtent[2]*image->GetSpacing()[1] ) - overlay->GetOrigin()[1]) /
305 overlay->GetSpacing()[1];
306 outExtent[3] = (( image->GetOrigin()[1] + inExtent[3]*image->GetSpacing()[1] ) - overlay->GetOrigin()[1]) /
307 overlay->GetSpacing()[1];
308 outExtent[4] = (( image->GetOrigin()[2] + inExtent[4]*image->GetSpacing()[2] ) - overlay->GetOrigin()[2]) /
309 overlay->GetSpacing()[2];
310 outExtent[5] = (( image->GetOrigin()[2] + inExtent[5]*image->GetSpacing()[2] ) - overlay->GetOrigin()[2]) /
311 overlay->GetSpacing()[2];
313 //----------------------------------------------------------------------------
316 //------------------------------------------------------------------------------
317 void vvBinaryImageOverlayActor::SetDisplayExtentAndCameraPosition(int orientation,
320 vtkImageActor * actor,
324 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_XY) {
325 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2] > slice)
326 actor->SetPosition(0,0, position);
328 actor->SetPosition(0,0, -position);
330 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_XZ) {
331 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1] > slice)
332 actor->SetPosition(0,position,0);
334 actor->SetPosition(0,-position,0);
336 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_YZ) {
337 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0] > slice)
338 actor->SetPosition(position,0, 0);
340 actor->SetPosition(-position,0, 0);
342 actor->SetDisplayExtent(extent);
344 //------------------------------------------------------------------------------