]> Creatis software - clitk.git/blob - vv/vvImageContour.cxx
- add "convert to" in context menu
[clitk.git] / vv / vvImageContour.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
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
8
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.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18
19 #include "vvImageContour.h"
20 #include <vtkImageActor.h>
21 #include <vtkCamera.h>
22 #include <vtkRenderer.h>
23 #include <vtkMarchingSquares.h>
24 #include <vtkImageClip.h>
25 #include <vtkImageData.h>
26 #include <vtkPolyDataMapper.h>
27 #include <vtkProperty.h>
28
29 //------------------------------------------------------------------------------
30 vvImageContour::vvImageContour() {
31   mTSlice = -1;
32   mSlice = 0;
33 }
34 //------------------------------------------------------------------------------
35
36
37 //------------------------------------------------------------------------------
38 vvImageContour::~vvImageContour() {
39   for (unsigned int i = 0; i < mSlicer->GetImage()->GetVTKImages().size(); i++) {
40     mSlicer->GetRenderer()->RemoveActor(mSquaresActorList[i]);
41   }
42   mSquaresActorList.clear();
43   mSquaresList.clear();
44   mClipperList.clear();
45 }
46 //------------------------------------------------------------------------------
47
48
49 //------------------------------------------------------------------------------
50 void vvImageContour::setSlicer(vvSlicer * slicer) {
51   mSlicer = slicer;  
52
53   for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
54     vtkImageClip * mClipper = vtkImageClip::New();
55     vtkMarchingSquares * mSquares = vtkMarchingSquares::New();
56     vtkPolyDataMapper * mSquaresMapper = vtkPolyDataMapper::New();
57     vtkActor * mSquaresActor = vtkActor::New();
58
59     mClipper->SetInput(mSlicer->GetImage()->GetVTKImages()[numImage]);
60     mSquares->SetInput(mClipper->GetOutput());
61     mSquaresMapper->SetInput(mSquares->GetOutput());
62     mSquaresMapper->ScalarVisibilityOff();
63     mSquaresActor->SetMapper(mSquaresMapper);
64     mSquaresActor->GetProperty()->SetColor(1.0,0,0);
65     mSquaresActor->SetPickable(0);
66     mSquaresActor->VisibilityOff();
67     mSlicer->GetRenderer()->AddActor(mSquaresActor);
68     
69     mSquaresActorList.push_back(mSquaresActor);
70     mSquaresList.push_back(mSquares);
71     mClipperList.push_back(mClipper);
72   }
73 }
74 //------------------------------------------------------------------------------
75
76
77 //------------------------------------------------------------------------------
78 void vvImageContour::setColor(double r, double g, double b) {
79   for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
80     mSquaresActorList[i]->GetProperty()->SetColor(r,g,b);
81   }
82 }
83 //------------------------------------------------------------------------------
84
85
86 //------------------------------------------------------------------------------
87 void vvImageContour::hideActors() {
88   if (!mSlicer) return;
89   mSlice = mSlicer->GetSlice();
90   for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
91     mSquaresActorList[i]->VisibilityOff();
92   }
93 }
94 //------------------------------------------------------------------------------
95
96   
97 //------------------------------------------------------------------------------
98 void vvImageContour::showActors() {
99   if (!mSlicer) return;
100   mSlice = mSlicer->GetSlice();
101   mTSlice = mSlicer->GetTSlice();
102   //  for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
103   mSquaresActorList[mTSlice]->VisibilityOn();
104   update(mValue);
105   //}
106 }
107 //------------------------------------------------------------------------------
108
109   
110 //------------------------------------------------------------------------------
111 void vvImageContour::update(double value) {
112   mValue= value;
113   if (!mSlicer) return;
114
115   // how to not update if not visible ?
116
117   mSlice = mSlicer->GetSlice();
118   // Only change actor visibility if tslice change
119   if (mTSlice != mSlicer->GetTSlice()) {
120     if (mTSlice != -1) 
121       mSquaresActorList[mTSlice]->VisibilityOff();
122     mTSlice = mSlicer->GetTSlice();
123     mSquaresActorList[mTSlice]->VisibilityOn();
124   }
125   
126   vtkMarchingSquares * mSquares = mSquaresList[mTSlice];
127   vtkImageClip * mClipper = mClipperList[mTSlice];
128   vtkActor * mSquaresActor = mSquaresActorList[mTSlice];
129
130   // Do it
131   mSquares->SetValue(0,value);
132
133   int* extent = mSlicer->GetImageActor()->GetDisplayExtent();
134   mClipper->SetOutputWholeExtent(extent[0],extent[1],extent[2],
135                                  extent[3],extent[4],extent[5]);
136   int i;
137   for (i = 0; i < 6;i = i+2) {
138     if (extent[i] == extent[i+1]) {
139       break;
140     }
141   }
142   
143   switch (i)
144     {
145     case 0:
146       if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0] > mSlice)
147         {
148           mSquaresActor->SetPosition(1,0,0);
149         }
150       else
151         {
152           mSquaresActor->SetPosition(-1,0,0);
153         }
154       break;
155     case 2:
156       if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1] > mSlice)
157         {
158           mSquaresActor->SetPosition(0,1,0);
159         }
160       else
161         {
162           mSquaresActor->SetPosition(0,-1,0);
163         }
164       break;
165     case 4:
166       if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2] > mSlice)
167         {
168           mSquaresActor->SetPosition(0,0,1);
169         }
170       else
171         {
172           mSquaresActor->SetPosition(0,0,-1);
173         }
174       break;
175     }
176   mSquares->Update();
177 }
178 //------------------------------------------------------------------------------
179