diff options
Diffstat (limited to 'source/blender/blenkernel/BKE_attribute_accessor.hh')
-rw-r--r-- | source/blender/blenkernel/BKE_attribute_accessor.hh | 84 |
1 files changed, 83 insertions, 1 deletions
diff --git a/source/blender/blenkernel/BKE_attribute_accessor.hh b/source/blender/blenkernel/BKE_attribute_accessor.hh index 3cf2fd4f865..0905f81d472 100644 --- a/source/blender/blenkernel/BKE_attribute_accessor.hh +++ b/source/blender/blenkernel/BKE_attribute_accessor.hh @@ -39,7 +39,7 @@ class ReadAttribute { { } - virtual ~ReadAttribute() = default; + virtual ~ReadAttribute(); AttributeDomain domain() const { @@ -67,7 +67,54 @@ class ReadAttribute { virtual void get_internal(const int64_t index, void *r_value) const = 0; }; +class WriteAttribute { + protected: + const AttributeDomain domain_; + const CPPType &cpp_type_; + const int64_t size_; + + public: + WriteAttribute(AttributeDomain domain, const CPPType &cpp_type, const int64_t size) + : domain_(domain), cpp_type_(cpp_type), size_(size) + { + } + + virtual ~WriteAttribute(); + + AttributeDomain domain() const + { + return domain_; + } + + const CPPType &cpp_type() const + { + return cpp_type_; + } + + int64_t size() const + { + return size_; + } + + void get(const int64_t index, void *r_value) const + { + BLI_assert(index < size_); + this->get_internal(index, r_value); + } + + void set(const int64_t index, const void *value) + { + BLI_assert(index < size_); + this->set_internal(index, value); + } + + protected: + virtual void get_internal(const int64_t index, void *r_value) const = 0; + virtual void set_internal(const int64_t index, const void *value) = 0; +}; + using ReadAttributePtr = std::unique_ptr<ReadAttribute>; +using WriteAttributePtr = std::unique_ptr<WriteAttribute>; template<typename T> class TypedReadAttribute { private: @@ -95,11 +142,46 @@ template<typename T> class TypedReadAttribute { } }; +template<typename T> class TypedWriteAttribute { + private: + WriteAttributePtr attribute_; + + public: + TypedWriteAttribute(WriteAttributePtr attribute) : attribute_(std::move(attribute)) + { + BLI_assert(attribute_); + BLI_assert(attribute_->cpp_type().is<T>()); + } + + int64_t size() const + { + return attribute_->size(); + } + + T operator[](const int64_t index) const + { + BLI_assert(index < attribute_->size()); + T value; + value.~T(); + attribute_->get(index, &value); + return value; + } + + void set(const int64_t index, const T &value) + { + attribute_->set(index, &value); + } +}; + using FloatReadAttribute = TypedReadAttribute<float>; using Float3ReadAttribute = TypedReadAttribute<float3>; +using FloatWriteAttribute = TypedWriteAttribute<float>; +using Float3WriteAttribute = TypedWriteAttribute<float3>; ReadAttributePtr mesh_attribute_get_for_read(const MeshComponent &mesh_component, const StringRef attribute_name); +std::optional<WriteAttributePtr> mesh_attribute_get_for_write(MeshComponent &mesh_component, + const StringRef attribute_name); ReadAttributePtr mesh_attribute_adapt_domain(const MeshComponent &mesh_component, ReadAttributePtr attribute, |