]> Creatis software - clitk.git/blob - vv/vvImageContour.cxx
binarize
[clitk.git] / vv / vvImageContour.cxx
1 /*=========================================================================
2
3   Program:   vv
4   Module:    $RCSfile: vvImageContour.cxx,v $
5   Language:  C++
6   Date:      $Date: 2010/02/07 12:00:59 $
7   Version:   $Revision: 1.2 $
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   }
81   //mSquares->Update();
82 }
83 //------------------------------------------------------------------------------
84
85
86 //------------------------------------------------------------------------------
87 void vvImageContour::update(int value) {
88   mSlice = mSlicer->GetSlice();
89
90   // Only change actor visibility if tslice change
91   if (mTSlice != mSlicer->GetTSlice()) {
92     if (mTSlice != -1) 
93       mSquaresActorList[mTSlice]->VisibilityOff();
94     mTSlice = mSlicer->GetTSlice();
95     mSquaresActorList[mTSlice]->VisibilityOn();
96   }
97   
98   vtkMarchingSquares * mSquares = mSquaresList[mTSlice];
99   vtkImageClip * mClipper = mClipperList[mTSlice];
100   vtkActor * mSquaresActor = mSquaresActorList[mTSlice];
101
102   // Do it
103   mSquares->SetValue(0,value);
104
105   int* extent = mSlicer->GetImageActor()->GetDisplayExtent();
106   mClipper->SetOutputWholeExtent(extent[0],extent[1],extent[2],
107                                  extent[3],extent[4],extent[5]);
108   int i;
109   for (i = 0; i < 6;i = i+2) {
110     if (extent[i] == extent[i+1]) {
111       break;
112     }
113   }
114   
115   switch (i)
116     {
117     case 0:
118       if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0] > mSlice)
119         {
120           mSquaresActor->SetPosition(1,0,0);
121           // mSquaresActor2->SetPosition(1,0,0);
122         }
123       else
124         {
125           mSquaresActor->SetPosition(-1,0,0);
126           // mSquaresActor2->SetPosition(-1,0,0);
127         }
128       break;
129     case 2:
130       if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1] > mSlice)
131         {
132           mSquaresActor->SetPosition(0,1,0);
133           //   mSquaresActor2->SetPosition(0,1,0);
134         }
135       else
136         {
137           mSquaresActor->SetPosition(0,-1,0);
138           // mSquaresActor2->SetPosition(0,-1,0);
139         }
140       break;
141     case 4:
142       if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2] > mSlice)
143         {
144           mSquaresActor->SetPosition(0,0,1);
145           // mSquaresActor2->SetPosition(0,0,1);
146         }
147       else
148         {
149           mSquaresActor->SetPosition(0,0,-1);
150           // mSquaresActor2->SetPosition(0,0,-1);
151         }
152       break;
153     }
154   mSquares->Update();
155 }
156 //------------------------------------------------------------------------------
157