Coverage Report

Created: 2025-12-05 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/AssetLib/STL/STLExporter.cpp
Line
Count
Source
1
/*
2
Open Asset Import Library (assimp)
3
----------------------------------------------------------------------
4
5
Copyright (c) 2006-2025, 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
#if !defined(ASSIMP_BUILD_NO_EXPORT) && !defined(ASSIMP_BUILD_NO_STL_EXPORTER)
42
43
#include "STLExporter.h"
44
#include <assimp/version.h>
45
#include <assimp/IOSystem.hpp>
46
#include <assimp/scene.h>
47
#include <assimp/Exporter.hpp>
48
#include <memory>
49
#include <assimp/Exceptional.h>
50
#include <assimp/ByteSwapper.h>
51
52
using namespace Assimp;
53
54
namespace Assimp {
55
56
// ------------------------------------------------------------------------------------------------
57
// Worker function for exporting a scene to Stereolithograpy. Prototyped and registered in Exporter.cpp
58
void ExportSceneSTL(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties )
59
0
{
60
0
    bool exportPointClouds = pProperties->GetPropertyBool(AI_CONFIG_EXPORT_POINT_CLOUDS);
61
62
    // invoke the exporter
63
0
    STLExporter exporter(pFile, pScene, exportPointClouds );
64
65
0
    if (exporter.mOutput.fail()) {
66
0
        throw DeadlyExportError("output data creation failed. Most likely the file became too large: " + std::string(pFile));
67
0
    }
68
69
    // we're still here - export successfully completed. Write the file.
70
0
    std::unique_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
71
0
    if (outfile == nullptr) {
72
0
        throw DeadlyExportError("could not open output .stl file: " + std::string(pFile));
73
0
    }
74
75
0
    outfile->Write( exporter.mOutput.str().c_str(), static_cast<size_t>(exporter.mOutput.tellp()),1);
76
0
}
77
78
void ExportSceneSTLBinary(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties )
79
0
{
80
0
    bool exportPointClouds = pProperties->GetPropertyBool(AI_CONFIG_EXPORT_POINT_CLOUDS);
81
82
    // invoke the exporter
83
0
    STLExporter exporter(pFile, pScene, exportPointClouds, true);
84
85
0
    if (exporter.mOutput.fail()) {
86
0
        throw DeadlyExportError("output data creation failed. Most likely the file became too large: " + std::string(pFile));
87
0
    }
88
89
    // we're still here - export successfully completed. Write the file.
90
0
    std::unique_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wb"));
91
0
    if (outfile == nullptr) {
92
0
        throw DeadlyExportError("could not open output .stl file: " + std::string(pFile));
93
0
    }
94
95
0
    outfile->Write( exporter.mOutput.str().c_str(), static_cast<size_t>(exporter.mOutput.tellp()),1);
96
0
}
97
98
} // end of namespace Assimp
99
100
static constexpr char SolidToken[]    = "solid";
101
static constexpr char EndSolidToken[] = "endsolid";
102
103
// ------------------------------------------------------------------------------------------------
104
0
STLExporter::STLExporter(const char* _filename, const aiScene* pScene, bool exportPointClouds, bool binary) : filename(_filename) , endl("\n")
105
0
{
106
    // make sure that all formatting happens using the standard, C locale and not the user's current locale
107
0
    const std::locale& l = std::locale("C");
108
0
    mOutput.imbue(l);
109
0
    mOutput.precision(ASSIMP_AI_REAL_TEXT_PRECISION);
110
0
    if (binary) {
111
0
        char buf[80] = {0} ;
112
0
        buf[0] = 'A'; buf[1] = 's'; buf[2] = 's'; buf[3] = 'i'; buf[4] = 'm'; buf[5] = 'p';
113
0
        buf[6] = 'S'; buf[7] = 'c'; buf[8] = 'e'; buf[9] = 'n'; buf[10] = 'e';
114
0
        mOutput.write(buf, 80);
115
0
        unsigned int meshnum = 0;
116
0
        for(unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
117
0
            for (unsigned int j = 0; j < pScene->mMeshes[i]->mNumFaces; ++j) {
118
0
                meshnum++;
119
0
            }
120
0
        }
121
0
        AI_SWAP4(meshnum);
122
0
        mOutput.write((char *)&meshnum, 4);
123
124
0
        if (exportPointClouds) {
125
0
            throw DeadlyExportError("This functionality is not yet implemented for binary output.");
126
0
        }
127
128
0
        for(unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
129
0
            WriteMeshBinary(pScene->mMeshes[i]);
130
0
        }
131
0
    } else {
132
133
        // Exporting only point clouds
134
0
        if (exportPointClouds) {
135
0
            WritePointCloud("Assimp_Pointcloud", pScene );
136
0
            return;
137
0
        }
138
139
        // Export the assimp mesh
140
0
        const std::string name = "AssimpScene";
141
0
        mOutput << SolidToken << " " << name << endl;
142
0
        for(unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
143
0
            WriteMesh(pScene->mMeshes[ i ]);
144
0
        }
145
0
        mOutput << EndSolidToken << " " << name << endl;
146
0
    }
147
0
}
148
149
// ------------------------------------------------------------------------------------------------
150
0
void STLExporter::WritePointCloud(const std::string &name, const aiScene* pScene) {
151
0
    mOutput << " " << SolidToken << " " << name << endl;
152
0
    aiVector3D nor;
153
0
    mOutput << " facet normal " << nor.x << " " << nor.y << " " << nor.z << endl;
154
0
    for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
155
0
        aiMesh *mesh = pScene->mMeshes[i];
156
0
        if (nullptr == mesh) {
157
0
            continue;
158
0
        }
159
160
0
        for (unsigned int a = 0; a < mesh->mNumVertices; ++a) {
161
0
            const aiVector3D& v = mesh->mVertices[a];
162
0
            mOutput << "  vertex " << v.x << " " << v.y << " " << v.z << endl;
163
0
            mOutput << "  vertex " << v.x << " " << v.y << " " << v.z << endl;
164
0
            mOutput << "  vertex " << v.x << " " << v.y << " " << v.z << endl;
165
0
        }
166
0
    }
167
0
    mOutput << EndSolidToken << " " << name << endl;
168
0
}
169
170
// ------------------------------------------------------------------------------------------------
171
0
void STLExporter::WriteMesh(const aiMesh* m) {
172
0
    for (unsigned int i = 0; i < m->mNumFaces; ++i) {
173
0
        const aiFace& f = m->mFaces[i];
174
0
        if (f.mNumIndices < 3) {
175
0
            continue;
176
0
        }
177
178
        // we need per-face normals. We specified aiProcess_GenNormals as pre-requisite for this exporter,
179
        // but nonetheless we have to expect per-vertex normals.
180
0
        aiVector3D nor;
181
0
        if (m->mNormals) {
182
0
            for (unsigned int a = 0; a < f.mNumIndices; ++a) {
183
0
                nor += m->mNormals[f.mIndices[a]];
184
0
            }
185
0
            nor.NormalizeSafe();
186
0
        }
187
0
        mOutput << " facet normal " << nor.x << " " << nor.y << " " << nor.z << endl;
188
0
        mOutput << "  outer loop" << endl;
189
0
        for (unsigned int a = 0; a < f.mNumIndices; ++a) {
190
0
            const aiVector3D &v = m->mVertices[f.mIndices[a]];
191
0
            mOutput << "  vertex " << v.x << " " << v.y << " " << v.z << endl;
192
0
        }
193
194
0
        mOutput << "  endloop" << endl;
195
0
        mOutput << " endfacet" << endl << endl;
196
0
    }
197
0
}
198
199
0
void STLExporter::WriteMeshBinary(const aiMesh* m) {
200
0
    for (unsigned int i = 0; i < m->mNumFaces; ++i) {
201
0
        const aiFace& f = m->mFaces[i];
202
0
        if (f.mNumIndices < 3) {
203
0
            continue;
204
0
        }
205
206
        // we need per-face normals. We specified aiProcess_GenNormals as pre-requisite for this exporter,
207
        // but nonetheless we have to expect per-vertex normals.
208
0
        aiVector3D nor;
209
0
        if (m->mNormals) {
210
0
            for(unsigned int a = 0; a < f.mNumIndices; ++a) {
211
0
                nor += m->mNormals[f.mIndices[a]];
212
0
            }
213
0
            nor.Normalize();
214
0
        }
215
        // STL binary files use 4-byte floats. This may possibly cause loss of precision
216
        // for clients using 8-byte doubles
217
0
        float nx = (float) nor.x;
218
0
        float ny = (float) nor.y;
219
0
        float nz = (float) nor.z;
220
0
        AI_SWAP4(nx); AI_SWAP4(ny); AI_SWAP4(nz);
221
0
        mOutput.write((char *)&nx, 4); mOutput.write((char *)&ny, 4); mOutput.write((char *)&nz, 4);
222
0
        for(unsigned int a = 0; a < f.mNumIndices; ++a) {
223
0
            const aiVector3D& v  = m->mVertices[f.mIndices[a]];
224
0
            float vx = (float) v.x, vy = (float) v.y, vz = (float) v.z;
225
0
            AI_SWAP4(vx); AI_SWAP4(vy); AI_SWAP4(vz);
226
0
            mOutput.write((char *)&vx, 4); mOutput.write((char *)&vy, 4); mOutput.write((char *)&vz, 4);
227
0
        }
228
0
        char dummy[2] = {0};
229
0
        mOutput.write(dummy, 2);
230
0
    }
231
0
}
232
233
#endif