[Bf-blender-cvs] [6d97a5f93c7] master: Cleanup: Miscellaneous improvements to duplicate geometry node

Hans Goudey noreply at git.blender.org
Thu Mar 17 22:24:11 CET 2022


Commit: 6d97a5f93c77c9fdcd1b92b43b0c02a6ff623c9b
Author: Hans Goudey
Date:   Thu Mar 17 16:23:58 2022 -0500
Branches: master
https://developer.blender.org/rB6d97a5f93c77c9fdcd1b92b43b0c02a6ff623c9b

Cleanup: Miscellaneous improvements to duplicate geometry node

- Pass less redundant information in function arguments.
- Use `IndexRange` more instead of direct offset calculations.
- Use specific geometry component types for specialized functions.
- Use const arguments.
- Declare variables closer to where they are created or used.
- Remove some redundant logic.
- Simplify the description for the output geometry.

===================================================================

M	source/blender/nodes/geometry/nodes/node_geo_duplicate_elements.cc

===================================================================

diff --git a/source/blender/nodes/geometry/nodes/node_geo_duplicate_elements.cc b/source/blender/nodes/geometry/nodes/node_geo_duplicate_elements.cc
index 8beb6dcb5ce..518d7650a74 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_duplicate_elements.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_duplicate_elements.cc
@@ -34,8 +34,7 @@ static void node_declare(NodeDeclarationBuilder &b)
       .description(N_("The number of duplicates to create for each element"));
 
   b.add_output<decl::Geometry>(N_("Geometry"))
-      .description(
-          N_("The duplicated geometry only. The output does not contain the original geometry"));
+      .description(N_("The duplicated geometry, not including the original geometry"));
   b.add_output<decl::Int>(N_("Duplicate Index"))
       .field_source()
       .description(N_("The indices of the duplicates for each element"));
@@ -100,7 +99,7 @@ static void threaded_slice_fill(Span<int> offsets, Span<T> src, MutableSpan<T> d
   BLI_assert(offsets.last() == dst.size());
   threading::parallel_for(IndexRange(offsets.size() - 1), 512, [&](IndexRange range) {
     for (const int i : range) {
-      dst.slice(offsets[i], offsets[i + 1] - offsets[i]).fill(src[i]);
+      dst.slice(range_for_offsets_index(offsets, i)).fill(src[i]);
     }
   });
 }
@@ -182,8 +181,8 @@ static void copy_stable_id_point(const Span<int> offsets,
 static void copy_stable_id_edges(const Mesh &mesh,
                                  const IndexMask selection,
                                  const Span<int> edge_offsets,
-                                 const GeometryComponent &src_component,
-                                 GeometryComponent &dst_component)
+                                 const MeshComponent &src_component,
+                                 MeshComponent &dst_component)
 {
   ReadAttributeLookup src_attribute = src_component.attribute_try_get_for_read("id");
   if (!src_attribute) {
@@ -230,8 +229,8 @@ static void copy_stable_id_faces(const Mesh &mesh,
                                  const IndexMask selection,
                                  const Span<int> poly_offsets,
                                  const Span<int> vert_mapping,
-                                 const GeometryComponent &src_component,
-                                 GeometryComponent &dst_component)
+                                 const MeshComponent &src_component,
+                                 MeshComponent &dst_component)
 {
   ReadAttributeLookup src_attribute = src_component.attribute_try_get_for_read("id");
   if (!src_attribute) {
@@ -533,7 +532,7 @@ static void copy_face_attributes_without_id(GeometrySet &geometry_set,
 static void duplicate_splines(GeometrySet &geometry_set,
                               const Field<int> &count_field,
                               const Field<bool> &selection_field,
-                              IndexAttributes &attributes)
+                              const IndexAttributes &attributes)
 {
   if (!geometry_set.has_curves()) {
     geometry_set.keep_only({GEO_COMPONENT_TYPE_INSTANCES});
@@ -559,7 +558,7 @@ static void duplicate_splines(GeometrySet &geometry_set,
   int dst_splines_size = 0;
   int dst_points_size = 0;
   for (const int i_spline : selection.index_range()) {
-    int count = std::max(counts[selection[i_spline]], 0);
+    const int count = std::max(counts[selection[i_spline]], 0);
     curve_offsets[i_spline] = dst_splines_size;
     dst_splines_size += count;
     dst_points_size += count * curve->splines()[selection[i_spline]]->size();
@@ -605,7 +604,7 @@ static void duplicate_splines(GeometrySet &geometry_set,
 static void duplicate_faces(GeometrySet &geometry_set,
                             const Field<int> &count_field,
                             const Field<bool> &selection_field,
-                            IndexAttributes &attributes)
+                            const IndexAttributes &attributes)
 {
   if (!geometry_set.has_mesh()) {
     geometry_set.keep_only({GEO_COMPONENT_TYPE_INSTANCES});
@@ -613,25 +612,21 @@ static void duplicate_faces(GeometrySet &geometry_set,
   }
   geometry_set.keep_only({GEO_COMPONENT_TYPE_MESH, GEO_COMPONENT_TYPE_INSTANCES});
 
-  GeometryComponent &component = geometry_set.get_component_for_write(GEO_COMPONENT_TYPE_MESH);
-  const int domain_size = component.attribute_domain_size(ATTR_DOMAIN_FACE);
-
-  GeometryComponentFieldContext field_context{component, ATTR_DOMAIN_FACE};
-  FieldEvaluator evaluator(field_context, domain_size);
+  const MeshComponent &src_component = *geometry_set.get_component_for_read<MeshComponent>();
+  const Mesh &mesh = *src_component.get_for_read();
+  Span<MVert> verts(mesh.mvert, mesh.totvert);
+  Span<MEdge> edges(mesh.medge, mesh.totedge);
+  Span<MPoly> polys(mesh.mpoly, mesh.totpoly);
+  Span<MLoop> loops(mesh.mloop, mesh.totloop);
 
+  GeometryComponentFieldContext field_context{src_component, ATTR_DOMAIN_FACE};
+  FieldEvaluator evaluator(field_context, polys.size());
   evaluator.add(count_field);
   evaluator.set_selection(selection_field);
   evaluator.evaluate();
   const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
   const VArray<int> counts = evaluator.get_evaluated<int>(0);
 
-  MeshComponent &mesh_component = static_cast<MeshComponent &>(component);
-  const Mesh &mesh = *mesh_component.get_for_read();
-  Span<MVert> verts(mesh.mvert, mesh.totvert);
-  Span<MEdge> edges(mesh.medge, mesh.totedge);
-  Span<MPoly> polys(mesh.mpoly, mesh.totpoly);
-  Span<MLoop> loops(mesh.mloop, mesh.totloop);
-
   int total_polys = 0;
   int total_loops = 0;
   Array<int> offsets(selection.size() + 1);
@@ -643,17 +638,16 @@ static void duplicate_faces(GeometrySet &geometry_set,
   }
   offsets[selection.size()] = total_polys;
 
-  Array<int> vert_mapping(total_loops);
-  Array<int> edge_mapping(total_loops);
-  Array<int> loop_mapping(total_loops);
-
   Mesh *new_mesh = BKE_mesh_new_nomain(total_loops, total_loops, 0, total_loops, total_polys);
-
   MutableSpan<MVert> new_verts(new_mesh->mvert, new_mesh->totvert);
   MutableSpan<MEdge> new_edges(new_mesh->medge, new_mesh->totedge);
   MutableSpan<MLoop> new_loops(new_mesh->mloop, new_mesh->totloop);
   MutableSpan<MPoly> new_poly(new_mesh->mpoly, new_mesh->totpoly);
 
+  Array<int> vert_mapping(new_verts.size());
+  Array<int> edge_mapping(new_edges.size());
+  Array<int> loop_mapping(new_loops.size());
+
   int poly_index = 0;
   int loop_index = 0;
   for (const int i_selection : selection.index_range()) {
@@ -684,6 +678,7 @@ static void duplicate_faces(GeometrySet &geometry_set,
       poly_index++;
     }
   }
+
   MeshComponent dst_component;
   dst_component.replace(new_mesh, GeometryOwnershipType::Editable);
 
@@ -692,33 +687,35 @@ static void duplicate_faces(GeometrySet &geometry_set,
                                   vert_mapping,
                                   loop_mapping,
                                   offsets,
-                                  mesh_component,
+                                  src_component,
                                   dst_component);
 
-  copy_stable_id_faces(mesh, selection, offsets, vert_mapping, mesh_component, dst_component);
-  mesh_component.replace(dst_component.get_for_write());
+  copy_stable_id_faces(mesh, selection, offsets, vert_mapping, src_component, dst_component);
 
   if (attributes.duplicate_index) {
     create_duplicate_index_attribute(
         dst_component, ATTR_DOMAIN_FACE, selection, attributes, offsets);
   }
+
+  geometry_set.replace_mesh(new_mesh);
 }
 
 static void duplicate_edges(GeometrySet &geometry_set,
                             const Field<int> &count_field,
                             const Field<bool> &selection_field,
-                            IndexAttributes &attributes)
+                            const IndexAttributes &attributes)
 {
   if (!geometry_set.has_mesh()) {
     geometry_set.keep_only({GEO_COMPONENT_TYPE_INSTANCES});
     return;
   };
-  const GeometryComponent &src_component = *geometry_set.get_component_for_read(
-      GEO_COMPONENT_TYPE_MESH);
-  const int domain_size = src_component.attribute_domain_size(ATTR_DOMAIN_EDGE);
+  const MeshComponent &src_component = *geometry_set.get_component_for_read<MeshComponent>();
+  const Mesh &mesh = *src_component.get_for_read();
+  Span<MVert> verts(mesh.mvert, mesh.totvert);
+  Span<MEdge> edges(mesh.medge, mesh.totedge);
 
   GeometryComponentFieldContext field_context{src_component, ATTR_DOMAIN_EDGE};
-  FieldEvaluator evaluator{field_context, domain_size};
+  FieldEvaluator evaluator{field_context, edges.size()};
   evaluator.add(count_field);
   evaluator.set_selection(selection_field);
   evaluator.evaluate();
@@ -727,10 +724,6 @@ static void duplicate_edges(GeometrySet &geometry_set,
 
   Array<int> edge_offsets = accumulate_counts_to_offsets(selection, counts);
 
-  const Mesh *mesh = geometry_set.get_mesh_for_read();
-  Span<MVert> verts(mesh->mvert, mesh->totvert);
-  Span<MEdge> edges(mesh->medge, mesh->totedge);
-
   Mesh *new_mesh = BKE_mesh_new_nomain(edge_offsets.last() * 2, edge_offsets.last(), 0, 0, 0);
   MutableSpan<MVert> new_verts(new_mesh->mvert, new_mesh->totvert);
   MutableSpan<MEdge> new_edges(new_mesh->medge, new_mesh->totedge);
@@ -767,24 +760,22 @@ static void duplicate_edges(GeometrySet &geometry_set,
   copy_edge_attributes_without_id(
       geometry_set, vert_orig_indices, edge_offsets, src_component, dst_component);
 
-  copy_stable_id_edges(*mesh, selection, edge_offsets, src_component, dst_component);
+  copy_stable_id_edges(mesh, selection, edge_offsets, src_component, dst_component);
 
   if (attributes.duplicate_index) {
     create_duplicate_index_attribute(
         dst_component, ATTR_DOMAIN_EDGE, selection, attributes, edge_offsets);
   }
 
-  MeshComponent &mesh_component = geometry_set.get_component_for_write<MeshComponent>();
-  mesh_component.replace(dst_component.get_for_write());
+  geometry_set.replace_mesh(new_mesh);
 }
 
-static void duplicate_points_curve(const GeometryComponentType component_type,
+static void duplicate_points_curve(GeometrySet &geometry_set,
                                    const Field<int> &c

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list