writeOBJ.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //
  2. // IGL Lib - Simple C++ mesh library
  3. //
  4. // Copyright 2011, Daniele Panozzo. All rights reserved.
  5. // History:
  6. // return type changed from void to bool Alec 20 Sept 2011
  7. #ifndef IGL_WRITEOBJ_H
  8. #define IGL_WRITEOBJ_H
  9. #include <Eigen/Core>
  10. #include <string>
  11. namespace igl
  12. {
  13. // Write a mesh in an ascii obj file
  14. // Inputs:
  15. // str path to outputfile
  16. // V eigen double matrix #V by 3 (mesh vertices)
  17. // F eigen int matrix #F by 3 (mesh indices)
  18. // Returns true on success, false on error
  19. inline bool writeOBJ(const std::string str, const Eigen::MatrixXd& V, const Eigen::MatrixXi& F);
  20. }
  21. // Implementation
  22. #include <iostream>
  23. #include <fstream>
  24. #include <cstdio>
  25. inline bool igl::writeOBJ(const std::string str, const Eigen::MatrixXd& V, const Eigen::MatrixXi& F)
  26. {
  27. std::ofstream s(str.c_str());
  28. if(!s.is_open())
  29. {
  30. fprintf(stderr,"IOError: writeOBJ() could not open %s\n",str.c_str());
  31. return false;
  32. }
  33. for(int i=0;i<V.rows();++i)
  34. s << "v " << V(i,0) << " " << V(i,1) << " " << V(i,2) << std::endl;
  35. for(int i=0;i<F.rows();++i)
  36. s << "f " << F(i,0)+1 << " " << F(i,1)+1 << " " << F(i,2)+1 << std::endl;
  37. s.close();
  38. return true;
  39. }
  40. #endif