Blender  V3.3
geometry_component_pointcloud.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include "DNA_pointcloud_types.h"
4 
5 #include "BKE_geometry_set.hh"
6 #include "BKE_lib_id.h"
7 #include "BKE_pointcloud.h"
8 
10 
11 /* -------------------------------------------------------------------- */
16 {
17 }
18 
20 {
21  this->clear();
22 }
23 
25 {
26  PointCloudComponent *new_component = new PointCloudComponent();
27  if (pointcloud_ != nullptr) {
28  new_component->pointcloud_ = BKE_pointcloud_copy_for_eval(pointcloud_, false);
29  new_component->ownership_ = GeometryOwnershipType::Owned;
30  }
31  return new_component;
32 }
33 
35 {
36  BLI_assert(this->is_mutable());
37  if (pointcloud_ != nullptr) {
38  if (ownership_ == GeometryOwnershipType::Owned) {
39  BKE_id_free(nullptr, pointcloud_);
40  }
41  pointcloud_ = nullptr;
42  }
43 }
44 
46 {
47  return pointcloud_ != nullptr;
48 }
49 
51 {
52  BLI_assert(this->is_mutable());
53  this->clear();
54  pointcloud_ = pointcloud;
55  ownership_ = ownership;
56 }
57 
59 {
60  BLI_assert(this->is_mutable());
61  PointCloud *pointcloud = pointcloud_;
62  pointcloud_ = nullptr;
63  return pointcloud;
64 }
65 
67 {
68  return pointcloud_;
69 }
70 
72 {
73  BLI_assert(this->is_mutable());
74  if (ownership_ == GeometryOwnershipType::ReadOnly) {
75  pointcloud_ = BKE_pointcloud_copy_for_eval(pointcloud_, false);
76  ownership_ = GeometryOwnershipType::Owned;
77  }
78  return pointcloud_;
79 }
80 
82 {
83  return pointcloud_ == nullptr;
84 }
85 
87 {
88  return ownership_ == GeometryOwnershipType::Owned;
89 }
90 
92 {
93  BLI_assert(this->is_mutable());
94  if (ownership_ != GeometryOwnershipType::Owned) {
95  pointcloud_ = BKE_pointcloud_copy_for_eval(pointcloud_, false);
96  ownership_ = GeometryOwnershipType::Owned;
97  }
98 }
99 
102 /* -------------------------------------------------------------------- */
106 namespace blender::bke {
107 
113 {
114  static auto update_custom_data_pointers = [](void * /*owner*/) {};
115  static CustomDataAccessInfo point_access = {
116  [](void *owner) -> CustomData * {
117  PointCloud *pointcloud = static_cast<PointCloud *>(owner);
118  return &pointcloud->pdata;
119  },
120  [](const void *owner) -> const CustomData * {
121  const PointCloud *pointcloud = static_cast<const PointCloud *>(owner);
122  return &pointcloud->pdata;
123  },
124  [](const void *owner) -> int {
125  const PointCloud *pointcloud = static_cast<const PointCloud *>(owner);
126  return pointcloud->totpoint;
127  },
128  update_custom_data_pointers};
129 
130  static BuiltinCustomDataLayerProvider position("position",
137  point_access,
138  make_array_read_attribute<float3>,
139  make_array_write_attribute<float3>,
140  nullptr);
141  static BuiltinCustomDataLayerProvider radius("radius",
148  point_access,
149  make_array_read_attribute<float>,
150  make_array_write_attribute<float>,
151  nullptr);
159  point_access,
160  make_array_read_attribute<int>,
161  make_array_write_attribute<int>,
162  nullptr);
163  static CustomDataAttributeProvider point_custom_data(ATTR_DOMAIN_POINT, point_access);
164  return ComponentAttributeProviders({&position, &radius, &id}, {&point_custom_data});
165 }
166 
168 {
169  static const ComponentAttributeProviders providers =
172  attribute_accessor_functions::accessor_functions_for_providers<providers>();
173  fn.domain_size = [](const void *owner, const eAttrDomain domain) {
174  if (owner == nullptr) {
175  return 0;
176  }
177  const PointCloud &pointcloud = *static_cast<const PointCloud *>(owner);
178  switch (domain) {
179  case ATTR_DOMAIN_POINT:
180  return pointcloud.totpoint;
181  default:
182  return 0;
183  }
184  };
185  fn.domain_supported = [](const void *UNUSED(owner), const eAttrDomain domain) {
186  return domain == ATTR_DOMAIN_POINT;
187  };
188  fn.adapt_domain = [](const void *UNUSED(owner),
189  const blender::GVArray &varray,
190  const eAttrDomain from_domain,
191  const eAttrDomain to_domain) {
192  if (from_domain == to_domain && from_domain == ATTR_DOMAIN_POINT) {
193  return varray;
194  }
195  return blender::GVArray{};
196  };
197  return fn;
198 }
199 
201 {
203  return fn;
204 }
205 
207 {
209 }
210 
212 {
214 }
215 
216 } // namespace blender::bke
217 
218 std::optional<blender::bke::AttributeAccessor> PointCloudComponent::attributes() const
219 {
220  return blender::bke::AttributeAccessor(pointcloud_,
222 }
223 
224 std::optional<blender::bke::MutableAttributeAccessor> PointCloudComponent::attributes_for_write()
225 {
226  PointCloud *pointcloud = this->get_for_write();
229 }
230 
eAttrDomain
Definition: BKE_attribute.h:25
@ ATTR_DOMAIN_POINT
Definition: BKE_attribute.h:27
@ GEO_COMPONENT_TYPE_POINT_CLOUD
GeometryOwnershipType
void BKE_id_free(struct Main *bmain, void *idv)
General operations for point clouds.
struct PointCloud * BKE_pointcloud_copy_for_eval(struct PointCloud *pointcloud_src, bool reference)
Definition: pointcloud.cc:344
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define UNUSED(x)
@ CD_PROP_FLOAT
@ CD_PROP_FLOAT3
@ CD_PROP_INT32
bool is_mutable() const
Definition: geometry_set.cc:97
void replace(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const PointCloud * get_for_read() const
bool owns_direct_data() const override
std::optional< blender::bke::MutableAttributeAccessor > attributes_for_write() final
GeometryComponent * copy() const override
std::optional< blender::bke::AttributeAccessor > attributes() const final
static ComponentAttributeProviders create_attribute_providers_for_point_cloud()
static const AttributeAccessorFunctions & get_pointcloud_accessor_functions_ref()
AttributeAccessor pointcloud_attributes(const PointCloud &pointcloud)
MutableAttributeAccessor pointcloud_attributes_for_write(PointCloud &pointcloud)
static AttributeAccessorFunctions get_pointcloud_accessor_functions()
struct CustomData pdata
bool(* domain_supported)(const void *owner, eAttrDomain domain)
GVArray(* adapt_domain)(const void *owner, const GVArray &varray, eAttrDomain from_domain, eAttrDomain to_domain)
int(* domain_size)(const void *owner, eAttrDomain domain)