]> Creatis software - clitk.git/blob - vv/vvImageContour.cxx
- update vv tool Binarize, now work with float pixel type + display lower contour
[clitk.git] / vv / vvImageContour.cxx
1 /*=========================================================================
2
3   Program:   vv
4   Module:    $RCSfile: vvImageContour.cxx,v $
5   Language:  C++
6   Date:      $Date: 2010/03/05 10:32:33 $
7   Version:   $Revision: 1.5 $
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::setColor(double r, double g, double b) {
88   for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
89     mSquaresActorList[i]->GetProperty()->SetColor(r,g,b);
90   }
91 }
92 //------------------------------------------------------------------------------
93
94
95 //------------------------------------------------------------------------------
96 void vvImageContour::hideActors() {
97   if (!mSlicer) return;
98   mSlice = mSlicer->GetSlice();
99   for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
100     mSquaresActorList[i]->VisibilityOff();
101   }
102 }
103 //------------------------------------------------------------------------------
104
105   
106 //------------------------------------------------------------------------------
107 void vvImageContour::showActors() {
108   if (!mSlicer) return;
109   mSlice = mSlicer->GetSlice();
110   mTSlice = mSlicer->GetTSlice();
111   //  for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
112   mSquaresActorList[mTSlice]->VisibilityOn();
113   update(mValue);
114   //}
115 }
116 //------------------------------------------------------------------------------
117
118   
119 //------------------------------------------------------------------------------
120 void vvImageContour::update(double value) {
121   mValue= value;
122   if (!mSlicer) return;
123
124   // how to not update if not visible ?
125
126   mSlice = mSlicer->GetSlice();
127   // Only change actor visibility if tslice change
128   if (mTSlice != mSlicer->GetTSlice()) {
129     if (mTSlice != -1) 
130       mSquaresActorList[mTSlice]->VisibilityOff();
131     mTSlice = mSlicer->GetTSlice();
132     mSquaresActorList[mTSlice]->VisibilityOn();
133   }
134   
135   vtkMarchingSquares * mSquares = mSquaresList[mTSlice];
136   vtkImageClip * mClipper = mClipperList[mTSlice];
137   vtkActor * mSquaresActor = mSquaresActorList[mTSlice];
138
139   // Do it
140   mSquares->SetValue(0,value);
141
142   int* extent = mSlicer->GetImageActor()->GetDisplayExtent();
143   mClipper->SetOutputWholeExtent(extent[0],extent[1],extent[2],
144                                  extent[3],extent[4],extent[5]);
145   int i;
146   for (i = 0; i < 6;i = i+2) {
147     if (extent[i] == extent[i+1]) {
148       break;
149     }
150   }
151   
152   switch (i)
153     {
154     case 0:
155       if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0] > mSlice)
156         {
157           mSquaresActor->SetPosition(1,0,0);
158         }
159       else
160         {
161           mSquaresActor->SetPosition(-1,0,0);
162         }
163       break;
164     case 2:
165       if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1] > mSlice)
166         {
167           mSquaresActor->SetPosition(0,1,0);
168         }
169       else
170         {
171           mSquaresActor->SetPosition(0,-1,0);
172         }
173       break;
174     case 4:
175       if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2] > mSlice)
176         {
177           mSquaresActor->SetPosition(0,0,1);
178         }
179       else
180         {
181           mSquaresActor->SetPosition(0,0,-1);
182         }
183       break;
184     }
185   mSquares->Update();
186 }
187 //------------------------------------------------------------------------------
188