writeOBJ.cpp 7.0 KB

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