Blender V5.0
pointcloud.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <optional>
10
11#include "BLI_function_ref.hh"
12#include "MEM_guardedalloc.h"
13
14#include "DNA_defaults.h"
15#include "DNA_material_types.h"
16#include "DNA_object_types.h"
18
19#include "BLI_bounds.hh"
20#include "BLI_index_range.hh"
21#include "BLI_resource_scope.hh"
22#include "BLI_span.hh"
23#include "BLI_utildefines.h"
24#include "BLI_vector.hh"
25
26#include "BKE_anim_data.hh"
31#include "BKE_customdata.hh"
32#include "BKE_geometry_set.hh"
33#include "BKE_idtype.hh"
34#include "BKE_lib_id.hh"
35#include "BKE_lib_query.hh"
36#include "BKE_modifier.hh"
37#include "BKE_object.hh"
38#include "BKE_object_types.hh"
39#include "BKE_pointcloud.hh"
40
41#include "BLT_translation.hh"
42
44
45#include "BLO_read_write.hh"
46
48
50using blender::float3;
53using blender::Span;
55using blender::VArray;
56using blender::Vector;
57
58constexpr StringRef ATTR_POSITION = "position";
59
60static void pointcloud_init_data(ID *id)
61{
62 PointCloud *pointcloud = (PointCloud *)id;
64
66
67 new (&pointcloud->attribute_storage.wrap()) blender::bke::AttributeStorage();
68 pointcloud->runtime = new blender::bke::PointCloudRuntime();
69}
70
71static void pointcloud_copy_data(Main * /*bmain*/,
72 std::optional<Library *> /*owner_library*/,
73 ID *id_dst,
74 const ID *id_src,
75 const int /*flag*/)
76{
77 PointCloud *pointcloud_dst = (PointCloud *)id_dst;
78 const PointCloud *pointcloud_src = (const PointCloud *)id_src;
79 pointcloud_dst->mat = static_cast<Material **>(MEM_dupallocN(pointcloud_src->mat));
80
81 new (&pointcloud_dst->attribute_storage.wrap())
83
84 pointcloud_dst->runtime = new blender::bke::PointCloudRuntime();
85 pointcloud_dst->runtime->bounds_cache = pointcloud_src->runtime->bounds_cache;
86 pointcloud_dst->runtime->bounds_with_radius_cache =
87 pointcloud_src->runtime->bounds_with_radius_cache;
88 pointcloud_dst->runtime->bvh_cache = pointcloud_src->runtime->bvh_cache;
89 if (pointcloud_src->runtime->bake_materials) {
90 pointcloud_dst->runtime->bake_materials =
91 std::make_unique<blender::bke::bake::BakeMaterialsList>(
92 *pointcloud_src->runtime->bake_materials);
93 }
94
95 pointcloud_dst->batch_cache = nullptr;
96}
97
98static void pointcloud_free_data(ID *id)
99{
100 PointCloud *pointcloud = (PointCloud *)id;
101 BKE_animdata_free(&pointcloud->id, false);
103 pointcloud->attribute_storage.wrap().~AttributeStorage();
104 MEM_SAFE_FREE(pointcloud->mat);
105 delete pointcloud->runtime;
106}
107
109{
110 PointCloud *pointcloud = (PointCloud *)id;
111 for (int i = 0; i < pointcloud->totcol; i++) {
113 }
114}
115
118{
119 PointCloud *pointcloud = (PointCloud *)id;
120 pointcloud->attribute_storage.wrap().foreach_working_space_color(fn);
121}
122
123static void pointcloud_blend_write(BlendWriter *writer, ID *id, const void *id_address)
124{
125 using namespace blender;
126 using namespace blender::bke;
127 PointCloud *pointcloud = (PointCloud *)id;
128
129 ResourceScope scope;
130 bke::AttributeStorage::BlendWriteData attribute_data{scope};
131 attribute_storage_blend_write_prepare(pointcloud->attribute_storage.wrap(), attribute_data);
132
133 if (attribute_data.attributes.is_empty()) {
134 pointcloud->attribute_storage.dna_attributes = nullptr;
136 }
137 else {
138 pointcloud->attribute_storage.dna_attributes = attribute_data.attributes.data();
139 pointcloud->attribute_storage.dna_attributes_num = attribute_data.attributes.size();
140 }
141
142 CustomData_reset(&pointcloud->pdata_legacy);
143
144 /* Write LibData */
145 BLO_write_id_struct(writer, PointCloud, id_address, &pointcloud->id);
146 BKE_id_blend_write(writer, &pointcloud->id);
147
148 /* Direct data */
149 pointcloud->attribute_storage.wrap().blend_write(*writer, attribute_data);
150
151 BLO_write_pointer_array(writer, pointcloud->totcol, pointcloud->mat);
152}
153
155{
156 PointCloud *pointcloud = (PointCloud *)id;
157
158 /* Geometry */
159 CustomData_blend_read(reader, &pointcloud->pdata_legacy, pointcloud->totpoint);
160 pointcloud->attribute_storage.wrap().blend_read(*reader);
161
162 /* Materials */
163 BLO_read_pointer_array(reader, pointcloud->totcol, (void **)&pointcloud->mat);
164
165 pointcloud->runtime = new blender::bke::PointCloudRuntime();
166}
167
169 /*id_code*/ PointCloud::id_type,
170 /*id_filter*/ FILTER_ID_PT,
171 /*dependencies_id_types*/ FILTER_ID_MA,
172 /*main_listbase_index*/ INDEX_ID_PT,
173 /*struct_size*/ sizeof(PointCloud),
174 /*name*/ "PointCloud",
175 /*name_plural*/ N_("pointclouds"),
176 /*translation_context*/ BLT_I18NCONTEXT_ID_POINTCLOUD,
178 /*asset_type_info*/ nullptr,
179
180 /*init_data*/ pointcloud_init_data,
181 /*copy_data*/ pointcloud_copy_data,
182 /*free_data*/ pointcloud_free_data,
183 /*make_local*/ nullptr,
184 /*foreach_id*/ pointcloud_foreach_id,
185 /*foreach_cache*/ nullptr,
186 /*foreach_path*/ nullptr,
187 /*foreach_working_space_color*/ pointcloud_foreach_working_space_color,
188 /*owner_pointer_get*/ nullptr,
189
190 /*blend_write*/ pointcloud_blend_write,
191 /*blend_read_data*/ pointcloud_blend_read_data,
192 /*blend_read_after_liblink*/ nullptr,
193
194 /*blend_read_undo_preserve*/ nullptr,
195
196 /*lib_override_apply_post*/ nullptr,
197};
198
199Span<float3> PointCloud::positions() const
200{
202 this->attribute_storage.wrap(), blender::bke::AttrDomain::Point, "position", this->totpoint);
203}
204MutableSpan<float3> PointCloud::positions_for_write()
205{
207 this->attribute_storage.wrap(), blender::bke::AttrDomain::Point, "position", this->totpoint);
208}
209
210VArray<float> PointCloud::radius() const
211{
214 "radius",
215 this->totpoint,
216 0.01f);
217}
218MutableSpan<float> PointCloud::radius_for_write()
219{
222 "radius",
223 this->totpoint,
224 0.01f);
225}
226
228{
229 PointCloud *pointcloud = BKE_id_new<PointCloud>(bmain, name);
230
231 return pointcloud;
232}
233
235{
236 PointCloud *pointcloud = static_cast<PointCloud *>(BKE_libblock_alloc(
238
239 BKE_libblock_init_empty(&pointcloud->id);
240
241 pointcloud->totpoint = totpoint;
242
243 pointcloud->attributes_for_write().add<float3>(
245
246 return pointcloud;
247}
248
249void BKE_pointcloud_nomain_to_pointcloud(PointCloud *pointcloud_src, PointCloud *pointcloud_dst)
250{
251 BLI_assert(pointcloud_src->id.tag & ID_TAG_NO_MAIN);
252
253 pointcloud_dst->totpoint = pointcloud_src->totpoint;
254 pointcloud_dst->attribute_storage.wrap() = std::move(pointcloud_src->attribute_storage.wrap());
255 pointcloud_dst->runtime->bounds_cache = pointcloud_src->runtime->bounds_cache;
256 pointcloud_dst->runtime->bounds_with_radius_cache =
257 pointcloud_src->runtime->bounds_with_radius_cache;
258 pointcloud_dst->runtime->bvh_cache = pointcloud_src->runtime->bvh_cache;
259 BKE_id_free(nullptr, pointcloud_src);
260}
261
262std::optional<blender::Bounds<float3>> PointCloud::bounds_min_max(const bool use_radius) const
263{
264 using namespace blender;
265 using namespace blender::bke;
266 if (this->totpoint == 0) {
267 return std::nullopt;
268 }
269 if (use_radius) {
270 this->runtime->bounds_with_radius_cache.ensure([&](Bounds<float3> &r_bounds) {
271 const VArray<float> radius = this->radius();
272 if (const std::optional radius_single = radius.get_if_single()) {
273 r_bounds = *this->bounds_min_max(false);
274 r_bounds.pad(*radius_single);
275 return;
276 }
277 const Span radius_span = radius.get_internal_span();
278 r_bounds = *bounds::min_max_with_radii(this->positions(), radius_span);
279 });
280 }
281 else {
282 this->runtime->bounds_cache.ensure(
283 [&](Bounds<float3> &r_bounds) { r_bounds = *bounds::min_max(this->positions()); });
284 }
285 return use_radius ? this->runtime->bounds_with_radius_cache.data() :
286 this->runtime->bounds_cache.data();
287}
288
289std::optional<int> PointCloud::material_index_max() const
290{
291 if (this->totpoint == 0) {
292 return std::nullopt;
293 }
294 std::optional<int> max_material_index = blender::bounds::max<int>(
295 this->attributes()
296 .lookup_or_default<int>("material_index", blender::bke::AttrDomain::Point, 0)
297 .varray);
298 if (max_material_index.has_value()) {
299 max_material_index = std::clamp(*max_material_index, 0, MAXMAT);
300 }
301 return max_material_index;
302}
303
304void PointCloud::count_memory(blender::MemoryCounter &memory) const
305{
306 this->attribute_storage.wrap().count_memory(memory);
307}
308
310{
311 return blender::bke::AttributeAccessor(this,
313}
314
315blender::bke::MutableAttributeAccessor PointCloud::attributes_for_write()
316{
317 return blender::bke::MutableAttributeAccessor(
319}
320
323{
324 return name == ATTR_POSITION;
325}
326
328{
329 dst.flag = src.flag;
330 MEM_SAFE_FREE(dst.mat);
331 dst.mat = MEM_malloc_arrayN<Material *>(src.totcol, __func__);
332 dst.totcol = src.totcol;
333 MutableSpan(dst.mat, dst.totcol).copy_from(Span(src.mat, src.totcol));
334}
335
336/* Dependency Graph */
337
339{
340 return reinterpret_cast<PointCloud *>(
341 BKE_id_copy_ex(nullptr, &pointcloud_src->id, nullptr, LIB_ID_COPY_LOCALIZE));
342}
343
345 Scene *scene,
346 Object *object,
347 blender::bke::GeometrySet &geometry_set)
348{
349 /* Modifier evaluation modes. */
350 const bool use_render = (DEG_get_mode(depsgraph) == DAG_EVAL_RENDER);
351 const int required_mode = use_render ? eModifierMode_Render : eModifierMode_Realtime;
352 ModifierApplyFlag apply_flag = use_render ? MOD_APPLY_RENDER : MOD_APPLY_USECACHE;
353 const ModifierEvalContext mectx = {depsgraph, object, apply_flag};
354
356
357 /* Get effective list of modifiers to execute. Some effects like shape keys
358 * are added as virtual modifiers before the user created modifiers. */
359 VirtualModifierData virtual_modifier_data;
360 ModifierData *md = BKE_modifiers_get_virtual_modifierlist(object, &virtual_modifier_data);
361
362 /* Evaluate modifiers. */
363 for (; md; md = md->next) {
365
366 if (!BKE_modifier_is_enabled(scene, md, required_mode)) {
367 continue;
368 }
369
370 blender::bke::ScopedModifierTimer modifier_timer{*md};
371
372 if (mti->modify_geometry_set) {
373 mti->modify_geometry_set(md, &mectx, &geometry_set);
374 }
375 }
376}
377
379 blender::bke::GeometrySet &geometry_set)
380{
381 if (!geometry_set.has<blender::bke::PointCloudComponent>()) {
382 return nullptr;
383 }
384 blender::bke::PointCloudComponent &pointcloud_component =
386 PointCloud *pointcloud = pointcloud_component.release();
387 if (pointcloud != nullptr) {
388 /* Add back, but as read-only non-owning component. */
389 pointcloud_component.replace(pointcloud, blender::bke::GeometryOwnershipType::ReadOnly);
390 }
391 else {
392 /* The component was empty, we can also remove it. */
394 }
395 return pointcloud;
396}
397
398void BKE_pointcloud_data_update(Depsgraph *depsgraph, Scene *scene, Object *object)
399{
400 /* Free any evaluated data and restore original data. */
402
403 /* Evaluate modifiers. */
404 PointCloud *pointcloud = static_cast<PointCloud *>(object->data);
407 pointcloud_evaluate_modifiers(depsgraph, scene, object, geometry_set);
408
409 PointCloud *pointcloud_eval = take_pointcloud_ownership_from_geometry_set(geometry_set);
410
411 /* If the geometry set did not contain a point cloud, we still create an empty one. */
412 if (pointcloud_eval == nullptr) {
413 pointcloud_eval = BKE_pointcloud_new_nomain(0);
414 }
415
416 /* Assign evaluated object. */
417 const bool eval_is_owned = pointcloud_eval != pointcloud;
418 BKE_object_eval_assign_data(object, &pointcloud_eval->id, eval_is_owned);
419 object->runtime->geometry_set_eval = new blender::bke::GeometrySet(std::move(geometry_set));
420}
421
422void PointCloud::tag_positions_changed()
423{
424 this->runtime->bounds_cache.tag_dirty();
425 this->runtime->bounds_with_radius_cache.tag_dirty();
426 this->runtime->bvh_cache.tag_dirty();
427}
428
429void PointCloud::tag_radii_changed()
430{
431 this->runtime->bounds_with_radius_cache.tag_dirty();
432}
433
434/* Draw Cache */
435
436void (*BKE_pointcloud_batch_cache_dirty_tag_cb)(PointCloud *pointcloud, int mode) = nullptr;
437void (*BKE_pointcloud_batch_cache_free_cb)(PointCloud *pointcloud) = nullptr;
438
440{
441 if (pointcloud->batch_cache) {
443 }
444}
445
447{
448 if (pointcloud->batch_cache) {
450 }
451}
452
453namespace blender::bke {
454
456{
457 PointCloud *pointcloud = BKE_id_new_nomain<PointCloud>(nullptr);
458 pointcloud->totpoint = totpoint;
459 return pointcloud;
460}
461
462} // namespace blender::bke
void BKE_animdata_free(ID *id, bool do_id_user)
Definition anim_data.cc:188
CustomData interface, see also DNA_customdata_types.h.
void CustomData_reset(CustomData *data)
void CustomData_blend_read(BlendDataReader *reader, CustomData *data, int count)
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:47
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:164
IDTypeInfo IDType_ID_PT
void * BKE_libblock_alloc(Main *bmain, short type, const char *name, int flag) ATTR_WARN_UNUSED_RESULT
Definition lib_id.cc:1447
void BKE_id_free(Main *bmain, void *idv)
void BKE_libblock_init_empty(ID *id) ATTR_NONNULL(1)
Definition lib_id.cc:1452
ID * BKE_id_copy_ex(Main *bmain, const ID *id, ID **new_id_p, int flag)
Definition lib_id.cc:777
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1514
@ LIB_ID_CREATE_LOCALIZE
@ LIB_ID_COPY_LOCALIZE
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1519
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2631
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(data_, id_super_, cb_flag_)
@ IDWALK_CB_USER
void BKE_modifiers_clear_errors(Object *ob)
bool BKE_modifier_is_enabled(const Scene *scene, ModifierData *md, int required_mode)
const ModifierTypeInfo * BKE_modifier_get_info(ModifierType type)
ModifierData * BKE_modifiers_get_virtual_modifierlist(const Object *ob, VirtualModifierData *data)
ModifierApplyFlag
@ MOD_APPLY_USECACHE
@ MOD_APPLY_RENDER
General operations, lookup, etc. for blender objects.
void BKE_object_eval_assign_data(Object *object, ID *data, bool is_owned)
void BKE_object_free_derived_caches(Object *ob)
General operations for point clouds.
void(* BKE_pointcloud_batch_cache_free_cb)(PointCloud *pointcloud)
void(* BKE_pointcloud_batch_cache_dirty_tag_cb)(PointCloud *pointcloud, int mode)
#define BLI_assert(a)
Definition BLI_assert.h:46
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define BLO_write_id_struct(writer, struct_name, id_address, id)
void BLO_write_pointer_array(BlendWriter *writer, int64_t num, const void *data_ptr)
void BLO_read_pointer_array(BlendDataReader *reader, int64_t array_size, void **ptr_p)
Definition readfile.cc:5880
#define BLT_I18NCONTEXT_ID_POINTCLOUD
@ DAG_EVAL_RENDER
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
@ ID_TAG_NO_MAIN
Definition DNA_ID.h:978
#define FILTER_ID_MA
Definition DNA_ID.h:1208
#define FILTER_ID_PT
Definition DNA_ID.h:1229
@ INDEX_ID_PT
Definition DNA_ID.h:1329
@ ID_PT
#define DNA_struct_default_get(struct_name)
#define MAXMAT
@ eModifierMode_Render
@ eModifierMode_Realtime
Object is a sort of wrapper for general info.
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
static const char * ATTR_POSITION
BMesh const char void * data
BPy_StructRNA * depsgraph
AttributeSet attributes
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:739
std::optional< T > get_if_single() const
Span< T > get_internal_span() const
void replace(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
GMutableSpan get_mutable_attribute(AttributeStorage &storage, const AttrDomain domain, const CPPType &cpp_type, const StringRef name, const int64_t domain_size, const void *custom_default_value)
void attribute_storage_blend_write_prepare(AttributeStorage &data, AttributeStorage::BlendWriteData &write_data)
std::optional< GSpan > get_span_attribute(const AttributeStorage &storage, const AttrDomain domain, const CPPType &cpp_type, const StringRef name, const int64_t domain_size)
const AttributeAccessorFunctions & pointcloud_attribute_accessor_functions()
GVArray get_varray_attribute(const AttributeStorage &storage, AttrDomain domain, const CPPType &cpp_type, StringRef name, int64_t domain_size, const void *default_value)
PointCloud * pointcloud_new_no_attributes(int totpoint)
std::optional< T > max(const VArray< T > &values)
VecBase< float, 3 > float3
PointCloud * BKE_pointcloud_add(Main *bmain, const char *name)
static void pointcloud_init_data(ID *id)
Definition pointcloud.cc:60
PointCloud * BKE_pointcloud_copy_for_eval(const PointCloud *pointcloud_src)
void BKE_pointcloud_nomain_to_pointcloud(PointCloud *pointcloud_src, PointCloud *pointcloud_dst)
static void pointcloud_free_data(ID *id)
Definition pointcloud.cc:98
static void pointcloud_foreach_id(ID *id, LibraryForeachIDData *data)
PointCloud * BKE_pointcloud_new_nomain(const int totpoint)
void BKE_pointcloud_batch_cache_dirty_tag(PointCloud *pointcloud, int mode)
void BKE_pointcloud_batch_cache_free(PointCloud *pointcloud)
static void pointcloud_foreach_working_space_color(ID *id, const IDTypeForeachColorFunctionCallback &fn)
static PointCloud * take_pointcloud_ownership_from_geometry_set(blender::bke::GeometrySet &geometry_set)
static void pointcloud_blend_write(BlendWriter *writer, ID *id, const void *id_address)
void BKE_pointcloud_data_update(Depsgraph *depsgraph, Scene *scene, Object *object)
static void pointcloud_blend_read_data(BlendDataReader *reader, ID *id)
void pointcloud_copy_parameters(const PointCloud &src, PointCloud &dst)
static void pointcloud_copy_data(Main *, std::optional< Library * >, ID *id_dst, const ID *id_src, const int)
Definition pointcloud.cc:71
void(* BKE_pointcloud_batch_cache_free_cb)(PointCloud *pointcloud)
static void pointcloud_evaluate_modifiers(Depsgraph *depsgraph, Scene *scene, Object *object, blender::bke::GeometrySet &geometry_set)
void(* BKE_pointcloud_batch_cache_dirty_tag_cb)(PointCloud *pointcloud, int mode)
bool BKE_pointcloud_attribute_required(const PointCloud *, const blender::StringRef name)
const char * name
struct Attribute * dna_attributes
void pad(const PaddingT &padding)
Definition DNA_ID.h:414
int tag
Definition DNA_ID.h:442
struct ModifierData * next
void(* modify_geometry_set)(ModifierData *md, const ModifierEvalContext *ctx, blender::bke::GeometrySet *geometry_set)
struct Material ** mat
PointCloudRuntimeHandle * runtime
struct AttributeStorage attribute_storage
struct CustomData pdata_legacy
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
bool has(const GeometryComponent::Type component_type) const
static GeometrySet from_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void remove(const GeometryComponent::Type component_type)
i
Definition text_draw.cc:230
#define N_(msgid)