]> Creatis software - clitk.git/blob - vv/vvMeshActor.cxx
Merge branch 'master' of /home/dsarrut/clitk3.server
[clitk.git] / vv / vvMeshActor.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #include "vvMeshActor.h"
19 #include "clitkCommon.h"
20 #include <vtkPolyDataMapper.h>
21 #include <vtkMarchingSquares.h>
22 #include <vtkImageData.h>
23 #include <vtkPlane.h>
24 #include <vtkProperty.h>
25 #include <vtkActor.h>
26 #include <cassert>
27
28 vvMeshActor::vvMeshActor() :
29   mCutDimension(0), mMesh(NULL),
30   mMarching(NULL), mMapper(NULL),
31   mActor(NULL),
32   mSuperpostionMode(false), mTimeSlice(0)
33 {}
34
35 vvMeshActor::~vvMeshActor()
36 {
37   mMarching->Delete();
38   mMapper->Delete();
39   mActor->Delete();
40 }
41
42 void vvMeshActor::Init(vvMesh::Pointer mesh,int time_slice,vvImage::Pointer vf)
43 {
44   mMesh=mesh;
45
46   mMarching=vtkMarchingSquares::New();
47   mTimeSlice=time_slice;
48   if (static_cast<unsigned int>(time_slice)<mMesh->GetNumberOfMeshes())
49     mMarching->SetInput(mMesh->GetMask(time_slice));
50   else
51     mMarching->SetInput(mMesh->GetMask(0));
52   mMarching->SetValue(0,0.5);
53   //mMarching->Update();
54
55   mMapper=vtkPolyDataMapper::New();
56   mMapper->SetInput(mMarching->GetOutput());
57   //The following line allows to display the contour over the image
58   //(http://www.nabble.com/What-happens-when-two-actors-are-at-the-same-depth--td23175458.html)
59   vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
60   mActor=vtkActor::New();
61   mActor->SetMapper(mMapper);
62   mActor->SetPickable(false);
63   mActor->GetProperty()->EdgeVisibilityOn();
64   mActor->GetProperty()->SetEdgeColor(mMesh->r,mMesh->g,mMesh->b);
65   mActor->GetProperty()->SetLineWidth(2.);
66 }
67
68 void vvMeshActor::SetCutSlice(double slice)
69 {
70   DD("SetCutSlice");
71   DD(slice);
72   mCutSlice=slice;
73   vtkImageData* mask;
74   if (static_cast<unsigned int>(mTimeSlice)<mMesh->GetNumberOfMasks())
75     mask=mMesh->GetMask(mTimeSlice);
76   else
77     mask=mMesh->GetMask(0);
78   int* dims=mask->GetDimensions();
79   int mask_slice=(slice-mask->GetOrigin()[mCutDimension])/mask->GetSpacing()[mCutDimension];
80   switch (mCutDimension) {
81   case 0:
82     mMarching->SetImageRange(mask_slice,mask_slice,0,dims[1],0,dims[2]);
83     break;
84   case 1:
85     mMarching->SetImageRange(0,dims[0],mask_slice,mask_slice,0,dims[2]);
86     break;
87   case 2:
88     mMarching->SetImageRange(0,dims[0],0,dims[1],mask_slice,mask_slice);
89     break;
90   default:
91     assert(false);
92   }
93   mMarching->Update();
94 }
95
96 void vvMeshActor::SetTimeSlice(int time)
97 {
98   mTimeSlice=time;
99   if (static_cast<unsigned int>(time)<mMesh->GetNumberOfMasks())
100     mMarching->SetInput(mMesh->GetMask(time));
101   else
102     mMarching->SetInput(mMesh->GetMask(0));
103   SetCutSlice(mCutSlice); //We need to find the new mask cut slice,
104   //since masks do not all have the same origin
105 }
106
107 void vvMeshActor::SetSlicingOrientation(unsigned int d)
108 {
109   mCutDimension=d;
110 }
111
112 void vvMeshActor::ToggleSuperposition()
113 {
114   DD("Warning: superposition not implemented");
115   // std::cout << "vvMeshActor::ToggleSuperposition size = " << mMeshes.size() << std::endl;
116   if (! mSuperpostionMode && mMesh->GetNumberOfMeshes() > 1) {
117     mSuperpostionMode=true;
118   } else {
119     mSuperpostionMode=false;
120   }
121 }