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() {
34 mHiddenImageIsUsed = false;
35 mDisplayModeIsPreserveMemory = true;
37 //------------------------------------------------------------------------------
40 //------------------------------------------------------------------------------
41 vvImageContour::~vvImageContour() {
42 for (unsigned int i = 0; i < mSlicer->GetImage()->GetVTKImages().size(); i++) {
43 mSlicer->GetRenderer()->RemoveActor(mSquaresActorList[i]);
45 mSquaresActorList.clear();
49 //------------------------------------------------------------------------------
52 //------------------------------------------------------------------------------
53 void vvImageContour::setSlicer(vvSlicer * slicer) {
55 // Create an actor for each time slice
56 for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
57 vtkImageClip * mClipper = vtkImageClip::New();
58 vtkMarchingSquares * mSquares = vtkMarchingSquares::New();
59 vtkActor * mSquaresActor = vtkActor::New();
60 createNewActor(&mSquaresActor, &mSquares, &mClipper, numImage);
61 mSquaresActorList.push_back(mSquaresActor);
62 mSquaresList.push_back(mSquares);
63 mClipperList.push_back(mClipper);
66 //------------------------------------------------------------------------------
69 //------------------------------------------------------------------------------
70 void vvImageContour::setImage(vvImage::Pointer image) {
71 for (unsigned int numImage = 0; numImage < image->GetVTKImages().size(); numImage++) {
72 mClipperList[numImage]->SetInput(image->GetVTKImages()[numImage]);
74 mHiddenImageIsUsed = true;
77 //------------------------------------------------------------------------------
80 //------------------------------------------------------------------------------
81 void vvImageContour::setPreserveMemoryModeEnabled(bool b) {
82 // FastCache mode work only if threshold is always the same
83 if (mDisplayModeIsPreserveMemory == b) return;
84 mDisplayModeIsPreserveMemory = b;
87 initializeCacheMode();
90 for(unsigned int d=0; d<mListOfCachedContourActors.size(); d++)
91 mListOfCachedContourActors[d].clear();
92 mListOfCachedContourActors.clear();
96 //------------------------------------------------------------------------------
99 //------------------------------------------------------------------------------
100 void vvImageContour::setColor(double r, double g, double b) {
101 for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
102 mSquaresActorList[i]->GetProperty()->SetColor(r,g,b);
105 //------------------------------------------------------------------------------
108 //------------------------------------------------------------------------------
109 void vvImageContour::SetLineWidth(double w) {
110 for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
111 mSquaresActorList[i]->GetProperty()->SetLineWidth(w);
114 //------------------------------------------------------------------------------
117 //------------------------------------------------------------------------------
118 void vvImageContour::hideActors() {
119 if (!mSlicer) return;
120 mSlice = mSlicer->GetSlice();
121 for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
122 mSquaresActorList[i]->VisibilityOff();
125 //------------------------------------------------------------------------------
128 //------------------------------------------------------------------------------
129 void vvImageContour::showActors() {
130 if (!mSlicer) return;
131 mSlice = mSlicer->GetSlice();
132 mTSlice = mSlicer->GetTSlice();
133 // for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
134 mSquaresActorList[mTSlice]->VisibilityOn();
138 //------------------------------------------------------------------------------
141 //------------------------------------------------------------------------------
142 void vvImageContour::update(double value) {
144 if (!mSlicer) return;
145 if (mPreviousValue == value) {
146 if (mPreviousSlice == mSlicer->GetSlice()) {
147 if (mPreviousTSlice == mSlicer->GetTSlice()) {
148 // DD("vvImageContour::update nothing");
149 return; // Nothing to do
154 // Get current threshold value
158 mSlice = mSlicer->GetSlice();
159 // DD(mDisplayModeIsPreserveMemory);
160 if (mDisplayModeIsPreserveMemory) {
161 updateWithPreserveMemoryMode();
164 updateWithFastCacheMode();
168 mSlicer->Render(); //DS ---> REMOVE ??
171 mPreviousTSlice = mSlicer->GetTSlice();
172 mPreviousSlice = mSlicer->GetSlice();
173 mPreviousValue = value;
175 //------------------------------------------------------------------------------
178 //------------------------------------------------------------------------------
179 void vvImageContour::updateWithPreserveMemoryMode() {
180 // Only change actor visibility if tslice change
183 int mPreviousTslice = mTSlice;
185 // if (mTSlice != mSlicer->GetTSlice()) {
186 // if (mTSlice != -1) mTsliceToSetOff = mTSlice;
187 // mSquaresActorList[mTSlice]->VisibilityOff();
188 mTSlice = mSlicer->GetTSlice();
192 vtkMarchingSquares * mSquares = mSquaresList[mTSlice];
193 vtkImageClip * mClipper = mClipperList[mTSlice];
194 vtkActor * mSquaresActor = mSquaresActorList[mTSlice];
195 int orientation = computeCurrentOrientation();
199 //DD(mPreviousTslice);
200 updateActor(mSquaresActor, mSquares, mClipper, mValue, orientation, mSlice);
201 mSquaresActorList[mTSlice]->VisibilityOn();
202 if (mPreviousTslice != mTSlice) {
203 if (mPreviousTslice != -1) mSquaresActorList[mPreviousTslice]->VisibilityOff();
206 //------------------------------------------------------------------------------
209 //------------------------------------------------------------------------------
210 void vvImageContour::initializeCacheMode() {
211 mPreviousSlice = mPreviousOrientation = 0;
212 int dim = mSlicer->GetImage()->GetNumberOfDimensions();
214 mListOfCachedContourActors.resize(dim);
215 for(int d=0; d<dim; d++) {
216 int size = mSlicer->GetImage()->GetSize()[d];
218 mListOfCachedContourActors[d].resize(size);
219 for(int j=0; j<size; j++) {
220 mListOfCachedContourActors[d][j] = NULL;
224 //------------------------------------------------------------------------------
227 //------------------------------------------------------------------------------
228 int vvImageContour::computeCurrentOrientation() {
229 // Get extent of image in the slicer
230 int* extent = mSlicer->GetImageActor()->GetDisplayExtent();
232 // Compute orientation
234 for (orientation = 0; orientation < 6;orientation = orientation+2) {
235 if (extent[orientation] == extent[orientation+1]) {
239 orientation = orientation/2;
242 //------------------------------------------------------------------------------
245 //------------------------------------------------------------------------------
246 void vvImageContour::updateWithFastCacheMode() {
247 // Compute orientation
248 int orientation = computeCurrentOrientation();
250 if ((mPreviousSlice == mSlice) && (mPreviousOrientation == orientation)) return;
252 vtkActor * actor = mListOfCachedContourActors[orientation][mSlice];
254 mListOfCachedContourActors[orientation][mSlice]->VisibilityOn();
257 vtkImageClip * mClipper;
258 vtkMarchingSquares * mSquares;
259 vtkActor * mSquaresActor;
260 createNewActor(&mSquaresActor, &mSquares, &mClipper, 0);
261 updateActor(mSquaresActor, mSquares, mClipper, mValue, orientation, mSlice);
262 mListOfCachedContourActors[orientation][mSlice] = mSquaresActor;
263 mSquaresActor->VisibilityOn();
266 if (mListOfCachedContourActors[mPreviousOrientation][mPreviousSlice] != NULL)
267 mListOfCachedContourActors[mPreviousOrientation][mPreviousSlice]->VisibilityOff();
268 mPreviousSlice = mSlice;
269 mPreviousOrientation = orientation;
271 //------------------------------------------------------------------------------
274 //------------------------------------------------------------------------------
275 void vvImageContour::createNewActor(vtkActor ** actor,
276 vtkMarchingSquares ** squares,
277 vtkImageClip ** clipper,
279 vtkActor * mSquaresActor = (*actor = vtkActor::New());
280 vtkImageClip * mClipper = (*clipper = vtkImageClip::New());
281 vtkMarchingSquares * mSquares = (*squares = vtkMarchingSquares::New());
282 vtkPolyDataMapper * mSquaresMapper = vtkPolyDataMapper::New();
284 if (mHiddenImageIsUsed)
285 mClipper->SetInput(mHiddenImage->GetVTKImages()[0]);
287 mClipper->SetInput(mSlicer->GetImage()->GetVTKImages()[numImage]);
288 mSquares->SetInput(mClipper->GetOutput());
289 mSquaresMapper->SetInput(mSquares->GetOutput());
290 mSquaresMapper->ScalarVisibilityOff();
291 mSquaresActor->SetMapper(mSquaresMapper);
292 mSquaresActor->GetProperty()->SetColor(1.0,0,0);
293 mSquaresActor->SetPickable(0);
294 mSquaresActor->VisibilityOff();
295 mSlicer->GetRenderer()->AddActor(mSquaresActor);
297 //------------------------------------------------------------------------------
300 //------------------------------------------------------------------------------
301 void vvImageContour::updateActor(vtkActor * actor,
302 vtkMarchingSquares * squares,
303 vtkImageClip * clipper,
304 double threshold, int orientation, int slice) {
306 // Set parameter for the MarchigSquare
307 squares->SetValue(0, threshold);
310 int* extent = mSlicer->GetImageActor()->GetDisplayExtent();
312 // Change extent if needed
314 if (mHiddenImageIsUsed) {
315 extent2 = new int[6];
317 extent3 = mHiddenImage->GetFirstVTKImageData()->GetExtent();
318 for(int i=0; i<6; i++) extent2[i] = extent3[i];
320 double s = (double)extent[orientation*2]*(double)mSlicer->GetImage()->GetSpacing()[orientation]; // in mm
321 s = s+mSlicer->GetImage()->GetOrigin()[orientation]; // from origin
322 s = s-mHiddenImage->GetFirstVTKImageData()->GetOrigin()[orientation]; // from corner second image
323 s = s/mHiddenImage->GetFirstVTKImageData()->GetSpacing()[orientation]; // in voxel
326 extent2[orientation*2] = extent2[orientation*2+1] = (int)floor(s);
329 extent2[orientation*2] = (int)floor(s);
330 extent2[orientation*2+1] = extent2[orientation*2];
336 clipper->SetOutputWholeExtent(extent2[0],extent2[1],extent2[2],
337 extent2[3],extent2[4],extent2[5]);
339 if (mHiddenImage) delete extent2;
341 // Move the actor to be visible
345 //TO SIMPLiFY :!!!!!!!!! == ???????
346 // actor->SetPosition(-1,-1,-1);
348 switch (orientation) {
350 // DD(mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0]);
351 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0] > slice) {
352 actor->SetPosition(1,0,0);
355 actor->SetPosition(-1,0,0);
359 // DD(mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1]);
360 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1] > slice) {
361 actor->SetPosition(0,1,0);
364 actor->SetPosition(0,-1,0);
368 // DD(mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2]);
369 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2] > slice) {
371 actor->SetPosition(0,0,1);
375 actor->SetPosition(0,0,-1);
382 //------------------------------------------------------------------------------