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