readPLY.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "readPLY.h"
  9. #include "list_to_matrix.h"
  10. #include "ply.h"
  11. #include <iostream>
  12. template <
  13. typename Vtype,
  14. typename Ftype,
  15. typename Ntype,
  16. typename UVtype>
  17. IGL_INLINE bool igl::readPLY(
  18. const std::string & filename,
  19. std::vector<std::vector<Vtype> > & V,
  20. std::vector<std::vector<Ftype> > & F,
  21. std::vector<std::vector<Ntype> > & N,
  22. std::vector<std::vector<UVtype> > & UV)
  23. {
  24. using namespace std;
  25. // Largely follows ply2iv.c
  26. typedef struct Vertex {
  27. double x,y,z; /* position */
  28. double nx,ny,nz; /* surface normal */
  29. double s,t; /* texture coordinates */
  30. void *other_props; /* other properties */
  31. } Vertex;
  32. typedef struct Face {
  33. unsigned char nverts; /* number of vertex indices in list */
  34. int *verts; /* vertex index list */
  35. void *other_props; /* other properties */
  36. } Face;
  37. PlyProperty vert_props[] = { /* list of property information for a vertex */
  38. {"x", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,x), 0, 0, 0, 0},
  39. {"y", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,y), 0, 0, 0, 0},
  40. {"z", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,z), 0, 0, 0, 0},
  41. {"nx", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,nx), 0, 0, 0, 0},
  42. {"ny", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,ny), 0, 0, 0, 0},
  43. {"nz", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,nz), 0, 0, 0, 0},
  44. {"s", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,s), 0, 0, 0, 0},
  45. {"t", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,t), 0, 0, 0, 0},
  46. };
  47. PlyProperty face_props[] = { /* list of property information for a face */
  48. {"vertex_indices", PLY_INT, PLY_INT, offsetof(Face,verts),
  49. 1, PLY_UCHAR, PLY_UCHAR, offsetof(Face,nverts)},
  50. };
  51. FILE * fp = fopen(filename.c_str(),"r");
  52. if(fp == NULL)
  53. {
  54. return false;
  55. }
  56. int nelems;
  57. char ** elem_names;
  58. PlyFile * in_ply = ply_read(fp,&nelems,&elem_names);
  59. if(in_ply==NULL)
  60. {
  61. return false;
  62. }
  63. bool has_normals = false;
  64. bool has_texture_coords = false;
  65. PlyProperty **plist;
  66. int nprops;
  67. int elem_count;
  68. plist = ply_get_element_description (in_ply,"vertex", &elem_count, &nprops);
  69. if (plist != NULL)
  70. {
  71. /* set up for getting vertex elements */
  72. ply_get_property (in_ply,"vertex",&vert_props[0]);
  73. ply_get_property (in_ply,"vertex",&vert_props[1]);
  74. ply_get_property (in_ply,"vertex",&vert_props[2]);
  75. for (int j = 0; j < nprops; j++)
  76. {
  77. PlyProperty * prop = plist[j];
  78. if (equal_strings ("nx", prop->name)
  79. || equal_strings ("ny", prop->name)
  80. || equal_strings ("nz", prop->name))
  81. {
  82. ply_get_property (in_ply,"vertex",&vert_props[3]);
  83. ply_get_property (in_ply,"vertex",&vert_props[4]);
  84. ply_get_property (in_ply,"vertex",&vert_props[5]);
  85. has_normals = true;
  86. }
  87. if (equal_strings ("s", prop->name) ||
  88. equal_strings ("t", prop->name))
  89. {
  90. ply_get_property(in_ply,"vertex",&vert_props[6]);
  91. ply_get_property(in_ply,"vertex",&vert_props[7]);
  92. has_texture_coords = true;
  93. }
  94. }
  95. // Is this call necessary?
  96. ply_get_other_properties(in_ply,"vertex",
  97. offsetof(Vertex,other_props));
  98. V.resize(elem_count,std::vector<Vtype>(3));
  99. if(has_normals)
  100. {
  101. N.resize(elem_count,std::vector<Ntype>(3));
  102. }else
  103. {
  104. N.resize(0);
  105. }
  106. if(has_texture_coords)
  107. {
  108. UV.resize(elem_count,std::vector<UVtype>(2));
  109. }else
  110. {
  111. UV.resize(0);
  112. }
  113. for(int j = 0;j<elem_count;j++)
  114. {
  115. Vertex v;
  116. ply_get_element_setup(in_ply,"vertex",3,vert_props);
  117. ply_get_element(in_ply,(void*)&v);
  118. V[j][0] = v.x;
  119. V[j][1] = v.y;
  120. V[j][2] = v.z;
  121. if(has_normals)
  122. {
  123. N[j][0] = v.nx;
  124. N[j][1] = v.ny;
  125. N[j][2] = v.nz;
  126. }
  127. if(has_texture_coords)
  128. {
  129. UV[j][0] = v.s;
  130. UV[j][1] = v.t;
  131. }
  132. }
  133. }
  134. plist = ply_get_element_description (in_ply,"face", &elem_count, &nprops);
  135. if (plist != NULL)
  136. {
  137. F.resize(elem_count);
  138. ply_get_property(in_ply,"face",&face_props[0]);
  139. for (int j = 0; j < elem_count; j++)
  140. {
  141. Face f;
  142. ply_get_element(in_ply, (void *) &f);
  143. for(size_t c = 0;c<f.nverts;c++)
  144. {
  145. F[j].push_back(f.verts[c]);
  146. }
  147. }
  148. }
  149. ply_close(in_ply);
  150. return true;
  151. }
  152. template <
  153. typename DerivedV,
  154. typename DerivedF,
  155. typename DerivedN,
  156. typename DerivedUV>
  157. IGL_INLINE bool igl::readPLY(
  158. const std::string & filename,
  159. Eigen::PlainObjectBase<DerivedV> & V,
  160. Eigen::PlainObjectBase<DerivedF> & F,
  161. Eigen::PlainObjectBase<DerivedN> & N,
  162. Eigen::PlainObjectBase<DerivedUV> & UV)
  163. {
  164. std::vector<std::vector<typename DerivedV::Scalar> > vV;
  165. std::vector<std::vector<typename DerivedF::Scalar> > vF;
  166. std::vector<std::vector<typename DerivedN::Scalar> > vN;
  167. std::vector<std::vector<typename DerivedUV::Scalar> > vUV;
  168. if(!readPLY(filename,vV,vF,vN,vUV))
  169. {
  170. return false;
  171. }
  172. return
  173. list_to_matrix(vV,V) &&
  174. list_to_matrix(vF,F) &&
  175. list_to_matrix(vN,N) &&
  176. list_to_matrix(vUV,UV);
  177. }
  178. template <
  179. typename DerivedV,
  180. typename DerivedF>
  181. IGL_INLINE bool igl::readPLY(
  182. const std::string & filename,
  183. Eigen::PlainObjectBase<DerivedV> & V,
  184. Eigen::PlainObjectBase<DerivedF> & F)
  185. {
  186. Eigen::MatrixXd N,UV;
  187. return readPLY(filename,V,F,N,UV);
  188. }
  189. #ifdef IGL_STATIC_LIBRARY
  190. // Explicit template specialization
  191. template bool igl::readPLY<double, int, double, double>(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&);
  192. #endif