Blender  V2.93
geometry_component_pointcloud.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #include "DNA_pointcloud_types.h"
18 
19 #include "BKE_attribute_access.hh"
20 #include "BKE_geometry_set.hh"
21 #include "BKE_lib_id.h"
22 #include "BKE_pointcloud.h"
23 
25 
26 /* -------------------------------------------------------------------- */
31 {
32 }
33 
35 {
36  this->clear();
37 }
38 
40 {
41  PointCloudComponent *new_component = new PointCloudComponent();
42  if (pointcloud_ != nullptr) {
43  new_component->pointcloud_ = BKE_pointcloud_copy_for_eval(pointcloud_, false);
44  new_component->ownership_ = GeometryOwnershipType::Owned;
45  }
46  return new_component;
47 }
48 
50 {
51  BLI_assert(this->is_mutable());
52  if (pointcloud_ != nullptr) {
53  if (ownership_ == GeometryOwnershipType::Owned) {
54  BKE_id_free(nullptr, pointcloud_);
55  }
56  pointcloud_ = nullptr;
57  }
58 }
59 
61 {
62  return pointcloud_ != nullptr;
63 }
64 
65 /* Clear the component and replace it with the new point cloud. */
67 {
68  BLI_assert(this->is_mutable());
69  this->clear();
70  pointcloud_ = pointcloud;
71  ownership_ = ownership;
72 }
73 
74 /* Return the point cloud and clear the component. The caller takes over responsibility for freeing
75  * the point cloud (if the component was responsible before). */
77 {
78  BLI_assert(this->is_mutable());
79  PointCloud *pointcloud = pointcloud_;
80  pointcloud_ = nullptr;
81  return pointcloud;
82 }
83 
84 /* Get the point cloud from this component. This method can be used by multiple threads at the same
85  * time. Therefore, the returned point cloud should not be modified. No ownership is transferred.
86  */
88 {
89  return pointcloud_;
90 }
91 
92 /* Get the point cloud from this component. This method can only be used when the component is
93  * mutable, i.e. it is not shared. The returned point cloud can be modified. No ownership is
94  * transferred. */
96 {
97  BLI_assert(this->is_mutable());
98  if (ownership_ == GeometryOwnershipType::ReadOnly) {
99  pointcloud_ = BKE_pointcloud_copy_for_eval(pointcloud_, false);
100  ownership_ = GeometryOwnershipType::Owned;
101  }
102  return pointcloud_;
103 }
104 
106 {
107  return pointcloud_ == nullptr;
108 }
109 
111 {
112  return ownership_ == GeometryOwnershipType::Owned;
113 }
114 
116 {
117  BLI_assert(this->is_mutable());
118  if (ownership_ != GeometryOwnershipType::Owned) {
119  pointcloud_ = BKE_pointcloud_copy_for_eval(pointcloud_, false);
120  ownership_ = GeometryOwnershipType::Owned;
121  }
122 }
123 
126 /* -------------------------------------------------------------------- */
131 {
132  if (pointcloud_ == nullptr) {
133  return 0;
134  }
135  if (domain != ATTR_DOMAIN_POINT) {
136  return 0;
137  }
138  return pointcloud_->totpoint;
139 }
140 
141 namespace blender::bke {
142 
143 template<typename T, AttributeDomain Domain>
144 static ReadAttributePtr make_array_read_attribute(const void *data, const int domain_size)
145 {
146  return std::make_unique<ArrayReadAttribute<T>>(Domain, Span<T>((const T *)data, domain_size));
147 }
148 
149 template<typename T, AttributeDomain Domain>
150 static WriteAttributePtr make_array_write_attribute(void *data, const int domain_size)
151 {
152  return std::make_unique<ArrayWriteAttribute<T>>(Domain, MutableSpan<T>((T *)data, domain_size));
153 }
154 
160 {
161  static auto update_custom_data_pointers = [](GeometryComponent &component) {
162  PointCloudComponent &pointcloud_component = static_cast<PointCloudComponent &>(component);
163  PointCloud *pointcloud = pointcloud_component.get_for_write();
164  if (pointcloud != nullptr) {
166  }
167  };
168  static CustomDataAccessInfo point_access = {
170  PointCloudComponent &pointcloud_component = static_cast<PointCloudComponent &>(component);
171  PointCloud *pointcloud = pointcloud_component.get_for_write();
172  return pointcloud ? &pointcloud->pdata : nullptr;
173  },
174  [](const GeometryComponent &component) -> const CustomData * {
175  const PointCloudComponent &pointcloud_component = static_cast<const PointCloudComponent &>(
176  component);
177  const PointCloud *pointcloud = pointcloud_component.get_for_read();
178  return pointcloud ? &pointcloud->pdata : nullptr;
179  },
180  update_custom_data_pointers};
181 
182  static BuiltinCustomDataLayerProvider position(
183  "position",
190  point_access,
191  make_array_read_attribute<float3, ATTR_DOMAIN_POINT>,
192  make_array_write_attribute<float3, ATTR_DOMAIN_POINT>,
193  nullptr);
194  static BuiltinCustomDataLayerProvider radius(
195  "radius",
202  point_access,
203  make_array_read_attribute<float, ATTR_DOMAIN_POINT>,
204  make_array_write_attribute<float, ATTR_DOMAIN_POINT>,
205  nullptr);
206  static CustomDataAttributeProvider point_custom_data(ATTR_DOMAIN_POINT, point_access);
207  return ComponentAttributeProviders({&position, &radius}, {&point_custom_data});
208 }
209 
210 } // namespace blender::bke
211 
212 const blender::bke::ComponentAttributeProviders *PointCloudComponent::get_attribute_providers()
213  const
214 {
217  return &providers;
218 }
219 
AttributeDomain
Definition: BKE_attribute.h:41
@ ATTR_DOMAIN_POINT
Definition: BKE_attribute.h:43
@ 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:335
void BKE_pointcloud_update_customdata_pointers(struct PointCloud *pointcloud)
Definition: pointcloud.cc:303
#define BLI_assert(a)
Definition: BLI_assert.h:58
static uint8 component(Color32 c, uint i)
Definition: ColorBlock.cpp:126
@ CD_PROP_FLOAT
@ CD_PROP_FLOAT3
bool is_mutable() const
Definition: geometry_set.cc:81
void replace(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const PointCloud * get_for_read() const
bool owns_direct_data() const override
int attribute_domain_size(const AttributeDomain domain) const final
GeometryComponent * copy() const override
#define T
static ComponentAttributeProviders create_attribute_providers_for_point_cloud()
std::unique_ptr< WriteAttribute > WriteAttributePtr
static WriteAttributePtr make_array_write_attribute(void *data, const int domain_size)
static ReadAttributePtr make_array_read_attribute(const void *data, const int domain_size)
std::unique_ptr< ReadAttribute > ReadAttributePtr
struct CustomData pdata