writeOFF.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include <fstream>
  11. // write mesh to an ascii off file
  12. template <typename DerivedV, typename DerivedF>
  13. IGL_INLINE bool igl::writeOFF(
  14. const std::string fname,
  15. const Eigen::PlainObjectBase<DerivedV>& V,
  16. const Eigen::PlainObjectBase<DerivedF>& F)
  17. {
  18. using namespace std;
  19. using namespace Eigen;
  20. assert(V.cols() == 3 && "V should have 3 columns");
  21. ofstream s(fname);
  22. if(!s.is_open())
  23. {
  24. fprintf(stderr,"IOError: writeOFF() could not open %s\n",fname.c_str());
  25. return false;
  26. }
  27. s<<
  28. "OFF\n"<<V.rows()<<" "<<F.rows()<<" 0\n"<<
  29. V.format(IOFormat(FullPrecision,DontAlignCols," ","\n","","","","\n"))<<
  30. (F.array()).format(IOFormat(FullPrecision,DontAlignCols," ","\n","3 ","","","\n"));
  31. return true;
  32. }
  33. // write mesh and colors-by-vertex to an ascii off file
  34. template <typename DerivedV, typename DerivedF, typename DerivedC>
  35. IGL_INLINE bool igl::writeOFF(
  36. const std::string fname,
  37. const Eigen::PlainObjectBase<DerivedV>& V,
  38. const Eigen::PlainObjectBase<DerivedF>& F,
  39. const Eigen::PlainObjectBase<DerivedC>& C)
  40. {
  41. using namespace std;
  42. using namespace Eigen;
  43. assert(V.cols() == 3 && "V should have 3 columns");
  44. assert(C.cols() == 3 && "C should have 3 columns");
  45. if(V.rows() != C.rows())
  46. {
  47. fprintf(stderr,"IOError: writeOFF() Only color per vertex supported. V and C should have same size.\n");
  48. return false;
  49. }
  50. ofstream s(fname);
  51. if(!s.is_open())
  52. {
  53. fprintf(stderr,"IOError: writeOFF() could not open %s\n",fname.c_str());
  54. return false;
  55. }
  56. //Check if RGB values are in the range [0..1] or [0..255]
  57. int rgbScale = (C.maxCoeff() <= 1.0)?255:1;
  58. Eigen::MatrixXd RGB = rgbScale * C;
  59. s<< "COFF\n"<<V.rows()<<" "<<F.rows()<<" 0\n";
  60. for (unsigned i=0; i< V.rows(); i++)
  61. {
  62. s <<V.row(i).format(IOFormat(FullPrecision,DontAlignCols," "," ","","",""," "));
  63. s << unsigned(RGB(i,0)) << " " << unsigned(RGB(i,1)) << " " << unsigned(RGB(i,2)) << " 255\n";
  64. }
  65. s<<(F.array()).format(IOFormat(FullPrecision,DontAlignCols," ","\n","3 ","","","\n"));
  66. return true;
  67. }
  68. #ifdef IGL_STATIC_LIBRARY
  69. // Explicit template specialization
  70. // generated by autoexplicit.sh
  71. template bool igl::writeOFF<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> >, Eigen::PlainObjectBase<Eigen::Matrix<double, 8, 3, 0, 8, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, 12, 3, 0, 12, 3> > const&);
  72. // generated by autoexplicit.sh
  73. 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&);
  74. 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&);
  75. 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&);
  76. template bool igl::writeOFF<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> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&);
  77. template bool igl::writeOFF<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(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, 3, 0, -1, 3> > const&);
  78. template bool igl::writeOFF<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> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&);
  79. template bool igl::writeOFF<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3>, Eigen::Matrix<double, -1, 3, 1, -1, 3> >(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<int, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&);
  80. template bool igl::writeOFF<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3>, Eigen::Matrix<double, -1, 3, 1, -1, 3> >(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, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&);
  81. #endif