readPLY.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. FILE * ply_file = fopen(filename.c_str(),"r");
  27. if(ply_file == NULL)
  28. {
  29. return false;
  30. }
  31. return readPLY(ply_file,V,F,N,UV);
  32. }
  33. template <
  34. typename Vtype,
  35. typename Ftype,
  36. typename Ntype,
  37. typename UVtype>
  38. IGL_INLINE bool igl::readPLY(
  39. FILE * ply_file,
  40. std::vector<std::vector<Vtype> > & V,
  41. std::vector<std::vector<Ftype> > & F,
  42. std::vector<std::vector<Ntype> > & N,
  43. std::vector<std::vector<UVtype> > & UV)
  44. {
  45. using namespace std;
  46. typedef struct Vertex {
  47. double x,y,z; /* position */
  48. double nx,ny,nz; /* surface normal */
  49. double s,t; /* texture coordinates */
  50. void *other_props; /* other properties */
  51. } Vertex;
  52. typedef struct Face {
  53. unsigned char nverts; /* number of vertex indices in list */
  54. int *verts; /* vertex index list */
  55. void *other_props; /* other properties */
  56. } Face;
  57. PlyProperty vert_props[] = { /* list of property information for a vertex */
  58. {"x", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,x), 0, 0, 0, 0},
  59. {"y", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,y), 0, 0, 0, 0},
  60. {"z", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,z), 0, 0, 0, 0},
  61. {"nx", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,nx), 0, 0, 0, 0},
  62. {"ny", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,ny), 0, 0, 0, 0},
  63. {"nz", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,nz), 0, 0, 0, 0},
  64. {"s", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,s), 0, 0, 0, 0},
  65. {"t", PLY_DOUBLE, PLY_DOUBLE, offsetof(Vertex,t), 0, 0, 0, 0},
  66. };
  67. PlyProperty face_props[] = { /* list of property information for a face */
  68. {"vertex_indices", PLY_INT, PLY_INT, offsetof(Face,verts),
  69. 1, PLY_UCHAR, PLY_UCHAR, offsetof(Face,nverts)},
  70. };
  71. int nelems;
  72. char ** elem_names;
  73. PlyFile * in_ply = ply_read(ply_file,&nelems,&elem_names);
  74. if(in_ply==NULL)
  75. {
  76. return false;
  77. }
  78. bool has_normals = false;
  79. bool has_texture_coords = false;
  80. PlyProperty **plist;
  81. int nprops;
  82. int elem_count;
  83. plist = ply_get_element_description (in_ply,"vertex", &elem_count, &nprops);
  84. if (plist != NULL)
  85. {
  86. /* set up for getting vertex elements */
  87. ply_get_property (in_ply,"vertex",&vert_props[0]);
  88. ply_get_property (in_ply,"vertex",&vert_props[1]);
  89. ply_get_property (in_ply,"vertex",&vert_props[2]);
  90. for (int j = 0; j < nprops; j++)
  91. {
  92. PlyProperty * prop = plist[j];
  93. if (equal_strings ("nx", prop->name)
  94. || equal_strings ("ny", prop->name)
  95. || equal_strings ("nz", prop->name))
  96. {
  97. ply_get_property (in_ply,"vertex",&vert_props[3]);
  98. ply_get_property (in_ply,"vertex",&vert_props[4]);
  99. ply_get_property (in_ply,"vertex",&vert_props[5]);
  100. has_normals = true;
  101. }
  102. if (equal_strings ("s", prop->name) ||
  103. equal_strings ("t", prop->name))
  104. {
  105. ply_get_property(in_ply,"vertex",&vert_props[6]);
  106. ply_get_property(in_ply,"vertex",&vert_props[7]);
  107. has_texture_coords = true;
  108. }
  109. }
  110. // Is this call necessary?
  111. ply_get_other_properties(in_ply,"vertex",
  112. offsetof(Vertex,other_props));
  113. V.resize(elem_count,std::vector<Vtype>(3));
  114. if(has_normals)
  115. {
  116. N.resize(elem_count,std::vector<Ntype>(3));
  117. }else
  118. {
  119. N.resize(0);
  120. }
  121. if(has_texture_coords)
  122. {
  123. UV.resize(elem_count,std::vector<UVtype>(2));
  124. }else
  125. {
  126. UV.resize(0);
  127. }
  128. for(int j = 0;j<elem_count;j++)
  129. {
  130. Vertex v;
  131. ply_get_element_setup(in_ply,"vertex",3,vert_props);
  132. ply_get_element(in_ply,(void*)&v);
  133. V[j][0] = v.x;
  134. V[j][1] = v.y;
  135. V[j][2] = v.z;
  136. if(has_normals)
  137. {
  138. N[j][0] = v.nx;
  139. N[j][1] = v.ny;
  140. N[j][2] = v.nz;
  141. }
  142. if(has_texture_coords)
  143. {
  144. UV[j][0] = v.s;
  145. UV[j][1] = v.t;
  146. }
  147. }
  148. }
  149. plist = ply_get_element_description (in_ply,"face", &elem_count, &nprops);
  150. if (plist != NULL)
  151. {
  152. F.resize(elem_count);
  153. ply_get_property(in_ply,"face",&face_props[0]);
  154. for (int j = 0; j < elem_count; j++)
  155. {
  156. Face f;
  157. ply_get_element(in_ply, (void *) &f);
  158. for(size_t c = 0;c<f.nverts;c++)
  159. {
  160. F[j].push_back(f.verts[c]);
  161. }
  162. }
  163. }
  164. ply_close(in_ply);
  165. return true;
  166. }
  167. template <
  168. typename DerivedV,
  169. typename DerivedF,
  170. typename DerivedN,
  171. typename DerivedUV>
  172. IGL_INLINE bool igl::readPLY(
  173. const std::string & filename,
  174. Eigen::PlainObjectBase<DerivedV> & V,
  175. Eigen::PlainObjectBase<DerivedF> & F,
  176. Eigen::PlainObjectBase<DerivedN> & N,
  177. Eigen::PlainObjectBase<DerivedUV> & UV)
  178. {
  179. std::vector<std::vector<typename DerivedV::Scalar> > vV;
  180. std::vector<std::vector<typename DerivedF::Scalar> > vF;
  181. std::vector<std::vector<typename DerivedN::Scalar> > vN;
  182. std::vector<std::vector<typename DerivedUV::Scalar> > vUV;
  183. if(!readPLY(filename,vV,vF,vN,vUV))
  184. {
  185. return false;
  186. }
  187. return
  188. list_to_matrix(vV,V) &&
  189. list_to_matrix(vF,F) &&
  190. list_to_matrix(vN,N) &&
  191. list_to_matrix(vUV,UV);
  192. }
  193. template <
  194. typename DerivedV,
  195. typename DerivedF>
  196. IGL_INLINE bool igl::readPLY(
  197. const std::string & filename,
  198. Eigen::PlainObjectBase<DerivedV> & V,
  199. Eigen::PlainObjectBase<DerivedF> & F)
  200. {
  201. Eigen::MatrixXd N,UV;
  202. return readPLY(filename,V,F,N,UV);
  203. }
  204. #ifdef IGL_STATIC_LIBRARY
  205. // Explicit template specialization
  206. 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> > > >&);
  207. #endif