Coverage Report

Created: 2025-06-22 07:30

/src/assimp/code/AssetLib/Raw/RawLoader.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
---------------------------------------------------------------------------
3
Open Asset Import Library (assimp)
4
---------------------------------------------------------------------------
5
6
Copyright (c) 2006-2025, assimp team
7
8
All rights reserved.
9
10
Redistribution and use of this software in source and binary forms,
11
with or without modification, are permitted provided that the following
12
conditions are met:
13
14
* Redistributions of source code must retain the above
15
  copyright notice, this list of conditions and the
16
  following disclaimer.
17
18
* Redistributions in binary form must reproduce the above
19
  copyright notice, this list of conditions and the
20
  following disclaimer in the documentation and/or other
21
  materials provided with the distribution.
22
23
* Neither the name of the assimp team, nor the names of its
24
  contributors may be used to endorse or promote products
25
  derived from this software without specific prior
26
  written permission of the assimp team.
27
28
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
---------------------------------------------------------------------------
40
*/
41
42
/** @file  RawLoader.cpp
43
 *  @brief Implementation of the RAW importer class
44
 */
45
46
#ifndef ASSIMP_BUILD_NO_RAW_IMPORTER
47
48
// internal headers
49
#include "RawLoader.h"
50
#include <assimp/ParsingUtils.h>
51
#include <assimp/fast_atof.h>
52
#include <assimp/importerdesc.h>
53
#include <assimp/scene.h>
54
#include <assimp/DefaultLogger.hpp>
55
#include <assimp/IOSystem.hpp>
56
#include <memory>
57
58
namespace Assimp {
59
60
static constexpr aiImporterDesc desc = {
61
    "Raw Importer",
62
    "",
63
    "",
64
    "",
65
    aiImporterFlags_SupportTextFlavour,
66
    0,
67
    0,
68
    0,
69
    0,
70
    "raw"
71
};
72
73
// ------------------------------------------------------------------------------------------------
74
// Returns whether the class can handle the format of the given file.
75
785
bool RAWImporter::CanRead(const std::string &filename, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const {
76
785
    return SimpleExtensionCheck(filename, "raw");
77
785
}
78
79
// ------------------------------------------------------------------------------------------------
80
2.35k
const aiImporterDesc *RAWImporter::GetInfo() const {
81
2.35k
    return &desc;
82
2.35k
}
83
84
// ------------------------------------------------------------------------------------------------
85
// Imports the given file into the given scene structure.
86
void RAWImporter::InternReadFile(const std::string &pFile,
87
0
        aiScene *pScene, IOSystem *pIOHandler) {
88
0
    std::unique_ptr<IOStream> file(pIOHandler->Open(pFile, "rb"));
89
90
    // Check whether we can read from the file
91
0
    if (file == nullptr) {
92
0
        throw DeadlyImportError("Failed to open RAW file ", pFile, ".");
93
0
    }
94
95
    // allocate storage and copy the contents of the file to a memory buffer
96
    // (terminate it with zero)
97
0
    std::vector<char> mBuffer2;
98
0
    TextFileToBuffer(file.get(), mBuffer2);
99
0
    const char *buffer = &mBuffer2[0];
100
101
    // list of groups loaded from the file
102
0
    std::vector<GroupInformation> outGroups(1, GroupInformation("<default>"));
103
0
    std::vector<GroupInformation>::iterator curGroup = outGroups.begin();
104
105
    // now read all lines
106
0
    char line[4096];
107
0
    const char *end = &line[4096];
108
0
    while (GetNextLine(buffer, line)) {
109
        // if the line starts with a non-numeric identifier, it marks
110
        // the beginning of a new group
111
0
        const char *sz = line;
112
0
        SkipSpaces(&sz, end);
113
0
        if (IsLineEnd(*sz)) continue;
114
0
        if (!IsNumeric(*sz)) {
115
0
            const char *sz2 = sz;
116
0
            while (!IsSpaceOrNewLine(*sz2))
117
0
                ++sz2;
118
0
            const unsigned int length = (unsigned int)(sz2 - sz);
119
120
            // find an existing group with this name
121
0
            for (std::vector<GroupInformation>::iterator it = outGroups.begin(), endIt = outGroups.end();
122
0
                    it != endIt; ++it) {
123
0
                if (length == (*it).name.length() && !::strcmp(sz, (*it).name.c_str())) {
124
0
                    curGroup = it;
125
0
                    sz2 = nullptr;
126
0
                    break;
127
0
                }
128
0
            }
129
0
            if (sz2) {
130
0
                outGroups.emplace_back(std::string(sz, length));
131
0
                curGroup = outGroups.end() - 1;
132
0
            }
133
0
        } else {
134
            // there can be maximally 12 floats plus an extra texture file name
135
0
            float data[12];
136
0
            unsigned int num;
137
0
            for (num = 0; num < 12; ++num) {
138
0
                if (!SkipSpaces(&sz, end) || !IsNumeric(*sz)) break;
139
0
                sz = fast_atoreal_move<float>(sz, data[num]);
140
0
            }
141
0
            if (num != 12 && num != 9) {
142
0
                ASSIMP_LOG_ERROR("A line may have either 9 or 12 floats and an optional texture");
143
0
                continue;
144
0
            }
145
146
0
            MeshInformation *output = nullptr;
147
148
0
            const char *sz2 = sz;
149
0
            unsigned int length;
150
0
            if (!IsLineEnd(*sz)) {
151
0
                while (!IsSpaceOrNewLine(*sz2))
152
0
                    ++sz2;
153
0
                length = (unsigned int)(sz2 - sz);
154
0
            } else if (9 == num) {
155
0
                sz = "%default%";
156
0
                length = 9;
157
0
            } else {
158
0
                sz = "";
159
0
                length = 0;
160
0
            }
161
162
            // search in the list of meshes whether we have one with this texture
163
0
            for (auto &mesh : (*curGroup).meshes) {
164
0
                if (length == mesh.name.length() && (length ? !::strcmp(sz, mesh.name.c_str()) : true)) {
165
0
                    output = &mesh;
166
0
                    break;
167
0
                }
168
0
            }
169
            // if we don't have the mesh, create it
170
0
            if (!output) {
171
0
                (*curGroup).meshes.emplace_back(std::string(sz, length));
172
0
                output = &((*curGroup).meshes.back());
173
0
            }
174
0
            if (12 == num) {
175
0
                aiColor4D v(data[0], data[1], data[2], 1.0f);
176
0
                output->colors.push_back(v);
177
0
                output->colors.push_back(v);
178
0
                output->colors.push_back(v);
179
180
0
                output->vertices.emplace_back(data[3], data[4], data[5]);
181
0
                output->vertices.emplace_back(data[6], data[7], data[8]);
182
0
                output->vertices.emplace_back(data[9], data[10], data[11]);
183
0
            } else {
184
0
                output->vertices.emplace_back(data[0], data[1], data[2]);
185
0
                output->vertices.emplace_back(data[3], data[4], data[5]);
186
0
                output->vertices.emplace_back(data[6], data[7], data[8]);
187
0
            }
188
0
        }
189
0
    }
190
191
0
    pScene->mRootNode = new aiNode();
192
0
    pScene->mRootNode->mName.Set("<RawRoot>");
193
194
    // count the number of valid groups
195
    // (meshes can't be empty)
196
0
    for (auto &outGroup : outGroups) {
197
0
        if (!outGroup.meshes.empty()) {
198
0
            ++pScene->mRootNode->mNumChildren;
199
0
            pScene->mNumMeshes += (unsigned int)outGroup.meshes.size();
200
0
        }
201
0
    }
202
203
0
    if (!pScene->mNumMeshes) {
204
0
        throw DeadlyImportError("RAW: No meshes loaded. The file seems to be corrupt or empty.");
205
0
    }
206
207
0
    pScene->mMeshes = new aiMesh *[pScene->mNumMeshes];
208
0
    aiNode **cc;
209
0
    if (1 == pScene->mRootNode->mNumChildren) {
210
0
        cc = &pScene->mRootNode;
211
0
        pScene->mRootNode->mNumChildren = 0;
212
0
    } else {
213
0
        cc = new aiNode *[pScene->mRootNode->mNumChildren];
214
0
        memset(cc, 0, sizeof(aiNode *) * pScene->mRootNode->mNumChildren);
215
0
        pScene->mRootNode->mChildren = cc;
216
0
    }
217
218
0
    pScene->mNumMaterials = pScene->mNumMeshes;
219
0
    aiMaterial **mats = pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials];
220
221
0
    unsigned int meshIdx = 0;
222
0
    for (auto &outGroup : outGroups) {
223
0
        if (outGroup.meshes.empty()) continue;
224
225
0
        aiNode *node;
226
0
        if (pScene->mRootNode->mNumChildren) {
227
0
            node = *cc = new aiNode();
228
0
            node->mParent = pScene->mRootNode;
229
0
        } else
230
0
            node = *cc;
231
0
        node->mName.Set(outGroup.name);
232
233
        // add all meshes
234
0
        node->mNumMeshes = (unsigned int)outGroup.meshes.size();
235
0
        unsigned int *pi = node->mMeshes = new unsigned int[node->mNumMeshes];
236
0
        for (std::vector<MeshInformation>::iterator it2 = outGroup.meshes.begin(),
237
0
                                                    end2 = outGroup.meshes.end();
238
0
                it2 != end2; ++it2) {
239
0
            ai_assert(!(*it2).vertices.empty());
240
241
            // allocate the mesh
242
0
            *pi++ = meshIdx;
243
0
            aiMesh *mesh = pScene->mMeshes[meshIdx] = new aiMesh();
244
0
            mesh->mMaterialIndex = meshIdx++;
245
246
0
            mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
247
248
            // allocate storage for the vertex components and copy them
249
0
            mesh->mNumVertices = (unsigned int)(*it2).vertices.size();
250
0
            mesh->mVertices = new aiVector3D[mesh->mNumVertices];
251
0
            ::memcpy(mesh->mVertices, &(*it2).vertices[0], sizeof(aiVector3D) * mesh->mNumVertices);
252
253
0
            if ((*it2).colors.size()) {
254
0
                ai_assert((*it2).colors.size() == mesh->mNumVertices);
255
256
0
                mesh->mColors[0] = new aiColor4D[mesh->mNumVertices];
257
0
                ::memcpy(mesh->mColors[0], &(*it2).colors[0], sizeof(aiColor4D) * mesh->mNumVertices);
258
0
            }
259
260
            // generate triangles
261
0
            ai_assert(0 == mesh->mNumVertices % 3);
262
0
            aiFace *fc = mesh->mFaces = new aiFace[mesh->mNumFaces = mesh->mNumVertices / 3];
263
0
            aiFace *const fcEnd = fc + mesh->mNumFaces;
264
0
            unsigned int n = 0;
265
0
            while (fc != fcEnd) {
266
0
                aiFace &f = *fc++;
267
0
                f.mIndices = new unsigned int[f.mNumIndices = 3];
268
0
                for (unsigned int m = 0; m < 3; ++m)
269
0
                    f.mIndices[m] = n++;
270
0
            }
271
272
            // generate a material for the mesh
273
0
            aiMaterial *mat = new aiMaterial();
274
275
0
            aiColor4D clr(1.0f, 1.0f, 1.0f, 1.0f);
276
0
            if ("%default%" == (*it2).name) // a gray default material
277
0
            {
278
0
                clr.r = clr.g = clr.b = 0.6f;
279
0
            } else if ((*it2).name.length() > 0) // a texture
280
0
            {
281
0
                aiString s;
282
0
                s.Set((*it2).name);
283
0
                mat->AddProperty(&s, AI_MATKEY_TEXTURE_DIFFUSE(0));
284
0
            }
285
0
            mat->AddProperty<aiColor4D>(&clr, 1, AI_MATKEY_COLOR_DIFFUSE);
286
0
            *mats++ = mat;
287
0
        }
288
0
    }
289
0
}
290
291
} // namespace Assimp
292
293
#endif // !! ASSIMP_BUILD_NO_RAW_IMPORTER