Blender V5.3
ply_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_attribute.h"
10#include "BKE_attribute.hh"
11#include "BKE_lib_id.hh"
12#include "BKE_mesh.hh"
13
15
16#include "BLI_color_types.hh"
17#include "BLI_math_color_c.hh"
18#include "BLI_math_vector_c.hh"
19#include "BLI_memory_utils.hh"
20#include "BLI_span.hh"
21
22#include "IO_validate.hh"
23
24#include "ply_import_mesh.hh"
25
26#include "CLG_log.h"
27
28#include <cinttypes>
29
30namespace blender {
31
32static CLG_LogRef LOG = {"io.ply"};
33
34namespace io::ply {
36{
37 if (!validate::size_fits_in_int(data.vertices.size()) ||
38 !validate::size_fits_in_int(data.edges.size()) ||
39 !validate::size_fits_in_int(data.face_sizes.size()) ||
40 !validate::size_fits_in_int(data.face_vertices.size()))
41 {
42 CLOG_WARN(&LOG, "PLY mesh too large to import, exceeds max int size");
43 return BKE_mesh_new_nomain(0, 0, 0, 0);
44 }
45
47 data.vertices.size(), data.edges.size(), data.face_sizes.size(), data.face_vertices.size());
48 /* Free the new mesh if an exception like std::bad_alloc is thrown. */
49 bool success = false;
50 BLI_SCOPED_DEFER([&]() {
51 if (!success) {
52 BKE_id_free(nullptr, mesh);
53 }
54 });
55
56 mesh->vert_positions_for_write().copy_from(data.vertices);
57
58 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
59
60 if (!data.edges.is_empty()) {
61 MutableSpan<int2> edges = mesh->edges_for_write();
62 for (const int i : data.edges.index_range()) {
63 int32_t v1 = data.edges[i].first;
64 int32_t v2 = data.edges[i].second;
65 if (!validate::index_in_range(v1, mesh->verts_num)) {
66 CLOG_WARN(&LOG, "Invalid PLY vertex index in edge %i/1: %d", i, v1);
67 v1 = 0;
68 }
69 if (!validate::index_in_range(v2, mesh->verts_num)) {
70 CLOG_WARN(&LOG, "Invalid PLY vertex index in edge %i/2: %d", i, v2);
71 v2 = 0;
72 }
73 edges[i] = {v1, v2};
74 }
75 }
76
77 /* Add faces to the mesh. */
78 if (!data.face_sizes.is_empty()) {
79 MutableSpan<int> face_offsets = mesh->face_offsets_for_write();
80 MutableSpan<int> corner_verts = mesh->corner_verts_for_write();
81
82 /* Fill in face data. */
83 int64_t offset = 0;
84 for (const int64_t i : data.face_sizes.index_range()) {
85 const int64_t size = data.face_sizes[i];
86 face_offsets[i] = offset;
87 for (int64_t j = 0; j < size; j++) {
88 uint32_t v = data.face_vertices[offset + j];
89 if (!validate::index_in_range(v, mesh->verts_num)) {
91 &LOG, "Invalid PLY vertex index in face %" PRId64 " loop %" PRId64 ": %u", i, j, v);
92 v = 0;
93 }
94 corner_verts[offset + j] = v;
95 }
96 offset += size;
97 }
98 }
99
100 /* Vertex colors */
101 if (!data.vertex_colors.is_empty() && params.vertex_colors != ePLYVertexColorMode::None) {
102 /* Create a data layer for vertex colors and set them. */
105
107 for (const int i : data.vertex_colors.index_range()) {
108 srgb_to_linearrgb_v4(colors.span[i], data.vertex_colors[i]);
109 }
110 }
111 else {
112 for (const int i : data.vertex_colors.index_range()) {
113 copy_v4_v4(colors.span[i], data.vertex_colors[i]);
114 }
115 }
116 colors.finish();
119 }
120
121 /* Uvmap */
122 if (!data.uv_coordinates.is_empty()) {
124 "UVMap", bke::AttrDomain::Corner);
125 for (const int i : data.face_vertices.index_range()) {
126 uint32_t v = data.face_vertices[i];
127 uv_map.span[i] = validate::index_in_range(v, data.uv_coordinates.size()) ?
128 data.uv_coordinates[v] :
129 float2(0.0f);
130 }
131 uv_map.finish();
132 mesh->uv_maps_active_set("UVMap");
133 mesh->uv_maps_default_set("UVMap");
134 }
135
136 /* If we have custom vertex normals, set them
137 * (NOTE: important to do this after initializing the loops). */
138 bool set_custom_normals_for_verts = false;
139 if (!data.vertex_normals.is_empty()) {
140 if (!data.face_sizes.is_empty()) {
141 /* For a non-point-cloud mesh, set custom normals. */
142 /* Deferred because this relies on valid mesh data. */
143 set_custom_normals_for_verts = true;
144 }
145 else if (params.import_attributes) {
146 /* If we have no faces, add vertex normals as custom attribute. */
147 attributes.add<float3>(
148 "normal",
151 }
152 }
153 else {
154 /* No vertex normals: set faces to sharp. */
155 bke::mesh_smooth_set(*mesh, false);
156 }
157
158 /* Custom attributes: add them after anything above. */
159 if (params.import_attributes && !data.vertex_custom_attr.is_empty()) {
160 for (const PlyCustomAttribute &attr : data.vertex_custom_attr) {
161 attributes.add<float>(attr.name,
164 }
165 }
166
167 /* It's important to validate the mesh before using it's geometry to calculate derived data. */
168 {
169 const bool allow_missing_edges = true;
170#ifndef NDEBUG
171 const bool verbose_validate = true;
172#else
173 const bool verbose_validate = false;
174#endif
175 bke::mesh_validate(*mesh, verbose_validate, allow_missing_edges);
176 }
177
178 if (set_custom_normals_for_verts) {
180 }
181
182 /* Merge all vertices on the same location. */
183 if (params.merge_verts) {
184 std::optional<Mesh *> merged_mesh = geometry::mesh_merge_by_distance_all(
185 *mesh, IndexMask(mesh->verts_num), 0.0001f);
186 if (merged_mesh) {
187 BKE_id_free(nullptr, &mesh->id);
188 mesh = *merged_mesh;
189 }
190 }
191
192 success = true;
193 return mesh;
194}
195} // namespace io::ply
196} // namespace blender
Generic geometry attributes built on CustomData.
#define BLI_SCOPED_DEFER(function_to_defer)
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
static VArray from_span(Span< T > values)
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
#define PRId64
Definition inttypes.h:78
void mesh_smooth_set(Mesh &mesh, bool use_smooth, bool keep_sharp_edges=false)
void mesh_set_custom_normals_from_verts(Mesh &mesh, MutableSpan< float3 > vert_normals)
bool mesh_validate(Mesh &mesh, bool verbose=false, bool allow_missing_edges=false)
std::optional< Mesh * > mesh_merge_by_distance_all(const Mesh &mesh, const IndexMask &selection, float merge_distance, const bke::AttributeFilter &attribute_filter=bke::AttributeFilter::default_filter())
Mesh * convert_ply_to_mesh(PlyData &data, const PLYImportParams &params)
bool size_fits_in_int(const int64_t size)
bool index_in_range(const int64_t index, const int64_t size)
BMesh Iterators.
__int64 int64_t
Definition BKE_text.h:90
void BKE_id_attributes_default_color_set(struct ID *id, std::optional< StringRef > name)
MINLINE void srgb_to_linearrgb_v4(float linear[4], const float srgb[4])
ATTR_WARN_UNUSED_RESULT const BMVert * v
unsigned int uint32_t
Definition BKE_text.h:81
VecBase< float, 2 > float2
BMesh const char void * data
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)
void BKE_id_free(Main *bmain, void *idv)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
void BKE_id_attributes_active_color_set(struct ID *id, std::optional< StringRef > name)
signed int int32_t
Definition BKE_text.h:78
static CLG_LogRef LOG
Definition logging.cpp:12
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
MINLINE void copy_v4_v4(float r[4], const float a[4])
VecBase< float, 3 > float3
ePLYVertexColorMode vertex_colors
Definition IO_ply.hh:72