writeOBJ.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include <cassert>
  15. template <
  16. typename DerivedV,
  17. typename DerivedF,
  18. typename DerivedCN,
  19. typename DerivedFN,
  20. typename DerivedTC,
  21. typename DerivedFTC>
  22. IGL_INLINE bool igl::writeOBJ(
  23. const std::string str,
  24. const Eigen::MatrixBase<DerivedV>& V,
  25. const Eigen::MatrixBase<DerivedF>& F,
  26. const Eigen::MatrixBase<DerivedCN>& CN,
  27. const Eigen::MatrixBase<DerivedFN>& FN,
  28. const Eigen::MatrixBase<DerivedTC>& TC,
  29. const Eigen::MatrixBase<DerivedFTC>& FTC)
  30. {
  31. FILE * obj_file = fopen(str.c_str(),"w");
  32. if(NULL==obj_file)
  33. {
  34. printf("IOError: %s could not be opened for writing...",str.c_str());
  35. return false;
  36. }
  37. // Loop over V
  38. for(int i = 0;i<(int)V.rows();i++)
  39. {
  40. fprintf(obj_file,"v");
  41. for(int j = 0;j<(int)V.cols();++j)
  42. {
  43. fprintf(obj_file," %0.17g", V(i,j));
  44. }
  45. fprintf(obj_file,"\n");
  46. }
  47. bool write_N = CN.rows() >0;
  48. if(write_N)
  49. {
  50. for(int i = 0;i<(int)CN.rows();i++)
  51. {
  52. fprintf(obj_file,"vn %0.17g %0.17g %0.17g\n",
  53. CN(i,0),
  54. CN(i,1),
  55. CN(i,2)
  56. );
  57. }
  58. fprintf(obj_file,"\n");
  59. }
  60. bool write_texture_coords = TC.rows() >0;
  61. if(write_texture_coords)
  62. {
  63. for(int i = 0;i<(int)TC.rows();i++)
  64. {
  65. fprintf(obj_file, "vt %0.17g %0.17g\n",TC(i,0),TC(i,1));
  66. }
  67. fprintf(obj_file,"\n");
  68. }
  69. // loop over F
  70. for(int i = 0;i<(int)F.rows();++i)
  71. {
  72. fprintf(obj_file,"f");
  73. for(int j = 0; j<(int)F.cols();++j)
  74. {
  75. // OBJ is 1-indexed
  76. fprintf(obj_file," %u",F(i,j)+1);
  77. if(write_texture_coords)
  78. fprintf(obj_file,"/%u",FTC(i,j)+1);
  79. if(write_N)
  80. {
  81. if (write_texture_coords)
  82. fprintf(obj_file,"/%u",FN(i,j)+1);
  83. else
  84. fprintf(obj_file,"//%u",FN(i,j)+1);
  85. }
  86. }
  87. fprintf(obj_file,"\n");
  88. }
  89. fclose(obj_file);
  90. return true;
  91. }
  92. template <typename DerivedV, typename DerivedF>
  93. IGL_INLINE bool igl::writeOBJ(
  94. const std::string str,
  95. const Eigen::MatrixBase<DerivedV>& V,
  96. const Eigen::MatrixBase<DerivedF>& F)
  97. {
  98. using namespace std;
  99. using namespace Eigen;
  100. assert(V.cols() == 3 && "V should have 3 columns");
  101. ofstream s(str);
  102. if(!s.is_open())
  103. {
  104. fprintf(stderr,"IOError: writeOBJ() could not open %s\n",str.c_str());
  105. return false;
  106. }
  107. s<<
  108. V.format(IOFormat(FullPrecision,DontAlignCols," ","\n","v ","","","\n"))<<
  109. (F.array()+1).format(IOFormat(FullPrecision,DontAlignCols," ","\n","f ","","","\n"));
  110. return true;
  111. }
  112. template <typename DerivedV, typename T>
  113. IGL_INLINE bool igl::writeOBJ(
  114. const std::string &str,
  115. const Eigen::MatrixBase<DerivedV>& V,
  116. const std::vector<std::vector<T> >& F)
  117. {
  118. using namespace std;
  119. using namespace Eigen;
  120. assert(V.cols() == 3 && "V should have 3 columns");
  121. ofstream s(str);
  122. if(!s.is_open())
  123. {
  124. fprintf(stderr,"IOError: writeOBJ() could not open %s\n",str.c_str());
  125. return false;
  126. }
  127. s<<V.format(IOFormat(FullPrecision,DontAlignCols," ","\n","v ","","","\n"));
  128. for(const auto& face : F)
  129. {
  130. int face_size = face.size();
  131. assert(face_size != 0);
  132. s << (face_size == 2 ? "l" : "f");
  133. for(const auto& vi : face)
  134. {
  135. s<<" "<<vi;
  136. }
  137. s<<"\n";
  138. }
  139. return true;
  140. }
  141. #ifdef IGL_STATIC_LIBRARY
  142. // Explicit template instantiation
  143. // generated by autoexplicit.sh
  144. template bool igl::writeOBJ<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::MatrixBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&);
  145. // generated by autoexplicit.sh
  146. template bool igl::writeOBJ<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::MatrixBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&);
  147. // generated by autoexplicit.sh
  148. template bool igl::writeOBJ<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::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&);
  149. // generated by autoexplicit.sh
  150. template bool igl::writeOBJ<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::MatrixBase<Eigen::Matrix<double, 8, 3, 0, 8, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, 12, 3, 0, 12, 3> > const&);
  151. template bool igl::writeOBJ<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  152. template bool igl::writeOBJ<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  153. 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>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  154. 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>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, 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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  155. 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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  156. #endif