OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::anonymous_namespace{AssimpImport.cpp} Namespace Reference

Functions

void clear_textured_mesh (TexturedMesh &out)
void set_error_message (std::string *error_message, const std::string &message)
bool is_fbx_path (const std::string &path)
bool should_flip_uvs (const std::string &path)
unsigned int assimp_import_flags (const std::string &path)
void configure_importer (Assimp::Importer &importer, const std::string &path, unsigned int flags)
bool read_external_texture_file (const boost::filesystem::path &path, TextureImage &out)
bool read_embedded_texture (const aiTexture &texture, TextureImage &out)
bool get_material_texture (const aiMaterial &material, aiString &texture_path)
std::array< float, 4 > get_material_color (const aiMaterial &material)
bool collect_mesh (const aiMesh &mesh, size_t &vertex_offset, TexturedMesh &out, std::string &error)
void collect_materials (const aiScene &scene, const boost::filesystem::path &base_dir, TexturedMesh &out)
std::string scene_failure_summary (const std::string &path, const char *assimp_error)

Function Documentation

◆ assimp_import_flags()

unsigned int Slic3r::anonymous_namespace{AssimpImport.cpp}::assimp_import_flags ( const std::string & path)
67{
68 unsigned int flags = aiProcess_Triangulate
69 | aiProcess_GenNormals
70 | aiProcess_PreTransformVertices
71 | aiProcess_SortByPType;
72 if (should_flip_uvs(path))
73 flags |= aiProcess_FlipUVs;
74 return flags;
75}
bool should_flip_uvs(const std::string &path)
Definition AssimpImport.cpp:60

References assimp_import_flags(), and should_flip_uvs().

Referenced by assimp_import_flags().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ clear_textured_mesh()

void Slic3r::anonymous_namespace{AssimpImport.cpp}::clear_textured_mesh ( TexturedMesh & out)
37{
38 out.vertices.clear();
39 out.indices.clear();
40 out.uvs.clear();
41 out.uv_coords.clear();
42 out.uv_indices.clear();
43 out.textures.clear();
44 out.material_ids.clear();
45 out.material_texture_map.clear();
46 out.material_colors.clear();
47}
std::vector< int > material_ids
Definition TexturePainting.hpp:29
std::vector< int > material_texture_map
Definition TexturePainting.hpp:31
std::vector< std::array< int, 3 > > uv_indices
Definition TexturePainting.hpp:38
std::vector< std::array< int, 3 > > indices
Definition TexturePainting.hpp:26
std::vector< std::array< float, 2 > > uv_coords
Definition TexturePainting.hpp:37
std::vector< std::array< float, 2 > > uvs
Definition TexturePainting.hpp:27
std::vector< TextureImage > textures
Definition TexturePainting.hpp:28
std::vector< std::array< float, 3 > > vertices
Definition TexturePainting.hpp:25
std::vector< std::array< float, 4 > > material_colors
Definition TexturePainting.hpp:33

References clear_textured_mesh(), Slic3r::TexturedMesh::indices, Slic3r::TexturedMesh::material_colors, Slic3r::TexturedMesh::material_ids, Slic3r::TexturedMesh::material_texture_map, Slic3r::TexturedMesh::textures, Slic3r::TexturedMesh::uv_coords, Slic3r::TexturedMesh::uv_indices, Slic3r::TexturedMesh::uvs, and Slic3r::TexturedMesh::vertices.

Referenced by clear_textured_mesh().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ collect_materials()

void Slic3r::anonymous_namespace{AssimpImport.cpp}::collect_materials ( const aiScene & scene,
const boost::filesystem::path & base_dir,
TexturedMesh & out )
230{
231 out.material_texture_map.assign(scene.mNumMaterials, -1);
232 out.material_colors.assign(scene.mNumMaterials, {1.f, 1.f, 1.f, 1.f});
233
234 for (unsigned int material_index = 0; material_index < scene.mNumMaterials; ++material_index) {
235 const aiMaterial* material = scene.mMaterials[material_index];
236 if (!material)
237 continue;
238
239 out.material_colors[material_index] = get_material_color(*material);
240
241 aiString texture_path;
242 if (!get_material_texture(*material, texture_path))
243 continue;
244
245 TextureImage image;
246 const aiTexture* embedded_texture = scene.GetEmbeddedTexture(texture_path.C_Str());
247 if (embedded_texture) {
248 if (!read_embedded_texture(*embedded_texture, image))
249 continue;
250 } else {
251 const boost::filesystem::path resolved = resource_path::resolve_external_resource_path(
252 base_dir, texture_path.C_Str(), "Assimp texture");
253 if (resolved.empty()) {
254 BOOST_LOG_TRIVIAL(warning) << "AssimpImport: texture file not found: "
255 << texture_path.C_Str();
256 continue;
257 }
258 if (!read_external_texture_file(resolved, image)) {
259 BOOST_LOG_TRIVIAL(warning) << "AssimpImport: failed to read texture: "
260 << resolved;
261 continue;
262 }
263 }
264
265 out.material_texture_map[material_index] = static_cast<int>(out.textures.size());
266 out.textures.push_back(std::move(image));
267 }
268}
bool read_external_texture_file(const boost::filesystem::path &path, TextureImage &out)
Definition AssimpImport.cpp:95
std::array< float, 4 > get_material_color(const aiMaterial &material)
Definition AssimpImport.cpp:178
bool read_embedded_texture(const aiTexture &texture, TextureImage &out)
Definition AssimpImport.cpp:120
bool get_material_texture(const aiMaterial &material, aiString &texture_path)
Definition AssimpImport.cpp:163
boost::filesystem::path resolve_external_resource_path(const boost::filesystem::path &base_dir, const std::string &raw_path, const char *context="resource_path", bool allow_basename_fallback=true)
Definition ResourcePathUtils.hpp:206
Definition TexturePainting.hpp:17

References collect_materials(), get_material_color(), get_material_texture(), Slic3r::TexturedMesh::material_colors, Slic3r::TexturedMesh::material_texture_map, read_embedded_texture(), read_external_texture_file(), Slic3r::resource_path::resolve_external_resource_path(), and Slic3r::TexturedMesh::textures.

Referenced by collect_materials().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ collect_mesh()

bool Slic3r::anonymous_namespace{AssimpImport.cpp}::collect_mesh ( const aiMesh & mesh,
size_t & vertex_offset,
TexturedMesh & out,
std::string & error )
189{
190 if (mesh.mNumVertices > static_cast<size_t>(std::numeric_limits<int>::max()) - vertex_offset) {
191 error = "Assimp mesh has too many vertices for TexturedMesh indices";
192 return false;
193 }
194
195 for (unsigned int i = 0; i < mesh.mNumVertices; ++i) {
196 const aiVector3D& v = mesh.mVertices[i];
197 out.vertices.push_back({v.x, v.y, v.z});
198
199 if (mesh.HasTextureCoords(0)) {
200 const aiVector3D& uv = mesh.mTextureCoords[0][i];
201 out.uvs.push_back({uv.x, uv.y});
202 } else {
203 out.uvs.push_back({0.f, 0.f});
204 }
205 }
206
207 const int material_index = static_cast<int>(mesh.mMaterialIndex);
208 for (unsigned int i = 0; i < mesh.mNumFaces; ++i) {
209 const aiFace& face = mesh.mFaces[i];
210 if (face.mNumIndices != 3)
211 continue;
212 if (face.mIndices[0] >= mesh.mNumVertices ||
213 face.mIndices[1] >= mesh.mNumVertices ||
214 face.mIndices[2] >= mesh.mNumVertices) {
215 error = "Assimp mesh face index is out of bounds";
216 return false;
217 }
218 out.indices.push_back({
219 static_cast<int>(static_cast<size_t>(face.mIndices[0]) + vertex_offset),
220 static_cast<int>(static_cast<size_t>(face.mIndices[1]) + vertex_offset),
221 static_cast<int>(static_cast<size_t>(face.mIndices[2]) + vertex_offset)});
222 out.material_ids.push_back(material_index);
223 }
224
225 vertex_offset += mesh.mNumVertices;
226 return true;
227}

References collect_mesh(), Slic3r::TexturedMesh::indices, Slic3r::TexturedMesh::material_ids, Slic3r::TexturedMesh::uvs, and Slic3r::TexturedMesh::vertices.

Referenced by collect_mesh().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ configure_importer()

void Slic3r::anonymous_namespace{AssimpImport.cpp}::configure_importer ( Assimp::Importer & importer,
const std::string & path,
unsigned int flags )
78{
79 importer.SetPropertyInteger(AI_CONFIG_PP_SBP_REMOVE,
80 aiPrimitiveType_POINT | aiPrimitiveType_LINE);
81
82 if (flags & aiProcess_PreTransformVertices)
83 importer.SetPropertyBool(AI_CONFIG_PP_PTV_KEEP_HIERARCHY, true);
84
85 if (is_fbx_path(path)) {
86 importer.SetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ALL_GEOMETRY_LAYERS, true);
87 importer.SetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_MATERIALS, true);
88 importer.SetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_TEXTURES, true);
89 importer.SetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ANIMATIONS, false);
90 importer.SetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_LIGHTS, false);
91 importer.SetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_CAMERAS, false);
92 }
93}
bool is_fbx_path(const std::string &path)
Definition AssimpImport.cpp:55

References configure_importer(), and is_fbx_path().

Referenced by configure_importer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_material_color()

std::array< float, 4 > Slic3r::anonymous_namespace{AssimpImport.cpp}::get_material_color ( const aiMaterial & material)
179{
180 aiColor4D color(1.f, 1.f, 1.f, 1.f);
181 if (material.Get(AI_MATKEY_BASE_COLOR, color) == AI_SUCCESS)
182 return {color.r, color.g, color.b, color.a};
183 if (material.Get(AI_MATKEY_COLOR_DIFFUSE, color) == AI_SUCCESS)
184 return {color.r, color.g, color.b, color.a};
185 return {1.f, 1.f, 1.f, 1.f};
186}

References get_material_color().

Referenced by collect_materials(), and get_material_color().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_material_texture()

bool Slic3r::anonymous_namespace{AssimpImport.cpp}::get_material_texture ( const aiMaterial & material,
aiString & texture_path )
164{
165 if (material.GetTextureCount(aiTextureType_DIFFUSE) > 0 &&
166 material.GetTexture(aiTextureType_DIFFUSE, 0, &texture_path) == AI_SUCCESS) {
167 return true;
168 }
169
170 if (material.GetTextureCount(aiTextureType_BASE_COLOR) > 0 &&
171 material.GetTexture(aiTextureType_BASE_COLOR, 0, &texture_path) == AI_SUCCESS) {
172 return true;
173 }
174
175 return false;
176}

References get_material_texture().

Referenced by collect_materials(), and get_material_texture().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_fbx_path()

bool Slic3r::anonymous_namespace{AssimpImport.cpp}::is_fbx_path ( const std::string & path)
56{
57 return boost::algorithm::iends_with(path, ".fbx");
58}

References is_fbx_path().

Referenced by configure_importer(), and is_fbx_path().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_embedded_texture()

bool Slic3r::anonymous_namespace{AssimpImport.cpp}::read_embedded_texture ( const aiTexture & texture,
TextureImage & out )
121{
122 out.data.clear();
123 if (texture.mHeight == 0) {
124 if (texture.mWidth == 0)
125 return false;
126 out.width = -1;
127 out.height = -1;
128 out.channels = 0;
129 out.data.assign(
130 reinterpret_cast<const unsigned char*>(texture.pcData),
131 reinterpret_cast<const unsigned char*>(texture.pcData) + texture.mWidth);
132 return !out.data.empty();
133 }
134
135 if (texture.mWidth == 0 || texture.mHeight == 0)
136 return false;
137 if (texture.mWidth > static_cast<unsigned int>(std::numeric_limits<int>::max()) ||
138 texture.mHeight > static_cast<unsigned int>(std::numeric_limits<int>::max())) {
139 return false;
140 }
141 const size_t width = static_cast<size_t>(texture.mWidth);
142 const size_t height = static_cast<size_t>(texture.mHeight);
143 if (width > std::numeric_limits<size_t>::max() / height ||
144 width * height > std::numeric_limits<size_t>::max() / 4) {
145 return false;
146 }
147
148 out.width = static_cast<int>(texture.mWidth);
149 out.height = static_cast<int>(texture.mHeight);
150 out.channels = 4;
151 const size_t pixel_count = width * height;
152 out.data.resize(pixel_count * 4);
153 for (size_t i = 0; i < pixel_count; ++i) {
154 const aiTexel& texel = texture.pcData[i];
155 out.data[i * 4 + 0] = texel.r;
156 out.data[i * 4 + 1] = texel.g;
157 out.data[i * 4 + 2] = texel.b;
158 out.data[i * 4 + 3] = texel.a;
159 }
160 return !out.data.empty();
161}
coord_t width(const BoundingBox &box)
Definition Arrange.cpp:1184
coord_t height(const BoundingBox &box)
Definition Arrange.cpp:1185
int height
Definition TexturePainting.hpp:19
int channels
Definition TexturePainting.hpp:20
int width
Definition TexturePainting.hpp:18
std::vector< unsigned char > data
Definition TexturePainting.hpp:21

References Slic3r::TextureImage::channels, Slic3r::TextureImage::data, Slic3r::TextureImage::height, read_embedded_texture(), and Slic3r::TextureImage::width.

Referenced by collect_materials(), and read_embedded_texture().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_external_texture_file()

bool Slic3r::anonymous_namespace{AssimpImport.cpp}::read_external_texture_file ( const boost::filesystem::path & path,
TextureImage & out )
96{
97 boost::nowide::ifstream file(path.string(), std::ios::binary | std::ios::ate);
98 if (!file.is_open())
99 return false;
100
101 const std::streamoff size = file.tellg();
102 if (size <= 0)
103 return false;
104 if (static_cast<uintmax_t>(size) > static_cast<uintmax_t>(std::numeric_limits<size_t>::max()))
105 return false;
106
107 file.seekg(0);
108 out.width = -1;
109 out.height = -1;
110 out.channels = 0;
111 out.data.resize(static_cast<size_t>(size));
112 file.read(reinterpret_cast<char*>(out.data.data()), size);
113 if (!file && !file.eof()) {
114 out.data.clear();
115 return false;
116 }
117 return true;
118}

References Slic3r::TextureImage::channels, Slic3r::TextureImage::data, Slic3r::TextureImage::height, read_external_texture_file(), and Slic3r::TextureImage::width.

Referenced by collect_materials(), and read_external_texture_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ scene_failure_summary()

std::string Slic3r::anonymous_namespace{AssimpImport.cpp}::scene_failure_summary ( const std::string & path,
const char * assimp_error )
271{
272 std::ostringstream ss;
273 ss << "Assimp failed to import " << path;
274 if (assimp_error && assimp_error[0] != '\0')
275 ss << ": " << assimp_error;
276 return ss.str();
277}

References scene_failure_summary().

Referenced by scene_failure_summary().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_error_message()

void Slic3r::anonymous_namespace{AssimpImport.cpp}::set_error_message ( std::string * error_message,
const std::string & message )
50{
51 if (error_message)
52 *error_message = message;
53}

References set_error_message().

Referenced by set_error_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ should_flip_uvs()

bool Slic3r::anonymous_namespace{AssimpImport.cpp}::should_flip_uvs ( const std::string & path)
61{
62 return boost::algorithm::iends_with(path, ".fbx") ||
63 boost::algorithm::iends_with(path, ".glb");
64}

References should_flip_uvs().

Referenced by assimp_import_flags(), and should_flip_uvs().

Here is the call graph for this function:
Here is the caller graph for this function: