]> Creatis software - clitk.git/blobdiff - tools/clitkBinaryImageToMesh.cxx
Debug RTStruct conversion with empty struc
[clitk.git] / tools / clitkBinaryImageToMesh.cxx
index 463fa454eefc85a499961d443ca65f8a783af5ac..0eb5db618af80b6ee894010c86112ea763f500ea 100644 (file)
@@ -16,7 +16,7 @@
   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
 ===========================================================================*/
 #include "clitkBinaryImageToMesh_ggo.h"
-#include "clitkImageToImageGenericFilter.h"
+#include "clitkCommon.h"
 
 #include "vtkMetaImageReader.h"
 #include "vtkContourFilter.h"
@@ -32,6 +32,9 @@
 
 #include "itksys/SystemTools.hxx"
 
+#include "vtkPolyDataWriter.h"
+#include "vtkSmoothPolyDataFilter.h"
+
 void run(const args_info_clitkBinaryImageToMesh& argsInfo);
 
 int main(int argc, char** argv)
@@ -55,22 +58,30 @@ void run(const args_info_clitkBinaryImageToMesh& argsInfo)
     vtkSmartPointer<vtkContourFilter> pcontour = vtkContourFilter::New();
     pcontour->SetValue(0, 0.5);
     pcontour->SetInputConnection(pbmp_reader->GetOutputPort());
-    
-    vtkSmartPointer<vtkDecimatePro> psurface = vtkDecimatePro::New();
-    psurface->SetInputConnection(pcontour->GetOutputPort());
 
+    vtkAlgorithmOutput *data = pcontour->GetOutputPort();
+
+    if ( (argsInfo.decimate_arg>=0) && (argsInfo.decimate_arg<=1) ) {
+      vtkSmartPointer<vtkDecimatePro> psurface = vtkDecimatePro::New();
+      psurface->SetInputConnection(pcontour->GetOutputPort());
+      psurface->SetTargetReduction(argsInfo.decimate_arg);
+
+      data = psurface->GetOutputPort();
+    }
+
+       
     vtkSmartPointer<vtkPolyDataMapper> skinMapper = vtkPolyDataMapper::New();
-      skinMapper->SetInputConnection(psurface->GetOutputPort());
-      skinMapper->ScalarVisibilityOff();
-    
+    skinMapper->SetInputConnection(data); //psurface->GetOutputPort()
+    skinMapper->ScalarVisibilityOff();
+      
     vtkSmartPointer<vtkActor> skin = vtkActor::New();
-      skin->SetMapper(skinMapper);
-    
+    skin->SetMapper(skinMapper);
+      
     vtkSmartPointer<vtkCamera> aCamera = vtkCamera::New();
-      aCamera->SetViewUp (0, 0, -1);
-      aCamera->SetPosition (0, 1, 0);
-      aCamera->SetFocalPoint (0, 0, 0);
-      aCamera->ComputeViewPlaneNormal();
+    aCamera->SetViewUp (0, 0, -1);
+    aCamera->SetPosition (0, 1, 0);
+    aCamera->SetFocalPoint (0, 0, 0);
+    aCamera->ComputeViewPlaneNormal();
     aCamera->Dolly(1.5);
 
     vtkSmartPointer<vtkRenderer> aRenderer = vtkRenderer::New();
@@ -81,32 +92,50 @@ void run(const args_info_clitkBinaryImageToMesh& argsInfo)
     aRenderer->ResetCameraClippingRange ();
 
     vtkSmartPointer<vtkRenderWindow> renWin = vtkRenderWindow::New();
-      renWin->AddRenderer(aRenderer);
+    renWin->AddRenderer(aRenderer);
     renWin->SetSize(640, 480);
-    
-    vtkSmartPointer<vtkOBJExporter> pwriter2 = vtkOBJExporter::New();
-    pwriter2->SetRenderWindow(renWin);
-
     std::string output;
-    if (argsInfo.output_given) {
-      output = argsInfo.output_arg;
-      if (itksys::SystemTools::FileIsDirectory(output.c_str())) {
-        file = itksys::SystemTools::GetFilenameName(file.c_str());
-        file = itksys::SystemTools::GetFilenameWithoutExtension(file.c_str());
-        file = itksys::SystemTools::CollapseFullPath(file.c_str(), output.c_str());
-      }
-      else {
-        file = output;
+    if (argsInfo.output_given) { output = argsInfo.output_arg; }
+
+    bool writeVTK = false;
+    if (output.length()>4) {
+      if (output.compare(output.length()-4, 4, ".vtk")==0) {
+        writeVTK=true;
       }
     }
-    else { 
-      file = itksys::SystemTools::GetFilenameWithoutExtension(file);
+    if (writeVTK) {
+      vtkSmartPointer<vtkPolyDataWriter> wr = vtkSmartPointer<vtkPolyDataWriter>::New();
+      wr->SetInputConnection(data); //psurface->GetOutputPort()
+      wr->SetFileName(output.c_str());
+      wr->Update();
+      wr->Write();
+    }
+    else {
+      vtkSmartPointer<vtkOBJExporter> pwriter2 = vtkOBJExporter::New();
+      pwriter2->SetRenderWindow(renWin);
+  
+      if (argsInfo.output_given) {
+        output = argsInfo.output_arg;
+        if (itksys::SystemTools::FileIsDirectory(output.c_str())) {
+          file = itksys::SystemTools::GetFilenameName(file.c_str());
+          file = itksys::SystemTools::GetFilenameWithoutExtension(file.c_str());
+          file = itksys::SystemTools::CollapseFullPath(file.c_str(), output.c_str());
+        }
+        else {
+          file = output;
+        }
+      }
+      else { 
+        file = itksys::SystemTools::GetFilenameWithoutExtension(file);
+      }
+      pwriter2->SetFilePrefix(file.c_str());
+      pwriter2->Write();
     }
-    pwriter2->SetFilePrefix(file.c_str());
-    pwriter2->Write();
+
 
     vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkRenderWindowInteractor::New();
-      iren->SetRenderWindow(renWin);
+    iren->SetRenderWindow(renWin);
 
     skinMapper->Update();
     bool interact = argsInfo.view_flag;