Coverage Report

Created: 2026-11-12 07:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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 <memory>
52
#include <string>
53
#include <string_view>
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
20.0k
    ~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
    // Copying not allowed
86
    ObjFileParser(const ObjFileParser&) = delete;
87
    ObjFileParser &operator=(const ObjFileParser& ) = delete;
88
89
protected:
90
    /// Parse the loaded file
91
    void parseFile(IOStreamBuffer<char> &streamBuffer);
92
    /// Method to copy the new delimited word in the current line.
93
    void copyNextWord();
94
    /// Get the number of components in a line.
95
    size_t getNumComponentsInDataDefinition();
96
    /// Stores the vector
97
    size_t getTexCoordVector(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
    /// Skip a comment.
111
    void skipComment();
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 skipGroupNumber();
120
    /// Gets the group number and resolution from file.
121
    void skipGroupNumberAndResolution();
122
    /// Reselect an existing group (Wavefront `group` keyword) for late material assignment.
123
    void reselectGroup();
124
    /// Returns the index of the material. Is -1 if not material was found.
125
    int getMaterialIndex(std::string_view strMaterialName);
126
    /// Parse object name
127
    void getObjectName();
128
    /// Creates a new object.
129
    void createObject(std::string_view strObjectName);
130
    /// Creates a new mesh.
131
    void createMesh(std::string_view meshName);
132
    /// Returns true, if a new mesh instance must be created.
133
    bool needsNewMesh(std::string_view rMaterialName);
134
    /// Error report in token
135
    void reportErrorTokenInFace();
136
    /// Parse next token from current position, sets mCurrentToken, mTokenStart, mTokenEnd
137
    void parseNextToken();
138
    /// Parse float directly from current token position (uses mTokenStart)
139
    ai_real parseFloatDirect();
140
    /// Parse int directly from current token position (uses mTokenStart)
141
    int parseIntDirect();
142
143
protected:
144
    static constexpr char DEFAULT_MATERIAL[] = AI_DEFAULT_MATERIAL_NAME;    ///< Default material name
145
    DataArrayIt mDataIt{};                                                  ///< Iterator to current position in buffer
146
    DataArrayIt mDataItEnd{};                                               ///< Iterator to end position of buffer
147
    std::unique_ptr<ObjFile::Model> mModel{};                               ///< Pointer to model instance
148
    bool mReselectMode{ false };                                            ///< When true, trailing `group`/`usemtl` assigns materials to already-built meshes (Viewpoint OBJs).
149
    unsigned int mLine{ 0 };                                                ///< Current line (for debugging)
150
    std::string mBuffer;                                                    ///< Helper buffer (safe) - kept for backward compatibility
151
    const char *mEnd{ nullptr };                                            ///< End of buffer
152
    IOSystem *mIO{ nullptr };                                               ///< Pointer to the IO-system instance.
153
    ProgressHandler *mProgress{ nullptr };                                  ///< Pointer to progress handler
154
    const std::string mOriginalObjFileName{};                               ///< Path to the current model, name of the obj file where the buffer comes from
155
    // --- Zero-copy parsing members ---
156
    /// Current token as string_view (avoids string allocations)
157
    std::string_view mCurrentToken;
158
    const char* mTokenStart{ nullptr };                                     ///< Start pointer of the current token
159
    const char* mTokenEnd{ nullptr };                                       ///< End pointer of the current token
160
};
161
162
} // Namespace Assimp
163
164
#endif