readOBJ.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. // Just V and F
  49. template <typename Scalar, typename Index>
  50. IGL_INLINE bool readOBJ(
  51. const std::string obj_file_name,
  52. std::vector<std::vector<Scalar > > & V,
  53. std::vector<std::vector<Index > > & F);
  54. #ifndef IGL_NO_EIGEN
  55. //! Read a mesh from an ascii obj file
  56. // Inputs:
  57. // str path to .obj file
  58. // Outputs:
  59. // V eigen matrix #V by 3
  60. // F eigen matrix #F by 3
  61. //
  62. // KNOWN BUG: This only knows how to read *triangle* meshes. It will probably
  63. // crash or give garbage on anything else.
  64. //
  65. // KNOWN BUG: This only knows how to face lines without normal or texture
  66. // indices. It will probably crash or give garbage on anything else.
  67. //
  68. // KNOWN BUG: The order of the attributes is different than the vector
  69. // version above
  70. template <typename DerivedV, typename DerivedF, typename DerivedT>
  71. IGL_INLINE bool readOBJ(
  72. const std::string str,
  73. Eigen::PlainObjectBase<DerivedV>& V,
  74. Eigen::PlainObjectBase<DerivedF>& F,
  75. Eigen::PlainObjectBase<DerivedV>& CN,
  76. Eigen::PlainObjectBase<DerivedF>& FN,
  77. Eigen::PlainObjectBase<DerivedT>& TC,
  78. Eigen::PlainObjectBase<DerivedF>& FTC);
  79. //! Read a poly mesh from an ascii obj file
  80. // Inputs:
  81. // str path to .obj file
  82. // Outputs:
  83. // V eigen matrix #V by 3
  84. // POLYF vector of vector with face indices
  85. //
  86. //
  87. // KNOWN BUG: This only knows how to face lines without normal or texture
  88. // indices. It will probably crash or give garbage on anything else.
  89. //
  90. // KNOWN BUG: The order of the attributes is different than the vector
  91. // version above
  92. template <
  93. typename DerivedV,
  94. typename DerivedF,
  95. typename DerivedT,
  96. typename Index>
  97. IGL_INLINE bool readOBJPoly(
  98. const std::string str,
  99. Eigen::PlainObjectBase<DerivedV>& V,
  100. std::vector<std::vector<Index> >& F,
  101. Eigen::PlainObjectBase<DerivedV>& CN,
  102. Eigen::PlainObjectBase<DerivedF>& FN,
  103. Eigen::PlainObjectBase<DerivedT>& TC,
  104. Eigen::PlainObjectBase<DerivedF>& FTC);
  105. //! Read a mesh from an ascii obj file
  106. // Inputs:
  107. // str path to .obj file
  108. // Outputs:
  109. // V eigen matrix #V by 3
  110. // F eigen matrix #F by 3
  111. //
  112. // KNOWN BUG: This only knows how to read *triangle* meshes. It will probably
  113. // crash or give garbage on anything else.
  114. //
  115. // KNOWN BUG: This only knows how to face lines without normal or texture
  116. // indices. It will probably crash or give garbage on anything else.
  117. template <typename DerivedV, typename DerivedF>
  118. IGL_INLINE bool readOBJ(
  119. const std::string str,
  120. Eigen::PlainObjectBase<DerivedV>& V,
  121. Eigen::PlainObjectBase<DerivedF>& F);
  122. #endif
  123. }
  124. #ifndef IGL_STATIC_LIBRARY
  125. # include "readOBJ.cpp"
  126. #endif
  127. #endif