-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathmeshIO.cpp
326 lines (284 loc) · 12 KB
/
meshIO.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2021 The Manifold Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "meshIO.h"
#include <algorithm>
#include <iostream>
#include "assimp/Exporter.hpp"
#include "assimp/Importer.hpp"
#include "assimp/material.h"
#include "assimp/postprocess.h"
#include "assimp/scene.h"
#include "optional_assert.h"
#ifndef AI_MATKEY_ROUGHNESS_FACTOR
#include "assimp/pbrmaterial.h"
#define AI_MATKEY_METALLIC_FACTOR \
AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLIC_FACTOR
#define AI_MATKEY_ROUGHNESS_FACTOR \
AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_ROUGHNESS_FACTOR
#endif
namespace {
using namespace manifold;
aiScene* CreateScene(const ExportOptions& options) {
aiScene* scene = new aiScene();
scene->mNumMaterials = 1;
scene->mMaterials = new aiMaterial*[scene->mNumMaterials];
scene->mMaterials[0] = new aiMaterial();
aiMaterial* material = scene->mMaterials[0];
material->AddProperty(&options.mat.roughness, 1, AI_MATKEY_ROUGHNESS_FACTOR);
material->AddProperty(&options.mat.metalness, 1, AI_MATKEY_METALLIC_FACTOR);
const glm::vec4& color = options.mat.color;
aiColor4D baseColor(color.r, color.g, color.b, color.a);
material->AddProperty(&baseColor, 1, AI_MATKEY_COLOR_DIFFUSE);
scene->mNumMeshes = 1;
scene->mMeshes = new aiMesh*[scene->mNumMeshes];
scene->mMeshes[0] = new aiMesh();
scene->mMeshes[0]->mMaterialIndex = 0;
scene->mRootNode = new aiNode();
scene->mRootNode->mNumMeshes = 1;
scene->mRootNode->mMeshes = new glm::uint[scene->mRootNode->mNumMeshes];
scene->mRootNode->mMeshes[0] = 0;
scene->mMeshes[0]->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
return scene;
}
std::string GetType(const std::string& filename) {
std::string ext = filename.substr(filename.find_last_of(".") + 1);
if (ext == "glb") ext = "glb2";
if (ext == "gltf") ext = "gltf2";
return ext;
}
void ExportScene(aiScene* scene, const std::string& filename) {
Assimp::Exporter exporter;
// int n = exporter.GetExportFormatCount();
// for (int i = 0; i < n; ++i) {
// auto desc = exporter.GetExportFormatDescription(i);
// std::cout << i << ", id = " << desc->id << ", " << desc->description
// << std::endl;
// }
auto ext = filename.substr(filename.length() - 4, 4);
if (ext == ".3mf") {
// Workaround https://github.com/assimp/assimp/issues/3816
aiNode* old_root = scene->mRootNode;
scene->mRootNode = new aiNode();
scene->mRootNode->addChildren(1, &old_root);
}
auto result = exporter.Export(scene, GetType(filename), filename);
delete scene;
ASSERT(result == AI_SUCCESS, userErr, exporter.GetErrorString());
}
} // namespace
namespace manifold {
/**
* Imports the given file as a Mesh structure, which can be converted to a
* Manifold if the mesh is a proper oriented 2-manifold. Any supported polygon
* format will be automatically triangulated.
*
* This is a very simple import function and is intended primarily as a
* demonstration. Generally users of this library will need to modify this to
* read all the important properties for their application and set up any custom
* data structures.
*
* @param filename Supports any format the Assimp library supports.
* @param forceCleanup This merges identical vertices, which can break
* manifoldness. However it is always done for STLs, as they cannot possibly be
* manifold without this step.
*/
Mesh ImportMesh(const std::string& filename, bool forceCleanup) {
std::string ext = filename.substr(filename.find_last_of(".") + 1);
const bool isYup = ext == "glb" || ext == "gltf";
Assimp::Importer importer;
importer.SetPropertyInteger(AI_CONFIG_PP_RVC_FLAGS, //
aiComponent_NORMALS | //
aiComponent_TANGENTS_AND_BITANGENTS | //
aiComponent_COLORS | //
aiComponent_TEXCOORDS | //
aiComponent_BONEWEIGHTS | //
aiComponent_ANIMATIONS | //
aiComponent_TEXTURES | //
aiComponent_LIGHTS | //
aiComponent_CAMERAS | //
aiComponent_MATERIALS);
importer.SetPropertyInteger(AI_CONFIG_PP_SBP_REMOVE,
aiPrimitiveType_POINT | aiPrimitiveType_LINE);
unsigned int flags = aiProcess_Triangulate | //
aiProcess_RemoveComponent | //
aiProcess_PreTransformVertices | //
aiProcess_SortByPType;
if (forceCleanup || ext == "stl") {
flags = flags | aiProcess_JoinIdenticalVertices | aiProcess_OptimizeMeshes;
}
const aiScene* scene = importer.ReadFile(filename, flags);
ASSERT(scene, userErr, importer.GetErrorString());
Mesh mesh_out;
for (int i = 0; i < scene->mNumMeshes; ++i) {
const aiMesh* mesh_i = scene->mMeshes[i];
for (int j = 0; j < mesh_i->mNumVertices; ++j) {
const aiVector3D vert = mesh_i->mVertices[j];
mesh_out.vertPos.push_back(isYup ? glm::vec3(vert.z, vert.x, vert.y)
: glm::vec3(vert.x, vert.y, vert.z));
}
for (int j = 0; j < mesh_i->mNumFaces; ++j) {
const aiFace face = mesh_i->mFaces[j];
ASSERT(face.mNumIndices == 3, userErr,
"Non-triangular face in " + filename);
mesh_out.triVerts.emplace_back(face.mIndices[0], face.mIndices[1],
face.mIndices[2]);
}
}
return mesh_out;
}
/**
* Saves the Mesh to the desired file type, determined from the extension
* specified. In the case of .glb/.gltf, this will save in version 2.0.
*
* This is a very simple export function and is intended primarily as a
* demonstration. Generally users of this library will need to modify this to
* write all the important properties for their application and read any custom
* data structures.
*
* @param filename The file extension must be one that Assimp supports for
* export. GLB & 3MF are recommended.
* @param mesh The mesh to export, likely from Manifold.GetMeshGL().
* @param options The options currently only affect an exported GLB's material.
* Pass {} for defaults.
*/
void ExportMesh(const std::string& filename, const MeshGL& mesh,
const ExportOptions& options) {
if (mesh.triVerts.size() == 0) {
std::cout << filename << " was not saved because the input mesh was empty."
<< std::endl;
return;
}
std::string type = GetType(filename);
const bool isYup = type == "glb2" || type == "gltf2";
aiScene* scene = CreateScene(options);
aiMesh* mesh_out = scene->mMeshes[0];
mesh_out->mNumVertices = mesh.NumVert();
mesh_out->mVertices = new aiVector3D[mesh_out->mNumVertices];
if (!options.faceted) {
bool validChannels = true;
for (int i : {0, 1, 2}) {
int c = options.mat.normalChannels[i];
validChannels &= c >= 3 && c < (int)mesh.numProp;
}
ASSERT(validChannels, userErr,
"When faceted is false, valid normalChannels must be supplied.");
mesh_out->mNormals = new aiVector3D[mesh_out->mNumVertices];
}
bool validChannels = true;
bool hasColor = false;
for (int i : {0, 1, 2, 3}) {
int c = options.mat.colorChannels[i];
validChannels &= c < (int)mesh.numProp;
hasColor |= c >= 0;
}
ASSERT(validChannels, userErr, "Invalid colorChannels.");
if (hasColor) mesh_out->mColors[0] = new aiColor4D[mesh_out->mNumVertices];
for (int i = 0; i < mesh_out->mNumVertices; ++i) {
glm::vec3 v;
for (int j : {0, 1, 2}) v[j] = mesh.vertProperties[i * mesh.numProp + j];
mesh_out->mVertices[i] =
isYup ? aiVector3D(v.y, v.z, v.x) : aiVector3D(v.x, v.y, v.z);
if (!options.faceted) {
glm::vec3 n;
for (int j : {0, 1, 2})
n[j] = mesh.vertProperties[i * mesh.numProp +
options.mat.normalChannels[j]];
mesh_out->mNormals[i] =
isYup ? aiVector3D(n.y, n.z, n.x) : aiVector3D(n.x, n.y, n.z);
}
if (hasColor) {
glm::vec4 c;
for (int j : {0, 1, 2, 3})
c[j] = options.mat.colorChannels[j] < 0
? 1
: mesh.vertProperties[i * mesh.numProp +
options.mat.colorChannels[j]];
c = glm::saturate(c);
mesh_out->mColors[0][i] = aiColor4D(c.r, c.g, c.b, c.a);
}
}
mesh_out->mNumFaces = mesh.NumTri();
mesh_out->mFaces = new aiFace[mesh_out->mNumFaces];
for (int i = 0; i < mesh_out->mNumFaces; ++i) {
aiFace& face = mesh_out->mFaces[i];
face.mNumIndices = 3;
face.mIndices = new glm::uint[face.mNumIndices];
for (int j : {0, 1, 2}) face.mIndices[j] = mesh.triVerts[3 * i + j];
}
ExportScene(scene, filename);
}
/**
* Saves the Mesh to the desired file type, determined from the extension
* specified. In the case of .glb/.gltf, this will save in version 2.0.
*
* This is a very simple export function and is intended primarily as a
* demonstration. Generally users of this library will need to modify this to
* write all the important properties for their application and read any custom
* data structures.
*
* @param filename The file extension must be one that Assimp supports for
* export. GLB & 3MF are recommended.
* @param mesh The mesh to export, likely from Manifold.GetMesh().
* @param options The options currently only affect an exported GLB's material.
* Pass {} for defaults.
*/
void ExportMesh(const std::string& filename, const Mesh& mesh,
const ExportOptions& options) {
if (mesh.triVerts.size() == 0) {
std::cout << filename << " was not saved because the input mesh was empty."
<< std::endl;
return;
}
std::string type = GetType(filename);
const bool isYup = type == "glb2" || type == "gltf2";
aiScene* scene = CreateScene(options);
aiMesh* mesh_out = scene->mMeshes[0];
mesh_out->mNumVertices = mesh.vertPos.size();
mesh_out->mVertices = new aiVector3D[mesh_out->mNumVertices];
if (!options.faceted) {
ASSERT(
mesh.vertNormal.size() == mesh.vertPos.size(), userErr,
"vertNormal must be the same length as vertPos when faceted is false.");
mesh_out->mNormals = new aiVector3D[mesh_out->mNumVertices];
}
if (!options.mat.vertColor.empty()) {
ASSERT(mesh.vertPos.size() == options.mat.vertColor.size(), userErr,
"If present, vertColor must be the same length as vertPos.");
mesh_out->mColors[0] = new aiColor4D[mesh_out->mNumVertices];
}
for (int i = 0; i < mesh_out->mNumVertices; ++i) {
const glm::vec3& v = mesh.vertPos[i];
mesh_out->mVertices[i] =
isYup ? aiVector3D(v.y, v.z, v.x) : aiVector3D(v.x, v.y, v.z);
if (!options.faceted) {
const glm::vec3& n = mesh.vertNormal[i];
mesh_out->mNormals[i] =
isYup ? aiVector3D(n.y, n.z, n.x) : aiVector3D(n.x, n.y, n.z);
}
if (!options.mat.vertColor.empty()) {
const glm::vec4& c = options.mat.vertColor[i];
mesh_out->mColors[0][i] = aiColor4D(c.r, c.g, c.b, c.a);
}
}
mesh_out->mNumFaces = mesh.triVerts.size();
mesh_out->mFaces = new aiFace[mesh_out->mNumFaces];
for (int i = 0; i < mesh_out->mNumFaces; ++i) {
aiFace& face = mesh_out->mFaces[i];
face.mNumIndices = 3;
face.mIndices = new glm::uint[face.mNumIndices];
for (int j : {0, 1, 2}) face.mIndices[j] = mesh.triVerts[i][j];
}
ExportScene(scene, filename);
}
} // namespace manifold