]> Creatis software - clitk.git/blob - vv/vvImageContour.cxx
- toolbase
[clitk.git] / vv / vvImageContour.cxx
1 /*=========================================================================
2
3   Program:   vv
4   Module:    $RCSfile: vvImageContour.cxx,v $
5   Language:  C++
6   Date:      $Date: 2010/02/24 11:42:42 $
7   Version:   $Revision: 1.3 $
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 <vtkMarchingSquares.h>
32 #include <vtkImageClip.h>
33 #include <vtkImageData.h>
34
35 //------------------------------------------------------------------------------
36 vvImageContour::vvImageContour() {
37   mTSlice = -1;
38   mSlice = 0;
39 }
40 //------------------------------------------------------------------------------
41
42
43 //------------------------------------------------------------------------------
44 vvImageContour::~vvImageContour() {
45   for (unsigned int i = 0; i < mSlicer->GetImage()->GetVTKImages().size(); i++) {
46     mSlicer->GetRenderer()->RemoveActor(mSquaresActorList[i]);
47   }
48   mSquaresActorList.clear();
49   mSquaresList.clear();
50   mClipperList.clear();
51 }
52 //------------------------------------------------------------------------------
53
54
55 //------------------------------------------------------------------------------
56 void vvImageContour::setSlicer(vvSlicer * slicer) {
57   mSlicer = slicer;  
58
59   for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
60     // DD(numImage);
61     vtkImageClip * mClipper = vtkImageClip::New();
62     vtkMarchingSquares * mSquares = vtkMarchingSquares::New();
63     vtkPolyDataMapper * mSquaresMapper = vtkPolyDataMapper::New();
64     vtkActor * mSquaresActor = vtkActor::New();
65
66     mClipper->SetInput(mSlicer->GetImage()->GetVTKImages()[numImage]);
67     mSquares->SetInput(mClipper->GetOutput());
68     mSquaresMapper->SetInput(mSquares->GetOutput());
69     mSquaresMapper->ScalarVisibilityOff();
70     mSquaresActor->SetMapper(mSquaresMapper);
71     mSquaresActor->GetProperty()->SetColor(1.0,0,0);
72     mSquaresActor->SetPickable(0);
73     mSquaresActor->VisibilityOff();
74     mSlicer->GetRenderer()->AddActor(mSquaresActor);
75     
76     mSquaresActorList.push_back(mSquaresActor);
77     mSquaresList.push_back(mSquares);
78     mClipperList.push_back(mClipper);
79   }
80   //mSquares->Update();
81 }
82 //------------------------------------------------------------------------------
83
84
85 //------------------------------------------------------------------------------
86 void vvImageContour::removeActors() {
87  //  DD("removeActors");
88   if (!mSlicer) return;
89   mSlice = mSlicer->GetSlice();
90  //  DD(mSlice);
91   for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
92     mSquaresActorList[i]->VisibilityOff();
93     //mSquaresActorList[i]->Update();
94   }
95 }
96 //------------------------------------------------------------------------------
97
98   
99 //------------------------------------------------------------------------------
100 void vvImageContour::update(int value) {
101   //  DD(value);
102   if (!mSlicer) return;
103   mSlice = mSlicer->GetSlice();
104
105   // Only change actor visibility if tslice change
106   if (mTSlice != mSlicer->GetTSlice()) {
107     if (mTSlice != -1) 
108       mSquaresActorList[mTSlice]->VisibilityOff();
109     mTSlice = mSlicer->GetTSlice();
110     mSquaresActorList[mTSlice]->VisibilityOn();
111   }
112   
113   vtkMarchingSquares * mSquares = mSquaresList[mTSlice];
114   vtkImageClip * mClipper = mClipperList[mTSlice];
115   vtkActor * mSquaresActor = mSquaresActorList[mTSlice];
116
117   // Do it
118   mSquares->SetValue(0,value);
119
120   int* extent = mSlicer->GetImageActor()->GetDisplayExtent();
121   mClipper->SetOutputWholeExtent(extent[0],extent[1],extent[2],
122                                  extent[3],extent[4],extent[5]);
123   int i;
124   for (i = 0; i < 6;i = i+2) {
125     if (extent[i] == extent[i+1]) {
126       break;
127     }
128   }
129   
130   switch (i)
131     {
132     case 0:
133       if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0] > mSlice)
134         {
135           mSquaresActor->SetPosition(1,0,0);
136           // mSquaresActor2->SetPosition(1,0,0);
137         }
138       else
139         {
140           mSquaresActor->SetPosition(-1,0,0);
141           // mSquaresActor2->SetPosition(-1,0,0);
142         }
143       break;
144     case 2:
145       if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1] > mSlice)
146         {
147           mSquaresActor->SetPosition(0,1,0);
148           //   mSquaresActor2->SetPosition(0,1,0);
149         }
150       else
151         {
152           mSquaresActor->SetPosition(0,-1,0);
153           // mSquaresActor2->SetPosition(0,-1,0);
154         }
155       break;
156     case 4:
157       if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2] > mSlice)
158         {
159           mSquaresActor->SetPosition(0,0,1);
160           // mSquaresActor2->SetPosition(0,0,1);
161         }
162       else
163         {
164           mSquaresActor->SetPosition(0,0,-1);
165           // mSquaresActor2->SetPosition(0,0,-1);
166         }
167       break;
168     }
169   mSquares->Update();
170 }
171 //------------------------------------------------------------------------------
172