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 integer type.");
171
172public:
176
184 Attribute(AttributeElement element, AttributeUsage usage, size_t num_channels);
185
189 ~Attribute() override;
190
196 Attribute(Attribute&& other) noexcept;
197
205 Attribute& operator=(Attribute&& other) noexcept;
206
212 explicit Attribute(const Attribute& other);
213
222
234 template <typename OtherValue>
236
248 template <typename OtherValue>
250
256 [[nodiscard]] AttributeValueType get_value_type() const override;
257
271 void wrap(span<ValueType> buffer, size_t num_elements);
272
287 void wrap(SharedSpan<ValueType> buffer_ptr, size_t num_elements);
288
303 void wrap_const(span<const ValueType> buffer, size_t num_elements);
304
319 void wrap_const(SharedSpan<const ValueType> shared_buffer, size_t num_elements);
320
324
331
336
343
350
357
364
371
378
385
392
399
403 void clear();
404
410 void shrink_to_fit();
411
419 void reserve_entries(size_t new_cap);
420
427 void resize_elements(size_t num_elements);
428
436
443 void insert_elements(std::initializer_list<const ValueType> values);
444
453 void insert_elements(size_t count);
454
458
465 [[nodiscard]] bool empty() const { return m_num_elements == 0; }
466
472 [[nodiscard]] size_t get_num_elements() const { return m_num_elements; }
473
480 [[nodiscard]] bool is_external() const { return m_is_external; }
481
489 [[nodiscard]] bool is_managed() const { return !is_external() || m_owner; }
490
497 [[nodiscard]] bool is_read_only() const { return m_is_read_only; }
498
509 ValueType get(size_t i, size_t c) const;
510
521 ValueType& ref(size_t i, size_t c);
522
532 ValueType get(size_t i) const;
533
543 ValueType& ref(size_t i);
544
552
560
568 lagrange::span<const ValueType> get_first(size_t num_elements) const;
569
577 lagrange::span<ValueType> ref_first(size_t num_elements);
578
586 lagrange::span<const ValueType> get_last(size_t num_elements) const;
587
595 lagrange::span<ValueType> ref_last(size_t num_elements);
596
605 lagrange::span<const ValueType> get_middle(size_t first_element, size_t num_elements) const;
606
615 lagrange::span<ValueType> ref_middle(size_t first_element, size_t num_elements);
616
624 lagrange::span<const ValueType> get_row(size_t element) const;
625
633 lagrange::span<ValueType> ref_row(size_t element);
634
636protected:
638
640 template <typename>
641 friend class Attribute;
642
650 void growth_check(size_t new_cap);
651
656 void write_check();
657
661 void update_views();
662
666 void clear_views();
667
669
670protected:
672 std::vector<ValueType> m_data;
673
676 std::shared_ptr<const ValueType> m_owner;
677
680
685
689
692
695
698
701
704 bool m_is_external = false;
705
709 bool m_is_read_only = false;
710
712 size_t m_num_elements = 0;
713};
714
716
717} // 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:32
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:489
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:523
ValueType get_default_value() const
Gets the default value to use when growing the attribute.
Definition: Attribute.h:335
lagrange::span< const ValueType > get_all() const
Returns a read-only view of the buffer spanning num elements x num channels.
Definition: Attribute.cpp:486
lagrange::span< ValueType > m_view
Writable pointer to the buffer storing the attribute data.
Definition: Attribute.h:684
AttributeCopyPolicy get_copy_policy() const
Get the copy policy for external buffers.
Definition: Attribute.h:391
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:539
bool m_is_external
Flag to determine whether an attribute is using an external or internal buffer.
Definition: Attribute.h:704
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:517
void reserve_entries(size_t new_cap)
Reserve enough memory for new_cap entries.
Definition: Attribute.cpp:388
AttributeGrowthPolicy m_growth_policy
Growth policy for external buffers.
Definition: Attribute.h:691
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:505
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:499
void wrap_const(span< const ValueType > buffer, size_t num_elements)
Wraps a const external buffer into the attribute.
Definition: Attribute.cpp:296
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:531
void shrink_to_fit()
Shrink attribute buffer to fit the current number of entries.
Definition: Attribute.cpp:355
bool empty() const
Test whether the attribute is empty (its size is 0).
Definition: Attribute.h:465
void create_internal_copy()
Creates an internal copy of the attribute data.
Definition: Attribute.cpp:327
bool is_read_only() const
Checks whether the attribute is external and pointing to a const buffer.
Definition: Attribute.h:497
void set_write_policy(AttributeWritePolicy policy)
Sets the write policy for read-only external buffers.
Definition: Attribute.h:370
bool m_is_read_only
Flag to determine whether an external attribute is read-only or writable.
Definition: Attribute.h:709
void wrap(span< ValueType > buffer, size_t num_elements)
Wraps an external buffer into the attribute.
Definition: Attribute.cpp:271
AttributeWritePolicy m_write_policy
Policy for write access to read-only external buffers.
Definition: Attribute.h:697
AttributeShrinkPolicy m_shrink_policy
Shrink policy for external buffers.
Definition: Attribute.h:694
std::shared_ptr< const ValueType > m_owner
Optional aliased ptr to extend the lifetime of memory owner object of external buffer.
Definition: Attribute.h:676
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:545
size_t get_num_elements() const
Gets the number of elements.
Definition: Attribute.h:472
bool is_external() const
Checks whether an attribute buffer is external or internally managed.
Definition: Attribute.h:480
void set_copy_policy(AttributeCopyPolicy policy)
Set copy policy for external buffers.
Definition: Attribute.h:384
void set_growth_policy(AttributeGrowthPolicy policy)
Sets the growth policy for external buffers.
Definition: Attribute.h:342
AttributeShrinkPolicy get_shrink_policy() const
Gets the shrink policy for external buffers.
Definition: Attribute.h:363
ValueType get(size_t i, size_t c) const
Gets an entry for element i, at channel p.
Definition: Attribute.cpp:458
std::vector< ValueType > m_data
Internal buffer storing the data (when the attribute is not external).
Definition: Attribute.h:672
void clear()
Clears the attribute buffer (new number of elements is 0).
Definition: Attribute.cpp:343
Attribute(Attribute &&other) noexcept
Move constructor.
ValueType m_default_value
Default values used to populate buffer when the attribute grows.
Definition: Attribute.h:679
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:511
size_t m_num_elements
Number of elements associated with the attribute.
Definition: Attribute.h:712
void set_default_value(ValueType value)
Sets the default value to use when growing the attribute.
Definition: Attribute.h:330
void resize_elements(size_t num_elements)
Resize the buffer to contain num_elements elements.
Definition: Attribute.cpp:398
lagrange::span< ValueType > ref_all()
Returns a writable view of the buffer spanning num elements x num channels.
Definition: Attribute.cpp:492
AttributeWritePolicy get_write_policy() const
Gets the write policy for read-only external buffers.
Definition: Attribute.h:377
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:688
void set_shrink_policy(AttributeShrinkPolicy policy)
Sets the shrink policy for external buffers.
Definition: Attribute.h:356
AttributeCopyPolicy m_copy_policy
Copy policy for external buffers.
Definition: Attribute.h:700
Attribute(AttributeElement element, AttributeUsage usage, size_t num_channels)
Constructs a new instance.
Definition: Attribute.cpp:64
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:464
void insert_elements(span< const ValueType > values)
Inserts values for new elements.
Definition: Attribute.cpp:416
AttributeGrowthPolicy get_growth_policy() const
Gets the growth policy for external buffers.
Definition: Attribute.h:349
Shared span with ownership tracking.
Definition: SharedSpan.h:36
AttributeWritePolicy
Policy for attempting to write to read-only external buffers.
Definition: AttributeFwd.h:123
AttributeCopyPolicy
Policy for copying attribute that are views onto external buffers.
Definition: AttributeFwd.h:146
AttributeUsage
Usage tag indicating how the attribute should behave under mesh transformations.
Definition: AttributeFwd.h:54
AttributeShrinkPolicy
Policy for shrinking external attribute buffers.
Definition: AttributeFwd.h:110
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
AttributeGrowthPolicy
Policy for growing external attribute buffers.
Definition: AttributeFwd.h:96
@ ErrorIfReadOnly
Throws an exception when trying to write to a read-only buffer.
@ CopyIfExternal
Copy the buffer during copy if the attribute points to an external buffer.
@ ErrorIfExternal
Throws an exception when trying to shrink an external buffer (even if the new size is.
@ ErrorIfExternal
Throws an exception when trying to grow an external buffer (even if the new size is.
::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