Blender V5.3
stl_import_mesh.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_mesh.hh"
10
11#include "BLI_array_utils.hh"
12#include "BLI_span.hh"
13
14#include "DNA_mesh_types.h"
15
16#include "stl_data.hh"
17#include "stl_import_mesh.hh"
18
19#include "CLG_log.h"
20
21namespace blender {
22
23static CLG_LogRef LOG = {"io.stl"};
24
25namespace io::stl {
26
27STLMeshHelper::STLMeshHelper(int tris_num, bool use_custom_normals)
28 : use_custom_normals_(use_custom_normals)
29{
30 degenerate_tris_num_ = 0;
31 duplicate_tris_num_ = 0;
32 tris_.reserve(tris_num);
33 /* Upper bound (all vertices are unique). */
34 verts_.reserve(int64_t(tris_num) * 3);
35 if (use_custom_normals) {
36 loop_normals_.reserve(int64_t(tris_num) * 3);
37 }
38}
39
41{
42 int v1_id = verts_.index_of_or_add(data.vertices[0]);
43 int v2_id = verts_.index_of_or_add(data.vertices[1]);
44 int v3_id = verts_.index_of_or_add(data.vertices[2]);
45 if ((v1_id == v2_id) || (v1_id == v3_id) || (v2_id == v3_id)) {
46 degenerate_tris_num_++;
47 return false;
48 }
49 if (!tris_.add({v1_id, v2_id, v3_id})) {
50 duplicate_tris_num_++;
51 return false;
52 }
53
54 if (use_custom_normals_) {
55 loop_normals_.append_n_times(data.normal, 3);
56 }
57 return true;
58}
59
61{
62 if (degenerate_tris_num_ > 0) {
63 CLOG_WARN(&LOG, "Removed %d degenerate triangles during import", degenerate_tris_num_);
64 }
65 if (duplicate_tris_num_ > 0) {
66 CLOG_WARN(&LOG, "Removed %d duplicate triangles during import", duplicate_tris_num_);
67 }
68
69 Mesh *mesh = BKE_mesh_new_nomain(verts_.size(), 0, tris_.size(), tris_.size() * 3);
70 mesh->vert_positions_for_write().copy_from(verts_);
71 offset_indices::fill_constant_group_size(3, 0, mesh->face_offsets_for_write());
72 array_utils::copy(tris_.as_span().cast<int>(), mesh->corner_verts_for_write());
73
75
76 /* NOTE: edges must be calculated first before setting custom normals. */
77 bke::mesh_calc_edges(*mesh, false, false);
78
79 if (use_custom_normals_ && loop_normals_.size() == mesh->corners_num) {
80 bke::mesh_set_custom_normals(*mesh, loop_normals_);
81 }
82
83 return mesh;
84}
85
86} // namespace io::stl
87} // namespace blender
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
bool add_triangle(const PackedTriangle &data)
STLMeshHelper(int tris_num, bool use_custom_normals)
void copy(const GVArray &src, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void mesh_smooth_set(Mesh &mesh, bool use_smooth, bool keep_sharp_edges=false)
void mesh_calc_edges(Mesh &mesh, bool keep_existing_edges, bool select_new_edges)
void mesh_set_custom_normals(Mesh &mesh, MutableSpan< float3 > corner_normals)
void fill_constant_group_size(int size, int start_offset, MutableSpan< int > offsets)
BMesh Iterators.
__int64 int64_t
Definition BKE_text.h:90
BMesh const char void * data
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)
static CLG_LogRef LOG
Definition logging.cpp:12