writeOFF.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  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::MatrixBase<DerivedV>& V,
  16. const Eigen::MatrixBase<DerivedF>& F)
  17. {
  18. assert(V.cols() == 3 && "V should have 3 columns");
  19. std::ofstream s(fname);
  20. if(!s.is_open())
  21. {
  22. fprintf(stderr,"IOError: writeOFF() could not open %s\n",fname.c_str());
  23. return false;
  24. }
  25. s<<
  26. "OFF\n"<<V.rows()<<" "<<F.rows()<<" 0\n"<<
  27. V.format(Eigen::IOFormat(Eigen::FullPrecision,Eigen::DontAlignCols," ","\n","","","","\n"))<<
  28. (F.array()).format(Eigen::IOFormat(Eigen::FullPrecision,Eigen::DontAlignCols," ","\n",std::to_string(F.cols()) + " ","","","\n"));
  29. return true;
  30. }
  31. // write mesh and colors-by-vertex to an ascii off file
  32. template <typename DerivedV, typename DerivedF, typename DerivedC>
  33. IGL_INLINE bool igl::writeOFF(
  34. const std::string fname,
  35. const Eigen::MatrixBase<DerivedV>& V,
  36. const Eigen::MatrixBase<DerivedF>& F,
  37. const Eigen::MatrixBase<DerivedC>& C)
  38. {
  39. assert(V.cols() == 3 && "V should have 3 columns");
  40. assert(C.cols() == 3 && "C should have 3 columns");
  41. if(V.rows() != C.rows())
  42. {
  43. fprintf(stderr,"IOError: writeOFF() Only color per vertex supported. V and C should have same size.\n");
  44. return false;
  45. }
  46. std::ofstream s(fname);
  47. if(!s.is_open())
  48. {
  49. fprintf(stderr,"IOError: writeOFF() could not open %s\n",fname.c_str());
  50. return false;
  51. }
  52. //Check if RGB values are in the range [0..1] or [0..255]
  53. int rgbScale = (C.maxCoeff() <= 1.0)?255:1;
  54. // Use RGB_Array instead of RGB because of clash with mingw macro
  55. // (https://github.com/libigl/libigl/pull/679)
  56. Eigen::Matrix<typename DerivedC::Scalar,Eigen::Dynamic,Eigen::Dynamic> RGB_Array = rgbScale * C;
  57. s<< "COFF\n"<<V.rows()<<" "<<F.rows()<<" 0\n";
  58. for (unsigned i=0; i< V.rows(); i++)
  59. {
  60. s <<V.row(i).format(Eigen::IOFormat(Eigen::FullPrecision,Eigen::DontAlignCols," "," ","","",""," "));
  61. s << unsigned(RGB_Array(i,0)) << " " << unsigned(RGB_Array(i,1)) << " " << unsigned(RGB_Array(i,2)) << " 255\n";
  62. }
  63. s<<(F.array()).format(Eigen::IOFormat(Eigen::FullPrecision,Eigen::DontAlignCols," ","\n",std::to_string(F.cols()) + " ","","","\n"));
  64. return true;
  65. }
  66. #ifdef IGL_STATIC_LIBRARY
  67. // Explicit template instantiation
  68. // generated by autoexplicit.sh
  69. template bool igl::writeOFF<Eigen::Matrix<float, -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::MatrixBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&);
  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::MatrixBase<Eigen::Matrix<double, 8, 3, 0, 8, 3> > const&, Eigen::MatrixBase<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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<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::MatrixBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<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::MatrixBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<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::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<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::MatrixBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<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::MatrixBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<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::MatrixBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&);
  81. #endif