writeOBJ.cpp 5.8 KB

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