writeOBJ.cpp 7.1 KB

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