From 0b80d5e7559354413632aaaf5b3fb3486c7a62c2 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Thu, 28 Mar 2024 14:45:52 -0400 Subject: [PATCH] Cleanup: Access sharp_face attribute with attribute API --- source/blender/blenkernel/BKE_shrinkwrap.hh | 3 ++- source/blender/blenkernel/intern/shrinkwrap.cc | 8 +++++--- source/blender/editors/mesh/mesh_data.cc | 6 +++--- source/blender/io/collada/MeshImporter.cpp | 13 ++++--------- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/source/blender/blenkernel/BKE_shrinkwrap.hh b/source/blender/blenkernel/BKE_shrinkwrap.hh index 165f35c2391..a3371c0ad45 100644 --- a/source/blender/blenkernel/BKE_shrinkwrap.hh +++ b/source/blender/blenkernel/BKE_shrinkwrap.hh @@ -15,6 +15,7 @@ #include "BLI_math_vector_types.hh" #include "BLI_offset_indices.hh" #include "BLI_span.hh" +#include "BLI_virtual_array.hh" /* * Shrinkwrap is composed by a set of functions and options that define the type of shrink. @@ -79,7 +80,7 @@ struct ShrinkwrapTreeData { blender::Span face_normals; blender::Span vert_normals; blender::Span corner_normals; - const bool *sharp_faces; + blender::VArraySpan sharp_faces; const ShrinkwrapBoundaryData *boundary; }; diff --git a/source/blender/blenkernel/intern/shrinkwrap.cc b/source/blender/blenkernel/intern/shrinkwrap.cc index e2d661b2b64..356646c0284 100644 --- a/source/blender/blenkernel/intern/shrinkwrap.cc +++ b/source/blender/blenkernel/intern/shrinkwrap.cc @@ -26,6 +26,7 @@ #include "BLI_utildefines.h" #include "BKE_DerivedMesh.hh" +#include "BKE_attribute.hh" #include "BKE_cdderivedmesh.h" #include "BKE_modifier.hh" #include "BKE_shrinkwrap.hh" @@ -96,6 +97,7 @@ bool BKE_shrinkwrap_needs_normals(int shrinkType, int shrinkMode) bool BKE_shrinkwrap_init_tree( ShrinkwrapTreeData *data, Mesh *mesh, int shrinkType, int shrinkMode, bool force_normals) { + using namespace blender::bke; *data = {}; if (mesh == nullptr) { @@ -115,8 +117,8 @@ bool BKE_shrinkwrap_init_tree( data->faces = mesh->faces(); data->corner_edges = mesh->corner_edges(); data->vert_normals = mesh->vert_normals(); - data->sharp_faces = static_cast( - CustomData_get_layer_named(&mesh->face_data, CD_PROP_BOOL, "sharp_face")); + const AttributeAccessor attributes = mesh->attributes(); + data->sharp_faces = *attributes.lookup("sharp_face", AttrDomain::Face); if (shrinkType == MOD_SHRINKWRAP_NEAREST_VERTEX) { data->bvh = BKE_bvhtree_from_mesh_get(&data->treeData, mesh, BVHTREE_FROM_VERTS, 2); @@ -1160,7 +1162,7 @@ void BKE_shrinkwrap_compute_smooth_normal(const ShrinkwrapTreeData *tree, const int face_i = tree->mesh->corner_tri_faces()[corner_tri_idx]; /* Interpolate smooth normals if enabled. */ - if (!(tree->sharp_faces && tree->sharp_faces[face_i])) { + if (tree->sharp_faces.is_empty() || tree->sharp_faces[face_i]) { const int vert_indices[3] = {treeData->corner_verts[tri[0]], treeData->corner_verts[tri[1]], treeData->corner_verts[tri[2]]}; diff --git a/source/blender/editors/mesh/mesh_data.cc b/source/blender/editors/mesh/mesh_data.cc index fd95f7ceaa3..2e5b767789d 100644 --- a/source/blender/editors/mesh/mesh_data.cc +++ b/source/blender/editors/mesh/mesh_data.cc @@ -1154,15 +1154,15 @@ void ED_mesh_split_faces(Mesh *mesh) const bke::AttributeAccessor attributes = mesh->attributes(); const VArray mesh_sharp_edges = *attributes.lookup_or_default( "sharp_edge", bke::AttrDomain::Edge, false); - const bool *sharp_faces = static_cast( - CustomData_get_layer_named(&mesh->face_data, CD_PROP_BOOL, "sharp_face")); + const VArraySpan sharp_faces = *attributes.lookup("sharp_face", + bke::AttrDomain::Face); Array sharp_edges(mesh->edges_num); mesh_sharp_edges.materialize(sharp_edges); threading::parallel_for(polys.index_range(), 1024, [&](const IndexRange range) { for (const int face_i : range) { - if (sharp_faces && sharp_faces[face_i]) { + if (!sharp_faces.is_empty() && sharp_faces[face_i]) { for (const int edge : corner_edges.slice(polys[face_i])) { sharp_edges[edge] = true; } diff --git a/source/blender/io/collada/MeshImporter.cpp b/source/blender/io/collada/MeshImporter.cpp index 3991a3825a5..c1bbf5802b5 100644 --- a/source/blender/io/collada/MeshImporter.cpp +++ b/source/blender/io/collada/MeshImporter.cpp @@ -622,13 +622,8 @@ void MeshImporter::read_polys(COLLADAFW::Mesh *collada_mesh, bke::MutableAttributeAccessor attributes = mesh->attributes_for_write(); bke::SpanAttributeWriter material_indices = attributes.lookup_or_add_for_write_span( "material_index", bke::AttrDomain::Face); - - bool *sharp_faces = static_cast(CustomData_get_layer_named_for_write( - &mesh->face_data, CD_PROP_BOOL, "sharp_face", mesh->faces_num)); - if (!sharp_faces) { - sharp_faces = static_cast(CustomData_add_layer_named( - &mesh->face_data, CD_PROP_BOOL, CD_SET_DEFAULT, mesh->faces_num, "sharp_face")); - } + bke::SpanAttributeWriter sharp_faces = attributes.lookup_or_add_for_write_span( + "sharp_face", bke::AttrDomain::Face); COLLADAFW::MeshPrimitiveArray &prim_arr = collada_mesh->getMeshPrimitives(); COLLADAFW::MeshVertexData &nor = collada_mesh->getNormals(); @@ -673,7 +668,7 @@ void MeshImporter::read_polys(COLLADAFW::Mesh *collada_mesh, if (mp_has_normals) { /* vertex normals, same implementation as for the triangles */ /* The same for vertices normals. */ uint vertex_normal_indices[3] = {first_normal, normal_indices[1], normal_indices[2]}; - sharp_faces[face_index] = is_flat_face(vertex_normal_indices, nor, 3); + sharp_faces.span[face_index] = is_flat_face(vertex_normal_indices, nor, 3); normal_indices++; } @@ -743,7 +738,7 @@ void MeshImporter::read_polys(COLLADAFW::Mesh *collada_mesh, if (mp_has_normals) { /* If it turns out that we have complete custom normals for each poly * and we want to use custom normals, this will be overridden. */ - sharp_faces[face_index] = is_flat_face(normal_indices, nor, vcount); + sharp_faces.span[face_index] = is_flat_face(normal_indices, nor, vcount); if (use_custom_normals) { /* Store the custom normals for later application. */