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 = true;
38 //------------------------------------------------------------------------------
41 //------------------------------------------------------------------------------
42 vvImageContour::~vvImageContour()
44 for (unsigned int i = 0; i < mSlicer->GetImage()->GetVTKImages().size(); i++) {
45 mSlicer->GetRenderer()->RemoveActor(mSquaresActorList[i]);
47 mSquaresActorList.clear();
51 //------------------------------------------------------------------------------
54 //------------------------------------------------------------------------------
55 void vvImageContour::SetSlicer(vvSlicer * slicer) {
57 // Create an actor for each time slice
58 for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
59 vtkImageClip * mClipper = vtkImageClip::New();
60 vtkMarchingSquares * mSquares = vtkMarchingSquares::New();
61 vtkActor * mSquaresActor = vtkActor::New();
62 CreateNewActor(&mSquaresActor, &mSquares, &mClipper, numImage);
63 mSquaresActorList.push_back(mSquaresActor);
64 mSquaresList.push_back(mSquares);
65 mClipperList.push_back(mClipper);
68 //------------------------------------------------------------------------------
71 //------------------------------------------------------------------------------
72 void vvImageContour::SetImage(vvImage::Pointer image) {
73 for (unsigned int numImage = 0; numImage < image->GetVTKImages().size(); numImage++) {
74 mClipperList[numImage]->SetInput(image->GetVTKImages()[numImage]);
76 mHiddenImageIsUsed = true;
79 //------------------------------------------------------------------------------
82 //------------------------------------------------------------------------------
83 void vvImageContour::SetPreserveMemoryModeEnabled(bool b) {
84 // FastCache mode work only if threshold is always the same
85 if (mDisplayModeIsPreserveMemory == b) return;
86 mDisplayModeIsPreserveMemory = b;
89 InitializeCacheMode();
92 for(unsigned int d=0; d<mListOfCachedContourActors.size(); d++)
93 mListOfCachedContourActors[d].clear();
94 mListOfCachedContourActors.clear();
98 //------------------------------------------------------------------------------
101 //------------------------------------------------------------------------------
102 void vvImageContour::SetColor(double r, double g, double b) {
103 for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
104 mSquaresActorList[i]->GetProperty()->SetColor(r,g,b);
107 //------------------------------------------------------------------------------
110 //------------------------------------------------------------------------------
111 void vvImageContour::SetLineWidth(double w)
113 for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
114 mSquaresActorList[i]->GetProperty()->SetLineWidth(w);
117 //------------------------------------------------------------------------------
120 //------------------------------------------------------------------------------
121 void vvImageContour::HideActors() {
122 if (!mSlicer) return;
123 mSlice = mSlicer->GetSlice();
124 for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
125 mSquaresActorList[i]->VisibilityOff();
128 //------------------------------------------------------------------------------
131 //------------------------------------------------------------------------------
132 void vvImageContour::ShowActors() {
133 if (!mSlicer) return;
134 mSlice = mSlicer->GetSlice();
135 mTSlice = mSlicer->GetTSlice();
136 // for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
137 mSquaresActorList[mTSlice]->VisibilityOn();
141 //------------------------------------------------------------------------------
144 //------------------------------------------------------------------------------
145 void vvImageContour::Update(double value) {
147 if (!mSlicer) return;
148 if (mPreviousValue == value) {
149 if (mPreviousSlice == mSlicer->GetSlice()) {
150 if (mPreviousTSlice == mSlicer->GetTSlice()) {
151 // DD("vvImageContour::update nothing");
152 return; // Nothing to do
157 // Get current threshold value
161 mSlice = mSlicer->GetSlice();
162 // DD(mDisplayModeIsPreserveMemory);
163 if (mDisplayModeIsPreserveMemory) {
164 UpdateWithPreserveMemoryMode();
167 UpdateWithFastCacheMode();
170 // mSlicer->Render(); //DS ---> REMOVE ??
172 mPreviousTSlice = mSlicer->GetTSlice();
173 mPreviousSlice = mSlicer->GetSlice();
174 mPreviousValue = value;
176 //------------------------------------------------------------------------------
179 //------------------------------------------------------------------------------
180 void vvImageContour::UpdateWithPreserveMemoryMode() {
181 // Only change actor visibility if tslice change
184 int mPreviousTslice = mTSlice;
186 // if (mTSlice != mSlicer->GetTSlice()) {
187 // if (mTSlice != -1) mTsliceToSetOff = mTSlice;
188 // mSquaresActorList[mTSlice]->VisibilityOff();
189 mTSlice = mSlicer->GetTSlice();
193 vtkMarchingSquares * mSquares = mSquaresList[mTSlice];
194 vtkImageClip * mClipper = mClipperList[mTSlice];
195 vtkActor * mSquaresActor = mSquaresActorList[mTSlice];
196 int orientation = ComputeCurrentOrientation();
200 //DD(mPreviousTslice);
201 UpdateActor(mSquaresActor, mSquares, mClipper, mValue, orientation, mSlice);
202 mSquaresActorList[mTSlice]->VisibilityOn();
203 if (mPreviousTslice != mTSlice) {
204 if (mPreviousTslice != -1) mSquaresActorList[mPreviousTslice]->VisibilityOff();
207 //------------------------------------------------------------------------------
210 //------------------------------------------------------------------------------
211 void vvImageContour::InitializeCacheMode() {
212 mPreviousSlice = mPreviousOrientation = 0;
213 int dim = mSlicer->GetImage()->GetNumberOfDimensions();
215 mListOfCachedContourActors.resize(dim);
216 for(int d=0; d<dim; d++) {
217 int size = mSlicer->GetImage()->GetSize()[d];
219 mListOfCachedContourActors[d].resize(size);
220 for(int j=0; j<size; j++) {
221 mListOfCachedContourActors[d][j] = NULL;
225 //------------------------------------------------------------------------------
228 //------------------------------------------------------------------------------
229 int vvImageContour::ComputeCurrentOrientation() {
230 // Get extent of image in the slicer
231 int* extent = mSlicer->GetImageActor()->GetDisplayExtent();
233 // Compute orientation
235 for (orientation = 0; orientation < 6; orientation = orientation+2) {
236 if (extent[orientation] == extent[orientation+1]) {
240 orientation = orientation/2;
243 //------------------------------------------------------------------------------
246 //------------------------------------------------------------------------------
247 void vvImageContour::UpdateWithFastCacheMode() {
248 // Compute orientation
249 int orientation = ComputeCurrentOrientation();
251 if ((mPreviousSlice == mSlice) && (mPreviousOrientation == orientation)) return;
253 vtkActor * actor = mListOfCachedContourActors[orientation][mSlice];
255 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) {
305 // Set parameter for the MarchigSquare
306 squares->SetValue(0, threshold);
309 int* extent = mSlicer->GetImageActor()->GetDisplayExtent();
311 // Change extent if needed
313 if (mHiddenImageIsUsed) {
314 extent2 = new int[6];
316 extent3 = mHiddenImage->GetFirstVTKImageData()->GetExtent();
317 for(int i=0; i<6; i++) extent2[i] = extent3[i];
319 double s = (double)extent[orientation*2]*(double)mSlicer->GetImage()->GetSpacing()[orientation]; // in mm
320 s = s+mSlicer->GetImage()->GetOrigin()[orientation]; // from origin
321 s = s-mHiddenImage->GetFirstVTKImageData()->GetOrigin()[orientation]; // from corner second image
322 s = s/mHiddenImage->GetFirstVTKImageData()->GetSpacing()[orientation]; // in voxel
325 extent2[orientation*2] = extent2[orientation*2+1] = (int)floor(s);
327 extent2[orientation*2] = (int)floor(s);
328 extent2[orientation*2+1] = extent2[orientation*2];
333 clipper->SetOutputWholeExtent(extent2[0],extent2[1],extent2[2],
334 extent2[3],extent2[4],extent2[5]);
336 if (mHiddenImage) delete extent2;
338 // Move the actor to be visible
342 //TO SIMPLiFY :!!!!!!!!! == ???????
343 // actor->SetPosition(-1,-1,-1);
345 switch (orientation) {
347 // DD(mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0]);
348 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0] > slice) {
349 actor->SetPosition(1,0,0);
351 actor->SetPosition(-1,0,0);
355 // DD(mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1]);
356 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1] > slice) {
357 actor->SetPosition(0,1,0);
359 actor->SetPosition(0,-1,0);
363 // DD(mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2]);
364 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2] > slice) {
366 actor->SetPosition(0,0,1);
369 actor->SetPosition(0,0,-1);
376 //------------------------------------------------------------------------------