]> Creatis software - clitk.git/blob - vv/vvMesh.cxx
Reformatted using new coding style
[clitk.git] / vv / vvMesh.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://oncora1.lyon.fnclcc.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 <sstream>
19 #include <cassert>
20 #include <vector>
21 #include <vtkSmartPointer.h>
22 #include <vtkFloatArray.h>
23 #include <vtkPointData.h>
24 #include <vtkPolyData.h>
25 #include <vtkPolyDataReader.h>
26 #include <vtkOBJReader.h>
27 #include <vtkImageData.h>
28 #include "clitkCommon.h"
29 #include "vvMesh.h"
30 #include <vtkImageStencil.h>
31 #include <vtkLinearExtrusionFilter.h>
32 #include <vtkPolyDataToImageStencil.h>
33 #include <vtkMarchingCubes.h>
34 #include <itksys/SystemTools.hxx>
35
36 #include <vtkMetaImageWriter.h>
37
38 vvMesh::vvMesh() :
39   r(1),g(0),b(0),
40   slice_spacing(-1)
41 {}
42
43 void vvMesh::AddMesh(vtkPolyData* p)
44 {
45   vtkPolyData * mesh=vtkPolyData::New();
46   mesh->ShallowCopy(p);
47   meshes.push_back(mesh);
48 }
49
50 void vvMesh::ReadFromVTK(const char * filename)
51 {
52   DD("hello!");
53   std::string extension=itksys::SystemTools::GetFilenameLastExtension(std::string(filename));
54   if (extension == ".vtk" || extension== ".VTK") {
55     assert(GetNumberOfMeshes() == 0); ///We assume the object is empty
56     vtkSmartPointer<vtkPolyDataReader> r=vtkSmartPointer<vtkPolyDataReader>::New();
57     r->SetFileName(filename);
58     r->Update();
59     assert(r->GetOutput());
60     AddMesh(r->GetOutput());
61   } else if (extension == ".obj" || extension== ".OBJ") {
62     assert(GetNumberOfMeshes() == 0); ///We assume the object is empty
63     vtkSmartPointer<vtkOBJReader> r=vtkSmartPointer<vtkOBJReader>::New();
64     r->SetFileName(filename);
65     r->Update();
66     assert(r->GetOutput());
67     AddMesh(r->GetOutput());
68   } else
69     assert (false) ; //shouldn't happen
70
71   assert(GetNumberOfMeshes() != 0); ///We assume the object is empty
72   structure_name=filename;
73 }
74
75 void vvMesh::RemoveMeshes()
76 {
77   for (std::vector<vtkPolyData*>::const_iterator i=meshes.begin(); i!=meshes.end(); i++)
78     (*i)->Delete();
79   meshes.erase(meshes.begin(),meshes.end());
80 }
81
82 void vvMesh::AddMask(vtkImageData* im)
83 {
84   assert(im->GetScalarType() == VTK_UNSIGNED_CHAR);
85   vtkImageData* image=vtkImageData::New();
86   image->ShallowCopy(im);
87   masks.push_back(image);
88 }
89
90 void vvMesh::RemoveMasks()
91 {
92   for (std::vector<vtkImageData*>::const_iterator i=masks.begin(); i!=masks.end(); i++)
93     (*i)->Delete();
94   masks.erase(masks.begin(),masks.end());
95 }
96
97 vvMesh::~vvMesh()
98 {
99   RemoveMeshes();
100   RemoveMasks();
101 }
102
103 void vvMesh::CopyInformation(vvMesh::Pointer input)
104 {
105   r=input->r;
106   g=input->g;
107   b=input->b;
108   structure_name=input->structure_name;
109   slice_spacing=input->slice_spacing;
110 }
111
112 void vvMesh::Print() const
113 {
114   std::cout << this << " : " << structure_name << std::endl << "RGB: " << r << "," << g << "," << b << std::endl;
115   for (std::vector<vtkPolyData*>::const_iterator i=meshes.begin(); i!=meshes.end(); i++) {
116     std::cout << (*i)->GetNumberOfPoints() << " points, " << (*i)->GetNumberOfCells() << " cells." << std::endl;
117     DDV((*i)->GetBounds(),6);
118   }
119   std::cout << "-------------------------" << std::endl << std::endl;
120 }
121
122 void vvMesh::ComputeMasks(vtkImageData* sample,bool extrude)
123 {
124   this->RemoveMasks();
125   for (std::vector<vtkPolyData*>::iterator i=meshes.begin(); i!=meshes.end(); i++) {
126     vtkPolyData* mesh=*i;
127     double *bounds=mesh->GetBounds();
128
129     vtkSmartPointer<vtkImageData> binary_image=vtkSmartPointer<vtkImageData>::New();
130     binary_image->SetScalarTypeToUnsignedChar();
131     ///Use the smallest mask in which the mesh fits
132     // Add two voxels on each side to make sure the mesh fits
133     double * samp_origin=sample->GetOrigin();
134     double * spacing=sample->GetSpacing();
135     binary_image->SetSpacing(spacing);
136     /// Put the origin on a voxel to avoid small skips
137     binary_image->SetOrigin(floor((bounds[0]-samp_origin[0])/spacing[0]-2)*spacing[0]+samp_origin[0],
138                             floor((bounds[2]-samp_origin[1])/spacing[1]-2)*spacing[1]+samp_origin[1],
139                             floor((bounds[4]-samp_origin[2])/spacing[2]-2)*spacing[2]+samp_origin[2]);
140     double * origin=binary_image->GetOrigin();
141     binary_image->SetExtent(0,ceil((bounds[1]-origin[0])/spacing[0]+4),
142                             0,ceil((bounds[3]-origin[1])/spacing[1]+4),
143                             0,ceil((bounds[5]-origin[2])/spacing[2])+4);
144     binary_image->AllocateScalars();
145     memset(binary_image->GetScalarPointer(),0,binary_image->GetDimensions()[0]*binary_image->GetDimensions()[1]*binary_image->GetDimensions()[2]*sizeof(unsigned char));
146
147
148     vtkSmartPointer<vtkPolyDataToImageStencil> sts=vtkSmartPointer<vtkPolyDataToImageStencil>::New();
149     //The following line is extremely important
150     //http://www.nabble.com/Bug-in-vtkPolyDataToImageStencil--td23368312.html#a23370933
151     sts->SetTolerance(0);
152     sts->SetInformationInput(binary_image);
153
154     if (extrude) {
155       vtkSmartPointer<vtkLinearExtrusionFilter> extrude=vtkSmartPointer<vtkLinearExtrusionFilter>::New();
156       extrude->SetInput(mesh);
157       ///We extrude in the -slice_spacing direction to respect the FOCAL convention
158       extrude->SetVector(0, 0, -slice_spacing);
159       sts->SetInput(extrude->GetOutput());
160     } else
161       sts->SetInput(mesh);
162
163     vtkSmartPointer<vtkImageStencil> stencil=vtkSmartPointer<vtkImageStencil>::New();
164     stencil->SetStencil(sts->GetOutput());
165     stencil->SetInput(binary_image);
166     stencil->Update();
167     this->AddMask(stencil->GetOutput());
168     //vtkSmartPointer<vtkMetaImageWriter> w = vtkSmartPointer<vtkMetaImageWriter>::New();
169     //w->SetInput(stencil->GetOutput());
170     //w->SetFileName("binary.mhd");
171     //w->Write();
172   }
173 }
174
175 void vvMesh::ComputeMeshes()
176 {
177   this->RemoveMeshes();
178   for (std::vector<vtkImageData*>::iterator i=masks.begin(); i!=masks.end(); i++) {
179     vtkSmartPointer<vtkMarchingCubes> marching = vtkSmartPointer<vtkMarchingCubes>::New();
180     marching->SetInput(*i);
181     marching->SetValue(0,0.5);
182     marching->Update();
183     this->AddMesh(marching->GetOutput());
184   }
185 }
186
187 void vvMesh::propagateContour(vvImage::Pointer vf)
188 {
189   assert(this->GetNumberOfMeshes() == 1);
190   std::vector<vtkImageData*> sgrids=vf->GetVTKImages();
191   vtkSmartPointer<vtkPolyData> reference_mesh = vtkSmartPointer<vtkPolyData>::New();
192   reference_mesh->ShallowCopy(this->GetMesh(0));
193   this->RemoveMeshes();
194
195   for (std::vector<vtkImageData*>::iterator i=sgrids.begin();
196        i!=sgrids.end(); i++) {
197     vtkPolyData* new_mesh=vtkPolyData::New();
198     new_mesh->DeepCopy(reference_mesh);
199     double Ox=vf->GetOrigin()[0];
200     double Oy=vf->GetOrigin()[1];
201     double Oz=vf->GetOrigin()[2];
202     double Sx=vf->GetSpacing()[0];
203     double Sy=vf->GetSpacing()[1];
204     double Sz=vf->GetSpacing()[2];
205     int *dims=vf->GetVTKImages()[0]->GetDimensions();
206     assert((*i)->GetScalarType() == VTK_FLOAT); //vfs are assumed to be of float type
207     assert((*i)->GetNumberOfScalarComponents() == 3);
208     float * vector_data=reinterpret_cast<float*>((*i)->GetScalarPointer());
209     for (int j=0; j<new_mesh->GetNumberOfPoints(); j++) {
210       double* old=new_mesh->GetPoint(j);
211       int ix=(old[0]-Ox)/Sx;
212       int iy=(old[1]-Oy)/Sy;
213       int iz=(old[2]-Oz)/Sz;
214       float* vector=vector_data+(ix+iy*vf->GetSize()[0]+iz*vf->GetSize()[0]*vf->GetSize()[1])*3;
215       if (ix>=0 && ix < dims[0]
216           && iy>=0 && iy < dims[1]
217           && iz>=0 && iz < dims[2])
218         new_mesh->GetPoints()->SetPoint(j,old[0]+vector[0],old[1]+vector[1],old[2]+vector[2]);
219     }
220     this->AddMesh(new_mesh);
221   }
222   if (GetNumberOfMasks()) { //If the input mesh has a mask, use it to compute the warped meshes' masks
223     vtkSmartPointer<vtkImageData> ref_mask = vtkSmartPointer<vtkImageData>::New();
224     ref_mask->ShallowCopy(GetMask(0));
225     this->ComputeMasks(ref_mask);
226   }
227 }