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