Browse Source

universal write, tempaltes

Former-commit-id: ae4a31229affca1fa5036fe885b6601421046e00
Alec Jacobson 10 years ago
parent
commit
d13d3163e1

+ 1 - 0
include/igl/read_triangle_mesh.h

@@ -32,6 +32,7 @@ namespace igl
   // Outputs:
   //   V  eigen double matrix #V by 3
   //   F  eigen int matrix #F by 3
+  // Returns true iff success
   template <typename Scalar, typename Index>
   IGL_INLINE bool read_triangle_mesh(
     const std::string str,

+ 4 - 0
include/igl/writeSTL.cpp

@@ -107,3 +107,7 @@ IGL_INLINE bool igl::writeSTL(
 {
   return writeSTL(filename,V,F, Eigen::PlainObjectBase<DerivedV>(), ascii);
 }
+
+#ifdef IGL_STATIC_LIBRARY
+template bool igl::writeSTL<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, bool);
+#endif

+ 42 - 17
include/igl/write_triangle_mesh.cpp

@@ -6,9 +6,13 @@
 // v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // obtain one at http://mozilla.org/MPL/2.0/.
 #include "write_triangle_mesh.h"
-
+#include "pathinfo.h"
+#include "writeMESH.h"
 #include "writeOBJ.h"
 #include "writeOFF.h"
+#include "writePLY.h"
+#include "writeSTL.h"
+#include "writeWRL.h"
 
 #include <iostream>
 
@@ -16,27 +20,48 @@ template <typename DerivedV, typename DerivedF>
 IGL_INLINE bool igl::write_triangle_mesh(
   const std::string str,
   const Eigen::PlainObjectBase<DerivedV>& V,
-  const Eigen::PlainObjectBase<DerivedF>& F)
+  const Eigen::PlainObjectBase<DerivedF>& F,
+  const bool ascii)
 {
   using namespace std;
-  const char* p;
-  for (p = str.c_str(); *p != '\0'; p++)
-    ;
-  while (*p != '.')
-    p--;
-
-  if (!strcmp(p, ".obj") || !strcmp(p, ".OBJ"))
-    return igl::writeOBJ(str,V,F);
-
-  if (!strcmp(p, ".off") || !strcmp(p, ".OFF"))
-    return igl::writeOFF(str,V,F);
-
-  cerr<<"^write Unsupported extension: "<<string(p)<<endl;
-  return false;
+  // dirname, basename, extension and filename
+  string d,b,e,f;
+  pathinfo(str,d,b,e,f);
+  // Convert extension to lower case
+  std::transform(e.begin(), e.end(), e.begin(), ::tolower);
+  if(e == "mesh")
+  {
+    assert(ascii && ".mesh only supports ascii");
+    Eigen::MatrixXi _1;
+    return writeMESH(str,V,_1,F);
+  }else if(e == "obj")
+  {
+    assert(ascii && ".obj only supports ascii");
+    return writeOBJ(str,V,F);
+  }else if(e == "off")
+  {
+    assert(ascii && ".off only supports ascii");
+    return writeOFF(str,V,F);
+  }else if(e == "ply")
+  {
+    return writePLY(str,V,F,ascii);
+  }else if(e == "stl")
+  {
+    return writeSTL(str,V,F,ascii);
+  }else if(e == "wrl")
+  {
+    assert(ascii && ".wrl only supports ascii");
+    return writeWRL(str,V,F);
+  }else
+  {
+    assert("Unsupported file format");
+    cerr<<"Unsupported file format: ."<<e<<endl;
+    return false;
+  }
 }
 
 #ifdef IGL_STATIC_LIBRARY
 // Explicit template specialization
 // generated by autoexplicit.sh
-template bool igl::write_triangle_mesh<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
+template bool igl::write_triangle_mesh<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, const bool);
 #endif

+ 16 - 6
include/igl/write_triangle_mesh.h

@@ -14,14 +14,24 @@
 
 namespace igl
 {
-  // write mesh to an ascii file with automatic detection of file format. supported: obj, off)
-  // Known Bugs:
-  //  Does not correctly find file extensions: myfile.foo.off
+  // write mesh to a file with automatic detection of file format.  supported:
+  // obj, off, stl, wrl, ply, mesh). 
+  // 
+  // Templates:
+  //   Scalar  type for positions and vectors (will be read as double and cast
+  //     to Scalar)
+  //   Index  type for indices (will be read as int and cast to Index)
+  // Inputs:
+  //   str  path to file
+  //   V  eigen double matrix #V by 3
+  //   F  eigen int matrix #F by 3
+  // Returns true iff success
   template <typename DerivedV, typename DerivedF>
   IGL_INLINE bool write_triangle_mesh(
-                        const std::string str,
-                        const Eigen::PlainObjectBase<DerivedV>& V,
-                        const Eigen::PlainObjectBase<DerivedF>& F);
+    const std::string str,
+    const Eigen::PlainObjectBase<DerivedV>& V,
+    const Eigen::PlainObjectBase<DerivedF>& F,
+    const bool ascii = true);
 }
 
 #ifndef IGL_STATIC_LIBRARY