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"
36 #include "vtkSmoothPolyDataFilter.h"
38 void run(const args_info_clitkBinaryImageToMesh& argsInfo);
40 int main(int argc, char** argv)
42 GGO(clitkBinaryImageToMesh, args_info);
49 void run(const args_info_clitkBinaryImageToMesh& argsInfo)
51 std::string file = argsInfo.input_arg;
53 vtkSmartPointer<vtkMetaImageReader> pbmp_reader = vtkMetaImageReader::New();
54 pbmp_reader->SetFileName(file.c_str());
55 pbmp_reader->Update();
57 printf("Filtering...\n");
58 vtkSmartPointer<vtkContourFilter> pcontour = vtkContourFilter::New();
59 pcontour->SetValue(0, 0.5);
60 pcontour->SetInputConnection(pbmp_reader->GetOutputPort());
62 vtkAlgorithmOutput *data = pcontour->GetOutputPort();
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);
69 data = psurface->GetOutputPort();
73 vtkSmartPointer<vtkPolyDataMapper> skinMapper = vtkPolyDataMapper::New();
74 skinMapper->SetInputConnection(data); //psurface->GetOutputPort()
75 skinMapper->ScalarVisibilityOff();
77 vtkSmartPointer<vtkActor> skin = vtkActor::New();
78 skin->SetMapper(skinMapper);
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();
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 ();
94 vtkSmartPointer<vtkRenderWindow> renWin = vtkRenderWindow::New();
95 renWin->AddRenderer(aRenderer);
96 renWin->SetSize(640, 480);
99 if (argsInfo.output_given) { output = argsInfo.output_arg; }
101 bool writeVTK = false;
102 if (output.length()>4) {
103 if (output.compare(output.length()-4, 4, ".vtk")==0) {
108 vtkSmartPointer<vtkPolyDataWriter> wr = vtkSmartPointer<vtkPolyDataWriter>::New();
109 wr->SetInputConnection(data); //psurface->GetOutputPort()
110 wr->SetFileName(output.c_str());
115 vtkSmartPointer<vtkOBJExporter> pwriter2 = vtkOBJExporter::New();
116 pwriter2->SetRenderWindow(renWin);
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());
130 file = itksys::SystemTools::GetFilenameWithoutExtension(file);
132 pwriter2->SetFilePrefix(file.c_str());
137 vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkRenderWindowInteractor::New();
138 iren->SetRenderWindow(renWin);
140 skinMapper->Update();
141 bool interact = argsInfo.view_flag;