Blender V4.3
BKE_geometry_set.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
11#include <iosfwd>
12#include <mutex>
13
14#include "BLI_bounds_types.hh"
15#include "BLI_function_ref.hh"
17#include "BLI_map.hh"
20
21/* For #Map. */
22#include "BKE_attribute.hh"
23
24struct Curves;
25struct Curve;
26struct Mesh;
27struct PointCloud;
28struct Volume;
29struct GreasePencil;
30namespace blender::bke {
31struct AttributeKind;
32class AttributeAccessor;
33struct AttributeMetaData;
34class ComponentAttributeProviders;
35class CurvesEditHints;
36class Instances;
37class GeometryComponent;
38class GreasePencilEditHints;
39class MutableAttributeAccessor;
40enum class AttrDomain : int8_t;
41struct GizmoEditHints;
42} // namespace blender::bke
43
44namespace blender::bke {
45
46#define GEO_COMPONENT_TYPE_ENUM_SIZE 7
47
49 /* The geometry is owned. This implies that it can be changed. */
50 Owned = 0,
51 /* The geometry can be changed, but someone else is responsible for freeing it. */
52 Editable = 1,
53 /* The geometry cannot be changed and someone else is responsible for freeing it. */
54 ReadOnly = 2,
55};
56
58
65 public:
72 enum class Type {
73 Mesh = 0,
74 PointCloud = 1,
75 Instance = 2,
76 Volume = 3,
77 Curve = 4,
78 Edit = 5,
79 GreasePencil = 6,
80 };
81
82 private:
83 Type type_;
84
85 public:
86 GeometryComponent(Type type);
87 virtual ~GeometryComponent() = default;
88 static GeometryComponentPtr create(Type component_type);
89
90 int attribute_domain_size(AttrDomain domain) const;
91
96 virtual std::optional<AttributeAccessor> attributes() const;
97 virtual std::optional<MutableAttributeAccessor> attributes_for_write();
98
99 virtual void count_memory(MemoryCounter &memory) const;
100
104 virtual GeometryComponentPtr copy() const = 0;
105
107 virtual void clear() = 0;
108
109 /* Direct data is everything except for instances of objects/collections.
110 * If this returns true, the geometry set can be cached and is still valid after e.g. modifier
111 * evaluation ends. Instances can only be valid as long as the data they instance is valid. */
112 virtual bool owns_direct_data() const = 0;
113 virtual void ensure_owns_direct_data() = 0;
114
115 Type type() const;
116
117 virtual bool is_empty() const;
118
119 private:
120 void delete_self() override;
121 void delete_data_only() override;
122};
123
124template<typename T>
125inline constexpr bool is_geometry_component_v = std::is_base_of_v<GeometryComponent, T>;
126
147 private:
148 /* Indexed by #GeometryComponent::Type. */
149 std::array<GeometryComponentPtr, GEO_COMPONENT_TYPE_ENUM_SIZE> components_;
150
151 public:
157 std::string name;
158
168
174 template<typename Component> Component &get_component_for_write()
175 {
177 return static_cast<Component &>(this->get_component_for_write(Component::static_type));
178 }
179
183 const GeometryComponent *get_component(GeometryComponent::Type component_type) const;
184 template<typename Component> const Component *get_component() const
185 {
187 return static_cast<const Component *>(get_component(Component::static_type));
188 }
189
190 bool has(const GeometryComponent::Type component_type) const;
191 template<typename Component> bool has() const
192 {
194 return this->has(Component::static_type);
195 }
196
197 template<typename Component> bool has_component() const
198 {
200 return components_[int(Component::static_type)];
201 }
202
203 void remove(const GeometryComponent::Type component_type);
204 template<typename Component> void remove()
205 {
207 return this->remove(Component::static_type);
208 }
209
213 void keep_only(Span<GeometryComponent::Type> component_types);
220
221 void add(const GeometryComponent &component);
222
227
228 std::optional<Bounds<float3>> compute_boundbox_without_instances() const;
229
230 friend std::ostream &operator<<(std::ostream &stream, const GeometrySet &geometry_set);
231
235 void clear();
236
237 bool owns_direct_data() const;
249
251 const AttributeMetaData &meta_data,
252 const GeometryComponent &component)>;
253
255 bool include_instances,
257
259 const AttributeAccessor src_attributes,
260 MutableAttributeAccessor dst_attributes,
261 const AttributeFilter &attribute_filter);
262
264 GeometryComponent::Type dst_component_type,
265 bool include_instances,
266 const AttributeFilter &attribute_filter,
267 Map<StringRef, AttributeKind> &r_attributes) const;
268
270 bool ignore_empty) const;
271
273
279
280 /* Utility methods for creation. */
284 static GeometrySet from_mesh(Mesh *mesh,
289 static GeometrySet from_volume(Volume *volume,
299 static GeometrySet from_curves(Curves *curves,
311
312 /* Utility methods for access. */
316 bool has_mesh() const;
320 bool has_pointcloud() const;
324 bool has_instances() const;
328 bool has_volume() const;
332 bool has_curves() const;
336 bool has_realized_data() const;
340 bool has_grease_pencil() const;
344 bool is_empty() const;
345
349 const Mesh *get_mesh() const;
353 const PointCloud *get_pointcloud() const;
357 const Volume *get_volume() const;
361 const Curves *get_curves() const;
365 const Instances *get_instances() const;
377 const GreasePencil *get_grease_pencil() const;
378
411
412 /* Utility methods for replacement. */
420 void replace_pointcloud(PointCloud *pointcloud,
425 void replace_volume(Volume *volume,
430 void replace_curves(Curves *curves,
435 void replace_instances(Instances *instances,
440 void replace_grease_pencil(GreasePencil *grease_pencil,
442
443 friend bool operator==(const GeometrySet &a, const GeometrySet &b)
444 {
445 /* This compares only the component pointers, not the actual geometry data. */
446 return Span(a.components_) == Span(b.components_) && a.name == b.name;
447 }
448
450 {
451 /* This should have the same data that's also taken into account in #operator==. */
452 return get_default_hash(Span(components_), this->name);
453 }
454
455 void count_memory(MemoryCounter &memory) const;
456
457 private:
462 GeometryComponent *get_component_ptr(GeometryComponent::Type type);
463 template<typename Component> Component *get_component_ptr()
464 {
466 return static_cast<Component *>(get_component_ptr(Component::static_type));
467 }
468};
469
475 private:
476 Mesh *mesh_ = nullptr;
478
479 public:
483 GeometryComponentPtr copy() const override;
484
485 void clear() override;
486 bool has_mesh() const;
495 Mesh *release();
496
501 const Mesh *get() const;
507
508 bool is_empty() const final;
509
510 bool owns_direct_data() const override;
511 void ensure_owns_direct_data() override;
512
513 void count_memory(MemoryCounter &memory) const override;
514
516
517 std::optional<AttributeAccessor> attributes() const final;
518 std::optional<MutableAttributeAccessor> attributes_for_write() final;
519};
520
532 private:
533 PointCloud *pointcloud_ = nullptr;
535
536 public:
541 GeometryComponentPtr copy() const override;
542
543 void clear() override;
544 bool has_pointcloud() const;
548 void replace(PointCloud *pointcloud,
555
561 const PointCloud *get() const;
568
569 bool is_empty() const final;
570
571 bool owns_direct_data() const override;
572 void ensure_owns_direct_data() override;
573
574 void count_memory(MemoryCounter &memory) const override;
575
576 std::optional<AttributeAccessor> attributes() const final;
577 std::optional<MutableAttributeAccessor> attributes_for_write() final;
578
580};
581
588 private:
589 Curves *curves_ = nullptr;
590 GeometryOwnershipType ownership_ = GeometryOwnershipType::Owned;
591
597 mutable Curve *curve_for_render_ = nullptr;
598 mutable std::mutex curve_for_render_mutex_;
599
600 public:
602 CurveComponent(Curves *curve, GeometryOwnershipType ownership = GeometryOwnershipType::Owned);
604 GeometryComponentPtr copy() const override;
605
606 void clear() override;
607 bool has_curves() const;
611 void replace(Curves *curve, GeometryOwnershipType ownership = GeometryOwnershipType::Owned);
612 Curves *release();
613
614 const Curves *get() const;
615 Curves *get_for_write();
616
617 bool is_empty() const final;
618
619 bool owns_direct_data() const override;
620 void ensure_owns_direct_data() override;
621
622 void count_memory(MemoryCounter &memory) const override;
623
628 const Curve *get_curve_for_render() const;
629
630 std::optional<AttributeAccessor> attributes() const final;
631 std::optional<MutableAttributeAccessor> attributes_for_write() final;
632
633 static constexpr inline GeometryComponent::Type static_type = Type::Curve;
634};
635
640 private:
641 Instances *instances_ = nullptr;
642 GeometryOwnershipType ownership_ = GeometryOwnershipType::Owned;
643
644 public:
646 InstancesComponent(Instances *instances,
647 GeometryOwnershipType ownership = GeometryOwnershipType::Owned);
649 GeometryComponentPtr copy() const override;
650
651 void clear() override;
652
653 const Instances *get() const;
654 Instances *get_for_write();
655
656 void replace(Instances *instances,
657 GeometryOwnershipType ownership = GeometryOwnershipType::Owned);
658
659 bool is_empty() const final;
660
661 Instances *release();
662
663 bool owns_direct_data() const override;
664 void ensure_owns_direct_data() override;
665
666 void count_memory(MemoryCounter &memory) const override;
667
668 std::optional<AttributeAccessor> attributes() const final;
669 std::optional<MutableAttributeAccessor> attributes_for_write() final;
670
671 static constexpr inline GeometryComponent::Type static_type = Type::Instance;
672};
673
680 private:
681 Volume *volume_ = nullptr;
682 GeometryOwnershipType ownership_ = GeometryOwnershipType::Owned;
683
684 public:
687 GeometryComponentPtr copy() const override;
688
689 void clear() override;
690 bool has_volume() const;
694 void replace(Volume *volume, GeometryOwnershipType ownership = GeometryOwnershipType::Owned);
699 Volume *release();
700
705 const Volume *get() const;
711 Volume *get_for_write();
712
713 bool owns_direct_data() const override;
714 void ensure_owns_direct_data() override;
715
716 void count_memory(MemoryCounter &memory) const override;
717
718 static constexpr inline GeometryComponent::Type static_type = Type::Volume;
719};
720
729 public:
736 std::unique_ptr<CurvesEditHints> curves_edit_hints_;
740 std::unique_ptr<GreasePencilEditHints> grease_pencil_edit_hints_;
744 std::unique_ptr<GizmoEditHints> gizmo_edit_hints_;
745
747
748 GeometryComponentPtr copy() const final;
749 bool owns_direct_data() const final;
750 void ensure_owns_direct_data() final;
751
752 void clear() override;
753
760 static void remember_deformed_positions_if_necessary(GeometrySet &geometry);
761
762 static constexpr inline GeometryComponent::Type static_type = GeometryComponent::Type::Edit;
763};
764
771 private:
772 GreasePencil *grease_pencil_ = nullptr;
773 GeometryOwnershipType ownership_ = GeometryOwnershipType::Owned;
774
775 public:
778 GeometryComponentPtr copy() const override;
779
780 void clear() override;
781 bool has_grease_pencil() const;
785 void replace(GreasePencil *grease_pencil,
786 GeometryOwnershipType ownership = GeometryOwnershipType::Owned);
791 GreasePencil *release();
792
793 const GreasePencil *get() const;
794 GreasePencil *get_for_write();
795
796 bool is_empty() const final;
797
798 bool owns_direct_data() const override;
799 void ensure_owns_direct_data() override;
800
801 static constexpr inline GeometryComponent::Type static_type = Type::GreasePencil;
802
803 std::optional<AttributeAccessor> attributes() const final;
804 std::optional<MutableAttributeAccessor> attributes_for_write() final;
805};
806
807bool attribute_is_builtin_on_component_type(const GeometryComponent::Type type, StringRef name);
808
809} // namespace blender::bke
#define BLI_STATIC_ASSERT(a, msg)
Definition BLI_assert.h:87
std::unique_ptr< GreasePencilEditHints > grease_pencil_edit_hints_
std::unique_ptr< GizmoEditHints > gizmo_edit_hints_
std::unique_ptr< CurvesEditHints > curves_edit_hints_
int attribute_domain_size(AttrDomain domain) const
virtual std::optional< AttributeAccessor > attributes() const
virtual ~GeometryComponent()=default
virtual std::optional< MutableAttributeAccessor > attributes_for_write()
virtual bool is_empty() const
virtual void count_memory(MemoryCounter &memory) const
virtual void ensure_owns_direct_data()=0
virtual bool owns_direct_data() const =0
virtual GeometryComponentPtr copy() const =0
static GeometryComponentPtr create(Type component_type)
static constexpr GeometryComponent::Type static_type
void replace(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
GeometryComponentPtr copy() const override
void count_memory(MemoryCounter &memory) const override
std::optional< AttributeAccessor > attributes() const final
std::optional< MutableAttributeAccessor > attributes_for_write() final
local_group_size(16, 16) .push_constant(Type b
DEGForeachIDComponentCallback callback
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
static void clear(Message &msg)
Definition msgfmt.cc:218
constexpr bool is_geometry_component_v
uint64_t get_default_hash(const T &v)
Definition BLI_hash.hh:219
static void copy(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node)
unsigned __int64 uint64_t
Definition stdint.h:90
signed char int8_t
Definition stdint.h:75
GeometrySet & operator=(GeometrySet &&other)
void replace_volume(Volume *volume, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void keep_only(Span< GeometryComponent::Type > component_types)
const CurvesEditHints * get_curve_edit_hints() const
GeometrySet & operator=(const GeometrySet &other)
static GeometrySet from_instances(Instances *instances, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
static GeometrySet from_grease_pencil(GreasePencil *grease_pencil, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
PointCloud * get_pointcloud_for_write()
static GeometrySet from_curves(Curves *curves, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
GizmoEditHints * get_gizmo_edit_hints_for_write()
void replace_instances(Instances *instances, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
Instances * get_instances_for_write()
const GreasePencil * get_grease_pencil() const
const Component * get_component() const
const GizmoEditHints * get_gizmo_edit_hints() const
Vector< const GeometryComponent * > get_components() const
const Volume * get_volume() const
static GeometrySet from_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
friend std::ostream & operator<<(std::ostream &stream, const GeometrySet &geometry_set)
void gather_attributes_for_propagation(Span< GeometryComponent::Type > component_types, GeometryComponent::Type dst_component_type, bool include_instances, const AttributeFilter &attribute_filter, Map< StringRef, AttributeKind > &r_attributes) const
void replace_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void keep_only_during_modify(Span< GeometryComponent::Type > component_types)
const Curves * get_curves() const
static GeometrySet from_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const Instances * get_instances() const
static GeometrySet from_volume(Volume *volume, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
static void propagate_attributes_from_layer_to_instances(const AttributeAccessor src_attributes, MutableAttributeAccessor dst_attributes, const AttributeFilter &attribute_filter)
GeometrySet(const GeometrySet &other)
void replace_curves(Curves *curves, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
CurvesEditHints * get_curve_edit_hints_for_write()
friend bool operator==(const GeometrySet &a, const GeometrySet &b)
void count_memory(MemoryCounter &memory) const
const PointCloud * get_pointcloud() const
const Mesh * get_mesh() const
void modify_geometry_sets(ForeachSubGeometryCallback callback)
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
GreasePencil * get_grease_pencil_for_write()
std::optional< Bounds< float3 > > compute_boundbox_without_instances() const
Vector< GeometryComponent::Type > gather_component_types(bool include_instances, bool ignore_empty) const
void attribute_foreach(Span< GeometryComponent::Type > component_types, bool include_instances, AttributeForeachCallback callback) const
void add(const GeometryComponent &component)
void replace_grease_pencil(GreasePencil *grease_pencil, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
GeometrySet(GeometrySet &&other)
bool override
Definition wm_files.cc:1167