1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
19 #include "vvImageContour.h"
21 #include <vtkImageActor.h>
22 #include <vtkCamera.h>
23 #include <vtkRenderer.h>
24 #include <vtkMarchingSquares.h>
25 #include <vtkImageClip.h>
26 #include <vtkImageData.h>
27 #include <vtkPolyDataMapper.h>
28 #include <vtkProperty.h>
30 //------------------------------------------------------------------------------
31 vvImageContour::vvImageContour()
35 mHiddenImageIsUsed = false;
36 mDisplayModeIsPreserveMemory = false;
37 SetPreserveMemoryModeEnabled(false);
39 //------------------------------------------------------------------------------
42 //------------------------------------------------------------------------------
43 vvImageContour::~vvImageContour()
45 for (unsigned int i = 0; i < mSlicer->GetImage()->GetVTKImages().size(); i++) {
46 mSlicer->GetRenderer()->RemoveActor(mSquaresActorList[i]);
48 mSquaresActorList.clear();
52 //------------------------------------------------------------------------------
55 //------------------------------------------------------------------------------
56 void vvImageContour::SetSlicer(vvSlicer * slicer) {
58 // Create an actor for each time slice
59 for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
60 vtkImageClip * mClipper = vtkImageClip::New();
61 vtkMarchingSquares * mSquares = vtkMarchingSquares::New();
62 vtkActor * mSquaresActor = vtkActor::New();
63 CreateNewActor(&mSquaresActor, &mSquares, &mClipper, numImage);
64 mSquaresActorList.push_back(mSquaresActor);
65 mSquaresList.push_back(mSquares);
66 mClipperList.push_back(mClipper);
69 //------------------------------------------------------------------------------
72 //------------------------------------------------------------------------------
73 void vvImageContour::SetImage(vvImage::Pointer image) {
74 for (unsigned int numImage = 0; numImage < image->GetVTKImages().size(); numImage++) {
75 mClipperList[numImage]->SetInput(image->GetVTKImages()[numImage]);
77 mHiddenImageIsUsed = true;
80 //------------------------------------------------------------------------------
83 //------------------------------------------------------------------------------
84 void vvImageContour::SetPreserveMemoryModeEnabled(bool b) {
85 // FastCache mode work only if threshold is always the same
86 if (mDisplayModeIsPreserveMemory == b) return;
87 mDisplayModeIsPreserveMemory = b;
90 InitializeCacheMode();
93 for(unsigned int d=0; d<mListOfCachedContourActors.size(); d++)
94 mListOfCachedContourActors[d].clear();
95 mListOfCachedContourActors.clear();
99 //------------------------------------------------------------------------------
102 //------------------------------------------------------------------------------
103 void vvImageContour::SetColor(double r, double g, double b) {
104 for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
105 mSquaresActorList[i]->GetProperty()->SetColor(r,g,b);
108 //------------------------------------------------------------------------------
111 //------------------------------------------------------------------------------
112 void vvImageContour::SetLineWidth(double w)
114 for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
115 mSquaresActorList[i]->GetProperty()->SetLineWidth(w);
118 //------------------------------------------------------------------------------
121 //------------------------------------------------------------------------------
122 void vvImageContour::HideActors() {
123 if (!mSlicer) return;
124 mSlice = mSlicer->GetSlice();
125 for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
126 mSquaresActorList[i]->VisibilityOff();
129 //------------------------------------------------------------------------------
132 //------------------------------------------------------------------------------
133 void vvImageContour::ShowActors() {
134 if (!mSlicer) return;
135 mSlice = mSlicer->GetSlice();
136 mTSlice = mSlicer->GetTSlice();
137 mSquaresActorList[mTSlice]->VisibilityOn();
140 //------------------------------------------------------------------------------
143 //------------------------------------------------------------------------------
144 void vvImageContour::Update(double value) {
145 if (!mSlicer) return;
146 if (mPreviousValue == value) {
147 if (mPreviousSlice == mSlicer->GetSlice()) {
148 if (mPreviousTSlice == mSlicer->GetTSlice()) {
149 return; // Nothing to do
154 // Get current threshold value
158 mSlice = mSlicer->GetSlice();
160 if (mDisplayModeIsPreserveMemory) {
161 UpdateWithPreserveMemoryMode();
164 UpdateWithFastCacheMode();
167 // mSlicer->Render(); //DS ---> REMOVE ??
169 mPreviousTSlice = mSlicer->GetTSlice();
170 mPreviousSlice = mSlicer->GetSlice();
171 mPreviousValue = value;
173 //------------------------------------------------------------------------------
176 //------------------------------------------------------------------------------
177 void vvImageContour::UpdateWithPreserveMemoryMode() {
178 // Only change actor visibility if tslice change
179 int mPreviousTslice = mTSlice;
180 mTSlice = mSlicer->GetTSlice();
182 vtkMarchingSquares * mSquares = mSquaresList[mTSlice];
183 vtkImageClip * mClipper = mClipperList[mTSlice];
184 vtkActor * mSquaresActor = mSquaresActorList[mTSlice];
185 int orientation = ComputeCurrentOrientation();
187 UpdateActor(mSquaresActor, mSquares, mClipper, mValue, orientation, mSlice);
188 mSquaresActorList[mTSlice]->VisibilityOn();
190 if (mPreviousTslice != mTSlice) {
191 if (mPreviousTslice != -1) mSquaresActorList[mPreviousTslice]->VisibilityOff();
194 //------------------------------------------------------------------------------
197 //------------------------------------------------------------------------------
198 void vvImageContour::InitializeCacheMode() {
199 mPreviousSlice = mPreviousOrientation = 0;
200 int dim = mSlicer->GetImage()->GetNumberOfDimensions();
202 mListOfCachedContourActors.resize(dim);
203 for(int d=0; d<dim; d++) {
204 int size = mSlicer->GetImage()->GetSize()[d];
205 mListOfCachedContourActors[d].resize(size);
206 for(int j=0; j<size; j++) {
207 mListOfCachedContourActors[d][j] = NULL;
211 //------------------------------------------------------------------------------
214 //------------------------------------------------------------------------------
215 int vvImageContour::ComputeCurrentOrientation() {
216 // Get extent of image in the slicer
217 int* extent = mSlicer->GetImageActor()->GetDisplayExtent();
219 // Compute orientation
221 for (orientation = 0; orientation < 6; orientation = orientation+2) {
222 if (extent[orientation] == extent[orientation+1]) {
226 orientation = orientation/2;
229 //------------------------------------------------------------------------------
232 //------------------------------------------------------------------------------
233 void vvImageContour::UpdateWithFastCacheMode() {
234 // Compute orientation
235 int orientation = ComputeCurrentOrientation();
237 if ((mPreviousSlice == mSlice) && (mPreviousOrientation == orientation)) return;
239 vtkActor * actor = mListOfCachedContourActors[orientation][mSlice];
241 mListOfCachedContourActors[orientation][mSlice]->VisibilityOn();
243 vtkImageClip * mClipper;
244 vtkMarchingSquares * mSquares;
245 vtkActor * mSquaresActor;
246 CreateNewActor(&mSquaresActor, &mSquares, &mClipper, 0);
247 UpdateActor(mSquaresActor, mSquares, mClipper, mValue, orientation, mSlice);
248 mListOfCachedContourActors[orientation][mSlice] = mSquaresActor;
249 mSquaresActor->VisibilityOn();
252 if (mListOfCachedContourActors[mPreviousOrientation][mPreviousSlice] != NULL)
253 mListOfCachedContourActors[mPreviousOrientation][mPreviousSlice]->VisibilityOff();
254 mPreviousSlice = mSlice;
255 mPreviousOrientation = orientation;
257 //------------------------------------------------------------------------------
260 //------------------------------------------------------------------------------
261 void vvImageContour::CreateNewActor(vtkActor ** actor,
262 vtkMarchingSquares ** squares,
263 vtkImageClip ** clipper,
265 vtkActor * mSquaresActor = (*actor = vtkActor::New());
266 vtkImageClip * mClipper = (*clipper = vtkImageClip::New());
267 vtkMarchingSquares * mSquares = (*squares = vtkMarchingSquares::New());
268 vtkPolyDataMapper * mSquaresMapper = vtkPolyDataMapper::New();
270 if (mHiddenImageIsUsed)
271 mClipper->SetInput(mHiddenImage->GetVTKImages()[0]);
273 mClipper->SetInput(mSlicer->GetImage()->GetVTKImages()[numImage]);
274 mSquares->SetInput(mClipper->GetOutput());
275 mSquaresMapper->SetInput(mSquares->GetOutput());
276 mSquaresMapper->ScalarVisibilityOff();
277 mSquaresActor->SetMapper(mSquaresMapper);
278 mSquaresActor->GetProperty()->SetColor(1.0,0,0);
279 mSquaresActor->SetPickable(0);
280 mSquaresActor->VisibilityOff();
281 mSlicer->GetRenderer()->AddActor(mSquaresActor);
283 //------------------------------------------------------------------------------
286 //------------------------------------------------------------------------------
287 void vvImageContour::UpdateActor(vtkActor * actor,
288 vtkMarchingSquares * squares,
289 vtkImageClip * clipper,
290 double threshold, int orientation, int slice) {
291 // Set parameter for the MarchigSquare
292 squares->SetValue(0, threshold);
295 int* extent = mSlicer->GetImageActor()->GetDisplayExtent();
297 // Change extent if needed
299 if (mHiddenImageIsUsed) {
300 extent2 = new int[6];
302 extent3 = mHiddenImage->GetFirstVTKImageData()->GetExtent();
304 for(int i=0; i<6; i++) extent2[i] = extent3[i];
306 double s = (double)extent[orientation*2]*(double)mSlicer->GetImage()->GetSpacing()[orientation]; // in mm
307 s = s+mSlicer->GetImage()->GetOrigin()[orientation]; // from origin
308 s = s-mHiddenImage->GetFirstVTKImageData()->GetOrigin()[orientation]; // from corner second image
309 s = s/mHiddenImage->GetFirstVTKImageData()->GetSpacing()[orientation]; // in voxel
312 extent2[orientation*2] = extent2[orientation*2+1] = (int)floor(s);
314 extent2[orientation*2] = (int)floor(s);
315 extent2[orientation*2+1] = extent2[orientation*2];
318 // Do not display a contour if there is no contour on this slice
319 if (extent2[orientation*2+1] > extent3[orientation*2+1]) {
320 actor->VisibilityOff();
323 else actor->VisibilityOn();
329 clipper->SetOutputWholeExtent(extent2[0],extent2[1],extent2[2],
330 extent2[3],extent2[4],extent2[5]);
332 if (mHiddenImage) delete extent2;
334 // Move the actor to be visible
335 switch (orientation) {
337 actor->SetPosition(-1,0,0);
339 // DD(mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0]);
340 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0] > slice) {
341 actor->SetPosition(1,0,0);
343 actor->SetPosition(-1,0,0);
347 actor->SetPosition(0,-1,0);
349 // DD(mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1]);
350 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1] > slice) {
351 actor->SetPosition(0,1,0);
353 actor->SetPosition(0,-1,0);
358 actor->SetPosition(0,0,-1);
360 DD(mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2]);
361 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2] > slice) {
363 actor->SetPosition(0,0,1);
366 actor->SetPosition(0,0,-1);
374 //------------------------------------------------------------------------------