Blender V5.3
pointcloud_attributes.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
12#include "BKE_pointcloud.hh"
13
15
16namespace blender::bke {
17
18static void tag_position_changed(void *owner)
19{
20 PointCloud &points = *static_cast<PointCloud *>(owner);
21 points.tag_positions_changed();
22}
23
24static void tag_radius_changed(void *owner)
25{
26 PointCloud &points = *static_cast<PointCloud *>(owner);
27 points.tag_radii_changed();
28}
29
30static const auto &changed_tags()
31{
32 static Map<StringRef, AttrUpdateOnChange> attributes{{"position", tag_position_changed},
33 {"radius", tag_radius_changed}};
34 return attributes;
35}
36
37static const auto &builtin_attributes()
38{
39 static auto attributes = []() {
41
43 position.deletable = false;
44 map.add_new("position", std::move(position));
45
47 map.add_new("radius", std::move(radius));
48
49 return map;
50 }();
51 return attributes;
52}
53
54static const auto &array_storage_required()
55{
56 static Set<StringRef> attributes{"position"};
57 return attributes;
58}
59
61{
63 fn.domain_supported = [](const void * /*owner*/, const AttrDomain domain) {
64 return domain == AttrDomain::Point;
65 };
66 fn.domain_size = [](const void *owner, const AttrDomain domain) {
67 return domain == AttrDomain::Point ? static_cast<const PointCloud *>(owner)->totpoint : 0;
68 };
69 fn.builtin_domain_and_type = [](const void * /*owner*/,
70 const StringRef name) -> std::optional<AttributeDomainAndType> {
71 const AttrBuiltinInfo *info = builtin_attributes().lookup_ptr(name);
72 if (!info) {
73 return std::nullopt;
74 }
75 return AttributeDomainAndType{info->domain, info->type};
76 };
77 fn.get_builtin_default = [](const void * /*owner*/, StringRef name) -> GPointer {
78 const AttrBuiltinInfo &info = builtin_attributes().lookup(name);
79 return info.default_value;
80 };
81 fn.lookup_meta_data = [](const void *owner, StringRef name) -> std::optional<AttributeMetaData> {
82 const PointCloud &pointcloud = *static_cast<const PointCloud *>(owner);
83 const AttributeStorage &storage = pointcloud.attribute_storage.wrap();
84 const Attribute *attr = storage.lookup(name);
85 if (!attr) {
86 return std::nullopt;
87 }
88 return AttributeMetaData{attr->domain(), attr->data_type()};
89 };
90 fn.lookup = [](const void *owner, const StringRef name) -> GAttributeReader {
91 const PointCloud &pointcloud = *static_cast<const PointCloud *>(owner);
92 const AttributeStorage &storage = pointcloud.attribute_storage.wrap();
93 const Attribute *attribute = storage.lookup(name);
94 if (!attribute) {
95 return {};
96 }
98 };
99 fn.adapt_domain = [](const void * /*owner*/,
100 const GVArray &varray,
101 const AttrDomain from_domain,
102 const AttrDomain to_domain) {
103 if (from_domain == to_domain && from_domain == AttrDomain::Point) {
104 return varray;
105 }
106 return GVArray{};
107 };
108 fn.foreach_attribute = [](const void *owner,
109 const FunctionRef<void(const AttributeIter &)> fn,
110 const AttributeAccessor &accessor) {
111 const PointCloud &pointcloud = *static_cast<const PointCloud *>(owner);
112 const AttributeStorage &storage = pointcloud.attribute_storage.wrap();
113 for (const Attribute &attribute : storage) {
114 const auto get_fn = [&]() {
116 };
117 AttributeIter iter(attribute.name(), attribute.domain(), attribute.data_type(), get_fn);
118 iter.is_builtin = builtin_attributes().contains(attribute.name());
119 iter.storage_type = attribute.storage_type();
120 iter.accessor = &accessor;
121 fn(iter);
122 if (iter.is_stopped()) {
123 break;
124 }
125 }
126 };
127 fn.lookup_validator = [](const void * /*owner*/, const StringRef name) -> AttributeValidator {
128 const AttrBuiltinInfo *info = builtin_attributes().lookup_ptr(name);
129 if (!info) {
130 return {};
131 }
132 return info->validator;
133 };
134 fn.lookup_for_write = [](void *owner, const StringRef name) -> GAttributeWriter {
135 PointCloud &pointcloud = *static_cast<PointCloud *>(owner);
136 AttributeStorage &storage = pointcloud.attribute_storage.wrap();
137 Attribute *attribute = storage.lookup(name);
138 if (!attribute) {
139 return {};
140 }
141 return attribute_to_writer(&pointcloud, changed_tags(), pointcloud.totpoint, *attribute);
142 };
143 fn.remove = [](void *owner, const StringRef name) -> bool {
144 PointCloud &pointcloud = *static_cast<PointCloud *>(owner);
145 AttributeStorage &storage = pointcloud.attribute_storage.wrap();
146 if (const AttrBuiltinInfo *info = builtin_attributes().lookup_ptr(name)) {
147 if (!info->deletable) {
148 return false;
149 }
150 }
151 const std::optional<AttrUpdateOnChange> fn = changed_tags().lookup_try(name);
152 const bool removed = storage.remove(name);
153 if (!removed) {
154 return false;
155 }
156 if (fn) {
157 (*fn)(owner);
158 }
159 return true;
160 };
161 fn.add = [](void *owner,
162 const StringRef name,
163 const AttrDomain domain,
164 const bke::AttrType type,
165 const AttributeInit &initializer) {
166 PointCloud &pointcloud = *static_cast<PointCloud *>(owner);
167 const int domain_size = pointcloud.totpoint;
168 AttributeStorage &storage = pointcloud.attribute_storage.wrap();
169 if (const AttrBuiltinInfo *info = builtin_attributes().lookup_ptr(name)) {
170 if (info->domain != domain || info->type != type) {
171 return false;
172 }
173 }
174 if (storage.lookup(name)) {
175 return false;
176 }
178 type, domain_size, initializer, array_storage_required().contains(name));
179 storage.add(name, domain, type, std::move(data));
180 if (initializer.type != AttributeInit::Type::Construct) {
181 if (const std::optional<AttrUpdateOnChange> fn = changed_tags().lookup_try(name)) {
182 (*fn)(owner);
183 }
184 }
185 return true;
186 };
187 fn.rename = [](void *owner,
188 const Map<StringRef, StringRef> &name_map,
189 bool overwrite) -> Set<StringRef> {
190 PointCloud &pointcloud = *static_cast<PointCloud *>(owner);
191 return rename_attributes(pointcloud.attribute_storage.wrap(),
192 name_map,
193 overwrite,
196 [&](const bke::AttrDomain /*domain*/) { return pointcloud.totpoint; },
197 std::nullopt,
198 {});
199 };
200 fn.assign_data = [](void *owner, StringRef name, const AttributeInit &initializer) {
201 PointCloud &pointcloud = *static_cast<PointCloud *>(owner);
202 AttributeStorage &storage = pointcloud.attribute_storage.wrap();
203 Attribute *attr = storage.lookup(name);
204 if (!attr) {
205 return false;
206 }
208 pointcloud.totpoint,
209 initializer,
210 array_storage_required().contains(name));
211 attr->assign_data(std::move(data));
212 if (initializer.type != AttributeInit::Type::Construct) {
213 if (const std::optional<AttrUpdateOnChange> fn = changed_tags().lookup_try(name)) {
214 (*fn)(owner);
215 }
216 }
217 return true;
218 };
219
220 return fn;
221}
222
228
229} // namespace blender::bke
General operations for point clouds.
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
std::optional< AttrStorageType > storage_type
const AttributeAccessor * accessor
std::variant< ArrayData, SingleData > DataVariant
void assign_data(DataVariant &&data)
#define storage(slot, qualifiers)
#define position
#define attribute(slot)
static constexpr AttributeAccessorFunctions get_pointcloud_accessor_functions()
GAttributeReader attribute_to_reader(const Attribute &attribute, const AttrDomain domain, const int64_t domain_size)
static const auto & array_storage_required()
Set< StringRef > rename_attributes(AttributeStorage &storage, const Map< StringRef, StringRef > &name_map, const bool overwrite, const Map< StringRef, AttrBuiltinInfo > &builtin_attributes, const Set< StringRef > &array_storage_required, const FunctionRef< int(AttrDomain)> domain_size_fn, std::optional< ListBaseT< bDeformGroup > * > vertex_groups, FunctionRef< MutableSpan< MDeformVert >()> get_mutable_dverts)
GAttributeWriter attribute_to_writer(void *owner, const Map< StringRef, AttrUpdateOnChange > &changed_tags, const int64_t domain_size, Attribute &attribute)
static const auto & builtin_attributes()
const AttributeAccessorFunctions & pointcloud_attribute_accessor_functions()
Attribute::DataVariant attribute_init_to_data(const bke::AttrType data_type, const int64_t domain_size, const AttributeInit &initializer, const bool require_array_data)
static void tag_radius_changed(void *owner)
static const auto & changed_tags()
static void tag_position_changed(void *owner)
BMesh const char void * data
struct AttributeStorage attribute_storage