readOBJ.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  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. // History:
  12. // return type changed from void to bool Alec 18 Sept 2011
  13. // added pure vector of vectors version that has much more support Alec 31 Oct
  14. // 2011
  15. #ifndef IGL_NO_EIGEN
  16. # include <Eigen/Core>
  17. #endif
  18. #include <string>
  19. #include <vector>
  20. #include <cstdio>
  21. namespace igl
  22. {
  23. /// Read a mesh from an ascii obj file, filling in vertex positions, normals
  24. /// and texture coordinates. Mesh may have faces of any number of degree
  25. ///
  26. /// @tparam Scalar type for positions and vectors (will be read as double and cast
  27. /// to Scalar)
  28. /// @tparam Index type for indices (will be read as int and cast to Index)
  29. /// @param[in] str path to .obj file
  30. /// @param[out] V double matrix of vertex positions #V by 3
  31. /// @param[out] TC double matrix of texture coordinats #TC by 2
  32. /// @param[out] N double matrix of corner normals #N by 3
  33. /// @param[out] F #F list of face indices into vertex positions
  34. /// @param[out] FTC #F list of face indices into vertex texture coordinates
  35. /// @param[out] FN #F list of face indices into vertex normals
  36. // @param[out] FM #tuple list containing (vertex index, normal index, texture coordinates index, material)
  37. /// @return true on success, false on errors
  38. template <typename Scalar, typename Index>
  39. IGL_INLINE bool readOBJ(
  40. const std::string obj_file_name,
  41. std::vector<std::vector<Scalar > > & V,
  42. std::vector<std::vector<Scalar > > & TC,
  43. std::vector<std::vector<Scalar > > & N,
  44. std::vector<std::vector<Index > > & F,
  45. std::vector<std::vector<Index > > & FTC,
  46. std::vector<std::vector<Index > > & FN,
  47. std::vector<std::tuple<std::string, Index, Index >> &FM
  48. );
  49. /// \overload
  50. template <typename Scalar, typename Index>
  51. IGL_INLINE bool readOBJ(
  52. const std::string obj_file_name,
  53. std::vector<std::vector<Scalar > > & V,
  54. std::vector<std::vector<Scalar > > & TC,
  55. std::vector<std::vector<Scalar > > & N,
  56. std::vector<std::vector<Index > > & F,
  57. std::vector<std::vector<Index > > & FTC,
  58. std::vector<std::vector<Index > > & FN);
  59. /// @param[in,out] obj_file pointer to already opened .obj file (will be
  60. /// closed)
  61. template <typename Scalar, typename Index>
  62. IGL_INLINE bool readOBJ(
  63. FILE * obj_file,
  64. std::vector<std::vector<Scalar > > & V,
  65. std::vector<std::vector<Scalar > > & TC,
  66. std::vector<std::vector<Scalar > > & N,
  67. std::vector<std::vector<Index > > & F,
  68. std::vector<std::vector<Index > > & FTC,
  69. std::vector<std::vector<Index > > & FN,
  70. std::vector<std::tuple<std::string, Index, Index >> &FM);
  71. /// \overload
  72. template <typename Scalar, typename Index>
  73. IGL_INLINE bool readOBJ(
  74. const std::string obj_file_name,
  75. std::vector<std::vector<Scalar > > & V,
  76. std::vector<std::vector<Index > > & F);
  77. /// \overload
  78. /// \brief Eigen Wrappers. These will return true only if the data is perfectly
  79. /// "rectangular": All faces are the same degree, all have the same number of
  80. /// textures/normals etc.
  81. template <
  82. typename DerivedV,
  83. typename DerivedTC,
  84. typename DerivedCN,
  85. typename DerivedF,
  86. typename DerivedFTC,
  87. typename DerivedFN>
  88. IGL_INLINE bool readOBJ(
  89. const std::string str,
  90. Eigen::PlainObjectBase<DerivedV>& V,
  91. Eigen::PlainObjectBase<DerivedTC>& TC,
  92. Eigen::PlainObjectBase<DerivedCN>& CN,
  93. Eigen::PlainObjectBase<DerivedF>& F,
  94. Eigen::PlainObjectBase<DerivedFTC>& FTC,
  95. Eigen::PlainObjectBase<DerivedFN>& FN);
  96. /// \overload
  97. template <typename DerivedV, typename DerivedF>
  98. IGL_INLINE bool readOBJ(
  99. const std::string str,
  100. Eigen::PlainObjectBase<DerivedV>& V,
  101. Eigen::PlainObjectBase<DerivedF>& F);
  102. /// \overload
  103. /// \brief Polygon mesh version.
  104. /// @param[out] I #I vectorized list of polygon corner indices into rows of some matrix V
  105. /// @param[out] C #P+1 list of cumulative polygon sizes so that C(i+1)-C(i) = size of
  106. /// the ith polygon, and so I(C(i)) through I(C(i+1)-1) are the indices of
  107. /// the ith polygon
  108. template <typename DerivedV, typename DerivedI, typename DerivedC>
  109. IGL_INLINE bool readOBJ(
  110. const std::string str,
  111. Eigen::PlainObjectBase<DerivedV>& V,
  112. Eigen::PlainObjectBase<DerivedI>& I,
  113. Eigen::PlainObjectBase<DerivedC>& C);
  114. }
  115. #ifndef IGL_STATIC_LIBRARY
  116. # include "readOBJ.cpp"
  117. #endif
  118. #endif