writeOFF.cpp 6.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // Use RGB_Array instead of RGB because of clash with mingw macro
  59. // (https://github.com/libigl/libigl/pull/679)
  60. Eigen::MatrixXd RGB_Array = rgbScale * C;
  61. s<< "COFF\n"<<V.rows()<<" "<<F.rows()<<" 0\n";
  62. for (unsigned i=0; i< V.rows(); i++)
  63. {
  64. s <<V.row(i).format(IOFormat(FullPrecision,DontAlignCols," "," ","","",""," "));
  65. s << unsigned(RGB_Array(i,0)) << " " << unsigned(RGB_Array(i,1)) << " " << unsigned(RGB_Array(i,2)) << " 255\n";
  66. }
  67. s<<(F.array()).format(IOFormat(FullPrecision,DontAlignCols," ","\n","3 ","","","\n"));
  68. return true;
  69. }
  70. #ifdef IGL_STATIC_LIBRARY
  71. // Explicit template instantiation
  72. // generated by autoexplicit.sh
  73. 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::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&);
  74. // generated by autoexplicit.sh
  75. 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&);
  76. // generated by autoexplicit.sh
  77. 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&);
  78. 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&);
  79. 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&);
  80. 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&);
  81. 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&);
  82. 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&);
  83. 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&);
  84. 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&);
  85. #endif