2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2021-03-08 11:41:23 -05:00
|
|
|
|
|
|
|
|
#include "DNA_pointcloud_types.h"
|
|
|
|
|
|
|
|
|
|
#include "BKE_geometry_set.hh"
|
|
|
|
|
#include "BKE_lib_id.h"
|
|
|
|
|
#include "BKE_pointcloud.h"
|
|
|
|
|
|
|
|
|
|
#include "attribute_access_intern.hh"
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Geometry Component Implementation
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2021-03-10 11:53:17 +01:00
|
|
|
PointCloudComponent::PointCloudComponent() : GeometryComponent(GEO_COMPONENT_TYPE_POINT_CLOUD)
|
2021-03-08 11:41:23 -05:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PointCloudComponent::~PointCloudComponent()
|
|
|
|
|
{
|
|
|
|
|
this->clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeometryComponent *PointCloudComponent::copy() const
|
|
|
|
|
{
|
|
|
|
|
PointCloudComponent *new_component = new PointCloudComponent();
|
|
|
|
|
if (pointcloud_ != nullptr) {
|
|
|
|
|
new_component->pointcloud_ = BKE_pointcloud_copy_for_eval(pointcloud_, false);
|
|
|
|
|
new_component->ownership_ = GeometryOwnershipType::Owned;
|
|
|
|
|
}
|
|
|
|
|
return new_component;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PointCloudComponent::clear()
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(this->is_mutable());
|
|
|
|
|
if (pointcloud_ != nullptr) {
|
|
|
|
|
if (ownership_ == GeometryOwnershipType::Owned) {
|
|
|
|
|
BKE_id_free(nullptr, pointcloud_);
|
|
|
|
|
}
|
|
|
|
|
pointcloud_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PointCloudComponent::has_pointcloud() const
|
|
|
|
|
{
|
|
|
|
|
return pointcloud_ != nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PointCloudComponent::replace(PointCloud *pointcloud, GeometryOwnershipType ownership)
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(this->is_mutable());
|
|
|
|
|
this->clear();
|
|
|
|
|
pointcloud_ = pointcloud;
|
|
|
|
|
ownership_ = ownership;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PointCloud *PointCloudComponent::release()
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(this->is_mutable());
|
|
|
|
|
PointCloud *pointcloud = pointcloud_;
|
|
|
|
|
pointcloud_ = nullptr;
|
|
|
|
|
return pointcloud;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const PointCloud *PointCloudComponent::get_for_read() const
|
|
|
|
|
{
|
|
|
|
|
return pointcloud_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PointCloud *PointCloudComponent::get_for_write()
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(this->is_mutable());
|
|
|
|
|
if (ownership_ == GeometryOwnershipType::ReadOnly) {
|
|
|
|
|
pointcloud_ = BKE_pointcloud_copy_for_eval(pointcloud_, false);
|
|
|
|
|
ownership_ = GeometryOwnershipType::Owned;
|
|
|
|
|
}
|
|
|
|
|
return pointcloud_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PointCloudComponent::is_empty() const
|
|
|
|
|
{
|
|
|
|
|
return pointcloud_ == nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-08 17:35:06 +02:00
|
|
|
bool PointCloudComponent::owns_direct_data() const
|
|
|
|
|
{
|
|
|
|
|
return ownership_ == GeometryOwnershipType::Owned;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PointCloudComponent::ensure_owns_direct_data()
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(this->is_mutable());
|
|
|
|
|
if (ownership_ != GeometryOwnershipType::Owned) {
|
|
|
|
|
pointcloud_ = BKE_pointcloud_copy_for_eval(pointcloud_, false);
|
|
|
|
|
ownership_ = GeometryOwnershipType::Owned;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 11:41:23 -05:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Attribute Access
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
namespace blender::bke {
|
|
|
|
|
|
Geometry: Cache bounds min and max, share between data-blocks
Bounding box calculation can be a large in some situations, especially
instancing. This patch caches the min and max of the bounding box in
runtime data of meshes, point clouds, and curves, implementing part of
T96968.
Bounds are now calculated lazily-- only after they are tagged dirty.
Also, cached bounds are also shared when copying geometry data-blocks
that have equivalent data. When bounds are calculated on an evaluated
data-block, they are also accessible on the original, and the next
evaluated ID will also share them. A geometry will stop sharing bounds
as soon as its positions (or radii) are changed.
Just caching the bounds gave a 2-3x speedup with thousands of mesh
geometry instances in the viewport. Sharing the bounds can eliminate
recalculations entirely in cases like copying meshes in geometry nodes
or the selection paint brush in curves sculpt mode, which causes a
reevaluation but doesn't change the positions.
**Implementation**
The sharing is achieved with a `shared_ptr` that points to a cache mutex
(from D16419) and the cached bounds data. When geometries are copied,
the bounds are shared by default, and only "un-shared" when the bounds
are tagged dirty.
Point clouds have a new runtime struct to store this data. Functions
for tagging the data dirty are improved for added for point clouds
and improved for curves. A missing tag has also been fixed for mesh
sculpt mode.
**Future**
There are further improvements which can be worked on next
- Apply changes to volume objects and other types where it makes sense
- Continue cleanup changes described in T96968
- Apply shared cache design to more expensive data like triangulation
or normals
Differential Revision: https://developer.blender.org/D16204
2022-11-15 13:46:55 -06:00
|
|
|
static void tag_component_positions_changed(void *owner)
|
|
|
|
|
{
|
|
|
|
|
PointCloud &points = *static_cast<PointCloud *>(owner);
|
|
|
|
|
points.tag_positions_changed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void tag_component_radius_changed(void *owner)
|
|
|
|
|
{
|
|
|
|
|
PointCloud &points = *static_cast<PointCloud *>(owner);
|
|
|
|
|
points.tag_radii_changed();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 11:41:23 -05:00
|
|
|
/**
|
|
|
|
|
* In this function all the attribute providers for a point cloud component are created. Most data
|
|
|
|
|
* in this function is statically allocated, because it does not change over time.
|
|
|
|
|
*/
|
|
|
|
|
static ComponentAttributeProviders create_attribute_providers_for_point_cloud()
|
|
|
|
|
{
|
|
|
|
|
static CustomDataAccessInfo point_access = {
|
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
2022-07-08 16:16:56 +02:00
|
|
|
[](void *owner) -> CustomData * {
|
|
|
|
|
PointCloud *pointcloud = static_cast<PointCloud *>(owner);
|
|
|
|
|
return &pointcloud->pdata;
|
|
|
|
|
},
|
|
|
|
|
[](const void *owner) -> const CustomData * {
|
|
|
|
|
const PointCloud *pointcloud = static_cast<const PointCloud *>(owner);
|
|
|
|
|
return &pointcloud->pdata;
|
2021-03-08 11:41:23 -05:00
|
|
|
},
|
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
2022-07-08 16:16:56 +02:00
|
|
|
[](const void *owner) -> int {
|
|
|
|
|
const PointCloud *pointcloud = static_cast<const PointCloud *>(owner);
|
|
|
|
|
return pointcloud->totpoint;
|
2022-09-06 09:43:32 -05:00
|
|
|
}};
|
2021-03-08 11:41:23 -05:00
|
|
|
|
Geometry Nodes: use virtual arrays in internal attribute api
A virtual array is a data structure that is similar to a normal array
in that its elements can be accessed by an index. However, a virtual
array does not have to be a contiguous array internally. Instead, its
elements can be layed out arbitrarily while element access happens
through a virtual function call. However, the virtual array data
structures are designed so that the virtual function call can be avoided
in cases where it could become a bottleneck.
Most commonly, a virtual array is backed by an actual array/span or
is a single value internally, that is the same for every index.
Besides those, there are many more specialized virtual arrays like the
ones that provides vertex positions based on the `MVert` struct or
vertex group weights.
Not all attributes used by geometry nodes are stored in simple contiguous
arrays. To provide uniform access to all kinds of attributes, the attribute
API has to provide virtual array functionality that hides the implementation
details of attributes.
Before this refactor, the attribute API provided its own virtual array
implementation as part of the `ReadAttribute` and `WriteAttribute` types.
That resulted in unnecessary code duplication with the virtual array system.
Even worse, it bound many algorithms used by geometry nodes to the specifics
of the attribute API, even though they could also use different data sources
(such as data from sockets, default values, later results of expressions, ...).
This refactor removes the `ReadAttribute` and `WriteAttribute` types and
replaces them with `GVArray` and `GVMutableArray` respectively. The `GV`
stands for "generic virtual". The "generic" means that the data type contained
in those virtual arrays is only known at run-time. There are the corresponding
statically typed types `VArray<T>` and `VMutableArray<T>` as well.
No regressions are expected from this refactor. It does come with one
improvement for users. The attribute API can convert the data type
on write now. This is especially useful when writing to builtin attributes
like `material_index` with e.g. the Attribute Math node (which usually
just writes to float attributes, while `material_index` is an integer attribute).
Differential Revision: https://developer.blender.org/D10994
2021-04-17 16:41:03 +02:00
|
|
|
static BuiltinCustomDataLayerProvider position("position",
|
|
|
|
|
ATTR_DOMAIN_POINT,
|
|
|
|
|
CD_PROP_FLOAT3,
|
|
|
|
|
CD_PROP_FLOAT3,
|
|
|
|
|
BuiltinAttributeProvider::NonCreatable,
|
|
|
|
|
BuiltinAttributeProvider::Writable,
|
|
|
|
|
BuiltinAttributeProvider::NonDeletable,
|
|
|
|
|
point_access,
|
|
|
|
|
make_array_read_attribute<float3>,
|
|
|
|
|
make_array_write_attribute<float3>,
|
Geometry: Cache bounds min and max, share between data-blocks
Bounding box calculation can be a large in some situations, especially
instancing. This patch caches the min and max of the bounding box in
runtime data of meshes, point clouds, and curves, implementing part of
T96968.
Bounds are now calculated lazily-- only after they are tagged dirty.
Also, cached bounds are also shared when copying geometry data-blocks
that have equivalent data. When bounds are calculated on an evaluated
data-block, they are also accessible on the original, and the next
evaluated ID will also share them. A geometry will stop sharing bounds
as soon as its positions (or radii) are changed.
Just caching the bounds gave a 2-3x speedup with thousands of mesh
geometry instances in the viewport. Sharing the bounds can eliminate
recalculations entirely in cases like copying meshes in geometry nodes
or the selection paint brush in curves sculpt mode, which causes a
reevaluation but doesn't change the positions.
**Implementation**
The sharing is achieved with a `shared_ptr` that points to a cache mutex
(from D16419) and the cached bounds data. When geometries are copied,
the bounds are shared by default, and only "un-shared" when the bounds
are tagged dirty.
Point clouds have a new runtime struct to store this data. Functions
for tagging the data dirty are improved for added for point clouds
and improved for curves. A missing tag has also been fixed for mesh
sculpt mode.
**Future**
There are further improvements which can be worked on next
- Apply changes to volume objects and other types where it makes sense
- Continue cleanup changes described in T96968
- Apply shared cache design to more expensive data like triangulation
or normals
Differential Revision: https://developer.blender.org/D16204
2022-11-15 13:46:55 -06:00
|
|
|
tag_component_positions_changed);
|
Geometry Nodes: use virtual arrays in internal attribute api
A virtual array is a data structure that is similar to a normal array
in that its elements can be accessed by an index. However, a virtual
array does not have to be a contiguous array internally. Instead, its
elements can be layed out arbitrarily while element access happens
through a virtual function call. However, the virtual array data
structures are designed so that the virtual function call can be avoided
in cases where it could become a bottleneck.
Most commonly, a virtual array is backed by an actual array/span or
is a single value internally, that is the same for every index.
Besides those, there are many more specialized virtual arrays like the
ones that provides vertex positions based on the `MVert` struct or
vertex group weights.
Not all attributes used by geometry nodes are stored in simple contiguous
arrays. To provide uniform access to all kinds of attributes, the attribute
API has to provide virtual array functionality that hides the implementation
details of attributes.
Before this refactor, the attribute API provided its own virtual array
implementation as part of the `ReadAttribute` and `WriteAttribute` types.
That resulted in unnecessary code duplication with the virtual array system.
Even worse, it bound many algorithms used by geometry nodes to the specifics
of the attribute API, even though they could also use different data sources
(such as data from sockets, default values, later results of expressions, ...).
This refactor removes the `ReadAttribute` and `WriteAttribute` types and
replaces them with `GVArray` and `GVMutableArray` respectively. The `GV`
stands for "generic virtual". The "generic" means that the data type contained
in those virtual arrays is only known at run-time. There are the corresponding
statically typed types `VArray<T>` and `VMutableArray<T>` as well.
No regressions are expected from this refactor. It does come with one
improvement for users. The attribute API can convert the data type
on write now. This is especially useful when writing to builtin attributes
like `material_index` with e.g. the Attribute Math node (which usually
just writes to float attributes, while `material_index` is an integer attribute).
Differential Revision: https://developer.blender.org/D10994
2021-04-17 16:41:03 +02:00
|
|
|
static BuiltinCustomDataLayerProvider radius("radius",
|
|
|
|
|
ATTR_DOMAIN_POINT,
|
|
|
|
|
CD_PROP_FLOAT,
|
|
|
|
|
CD_PROP_FLOAT,
|
|
|
|
|
BuiltinAttributeProvider::Creatable,
|
|
|
|
|
BuiltinAttributeProvider::Writable,
|
|
|
|
|
BuiltinAttributeProvider::Deletable,
|
|
|
|
|
point_access,
|
|
|
|
|
make_array_read_attribute<float>,
|
|
|
|
|
make_array_write_attribute<float>,
|
Geometry: Cache bounds min and max, share between data-blocks
Bounding box calculation can be a large in some situations, especially
instancing. This patch caches the min and max of the bounding box in
runtime data of meshes, point clouds, and curves, implementing part of
T96968.
Bounds are now calculated lazily-- only after they are tagged dirty.
Also, cached bounds are also shared when copying geometry data-blocks
that have equivalent data. When bounds are calculated on an evaluated
data-block, they are also accessible on the original, and the next
evaluated ID will also share them. A geometry will stop sharing bounds
as soon as its positions (or radii) are changed.
Just caching the bounds gave a 2-3x speedup with thousands of mesh
geometry instances in the viewport. Sharing the bounds can eliminate
recalculations entirely in cases like copying meshes in geometry nodes
or the selection paint brush in curves sculpt mode, which causes a
reevaluation but doesn't change the positions.
**Implementation**
The sharing is achieved with a `shared_ptr` that points to a cache mutex
(from D16419) and the cached bounds data. When geometries are copied,
the bounds are shared by default, and only "un-shared" when the bounds
are tagged dirty.
Point clouds have a new runtime struct to store this data. Functions
for tagging the data dirty are improved for added for point clouds
and improved for curves. A missing tag has also been fixed for mesh
sculpt mode.
**Future**
There are further improvements which can be worked on next
- Apply changes to volume objects and other types where it makes sense
- Continue cleanup changes described in T96968
- Apply shared cache design to more expensive data like triangulation
or normals
Differential Revision: https://developer.blender.org/D16204
2022-11-15 13:46:55 -06:00
|
|
|
tag_component_radius_changed);
|
2021-10-20 10:54:54 -05:00
|
|
|
static BuiltinCustomDataLayerProvider id("id",
|
|
|
|
|
ATTR_DOMAIN_POINT,
|
|
|
|
|
CD_PROP_INT32,
|
|
|
|
|
CD_PROP_INT32,
|
|
|
|
|
BuiltinAttributeProvider::Creatable,
|
|
|
|
|
BuiltinAttributeProvider::Writable,
|
|
|
|
|
BuiltinAttributeProvider::Deletable,
|
|
|
|
|
point_access,
|
|
|
|
|
make_array_read_attribute<int>,
|
|
|
|
|
make_array_write_attribute<int>,
|
|
|
|
|
nullptr);
|
2021-03-08 11:41:23 -05:00
|
|
|
static CustomDataAttributeProvider point_custom_data(ATTR_DOMAIN_POINT, point_access);
|
2021-10-20 10:54:54 -05:00
|
|
|
return ComponentAttributeProviders({&position, &radius, &id}, {&point_custom_data});
|
2021-03-08 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
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
2022-07-08 16:16:56 +02:00
|
|
|
static AttributeAccessorFunctions get_pointcloud_accessor_functions()
|
|
|
|
|
{
|
|
|
|
|
static const ComponentAttributeProviders providers =
|
|
|
|
|
create_attribute_providers_for_point_cloud();
|
|
|
|
|
AttributeAccessorFunctions fn =
|
|
|
|
|
attribute_accessor_functions::accessor_functions_for_providers<providers>();
|
|
|
|
|
fn.domain_size = [](const void *owner, const eAttrDomain domain) {
|
|
|
|
|
if (owner == nullptr) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
const PointCloud &pointcloud = *static_cast<const PointCloud *>(owner);
|
|
|
|
|
switch (domain) {
|
|
|
|
|
case ATTR_DOMAIN_POINT:
|
|
|
|
|
return pointcloud.totpoint;
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-10-03 17:37:25 -05:00
|
|
|
fn.domain_supported = [](const void * /*owner*/, const eAttrDomain domain) {
|
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
2022-07-08 16:16:56 +02:00
|
|
|
return domain == ATTR_DOMAIN_POINT;
|
|
|
|
|
};
|
2022-10-03 17:37:25 -05:00
|
|
|
fn.adapt_domain = [](const void * /*owner*/,
|
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
2022-07-08 16:16:56 +02:00
|
|
|
const blender::GVArray &varray,
|
|
|
|
|
const eAttrDomain from_domain,
|
|
|
|
|
const eAttrDomain to_domain) {
|
|
|
|
|
if (from_domain == to_domain && from_domain == ATTR_DOMAIN_POINT) {
|
|
|
|
|
return varray;
|
|
|
|
|
}
|
|
|
|
|
return blender::GVArray{};
|
|
|
|
|
};
|
|
|
|
|
return fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const AttributeAccessorFunctions &get_pointcloud_accessor_functions_ref()
|
|
|
|
|
{
|
|
|
|
|
static const AttributeAccessorFunctions fn = get_pointcloud_accessor_functions();
|
|
|
|
|
return fn;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-07 21:41:39 -05:00
|
|
|
} // namespace blender::bke
|
|
|
|
|
|
|
|
|
|
blender::bke::AttributeAccessor PointCloud::attributes() const
|
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
2022-07-08 16:16:56 +02:00
|
|
|
{
|
2022-09-07 21:41:39 -05:00
|
|
|
return blender::bke::AttributeAccessor(this,
|
|
|
|
|
blender::bke::get_pointcloud_accessor_functions_ref());
|
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
2022-07-08 16:16:56 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-07 21:41:39 -05:00
|
|
|
blender::bke::MutableAttributeAccessor PointCloud::attributes_for_write()
|
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
2022-07-08 16:16:56 +02:00
|
|
|
{
|
2022-09-07 21:41:39 -05:00
|
|
|
return blender::bke::MutableAttributeAccessor(
|
|
|
|
|
this, blender::bke::get_pointcloud_accessor_functions_ref());
|
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
2022-07-08 16:16:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<blender::bke::AttributeAccessor> PointCloudComponent::attributes() const
|
|
|
|
|
{
|
|
|
|
|
return blender::bke::AttributeAccessor(pointcloud_,
|
|
|
|
|
blender::bke::get_pointcloud_accessor_functions_ref());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<blender::bke::MutableAttributeAccessor> PointCloudComponent::attributes_for_write()
|
2021-03-08 11:41:23 -05:00
|
|
|
{
|
2022-07-12 16:26:50 +02:00
|
|
|
PointCloud *pointcloud = this->get_for_write();
|
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
2022-07-08 16:16:56 +02:00
|
|
|
return blender::bke::MutableAttributeAccessor(
|
2022-07-12 16:26:50 +02:00
|
|
|
pointcloud, blender::bke::get_pointcloud_accessor_functions_ref());
|
2021-03-08 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|