Browse Source

ply support any vertex/normal/uv/face type

Former-commit-id: 56c9d1af8f5fd584a7722b6beab27e4699731d30
Alec Jacobson 7 years ago
parent
commit
416a0cd915
2 changed files with 50 additions and 33 deletions
  1. 44 27
      include/igl/writePLY.cpp
  2. 6 6
      include/igl/writePLY.h

+ 44 - 27
include/igl/writePLY.cpp

@@ -11,6 +11,19 @@
 #include <igl/ply.h>
 #include <igl/ply.h>
 #include <vector>
 #include <vector>
 
 
+namespace
+{
+  inline template <typename Scalar> int ply_type();
+  inline template <> int ply_type<char>(){ return PLY_CHAR; }
+  inline template <> int ply_type<short>(){ return PLY_SHORT; }
+  inline template <> int ply_type<int>(){ return PLY_INT; }
+  inline template <> int ply_type<unsigned char>(){ return PLY_UCHAR; }
+  inline template <> int ply_type<unsigned short>(){ return PLY_SHORT; }
+  inline template <> int ply_type<unsigned int>(){ return PLY_UINT; }
+  inline template <> int ply_type<float>(){ return PLY_FLOAT; }
+  inline template <> int ply_type<double>(){ return PLY_DOUBLE; }
+}
+
 template <
 template <
   typename DerivedV,
   typename DerivedV,
   typename DerivedF,
   typename DerivedF,
@@ -18,43 +31,47 @@ template <
   typename DerivedUV>
   typename DerivedUV>
 IGL_INLINE bool igl::writePLY(
 IGL_INLINE bool igl::writePLY(
   const std::string & filename,
   const std::string & filename,
-  const Eigen::PlainObjectBase<DerivedV> & V,
-  const Eigen::PlainObjectBase<DerivedF> & F,
-  const Eigen::PlainObjectBase<DerivedN> & N,
-  const Eigen::PlainObjectBase<DerivedUV> & UV,
+  const Eigen::MatrixBase<DerivedV> & V,
+  const Eigen::MatrixBase<DerivedF> & F,
+  const Eigen::MatrixBase<DerivedN> & N,
+  const Eigen::MatrixBase<DerivedUV> & UV,
   const bool ascii)
   const bool ascii)
 {
 {
   // Largely based on obj2ply.c
   // Largely based on obj2ply.c
+  typedef typename DerivedV::Scalar VScalar;
+  typedef typename DerivedN::Scalar NScalar;
+  typedef typename DerivedUV::Scalar UVScalar;
+  typedef typename DerivedF::Scalar FScalar;
 
 
   typedef struct Vertex
   typedef struct Vertex
   {
   {
-    double x,y,z,w;          /* position */
-    double nx,ny,nz;         /* surface normal */
-    double s,t;              /* texture coordinates */
+    VScalar x,y,z,w;          /* position */
+    NScalar nx,ny,nz;         /* surface normal */
+    UVScalar s,t;              /* texture coordinates */
   } Vertex;
   } Vertex;
 
 
   typedef struct Face
   typedef struct Face
   {
   {
     unsigned char nverts;    /* number of vertex indices in list */
     unsigned char nverts;    /* number of vertex indices in list */
-    int *verts;              /* vertex index list */
+    FScalar *verts;              /* vertex index list */
   } Face;
   } Face;
 
 
   PlyProperty vert_props[] =
   PlyProperty vert_props[] =
   { /* list of property information for a vertex */
   { /* list of property information for a vertex */
-    {"x", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,x), 0, 0, 0, 0},
-    {"y", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,y), 0, 0, 0, 0},
-    {"z", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,z), 0, 0, 0, 0},
-    {"nx", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,nx), 0, 0, 0, 0},
-    {"ny", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,ny), 0, 0, 0, 0},
-    {"nz", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,nz), 0, 0, 0, 0},
-    {"s", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,s), 0, 0, 0, 0},
-    {"t", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,t), 0, 0, 0, 0},
+    {"x", ply_type<VScalar>(), ply_type<VScalar>(),offsetof(Vertex,x),0,0,0,0},
+    {"y", ply_type<VScalar>(), ply_type<VScalar>(),offsetof(Vertex,y),0,0,0,0},
+    {"z", ply_type<VScalar>(), ply_type<VScalar>(),offsetof(Vertex,z),0,0,0,0},
+    {"nx",ply_type<NScalar>(), ply_type<NScalar>(),offsetof(Vertex,nx),0,0,0,0},
+    {"ny",ply_type<NScalar>(), ply_type<NScalar>(),offsetof(Vertex,ny),0,0,0,0},
+    {"nz",ply_type<NScalar>(), ply_type<NScalar>(),offsetof(Vertex,nz),0,0,0,0},
+    {"s", ply_type<UVScalar>(),ply_type<UVScalar>(),offsetof(Vertex,s),0,0,0,0},
+    {"t", ply_type<UVScalar>(),ply_type<UVScalar>(),offsetof(Vertex,t),0,0,0,0},
   };
   };
 
 
   PlyProperty face_props[] =
   PlyProperty face_props[] =
   { /* list of property information for a face */
   { /* list of property information for a face */
-    {"vertex_indices", PLY_INT, PLY_INT, offsetof(Face,verts),
-      1, PLY_UCHAR, PLY_UCHAR, offsetof(Face,nverts)},
+    {"vertex_indices", ply_type<FScalar>(), ply_type<FScalar>(), 
+      offsetof(Face,verts), 1, PLY_UCHAR, PLY_UCHAR, offsetof(Face,nverts)},
   };
   };
   const bool has_normals = N.rows() > 0;
   const bool has_normals = N.rows() > 0;
   const bool has_texture_coords = UV.rows() > 0;
   const bool has_texture_coords = UV.rows() > 0;
@@ -80,7 +97,7 @@ IGL_INLINE bool igl::writePLY(
   for(size_t i = 0;i<(size_t)F.rows();i++)
   for(size_t i = 0;i<(size_t)F.rows();i++)
   {
   {
     flist[i].nverts = F.cols();
     flist[i].nverts = F.cols();
-    flist[i].verts = new int[F.cols()];
+    flist[i].verts = new FScalar[F.cols()];
     for(size_t c = 0;c<(size_t)F.cols();c++)
     for(size_t c = 0;c<(size_t)F.cols();c++)
     {
     {
       flist[i].verts[c] = F(i,c);
       flist[i].verts[c] = F(i,c);
@@ -145,21 +162,21 @@ template <
   typename DerivedF>
   typename DerivedF>
 IGL_INLINE bool igl::writePLY(
 IGL_INLINE bool igl::writePLY(
   const std::string & filename,
   const std::string & filename,
-  const Eigen::PlainObjectBase<DerivedV> & V,
-  const Eigen::PlainObjectBase<DerivedF> & F,
+  const Eigen::MatrixBase<DerivedV> & V,
+  const Eigen::MatrixBase<DerivedF> & F,
   const bool ascii)
   const bool ascii)
 {
 {
-  Eigen::MatrixXd N,UV;
+  Eigen::Matrix<typename DerivedV::Scalar,Eigen::Dynamic,Eigen::Dynamic> N,UV;
   return writePLY(filename,V,F,N,UV,ascii);
   return writePLY(filename,V,F,N,UV,ascii);
 }
 }
 
 
 #ifdef IGL_STATIC_LIBRARY
 #ifdef IGL_STATIC_LIBRARY
 // Explicit template instantiation
 // Explicit template instantiation
 // generated by autoexplicit.sh
 // generated by autoexplicit.sh
-template bool igl::writePLY<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, bool);
+template bool igl::writePLY<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, bool);
 // generated by autoexplicit.sh
 // generated by autoexplicit.sh
-template bool igl::writePLY<Eigen::Matrix<double, 8, 3, 0, 8, 3>, Eigen::Matrix<int, 12, 3, 0, 12, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 8, 3, 0, 8, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, 12, 3, 0, 12, 3> > const&, bool);
-template bool igl::writePLY<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);
-template bool igl::writePLY<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, bool);
-template bool igl::writePLY<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, bool);
+template bool igl::writePLY<Eigen::Matrix<double, 8, 3, 0, 8, 3>, Eigen::Matrix<int, 12, 3, 0, 12, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 8, 3, 0, 8, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, 12, 3, 0, 12, 3> > const&, bool);
+template bool igl::writePLY<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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, bool);
+template bool igl::writePLY<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, bool);
+template bool igl::writePLY<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, bool);
 #endif
 #endif

+ 6 - 6
include/igl/writePLY.h

@@ -29,18 +29,18 @@ namespace igl
     typename DerivedUV>
     typename DerivedUV>
   IGL_INLINE bool writePLY(
   IGL_INLINE bool writePLY(
     const std::string & filename,
     const std::string & filename,
-    const Eigen::PlainObjectBase<DerivedV> & V,
-    const Eigen::PlainObjectBase<DerivedF> & F,
-    const Eigen::PlainObjectBase<DerivedN> & N,
-    const Eigen::PlainObjectBase<DerivedUV> & UV,
+    const Eigen::MatrixBase<DerivedV> & V,
+    const Eigen::MatrixBase<DerivedF> & F,
+    const Eigen::MatrixBase<DerivedN> & N,
+    const Eigen::MatrixBase<DerivedUV> & UV,
     const bool ascii = true);
     const bool ascii = true);
   template <
   template <
     typename DerivedV,
     typename DerivedV,
     typename DerivedF>
     typename DerivedF>
   IGL_INLINE bool writePLY(
   IGL_INLINE bool writePLY(
     const std::string & filename,
     const std::string & filename,
-    const Eigen::PlainObjectBase<DerivedV> & V,
-    const Eigen::PlainObjectBase<DerivedF> & F,
+    const Eigen::MatrixBase<DerivedV> & V,
+    const Eigen::MatrixBase<DerivedF> & F,
     const bool ascii = true);
     const bool ascii = true);
 }
 }
 #ifndef IGL_STATIC_LIBRARY
 #ifndef IGL_STATIC_LIBRARY