Blender V5.3
foreach_geometry.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_geometry_set.hh"
10#include "BKE_instances.hh"
11
14
15namespace blender::geometry {
16
19 Vector<int> &path,
20 Map<bke::GeometrySet, Vector<Vector<int>>> &r_real_geometries)
21{
22 bke::GeometrySet real_geometry = geometry;
25
26 r_real_geometries.lookup_or_add_default(std::move(real_geometry)).append(path);
27
28 bke::Instances *instances = geometry.get_instances_for_write();
29 if (!instances) {
30 return;
31 }
32 instances->ensure_geometry_instances();
34 for (const int i : references.index_range()) {
35 bke::InstanceReference &reference = references[i];
37 bke::GeometrySet &sub_geometry = reference.geometry_set();
38 path.append(i);
39 extract_real_geometries_recursive(sub_geometry, path, r_real_geometries);
40 path.pop_last();
41 }
42 }
43}
44
46 const bke::GeometrySet &geometry_to_insert,
47 const Span<int> path)
48{
49 if (path.is_empty()) {
50 /* Instance references must not be merged here as that could invalidate the paths. */
51 const bool allow_merging_instance_references = false;
52 /* Important to pass the old geometry first, so that the instance reference paths stay
53 * valid. */
55 {geometry, geometry_to_insert}, {}, {}, allow_merging_instance_references);
56 return;
57 }
58 bke::Instances *instances = geometry.get_instances_for_write();
59 BLI_assert(instances);
60 const int reference_i = path.first();
61 const MutableSpan<bke::InstanceReference> references = instances->references_for_write();
62 BLI_assert(reference_i < references.size());
63 bke::InstanceReference &reference = references[reference_i];
65 bke::GeometrySet &sub_geometry = reference.geometry_set();
66 reinsert_modified_geometry_recursive(sub_geometry, geometry_to_insert, path.drop_front(1));
67}
68
73
75 FunctionRef<void(bke::GeometrySet &geometry_set)> fn)
76{
77 /* Afterwards the geometry does not have realized geometry anymore. It has been extracted and
78 * will be reinserted afterwards. */
80 {
81 Vector<int> path;
82 extract_real_geometries_recursive(geometry, path, real_geometries);
83 }
84 /* Take the geometries out of the map so that they can be edited in-place. As keys in the #Map
85 * the geometries are const and thus can't be modified. */
86 Vector<GeometryWithPaths> geometries_with_paths;
87 for (auto &&item : real_geometries.items()) {
88 geometries_with_paths.append({item.key, std::move(item.value)});
89 }
90 /* Clear to avoid extra references to the geometries which prohibit editing them in-place. */
91 real_geometries.clear();
92
93 /* Actually modify the geometries in parallel. */
94 threading::parallel_for(geometries_with_paths.index_range(), 1, [&](const IndexRange range) {
95 for (const int i : range) {
96 bke::GeometrySet &geometry_to_modify = geometries_with_paths[i].geometry;
97 fn(geometry_to_modify);
98 }
99 });
100
101 /* Reinsert modified geometries. */
102 for (GeometryWithPaths &geometry_with_paths : geometries_with_paths) {
103 for (const Span<int> path : geometry_with_paths.paths) {
104 reinsert_modified_geometry_recursive(geometry, geometry_with_paths.geometry, path);
105 }
106 }
107}
108
109} // namespace blender::geometry
void clear()
Definition BLI_map.hh:1038
ItemIterator items() const &
Definition BLI_map.hh:902
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr Span drop_front(int64_t n) const
Definition BLI_span.hh:175
constexpr const T & first() const
Definition BLI_span.hh:319
constexpr bool is_empty() const
Definition BLI_span.hh:264
void append(const T &value)
IndexRange index_range() const
MutableSpan< InstanceReference > references_for_write()
Definition instances.cc:295
bke::GeometrySet join_geometries(Span< bke::GeometrySet > geometries, const bke::AttributeFilter &attribute_filter, const std::optional< Span< bke::GeometryComponent::Type > > &component_types_to_join=std::nullopt, bool allow_merging_instance_references=true)
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
static void reinsert_modified_geometry_recursive(bke::GeometrySet &geometry, const bke::GeometrySet &geometry_to_insert, const Span< int > path)
static void extract_real_geometries_recursive(bke::GeometrySet &geometry, Vector< int > &path, Map< bke::GeometrySet, Vector< Vector< int > > > &r_real_geometries)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void remove(const GeometryComponent::Type component_type)