writeOBJ.cpp 5.9 KB

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