]> Creatis software - clitk.git/blob - vv/vvROIActor.cxx
Merge branch 'master' into OpenGL2
[clitk.git] / vv / vvROIActor.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
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
8
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.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ===========================================================================**/
18
19 // vv
20 #include "vvROIActor.h"
21 #include "vvSlicerManager.h"
22
23 // vtk
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
33 //------------------------------------------------------------------------------
34 vvROIActor::vvROIActor()
35
36   mIsVisible = true;
37   mIsContourVisible = false;
38   mOpacity = 0.5;
39   mIsSelected = false;
40   mContourWidth = 1;
41   mContourColor.resize(3);
42   m_modeBG = true;
43   mDepth = 1.0;
44   mROI = 0;
45   mSlicerManager = 0;
46   mContourColor[0] = 1;
47   mContourColor[1] = 1;
48   mContourColor[2] = 1;
49 }
50 //------------------------------------------------------------------------------
51
52
53 //------------------------------------------------------------------------------
54 vvROIActor::~vvROIActor()
55
56 }
57 //------------------------------------------------------------------------------
58
59
60 //------------------------------------------------------------------------------
61 void vvROIActor::RemoveActors()
62
63   for(unsigned int i= 0; i<mOverlayActors.size(); i++) {
64     mOverlayActors[i]->RemoveActors();
65   }
66
67   for(unsigned int i= 0; i<mImageContour.size(); i++) {
68     mImageContour[i]->RemoveActors();
69   }  
70   
71   Update(true);
72   //mImageContour.clear();
73   //mOverlayActors.clear();
74 }
75 //------------------------------------------------------------------------------
76
77
78 //------------------------------------------------------------------------------
79 void vvROIActor::SetROI(clitk::DicomRT_ROI * s)
80
81   mROI = s;
82 }
83 //------------------------------------------------------------------------------
84
85
86 //------------------------------------------------------------------------------
87 void vvROIActor::SetContourWidth(int n) 
88
89   mContourWidth = n;
90 }
91 //------------------------------------------------------------------------------
92
93
94 //------------------------------------------------------------------------------
95 void vvROIActor::SetSlicerManager(vvSlicerManager * s) 
96
97   mSlicerManager = s;
98 }
99 //------------------------------------------------------------------------------
100
101
102 //------------------------------------------------------------------------------
103 void vvROIActor::UpdateImage()
104
105   mOverlayActors.clear();
106   mImageContour.clear();
107   Initialize(mDepth, mIsVisible);
108   Update(); // No Render
109 }
110 //------------------------------------------------------------------------------
111
112
113 //------------------------------------------------------------------------------
114 void vvROIActor::SetVisible(bool b)
115
116   mIsVisible = b;
117   if (!b) { // remove actor
118     for(unsigned int i= 0; i<mOverlayActors.size(); i++)
119       mOverlayActors[i]->HideActors();
120   } else {
121     for(unsigned int i= 0; i<mOverlayActors.size(); i++)
122       mOverlayActors[i]->ShowActors();
123   }
124   //Update(); // No Render
125 }
126 //------------------------------------------------------------------------------
127
128
129 //------------------------------------------------------------------------------
130 void vvROIActor::SetContourVisible(bool b) 
131
132   mIsContourVisible = b;
133   if (!b) { // remove actor
134     for(unsigned int i= 0; i<mImageContour.size(); i++) {
135       mImageContour[i]->HideActors();
136     }
137   }
138   else {
139     for(unsigned int i= 0; i<mImageContour.size(); i++) {
140       mImageContour[i]->ShowActors();
141     }
142   }
143   //Update(); // No Render
144 }
145 //------------------------------------------------------------------------------
146
147
148 //------------------------------------------------------------------------------
149 bool vvROIActor::IsVisible() 
150
151   return mIsVisible;
152 }
153 //------------------------------------------------------------------------------
154
155
156 //------------------------------------------------------------------------------
157 bool vvROIActor::IsContourVisible() 
158
159   return mIsContourVisible;
160 }
161 //------------------------------------------------------------------------------
162
163
164 //------------------------------------------------------------------------------
165 void vvROIActor::Initialize(double depth, bool IsVisible) 
166
167   if (mROI->GetImage()) {
168     mImageContour.clear();
169     mOverlayActors.clear();
170     mDepth = depth;
171     for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
172       mImageContour.push_back(vvImageContour::New());
173       mImageContour[i]->SetSlicer(mSlicerManager->GetSlicer(i));
174       mImageContour[i]->SetImage(mROI->GetImage());
175       // Color of the contour is same of roi color
176       mContourColor[0] = mROI->GetDisplayColor()[0];
177       mContourColor[1] = mROI->GetDisplayColor()[1];
178       mContourColor[2] = mROI->GetDisplayColor()[2];
179       mImageContour[i]->SetColor(mContourColor[0], mContourColor[1], mContourColor[2]);
180       mImageContour[i]->SetLineWidth(mContourWidth);
181       mImageContour[i]->SetPreserveMemoryModeEnabled(true);
182       mImageContour[i]->SetDepth(mDepth+0.5);
183       mImageContour[i]->ShowActors();
184       
185       mOverlayActors.push_back(vvBinaryImageOverlayActor::New());
186
187       // BG or FG
188       if (m_modeBG) {
189         mOverlayActors[i]->SetImage(mROI->GetImage(), mROI->GetBackgroundValueLabelImage());
190       }
191       else {
192         mOverlayActors[i]->SetImage(mROI->GetImage(), mROI->GetForegroundValueLabelImage(), false);
193       }
194
195
196       mOverlayActors[i]->SetColor(mROI->GetDisplayColor()[0],
197                                   mROI->GetDisplayColor()[1],
198                                   mROI->GetDisplayColor()[2]);
199       mOverlayActors[i]->SetOpacity(mOpacity);
200       mOverlayActors[i]->SetSlicer(mSlicerManager->GetSlicer(i));
201       mOverlayActors[i]->Initialize(IsVisible);      
202       mOverlayActors[i]->SetDepth(mDepth);
203     }
204     connect(mSlicerManager,SIGNAL(UpdateSlice(int,int)),this,SLOT(UpdateSlice(int, int)));
205     connect(mSlicerManager,SIGNAL(UpdateTSlice(int,int,int)),this,SLOT(UpdateSlice(int, int, int)));
206     connect(mSlicerManager, SIGNAL(AVerticalSliderHasChanged(int, int)), SLOT(UpdateSlice(int, int)));
207   }
208 }
209 //------------------------------------------------------------------------------
210
211
212 //------------------------------------------------------------------------------
213 void vvROIActor::SetDepth(double d)
214
215   mDepth = d;
216   if (!mSlicerManager) return;
217   for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {  
218     mOverlayActors[i]->SetDepth(d);
219     mImageContour[i]->SetDepth(d+0.5);
220   }
221   Update(true);
222 }
223 //------------------------------------------------------------------------------
224
225
226 //------------------------------------------------------------------------------
227 void vvROIActor::Update(bool force)
228
229   if (!mSlicerManager) return;
230   for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
231     UpdateSlice(i, mSlicerManager->GetSlicer(i)->GetSlice(), force);
232   }
233 }
234 //------------------------------------------------------------------------------
235
236
237 //------------------------------------------------------------------------------
238 void vvROIActor::UpdateSlice(int slicer, int slices, int force)
239
240   if (!mROI->GetImage())  return;
241   if ((!mIsVisible) && (!mIsContourVisible)) return; 
242   if (!mSlicerManager) {
243     std::cerr << "Error. No mSlicerManager ?" << std::endl;
244     exit(0);
245   }
246
247   if (mIsContourVisible) {
248     mImageContour[slicer]->Update(mROI->GetBackgroundValueLabelImage()+1); 
249   }
250
251   // Refresh overlays
252   if (mIsVisible) {
253     mOverlayActors[slicer]->UpdateSlice(slicer, slices, force);
254   }
255   //mSlicerManager->GetSlicer(slicer)->Render();
256 }
257 //------------------------------------------------------------------------------
258
259
260 //------------------------------------------------------------------------------
261 void vvROIActor::SetOpacity(double d)
262
263   mOpacity = d;
264 }
265 //------------------------------------------------------------------------------
266
267
268 //------------------------------------------------------------------------------
269 void vvROIActor::SetContourColor(double r, double v, double b) 
270
271   mContourColor[0] = r;
272   mContourColor[1] = v;
273   mContourColor[2] = b;  
274 }
275 //------------------------------------------------------------------------------
276
277
278 //------------------------------------------------------------------------------
279 void vvROIActor::SetOverlayColor(double r, double v, double b) 
280
281   if (mROI)
282     mROI->SetDisplayColor(r,v,b);
283 }
284 //------------------------------------------------------------------------------
285
286
287 //------------------------------------------------------------------------------
288 std::vector<double> & vvROIActor::GetContourColor() 
289
290   return mContourColor; 
291 }
292 //------------------------------------------------------------------------------
293
294
295 //------------------------------------------------------------------------------
296 std::vector<double> & vvROIActor::GetOverlayColor() 
297
298   return mROI->GetDisplayColor();
299 }
300 //------------------------------------------------------------------------------
301
302
303 //------------------------------------------------------------------------------
304 void vvROIActor::UpdateColor() 
305
306   for(unsigned int i=0; i<mOverlayActors.size(); i++) {
307     mOverlayActors[i]->SetOpacity(mOpacity);
308     mOverlayActors[i]->SetColor(mROI->GetDisplayColor()[0],
309                                 mROI->GetDisplayColor()[1],
310                                 mROI->GetDisplayColor()[2]);
311     mOverlayActors[i]->UpdateColor();
312   }
313   for(unsigned int i=0; i<mImageContour.size(); i++) {
314     mImageContour[i]->SetLineWidth(mContourWidth);
315     mImageContour[i]->SetColor(mContourColor[0], mContourColor[1], mContourColor[2]);
316     if (mIsContourVisible)
317       mImageContour[i]->Update(mROI->GetBackgroundValueLabelImage()+1);
318   }
319 }
320 //------------------------------------------------------------------------------
321
322
323 //------------------------------------------------------------------------------
324 double vvROIActor::GetOpacity()
325
326   return mOpacity;
327 }
328 //------------------------------------------------------------------------------
329
330
331 //------------------------------------------------------------------------------
332 void vvROIActor::SetSelected(bool b)
333
334   mIsSelected = b;
335   if (b) {
336     for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
337       mImageContour[i]->SetLineWidth(3.0);
338       mImageContour[i]->ShowActors();
339     }
340   } else {
341     for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
342       mImageContour[i]->HideActors();
343     }
344   }
345 }
346 //------------------------------------------------------------------------------
347
348
349 //------------------------------------------------------------------------------
350 void vvROIActor::CopyParameters(QSharedPointer<vvROIActor> roi)
351
352   // Overlay
353   SetVisible(roi->IsVisible());
354   SetOpacity(roi->GetOpacity());
355   SetOverlayColor(roi->GetOverlayColor()[0], roi->GetOverlayColor()[1], roi->GetOverlayColor()[2]);
356
357   // Contour
358   SetContourVisible(roi->IsContourVisible());
359   SetContourWidth(roi->GetContourWidth());
360   SetContourColor(roi->GetContourColor()[0], roi->GetContourColor()[1], roi->GetContourColor()[2]);
361   
362   // Global
363   SetDepth(roi->GetDepth());
364 }
365 //------------------------------------------------------------------------------