Coverage Report

Created: 2025-08-26 06:41

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