2
0

ObjFileParser.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <assimp/IOStreamBuffer.h>
  36. #include <assimp/mesh.h>
  37. #include <assimp/vector2.h>
  38. #include <assimp/vector3.h>
  39. #include <map>
  40. #include <memory>
  41. #include <string>
  42. #include <vector>
  43. namespace Assimp {
  44. namespace ObjFile {
  45. struct Model;
  46. struct Object;
  47. struct Material;
  48. struct Point3;
  49. struct Point2;
  50. } // namespace ObjFile
  51. class ObjFileImporter;
  52. class IOSystem;
  53. class ProgressHandler;
  54. /// \class ObjFileParser
  55. /// \brief Parser for a obj waveform file
  56. class ASSIMP_API ObjFileParser {
  57. public:
  58. static const size_t Buffersize = 4096;
  59. typedef std::vector<char> DataArray;
  60. typedef std::vector<char>::iterator DataArrayIt;
  61. typedef std::vector<char>::const_iterator ConstDataArrayIt;
  62. public:
  63. /// @brief The default constructor.
  64. ObjFileParser();
  65. /// @brief Constructor with data array.
  66. ObjFileParser(IOStreamBuffer<char> &streamBuffer, const std::string &modelName, IOSystem *io, ProgressHandler *progress, const std::string &originalObjFileName);
  67. /// @brief Destructor
  68. ~ObjFileParser();
  69. /// @brief If you want to load in-core data.
  70. void setBuffer(std::vector<char> &buffer);
  71. /// @brief Model getter.
  72. ObjFile::Model *GetModel() const;
  73. ObjFileParser(const ObjFileParser&) = delete;
  74. ObjFileParser &operator=(const ObjFileParser& ) = delete;
  75. protected:
  76. /// Parse the loaded file
  77. void parseFile(IOStreamBuffer<char> &streamBuffer);
  78. /// Method to copy the new delimited word in the current line.
  79. void copyNextWord(char *pBuffer, size_t length);
  80. /// Method to copy the new line.
  81. // void copyNextLine(char *pBuffer, size_t length);
  82. /// Get the number of components in a line.
  83. size_t getNumComponentsInDataDefinition();
  84. /// Stores the vector
  85. size_t getTexCoordVector(std::vector<aiVector3D> &point3d_array);
  86. /// Stores the following 3d vector.
  87. void getVector3(std::vector<aiVector3D> &point3d_array);
  88. /// Stores the following homogeneous vector as a 3D vector
  89. void getHomogeneousVector3(std::vector<aiVector3D> &point3d_array);
  90. /// Stores the following two 3d vectors on the line.
  91. void getTwoVectors3(std::vector<aiVector3D> &point3d_array_a, std::vector<aiVector3D> &point3d_array_b);
  92. /// Stores the following 3d vector.
  93. void getVector2(std::vector<aiVector2D> &point2d_array);
  94. /// Stores the following face.
  95. void getFace(aiPrimitiveType type);
  96. /// Reads the material description.
  97. void getMaterialDesc();
  98. /// Gets a comment.
  99. void getComment();
  100. /// Gets a a material library.
  101. void getMaterialLib();
  102. /// Creates a new material.
  103. void getNewMaterial();
  104. /// Gets the group name from file.
  105. void getGroupName();
  106. /// Gets the group number from file.
  107. void getGroupNumber();
  108. /// Gets the group number and resolution from file.
  109. void getGroupNumberAndResolution();
  110. /// Returns the index of the material. Is -1 if not material was found.
  111. int getMaterialIndex(const std::string &strMaterialName);
  112. /// Parse object name
  113. void getObjectName();
  114. /// Creates a new object.
  115. void createObject(const std::string &strObjectName);
  116. /// Creates a new mesh.
  117. void createMesh(const std::string &meshName);
  118. /// Returns true, if a new mesh instance must be created.
  119. bool needsNewMesh(const std::string &rMaterialName);
  120. /// Error report in token
  121. void reportErrorTokenInFace();
  122. private:
  123. // Copy and assignment constructor should be private
  124. // because the class contains pointer to allocated memory
  125. /// Default material name
  126. static const std::string DEFAULT_MATERIAL;
  127. //! Iterator to current position in buffer
  128. DataArrayIt m_DataIt;
  129. //! Iterator to end position of buffer
  130. DataArrayIt m_DataItEnd;
  131. //! Pointer to model instance
  132. std::unique_ptr<ObjFile::Model> m_pModel;
  133. //! Current line (for debugging)
  134. unsigned int m_uiLine;
  135. //! Helper buffer
  136. char m_buffer[Buffersize];
  137. /// Pointer to IO system instance.
  138. IOSystem *m_pIO;
  139. //! Pointer to progress handler
  140. ProgressHandler *m_progress;
  141. /// Path to the current model, name of the obj file where the buffer comes from
  142. const std::string m_originalObjFileName;
  143. };
  144. } // Namespace Assimp
  145. #endif