Lagrange
Loading...
Searching...
No Matches
Attribute.h
1/*
2 * Copyright 2022 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/AttributeFwd.h>
15#include <lagrange/utils/SharedSpan.h>
16#include <lagrange/utils/span.h>
17
18#include <vector>
19
20namespace lagrange {
21
23namespace internal {
24
37template <typename ValueType>
38lagrange::span<const ValueType> get_all_unpoisoned(const Attribute<ValueType>& attr);
39
40} // namespace internal
42
51
56enum class AttributeValueType : uint8_t;
57
62{
63public:
71 AttributeBase(AttributeElement element, AttributeUsage usage, size_t num_channels);
72
76 virtual ~AttributeBase();
77
83 AttributeBase(AttributeBase&& other) = default;
84
93
99 AttributeBase(const AttributeBase& other) = default;
100
108 AttributeBase& operator=(const AttributeBase& other) = default;
109
115 [[nodiscard]] virtual AttributeValueType get_value_type() const = 0;
116
122 [[nodiscard]] AttributeElement get_element_type() const { return m_element; }
123
129 [[nodiscard]] AttributeUsage get_usage() const { return m_usage; }
130
136 [[nodiscard]] size_t get_num_channels() const { return m_num_channels; }
137
145 void unsafe_set_usage(AttributeUsage usage) { m_usage = usage; }
146
155
156protected:
159
162
164 size_t m_num_channels = 0;
165};
166
172template <typename ValueType_>
174{
175public:
177 using ValueType = ValueType_;
178
180 static constexpr bool IsIndexed = false;
181
182 // Static assertions to prevent users from instantiating unsupported types. We do not use the
183 // macros from AttributeTypes.h since we do not want to implicitly expose them in the main
184 // Attribute.h header.
185 static_assert(
186 std::is_same_v<ValueType, int8_t> || std::is_same_v<ValueType, int16_t> ||
187 std::is_same_v<ValueType, int32_t> || std::is_same_v<ValueType, int64_t> ||
188 std::is_same_v<ValueType, uint8_t> || std::is_same_v<ValueType, uint16_t> ||
189 std::is_same_v<ValueType, uint32_t> || std::is_same_v<ValueType, uint64_t> ||
190 std::is_same_v<ValueType, float> || std::is_same_v<ValueType, double>,
191 "Attribute's ValueType template parameter can only be float, double, or a fixed size "
192 "integer type.");
193
194public:
198
206 Attribute(AttributeElement element, AttributeUsage usage, size_t num_channels);
207
211 ~Attribute() override;
212
218 Attribute(Attribute&& other) noexcept;
219
227 Attribute& operator=(Attribute&& other) noexcept;
228
234 explicit Attribute(const Attribute& other);
235
244
256 template <typename OtherValue>
258
270 template <typename OtherValue>
272
278 [[nodiscard]] AttributeValueType get_value_type() const override;
279
293 void wrap(span<ValueType> buffer, size_t num_elements);
294
309 void wrap(SharedSpan<ValueType> buffer_ptr, size_t num_elements);
310
325 void wrap_const(span<const ValueType> buffer, size_t num_elements);
326
341 void wrap_const(SharedSpan<const ValueType> shared_buffer, size_t num_elements);
342
346
353
358
365
372
379
386
393
400
407
414
421
428
435
439 void clear();
440
446 void shrink_to_fit();
447
455 void reserve_entries(size_t new_cap);
456
463 void resize_elements(size_t num_elements);
464
472
479 void insert_elements(std::initializer_list<const ValueType> values);
480
489 void insert_elements(size_t count);
490
494
501 [[nodiscard]] bool empty() const { return m_num_elements == 0; }
502
508 [[nodiscard]] size_t get_num_elements() const { return m_num_elements; }
509
516 [[nodiscard]] bool is_external() const { return m_is_external; }
517
525 [[nodiscard]] bool is_managed() const { return !is_external() || m_owner; }
526
533 [[nodiscard]] bool is_read_only() const { return m_is_read_only; }
534
545 ValueType get(size_t i, size_t c) const;
546
557 ValueType& ref(size_t i, size_t c);
558
568 ValueType get(size_t i) const;
569
579 ValueType& ref(size_t i);
580
588
590 template <typename V>
591 friend lagrange::span<const V> internal::get_all_unpoisoned(const Attribute<V>& attr);
593
601
609 lagrange::span<const ValueType> get_first(size_t num_elements) const;
610
618 lagrange::span<ValueType> ref_first(size_t num_elements);
619
627 lagrange::span<const ValueType> get_last(size_t num_elements) const;
628
636 lagrange::span<ValueType> ref_last(size_t num_elements);
637
646 lagrange::span<const ValueType> get_middle(size_t first_element, size_t num_elements) const;
647
656 lagrange::span<ValueType> ref_middle(size_t first_element, size_t num_elements);
657
665 lagrange::span<const ValueType> get_row(size_t element) const;
666
674 lagrange::span<ValueType> ref_row(size_t element);
675
677protected:
679
681 template <typename>
682 friend class Attribute;
683
691 void growth_check(size_t new_cap);
692
697 void write_check();
698
702 void update_views();
703
707 void clear_views();
708
710
711protected:
713 std::vector<ValueType> m_data;
714
717 std::shared_ptr<const ValueType> m_owner;
718
721
726
730
733
736
739
742
745
748 bool m_is_external = false;
749
753 bool m_is_read_only = false;
754
756 size_t m_num_elements = 0;
757};
758
760
761} // namespace lagrange
size_t m_num_channels
Number of channel for each value.
Definition Attribute.h:164
void unsafe_set_element_type(AttributeElement element)
Sets the attribute element type.
Definition Attribute.h:154
void unsafe_set_usage(AttributeUsage usage)
Sets the attribute usage tag.
Definition Attribute.h:145
AttributeBase & operator=(const AttributeBase &other)=default
Assignment copy operator.
AttributeBase(AttributeElement element, AttributeUsage usage, size_t num_channels)
Constructs a new instance.
Definition Attribute.cpp:46
virtual ~AttributeBase()
Destroys the object.
AttributeUsage get_usage() const
Gets the attribute usage tag.
Definition Attribute.h:129
AttributeUsage m_usage
Attribute usage tag.
Definition Attribute.h:161
AttributeElement get_element_type() const
Gets the attribute element type.
Definition Attribute.h:122
AttributeBase(const AttributeBase &other)=default
Copy constructor.
virtual AttributeValueType get_value_type() const =0
Gets the attribute value type.
AttributeBase & operator=(AttributeBase &&other)=default
Assignment move operator.
AttributeBase(AttributeBase &&other)=default
Move constructor.
AttributeElement m_element
Element type (vertex, facet, indexed, etc.).
Definition Attribute.h:158
size_t get_num_channels() const
Gets the number of channels for the attribute.
Definition Attribute.h:136
Derived attribute class that stores the actual information.
Definition Attribute.h:174
bool is_managed() const
Checks whether the attribute is managing the lifetime of the underlying buffer.
Definition Attribute.h:525
lagrange::span< const ValueType > get_middle(size_t first_element, size_t num_elements) const
Returns a read-only view of the attribute values for the middle elements in the buffer.
Definition Attribute.cpp:583
ValueType get_default_value() const
Gets the default value to use when growing the attribute.
Definition Attribute.h:357
lagrange::span< const ValueType > get_all() const
Returns a read-only view of the buffer spanning num elements x num channels.
Definition Attribute.cpp:525
lagrange::span< ValueType > m_view
Writable pointer to the buffer storing the attribute data.
Definition Attribute.h:725
AttributeCopyPolicy get_copy_policy() const
Gets the copy policy for external buffers.
Definition Attribute.h:413
Attribute & operator=(Attribute &&other) noexcept
Assignment move operator.
AttributeValueType get_value_type() const override
Gets the attribute value type.
lagrange::span< const ValueType > get_row(size_t element) const
Returns a read-only view of the attribute values for the element in the buffer.
Definition Attribute.cpp:599
AttributeCastPolicy get_cast_policy() const
Gets the cast policy.
Definition Attribute.h:427
bool m_is_external
Flag to determine whether an attribute is using an external or internal buffer.
Definition Attribute.h:748
lagrange::span< ValueType > ref_last(size_t num_elements)
Returns a writable view of the attribute values for the last elements in the buffer.
Definition Attribute.cpp:577
void reserve_entries(size_t new_cap)
Reserve enough memory for new_cap entries.
Definition Attribute.cpp:427
AttributeGrowthPolicy m_growth_policy
Growth policy for external buffers.
Definition Attribute.h:732
static constexpr bool IsIndexed
Whether this attribute type is indexed.
Definition Attribute.h:180
lagrange::span< ValueType > ref_first(size_t num_elements)
Returns a writable view of the attribute values for the first elements in the buffer.
Definition Attribute.cpp:565
ValueType_ ValueType
Attribute value type.
Definition Attribute.h:177
~Attribute() override
Destroys the object.
lagrange::span< const ValueType > get_first(size_t num_elements) const
Returns a read-only view of the attribute values for the first elements in the buffer.
Definition Attribute.cpp:559
void wrap_const(span< const ValueType > buffer, size_t num_elements)
Wraps a const external buffer into the attribute.
Definition Attribute.cpp:334
lagrange::span< ValueType > ref_middle(size_t first_element, size_t num_elements)
Returns a writable view of the attribute values for the middle elements in the buffer.
Definition Attribute.cpp:591
AttributeCastPolicy m_cast_policy
Cast policy when converting from this attribute.
Definition Attribute.h:744
void shrink_to_fit()
Shrink attribute buffer to fit the current number of entries.
Definition Attribute.cpp:393
bool empty() const
Test whether the attribute is empty (its size is 0).
Definition Attribute.h:501
void create_internal_copy()
Creates an internal copy of the attribute data.
Definition Attribute.cpp:365
bool is_read_only() const
Checks whether the attribute is external and pointing to a const buffer.
Definition Attribute.h:533
void set_write_policy(AttributeWritePolicy policy)
Sets the write policy for read-only external buffers.
Definition Attribute.h:392
bool m_is_read_only
Flag to determine whether an external attribute is read-only or writable.
Definition Attribute.h:753
void wrap(span< ValueType > buffer, size_t num_elements)
Wraps an external buffer into the attribute.
Definition Attribute.cpp:309
AttributeWritePolicy m_write_policy
Policy for write access to read-only external buffers.
Definition Attribute.h:738
AttributeShrinkPolicy m_shrink_policy
Shrink policy for external buffers.
Definition Attribute.h:735
std::shared_ptr< const ValueType > m_owner
Optional aliased ptr to extend the lifetime of memory owner object of external buffer.
Definition Attribute.h:717
Attribute & operator=(const Attribute &other)
Assignment copy operator.
static Attribute cast_copy(const Attribute< OtherValue > &other)
Cast copy operator.
lagrange::span< ValueType > ref_row(size_t element)
Returns a writable view of the attribute values for the element in the buffer.
Definition Attribute.cpp:605
size_t get_num_elements() const
Gets the number of elements.
Definition Attribute.h:508
bool is_external() const
Checks whether an attribute buffer is external or internally managed.
Definition Attribute.h:516
void set_copy_policy(AttributeCopyPolicy policy)
Sets the copy policy for external buffers.
Definition Attribute.h:406
void set_growth_policy(AttributeGrowthPolicy policy)
Sets the growth policy for external buffers.
Definition Attribute.h:364
AttributeShrinkPolicy get_shrink_policy() const
Gets the shrink policy for external buffers.
Definition Attribute.h:385
ValueType get(size_t i, size_t c) const
Gets an entry for element i, at channel p.
Definition Attribute.cpp:497
std::vector< ValueType > m_data
Internal buffer storing the data (when the attribute is not external).
Definition Attribute.h:713
void clear()
Clears the attribute buffer (new number of elements is 0).
Definition Attribute.cpp:381
Attribute(Attribute &&other) noexcept
Move constructor.
ValueType m_default_value
Default values used to populate buffer when the attribute grows.
Definition Attribute.h:720
lagrange::span< const ValueType > get_last(size_t num_elements) const
Returns a read-only view of the attribute values for the last elements in the buffer.
Definition Attribute.cpp:571
size_t m_num_elements
Number of elements associated with the attribute.
Definition Attribute.h:756
void set_default_value(ValueType value)
Sets the default value to use when growing the attribute.
Definition Attribute.h:352
void resize_elements(size_t num_elements)
Resize the buffer to contain num_elements elements.
Definition Attribute.cpp:437
lagrange::span< ValueType > ref_all()
Returns a writable view of the buffer spanning num elements x num channels.
Definition Attribute.cpp:552
AttributeWritePolicy get_write_policy() const
Gets the write policy for read-only external buffers.
Definition Attribute.h:399
Attribute(const Attribute &other)
Copy constructor.
lagrange::span< const ValueType > m_const_view
Read-only pointer to the buffer storing the attribute data.
Definition Attribute.h:729
void set_shrink_policy(AttributeShrinkPolicy policy)
Sets the shrink policy for external buffers.
Definition Attribute.h:378
void set_cast_policy(AttributeCastPolicy policy)
Sets the cast policy.
Definition Attribute.h:420
AttributeCopyPolicy m_copy_policy
Copy policy for external buffers.
Definition Attribute.h:741
Attribute(AttributeElement element, AttributeUsage usage, size_t num_channels)
Constructs a new instance.
Definition Attribute.cpp:78
Attribute & cast_assign(const Attribute< OtherValue > &other)
Cast assignment operator.
ValueType & ref(size_t i, size_t c)
Gets a writable reference to the entry for element i, at channel p.
Definition Attribute.cpp:503
void insert_elements(span< const ValueType > values)
Inserts values for new elements.
Definition Attribute.cpp:455
AttributeGrowthPolicy get_growth_policy() const
Gets the growth policy for external buffers.
Definition Attribute.h:371
Shared span with ownership tracking.
Definition SharedSpan.h:36
AttributeShrinkPolicy
Policy for shrinking external attribute buffers.
Definition AttributeFwd.h:117
AttributeUsage
Usage tag indicating how the attribute should behave under mesh transformations.
Definition AttributeFwd.h:54
AttributeGrowthPolicy
Policy for growing external attribute buffers.
Definition AttributeFwd.h:96
AttributeCopyPolicy
Policy for copying attribute that are views onto external buffers.
Definition AttributeFwd.h:161
AttributeElement
Type of element to which the attribute is attached.
Definition AttributeFwd.h:26
AttributeValueType
Enum describing at runtime the value type of an attribute.
Definition AttributeValueType.h:25
AttributeWritePolicy
Policy for attempting to write to read-only external buffers.
Definition AttributeFwd.h:138
AttributeCastPolicy
Policy for remapping invalid values when casting to a different value type.
Definition AttributeFwd.h:182
@ ErrorIfExternal
Throws an exception when trying to shrink an external buffer (even if the new size is still within th...
Definition AttributeFwd.h:120
@ ErrorIfExternal
Throws an exception when trying to grow an external buffer (even if the new size is still within the ...
Definition AttributeFwd.h:99
@ CopyIfExternal
Copy the buffer during copy if the attribute points to an external buffer.
Definition AttributeFwd.h:162
@ ErrorIfReadOnly
Throws an exception when trying to write to a read-only buffer.
Definition AttributeFwd.h:139
@ RemapInvalidIndices
Map invalue values only if the AttributeUsage represents indices.
Definition AttributeFwd.h:183
::nonstd::span< T, Extent > span
A bounds-safe view for sequences of objects.
Definition span.h:27
nullptr_t, size_t, ptrdiff_t basic_ostream bad_weak_ptr extent, remove_extent, is_array,...
Definition attribute_string_utils.h:21
Main namespace for Lagrange.