writeOBJ.cpp 4.2 KB

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