]> Creatis software - clitk.git/blob - vv/vvROIActor.cxx
Set contours depth at 100 to be in front of other objects
[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.7;
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   mContourWidth = n;
89 }
90 //------------------------------------------------------------------------------
91
92
93 //------------------------------------------------------------------------------
94 void vvROIActor::SetSlicerManager(vvSlicerManager * s) {
95   mSlicerManager = s;
96 }
97 //------------------------------------------------------------------------------
98
99
100 //------------------------------------------------------------------------------
101 void vvROIActor::UpdateImage()
102 {
103   mOverlayActors.clear();
104   mImageContour.clear();
105   Initialize(mDepth, mIsVisible);
106   Update(); // No Render
107 }
108 //------------------------------------------------------------------------------
109
110
111 //------------------------------------------------------------------------------
112 void vvROIActor::SetVisible(bool b)
113 {
114   mIsVisible = b;
115   if (!b) { // remove actor
116     for(unsigned int i= 0; i<mOverlayActors.size(); i++)
117       mOverlayActors[i]->HideActors();
118   } else {
119     for(unsigned int i= 0; i<mOverlayActors.size(); i++)
120       mOverlayActors[i]->ShowActors();
121   }
122   Update(); // No Render
123 }
124 //------------------------------------------------------------------------------
125
126
127 //------------------------------------------------------------------------------
128 void vvROIActor::SetContourVisible(bool b) {
129   mIsContourVisible = b;
130   if (!b) { // remove actor
131     for(unsigned int i= 0; i<mImageContour.size(); i++) {
132       mImageContour[i]->HideActors();
133     }
134   }
135   else {
136     for(unsigned int i= 0; i<mImageContour.size(); i++) {
137       mImageContour[i]->ShowActors();
138     }
139   }
140   Update(); // No Render
141 }
142 //------------------------------------------------------------------------------
143
144
145 //------------------------------------------------------------------------------
146 bool vvROIActor::IsVisible() {
147   return mIsVisible;
148 }
149 //------------------------------------------------------------------------------
150
151
152 //------------------------------------------------------------------------------
153 bool vvROIActor::IsContourVisible() {
154   return mIsContourVisible;
155 }
156 //------------------------------------------------------------------------------
157
158
159 //------------------------------------------------------------------------------
160 void vvROIActor::Initialize(double depth, bool IsVisible) {
161   if (mROI->GetImage()) {
162     mImageContour.clear();
163     mOverlayActors.clear();
164     mDepth = depth;
165     for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
166       mImageContour.push_back(vvImageContour::New());
167       mImageContour[i]->SetSlicer(mSlicerManager->GetSlicer(i));
168       mImageContour[i]->SetImage(mROI->GetImage());
169       mImageContour[i]->SetColor(mContourColor[0], mContourColor[1], mContourColor[2]);
170       mImageContour[i]->SetLineWidth(mContourWidth);
171       mImageContour[i]->SetPreserveMemoryModeEnabled(true);
172       mImageContour[i]->SetDepth(mDepth+0.5);
173       mImageContour[i]->HideActors();
174       
175       mOverlayActors.push_back(vvBinaryImageOverlayActor::New());
176
177       // BG or FG
178       if (m_modeBG) {
179         mOverlayActors[i]->SetImage(mROI->GetImage(), mROI->GetBackgroundValueLabelImage());
180       }
181       else {
182         mOverlayActors[i]->SetImage(mROI->GetImage(), mROI->GetForegroundValueLabelImage(), false);
183       }
184
185
186       mOverlayActors[i]->SetColor(mROI->GetDisplayColor()[0],
187                                   mROI->GetDisplayColor()[1],
188                                   mROI->GetDisplayColor()[2]);
189       mOverlayActors[i]->SetOpacity(mOpacity);
190       mOverlayActors[i]->SetSlicer(mSlicerManager->GetSlicer(i));
191       mOverlayActors[i]->Initialize(IsVisible);      
192       mOverlayActors[i]->SetDepth(mDepth);
193     }
194
195     connect(mSlicerManager,SIGNAL(UpdateSlice(int,int)),this,SLOT(UpdateSlice(int, int)));
196     //connect(mSlicerManager,SIGNAL(UpdateTSlice(int,int)),this,SLOT(UpdateSlice(int, int)));
197     connect(mSlicerManager, SIGNAL(AVerticalSliderHasChanged(int, int)), SLOT(UpdateSlice(int, int)));
198   }
199 }
200 //------------------------------------------------------------------------------
201
202
203 //------------------------------------------------------------------------------
204 void vvROIActor::SetDepth(double d)
205 {
206   mDepth = d;
207   if (!mSlicerManager) return;
208   for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {  
209     mOverlayActors[i]->SetDepth(d);
210     mImageContour[i]->SetDepth(d+0.5);
211   }
212   Update(true);
213 }
214 //------------------------------------------------------------------------------
215
216
217 //------------------------------------------------------------------------------
218 void vvROIActor::Update(bool force)
219 {
220   if (!mSlicerManager) return;
221   for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
222     UpdateSlice(i, mSlicerManager->GetSlicer(i)->GetSlice(), force);
223   }
224 }
225 //------------------------------------------------------------------------------
226
227
228 //------------------------------------------------------------------------------
229 void vvROIActor::UpdateSlice(int slicer, int slices, bool force)
230 {
231   if (!mROI->GetImage())  return;
232   if ((!mIsVisible) && (!mIsContourVisible)) return; 
233   if (!mSlicerManager) {
234     std::cerr << "Error. No mSlicerManager ?" << std::endl;
235     exit(0);
236   }
237
238   if (mIsContourVisible) {
239     mImageContour[slicer]->Update(mROI->GetBackgroundValueLabelImage()+1); 
240   }
241
242   // Refresh overlays
243   mOverlayActors[slicer]->UpdateSlice(slicer, slices, force);
244 }
245 //------------------------------------------------------------------------------
246
247
248 //------------------------------------------------------------------------------
249 void vvROIActor::SetOpacity(double d)
250 {
251   mOpacity = d;
252 }
253 //------------------------------------------------------------------------------
254
255
256 //------------------------------------------------------------------------------
257 void vvROIActor::SetContourColor(double r, double v, double b) {
258   mContourColor[0] = r;
259   mContourColor[1] = v;
260   mContourColor[2] = b;  
261 }
262 //------------------------------------------------------------------------------
263
264
265 //------------------------------------------------------------------------------
266 void vvROIActor::SetOverlayColor(double r, double v, double b) {
267   if (mROI)
268     mROI->SetDisplayColor(r,v,b);
269 }
270 //------------------------------------------------------------------------------
271
272
273 //------------------------------------------------------------------------------
274 std::vector<double> & vvROIActor::GetContourColor() { 
275   return mContourColor; 
276 }
277 //------------------------------------------------------------------------------
278
279
280 //------------------------------------------------------------------------------
281 std::vector<double> & vvROIActor::GetOverlayColor() { 
282   return mROI->GetDisplayColor();
283 }
284 //------------------------------------------------------------------------------
285
286
287 //------------------------------------------------------------------------------
288 void vvROIActor::UpdateColor() {
289   for(unsigned int i=0; i<mOverlayActors.size(); i++) {
290     mOverlayActors[i]->SetOpacity(mOpacity);
291     mOverlayActors[i]->SetColor(mROI->GetDisplayColor()[0],
292                                 mROI->GetDisplayColor()[1],
293                                 mROI->GetDisplayColor()[2]);
294     mOverlayActors[i]->UpdateColor();
295   }
296   for(unsigned int i=0; i<mImageContour.size(); i++) {
297     mImageContour[i]->SetLineWidth(mContourWidth);
298     mImageContour[i]->SetColor(mContourColor[0], mContourColor[1], mContourColor[2]);
299     if (mIsContourVisible)
300       mImageContour[i]->Update(mROI->GetBackgroundValueLabelImage()+1);
301   }
302 }
303 //------------------------------------------------------------------------------
304
305
306 //------------------------------------------------------------------------------
307 double vvROIActor::GetOpacity()
308 {
309   return mOpacity;
310 }
311 //------------------------------------------------------------------------------
312
313
314 //------------------------------------------------------------------------------
315 void vvROIActor::SetSelected(bool b)
316 {
317   mIsSelected = b;
318   if (b) {
319     for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
320       mImageContour[i]->SetLineWidth(3.0);
321       mImageContour[i]->ShowActors();
322     }
323   } else {
324     for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
325       mImageContour[i]->HideActors();
326     }
327   }
328 }
329 //------------------------------------------------------------------------------
330
331
332 //------------------------------------------------------------------------------
333 void vvROIActor::CopyParameters(QSharedPointer<vvROIActor> roi)
334 {
335   // Overlay
336   SetVisible(roi->IsVisible());
337   SetOpacity(roi->GetOpacity());
338   SetOverlayColor(roi->GetOverlayColor()[0], roi->GetOverlayColor()[1], roi->GetOverlayColor()[2]);
339
340   // Contour
341   SetContourVisible(roi->IsContourVisible());
342   SetContourWidth(roi->GetContourWidth());
343   SetContourColor(roi->GetContourColor()[0], roi->GetContourColor()[1], roi->GetContourColor()[2]);
344   
345   // Global
346   SetDepth(roi->GetDepth());
347 }
348 //------------------------------------------------------------------------------