]> Creatis software - clitk.git/blob - tools/clitkBinaryImageToMesh.cxx
GateAsciiImageIO is now cross-platform using itksys::RegularExpression
[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 "clitkImageToImageGenericFilter.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 void run(const args_info_clitkBinaryImageToMesh& argsInfo);
36
37 int main(int argc, char** argv)
38 {
39   GGO(clitkBinaryImageToMesh, args_info);
40
41   run(args_info);
42   
43   return EXIT_SUCCESS;
44 }
45
46 void run(const args_info_clitkBinaryImageToMesh& argsInfo)
47 {
48     std::string file = argsInfo.input_arg;
49    
50     vtkSmartPointer<vtkMetaImageReader> pbmp_reader = vtkMetaImageReader::New();
51     pbmp_reader->SetFileName(file.c_str());
52     pbmp_reader->Update();
53
54     printf("Filtering...\n");
55     vtkSmartPointer<vtkContourFilter> pcontour = vtkContourFilter::New();
56     pcontour->SetValue(0, 0.5);
57     pcontour->SetInputConnection(pbmp_reader->GetOutputPort());
58     
59     vtkSmartPointer<vtkDecimatePro> psurface = vtkDecimatePro::New();
60     psurface->SetInputConnection(pcontour->GetOutputPort());
61
62     vtkSmartPointer<vtkPolyDataMapper> skinMapper = vtkPolyDataMapper::New();
63       skinMapper->SetInputConnection(psurface->GetOutputPort());
64       skinMapper->ScalarVisibilityOff();
65     
66     vtkSmartPointer<vtkActor> skin = vtkActor::New();
67       skin->SetMapper(skinMapper);
68     
69     vtkSmartPointer<vtkCamera> aCamera = vtkCamera::New();
70       aCamera->SetViewUp (0, 0, -1);
71       aCamera->SetPosition (0, 1, 0);
72       aCamera->SetFocalPoint (0, 0, 0);
73       aCamera->ComputeViewPlaneNormal();
74     aCamera->Dolly(1.5);
75
76     vtkSmartPointer<vtkRenderer> aRenderer = vtkRenderer::New();
77     aRenderer->AddActor(skin);
78     aRenderer->SetActiveCamera(aCamera);
79     aRenderer->ResetCamera ();
80     aRenderer->SetBackground(0,0,0);
81     aRenderer->ResetCameraClippingRange ();
82
83     vtkSmartPointer<vtkRenderWindow> renWin = vtkRenderWindow::New();
84       renWin->AddRenderer(aRenderer);
85     renWin->SetSize(640, 480);
86     
87     vtkSmartPointer<vtkOBJExporter> pwriter2 = vtkOBJExporter::New();
88     pwriter2->SetRenderWindow(renWin);
89
90     std::string output;
91     if (argsInfo.output_given) {
92       output = argsInfo.output_arg;
93       if (itksys::SystemTools::FileIsDirectory(output.c_str())) {
94         file = itksys::SystemTools::GetFilenameName(file.c_str());
95         file = itksys::SystemTools::GetFilenameWithoutExtension(file.c_str());
96         file = itksys::SystemTools::CollapseFullPath(file.c_str(), output.c_str());
97       }
98       else {
99         file = output;
100       }
101     }
102     else { 
103       file = itksys::SystemTools::GetFilenameWithoutExtension(file);
104     }
105     pwriter2->SetFilePrefix(file.c_str());
106     pwriter2->Write();
107
108     vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkRenderWindowInteractor::New();
109       iren->SetRenderWindow(renWin);
110
111     skinMapper->Update();
112     bool interact = argsInfo.view_flag;
113     if (interact)
114     {
115       iren->Initialize();
116       iren->Start(); 
117     }
118     else
119       renWin->Render();
120 }
121
122