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;
37 SetPreserveMemoryModeEnabled(true);
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;
89 clitkExceptionMacro("TODO : not implemented yet");
91 InitializeCacheMode();
94 for(unsigned int d=0; d<mListOfCachedContourActors.size(); d++)
95 mListOfCachedContourActors[d].clear();
96 mListOfCachedContourActors.clear();
100 //------------------------------------------------------------------------------
103 //------------------------------------------------------------------------------
104 void vvImageContour::SetColor(double r, double g, double b) {
105 for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
106 mSquaresActorList[i]->GetProperty()->SetColor(r,g,b);
109 //------------------------------------------------------------------------------
112 //------------------------------------------------------------------------------
113 void vvImageContour::SetLineWidth(double w)
115 for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
116 mSquaresActorList[i]->GetProperty()->SetLineWidth(w);
119 //------------------------------------------------------------------------------
122 //------------------------------------------------------------------------------
123 void vvImageContour::HideActors() {
124 if (!mSlicer) return;
125 mSlice = mSlicer->GetSlice();
126 for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
127 mSquaresActorList[i]->VisibilityOff();
130 //------------------------------------------------------------------------------
133 //------------------------------------------------------------------------------
134 void vvImageContour::ShowActors() {
135 if (!mSlicer) return;
136 mSlice = mSlicer->GetSlice();
137 mTSlice = mSlicer->GetTSlice();
138 mSquaresActorList[mTSlice]->VisibilityOn();
141 //------------------------------------------------------------------------------
144 //------------------------------------------------------------------------------
145 void vvImageContour::Update(double value) {
146 if (!mSlicer) return;
147 if (mPreviousValue == value) {
148 if (mPreviousSlice == mSlicer->GetSlice()) {
149 if (mPreviousTSlice == mSlicer->GetTSlice()) {
150 return; // Nothing to do
155 // Get current threshold value
159 mSlice = mSlicer->GetSlice();
161 if (mDisplayModeIsPreserveMemory) {
162 UpdateWithPreserveMemoryMode();
165 UpdateWithFastCacheMode();
168 // mSlicer->Render(); //DS ---> REMOVE ??
170 mPreviousTSlice = mSlicer->GetTSlice();
171 mPreviousSlice = mSlicer->GetSlice();
172 mPreviousValue = value;
174 //------------------------------------------------------------------------------
177 //------------------------------------------------------------------------------
178 void vvImageContour::UpdateWithPreserveMemoryMode() {
179 // Only change actor visibility if tslice change
180 int mPreviousTslice = mTSlice;
181 mTSlice = mSlicer->GetTSlice();
183 vtkMarchingSquares * mSquares = mSquaresList[mTSlice];
184 vtkImageClip * mClipper = mClipperList[mTSlice];
185 vtkActor * mSquaresActor = mSquaresActorList[mTSlice];
186 int orientation = ComputeCurrentOrientation();
188 UpdateActor(mSquaresActor, mSquares, mClipper, mValue, orientation, mSlice);
189 mSquaresActorList[mTSlice]->VisibilityOn();
191 if (mPreviousTslice != mTSlice) {
192 if (mPreviousTslice != -1) mSquaresActorList[mPreviousTslice]->VisibilityOff();
195 //------------------------------------------------------------------------------
198 //------------------------------------------------------------------------------
199 void vvImageContour::InitializeCacheMode() {
200 mPreviousSlice = mPreviousOrientation = 0;
201 int dim = mSlicer->GetImage()->GetNumberOfDimensions();
203 mListOfCachedContourActors.resize(dim);
204 for(int d=0; d<dim; d++) {
205 int size = mSlicer->GetImage()->GetSize()[d];
206 mListOfCachedContourActors[d].resize(size);
207 for(int j=0; j<size; j++) {
208 mListOfCachedContourActors[d][j] = NULL;
212 //------------------------------------------------------------------------------
215 //------------------------------------------------------------------------------
216 int vvImageContour::ComputeCurrentOrientation() {
217 // Get extent of image in the slicer
218 int* extent = mSlicer->GetImageActor()->GetDisplayExtent();
220 // Compute orientation
222 for (orientation = 0; orientation < 6; orientation = orientation+2) {
223 if (extent[orientation] == extent[orientation+1]) {
227 orientation = orientation/2;
230 //------------------------------------------------------------------------------
233 //------------------------------------------------------------------------------
234 void vvImageContour::UpdateWithFastCacheMode() {
235 // Compute orientation
236 int orientation = ComputeCurrentOrientation();
238 if ((mPreviousSlice == mSlice) && (mPreviousOrientation == orientation)) return;
240 vtkActor * actor = mListOfCachedContourActors[orientation][mSlice];
242 mListOfCachedContourActors[orientation][mSlice]->VisibilityOn();
244 vtkImageClip * mClipper;
245 vtkMarchingSquares * mSquares;
246 vtkActor * mSquaresActor;
247 CreateNewActor(&mSquaresActor, &mSquares, &mClipper, 0);
248 UpdateActor(mSquaresActor, mSquares, mClipper, mValue, orientation, mSlice);
249 mListOfCachedContourActors[orientation][mSlice] = mSquaresActor;
250 mSquaresActor->VisibilityOn();
253 if (mListOfCachedContourActors[mPreviousOrientation][mPreviousSlice] != NULL)
254 mListOfCachedContourActors[mPreviousOrientation][mPreviousSlice]->VisibilityOff();
255 mPreviousSlice = mSlice;
256 mPreviousOrientation = orientation;
258 //------------------------------------------------------------------------------
261 //------------------------------------------------------------------------------
262 void vvImageContour::CreateNewActor(vtkActor ** actor,
263 vtkMarchingSquares ** squares,
264 vtkImageClip ** clipper,
266 vtkActor * mSquaresActor = (*actor = vtkActor::New());
267 vtkImageClip * mClipper = (*clipper = vtkImageClip::New());
268 vtkMarchingSquares * mSquares = (*squares = vtkMarchingSquares::New());
269 vtkPolyDataMapper * mSquaresMapper = vtkPolyDataMapper::New();
271 if (mHiddenImageIsUsed)
272 mClipper->SetInput(mHiddenImage->GetVTKImages()[0]);
274 mClipper->SetInput(mSlicer->GetImage()->GetVTKImages()[numImage]);
275 mSquares->SetInput(mClipper->GetOutput());
276 mSquaresMapper->SetInput(mSquares->GetOutput());
277 mSquaresMapper->ScalarVisibilityOff();
278 mSquaresActor->SetMapper(mSquaresMapper);
279 mSquaresActor->GetProperty()->SetColor(1.0,0,0);
280 mSquaresActor->SetPickable(0);
281 mSquaresActor->VisibilityOff();
282 mSlicer->GetRenderer()->AddActor(mSquaresActor);
284 //------------------------------------------------------------------------------
287 //------------------------------------------------------------------------------
288 void vvImageContour::UpdateActor(vtkActor * actor,
289 vtkMarchingSquares * squares,
290 vtkImageClip * clipper,
291 double threshold, int orientation, int slice) {
292 // Set parameter for the MarchigSquare
293 squares->SetValue(0, threshold);
296 int* extent = mSlicer->GetImageActor()->GetDisplayExtent();
298 // Change extent if needed
300 if (mHiddenImageIsUsed) {
301 extent2 = new int[6];
303 extent3 = mHiddenImage->GetFirstVTKImageData()->GetExtent();
305 for(int i=0; i<6; i++) extent2[i] = extent3[i];
307 double s = (double)extent[orientation*2]*(double)mSlicer->GetImage()->GetSpacing()[orientation]; // in mm
308 s = s+mSlicer->GetImage()->GetOrigin()[orientation]; // from origin
309 s = s-mHiddenImage->GetFirstVTKImageData()->GetOrigin()[orientation]; // from corner second image
310 s = s/mHiddenImage->GetFirstVTKImageData()->GetSpacing()[orientation]; // in voxel
313 extent2[orientation*2] = extent2[orientation*2+1] = (int)floor(s);
315 extent2[orientation*2] = (int)floor(s);
316 extent2[orientation*2+1] = extent2[orientation*2];
319 // Do not display a contour if there is no contour on this slice
320 if (extent2[orientation*2+1] > extent3[orientation*2+1]) {
321 actor->VisibilityOff();
324 else actor->VisibilityOn();
330 clipper->SetOutputWholeExtent(extent2[0],extent2[1],extent2[2],
331 extent2[3],extent2[4],extent2[5]);
333 if (mHiddenImage) delete extent2;
335 // Move the actor to be visible
336 switch (orientation) {
338 actor->SetPosition(-1,0,0);
340 // DD(mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0]);
341 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0] > slice) {
342 actor->SetPosition(1,0,0);
344 actor->SetPosition(-1,0,0);
348 actor->SetPosition(0,-1,0);
350 // DD(mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1]);
351 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1] > slice) {
352 actor->SetPosition(0,1,0);
354 actor->SetPosition(0,-1,0);
359 actor->SetPosition(0,0,-1);
361 DD(mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2]);
362 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2] > slice) {
364 actor->SetPosition(0,0,1);
367 actor->SetPosition(0,0,-1);
375 //------------------------------------------------------------------------------