writePLY.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #include "writePLY.h"
  2. #include <fstream>
  3. #include "tinyply.h"
  4. namespace igl
  5. {
  6. template <typename Scalar> tinyply::Type tynyply_type();
  7. template <> tinyply::Type IGL_INLINE tynyply_type<char>(){ return tinyply::Type::INT8; }
  8. template <> tinyply::Type IGL_INLINE tynyply_type<short>(){ return tinyply::Type::INT16; }
  9. template <> tinyply::Type IGL_INLINE tynyply_type<int>(){ return tinyply::Type::INT32; }
  10. template <> tinyply::Type IGL_INLINE tynyply_type<unsigned char>(){ return tinyply::Type::UINT8; }
  11. template <> tinyply::Type IGL_INLINE tynyply_type<unsigned short>(){ return tinyply::Type::UINT16; }
  12. template <> tinyply::Type IGL_INLINE tynyply_type<unsigned int>(){ return tinyply::Type::UINT32; }
  13. template <> tinyply::Type IGL_INLINE tynyply_type<float>(){ return tinyply::Type::FLOAT32; }
  14. template <> tinyply::Type IGL_INLINE tynyply_type<double>(){ return tinyply::Type::FLOAT64; }
  15. template <
  16. typename DerivedV,
  17. typename DerivedF,
  18. typename DerivedE,
  19. typename DerivedN,
  20. typename DerivedUV,
  21. typename DerivedVD,
  22. typename DerivedFD,
  23. typename DerivedED
  24. >
  25. bool writePLY(
  26. std::ostream & ply_stream,
  27. const Eigen::MatrixBase<DerivedV> & V,
  28. const Eigen::MatrixBase<DerivedF> & F,
  29. const Eigen::MatrixBase<DerivedE> & E,
  30. const Eigen::MatrixBase<DerivedN> & N,
  31. const Eigen::MatrixBase<DerivedUV> & UV,
  32. const Eigen::MatrixBase<DerivedVD> & VD,
  33. const std::vector<std::string> & VDheader,
  34. const Eigen::MatrixBase<DerivedFD> & FD,
  35. const std::vector<std::string> & FDheader,
  36. const Eigen::MatrixBase<DerivedED> & ED,
  37. const std::vector<std::string> & EDheader,
  38. const std::vector<std::string> & comments,
  39. bool isBinary
  40. )
  41. {
  42. typedef typename DerivedV::Scalar VScalar;
  43. typedef typename DerivedN::Scalar NScalar;
  44. typedef typename DerivedUV::Scalar UVScalar;
  45. typedef typename DerivedF::Scalar FScalar;
  46. typedef typename DerivedE::Scalar EScalar;
  47. typedef typename DerivedVD::Scalar VDScalar;
  48. typedef typename DerivedFD::Scalar FDScalar;
  49. typedef typename DerivedED::Scalar EDScalar;
  50. // temporary storage for data to be passed to tinyply internals
  51. std::vector<VScalar> _v;
  52. std::vector<NScalar> _n;
  53. std::vector<UVScalar> _uv;
  54. std::vector<VDScalar> _vd;
  55. std::vector<FDScalar> _fd;
  56. std::vector<EScalar> _ev;
  57. std::vector<EDScalar> _ed;
  58. // check dimensions
  59. if( V.cols()!=3)
  60. {
  61. std::cerr << "writePLY: unexpected dimensions " << std::endl;
  62. return false;
  63. }
  64. tinyply::PlyFile file;
  65. _v.resize(V.size());
  66. Eigen::Map< Eigen::Matrix<VScalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > >( &_v[0], V.rows(), V.cols() ) = V;
  67. file.add_properties_to_element("vertex", { "x", "y", "z" },
  68. tynyply_type<VScalar>(), V.rows(), reinterpret_cast<uint8_t*>( &_v[0] ), tinyply::Type::INVALID, 0);
  69. if(N.rows()>0)
  70. {
  71. _n.resize(N.size());
  72. Eigen::Map<Eigen::Matrix<NScalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > >( &_n[0], N.rows(), N.cols() ) = N;
  73. file.add_properties_to_element("vertex", { "nx", "ny", "nz" },
  74. tynyply_type<NScalar>(), N.rows(), reinterpret_cast<uint8_t*>( &_n[0] ),tinyply::Type::INVALID, 0);
  75. }
  76. if(UV.rows()>0)
  77. {
  78. _uv.resize(UV.size());
  79. Eigen::Map<Eigen::Matrix<UVScalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > >( &_uv[0], UV.rows(), UV.cols() ) = UV;
  80. file.add_properties_to_element("vertex", { "u", "v" },
  81. tynyply_type<UVScalar>(), UV.rows() , reinterpret_cast<uint8_t*>( &_uv[0] ), tinyply::Type::INVALID, 0);
  82. }
  83. if(VD.cols()>0)
  84. {
  85. assert(VD.cols() == VDheader.size());
  86. assert(VD.rows() == V.rows());
  87. _vd.resize(VD.size());
  88. Eigen::Map< Eigen::Matrix<VDScalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > >( &_vd[0], VD.rows(), VD.cols() ) = VD;
  89. file.add_properties_to_element("vertex", VDheader,
  90. tynyply_type<VDScalar>(), VD.rows(), reinterpret_cast<uint8_t*>( &_vd[0] ), tinyply::Type::INVALID, 0);
  91. }
  92. std::vector<FScalar> _f(F.size());
  93. Eigen::Map<Eigen::Matrix<FScalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > >( &_f[0], F.rows(), F.cols() ) = F;
  94. file.add_properties_to_element("face", { "vertex_indices" },
  95. tynyply_type<FScalar>(), F.rows(), reinterpret_cast<uint8_t*>(&_f[0]), tinyply::Type::UINT8, F.cols() );
  96. if(FD.cols()>0)
  97. {
  98. assert(FD.rows()==F.rows());
  99. assert(FD.cols() == FDheader.size());
  100. _fd.resize(FD.size());
  101. Eigen::Map<Eigen::Matrix<FDScalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > >( &_fd[0], FD.rows(), FD.cols() ) = FD;
  102. file.add_properties_to_element("face", FDheader,
  103. tynyply_type<FDScalar>(), FD.rows(), reinterpret_cast<uint8_t*>( &_fd[0] ), tinyply::Type::INVALID, 0);
  104. }
  105. if(E.rows()>0)
  106. {
  107. assert(E.cols()==2);
  108. _ev.resize(E.size());
  109. Eigen::Map<Eigen::Matrix<EScalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > >( &_ev[0], E.rows(), E.cols() ) = E;
  110. file.add_properties_to_element("edge", { "vertex1", "vertex2" },
  111. tynyply_type<EScalar>(), E.rows() , reinterpret_cast<uint8_t*>( &_ev[0] ), tinyply::Type::INVALID, 0);
  112. }
  113. if(ED.cols()>0)
  114. {
  115. assert(ED.rows()==F.rows());
  116. assert(ED.cols() == EDheader.size());
  117. _ed.resize(ED.size());
  118. Eigen::Map<Eigen::Matrix<EDScalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > >( &_ed[0], ED.rows(), ED.cols() ) = ED;
  119. file.add_properties_to_element("face", FDheader,
  120. tynyply_type<EDScalar>(), ED.rows(), reinterpret_cast<uint8_t*>( &_ed[0] ), tinyply::Type::INVALID, 0);
  121. }
  122. for(auto a:comments)
  123. file.get_comments().push_back(a);
  124. // Write a binary file
  125. file.write(ply_stream, isBinary);
  126. return true;
  127. }
  128. template <
  129. typename DerivedV,
  130. typename DerivedF,
  131. typename DerivedE,
  132. typename DerivedN,
  133. typename DerivedUV,
  134. typename DerivedVD,
  135. typename DerivedFD,
  136. typename DerivedED
  137. >
  138. bool writePLY(
  139. const std::string & filename,
  140. const Eigen::MatrixBase<DerivedV> & V,
  141. const Eigen::MatrixBase<DerivedF> & F,
  142. const Eigen::MatrixBase<DerivedE> & E,
  143. const Eigen::MatrixBase<DerivedN> & N,
  144. const Eigen::MatrixBase<DerivedUV> & UV,
  145. const Eigen::MatrixBase<DerivedVD> & VD,
  146. const std::vector<std::string> & VDheader,
  147. const Eigen::MatrixBase<DerivedFD> & FD,
  148. const std::vector<std::string> & FDheader,
  149. const Eigen::MatrixBase<DerivedED> & ED,
  150. const std::vector<std::string> & EDheader,
  151. const std::vector<std::string> & comments,
  152. bool isBinary
  153. )
  154. {
  155. try
  156. {
  157. if(isBinary)
  158. {
  159. std::filebuf fb_binary;
  160. fb_binary.open(filename , std::ios::out | std::ios::binary);
  161. std::ostream outstream_binary(&fb_binary);
  162. if (outstream_binary.fail()) {
  163. std::cerr << "writePLY: Error opening file " << filename << std::endl;
  164. return false; //throw std::runtime_error("failed to open " + filename);
  165. }
  166. return writePLY(outstream_binary,V,F,E,N,UV,VD,VDheader,FD,FDheader,ED,EDheader,comments,isBinary);
  167. } else {
  168. std::filebuf fb_ascii;
  169. fb_ascii.open(filename, std::ios::out);
  170. std::ostream outstream_ascii(&fb_ascii);
  171. if (outstream_ascii.fail()) {
  172. std::cerr << "writePLY: Error opening file " << filename << std::endl;
  173. return false; //throw std::runtime_error("failed to open " + filename);
  174. }
  175. return writePLY(outstream_ascii,V,F,E,N,UV,VD,VDheader,FD,FDheader,ED,EDheader,comments,isBinary);
  176. }
  177. }
  178. catch(const std::exception& e)
  179. {
  180. std::cerr << "writePLY error: " << filename << e.what() << std::endl;
  181. }
  182. return false;
  183. }
  184. template <
  185. typename DerivedV,
  186. typename DerivedF
  187. >
  188. bool writePLY(
  189. const std::string & filename,
  190. const Eigen::MatrixBase<DerivedV> & V,
  191. const Eigen::MatrixBase<DerivedF> & F
  192. )
  193. {
  194. Eigen::MatrixXd _dummy;
  195. std::vector<std::string> _dummy_header;
  196. return writePLY(filename,V,F,_dummy, _dummy, _dummy, _dummy, _dummy_header, _dummy, _dummy_header, _dummy, _dummy_header, _dummy_header, true);
  197. }
  198. template <
  199. typename DerivedV,
  200. typename DerivedF,
  201. typename DerivedE
  202. >
  203. bool writePLY(
  204. const std::string & filename,
  205. const Eigen::MatrixBase<DerivedV> & V,
  206. const Eigen::MatrixBase<DerivedF> & F,
  207. const Eigen::MatrixBase<DerivedE> & E
  208. )
  209. {
  210. Eigen::MatrixXd _dummy;
  211. std::vector<std::string> _dummy_header;
  212. return writePLY(filename,V,F,E, _dummy, _dummy, _dummy, _dummy_header, _dummy, _dummy_header, _dummy, _dummy_header, _dummy_header, true);
  213. }
  214. template <
  215. typename DerivedV,
  216. typename DerivedF,
  217. typename DerivedN,
  218. typename DerivedUV
  219. >
  220. bool writePLY(
  221. const std::string & filename,
  222. const Eigen::MatrixBase<DerivedV> & V,
  223. const Eigen::MatrixBase<DerivedF> & F,
  224. const Eigen::MatrixBase<DerivedN> & N,
  225. const Eigen::MatrixBase<DerivedUV> & UV
  226. )
  227. {
  228. Eigen::MatrixXd _dummy;
  229. std::vector<std::string> _dummy_header;
  230. return writePLY(filename,V,F,_dummy, N,UV, _dummy, _dummy_header, _dummy, _dummy_header, _dummy, _dummy_header, _dummy_header, true);
  231. }
  232. template <
  233. typename DerivedV,
  234. typename DerivedF,
  235. typename DerivedE,
  236. typename DerivedN,
  237. typename DerivedUV
  238. >
  239. bool writePLY(
  240. const std::string & filename,
  241. const Eigen::MatrixBase<DerivedV> & V,
  242. const Eigen::MatrixBase<DerivedF> & F,
  243. const Eigen::MatrixBase<DerivedE> & E,
  244. const Eigen::MatrixBase<DerivedN> & N,
  245. const Eigen::MatrixBase<DerivedUV> & UV
  246. )
  247. {
  248. Eigen::MatrixXd _dummy;
  249. std::vector<std::string> _dummy_header;
  250. return writePLY(filename,V,F,E, N,UV, _dummy, _dummy_header, _dummy, _dummy_header, _dummy, _dummy_header, _dummy_header, true);
  251. }
  252. template <
  253. typename DerivedV,
  254. typename DerivedF
  255. >
  256. bool writePLY(
  257. const std::string & filename,
  258. const Eigen::MatrixBase<DerivedV> & V,
  259. const Eigen::MatrixBase<DerivedF> & F,
  260. bool force_ascii
  261. )
  262. {
  263. Eigen::MatrixXd _dummy(0,0);
  264. std::vector<std::string> _dummy_header;
  265. return writePLY(filename,V,F,_dummy, _dummy,_dummy, _dummy, _dummy_header,
  266. _dummy, _dummy_header, _dummy, _dummy_header, _dummy_header, force_ascii);
  267. }
  268. template <
  269. typename DerivedV,
  270. typename DerivedF,
  271. typename DerivedE
  272. >
  273. bool writePLY(
  274. const std::string & filename,
  275. const Eigen::MatrixBase<DerivedV> & V,
  276. const Eigen::MatrixBase<DerivedF> & F,
  277. const Eigen::MatrixBase<DerivedE> & E,
  278. bool force_ascii
  279. )
  280. {
  281. Eigen::MatrixXd _dummy(0,0);
  282. std::vector<std::string> _dummy_header;
  283. return writePLY(filename,V,F,E, _dummy,_dummy, _dummy, _dummy_header,
  284. _dummy, _dummy_header, _dummy, _dummy_header, _dummy_header, force_ascii);
  285. }
  286. template <
  287. typename DerivedV,
  288. typename DerivedF,
  289. typename DerivedN,
  290. typename DerivedUV,
  291. typename DerivedVD
  292. >
  293. bool writePLY(
  294. const std::string & filename,
  295. const Eigen::MatrixBase<DerivedV> & V,
  296. const Eigen::MatrixBase<DerivedF> & F,
  297. const Eigen::MatrixBase<DerivedN> & N,
  298. const Eigen::MatrixBase<DerivedUV> & UV,
  299. const Eigen::MatrixBase<DerivedVD> & VD,
  300. const std::vector<std::string> & VDheader,
  301. const std::vector<std::string> & comments
  302. )
  303. {
  304. Eigen::MatrixXd _dummy(0,0);
  305. std::vector<std::string> _dummy_header;
  306. return writePLY(filename,V,F,_dummy, N, UV, VD, VDheader,
  307. _dummy, _dummy_header, _dummy, _dummy_header, comments, true);
  308. }
  309. template <
  310. typename DerivedV,
  311. typename DerivedF,
  312. typename DerivedE,
  313. typename DerivedN,
  314. typename DerivedUV,
  315. typename DerivedVD
  316. >
  317. bool writePLY(
  318. const std::string & filename,
  319. const Eigen::MatrixBase<DerivedV> & V,
  320. const Eigen::MatrixBase<DerivedF> & F,
  321. const Eigen::MatrixBase<DerivedE> & E,
  322. const Eigen::MatrixBase<DerivedN> & N,
  323. const Eigen::MatrixBase<DerivedUV> & UV,
  324. const Eigen::MatrixBase<DerivedVD> & VD,
  325. const std::vector<std::string> & VDheader,
  326. const std::vector<std::string> & comments
  327. )
  328. {
  329. Eigen::MatrixXd _dummy(0,0);
  330. std::vector<std::string> _dummy_header;
  331. return writePLY(filename,V,F,E, N, UV, VD, VDheader,
  332. _dummy, _dummy_header, _dummy, _dummy_header, comments, true);
  333. }
  334. }
  335. #ifdef IGL_STATIC_LIBRARY
  336. // Explicit template instantiation
  337. 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&);
  338. template bool igl::writePLY<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -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&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, bool);
  339. template bool igl::writePLY<Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> > const&, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> > const&, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> > const&, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, bool);
  340. #endif