Blender V5.3
curves_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
9#include "BLI_listbase.hh"
10
11#include "DNA_object_types.h"
12
14#include "BKE_curves.hh"
15#include "BKE_deform.hh"
16
18
20
21namespace blender::bke::curves {
22
23static void tag_topology_changed(void *owner)
24{
25 CurvesGeometry &curves = *static_cast<CurvesGeometry *>(owner);
26 curves.tag_topology_changed();
27}
28
29static void tag_curve_types_changed(void *owner)
30{
31 CurvesGeometry &curves = *static_cast<CurvesGeometry *>(owner);
32 curves.update_curve_types();
33 curves.tag_topology_changed();
34}
35
36static void tag_positions_changed(void *owner)
37{
38 CurvesGeometry &curves = *static_cast<CurvesGeometry *>(owner);
39 curves.tag_positions_changed();
40}
41
42static void tag_radii_changed(void *owner)
43{
44 CurvesGeometry &curves = *static_cast<CurvesGeometry *>(owner);
45 curves.tag_radii_changed();
46}
47
48static void tag_normals_changed(void *owner)
49{
50 CurvesGeometry &curves = *static_cast<CurvesGeometry *>(owner);
51 curves.tag_normals_changed();
52}
53
54static void tag_material_index_changed(void *owner)
55{
56 CurvesGeometry &curves = *static_cast<CurvesGeometry *>(owner);
57 curves.tag_material_index_changed();
58}
59
60static const auto &changed_tags()
61{
62 static Map<StringRef, AttrUpdateOnChange> attributes{
63 {"position", tag_positions_changed},
64 {"radius", tag_radii_changed},
65 {"tilt", tag_normals_changed},
66 {"handle_left", tag_positions_changed},
67 {"handle_right", tag_positions_changed},
68 {"handle_type_left", tag_topology_changed},
69 {"handle_type_right", tag_topology_changed},
70 {"nurbs_weight", tag_positions_changed},
71 {"nurbs_order", tag_topology_changed},
72 {"normal_mode", tag_normals_changed},
73 {"custom_normal", tag_normals_changed},
74 {"curve_type", tag_curve_types_changed},
75 {"resolution", tag_topology_changed},
76 {"cyclic", tag_topology_changed},
77 {"material_index", tag_material_index_changed},
78 };
79 return attributes;
80}
81
82static int get_domain_size(const void *owner, const AttrDomain domain)
83{
84 const CurvesGeometry &curves = *static_cast<const CurvesGeometry *>(owner);
85 switch (domain) {
87 return curves.points_num();
89 return curves.curves_num();
90 default:
91 return 0;
92 }
93}
94
96 const Span<MDeformVert> dverts,
97 const int vertex_group_index)
98{
99 BLI_assert(vertex_group_index >= 0);
100 if (dverts.is_empty()) {
101 return {VArray<float>::from_single(0.0f, curves.points_num()), AttrDomain::Point};
102 }
103 return {varray_for_deform_verts(dverts, vertex_group_index), AttrDomain::Point};
104}
105
106static GAttributeReader try_get_vertex_group(const void *owner, const StringRef name)
107{
108 const CurvesGeometry *curves = static_cast<const CurvesGeometry *>(owner);
109 if (curves == nullptr) {
110 return {};
111 }
112 const int vertex_group_index = BKE_defgroup_name_index(&curves->vertex_group_names, name);
113 if (vertex_group_index < 0) {
114 return {};
115 }
116 const Span<MDeformVert> dverts = curves->deform_verts();
117 return reader_for_vertex_group_index(*curves, dverts, vertex_group_index);
118}
119
121{
122 CurvesGeometry *curves = static_cast<CurvesGeometry *>(owner);
123 if (curves == nullptr) {
124 return {};
125 }
126 const int vertex_group_index = BKE_defgroup_name_index(&curves->vertex_group_names, name);
127 if (vertex_group_index < 0) {
128 return {};
129 }
130 MutableSpan<MDeformVert> dverts = curves->deform_verts_for_write();
131 return {varray_for_mutable_deform_verts(dverts, vertex_group_index), AttrDomain::Point};
132}
133
134static bool foreach_vertex_group(const void *owner, FunctionRef<void(const AttributeIter &)> fn)
135{
136 const CurvesGeometry *curves = static_cast<const CurvesGeometry *>(owner);
137 if (curves == nullptr) {
138 return true;
139 }
140 const AttributeAccessor accessor = curves->attributes();
141 const Span<MDeformVert> dverts = curves->deform_verts();
142
143 for (const auto [group_index, group] : curves->vertex_group_names.enumerate()) {
144 const auto get_fn = [&, group_index = group_index]() {
145 return reader_for_vertex_group_index(*curves, dverts, group_index);
146 };
147 AttributeIter iter{group.name, AttrDomain::Point, bke::AttrType::Float, get_fn};
148 iter.is_builtin = false;
149 iter.accessor = &accessor;
150 fn(iter);
151 if (iter.is_stopped()) {
152 return false;
153 }
154 }
155 return true;
156}
157
158static const auto &builtin_attributes()
159{
160 static auto attributes = []() {
162
164 position.deletable = false;
165 map.add_new("position", std::move(position));
166
168 map.add_new("radius", std::move(radius));
169
171 map.add_new("tilt", std::move(tilt));
172
174 map.add_new("handle_left", std::move(handle_left));
175
177 map.add_new("handle_right", std::move(handle_right));
178
179 static auto handle_type_clamp = mf::build::SI1_SO<int8_t, int8_t>(
180 "Handle Type Validate",
181 [](int8_t value) {
182 return std::clamp<int8_t>(value, BEZIER_HANDLE_FREE, BEZIER_HANDLE_ALIGN);
183 },
184 mf::build::exec_presets::AllSpanOrSingle());
185
187 handle_type_left.validator = AttributeValidator{&handle_type_clamp};
188 map.add_new("handle_type_left", std::move(handle_type_left));
189
191 handle_type_right.validator = AttributeValidator{&handle_type_clamp};
192 map.add_new("handle_type_right", std::move(handle_type_right));
193
194 static float default_nurbs_weight = 1.0f;
196 nurbs_weight.default_value = &default_nurbs_weight;
197 map.add_new("nurbs_weight", std::move(nurbs_weight));
198
199 static const auto nurbs_order_clamp = mf::build::SI1_SO<int8_t, int8_t>(
200 "NURBS Order Validate",
201 [](int8_t value) { return std::max<int8_t>(value, 1); },
202 mf::build::exec_presets::AllSpanOrSingle());
203 static int nurbs_order_default = 4;
205 nurbs_order.default_value = &nurbs_order_default;
206 nurbs_order.validator = AttributeValidator{&nurbs_order_clamp};
207 map.add_new("nurbs_order", std::move(nurbs_order));
208
209 static const auto normal_mode_clamp = mf::build::SI1_SO<int8_t, int8_t>(
210 "Normal Mode Validate",
211 [](int8_t value) {
212 return std::clamp<int8_t>(value, NORMAL_MODE_MINIMUM_TWIST, NORMAL_MODE_FREE);
213 },
214 mf::build::exec_presets::AllSpanOrSingle());
216 normal_mode.validator = AttributeValidator{&normal_mode_clamp};
217 map.add_new("normal_mode", std::move(normal_mode));
218
220 map.add_new("custom_normal", std::move(custom_normal));
221
222 static const auto knots_mode_clamp = mf::build::SI1_SO<int8_t, int8_t>(
223 "Knots Mode Validate",
224 [](int8_t value) {
225 return std::clamp<int8_t>(
227 },
228 mf::build::exec_presets::AllSpanOrSingle());
230 knots_mode.validator = AttributeValidator{&knots_mode_clamp};
231 map.add_new("knots_mode", std::move(knots_mode));
232
233 static const auto curve_type_clamp = mf::build::SI1_SO<int8_t, int8_t>(
234 "Curve Type Validate",
235 [](int8_t value) {
236 return std::clamp<int8_t>(value, CURVE_TYPE_CATMULL_ROM, CURVE_TYPE_NURBS);
237 },
238 mf::build::exec_presets::AllSpanOrSingle());
240 curve_type.validator = AttributeValidator{&curve_type_clamp};
241 map.add_new("curve_type", std::move(curve_type));
242
243 static const auto resolution_clamp = mf::build::SI1_SO<int, int>(
244 "Resolution Validate",
245 [](int value) { return std::max<int>(value, 1); },
246 mf::build::exec_presets::AllSpanOrSingle());
247 static int resolution_default = 12;
249 resolution.default_value = &resolution_default;
250 resolution.validator = AttributeValidator{&resolution_clamp};
251 map.add_new("resolution", std::move(resolution));
252
254 map.add_new("cyclic", std::move(cyclic));
255
256 static const auto material_index_clamp = mf::build::SI1_SO<int, int>(
257 "Material Index Validate",
258 [](int value) {
259 /* Use #short for the maximum since many areas still use that type for indices. */
260 return std::clamp<int>(value, 0, std::numeric_limits<short>::max());
261 },
262 mf::build::exec_presets::AllSpanOrSingle());
264 material_index.validator = AttributeValidator{&material_index_clamp};
265 map.add_new("material_index", std::move(material_index));
266
267 return map;
268 }();
269 return attributes;
270}
271
272static const auto &array_storage_required()
273{
274 static Set<StringRef> attributes{
275 "position", "handle_left", "handle_right", "nurbs_weight", "surface_uv_coordinate"};
276 return attributes;
277}
278
280{
282 fn.domain_supported = [](const void * /*owner*/, const AttrDomain domain) {
284 };
285 fn.domain_size = get_domain_size;
286 fn.builtin_domain_and_type = [](const void * /*owner*/,
287 const StringRef name) -> std::optional<AttributeDomainAndType> {
288 const AttrBuiltinInfo *info = builtin_attributes().lookup_ptr(name);
289 if (!info) {
290 return std::nullopt;
291 }
292 return AttributeDomainAndType{info->domain, info->type};
293 };
294 fn.get_builtin_default = [](const void * /*owner*/, StringRef name) -> GPointer {
295 const AttrBuiltinInfo &info = builtin_attributes().lookup(name);
296 return info.default_value;
297 };
298 fn.lookup_meta_data = [](const void *owner, StringRef name) -> std::optional<AttributeMetaData> {
299 const CurvesGeometry &curves = *static_cast<const CurvesGeometry *>(owner);
300 if (BKE_defgroup_name_index(&curves.vertex_group_names, name) != -1) {
302 }
303 const AttributeStorage &storage = curves.attribute_storage.wrap();
304 const Attribute *attr = storage.lookup(name);
305 if (!attr) {
306 return std::nullopt;
307 }
308 return AttributeMetaData{attr->domain(), attr->data_type()};
309 };
310 fn.lookup = [](const void *owner, const StringRef name) -> GAttributeReader {
311 const CurvesGeometry &curves = *static_cast<const CurvesGeometry *>(owner);
312
313 if (GAttributeReader vertex_group = try_get_vertex_group(owner, name)) {
314 return vertex_group;
315 }
316
317 const AttributeStorage &storage = curves.attribute_storage.wrap();
318 const Attribute *attr = storage.lookup(name);
319 if (!attr) {
320 return {};
321 }
322 const int domain_size = get_domain_size(owner, attr->domain());
323 return attribute_to_reader(*attr, attr->domain(), domain_size);
324 };
325 fn.adapt_domain = [](const void *owner,
326 const GVArray &varray,
327 const AttrDomain from_domain,
328 const AttrDomain to_domain) -> GVArray {
329 const CurvesGeometry &curves = *static_cast<const CurvesGeometry *>(owner);
330 return curves.adapt_domain(varray, from_domain, to_domain);
331 };
332 fn.foreach_attribute = [](const void *owner,
333 const FunctionRef<void(const AttributeIter &)> fn,
334 const AttributeAccessor &accessor) {
335 const CurvesGeometry &curves = *static_cast<const CurvesGeometry *>(owner);
336
337 const bool should_continue = foreach_vertex_group(owner, fn);
338 if (!should_continue) {
339 return;
340 }
341
342 const AttributeStorage &storage = curves.attribute_storage.wrap();
343 for (const Attribute &attr : storage) {
344 const auto get_fn = [&]() {
345 const int domain_size = get_domain_size(owner, attr.domain());
346 return attribute_to_reader(attr, attr.domain(), domain_size);
347 };
348 AttributeIter iter(attr.name(), attr.domain(), attr.data_type(), get_fn);
349 iter.is_builtin = builtin_attributes().contains(attr.name());
350 iter.storage_type = attr.storage_type();
351 iter.accessor = &accessor;
352 fn(iter);
353 if (iter.is_stopped()) {
354 break;
355 }
356 }
357 };
358 fn.lookup_validator = [](const void * /*owner*/, const StringRef name) -> AttributeValidator {
359 const AttrBuiltinInfo *info = builtin_attributes().lookup_ptr(name);
360 if (!info) {
361 return {};
362 }
363 return info->validator;
364 };
365 fn.lookup_for_write = [](void *owner, const StringRef name) -> GAttributeWriter {
366 CurvesGeometry &curves = *static_cast<CurvesGeometry *>(owner);
367
368 if (GAttributeWriter vertex_group = try_get_vertex_group_for_write(owner, name)) {
369 return vertex_group;
370 }
371
372 AttributeStorage &storage = curves.attribute_storage.wrap();
373 Attribute *attr = storage.lookup(name);
374 if (!attr) {
375 return {};
376 }
377 const int domain_size = get_domain_size(owner, attr->domain());
378 return attribute_to_writer(&curves, changed_tags(), domain_size, *attr);
379 };
380 fn.remove = [](void *owner, const StringRef name) -> bool {
381 CurvesGeometry &curves = *static_cast<CurvesGeometry *>(owner);
382
384 curves.vertex_group_names, name, [&]() { return curves.deform_verts_for_write(); }))
385 {
386 return true;
387 }
388
389 AttributeStorage &storage = curves.attribute_storage.wrap();
390 if (const AttrBuiltinInfo *info = builtin_attributes().lookup_ptr(name)) {
391 if (!info->deletable) {
392 return false;
393 }
394 }
395 const std::optional<AttrUpdateOnChange> fn = changed_tags().lookup_try(name);
396 const bool removed = storage.remove(name);
397 if (!removed) {
398 return false;
399 }
400 if (fn) {
401 (*fn)(owner);
402 }
403 return true;
404 };
405 fn.add = [](void *owner,
406 const StringRef name,
407 const AttrDomain domain,
408 const AttrType type,
409 const AttributeInit &initializer) {
410 CurvesGeometry &curves = *static_cast<CurvesGeometry *>(owner);
411 const int domain_size = get_domain_size(owner, domain);
412 AttributeStorage &storage = curves.attribute_storage.wrap();
413 if (const AttrBuiltinInfo *info = builtin_attributes().lookup_ptr(name)) {
414 if (info->domain != domain || info->type != type) {
415 return false;
416 }
417 }
418 if (storage.lookup(name)) {
419 return false;
420 }
421 const bool array = array_storage_required().contains(name);
422 Attribute::DataVariant data = attribute_init_to_data(type, domain_size, initializer, array);
423 storage.add(name, domain, type, std::move(data));
424 if (initializer.type != AttributeInit::Type::Construct) {
425 if (const std::optional<AttrUpdateOnChange> fn = changed_tags().lookup_try(name)) {
426 (*fn)(owner);
427 }
428 }
429 return true;
430 };
431 fn.rename = [](void *owner, const Map<StringRef, StringRef> &name_map, bool overwrite) {
432 CurvesGeometry &curves = *static_cast<CurvesGeometry *>(owner);
433 return rename_attributes(
434 curves.attribute_storage.wrap(),
435 name_map,
436 overwrite,
439 [&](const bke::AttrDomain domain) { return get_domain_size(owner, domain); },
440 &curves.vertex_group_names,
441 [&]() { return curves.deform_verts_for_write(); });
442 };
443 fn.assign_data = [](void *owner, StringRef name, const AttributeInit &initializer) {
444 CurvesGeometry &curves = *static_cast<CurvesGeometry *>(owner);
445 AttributeStorage &storage = curves.attribute_storage.wrap();
446 Attribute *attr = storage.lookup(name);
447 if (!attr) {
448 return false;
449 }
451 get_domain_size(owner, attr->domain()),
452 initializer,
453 array_storage_required().contains(name));
454 attr->assign_data(std::move(data));
455 if (initializer.type != AttributeInit::Type::Construct) {
456 if (const std::optional<AttrUpdateOnChange> fn = changed_tags().lookup_try(name)) {
457 (*fn)(owner);
458 }
459 }
460 return true;
461 };
462
463 return fn;
464}
465
471
472} // namespace blender::bke::curves
Low-level operations for curves.
support for deformation groups and hooks.
Object is a sort of wrapper for general info.
#define ELEM(...)
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
constexpr bool is_empty() const
Definition BLI_span.hh:264
static VArray from_single(T value, const int64_t size)
std::optional< AttrStorageType > storage_type
const AttributeAccessor * accessor
std::variant< ArrayData, SingleData > DataVariant
void assign_data(DataVariant &&data)
#define storage(slot, qualifiers)
#define position
static void tag_curve_types_changed(void *owner)
static GAttributeWriter try_get_vertex_group_for_write(void *owner, const StringRef name)
static void tag_topology_changed(void *owner)
static bool foreach_vertex_group(const void *owner, FunctionRef< void(const AttributeIter &)> fn)
static void tag_material_index_changed(void *owner)
static void tag_normals_changed(void *owner)
static const auto & changed_tags()
static void tag_radii_changed(void *owner)
static void tag_positions_changed(void *owner)
static int get_domain_size(const void *owner, const AttrDomain domain)
static const auto & builtin_attributes()
const AttributeAccessorFunctions & get_attribute_accessor_functions()
static GAttributeReader try_get_vertex_group(const void *owner, const StringRef name)
static GAttributeReader reader_for_vertex_group_index(const CurvesGeometry &curves, const Span< MDeformVert > dverts, const int vertex_group_index)
static const auto & array_storage_required()
static AttributeAccessorFunctions get_curves_accessor_functions()
GAttributeReader attribute_to_reader(const Attribute &attribute, const AttrDomain domain, const int64_t domain_size)
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)
bool try_delete_vertex_group(ListBaseT< bDeformGroup > &vertex_groups, const StringRef name, FunctionRef< MutableSpan< MDeformVert >()> get_mutable_dverts)
Attribute::DataVariant attribute_init_to_data(const bke::AttrType data_type, const int64_t domain_size, const AttributeInit &initializer, const bool require_array_data)
VMutableArray< float > varray_for_mutable_deform_verts(MutableSpan< MDeformVert > dverts, int defgroup_index)
Definition deform.cc:1752
VArray< float > varray_for_deform_verts(Span< MDeformVert > dverts, int defgroup_index)
Definition deform.cc:1748
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int BKE_defgroup_name_index(const ListBaseT< bDeformGroup > *defbase, StringRef name)
Definition deform.cc:544
@ NURBS_KNOT_MODE_ENDPOINT_BEZIER
@ NURBS_KNOT_MODE_NORMAL
BMesh const char void * data
@ CURVE_TYPE_CATMULL_ROM
@ NORMAL_MODE_MINIMUM_TWIST
signed char int8_t
Definition BKE_text.h:76