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