ObjFileParser.h 5.2 KB

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