Blender V4.5
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
13#include "BLI_bounds_types.hh"
14#include "BLI_function_ref.hh"
16#include "BLI_map.hh"
19#include "BLI_mutex.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 {
34class CurvesEditHints;
35class Instances;
39enum class AttrDomain : int8_t;
40struct GizmoEditHints;
41} // namespace blender::bke
42
43namespace blender::bke {
44
45#define GEO_COMPONENT_TYPE_ENUM_SIZE 7
46
48 /* The geometry is owned. This implies that it can be changed. */
49 Owned = 0,
50 /* The geometry can be changed, but someone else is responsible for freeing it. */
52 /* The geometry cannot be changed and someone else is responsible for freeing it. */
54};
55
57
64 public:
71 enum class Type {
72 Mesh = 0,
75 Volume = 3,
76 Curve = 4,
77 Edit = 5,
79 };
80
81 private:
82 Type type_;
83
84 public:
86 ~GeometryComponent() override = default;
87 static GeometryComponentPtr create(Type component_type);
88
89 int attribute_domain_size(AttrDomain domain) const;
90
95 virtual std::optional<AttributeAccessor> attributes() const;
96 virtual std::optional<MutableAttributeAccessor> attributes_for_write();
97
98 virtual void count_memory(MemoryCounter &memory) const;
99
103 virtual GeometryComponentPtr copy() const = 0;
104
106 virtual void clear() = 0;
107
108 /* Direct data is everything except for instances of objects/collections.
109 * If this returns true, the geometry set can be cached and is still valid after e.g. modifier
110 * evaluation ends. Instances can only be valid as long as the data they instance is valid. */
111 virtual bool owns_direct_data() const = 0;
112 virtual void ensure_owns_direct_data() = 0;
113
114 Type type() const;
115
116 virtual bool is_empty() const;
117
118 private:
119 void delete_self() override;
120 void delete_data_only() override;
121};
122
123template<typename T>
124inline constexpr bool is_geometry_component_v = std::is_base_of_v<GeometryComponent, T>;
125
146 private:
147 /* Indexed by #GeometryComponent::Type. */
148 std::array<GeometryComponentPtr, GEO_COMPONENT_TYPE_ENUM_SIZE> components_;
149
150 public:
156 std::string name;
157
167
173 template<typename Component> Component &get_component_for_write()
174 {
176 return static_cast<Component &>(this->get_component_for_write(Component::static_type));
177 }
178
182 const GeometryComponent *get_component(GeometryComponent::Type component_type) const;
183 template<typename Component> const Component *get_component() const
184 {
186 return static_cast<const Component *>(get_component(Component::static_type));
187 }
188
189 bool has(const GeometryComponent::Type component_type) const;
190 template<typename Component> bool has() const
191 {
193 return this->has(Component::static_type);
194 }
195
196 template<typename Component> bool has_component() const
197 {
199 return components_[int(Component::static_type)];
200 }
201
202 void remove(const GeometryComponent::Type component_type);
203 template<typename Component> void remove()
204 {
206 return this->remove(Component::static_type);
207 }
208
212 void keep_only(Span<GeometryComponent::Type> component_types);
219
220 void add(const GeometryComponent &component);
221
226
227 std::optional<Bounds<float3>> compute_boundbox_without_instances(bool use_radius = true,
228 bool use_subdiv = false) 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;
256
258 const AttributeMetaData &meta_data,
259 const GeometryComponent &component)>;
260
262 bool include_instances,
263 AttributeForeachCallback callback) const;
264
266 Span<GeometryComponent::Type> component_types,
267 GeometryComponent::Type dst_component_type,
268 bool include_instances,
269 const AttributeFilter &attribute_filter,
270 Map<StringRef, AttributeDomainAndType> &r_attributes) const;
271
273 bool ignore_empty) const;
274
276
282
283 /* Utility methods for creation. */
292 static GeometrySet from_volume(Volume *volume,
314
315 /* Utility methods for access. */
319 bool has_mesh() const;
323 bool has_pointcloud() const;
327 bool has_instances() const;
331 bool has_volume() const;
335 bool has_curves() const;
339 bool has_realized_data() const;
343 bool has_grease_pencil() const;
347 bool is_empty() const;
348
352 const Mesh *get_mesh() const;
356 const PointCloud *get_pointcloud() const;
360 const Volume *get_volume() const;
364 const Curves *get_curves() const;
368 const Instances *get_instances() const;
384 const GreasePencil *get_grease_pencil() const;
385
422
423 /* Utility methods for replacement. */
431 void replace_pointcloud(PointCloud *pointcloud,
436 void replace_volume(Volume *volume,
446 void replace_instances(Instances *instances,
451 void replace_grease_pencil(GreasePencil *grease_pencil,
453
454 friend bool operator==(const GeometrySet &a, const GeometrySet &b)
455 {
456 /* This compares only the component pointers, not the actual geometry data. */
457 return Span(a.components_) == Span(b.components_) && a.name == b.name;
458 }
459
461 {
462 /* This should have the same data that's also taken into account in #operator==. */
463 return get_default_hash(Span(components_), this->name);
464 }
465
466 void count_memory(MemoryCounter &memory) const;
467
468 private:
473 GeometryComponent *get_component_ptr(GeometryComponent::Type type);
474 template<typename Component> Component *get_component_ptr()
475 {
477 return static_cast<Component *>(get_component_ptr(Component::static_type));
478 }
479};
480
486 private:
487 Mesh *mesh_ = nullptr;
489
490 public:
493 ~MeshComponent() override;
494 GeometryComponentPtr copy() const override;
495
496 void clear() override;
497 bool has_mesh() const;
506 Mesh *release();
507
512 const Mesh *get() const;
518
519 bool is_empty() const final;
520
521 bool owns_direct_data() const override;
522 void ensure_owns_direct_data() override;
523
524 void count_memory(MemoryCounter &memory) const override;
525
527
528 std::optional<AttributeAccessor> attributes() const final;
530};
531
543 private:
544 PointCloud *pointcloud_ = nullptr;
546
547 public:
551 ~PointCloudComponent() override;
552 GeometryComponentPtr copy() const override;
553
554 void clear() override;
555 bool has_pointcloud() const;
559 void replace(PointCloud *pointcloud,
566
572 const PointCloud *get() const;
579
580 bool is_empty() const final;
581
582 bool owns_direct_data() const override;
583 void ensure_owns_direct_data() override;
584
585 void count_memory(MemoryCounter &memory) const override;
586
587 std::optional<AttributeAccessor> attributes() const final;
588 std::optional<MutableAttributeAccessor> attributes_for_write() final;
589
591};
592
599 private:
600 Curves *curves_ = nullptr;
602
608 mutable Curve *curve_for_render_ = nullptr;
609 mutable Mutex curve_for_render_mutex_;
610
611 public:
614 ~CurveComponent() override;
615 GeometryComponentPtr copy() const override;
616
617 void clear() override;
618 bool has_curves() const;
623 Curves *release();
624
625 const Curves *get() const;
627
628 bool is_empty() const final;
629
630 bool owns_direct_data() const override;
631 void ensure_owns_direct_data() override;
632
633 void count_memory(MemoryCounter &memory) const override;
634
639 const Curve *get_curve_for_render() const;
640
641 std::optional<AttributeAccessor> attributes() const final;
642 std::optional<MutableAttributeAccessor> attributes_for_write() final;
643
645};
646
651 private:
652 Instances *instances_ = nullptr;
654
655 public:
657 InstancesComponent(Instances *instances,
659 ~InstancesComponent() override;
660 GeometryComponentPtr copy() const override;
661
662 void clear() override;
663
664 const Instances *get() const;
666
667 void replace(Instances *instances,
669
670 bool is_empty() const final;
671
673
674 bool owns_direct_data() const override;
675 void ensure_owns_direct_data() override;
676
677 void count_memory(MemoryCounter &memory) const override;
678
679 std::optional<AttributeAccessor> attributes() const final;
680 std::optional<MutableAttributeAccessor> attributes_for_write() final;
681
683};
684
691 private:
692 Volume *volume_ = nullptr;
694
695 public:
697 ~VolumeComponent() override;
698 GeometryComponentPtr copy() const override;
699
700 void clear() override;
701 bool has_volume() const;
710 Volume *release();
711
716 const Volume *get() const;
723
724 bool owns_direct_data() const override;
725 void ensure_owns_direct_data() override;
726
727 void count_memory(MemoryCounter &memory) const override;
728
730};
731
740 public:
747 std::unique_ptr<CurvesEditHints> curves_edit_hints_;
751 std::unique_ptr<GreasePencilEditHints> grease_pencil_edit_hints_;
755 std::unique_ptr<GizmoEditHints> gizmo_edit_hints_;
756
758
760 bool owns_direct_data() const final;
762
763 void clear() override;
764
772
774};
775
782 private:
783 GreasePencil *grease_pencil_ = nullptr;
785
786 public:
788 ~GreasePencilComponent() override;
789 GeometryComponentPtr copy() const override;
790
791 void clear() override;
792 bool has_grease_pencil() const;
796 void replace(GreasePencil *grease_pencil,
803
804 const GreasePencil *get() const;
806
807 bool is_empty() const final;
808
809 bool owns_direct_data() const override;
810 void ensure_owns_direct_data() override;
811
813
814 std::optional<AttributeAccessor> attributes() const final;
816};
817
819
820} // namespace blender::bke
#define BLI_STATIC_ASSERT(a, msg)
Definition BLI_assert.h:83
#define final(a, b, c)
Definition BLI_hash.h:19
unsigned long long int uint64_t
void count_memory(MemoryCounter &memory) const override
static constexpr GeometryComponent::Type static_type
void replace(Curves *curve, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
std::optional< AttributeAccessor > attributes() const final
std::optional< MutableAttributeAccessor > attributes_for_write() final
std::unique_ptr< GreasePencilEditHints > grease_pencil_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
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
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
static constexpr GeometryComponent::Type static_type
void count_memory(MemoryCounter &memory) const override
static constexpr GeometryComponent::Type static_type
void replace(Volume *volume, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
#define class
#define public
static void clear(Message &msg)
Definition msgfmt.cc:213
ImplicitSharingPtr< GeometryComponent > GeometryComponentPtr
constexpr bool is_geometry_component_v
bool attribute_is_builtin_on_component_type(const GeometryComponent::Type type, StringRef name)
uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:233
std::mutex Mutex
Definition BLI_mutex.hh:47
static void copy(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node)
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
FunctionRef< void(StringRef attribute_id, const AttributeMetaData &meta_data, const GeometryComponent &component)> AttributeForeachCallback
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)
friend std::ostream & operator<<(std::ostream &stream, const GeometrySet &geometry_set)
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)
GeometrySet(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()
void gather_attributes_for_propagation(Span< GeometryComponent::Type > component_types, GeometryComponent::Type dst_component_type, bool include_instances, const AttributeFilter &attribute_filter, Map< StringRef, AttributeDomainAndType > &r_attributes) const
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 modify_geometry_sets(ForeachSubGeometryCallback callback)
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
void attribute_foreach(Span< GeometryComponent::Type > component_types, bool include_instances, AttributeForeachCallback callback) const
FunctionRef< void(GeometrySet &geometry_set)> ForeachSubGeometryCallback
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:1184