writeOBJ.cpp 6.0 KB

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