readOBJ.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // 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. namespace igl
  21. {
  22. // Read a mesh from an ascii obj file, filling in vertex positions, normals
  23. // and texture coordinates. Mesh may have faces of any number of degree
  24. //
  25. // Templates:
  26. // Scalar type for positions and vectors (will be read as double and cast
  27. // to Scalar)
  28. // Index type for indices (will be read as int and cast to Index)
  29. // Inputs:
  30. // str path to .obj file
  31. // Outputs:
  32. // V double matrix of vertex positions #V by 3
  33. // F #F list of face indices into vertex positions
  34. // TC double matrix of texture coordinats #TC by 2
  35. // FTC #F list of face indices into vertex texture coordinates
  36. // N double matrix of corner normals #N by 3
  37. // FN #F list of face indices into vertex normals
  38. // Returns true on success, false on errors
  39. template <typename Scalar, typename Index>
  40. IGL_INLINE bool readOBJ(
  41. const std::string obj_file_name,
  42. std::vector<std::vector<Scalar > > & V,
  43. std::vector<std::vector<Scalar > > & TC,
  44. std::vector<std::vector<Scalar > > & N,
  45. std::vector<std::vector<Index > > & F,
  46. std::vector<std::vector<Index > > & FTC,
  47. std::vector<std::vector<Index > > & FN);
  48. #ifndef IGL_NO_EIGEN
  49. //! Read a mesh from an ascii obj file
  50. // Inputs:
  51. // str path to .obj file
  52. // Outputs:
  53. // V eigen matrix #V by 3
  54. // F eigen matrix #F by 3
  55. //
  56. // KNOWN BUG: This only knows how to read *triangle* meshes. It will probably
  57. // crash or give garbage on anything else.
  58. //
  59. // KNOWN BUG: This only knows how to face lines without normal or texture
  60. // indices. It will probably crash or give garbage on anything else.
  61. //
  62. // KNOWN BUG: The order of the attributes is different than the vector
  63. // version above
  64. template <typename DerivedV, typename DerivedF, typename DerivedT>
  65. IGL_INLINE bool readOBJ(
  66. const std::string str,
  67. Eigen::PlainObjectBase<DerivedV>& V,
  68. Eigen::PlainObjectBase<DerivedF>& F,
  69. Eigen::PlainObjectBase<DerivedV>& CN,
  70. Eigen::PlainObjectBase<DerivedF>& FN,
  71. Eigen::PlainObjectBase<DerivedT>& TC,
  72. Eigen::PlainObjectBase<DerivedF>& FTC);
  73. //! Read a poly mesh from an ascii obj file
  74. // Inputs:
  75. // str path to .obj file
  76. // Outputs:
  77. // V eigen matrix #V by 3
  78. // POLYF vector of vector with face indices
  79. //
  80. //
  81. // KNOWN BUG: This only knows how to face lines without normal or texture
  82. // indices. It will probably crash or give garbage on anything else.
  83. //
  84. // KNOWN BUG: The order of the attributes is different than the vector
  85. // version above
  86. template <
  87. typename DerivedV,
  88. typename DerivedF,
  89. typename DerivedT,
  90. typename Index>
  91. IGL_INLINE bool readOBJPoly(
  92. const std::string str,
  93. Eigen::PlainObjectBase<DerivedV>& V,
  94. std::vector<std::vector<Index> >& F,
  95. Eigen::PlainObjectBase<DerivedV>& CN,
  96. Eigen::PlainObjectBase<DerivedF>& FN,
  97. Eigen::PlainObjectBase<DerivedT>& TC,
  98. Eigen::PlainObjectBase<DerivedF>& FTC);
  99. //! Read a mesh from an ascii obj file
  100. // Inputs:
  101. // str path to .obj file
  102. // Outputs:
  103. // V eigen matrix #V by 3
  104. // F eigen matrix #F by 3
  105. //
  106. // KNOWN BUG: This only knows how to read *triangle* meshes. It will probably
  107. // crash or give garbage on anything else.
  108. //
  109. // KNOWN BUG: This only knows how to face lines without normal or texture
  110. // indices. It will probably crash or give garbage on anything else.
  111. template <typename DerivedV, typename DerivedF>
  112. IGL_INLINE bool readOBJ(
  113. const std::string str,
  114. Eigen::PlainObjectBase<DerivedV>& V,
  115. Eigen::PlainObjectBase<DerivedF>& F);
  116. #endif
  117. }
  118. #ifdef IGL_HEADER_ONLY
  119. # include "readOBJ.cpp"
  120. #endif
  121. #endif