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 for (unsigned int i = 0; i < mImageActorList.size(); i++) {
54 mSlicer->GetRenderer()->RemoveActor(mImageActorList[i]);
57 //------------------------------------------------------------------------------
60 //------------------------------------------------------------------------------
61 void vvBinaryImageOverlayActor::SetColor(double r, double g, double b)
67 //------------------------------------------------------------------------------
70 //------------------------------------------------------------------------------
71 void vvBinaryImageOverlayActor::SetSlicer(vvSlicer * slicer)
75 //------------------------------------------------------------------------------
78 //------------------------------------------------------------------------------
79 void vvBinaryImageOverlayActor::Initialize(bool IsVisible)
82 std::cerr << "ERROR. Please use setSlicer before setSlicer in vvBinaryImageOverlayActor." << std::endl;
87 std::cerr << "ERROR. Please use setImage before setSlicer in vvBinaryImageOverlayActor." << std::endl;
90 // Create an actor for each time slice
91 for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
92 // how many intensity ?
93 vtkSmartPointer<vtkImageMapToRGBA> mOverlayMapper = vtkSmartPointer<vtkImageMapToRGBA>::New();
94 mOverlayMapper->SetInput(mImage->GetVTKImages()[0]); // DS TODO : to change if it is 4D !!!
97 mImage->GetVTKImages()[0]->GetScalarRange(range);
98 int n = range[1]-range[0]+1;
99 mColorLUT->SetRange(range[0],range[1]);
100 mColorLUT->SetNumberOfTableValues(n);
102 // Mode BG -> all is color except BG
104 for(int i=0; i<n; i++) {
105 mColorLUT->SetTableValue(i, mColor[0], mColor[1], mColor[2], mAlpha);
107 mColorLUT->SetTableValue(mBackgroundValue, 0, 0, 0, 0.0);
110 // Mode FG -> all is BG, except FG which is color
111 for(int i=0; i<n; i++) {
112 mColorLUT->SetTableValue(i, 0, 0, 0, 0.0);
114 mColorLUT->SetTableValue(mForegroundValue, mColor[0], mColor[1], mColor[2], mAlpha);
117 mOverlayMapper->SetLookupTable(mColorLUT);
119 vtkSmartPointer<vtkImageActor> mOverlayActor = vtkSmartPointer<vtkImageActor>::New();
120 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
121 mOverlayActor->SetPickable(0);
122 mOverlayActor->SetVisibility(IsVisible);
123 //mOverlayActor->SetOpacity(1.0);
125 mMapperList.push_back(mOverlayMapper);
126 mImageActorList.push_back(mOverlayActor);
127 mSlicer->GetRenderer()->AddActor(mOverlayActor);
130 //------------------------------------------------------------------------------
133 //------------------------------------------------------------------------------
134 void vvBinaryImageOverlayActor::SetOpacity(double d)
138 //------------------------------------------------------------------------------
141 //------------------------------------------------------------------------------
142 // void vvBinaryImageOverlayActor::UpdateOpacity(double d) {
144 // mColorLUT->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
145 // for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
146 // // how many intensity ?
147 // vtkImageMapToRGBA * mOverlayMapper = mMapperList[numImage];
148 // mOverlayMapper->SetLookupTable(mColorLUT);
150 // vtkImageActor * mOverlayActor = mImageActorList[numImage];
151 // mOverlayActor->SetInput(mOverlayMapper->GetOutput());
154 //------------------------------------------------------------------------------
157 //------------------------------------------------------------------------------
158 // void vvBinaryImageOverlayActor::SetColor(double r, double v, double b) {
162 // // mColorLUT->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
163 // // for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
164 // // // how many intensity ?
165 // // vtkImageMapToRGBA * mOverlayMapper = mMapperList[numImage];
166 // // mOverlayMapper->SetLookupTable(mColorLUT);
168 // // vtkImageActor * mOverlayActor = mImageActorList[numImage];
169 // // mOverlayActor->SetInput(mOverlayMapper->GetOutput());
172 //------------------------------------------------------------------------------
175 //------------------------------------------------------------------------------
176 void vvBinaryImageOverlayActor::SetImage(vvImage * image, double bg, bool modeBG)
180 mBackgroundValue = bg;
184 mForegroundValue = bg;
188 //------------------------------------------------------------------------------
191 //------------------------------------------------------------------------------
192 void vvBinaryImageOverlayActor::HideActors()
194 if (!mSlicer) return;
195 mSlice = mSlicer->GetSlice();
196 for(unsigned int i=0; i<mImageActorList.size(); i++) {
197 mImageActorList[i]->VisibilityOff();
199 // Caller MUST call Render
200 // mSlicer->Render();
202 //------------------------------------------------------------------------------
205 //------------------------------------------------------------------------------
206 void vvBinaryImageOverlayActor::ShowActors()
208 if (!mSlicer) return;
209 mSlice = mSlicer->GetSlice();
210 mTSlice = mSlicer->GetTSlice();
211 // for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
212 mImageActorList[mTSlice]->VisibilityOn();
213 UpdateSlice(0, mSlice);
215 // Caller MUST call Render
218 //------------------------------------------------------------------------------
221 //------------------------------------------------------------------------------
222 void vvBinaryImageOverlayActor::UpdateColor()
224 mColorLUT->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
225 for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
226 // how many intensity ?
227 vtkImageMapToRGBA * mOverlayMapper = mMapperList[numImage];
228 mOverlayMapper->SetLookupTable(mColorLUT);
230 vtkImageActor * mOverlayActor = mImageActorList[numImage];
231 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
234 //------------------------------------------------------------------------------
236 //------------------------------------------------------------------------------
237 void vvBinaryImageOverlayActor::UpdateSlice(int slicer, int slice, bool force)
239 if (!mSlicer) return;
242 if (mPreviousSlice == mSlicer->GetSlice()) {
243 if (mPreviousTSlice == mSlicer->GetTSlice()) {
244 return; // Nothing to do
250 mSlice = mSlicer->GetSlice();
251 mTSlice = mSlicer->GetTSlice();
254 int * imageExtent = mSlicer->GetExtent();
255 int orientation = mSlicer->GetOrientation();
257 ComputeExtent(orientation, mSlice, imageExtent, maskExtent);
258 ComputeExtent(maskExtent, maskExtent, mSlicer->GetImage()->GetFirstVTKImageData(), mImage->GetFirstVTKImageData());
259 mSlicer->ClipDisplayedExtent(maskExtent, mMapperList[mTSlice]->GetInput()->GetWholeExtent());
260 SetDisplayExtentAndCameraPosition(orientation, mSlice, maskExtent, mImageActorList[mTSlice], mDepth);
262 // set previous slice
263 mPreviousTSlice = mSlicer->GetTSlice();
264 mPreviousSlice = mSlicer->GetSlice();
266 //------------------------------------------------------------------------------
269 //------------------------------------------------------------------------------
270 void vvBinaryImageOverlayActor::ComputeExtent(int orientation,
275 switch (orientation) {
276 case vtkImageViewer2::SLICE_ORIENTATION_XY:
277 for(int i=0; i<4; i++) outExtent[i] = inExtent[i];
278 outExtent[4] = outExtent[5] = slice;
280 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
281 for(int i=0; i<2; i++) outExtent[i] = inExtent[i];
282 for(int i=4; i<6; i++) outExtent[i] = inExtent[i];
283 outExtent[2] = outExtent[3] = slice;
285 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
286 for(int i=2; i<6; i++) outExtent[i] = inExtent[i];
287 outExtent[0] = outExtent[1] = slice;
291 //------------------------------------------------------------------------------
294 //----------------------------------------------------------------------------
295 void vvBinaryImageOverlayActor::ComputeExtent(int * inExtent, int * outExtent, vtkImageData * image, vtkImageData * overlay)
297 outExtent[0] = (int)lrint(((image->GetOrigin()[0] + inExtent[0]*image->GetSpacing()[0]) - overlay->GetOrigin()[0]) / overlay->GetSpacing()[0]);
298 outExtent[1] = (int)lrint(((image->GetOrigin()[0] + inExtent[1]*image->GetSpacing()[0]) - overlay->GetOrigin()[0]) / overlay->GetSpacing()[0]);
299 outExtent[2] = (int)lrint(((image->GetOrigin()[1] + inExtent[2]*image->GetSpacing()[1]) - overlay->GetOrigin()[1]) / overlay->GetSpacing()[1]);
300 outExtent[3] = (int)lrint(((image->GetOrigin()[1] + inExtent[3]*image->GetSpacing()[1]) - overlay->GetOrigin()[1]) / overlay->GetSpacing()[1]);
301 outExtent[4] = (int)lrint(((image->GetOrigin()[2] + inExtent[4]*image->GetSpacing()[2]) - overlay->GetOrigin()[2]) / overlay->GetSpacing()[2]);
302 outExtent[5] = (int)lrint(((image->GetOrigin()[2] + inExtent[5]*image->GetSpacing()[2]) - overlay->GetOrigin()[2]) / overlay->GetSpacing()[2]);
304 //----------------------------------------------------------------------------
307 //------------------------------------------------------------------------------
308 void vvBinaryImageOverlayActor::SetDisplayExtentAndCameraPosition(int orientation,
311 vtkImageActor * actor,
315 Error according to camera orientation
319 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_XY) {
320 //if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2] > slice)
321 actor->SetPosition(0,0, -position);
323 //actor->SetPosition(0,0, position);
325 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_XZ) {
326 //if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1] > slice)
327 // actor->SetPosition(0,position,0);
329 actor->SetPosition(0,-position,0);
331 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_YZ) {
332 //if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0] > slice)
333 // actor->SetPosition(position,0, 0);
335 actor->SetPosition(-position,0, 0);
337 actor->SetDisplayExtent(extent);
339 //------------------------------------------------------------------------------