ObjFileParser.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #ifndef OBJ_FILEPARSER_H_INC
  34. #define OBJ_FILEPARSER_H_INC
  35. #include "ObjFileData.h"
  36. #include <assimp/IOStreamBuffer.h>
  37. #include <assimp/material.h>
  38. #include <assimp/mesh.h>
  39. #include <assimp/vector2.h>
  40. #include <assimp/vector3.h>
  41. #include <map>
  42. #include <memory>
  43. #include <string>
  44. #include <vector>
  45. namespace Assimp {
  46. // Forward declarations
  47. class ObjFileImporter;
  48. class IOSystem;
  49. class ProgressHandler;
  50. // ------------------------------------------------------------------------------------------------
  51. /// \class ObjFileParser
  52. /// \brief Parser for a obj waveform file
  53. // ------------------------------------------------------------------------------------------------
  54. class ASSIMP_API ObjFileParser {
  55. public:
  56. static constexpr size_t Buffersize = 4096;
  57. using DataArray = std::vector<char>;
  58. using DataArrayIt = std::vector<char>::iterator;
  59. using ConstDataArrayIt = std::vector<char>::const_iterator;
  60. /// @brief The default constructor.
  61. ObjFileParser();
  62. /// @brief Constructor with data array.
  63. ObjFileParser(IOStreamBuffer<char> &streamBuffer, const std::string &modelName, IOSystem *io, ProgressHandler *progress, const std::string &originalObjFileName);
  64. /// @brief Destructor
  65. ~ObjFileParser() = default;
  66. /// @brief If you want to load in-core data.
  67. void setBuffer(std::vector<char> &buffer);
  68. /// @brief Model getter.
  69. ObjFile::Model *GetModel() const;
  70. ObjFileParser(const ObjFileParser&) = delete;
  71. ObjFileParser &operator=(const ObjFileParser& ) = delete;
  72. protected:
  73. /// Parse the loaded file
  74. void parseFile(IOStreamBuffer<char> &streamBuffer);
  75. /// Method to copy the new delimited word in the current line.
  76. void copyNextWord(char *pBuffer, size_t length);
  77. /// Method to copy the new line.
  78. // void copyNextLine(char *pBuffer, size_t length);
  79. /// Get the number of components in a line.
  80. size_t getNumComponentsInDataDefinition();
  81. /// Stores the vector
  82. size_t getTexCoordVector(std::vector<aiVector3D> &point3d_array);
  83. /// Stores the following 3d vector.
  84. void getVector3(std::vector<aiVector3D> &point3d_array);
  85. /// Stores the following homogeneous vector as a 3D vector
  86. void getHomogeneousVector3(std::vector<aiVector3D> &point3d_array);
  87. /// Stores the following two 3d vectors on the line.
  88. void getTwoVectors3(std::vector<aiVector3D> &point3d_array_a, std::vector<aiVector3D> &point3d_array_b);
  89. /// Stores the following 3d vector.
  90. void getVector2(std::vector<aiVector2D> &point2d_array);
  91. /// Stores the following face.
  92. void getFace(aiPrimitiveType type);
  93. /// Reads the material description.
  94. void getMaterialDesc();
  95. /// Gets a comment.
  96. void getComment();
  97. /// Gets a a material library.
  98. void getMaterialLib();
  99. /// Creates a new material.
  100. void getNewMaterial();
  101. /// Gets the group name from file.
  102. void getGroupName();
  103. /// Gets the group number from file.
  104. void getGroupNumber();
  105. /// Gets the group number and resolution from file.
  106. void getGroupNumberAndResolution();
  107. /// Returns the index of the material. Is -1 if not material was found.
  108. int getMaterialIndex(const std::string &strMaterialName);
  109. /// Parse object name
  110. void getObjectName();
  111. /// Creates a new object.
  112. void createObject(const std::string &strObjectName);
  113. /// Creates a new mesh.
  114. void createMesh(const std::string &meshName);
  115. /// Returns true, if a new mesh instance must be created.
  116. bool needsNewMesh(const std::string &rMaterialName);
  117. /// Error report in token
  118. void reportErrorTokenInFace();
  119. private:
  120. /// Default material name
  121. static constexpr const char DEFAULT_MATERIAL[] = AI_DEFAULT_MATERIAL_NAME;
  122. //! Iterator to current position in buffer
  123. DataArrayIt m_DataIt;
  124. //! Iterator to end position of buffer
  125. DataArrayIt m_DataItEnd;
  126. //! Pointer to model instance
  127. std::unique_ptr<ObjFile::Model> m_pModel;
  128. //! Current line (for debugging)
  129. unsigned int m_uiLine;
  130. //! Helper buffer
  131. char m_buffer[Buffersize];
  132. const char *mEnd;
  133. /// Pointer to IO system instance.
  134. IOSystem *m_pIO;
  135. //! Pointer to progress handler
  136. ProgressHandler *m_progress;
  137. /// Path to the current model, name of the obj file where the buffer comes from
  138. const std::string m_originalObjFileName;
  139. };
  140. } // Namespace Assimp
  141. #endif