Lagrange
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
30
35enum class AttributeValueType : uint8_t;
36
41{
42public:
50 AttributeBase(AttributeElement element, AttributeUsage usage, size_t num_channels);
51
55 virtual ~AttributeBase();
56
62 AttributeBase(AttributeBase&& other) = default;
63
72
78 AttributeBase(const AttributeBase& other) = default;
79
87 AttributeBase& operator=(const AttributeBase& other) = default;
88
94 [[nodiscard]] virtual AttributeValueType get_value_type() const = 0;
95
101 [[nodiscard]] AttributeElement get_element_type() const { return m_element; }
102
108 [[nodiscard]] AttributeUsage get_usage() const { return m_usage; }
109
115 [[nodiscard]] size_t get_num_channels() const { return m_num_channels; }
116
124 void unsafe_set_usage(AttributeUsage usage) { m_usage = usage; }
125
134
135protected:
138
141
143 size_t m_num_channels = 0;
144};
145
151template <typename ValueType_>
153{
154public:
156 using ValueType = ValueType_;
157
159 static constexpr bool IsIndexed = false;
160
161 // Static assertions to prevent users from instantiating unsupported types. We do not use the
162 // macros from AttributeTypes.h since we do not want to implicitly expose them in the main
163 // Attribute.h header.
164 static_assert(
165 std::is_same_v<ValueType, int8_t> || std::is_same_v<ValueType, int16_t> ||
166 std::is_same_v<ValueType, int32_t> || std::is_same_v<ValueType, int64_t> ||
167 std::is_same_v<ValueType, uint8_t> || std::is_same_v<ValueType, uint16_t> ||
168 std::is_same_v<ValueType, uint32_t> || std::is_same_v<ValueType, uint64_t> ||
169 std::is_same_v<ValueType, float> || std::is_same_v<ValueType, double>,
170 "Attribute's ValueType template parameter can only be float, double, or a fixed size "
171 "integer type.");
172
173public:
177
185 Attribute(AttributeElement element, AttributeUsage usage, size_t num_channels);
186
190 ~Attribute() override;
191
197 Attribute(Attribute&& other) noexcept;
198
206 Attribute& operator=(Attribute&& other) noexcept;
207
213 explicit Attribute(const Attribute& other);
214
223
235 template <typename OtherValue>
237
249 template <typename OtherValue>
251
257 [[nodiscard]] AttributeValueType get_value_type() const override;
258
272 void wrap(span<ValueType> buffer, size_t num_elements);
273
288 void wrap(SharedSpan<ValueType> buffer_ptr, size_t num_elements);
289
304 void wrap_const(span<const ValueType> buffer, size_t num_elements);
305
320 void wrap_const(SharedSpan<const ValueType> shared_buffer, size_t num_elements);
321
325
332
337
344
351
358
365
372
379
386
393
400
407
414
418 void clear();
419
425 void shrink_to_fit();
426
434 void reserve_entries(size_t new_cap);
435
442 void resize_elements(size_t num_elements);
443
451
458 void insert_elements(std::initializer_list<const ValueType> values);
459
468 void insert_elements(size_t count);
469
473
480 [[nodiscard]] bool empty() const { return m_num_elements == 0; }
481
487 [[nodiscard]] size_t get_num_elements() const { return m_num_elements; }
488
495 [[nodiscard]] bool is_external() const { return m_is_external; }
496
504 [[nodiscard]] bool is_managed() const { return !is_external() || m_owner; }
505
512 [[nodiscard]] bool is_read_only() const { return m_is_read_only; }
513
524 ValueType get(size_t i, size_t c) const;
525
536 ValueType& ref(size_t i, size_t c);
537
547 ValueType get(size_t i) const;
548
558 ValueType& ref(size_t i);
559
567
575
583 lagrange::span<const ValueType> get_first(size_t num_elements) const;
584
592 lagrange::span<ValueType> ref_first(size_t num_elements);
593
601 lagrange::span<const ValueType> get_last(size_t num_elements) const;
602
610 lagrange::span<ValueType> ref_last(size_t num_elements);
611
620 lagrange::span<const ValueType> get_middle(size_t first_element, size_t num_elements) const;
621
630 lagrange::span<ValueType> ref_middle(size_t first_element, size_t num_elements);
631
639 lagrange::span<const ValueType> get_row(size_t element) const;
640
648 lagrange::span<ValueType> ref_row(size_t element);
649
651protected:
653
655 template <typename>
656 friend class Attribute;
657
665 void growth_check(size_t new_cap);
666
671 void write_check();
672
676 void update_views();
677
681 void clear_views();
682
684
685protected:
687 std::vector<ValueType> m_data;
688
691 std::shared_ptr<const ValueType> m_owner;
692
695
700
704
707
710
713
716
719
722 bool m_is_external = false;
723
727 bool m_is_read_only = false;
728
730 size_t m_num_elements = 0;
731};
732
734
735} // namespace lagrange
Base handle for attributes.
Definition: Attribute.h:41
size_t m_num_channels
Number of channel for each value.
Definition: Attribute.h:143
void unsafe_set_element_type(AttributeElement element)
Sets the attribute element type.
Definition: Attribute.h:133
void unsafe_set_usage(AttributeUsage usage)
Sets the attribute usage tag.
Definition: Attribute.h:124
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:33
virtual ~AttributeBase()
Destroys the object.
AttributeUsage get_usage() const
Gets the attribute usage tag.
Definition: Attribute.h:108
AttributeUsage m_usage
Attribute usage tag.
Definition: Attribute.h:140
AttributeElement get_element_type() const
Gets the attribute element type.
Definition: Attribute.h:101
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:137
size_t get_num_channels() const
Gets the number of channels for the attribute.
Definition: Attribute.h:115
Derived attribute class that stores the actual information.
Definition: Attribute.h:153
bool is_managed() const
Checks whether the attribute is managing the lifetime of the underlying buffer.
Definition: Attribute.h:504
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:548
ValueType get_default_value() const
Gets the default value to use when growing the attribute.
Definition: Attribute.h:336
lagrange::span< const ValueType > get_all() const
Returns a read-only view of the buffer spanning num elements x num channels.
Definition: Attribute.cpp:511
lagrange::span< ValueType > m_view
Writable pointer to the buffer storing the attribute data.
Definition: Attribute.h:699
AttributeCopyPolicy get_copy_policy() const
Gets the copy policy for external buffers.
Definition: Attribute.h:392
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:564
AttributeCastPolicy get_cast_policy() const
Gets the cast policy.
Definition: Attribute.h:406
bool m_is_external
Flag to determine whether an attribute is using an external or internal buffer.
Definition: Attribute.h:722
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:542
void reserve_entries(size_t new_cap)
Reserve enough memory for new_cap entries.
Definition: Attribute.cpp:413
AttributeGrowthPolicy m_growth_policy
Growth policy for external buffers.
Definition: Attribute.h:706
static constexpr bool IsIndexed
Whether this attribute type is indexed.
Definition: Attribute.h:159
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:530
ValueType_ ValueType
Attribute value type.
Definition: Attribute.h:156
~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:524
void wrap_const(span< const ValueType > buffer, size_t num_elements)
Wraps a const external buffer into the attribute.
Definition: Attribute.cpp:321
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:556
AttributeCastPolicy m_cast_policy
Cast policy when converting from this attribute.
Definition: Attribute.h:718
void shrink_to_fit()
Shrink attribute buffer to fit the current number of entries.
Definition: Attribute.cpp:380
bool empty() const
Test whether the attribute is empty (its size is 0).
Definition: Attribute.h:480
void create_internal_copy()
Creates an internal copy of the attribute data.
Definition: Attribute.cpp:352
bool is_read_only() const
Checks whether the attribute is external and pointing to a const buffer.
Definition: Attribute.h:512
void set_write_policy(AttributeWritePolicy policy)
Sets the write policy for read-only external buffers.
Definition: Attribute.h:371
bool m_is_read_only
Flag to determine whether an external attribute is read-only or writable.
Definition: Attribute.h:727
void wrap(span< ValueType > buffer, size_t num_elements)
Wraps an external buffer into the attribute.
Definition: Attribute.cpp:296
AttributeWritePolicy m_write_policy
Policy for write access to read-only external buffers.
Definition: Attribute.h:712
AttributeShrinkPolicy m_shrink_policy
Shrink policy for external buffers.
Definition: Attribute.h:709
std::shared_ptr< const ValueType > m_owner
Optional aliased ptr to extend the lifetime of memory owner object of external buffer.
Definition: Attribute.h:691
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:570
size_t get_num_elements() const
Gets the number of elements.
Definition: Attribute.h:487
bool is_external() const
Checks whether an attribute buffer is external or internally managed.
Definition: Attribute.h:495
void set_copy_policy(AttributeCopyPolicy policy)
Sets the copy policy for external buffers.
Definition: Attribute.h:385
void set_growth_policy(AttributeGrowthPolicy policy)
Sets the growth policy for external buffers.
Definition: Attribute.h:343
AttributeShrinkPolicy get_shrink_policy() const
Gets the shrink policy for external buffers.
Definition: Attribute.h:364
ValueType get(size_t i, size_t c) const
Gets an entry for element i, at channel p.
Definition: Attribute.cpp:483
std::vector< ValueType > m_data
Internal buffer storing the data (when the attribute is not external).
Definition: Attribute.h:687
void clear()
Clears the attribute buffer (new number of elements is 0).
Definition: Attribute.cpp:368
Attribute(Attribute &&other) noexcept
Move constructor.
ValueType m_default_value
Default values used to populate buffer when the attribute grows.
Definition: Attribute.h:694
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:536
size_t m_num_elements
Number of elements associated with the attribute.
Definition: Attribute.h:730
void set_default_value(ValueType value)
Sets the default value to use when growing the attribute.
Definition: Attribute.h:331
void resize_elements(size_t num_elements)
Resize the buffer to contain num_elements elements.
Definition: Attribute.cpp:423
lagrange::span< ValueType > ref_all()
Returns a writable view of the buffer spanning num elements x num channels.
Definition: Attribute.cpp:517
AttributeWritePolicy get_write_policy() const
Gets the write policy for read-only external buffers.
Definition: Attribute.h:378
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:703
void set_shrink_policy(AttributeShrinkPolicy policy)
Sets the shrink policy for external buffers.
Definition: Attribute.h:357
void set_cast_policy(AttributeCastPolicy policy)
Sets the cast policy.
Definition: Attribute.h:399
AttributeCopyPolicy m_copy_policy
Copy policy for external buffers.
Definition: Attribute.h:715
Attribute(AttributeElement element, AttributeUsage usage, size_t num_channels)
Constructs a new instance.
Definition: Attribute.cpp:65
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:489
void insert_elements(span< const ValueType > values)
Inserts values for new elements.
Definition: Attribute.cpp:441
AttributeGrowthPolicy get_growth_policy() const
Gets the growth policy for external buffers.
Definition: Attribute.h:350
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...
@ ErrorIfExternal
Throws an exception when trying to grow an external buffer (even if the new size is still within the ...
@ CopyIfExternal
Copy the buffer during copy if the attribute points to an external buffer.
@ ErrorIfReadOnly
Throws an exception when trying to write to a read-only buffer.
@ RemapInvalidIndices
Map invalue values only if the AttributeUsage represents indices.
::nonstd::span< T, Extent > span
A bounds-safe view for sequences of objects.
Definition: span.h:27
Main namespace for Lagrange.
Definition: AABBIGL.h:30