writeOBJ.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "writeOBJ.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cstdio>
  5. template <typename DerivedV, typename DerivedF>
  6. IGL_INLINE bool igl::writeOBJ(
  7. const std::string str,
  8. const Eigen::PlainObjectBase<DerivedV>& V,
  9. const Eigen::PlainObjectBase<DerivedF>& F)
  10. {
  11. std::ofstream s(str.c_str());
  12. if(!s.is_open())
  13. {
  14. fprintf(stderr,"IOError: writeOBJ() could not open %s\n",str.c_str());
  15. return false;
  16. }
  17. for(int i=0;i<V.rows();++i)
  18. s << "v " << V(i,0) << " " << V(i,1) << " " << V(i,2) << std::endl;
  19. for(int i=0;i<F.rows();++i)
  20. s << "f " << F(i,0)+1 << " " << F(i,1)+1 << " " << F(i,2)+1 << std::endl;
  21. s.close();
  22. return true;
  23. }
  24. template <typename DerivedV, typename DerivedF, typename DerivedT>
  25. IGL_INLINE bool igl::writeOBJ(
  26. const std::string str,
  27. const Eigen::PlainObjectBase<DerivedV>& V,
  28. const Eigen::PlainObjectBase<DerivedF>& F,
  29. const Eigen::PlainObjectBase<DerivedV>& CN,
  30. const Eigen::PlainObjectBase<DerivedF>& FN,
  31. const Eigen::PlainObjectBase<DerivedT>& TC,
  32. const Eigen::PlainObjectBase<DerivedF>& FTC)
  33. {
  34. FILE * obj_file = fopen(str.c_str(),"w");
  35. if(NULL==obj_file)
  36. {
  37. printf("IOError: %s could not be opened for writing...",str.c_str());
  38. return false;
  39. }
  40. // Loop over V
  41. for(unsigned i = 0;i<V.rows();i++)
  42. {
  43. fprintf(obj_file,"v %0.15g %0.15g %0.15g\n",
  44. V(i,0),
  45. V(i,1),
  46. V(i,2)
  47. );
  48. }
  49. bool write_N = CN.rows() >0;
  50. if(write_N)
  51. {
  52. for(unsigned i = 0;i<CN.rows();i++)
  53. {
  54. fprintf(obj_file,"v %0.15g %0.15g %0.15g\n",
  55. CN(i,0),
  56. CN(i,1),
  57. CN(i,2)
  58. );
  59. }
  60. fprintf(obj_file,"\n");
  61. }
  62. bool write_texture_coords = TC.rows() >0;
  63. if(write_texture_coords)
  64. {
  65. for(unsigned i = 0;i<TC.rows();i++)
  66. {
  67. fprintf(obj_file, "vt %0.15g %0.15g\n",TC(i,0),1-TC(i,1));
  68. }
  69. fprintf(obj_file,"\n");
  70. }
  71. // loop over F
  72. for(unsigned i = 0;i<F.rows();++i)
  73. {
  74. fprintf(obj_file,"f");
  75. for(unsigned j = 0; j<F.cols();++j)
  76. {
  77. // OBJ is 1-indexed
  78. fprintf(obj_file," %u",F(i,j)+1);
  79. if(write_texture_coords)
  80. fprintf(obj_file,"/%u",FTC(i,j)+1);
  81. if(write_N)
  82. if (write_texture_coords)
  83. fprintf(obj_file,"/%u",FN(i,j)+1);
  84. else
  85. fprintf(obj_file,"//%u",FN(i,j)+1);
  86. }
  87. fprintf(obj_file,"\n");
  88. }
  89. fclose(obj_file);
  90. return true;
  91. }
  92. #ifndef IGL_HEADER_ONLY
  93. // Explicit template specialization
  94. // generated by autoexplicit.sh
  95. template bool igl::writeOBJ<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&);
  96. #endif