writeOBJ.cpp 4.9 KB

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