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://www.centreleonberard.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 <vtkVersion.h>
22 #include <vtkImageActor.h>
23 #include <vtkCamera.h>
24 #include <vtkRenderer.h>
25 #include <vtkMarchingSquares.h>
26 #include <vtkImageClip.h>
27 #include <vtkImageData.h>
28 #include <vtkPolyDataMapper.h>
29 #include <vtkProperty.h>
31 //------------------------------------------------------------------------------
32 vvImageContour::vvImageContour()
36 mHiddenImageIsUsed = false;
37 mDisplayModeIsPreserveMemory = true;
38 SetPreserveMemoryModeEnabled(true);
39 mPreviousOrientation = -1;
43 //------------------------------------------------------------------------------
46 //------------------------------------------------------------------------------
47 vvImageContour::~vvImageContour()
49 mSquaresActorList.clear();
51 //------------------------------------------------------------------------------
54 //------------------------------------------------------------------------------
55 void vvImageContour::RemoveActors()
57 for (unsigned int i = 0; i < mSquaresActorList.size(); i++) {
60 if (mSlicer->GetRenderer() != NULL) {
61 if (mSquaresActorList[i] != NULL) {
62 mSlicer->GetRenderer()->RemoveActor(mSquaresActorList[i]);
69 //------------------------------------------------------------------------------
72 //------------------------------------------------------------------------------
73 void vvImageContour::SetSlicer(vvSlicer * slicer) {
75 // Create an actor for each time slice
76 for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
77 CreateNewActor(numImage);
80 //------------------------------------------------------------------------------
83 //------------------------------------------------------------------------------
84 void vvImageContour::SetImage(vvImage::Pointer image) {
85 for (unsigned int numImage = 0; numImage < image->GetVTKImages().size(); numImage++) {
86 #if VTK_MAJOR_VERSION <= 5
87 mClipperList[numImage]->SetInput(image->GetVTKImages()[numImage]);
89 mClipperList[numImage]->SetInputData(image->GetVTKImages()[numImage]);
92 mHiddenImageIsUsed = true;
95 //------------------------------------------------------------------------------
98 //------------------------------------------------------------------------------
99 void vvImageContour::SetPreserveMemoryModeEnabled(bool b) {
100 // FastCache mode work only if threshold is always the same
101 if (mDisplayModeIsPreserveMemory == b) return;
102 mDisplayModeIsPreserveMemory = b;
104 clitkExceptionMacro("TODO : not implemented yet");
106 InitializeCacheMode();
109 for(unsigned int d=0; d<mListOfCachedContourActors.size(); d++)
110 mListOfCachedContourActors[d].clear();
111 mListOfCachedContourActors.clear();
115 //------------------------------------------------------------------------------
118 //------------------------------------------------------------------------------
119 void vvImageContour::SetColor(double r, double g, double b) {
120 for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
121 mSquaresActorList[i]->GetProperty()->SetColor(r,g,b);
124 //------------------------------------------------------------------------------
127 //------------------------------------------------------------------------------
128 void vvImageContour::SetLineWidth(double w)
130 for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
131 mSquaresActorList[i]->GetProperty()->SetLineWidth(w);
134 //------------------------------------------------------------------------------
137 //------------------------------------------------------------------------------
138 void vvImageContour::HideActors() {
139 if (!mSlicer) return;
140 mSlice = mSlicer->GetSlice();
141 for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
142 mSquaresActorList[i]->VisibilityOff();
145 //------------------------------------------------------------------------------
148 //------------------------------------------------------------------------------
149 void vvImageContour::ShowActors() {
150 if (!mSlicer) return;
151 mSlice = mSlicer->GetSlice();
152 mTSlice = mSlicer->GetTSlice();
153 mSquaresActorList[mTSlice]->VisibilityOn();
156 //------------------------------------------------------------------------------
159 //------------------------------------------------------------------------------
160 void vvImageContour::SetDepth(double d)
163 // Move the actor to be visible
164 double position[3] = {0, 0, 0};
165 int orientation = ComputeCurrentOrientation();
166 position[orientation] = -mDepth;
168 for(unsigned int i=0; i<mSquaresActorList.size(); i++)
169 mSquaresActorList[i]->SetPosition(position);
171 //------------------------------------------------------------------------------
173 //------------------------------------------------------------------------------
174 void vvImageContour::Update(double value) {
175 if (!mSlicer) return;
176 if (mPreviousValue == value) {
177 if (mPreviousSlice == mSlicer->GetSlice()) {
178 if (mPreviousTSlice == mSlicer->GetTSlice()) {
179 if (mPreviousOrientation == ComputeCurrentOrientation()) {
180 return; // Nothing to do
186 // Get current threshold value
190 mSlice = mSlicer->GetSlice();
192 if (mDisplayModeIsPreserveMemory) {
193 UpdateWithPreserveMemoryMode();
196 UpdateWithFastCacheMode();
199 //mSlicer->Render(); //DS ---> REMOVE ??
201 mPreviousTSlice = mSlicer->GetTSlice();
202 mPreviousSlice = mSlicer->GetSlice();
203 mPreviousValue = value;
204 mPreviousOrientation = ComputeCurrentOrientation();
206 //------------------------------------------------------------------------------
209 //------------------------------------------------------------------------------
210 void vvImageContour::UpdateWithPreserveMemoryMode() {
211 // Only change actor visibility if tslice change
212 mPreviousTslice = mTSlice;
213 mTSlice = mSlicer->GetTSlice();
215 vtkMarchingSquares * mSquares = mSquaresList[mTSlice];
216 vtkPolyDataMapper* mapper = mSquaresMapperList[mTSlice];
217 vtkImageClip * mClipper = mClipperList[mTSlice];
218 vtkActor * mSquaresActor = mSquaresActorList[mTSlice];
219 int orientation = ComputeCurrentOrientation();
221 UpdateActor(mSquaresActor, mapper, mSquares, mClipper, mValue, orientation, mSlice);
223 if (mPreviousTslice != mTSlice) {
224 if (mPreviousTslice != -1) mSquaresActorList[mPreviousTslice]->VisibilityOff();
229 //------------------------------------------------------------------------------
232 //------------------------------------------------------------------------------
233 void vvImageContour::InitializeCacheMode() {
234 clitkExceptionMacro("TODO : not implemented yet");
235 mPreviousSlice = mPreviousOrientation = 0;
236 int dim = mSlicer->GetImage()->GetNumberOfDimensions();
238 mListOfCachedContourActors.resize(dim);
239 for(int d=0; d<dim; d++) {
240 int size = mSlicer->GetImage()->GetSize()[d];
241 mListOfCachedContourActors[d].resize(size);
242 for(int j=0; j<size; j++) {
243 mListOfCachedContourActors[d][j] = NULL;
247 //------------------------------------------------------------------------------
250 //------------------------------------------------------------------------------
251 int vvImageContour::ComputeCurrentOrientation() {
252 // Get extent of image in the slicer
253 int* extent = mSlicer->GetImageActor()->GetDisplayExtent();
255 // Compute orientation
257 for (orientation = 0; orientation < 6; orientation = orientation+2) {
258 if (extent[orientation] == extent[orientation+1]) {
262 orientation = orientation/2;
265 //------------------------------------------------------------------------------
268 //------------------------------------------------------------------------------
269 void vvImageContour::UpdateWithFastCacheMode() {
270 clitkExceptionMacro("TODO : not implemented yet");
272 // Compute orientation
273 int orientation = ComputeCurrentOrientation();
275 if ((mPreviousSlice == mSlice) && (mPreviousOrientation == orientation)) return;
277 vtkActor * actor = mListOfCachedContourActors[orientation][mSlice];
279 mListOfCachedContourActors[orientation][mSlice]->VisibilityOn();
282 //SR: commented out, this code is never reached anyway
283 //UpdateActor(mSquaresActor, mSquares, mClipper, mValue, orientation, mSlice);
284 //mListOfCachedContourActors[orientation][mSlice] = mSquaresActor;
285 //mSquaresActor->VisibilityOn();
288 if (mListOfCachedContourActors[mPreviousOrientation][mPreviousSlice] != NULL)
289 mListOfCachedContourActors[mPreviousOrientation][mPreviousSlice]->VisibilityOff();
290 mPreviousSlice = mSlice;
291 mPreviousOrientation = orientation;
293 //------------------------------------------------------------------------------
296 //------------------------------------------------------------------------------
297 void vvImageContour::CreateNewActor(int numImage) {
298 vtkSmartPointer<vtkActor> squaresActor = vtkSmartPointer<vtkActor>::New();
299 vtkSmartPointer<vtkImageClip> clipper = vtkSmartPointer<vtkImageClip>::New();
300 vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
301 vtkSmartPointer<vtkPolyDataMapper> squaresMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
303 if (mHiddenImageIsUsed) {
304 #if VTK_MAJOR_VERSION <= 5
305 clipper->SetInput(mHiddenImage->GetVTKImages()[0]);
307 clipper->SetInputData(mHiddenImage->GetVTKImages()[0]);
310 #if VTK_MAJOR_VERSION <= 5
311 clipper->SetInput(mSlicer->GetImage()->GetVTKImages()[numImage]);
313 clipper->SetInputData(mSlicer->GetImage()->GetVTKImages()[numImage]);
317 #if VTK_MAJOR_VERSION <= 5
318 squares->SetInput(clipper->GetOutput());
319 squaresMapper->SetInput(squares->GetOutput());
321 squares->SetInputData(clipper->GetOutput());
322 squaresMapper->SetInputData(squares->GetOutput());
324 squaresMapper->ScalarVisibilityOff();
325 squaresActor->SetMapper(squaresMapper);
326 squaresActor->GetProperty()->SetColor(1.0,0,0);
327 squaresActor->SetPickable(0);
328 squaresActor->VisibilityOff();
329 mSlicer->GetRenderer()->AddActor(squaresActor);
331 mSquaresActorList.push_back(squaresActor);
332 mClipperList.push_back(clipper);
333 mSquaresList.push_back(squares);
334 mSquaresMapperList.push_back(squaresMapper);
336 //------------------------------------------------------------------------------
339 //------------------------------------------------------------------------------
340 void vvImageContour::UpdateActor(vtkActor * actor,
341 vtkPolyDataMapper * mapper,
342 vtkMarchingSquares * squares,
343 vtkImageClip * clipper,
344 double threshold, int orientation, int slice) {
345 // Set parameter for the MarchigSquare
346 squares->SetValue(0, threshold);
349 int* extent = mSlicer->GetImageActor()->GetDisplayExtent();
351 // Change extent if needed
353 if (mHiddenImageIsUsed) {
354 extent2 = new int[6];
356 extent3 = mHiddenImage->GetFirstVTKImageData()->GetExtent();
358 for(int i=0; i<6; i++) extent2[i] = extent3[i];
360 double s = (double)extent[orientation*2]*(double)mSlicer->GetImage()->GetSpacing()[orientation]; // in mm
361 s = s+mSlicer->GetImage()->GetOrigin()[orientation]; // from origin
362 s = s-mHiddenImage->GetFirstVTKImageData()->GetOrigin()[orientation]; // from corner second image
363 s = s/mHiddenImage->GetFirstVTKImageData()->GetSpacing()[orientation]; // in voxel
365 // Rint to the closest slice
366 extent2[orientation*2+1] = extent2[orientation*2] = (int)lrint(s);
368 // Do not display a contour if there is no contour on this slice
369 // DD(extent2[orientation*2+1]);
370 // DD(extent3[orientation*2+1]);
371 // DD(extent2[orientation*2]);
372 // DD(extent3[orientation*2]);
373 if ((extent2[orientation*2+1] > extent3[orientation*2+1]) ||
374 (extent2[orientation*2] < extent3[orientation*2])) {
375 actor->VisibilityOff();
378 else actor->VisibilityOn();
382 actor->VisibilityOn();
385 clipper->SetOutputWholeExtent(extent2[0],extent2[1],extent2[2],
386 extent2[3],extent2[4],extent2[5]);
388 if (mHiddenImageIsUsed) delete extent2;
390 // Move the actor to be visible
392 // double position[3] = {0, 0, 0};
394 // position[orientation] = -mDepth;
395 // actor->SetPosition(position);
399 //------------------------------------------------------------------------------