writeOFF.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "writeOFF.h"
  9. #include <cstdio>
  10. // write mesh to an ascii off file
  11. template <typename DerivedV, typename DerivedF>
  12. IGL_INLINE bool igl::writeOFF(
  13. const std::string fname,
  14. const Eigen::PlainObjectBase<DerivedV>& V,
  15. const Eigen::PlainObjectBase<DerivedF>& F)
  16. {
  17. FILE *fp = fopen (fname.c_str(), "w");
  18. if (!fp)
  19. {
  20. fprintf (stderr, "writeOFF(): could not open file %s", fname.c_str());
  21. return false;
  22. }
  23. fprintf (fp, "OFF\n%d %d 0\n", (int) V.rows(), (int) F.rows());
  24. for (int i = 0; i < V.rows(); i++)
  25. {
  26. fprintf(
  27. fp,
  28. "%0.17g %0.17g %0.17g\n",
  29. (double)V(i,0),
  30. (double)V(i,1),
  31. (double)V(i,2));
  32. }
  33. // for (int i = 0; i < F.rows(); i++)
  34. // fprintf (fp, "3 %d %d %d\n", F(i,0), F(i,1), F(i,2));
  35. for (int i = 0; i < (int)F.rows(); i++)
  36. {
  37. fprintf (fp, "%d", (int)F.cols());
  38. for (int j = 0; j < (int)F.cols(); j++)
  39. fprintf (fp, " %d", (int)F(i,j));
  40. fprintf (fp, "\n");
  41. }
  42. fclose (fp);
  43. return true;
  44. }
  45. #ifdef IGL_STATIC_LIBRARY
  46. // Explicit template specialization
  47. // generated by autoexplicit.sh
  48. template bool igl::writeOFF<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> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  49. template bool igl::writeOFF<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> > const&);
  50. template bool igl::writeOFF<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> > const&);
  51. template bool igl::writeOFF<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&);
  52. #endif