]> Creatis software - clitk.git/blob - vv/vvROIActor.cxx
Merge branch 'master' into PacsConnection
[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   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       // Color of the contour is same of roi color
170       mContourColor[0] = mROI->GetDisplayColor()[0];
171       mContourColor[1] = mROI->GetDisplayColor()[1];
172       mContourColor[2] = mROI->GetDisplayColor()[2];
173       mImageContour[i]->SetColor(mContourColor[0], mContourColor[1], mContourColor[2]);
174       mImageContour[i]->SetLineWidth(mContourWidth);
175       mImageContour[i]->SetPreserveMemoryModeEnabled(true);
176       mImageContour[i]->SetDepth(mDepth+0.5);
177       mImageContour[i]->HideActors();
178       
179       mOverlayActors.push_back(vvBinaryImageOverlayActor::New());
180
181       // BG or FG
182       if (m_modeBG) {
183         mOverlayActors[i]->SetImage(mROI->GetImage(), mROI->GetBackgroundValueLabelImage());
184       }
185       else {
186         mOverlayActors[i]->SetImage(mROI->GetImage(), mROI->GetForegroundValueLabelImage(), false);
187       }
188
189
190       mOverlayActors[i]->SetColor(mROI->GetDisplayColor()[0],
191                                   mROI->GetDisplayColor()[1],
192                                   mROI->GetDisplayColor()[2]);
193       mOverlayActors[i]->SetOpacity(mOpacity);
194       mOverlayActors[i]->SetSlicer(mSlicerManager->GetSlicer(i));
195       mOverlayActors[i]->Initialize(IsVisible);      
196       mOverlayActors[i]->SetDepth(mDepth);
197     }
198
199     connect(mSlicerManager,SIGNAL(UpdateSlice(int,int)),this,SLOT(UpdateSlice(int, int)));
200     //connect(mSlicerManager,SIGNAL(UpdateTSlice(int,int)),this,SLOT(UpdateSlice(int, int)));
201     connect(mSlicerManager, SIGNAL(AVerticalSliderHasChanged(int, int)), SLOT(UpdateSlice(int, int)));
202   }
203 }
204 //------------------------------------------------------------------------------
205
206
207 //------------------------------------------------------------------------------
208 void vvROIActor::SetDepth(double d)
209 {
210   mDepth = d;
211   if (!mSlicerManager) return;
212   for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {  
213     mOverlayActors[i]->SetDepth(d);
214     mImageContour[i]->SetDepth(d+0.5);
215   }
216   Update(true);
217 }
218 //------------------------------------------------------------------------------
219
220
221 //------------------------------------------------------------------------------
222 void vvROIActor::Update(bool force)
223 {
224   if (!mSlicerManager) return;
225   for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
226     UpdateSlice(i, mSlicerManager->GetSlicer(i)->GetSlice(), force);
227   }
228 }
229 //------------------------------------------------------------------------------
230
231
232 //------------------------------------------------------------------------------
233 void vvROIActor::UpdateSlice(int slicer, int slices, bool force)
234 {
235   if (!mROI->GetImage())  return;
236   if ((!mIsVisible) && (!mIsContourVisible)) return; 
237   if (!mSlicerManager) {
238     std::cerr << "Error. No mSlicerManager ?" << std::endl;
239     exit(0);
240   }
241
242   if (mIsContourVisible) {
243     mImageContour[slicer]->Update(mROI->GetBackgroundValueLabelImage()+1); 
244   }
245
246   // Refresh overlays
247   mOverlayActors[slicer]->UpdateSlice(slicer, slices, force);
248 }
249 //------------------------------------------------------------------------------
250
251
252 //------------------------------------------------------------------------------
253 void vvROIActor::SetOpacity(double d)
254 {
255   mOpacity = d;
256 }
257 //------------------------------------------------------------------------------
258
259
260 //------------------------------------------------------------------------------
261 void vvROIActor::SetContourColor(double r, double v, double b) {
262   mContourColor[0] = r;
263   mContourColor[1] = v;
264   mContourColor[2] = b;  
265 }
266 //------------------------------------------------------------------------------
267
268
269 //------------------------------------------------------------------------------
270 void vvROIActor::SetOverlayColor(double r, double v, double b) {
271   if (mROI)
272     mROI->SetDisplayColor(r,v,b);
273 }
274 //------------------------------------------------------------------------------
275
276
277 //------------------------------------------------------------------------------
278 std::vector<double> & vvROIActor::GetContourColor() { 
279   return mContourColor; 
280 }
281 //------------------------------------------------------------------------------
282
283
284 //------------------------------------------------------------------------------
285 std::vector<double> & vvROIActor::GetOverlayColor() { 
286   return mROI->GetDisplayColor();
287 }
288 //------------------------------------------------------------------------------
289
290
291 //------------------------------------------------------------------------------
292 void vvROIActor::UpdateColor() {
293   for(unsigned int i=0; i<mOverlayActors.size(); i++) {
294     mOverlayActors[i]->SetOpacity(mOpacity);
295     mOverlayActors[i]->SetColor(mROI->GetDisplayColor()[0],
296                                 mROI->GetDisplayColor()[1],
297                                 mROI->GetDisplayColor()[2]);
298     mOverlayActors[i]->UpdateColor();
299   }
300   for(unsigned int i=0; i<mImageContour.size(); i++) {
301     mImageContour[i]->SetLineWidth(mContourWidth);
302     mImageContour[i]->SetColor(mContourColor[0], mContourColor[1], mContourColor[2]);
303     if (mIsContourVisible)
304       mImageContour[i]->Update(mROI->GetBackgroundValueLabelImage()+1);
305   }
306 }
307 //------------------------------------------------------------------------------
308
309
310 //------------------------------------------------------------------------------
311 double vvROIActor::GetOpacity()
312 {
313   return mOpacity;
314 }
315 //------------------------------------------------------------------------------
316
317
318 //------------------------------------------------------------------------------
319 void vvROIActor::SetSelected(bool b)
320 {
321   mIsSelected = b;
322   if (b) {
323     for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
324       mImageContour[i]->SetLineWidth(3.0);
325       mImageContour[i]->ShowActors();
326     }
327   } else {
328     for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
329       mImageContour[i]->HideActors();
330     }
331   }
332 }
333 //------------------------------------------------------------------------------
334
335
336 //------------------------------------------------------------------------------
337 void vvROIActor::CopyParameters(QSharedPointer<vvROIActor> roi)
338 {
339   // Overlay
340   SetVisible(roi->IsVisible());
341   SetOpacity(roi->GetOpacity());
342   SetOverlayColor(roi->GetOverlayColor()[0], roi->GetOverlayColor()[1], roi->GetOverlayColor()[2]);
343
344   // Contour
345   SetContourVisible(roi->IsContourVisible());
346   SetContourWidth(roi->GetContourWidth());
347   SetContourColor(roi->GetContourColor()[0], roi->GetContourColor()[1], roi->GetContourColor()[2]);
348   
349   // Global
350   SetDepth(roi->GetDepth());
351 }
352 //------------------------------------------------------------------------------