Blender V5.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
10
11#include <iosfwd>
12#include <optional>
13
14#include "BLI_bounds_types.hh"
15#include "BLI_function_ref.hh"
19#include "BLI_mutex.hh"
20#include "BLI_vector_set.hh"
21
22#include "BKE_attribute.hh"
23
24namespace blender {
25
26struct Curves;
27struct Curve;
28struct Mesh;
29struct PointCloud;
30struct Volume;
31struct GreasePencil;
32namespace bke {
36class CurvesEditHints;
37class Instances;
41enum class AttrDomain : int8_t;
42struct GizmoEditHints;
43} // namespace bke
44namespace nodes {
45class Bundle;
47} // namespace nodes
48
49namespace bke {
50
51#define GEO_COMPONENT_TYPE_ENUM_SIZE 7
52
54 /* The geometry is owned. This implies that it can be changed. */
55 Owned = 0,
56 /* The geometry can be changed, but someone else is responsible for freeing it. */
58 /* The geometry cannot be changed and someone else is responsible for freeing it. */
60};
61
63
70 public:
77 enum class Type {
78 Mesh = 0,
81 Volume = 3,
82 Curve = 4,
83 Edit = 5,
85 };
86
87 private:
88 Type type_;
89
90 public:
92 ~GeometryComponent() override = default;
93 static GeometryComponentPtr create(Type component_type);
94
95 int attribute_domain_size(AttrDomain domain) const;
96
101 virtual std::optional<AttributeAccessor> attributes() const;
102 virtual std::optional<MutableAttributeAccessor> attributes_for_write();
103
104 virtual void count_memory(MemoryCounter &memory) const;
105
109 virtual GeometryComponentPtr copy() const = 0;
110
112 virtual void clear() = 0;
113
114 /* Direct data is everything except for instances of objects/collections.
115 * If this returns true, the geometry set can be cached and is still valid after e.g. modifier
116 * evaluation ends. Instances can only be valid as long as the data they instance is valid. */
117 virtual bool owns_direct_data() const = 0;
118 virtual void ensure_owns_direct_data() = 0;
119
120 Type type() const;
121
122 virtual bool is_empty() const;
123
124 private:
125 void delete_self() override;
126 void delete_data_only() override;
127};
128
129template<typename T>
130inline constexpr bool is_geometry_component_v = std::is_base_of_v<GeometryComponent, T>;
131
152 private:
153 /* Indexed by #GeometryComponent::Type. */
154 std::array<GeometryComponentPtr, GEO_COMPONENT_TYPE_ENUM_SIZE> components_;
155 nodes::BundlePtr bundle_;
156
162 std::string name_;
163
164 public:
174
180 template<typename Component> Component &get_component_for_write()
181 {
183 return static_cast<Component &>(this->get_component_for_write(Component::static_type));
184 }
185
187 {
188 return components_[int(component_type)];
189 }
190
194 const GeometryComponent *get_component(GeometryComponent::Type component_type) const;
195 template<typename Component> const Component *get_component() const
196 {
198 return static_cast<const Component *>(get_component(Component::static_type));
199 }
200
201 bool has(const GeometryComponent::Type component_type) const;
202 template<typename Component> bool has() const
203 {
205 return this->has(Component::static_type);
206 }
207
208 template<typename Component> bool has_component() const
209 {
211 return components_[int(Component::static_type)];
212 }
213
214 void remove(const GeometryComponent::Type component_type);
215 template<typename Component> void remove()
216 {
218 return this->remove(Component::static_type);
219 }
220
224 void keep_only(Span<GeometryComponent::Type> component_types);
225
226 void add(const GeometryComponent &component);
227
232
233 std::optional<Bounds<float3>> compute_boundbox_without_instances(bool use_radius = true,
234 bool use_subdiv = false) const;
235
236 friend std::ostream &operator<<(std::ostream &stream, const GeometrySet &geometry_set);
237
241 void clear();
242
243 bool owns_direct_data() const;
262
264 StringRef name, const AttributeMetaData &meta_data, const GeometryComponent &component)>;
265
267 bool include_instances,
268 AttributeForeachCallback callback) const;
269
275
277 bool ignore_empty) const;
278
279 /* Utility methods for creation. */
288 static GeometrySet from_volume(Volume *volume,
303 static GeometrySet from_instances(std::unique_ptr<Instances> instances);
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;
381 const GreasePencil *get_grease_pencil() const;
382
419
420 /* Utility methods for replacement. */
428 void replace_pointcloud(PointCloud *pointcloud,
433 void replace_volume(Volume *volume,
443 void replace_instances(Instances *instances,
450
451 bool has_bundle() const;
452 const nodes::Bundle *bundle() const;
453 const nodes::BundlePtr &bundle_ptr() const;
456
457 void copy_bundle_from(const GeometrySet &other);
458 void merge_bundle_from(const GeometrySet &other);
459
460 void set_name(std::string name);
461 StringRefNull name() const;
462
463 friend bool operator==(const GeometrySet &a, const GeometrySet &b)
464 {
465 /* This compares only the component pointers, not the actual geometry data. */
466 return Span(a.components_) == Span(b.components_) && a.name_ == b.name_ &&
467 a.bundle_ == b.bundle_;
468 }
469
471 {
472 /* This should have the same data that's also taken into account in #operator==. */
473 return get_default_hash(Span(components_), name_, bundle_.get());
474 }
475
476 void count_memory(MemoryCounter &memory) const;
477
478 private:
483 GeometryComponent *get_component_for_write_ptr(GeometryComponent::Type type);
484 template<typename Component> Component *get_component_for_write_ptr()
485 {
487 return static_cast<Component *>(get_component_for_write_ptr(Component::static_type));
488 }
489};
490
496 private:
497 Mesh *mesh_ = nullptr;
499
500 public:
503 ~MeshComponent() override;
504 GeometryComponentPtr copy() const override;
505
506 void clear() override;
507 bool has_mesh() const;
516 Mesh *release();
517
522 const Mesh *get() const;
528
529 bool is_empty() const final;
530
531 bool owns_direct_data() const override;
532 void ensure_owns_direct_data() override;
533
534 void count_memory(MemoryCounter &memory) const override;
535
537
538 std::optional<AttributeAccessor> attributes() const final;
540};
541
553 private:
554 PointCloud *pointcloud_ = nullptr;
556
557 public:
561 ~PointCloudComponent() override;
562 GeometryComponentPtr copy() const override;
563
564 void clear() override;
565 bool has_pointcloud() const;
569 void replace(PointCloud *pointcloud,
576
582 const PointCloud *get() const;
589
590 bool is_empty() const final;
591
592 bool owns_direct_data() const override;
593 void ensure_owns_direct_data() override;
594
595 void count_memory(MemoryCounter &memory) const override;
596
597 std::optional<AttributeAccessor> attributes() const final;
598 std::optional<MutableAttributeAccessor> attributes_for_write() final;
599
601};
602
609 private:
610 Curves *curves_ = nullptr;
612
618 mutable Curve *curve_for_render_ = nullptr;
619 mutable Mutex curve_for_render_mutex_;
620
621 public:
624 ~CurveComponent() override;
625 GeometryComponentPtr copy() const override;
626
627 void clear() override;
628 bool has_curves() const;
633 Curves *release();
634
635 const Curves *get() const;
637
638 bool is_empty() const final;
639
640 bool owns_direct_data() const override;
641 void ensure_owns_direct_data() override;
642
643 void count_memory(MemoryCounter &memory) const override;
644
649 const Curve *get_curve_for_render() const;
650
651 std::optional<AttributeAccessor> attributes() const final;
652 std::optional<MutableAttributeAccessor> attributes_for_write() final;
653
655};
656
661 private:
662 Instances *instances_ = nullptr;
664
665 public:
667 InstancesComponent(Instances *instances,
669 ~InstancesComponent() override;
670 GeometryComponentPtr copy() const override;
671
672 void clear() override;
673
674 const Instances *get() const;
676
677 void replace(Instances *instances,
679
680 bool is_empty() const final;
681
683
684 bool owns_direct_data() const override;
685 void ensure_owns_direct_data() override;
686
687 void count_memory(MemoryCounter &memory) const override;
688
689 std::optional<AttributeAccessor> attributes() const final;
690 std::optional<MutableAttributeAccessor> attributes_for_write() final;
691
693};
694
701 private:
702 Volume *volume_ = nullptr;
704
705 public:
707 ~VolumeComponent() override;
708 GeometryComponentPtr copy() const override;
709
710 void clear() override;
711 bool has_volume() const;
720 Volume *release();
721
726 const Volume *get() const;
733
734 bool owns_direct_data() const override;
735 void ensure_owns_direct_data() override;
736
737 void count_memory(MemoryCounter &memory) const override;
738
740};
741
760
769 public:
776 std::unique_ptr<CurvesEditHints> curves_edit_hints_;
780 std::unique_ptr<GreasePencilEditHints> grease_pencil_edit_hints_;
784 std::unique_ptr<GizmoEditHints> gizmo_edit_hints_;
785
786 std::unique_ptr<MeshEditHints> mesh_edit_hints_;
787
789
791 bool owns_direct_data() const final;
793
794 void clear() override;
795
803
805};
806
813 private:
814 GreasePencil *grease_pencil_ = nullptr;
816
817 public:
819 ~GreasePencilComponent() override;
820 GeometryComponentPtr copy() const override;
821
822 void clear() override;
823 bool has_grease_pencil() const;
834
835 const GreasePencil *get() const;
837
838 bool is_empty() const final;
839
840 bool owns_direct_data() const override;
841 void ensure_owns_direct_data() override;
842
844
845 std::optional<AttributeAccessor> attributes() const final;
847};
848
850
851} // namespace bke
852} // namespace blender
#define final(a, b, c)
Definition BLI_hash_c.hh:21
constexpr Span()=default
void count_memory(MemoryCounter &memory) const override
static constexpr GeometryComponent::Type static_type
void replace(Curves *curve, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
GeometryComponentPtr copy() const override
std::optional< AttributeAccessor > attributes() const final
std::optional< MutableAttributeAccessor > attributes_for_write() final
std::unique_ptr< GreasePencilEditHints > grease_pencil_edit_hints_
std::unique_ptr< MeshEditHints > mesh_edit_hints_
std::unique_ptr< GizmoEditHints > gizmo_edit_hints_
static constexpr GeometryComponent::Type static_type
std::unique_ptr< CurvesEditHints > curves_edit_hints_
static void remember_deformed_positions_if_necessary(GeometrySet &geometry)
int attribute_domain_size(AttrDomain domain) const
virtual std::optional< AttributeAccessor > attributes() const
~GeometryComponent() override=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)
std::optional< AttributeAccessor > attributes() const final
static constexpr GeometryComponent::Type static_type
std::optional< MutableAttributeAccessor > attributes_for_write() final
GeometryComponentPtr copy() const override
void replace(GreasePencil *grease_pencil, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
std::optional< MutableAttributeAccessor > attributes_for_write() final
static constexpr GeometryComponent::Type static_type
void count_memory(MemoryCounter &memory) const override
GeometryComponentPtr copy() const override
std::optional< AttributeAccessor > attributes() const final
void replace(Instances *instances, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
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
GeometryComponentPtr copy() const override
void count_memory(MemoryCounter &memory) const override
static constexpr GeometryComponent::Type static_type
void replace(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
std::optional< MutableAttributeAccessor > attributes_for_write() final
std::optional< AttributeAccessor > attributes() const final
void count_memory(MemoryCounter &memory) const override
static constexpr GeometryComponent::Type static_type
GeometryComponentPtr copy() const override
void replace(Volume *volume, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
A running instance of the engine.
ImplicitSharingPtr< GeometryComponent > GeometryComponentPtr
constexpr bool is_geometry_component_v
bool attribute_is_builtin_on_component_type(const GeometryComponent::Type type, StringRef name)
std::ostream & operator<<(std::ostream &stream, const GeometrySet &geometry_set)
ImplicitSharingPtr< Bundle > BundlePtr
BMesh Iterators.
unsigned __int64 uint64_t
Definition BKE_text.h:91
BLI_STATIC_ASSERT(ARRAY_SIZE(data_mode_strings)==CTX_MODE_NUM+1, "Must have a string for each context mode") const char *CTX_data_mode_string(const bContext *C)
std::mutex Mutex
Definition BLI_mutex.hh:51
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
signed char int8_t
Definition BKE_text.h:76
void add(const StringRef name, const AttributeDomainAndType &kind)
Vector< AttributeDomainAndType, 16 > kinds
GeometrySet & operator=(GeometrySet &&other)
void merge_bundle_from(const 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
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
GeometrySet & operator=(const GeometrySet &other)
StringRefNull name() const
GeometryComponentPtr get_component_ptr(GeometryComponent::Type component_type) const
void set_name(std::string name)
FunctionRef< void( StringRef name, const AttributeMetaData &meta_data, const GeometryComponent &component)> AttributeForeachCallback
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
nodes::Bundle & bundle_for_write()
const Component * get_component() const
const GizmoEditHints * get_gizmo_edit_hints() const
Vector< const GeometryComponent * > get_components() const
const Volume * get_volume() const
std::optional< Bounds< float3 > > compute_boundbox_without_instances(bool use_radius=true, bool use_subdiv=false) const
static GeometrySet from_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
bool has(const GeometryComponent::Type component_type) const
void replace_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const nodes::Bundle * bundle() const
static GeometrySet from_instances(std::unique_ptr< Instances > instances)
const GeometryComponent * get_component(GeometryComponent::Type component_type) const
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)
GeometrySet(const GeometrySet &other)
void copy_bundle_from(const GeometrySet &other)
GreasePencilEditHints * get_grease_pencil_edit_hints_for_write()
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 GreasePencilEditHints * get_grease_pencil_edit_hints() const
const PointCloud * get_pointcloud() const
const Mesh * get_mesh() const
void remove(const GeometryComponent::Type component_type)
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
GreasePencil * get_grease_pencil_for_write()
Vector< GeometryComponent::Type > gather_component_types(bool include_instances, bool ignore_empty) const
const nodes::BundlePtr & bundle_ptr() 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)
GeometryComponentPtr mesh_deform
GeometryComponentPtr mesh_cage