readOBJ.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #ifndef IGL_READOBJ_H
  9. #define IGL_READOBJ_H
  10. #include "igl_inline.h"
  11. #include "deprecated.h"
  12. // History:
  13. // return type changed from void to bool Alec 18 Sept 2011
  14. // added pure vector of vectors version that has much more support Alec 31 Oct
  15. // 2011
  16. #ifndef IGL_NO_EIGEN
  17. # include <Eigen/Core>
  18. #endif
  19. #include <string>
  20. #include <vector>
  21. #include <cstdio>
  22. namespace igl
  23. {
  24. // Read a mesh from an ascii obj file, filling in vertex positions, normals
  25. // and texture coordinates. Mesh may have faces of any number of degree
  26. //
  27. // Templates:
  28. // Scalar type for positions and vectors (will be read as double and cast
  29. // to Scalar)
  30. // Index type for indices (will be read as int and cast to Index)
  31. // Inputs:
  32. // str path to .obj file
  33. // Outputs:
  34. // V double matrix of vertex positions #V by 3
  35. // TC double matrix of texture coordinats #TC by 2
  36. // N double matrix of corner normals #N by 3
  37. // F #F list of face indices into vertex positions
  38. // FTC #F list of face indices into vertex texture coordinates
  39. // FN #F list of face indices into vertex normals
  40. // Returns true on success, false on errors
  41. template <typename Scalar, typename Index>
  42. IGL_INLINE bool readOBJ(
  43. const std::string obj_file_name,
  44. std::vector<std::vector<Scalar > > & V,
  45. std::vector<std::vector<Scalar > > & TC,
  46. std::vector<std::vector<Scalar > > & N,
  47. std::vector<std::vector<Index > > & F,
  48. std::vector<std::vector<Index > > & FTC,
  49. std::vector<std::vector<Index > > & FN);
  50. // Inputs:
  51. // obj_file pointer to already opened .obj file
  52. // Outputs:
  53. // obj_file closed file
  54. template <typename Scalar, typename Index>
  55. IGL_INLINE bool readOBJ(
  56. FILE * obj_file,
  57. std::vector<std::vector<Scalar > > & V,
  58. std::vector<std::vector<Scalar > > & TC,
  59. std::vector<std::vector<Scalar > > & N,
  60. std::vector<std::vector<Index > > & F,
  61. std::vector<std::vector<Index > > & FTC,
  62. std::vector<std::vector<Index > > & FN);
  63. // Just V and F
  64. template <typename Scalar, typename Index>
  65. IGL_INLINE bool readOBJ(
  66. const std::string obj_file_name,
  67. std::vector<std::vector<Scalar > > & V,
  68. std::vector<std::vector<Index > > & F);
  69. // Eigen Wrappers. These will return true only if the data is perfectly
  70. // "rectangular": All faces are the same degree, all have the same number of
  71. // textures/normals etc.
  72. template <
  73. typename DerivedV,
  74. typename DerivedTC,
  75. typename DerivedCN,
  76. typename DerivedF,
  77. typename DerivedFTC,
  78. typename DerivedFN>
  79. IGL_INLINE bool readOBJ(
  80. const std::string str,
  81. Eigen::PlainObjectBase<DerivedV>& V,
  82. Eigen::PlainObjectBase<DerivedTC>& TC,
  83. Eigen::PlainObjectBase<DerivedCN>& CN,
  84. Eigen::PlainObjectBase<DerivedF>& F,
  85. Eigen::PlainObjectBase<DerivedFTC>& FTC,
  86. Eigen::PlainObjectBase<DerivedFN>& FN);
  87. template <typename DerivedV, typename DerivedF>
  88. IGL_INLINE bool readOBJ(
  89. const std::string str,
  90. Eigen::PlainObjectBase<DerivedV>& V,
  91. Eigen::PlainObjectBase<DerivedF>& F);
  92. }
  93. #ifndef IGL_STATIC_LIBRARY
  94. # include "readOBJ.cpp"
  95. #endif
  96. #endif