Cleanup: Move mesh utility to create mesh without attributes

Mesh with no attributes is used to build new mesh from scratch.
Some data can be shared, so there is no reason to have allocated attributes.

Pull Request: https://projects.blender.org/blender/blender/pulls/118297
This commit is contained in:
Iliya Katueshenock 2024-02-14 23:14:12 +01:00 committed by Hans Goudey
parent 33af56f13e
commit faf056f17b
3 changed files with 32 additions and 25 deletions

View File

@ -306,6 +306,9 @@ inline int edge_other_vert(const int2 edge, const int vert)
} // namespace mesh
/** Create a mesh with no built-in attributes. */
Mesh *mesh_new_no_attributes(int verts_num, int edges_num, int faces_num, int corners_num);
/** Calculate edges from faces. */
void mesh_calc_edges(Mesh &mesh, bool keep_existing_edges, bool select_new_edges);

View File

@ -702,6 +702,26 @@ Mesh *BKE_mesh_new_nomain(const int verts_num,
return mesh;
}
namespace blender::bke {
Mesh *mesh_new_no_attributes(const int verts_num,
const int edges_num,
const int faces_num,
const int corners_num)
{
Mesh *mesh = BKE_mesh_new_nomain(0, 0, faces_num, 0);
mesh->verts_num = verts_num;
mesh->edges_num = edges_num;
mesh->corners_num = corners_num;
CustomData_free_layer_named(&mesh->vert_data, "position", 0);
CustomData_free_layer_named(&mesh->edge_data, ".edge_verts", 0);
CustomData_free_layer_named(&mesh->corner_data, ".corner_vert", 0);
CustomData_free_layer_named(&mesh->corner_data, ".corner_edge", 0);
return mesh;
}
} // namespace blender::bke
static void copy_attribute_names(const Mesh &mesh_src, Mesh &mesh_dst)
{
if (mesh_src.active_color_attribute) {

View File

@ -70,25 +70,6 @@ static void remap_edges(const OffsetIndices<int> src_faces,
});
}
/** Create a mesh with no built-in attributes. */
static Mesh *create_mesh_no_attributes(const Mesh &params_mesh,
const int verts_num,
const int edges_num,
const int faces_num,
const int corners_num)
{
Mesh *mesh = BKE_mesh_new_nomain(0, 0, faces_num, 0);
mesh->verts_num = verts_num;
mesh->edges_num = edges_num;
mesh->corners_num = corners_num;
CustomData_free_layer_named(&mesh->vert_data, "position", 0);
CustomData_free_layer_named(&mesh->edge_data, ".edge_verts", 0);
CustomData_free_layer_named(&mesh->corner_data, ".corner_vert", 0);
CustomData_free_layer_named(&mesh->corner_data, ".corner_edge", 0);
BKE_mesh_copy_parameters_for_eval(mesh, &params_mesh);
return mesh;
}
static void copy_loose_vert_hint(const Mesh &src, Mesh &dst)
{
const auto &src_cache = src.runtime->loose_verts_cache;
@ -218,8 +199,9 @@ std::optional<Mesh *> mesh_copy_selection(
return std::nullopt;
}
Mesh *dst_mesh = create_mesh_no_attributes(
src_mesh, vert_mask.size(), edge_mask.size(), face_mask.size(), 0);
Mesh *dst_mesh = bke::mesh_new_no_attributes(
vert_mask.size(), edge_mask.size(), face_mask.size(), 0);
BKE_mesh_copy_parameters_for_eval(dst_mesh, &src_mesh);
bke::MutableAttributeAccessor dst_attributes = dst_mesh->attributes_for_write();
dst_attributes.add<int2>(".edge_verts", bke::AttrDomain::Edge, bke::AttributeInitConstruct());
MutableSpan<int2> dst_edges = dst_mesh->edges_for_write();
@ -351,8 +333,9 @@ std::optional<Mesh *> mesh_copy_selection_keep_verts(
return std::nullopt;
}
Mesh *dst_mesh = create_mesh_no_attributes(
src_mesh, src_mesh.verts_num, edge_mask.size(), face_mask.size(), 0);
Mesh *dst_mesh = bke::mesh_new_no_attributes(
src_mesh.verts_num, edge_mask.size(), face_mask.size(), 0);
BKE_mesh_copy_parameters_for_eval(dst_mesh, &src_mesh);
bke::MutableAttributeAccessor dst_attributes = dst_mesh->attributes_for_write();
const OffsetIndices<int> dst_faces = offset_indices::gather_selected_offsets(
@ -443,8 +426,9 @@ std::optional<Mesh *> mesh_copy_selection_keep_edges(
return std::nullopt;
}
Mesh *dst_mesh = create_mesh_no_attributes(
src_mesh, src_mesh.verts_num, src_mesh.edges_num, face_mask.size(), 0);
Mesh *dst_mesh = bke::mesh_new_no_attributes(
src_mesh.verts_num, src_mesh.edges_num, face_mask.size(), 0);
BKE_mesh_copy_parameters_for_eval(dst_mesh, &src_mesh);
bke::MutableAttributeAccessor dst_attributes = dst_mesh->attributes_for_write();
const OffsetIndices<int> dst_faces = offset_indices::gather_selected_offsets(