]> Creatis software - clitk.git/blob - vv/vvMeshActor.cxx
Initial revision
[clitk.git] / vv / vvMeshActor.cxx
1 /*=========================================================================
2
3  Program:   vv
4  Language:  C++
5  Author :   Joel Schaerer (joel.schaerer@insa-lyon.fr)
6
7 Copyright (C) 2008
8 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
9 CREATIS-LRMN http://www.creatis.insa-lyon.fr
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, version 3 of the License.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 =========================================================================*/
24
25 #include "vvMeshActor.h"
26 #include "clitkCommon.h"
27 #include <vtkPolyDataMapper.h>
28 #include <vtkMarchingSquares.h>
29 #include <vtkImageData.h>
30 #include <vtkPlane.h>
31 #include <vtkProperty.h>
32 #include <vtkActor.h>
33 #include <cassert>
34
35 vvMeshActor::vvMeshActor() :
36     mCutDimension(NULL), mMesh(NULL),
37     mMarching(NULL), mMapper(NULL),
38     mActor(NULL),
39     mSuperpostionMode(false), mTimeSlice(0)
40 {}
41
42 vvMeshActor::~vvMeshActor()
43 {
44     mMarching->Delete();
45     mMapper->Delete();
46     mActor->Delete();
47 }
48
49 void vvMeshActor::Init(vvMesh::Pointer mesh,int time_slice,vvImage::Pointer vf)
50 {
51     mMesh=mesh;
52
53     mMarching=vtkMarchingSquares::New();
54     mTimeSlice=time_slice;
55     if (static_cast<unsigned int>(time_slice)<mMesh->GetNumberOfMeshes())
56         mMarching->SetInput(mMesh->GetMask(time_slice));
57     else
58         mMarching->SetInput(mMesh->GetMask(0));
59     mMarching->SetValue(0,0.5);
60     //mMarching->Update();
61
62     mMapper=vtkPolyDataMapper::New();
63     mMapper->SetInput(mMarching->GetOutput());
64     //The following line allows to display the contour over the image 
65     //(http://www.nabble.com/What-happens-when-two-actors-are-at-the-same-depth--td23175458.html)
66     vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
67     mActor=vtkActor::New();
68     mActor->SetMapper(mMapper);
69     mActor->SetPickable(false);
70     mActor->GetProperty()->EdgeVisibilityOn();
71     mActor->GetProperty()->SetEdgeColor(mMesh->r,mMesh->g,mMesh->b);
72     mActor->GetProperty()->SetLineWidth(2.);
73 }
74
75 void vvMeshActor::SetCutSlice(double slice)
76 {
77     mCutSlice=slice;
78     vtkImageData* mask;
79     if (static_cast<unsigned int>(mTimeSlice)<mMesh->GetNumberOfMasks())
80         mask=mMesh->GetMask(mTimeSlice);
81     else
82         mask=mMesh->GetMask(0);
83     int* dims=mask->GetDimensions(); 
84     int mask_slice=(slice-mask->GetOrigin()[mCutDimension])/mask->GetSpacing()[mCutDimension];
85     switch (mCutDimension)
86     {
87         case 0:
88             mMarching->SetImageRange(mask_slice,mask_slice,0,dims[1],0,dims[2]);
89             break;
90         case 1:
91             mMarching->SetImageRange(0,dims[0],mask_slice,mask_slice,0,dims[2]);
92             break;
93         case 2:
94             mMarching->SetImageRange(0,dims[0],0,dims[1],mask_slice,mask_slice);
95             break;
96         default:
97             assert(false);
98     }
99     mMarching->Update();
100 }
101
102 void vvMeshActor::SetTimeSlice(int time)
103 {
104     mTimeSlice=time;
105     if (static_cast<unsigned int>(time)<mMesh->GetNumberOfMasks())
106         mMarching->SetInput(mMesh->GetMask(time));
107     else
108         mMarching->SetInput(mMesh->GetMask(0));
109     SetCutSlice(mCutSlice); //We need to find the new mask cut slice,
110     //since masks do not all have the same origin
111 }
112
113 void vvMeshActor::SetSlicingOrientation(unsigned int d)
114 {
115     mCutDimension=d;
116 }
117
118 void vvMeshActor::ToggleSuperposition()
119 {
120     DD("Warning: superposition not implemented");
121   // std::cout << "vvMeshActor::ToggleSuperposition size = " << mMeshes.size() << std::endl;
122     if (not mSuperpostionMode and mMesh->GetNumberOfMeshes() > 1)
123     {
124         mSuperpostionMode=true;
125     }
126     else
127     {
128         mSuperpostionMode=false;
129     }
130 }