]> Creatis software - clitk.git/blob - vv/vvImageContour.cxx
- toolbase + binarize
[clitk.git] / vv / vvImageContour.cxx
1 /*=========================================================================
2
3   Program:   vv
4   Module:    $RCSfile: vvImageContour.cxx,v $
5   Language:  C++
6   Date:      $Date: 2010/03/01 07:37:25 $
7   Version:   $Revision: 1.4 $
8   Author :   David Sarrut (david.sarrut@creatis.insa-lyon.fr)
9
10   Copyright (C) 2010
11   Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
12   CREATIS                   http://www.creatis.insa-lyon.fr
13
14   This program is free software: you can redistribute it and/or modify
15   it under the terms of the GNU General Public License as published by
16   the Free Software Foundation, version 3 of the License.
17
18   This program is distributed in the hope that it will be useful,
19   but WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21   GNU General Public License for more details.
22
23   You should have received a copy of the GNU General Public License
24   along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
26   =========================================================================*/
27
28 #include "vvImageContour.h"
29 #include <vtkImageActor.h>
30 #include <vtkCamera.h>
31 #include <vtkRenderer.h>
32 #include <vtkMarchingSquares.h>
33 #include <vtkImageClip.h>
34 #include <vtkImageData.h>
35 #include <vtkPolyDataMapper.h>
36 #include <vtkProperty.h>
37
38 //------------------------------------------------------------------------------
39 vvImageContour::vvImageContour() {
40   mTSlice = -1;
41   mSlice = 0;
42 }
43 //------------------------------------------------------------------------------
44
45
46 //------------------------------------------------------------------------------
47 vvImageContour::~vvImageContour() {
48   for (unsigned int i = 0; i < mSlicer->GetImage()->GetVTKImages().size(); i++) {
49     mSlicer->GetRenderer()->RemoveActor(mSquaresActorList[i]);
50   }
51   mSquaresActorList.clear();
52   mSquaresList.clear();
53   mClipperList.clear();
54 }
55 //------------------------------------------------------------------------------
56
57
58 //------------------------------------------------------------------------------
59 void vvImageContour::setSlicer(vvSlicer * slicer) {
60   mSlicer = slicer;  
61
62   for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
63     vtkImageClip * mClipper = vtkImageClip::New();
64     vtkMarchingSquares * mSquares = vtkMarchingSquares::New();
65     vtkPolyDataMapper * mSquaresMapper = vtkPolyDataMapper::New();
66     vtkActor * mSquaresActor = vtkActor::New();
67
68     mClipper->SetInput(mSlicer->GetImage()->GetVTKImages()[numImage]);
69     mSquares->SetInput(mClipper->GetOutput());
70     mSquaresMapper->SetInput(mSquares->GetOutput());
71     mSquaresMapper->ScalarVisibilityOff();
72     mSquaresActor->SetMapper(mSquaresMapper);
73     mSquaresActor->GetProperty()->SetColor(1.0,0,0);
74     mSquaresActor->SetPickable(0);
75     mSquaresActor->VisibilityOff();
76     mSlicer->GetRenderer()->AddActor(mSquaresActor);
77     
78     mSquaresActorList.push_back(mSquaresActor);
79     mSquaresList.push_back(mSquares);
80     mClipperList.push_back(mClipper);
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(int 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