]> Creatis software - clitk.git/blob - tools/clitkBinaryImageToMesh.cxx
Merge branch 'master' of git.creatis.insa-lyon.fr:clitk
[clitk.git] / tools / clitkBinaryImageToMesh.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 "clitkBinaryImageToMesh_ggo.h"
19 #include "clitkCommon.h"
20
21 #include "vtkMetaImageReader.h"
22 #include "vtkContourFilter.h"
23 #include "vtkDecimatePro.h"
24 #include "vtkPolyDataMapper.h"
25 #include "vtkRenderer.h"
26 #include "vtkRenderWindow.h"
27 #include "vtkActor.h"
28 #include "vtkCamera.h"
29 #include "vtkOBJExporter.h"
30 #include "vtkRenderWindowInteractor.h"
31 #include "vtkSmartPointer.h"
32
33 #include "itksys/SystemTools.hxx"
34
35 #include "vtkPolyDataWriter.h"
36
37 void run(const args_info_clitkBinaryImageToMesh& argsInfo);
38
39 int main(int argc, char** argv)
40 {
41   GGO(clitkBinaryImageToMesh, args_info);
42
43   run(args_info);
44   
45   return EXIT_SUCCESS;
46 }
47
48 void run(const args_info_clitkBinaryImageToMesh& argsInfo)
49 {
50     std::string file = argsInfo.input_arg;
51    
52     vtkSmartPointer<vtkMetaImageReader> pbmp_reader = vtkMetaImageReader::New();
53     pbmp_reader->SetFileName(file.c_str());
54     pbmp_reader->Update();
55
56     printf("Filtering...\n");
57     vtkSmartPointer<vtkContourFilter> pcontour = vtkContourFilter::New();
58     pcontour->SetValue(0, 0.5);
59     pcontour->SetInputConnection(pbmp_reader->GetOutputPort());
60     
61     vtkSmartPointer<vtkDecimatePro> psurface = vtkDecimatePro::New();
62     psurface->SetInputConnection(pcontour->GetOutputPort());
63
64     vtkSmartPointer<vtkPolyDataMapper> skinMapper = vtkPolyDataMapper::New();
65     skinMapper->SetInputConnection(psurface->GetOutputPort());
66     skinMapper->ScalarVisibilityOff();
67       
68     vtkSmartPointer<vtkActor> skin = vtkActor::New();
69     skin->SetMapper(skinMapper);
70       
71     vtkSmartPointer<vtkCamera> aCamera = vtkCamera::New();
72     aCamera->SetViewUp (0, 0, -1);
73     aCamera->SetPosition (0, 1, 0);
74     aCamera->SetFocalPoint (0, 0, 0);
75     aCamera->ComputeViewPlaneNormal();
76     aCamera->Dolly(1.5);
77
78     vtkSmartPointer<vtkRenderer> aRenderer = vtkRenderer::New();
79     aRenderer->AddActor(skin);
80     aRenderer->SetActiveCamera(aCamera);
81     aRenderer->ResetCamera ();
82     aRenderer->SetBackground(0,0,0);
83     aRenderer->ResetCameraClippingRange ();
84
85     vtkSmartPointer<vtkRenderWindow> renWin = vtkRenderWindow::New();
86     renWin->AddRenderer(aRenderer);
87     renWin->SetSize(640, 480);
88  
89     std::string output;
90     if (argsInfo.output_given) { output = argsInfo.output_arg; }
91
92     bool writeVTK = false;
93     if (output.length()>4) {
94       if (output.compare(output.length()-4, 4, ".vtk")==0) {
95         writeVTK=true;
96       }
97     }
98     if (writeVTK) {
99       vtkSmartPointer<vtkPolyDataWriter> wr = vtkSmartPointer<vtkPolyDataWriter>::New();
100       wr->SetInputConnection(psurface->GetOutputPort());
101       wr->SetFileName(output.c_str());
102       wr->Update();
103       wr->Write();
104     }
105     else {
106       vtkSmartPointer<vtkOBJExporter> pwriter2 = vtkOBJExporter::New();
107       pwriter2->SetRenderWindow(renWin);
108   
109       if (argsInfo.output_given) {
110         output = argsInfo.output_arg;
111         if (itksys::SystemTools::FileIsDirectory(output.c_str())) {
112           file = itksys::SystemTools::GetFilenameName(file.c_str());
113           file = itksys::SystemTools::GetFilenameWithoutExtension(file.c_str());
114           file = itksys::SystemTools::CollapseFullPath(file.c_str(), output.c_str());
115         }
116         else {
117           file = output;
118         }
119       }
120       else { 
121         file = itksys::SystemTools::GetFilenameWithoutExtension(file);
122       }
123       pwriter2->SetFilePrefix(file.c_str());
124       pwriter2->Write();
125     }
126
127
128     vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkRenderWindowInteractor::New();
129     iren->SetRenderWindow(renWin);
130
131     skinMapper->Update();
132     bool interact = argsInfo.view_flag;
133     if (interact)
134     {
135       iren->Initialize();
136       iren->Start(); 
137     }
138     else
139       renWin->Render();
140 }
141
142