1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
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"
21 #include "vtkMetaImageReader.h"
22 #include "vtkContourFilter.h"
23 #include "vtkDecimatePro.h"
24 #include "vtkPolyDataMapper.h"
25 #include "vtkRenderer.h"
26 #include "vtkRenderWindow.h"
28 #include "vtkCamera.h"
29 #include "vtkOBJExporter.h"
30 #include "vtkRenderWindowInteractor.h"
31 #include "vtkSmartPointer.h"
33 #include "itksys/SystemTools.hxx"
35 #include "vtkPolyDataWriter.h"
37 void run(const args_info_clitkBinaryImageToMesh& argsInfo);
39 int main(int argc, char** argv)
41 GGO(clitkBinaryImageToMesh, args_info);
48 void run(const args_info_clitkBinaryImageToMesh& argsInfo)
50 std::string file = argsInfo.input_arg;
52 vtkSmartPointer<vtkMetaImageReader> pbmp_reader = vtkMetaImageReader::New();
53 pbmp_reader->SetFileName(file.c_str());
54 pbmp_reader->Update();
56 printf("Filtering...\n");
57 vtkSmartPointer<vtkContourFilter> pcontour = vtkContourFilter::New();
58 pcontour->SetValue(0, 0.5);
59 pcontour->SetInputConnection(pbmp_reader->GetOutputPort());
61 vtkSmartPointer<vtkDecimatePro> psurface = vtkDecimatePro::New();
62 psurface->SetInputConnection(pcontour->GetOutputPort());
64 vtkSmartPointer<vtkPolyDataMapper> skinMapper = vtkPolyDataMapper::New();
65 skinMapper->SetInputConnection(psurface->GetOutputPort());
66 skinMapper->ScalarVisibilityOff();
68 vtkSmartPointer<vtkActor> skin = vtkActor::New();
69 skin->SetMapper(skinMapper);
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();
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 ();
85 vtkSmartPointer<vtkRenderWindow> renWin = vtkRenderWindow::New();
86 renWin->AddRenderer(aRenderer);
87 renWin->SetSize(640, 480);
90 if (argsInfo.output_given) { output = argsInfo.output_arg; }
92 bool writeVTK = false;
93 if (output.length()>4) {
94 if (output.compare(output.length()-4, 4, ".vtk")==0) {
99 vtkSmartPointer<vtkPolyDataWriter> wr = vtkSmartPointer<vtkPolyDataWriter>::New();
100 wr->SetInputConnection(psurface->GetOutputPort());
101 wr->SetFileName(output.c_str());
106 vtkSmartPointer<vtkOBJExporter> pwriter2 = vtkOBJExporter::New();
107 pwriter2->SetRenderWindow(renWin);
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());
121 file = itksys::SystemTools::GetFilenameWithoutExtension(file);
123 pwriter2->SetFilePrefix(file.c_str());
128 vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkRenderWindowInteractor::New();
129 iren->SetRenderWindow(renWin);
131 skinMapper->Update();
132 bool interact = argsInfo.view_flag;