]> Creatis software - clitk.git/blob - vv/vvROIActor.cxx
Merge branch 'master' of git.creatis.insa-lyon.fr:clitk
[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 }
45 //------------------------------------------------------------------------------
46
47
48 //------------------------------------------------------------------------------
49 vvROIActor::~vvROIActor()
50 {
51 }
52 //------------------------------------------------------------------------------
53
54
55 //------------------------------------------------------------------------------
56 void vvROIActor::SetROI(clitk::DicomRT_ROI * s)
57 {
58   mROI = s;
59 }
60 //------------------------------------------------------------------------------
61
62
63 //------------------------------------------------------------------------------
64 void vvROIActor::SetContourWidth(int n) {
65   mContourWidth = n;
66 }
67 //------------------------------------------------------------------------------
68
69
70 //------------------------------------------------------------------------------
71 void vvROIActor::SetSlicerManager(vvSlicerManager * s) {
72   mSlicerManager = s;
73 }
74 //------------------------------------------------------------------------------
75
76
77 //------------------------------------------------------------------------------
78 void vvROIActor::UpdateImage()
79 {
80   mOverlayActors.clear();
81   mImageContour.clear();
82   Initialize(mDepth, mIsVisible);
83   Update(); // No Render
84 }
85 //------------------------------------------------------------------------------
86
87
88 //------------------------------------------------------------------------------
89 void vvROIActor::SetVisible(bool b)
90 {
91   mIsVisible = b;
92   if (!b) { // remove actor
93     for(unsigned int i= 0; i<mOverlayActors.size(); i++)
94       mOverlayActors[i]->HideActors();
95   } else {
96     for(unsigned int i= 0; i<mOverlayActors.size(); i++)
97       mOverlayActors[i]->ShowActors();
98   }
99   Update(); // No Render
100 }
101 //------------------------------------------------------------------------------
102
103
104 //------------------------------------------------------------------------------
105 void vvROIActor::SetContourVisible(bool b) {
106   mIsContourVisible = b;
107   if (!b) { // remove actor
108     for(unsigned int i= 0; i<mImageContour.size(); i++) {
109       mImageContour[i]->HideActors();
110     }
111   }
112   else {
113     for(unsigned int i= 0; i<mImageContour.size(); i++) {
114       mImageContour[i]->ShowActors();
115     }
116   }
117   Update(); // No Render
118 }
119 //------------------------------------------------------------------------------
120
121
122 //------------------------------------------------------------------------------
123 bool vvROIActor::IsVisible() {
124   return mIsVisible;
125 }
126 //------------------------------------------------------------------------------
127
128
129 //------------------------------------------------------------------------------
130 bool vvROIActor::IsContourVisible() {
131   return mIsContourVisible;
132 }
133 //------------------------------------------------------------------------------
134
135
136 //------------------------------------------------------------------------------
137 void vvROIActor::Initialize(double depth, bool IsVisible) {
138   if (mROI->GetImage()) {
139     mImageContour.clear();
140     mOverlayActors.clear();
141     mDepth = depth;
142     for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
143
144       mImageContour.push_back(vvImageContour::New());
145       mImageContour[i]->SetSlicer(mSlicerManager->GetSlicer(i));
146       mImageContour[i]->SetImage(mROI->GetImage());
147       mContourColor[0] = mROI->GetDisplayColor()[0];
148       mContourColor[1] = mROI->GetDisplayColor()[1];
149       mContourColor[2] = mROI->GetDisplayColor()[2];
150       mImageContour[i]->SetColor(mContourColor[0], mContourColor[1], mContourColor[2]);
151       mImageContour[i]->SetLineWidth(mContourWidth);
152       mImageContour[i]->SetPreserveMemoryModeEnabled(true);
153       mImageContour[i]->SetDepth(mDepth);
154       //mImageContour[i]->SetPreserveMemoryModeEnabled(false); // SEG FAULT !!!
155       mImageContour[i]->SetSlicer(mSlicerManager->GetSlicer(i));
156       mImageContour[i]->HideActors();
157       
158       mOverlayActors.push_back(vvBinaryImageOverlayActor::New());
159
160       // BG or FG
161       if (m_modeBG) {
162         mOverlayActors[i]->SetImage(mROI->GetImage(), mROI->GetBackgroundValueLabelImage());
163       }
164       else {
165         mOverlayActors[i]->SetImage(mROI->GetImage(), mROI->GetForegroundValueLabelImage(), false);
166       }
167
168
169       mOverlayActors[i]->SetColor(mROI->GetDisplayColor()[0],
170                                   mROI->GetDisplayColor()[1],
171                                   mROI->GetDisplayColor()[2]);
172       mOverlayActors[i]->SetOpacity(mOpacity);
173       mOverlayActors[i]->SetSlicer(mSlicerManager->GetSlicer(i));
174       mOverlayActors[i]->Initialize(IsVisible);      
175       mOverlayActors[i]->SetDepth(mDepth);
176     }
177
178     connect(mSlicerManager,SIGNAL(UpdateSlice(int,int)),this,SLOT(UpdateSlice(int, int)));
179     //connect(mSlicerManager,SIGNAL(UpdateTSlice(int,int)),this,SLOT(UpdateSlice(int, int)));
180     connect(mSlicerManager, SIGNAL(AVerticalSliderHasChanged(int, int)), SLOT(UpdateSlice(int, int)));
181   }
182 }
183 //------------------------------------------------------------------------------
184
185
186 //------------------------------------------------------------------------------
187 void vvROIActor::SetDepth(double d)
188 {
189   mDepth = d;
190   for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {  
191     mOverlayActors[i]->SetDepth(d);
192     mImageContour[i]->SetDepth(d);
193   }
194   Update(true);
195 }
196 //------------------------------------------------------------------------------
197
198
199 //------------------------------------------------------------------------------
200 void vvROIActor::Update(bool force)
201 {
202   for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
203     UpdateSlice(i, mSlicerManager->GetSlicer(i)->GetSlice(), force);
204   }
205 }
206 //------------------------------------------------------------------------------
207
208
209 //------------------------------------------------------------------------------
210 void vvROIActor::UpdateSlice(int slicer, int slices, bool force)
211 {
212   if (!mROI->GetImage())  return;
213   if ((!mIsVisible) && (!mIsContourVisible)) return; 
214   if (!mSlicerManager) {
215     std::cerr << "Error. No mSlicerManager ?" << std::endl;
216     exit(0);
217   }
218
219   if (mIsContourVisible) {
220     mImageContour[slicer]->Update(mROI->GetBackgroundValueLabelImage()+1); 
221   }
222
223   // Refresh overlays
224   mOverlayActors[slicer]->UpdateSlice(slicer, slices, force);
225 }
226 //------------------------------------------------------------------------------
227
228
229 //------------------------------------------------------------------------------
230 void vvROIActor::SetOpacity(double d)
231 {
232   mOpacity = d;
233 }
234 //------------------------------------------------------------------------------
235
236
237 //------------------------------------------------------------------------------
238 void vvROIActor::SetContourColor(double r, double v, double b) {
239   mContourColor[0] = r;
240   mContourColor[1] = v;
241   mContourColor[2] = b;  
242 }
243 //------------------------------------------------------------------------------
244
245
246 //------------------------------------------------------------------------------
247 std::vector<double> & vvROIActor::GetContourColor() { 
248   return mContourColor; 
249 }
250 //------------------------------------------------------------------------------
251
252
253 //------------------------------------------------------------------------------
254 void vvROIActor::UpdateColor() {
255   for(unsigned int i=0; i<mOverlayActors.size(); i++) {
256     mOverlayActors[i]->SetOpacity(mOpacity);
257     mOverlayActors[i]->SetColor(mROI->GetDisplayColor()[0],
258                                 mROI->GetDisplayColor()[1],
259                                 mROI->GetDisplayColor()[2]);
260     mOverlayActors[i]->UpdateColor();
261   }
262   for(unsigned int i=0; i<mImageContour.size(); i++) {
263     mImageContour[i]->SetLineWidth(mContourWidth);
264     mImageContour[i]->SetColor(mContourColor[0], mContourColor[1], mContourColor[2]);
265     if (mIsContourVisible)
266       mImageContour[i]->Update(mROI->GetBackgroundValueLabelImage()+1);
267   }
268 }
269 //------------------------------------------------------------------------------
270
271
272 //------------------------------------------------------------------------------
273 double vvROIActor::GetOpacity()
274 {
275   return mOpacity;
276 }
277 //------------------------------------------------------------------------------
278
279
280 //------------------------------------------------------------------------------
281 void vvROIActor::SetSelected(bool b)
282 {
283   mIsSelected = b;
284   if (b) {
285     for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
286       mImageContour[i]->SetLineWidth(3.0);
287       mImageContour[i]->ShowActors();
288     }
289   } else {
290     for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
291       mImageContour[i]->HideActors();
292     }
293   }
294 }
295 //------------------------------------------------------------------------------
296