]> Creatis software - clitk.git/blob - vv/vvMeshActor.cxx
Debug RTStruct conversion with empty struc
[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 <vtkVersion.h>
21 #include <vtkPolyDataMapper.h>
22 #include <vtkMarchingSquares.h>
23 #include <vtkImageData.h>
24 #include <vtkPlane.h>
25 #include <vtkProperty.h>
26 #include <vtkActor.h>
27 #include <cassert>
28
29 vvMeshActor::vvMeshActor() :
30   mCutDimension(0), mMesh(ITK_NULLPTR),
31   mMarching(ITK_NULLPTR), mMapper(ITK_NULLPTR),
32   mActor(ITK_NULLPTR),
33   mSuperpostionMode(false), mTimeSlice(0)
34 {}
35
36 vvMeshActor::~vvMeshActor()
37 {
38   mMarching->Delete();
39   mMapper->Delete();
40   mActor->Delete();
41 }
42
43 void vvMeshActor::Init(vvMesh::Pointer mesh,int time_slice,vvImage::Pointer vf)
44 {
45   mMesh=mesh;
46
47   mMarching=vtkMarchingSquares::New();
48   mTimeSlice=time_slice;
49   if (static_cast<unsigned int>(time_slice)<mMesh->GetNumberOfMeshes()) {
50 #if VTK_MAJOR_VERSION <= 5
51     mMarching->SetInput(mMesh->GetMask(time_slice));
52 #else
53     mMarching->SetInputData(mMesh->GetMask(time_slice));
54 #endif
55   } else {
56 #if VTK_MAJOR_VERSION <= 5
57     mMarching->SetInput(mMesh->GetMask(0));
58 #else
59     mMarching->SetInputData(mMesh->GetMask(0));
60 #endif
61   }
62   mMarching->SetValue(0,0.5);
63   //mMarching->Update();
64
65   mMapper=vtkPolyDataMapper::New();
66 #if VTK_MAJOR_VERSION <= 5
67   mMapper->SetInput(mMarching->GetOutput());
68 #else
69   mMapper->SetInputConnection(mMarching->GetOutputPort());
70 #endif
71   //The following line allows to display the contour over the image
72   //(http://www.nabble.com/What-happens-when-two-actors-are-at-the-same-depth--td23175458.html)
73   vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
74   mActor=vtkActor::New();
75   mActor->SetMapper(mMapper);
76   mActor->SetPickable(false);
77   mActor->GetProperty()->EdgeVisibilityOn();
78   mActor->GetProperty()->SetEdgeColor(mMesh->r,mMesh->g,mMesh->b);
79   mActor->GetProperty()->SetLineWidth(2.);
80   mActor->GetProperty()->SetOpacity(0.995); //in order to get VTK to turn on the alpha-blending in OpenGL
81 }
82
83 void vvMeshActor::SetCutSlice(double slice)
84 {
85   //DD("SetCutSlice");
86   //DD(slice);
87   mCutSlice=slice;
88   vtkImageData* mask;
89   if (static_cast<unsigned int>(mTimeSlice)<mMesh->GetNumberOfMasks())
90     mask=mMesh->GetMask(mTimeSlice);
91   else
92     mask=mMesh->GetMask(0);
93   int* dims=mask->GetDimensions();
94   int mask_slice=(slice-mask->GetOrigin()[mCutDimension])/mask->GetSpacing()[mCutDimension];
95   switch (mCutDimension) {
96   case 0:
97     mMarching->SetImageRange(mask_slice,mask_slice,0,dims[1],0,dims[2]);
98     break;
99   case 1:
100     mMarching->SetImageRange(0,dims[0],mask_slice,mask_slice,0,dims[2]);
101     break;
102   case 2:
103     mMarching->SetImageRange(0,dims[0],0,dims[1],mask_slice,mask_slice);
104     break;
105   default:
106     assert(false);
107   }
108   mMarching->Update();
109 }
110
111 void vvMeshActor::SetTimeSlice(int time)
112 {
113   mTimeSlice=time;
114   if (static_cast<unsigned int>(time)<mMesh->GetNumberOfMasks()) {
115 #if VTK_MAJOR_VERSION <= 5
116     mMarching->SetInput(mMesh->GetMask(time));
117 #else
118     mMarching->SetInputData(mMesh->GetMask(time));
119 #endif
120   } else {
121 #if VTK_MAJOR_VERSION <= 5
122     mMarching->SetInput(mMesh->GetMask(0));
123 #else
124     mMarching->SetInputData(mMesh->GetMask(0));
125 #endif
126  }
127   SetCutSlice(mCutSlice); //We need to find the new mask cut slice,
128   //since masks do not all have the same origin
129 }
130
131 void vvMeshActor::SetSlicingOrientation(unsigned int d)
132 {
133   mCutDimension=d;
134 }
135
136 void vvMeshActor::ToggleSuperposition()
137 {
138   DD("Warning: superposition not implemented");
139   // std::cout << "vvMeshActor::ToggleSuperposition size = " << mMeshes.size() << std::endl;
140   if (! mSuperpostionMode && mMesh->GetNumberOfMeshes() > 1) {
141     mSuperpostionMode=true;
142   } else {
143     mSuperpostionMode=false;
144   }
145 }