readOFF.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 18 Sept 2011
  7. #ifndef IGL_READOFF_H
  8. #define IGL_READOFF_H
  9. #include <Eigen/Core>
  10. #include <string>
  11. namespace igl
  12. {
  13. // read mesh from a ascii off file
  14. // Inputs:
  15. // str path to .off file
  16. // Outputs:
  17. // V eigen double matrix #V by 3
  18. // F eigen int matrix #F by 3
  19. bool readOFF (const std::string meshfile, Eigen::MatrixXd& V, Eigen::MatrixXi& F);
  20. }
  21. // Implementation
  22. bool igl::readOFF (const std::string meshfile, Eigen::MatrixXd& V, Eigen::MatrixXi& F)
  23. {
  24. int vnum, fnum;
  25. FILE *fp = fopen (meshfile.c_str(), "r");
  26. if (!fp)
  27. {
  28. fprintf (stderr, "readOFF(): could not open file %s", meshfile.c_str());
  29. return false;
  30. }
  31. fscanf (fp, "OFF\n%d %d 0\n", &vnum, &fnum);
  32. V = Eigen::MatrixXd (vnum, 3);
  33. F = Eigen::MatrixXi (fnum, 3);
  34. for (unsigned i = 0; i < V.rows(); i++)
  35. fscanf (fp, "%lf %lf %lf\n", &V(i,0), &V(i,1), &V(i,2));
  36. for (unsigned i = 0; i < F.rows(); i++)
  37. fscanf (fp, "3 %d %d %d\n", &F(i,0), &F(i,1), &F(i,2));
  38. fclose (fp);
  39. return true;
  40. }
  41. #endif