Browse Source

improved doc and simpler code

Former-commit-id: d13b21e62194216d498616fac46d0eef26c91625
Alec Jacobson 10 years ago
parent
commit
2de83c371d
3 changed files with 45 additions and 74 deletions
  1. 22 39
      include/igl/writeOBJ.cpp
  2. 11 8
      include/igl/writeOBJ.h
  3. 12 27
      include/igl/writeOFF.cpp

+ 22 - 39
include/igl/writeOBJ.cpp

@@ -14,45 +14,6 @@
 #include <cstdio>
 #include <cassert>
 
-template <typename DerivedV, typename DerivedF>
-IGL_INLINE bool igl::writeOBJ(
-  const std::string str,
-  const Eigen::PlainObjectBase<DerivedV>& V,
-  const Eigen::PlainObjectBase<DerivedF>& F)
-{
-  assert(V.cols() == 3 && "V should have 3 columns");
-  std::ofstream s(str.c_str());
-  s.precision(std::numeric_limits<double>::digits10 + 1);
-
-  if(!s.is_open())
-  {
-    fprintf(stderr,"IOError: writeOBJ() could not open %s\n",str.c_str());
-    return false;
-  }
-
-  for(int i=0;i<(int)V.rows();++i)
-  {
-    s << "v " << V(i,0) << " " << V(i,1) << " " << V(i,2) << std::endl;
-  }
-
-  for(int i=0;i<(int)F.rows();++i)
-  {
-    s << "f ";
-    for(int c =0;c<(int)F.cols();++c)
-    {
-      if(c>0)
-      {
-        s<<" ";
-      }
-      s<< F(i,c)+1;
-    }
-    s<<std::endl;
-  }
-
-  s.close();
-  return true;
-}
-
 template <typename DerivedV, typename DerivedF, typename DerivedT>
 IGL_INLINE bool igl::writeOBJ(
   const std::string str,
@@ -128,6 +89,28 @@ IGL_INLINE bool igl::writeOBJ(
   fclose(obj_file);
   return true;
 }
+
+template <typename DerivedV, typename DerivedF>
+IGL_INLINE bool igl::writeOBJ(
+  const std::string str,
+  const Eigen::PlainObjectBase<DerivedV>& V,
+  const Eigen::PlainObjectBase<DerivedF>& F)
+{
+  using namespace std;
+  using namespace Eigen;
+  assert(V.cols() == 3 && "V should have 3 columns");
+  ofstream s(str);
+  if(!s.is_open())
+  {
+    fprintf(stderr,"IOError: writeOBJ() could not open %s\n",str.c_str());
+    return false;
+  }
+  s<<
+    V.format(IOFormat(FullPrecision,DontAlignCols," ","\n","v ","","",""))<<
+    (F.array()+1).format(IOFormat(FullPrecision,DontAlignCols," ","\n","f ","","",""));
+  return true;
+}
+
 #ifdef IGL_STATIC_LIBRARY
 // Explicit template specialization
 // generated by autoexplicit.sh

+ 11 - 8
include/igl/writeOBJ.h

@@ -19,15 +19,13 @@ namespace igl
   // Write a mesh in an ascii obj file
   // Inputs:
   //   str  path to outputfile
-  //   V  eigen double matrix #V by 3 (mesh vertices)
-  //   F  eigen int matrix #F by 3 (mesh indices)
+  //   V  #V by 3 mesh vertex positions
+  //   F  #F by 3|4 mesh indices into V
+  //   CN #CN by 3 normal vectors
+  //   FN  #F by 3|4 corner normal indices into CN
+  //   TC  #TC by 2|3 texture coordinates
+  //   FTC #F by 3|4 corner texture coord indices into TC
   // Returns true on success, false on error
-  template <typename DerivedV, typename DerivedF>
-  IGL_INLINE bool writeOBJ(
-    const std::string str,
-    const Eigen::PlainObjectBase<DerivedV>& V,
-    const Eigen::PlainObjectBase<DerivedF>& F);
-  
   template <typename DerivedV, typename DerivedF, typename DerivedT>
   IGL_INLINE bool writeOBJ(
     const std::string str,
@@ -37,6 +35,11 @@ namespace igl
     const Eigen::PlainObjectBase<DerivedF>& FN,
     const Eigen::PlainObjectBase<DerivedT>& TC,
     const Eigen::PlainObjectBase<DerivedF>& FTC);
+  template <typename DerivedV, typename DerivedF>
+  IGL_INLINE bool writeOBJ(
+    const std::string str,
+    const Eigen::PlainObjectBase<DerivedV>& V,
+    const Eigen::PlainObjectBase<DerivedF>& F);
 
 }
 

+ 12 - 27
include/igl/writeOFF.cpp

@@ -7,6 +7,7 @@
 // obtain one at http://mozilla.org/MPL/2.0/.
 #include "writeOFF.h"
 #include <cstdio>
+#include <fstream>
 
 // write mesh to an ascii off file
 template <typename DerivedV, typename DerivedF>
@@ -15,39 +16,23 @@ IGL_INLINE bool igl::writeOFF(
   const Eigen::PlainObjectBase<DerivedV>& V,
   const Eigen::PlainObjectBase<DerivedF>& F)
 {
-  FILE *fp = fopen (fname.c_str(), "w");
-
-
-  if (!fp)
+  using namespace std;
+  using namespace Eigen;
+  assert(V.cols() == 3 && "V should have 3 columns");
+  ofstream s(fname);
+  if(!s.is_open())
   {
-      fprintf (stderr, "writeOFF(): could not open file %s", fname.c_str());
+    fprintf(stderr,"IOError: writeOFF() could not open %s\n",fname.c_str());
     return false;
   }
 
-  fprintf (fp, "OFF\n%d %d 0\n",  (int) V.rows(), (int) F.rows());
-
-  for (int i = 0; i < V.rows(); i++)
-  {
-    fprintf(
-      fp,
-      "%0.17g %0.17g %0.17g\n",
-      (double)V(i,0),
-      (double)V(i,1),
-      (double)V(i,2));
-  }
-
-//  for (int i = 0; i < F.rows(); i++)
-//      fprintf (fp, "3 %d %d %d\n", F(i,0), F(i,1), F(i,2));
-  for (int i = 0; i < (int)F.rows(); i++)
-  {
-    fprintf (fp, "%d", (int)F.cols());
-    for (int j = 0; j < (int)F.cols(); j++)
-      fprintf (fp, " %d", (int)F(i,j));
-    fprintf (fp, "\n");
-  }
-  fclose (fp);
+  s<<
+    "OFF\n"<<V.rows()<<" "<<F.rows()<<" 0\n"<<
+    V.format(IOFormat(FullPrecision,DontAlignCols," ","\n","","","","\n"))<<
+    (F.array()).format(IOFormat(FullPrecision,DontAlignCols," ","\n","3 ","","","\n"));
   return true;
 }
+
 #ifdef IGL_STATIC_LIBRARY
 // Explicit template specialization
 // generated by autoexplicit.sh