diff options
author | Jacques Lucke <jacques@blender.org> | 2022-07-08 17:16:56 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2022-07-08 17:16:56 +0300 |
commit | b876ce2a4a4638142439a7cf265a0780491ae4cc (patch) | |
tree | 871d71eb6d1cf215869fc941c831c81bcacc6433 /source/blender/blenkernel/intern/curve_to_mesh_convert.cc | |
parent | f391e8f316bd29b700cef874a59cf3b64203d70c (diff) |
Geometry Nodes: new geometry attribute API
Currently, there are two attribute API. The first, defined in `BKE_attribute.h` is
accessible from RNA and C code. The second is implemented with `GeometryComponent`
and is only accessible in C++ code. The second is widely used, but only being
accessible through the `GeometrySet` API makes it awkward to use, and even impossible
for types that don't correspond directly to a geometry component like `CurvesGeometry`.
This patch adds a new attribute API, designed to replace the `GeometryComponent`
attribute API now, and to eventually replace or be the basis of the other one.
The basic idea is that there is an `AttributeAccessor` class that allows code to
interact with a set of attributes owned by some geometry. The accessor itself has
no ownership. `AttributeAccessor` is a simple type that can be passed around by
value. That makes it easy to return it from functions and to store it in containers.
For const-correctness, there is also a `MutableAttributeAccessor` that allows
changing individual and can add or remove attributes.
Currently, `AttributeAccessor` is composed of two pointers. The first is a pointer
to the owner of the attribute data. The second is a pointer to a struct with
function pointers, that is similar to a virtual function table. The functions
know how to access attributes on the owner.
The actual attribute access for geometries is still implemented with the `AttributeProvider`
pattern, which makes it easy to support different sources of attributes on a
geometry and simplifies dealing with built-in attributes.
There are different ways to get an attribute accessor for a geometry:
* `GeometryComponent.attributes()`
* `CurvesGeometry.attributes()`
* `bke::mesh_attributes(const Mesh &)`
* `bke::pointcloud_attributes(const PointCloud &)`
All of these also have a `_for_write` variant that returns a `MutabelAttributeAccessor`.
Differential Revision: https://developer.blender.org/D15280
Diffstat (limited to 'source/blender/blenkernel/intern/curve_to_mesh_convert.cc')
-rw-r--r-- | source/blender/blenkernel/intern/curve_to_mesh_convert.cc | 72 |
1 files changed, 32 insertions, 40 deletions
diff --git a/source/blender/blenkernel/intern/curve_to_mesh_convert.cc b/source/blender/blenkernel/intern/curve_to_mesh_convert.cc index baf56c0c350..7f051b683b4 100644 --- a/source/blender/blenkernel/intern/curve_to_mesh_convert.cc +++ b/source/blender/blenkernel/intern/curve_to_mesh_convert.cc @@ -8,7 +8,6 @@ #include "DNA_mesh_types.h" #include "DNA_meshdata_types.h" -#include "BKE_attribute_access.hh" #include "BKE_attribute_math.hh" #include "BKE_curves.hh" #include "BKE_geometry_set.hh" @@ -315,15 +314,15 @@ static ResultOffsets calculate_result_offsets(const CurvesInfo &info, const bool return result; } -static eAttrDomain get_attribute_domain_for_mesh(const MeshComponent &mesh, +static eAttrDomain get_attribute_domain_for_mesh(const AttributeAccessor &mesh_attributes, const AttributeIDRef &attribute_id) { /* Only use a different domain if it is builtin and must only exist on one domain. */ - if (!mesh.attribute_is_builtin(attribute_id)) { + if (!mesh_attributes.is_builtin(attribute_id)) { return ATTR_DOMAIN_POINT; } - std::optional<AttributeMetaData> meta_data = mesh.attribute_get_meta_data(attribute_id); + std::optional<AttributeMetaData> meta_data = mesh_attributes.lookup_meta_data(attribute_id); if (!meta_data) { return ATTR_DOMAIN_POINT; } @@ -331,16 +330,17 @@ static eAttrDomain get_attribute_domain_for_mesh(const MeshComponent &mesh, return meta_data->domain; } -static bool should_add_attribute_to_mesh(const CurveComponent &curve_component, - const MeshComponent &mesh_component, +static bool should_add_attribute_to_mesh(const AttributeAccessor &curve_attributes, + const AttributeAccessor &mesh_attributes, const AttributeIDRef &id) { + /* The position attribute has special non-generic evaluation. */ if (id.is_named() && id.name() == "position") { return false; } /* Don't propagate built-in curves attributes that are not built-in on meshes. */ - if (curve_component.attribute_is_builtin(id) && !mesh_component.attribute_is_builtin(id)) { + if (curve_attributes.is_builtin(id) && !mesh_attributes.is_builtin(id)) { return false; } if (!id.should_be_kept()) { @@ -667,20 +667,13 @@ Mesh *curve_to_mesh_sweep(const CurvesGeometry &main, Vector<std::byte> eval_buffer; - Curves main_id = {{nullptr}}; - main_id.geometry = reinterpret_cast<const ::CurvesGeometry &>(main); - CurveComponent main_component; - main_component.replace(&main_id, GeometryOwnershipType::Editable); - - Curves profile_id = {{nullptr}}; - profile_id.geometry = reinterpret_cast<const ::CurvesGeometry &>(profile); - CurveComponent profile_component; - profile_component.replace(&profile_id, GeometryOwnershipType::Editable); + const AttributeAccessor main_attributes = main.attributes(); + const AttributeAccessor profile_attributes = profile.attributes(); Span<float> radii = {}; - if (main_component.attribute_exists("radius")) { + if (main_attributes.contains("radius")) { radii = evaluated_attribute_if_necessary( - main_component.attribute_get_for_read<float>("radius", ATTR_DOMAIN_POINT, 1.0f), + main_attributes.lookup_or_default<float>("radius", ATTR_DOMAIN_POINT, 1.0f), main, main.curve_type_counts(), eval_buffer) @@ -716,24 +709,23 @@ Mesh *curve_to_mesh_sweep(const CurvesGeometry &main, }); } - Set<AttributeIDRef> main_attributes; + Set<AttributeIDRef> main_attributes_set; - MeshComponent mesh_component; - mesh_component.replace(mesh, GeometryOwnershipType::Editable); + MutableAttributeAccessor mesh_attributes = bke::mesh_attributes_for_write(*mesh); - main_component.attribute_foreach([&](const AttributeIDRef &id, - const AttributeMetaData meta_data) { - if (!should_add_attribute_to_mesh(main_component, mesh_component, id)) { + main_attributes.for_all([&](const AttributeIDRef &id, const AttributeMetaData meta_data) { + if (!should_add_attribute_to_mesh(main_attributes, mesh_attributes, id)) { return true; } - main_attributes.add_new(id); + main_attributes_set.add_new(id); const eAttrDomain src_domain = meta_data.domain; const eCustomDataType type = meta_data.data_type; - GVArray src = main_component.attribute_try_get_for_read(id, src_domain, type); + GVArray src = main_attributes.lookup(id, src_domain, type); - const eAttrDomain dst_domain = get_attribute_domain_for_mesh(mesh_component, id); - OutputAttribute dst = mesh_component.attribute_try_get_for_output_only(id, dst_domain, type); + const eAttrDomain dst_domain = get_attribute_domain_for_mesh(mesh_attributes, id); + GSpanAttributeWriter dst = mesh_attributes.lookup_or_add_for_write_only_span( + id, dst_domain, type); if (!dst) { return true; } @@ -744,31 +736,31 @@ Mesh *curve_to_mesh_sweep(const CurvesGeometry &main, offsets, dst_domain, evaluated_attribute_if_necessary(src, main, main.curve_type_counts(), eval_buffer), - dst.as_span()); + dst.span); } else if (src_domain == ATTR_DOMAIN_CURVE) { copy_curve_domain_attribute_to_mesh( - offsets, offsets.main_indices, dst_domain, src, dst.as_span()); + offsets, offsets.main_indices, dst_domain, src, dst.span); } - dst.save(); + dst.finish(); return true; }); - profile_component.attribute_foreach([&](const AttributeIDRef &id, - const AttributeMetaData meta_data) { + profile_attributes.for_all([&](const AttributeIDRef &id, const AttributeMetaData meta_data) { if (main_attributes.contains(id)) { return true; } - if (!should_add_attribute_to_mesh(profile_component, mesh_component, id)) { + if (!should_add_attribute_to_mesh(profile_attributes, mesh_attributes, id)) { return true; } const eAttrDomain src_domain = meta_data.domain; const eCustomDataType type = meta_data.data_type; - GVArray src = profile_component.attribute_try_get_for_read(id, src_domain, type); + GVArray src = profile_attributes.lookup(id, src_domain, type); - const eAttrDomain dst_domain = get_attribute_domain_for_mesh(mesh_component, id); - OutputAttribute dst = mesh_component.attribute_try_get_for_output_only(id, dst_domain, type); + const eAttrDomain dst_domain = get_attribute_domain_for_mesh(mesh_attributes, id); + GSpanAttributeWriter dst = mesh_attributes.lookup_or_add_for_write_only_span( + id, dst_domain, type); if (!dst) { return true; } @@ -779,14 +771,14 @@ Mesh *curve_to_mesh_sweep(const CurvesGeometry &main, offsets, dst_domain, evaluated_attribute_if_necessary(src, profile, profile.curve_type_counts(), eval_buffer), - dst.as_span()); + dst.span); } else if (src_domain == ATTR_DOMAIN_CURVE) { copy_curve_domain_attribute_to_mesh( - offsets, offsets.profile_indices, dst_domain, src, dst.as_span()); + offsets, offsets.profile_indices, dst_domain, src, dst.span); } - dst.save(); + dst.finish(); return true; }); |