Lagrange
|
Derived attribute class that stores the actual information. More...
#include <lagrange/Attribute.h>
Inherits AttributeBase.
Public Types | |
using | ValueType = ValueType_ |
Attribute value type. | |
Public Member Functions | |
template<typename SourceValueType > | |
Attribute< TargetValueType > | cast_copy (const Attribute< SourceValueType > &source) |
template<typename SourceValueType > | |
Attribute< TargetValueType > & | cast_assign (const Attribute< SourceValueType > &source) |
Attribute growth | |
void | set_default_value (ValueType value) |
Sets the default value to use when growing the attribute. More... | |
ValueType | get_default_value () const |
Gets the default value to use when growing the attribute. | |
void | set_growth_policy (AttributeGrowthPolicy policy) |
Sets the growth policy for external buffers. More... | |
AttributeGrowthPolicy | get_growth_policy () const |
Gets the growth policy for external buffers. More... | |
void | set_shrink_policy (AttributeShrinkPolicy policy) |
Sets the shrink policy for external buffers. More... | |
AttributeShrinkPolicy | get_shrink_policy () const |
Gets the shrink policy for external buffers. More... | |
void | set_write_policy (AttributeWritePolicy policy) |
Sets the write policy for read-only external buffers. More... | |
AttributeWritePolicy | get_write_policy () const |
Gets the write policy for read-only external buffers. More... | |
void | set_copy_policy (AttributeCopyPolicy policy) |
Set copy policy for external buffers. More... | |
AttributeCopyPolicy | get_copy_policy () const |
Get the copy policy for external buffers. More... | |
void | create_internal_copy () |
Creates an internal copy of the attribute data. More... | |
void | clear () |
Clears the attribute buffer (new number of elements is 0). | |
void | shrink_to_fit () |
Shrink attribute buffer to fit the current number of entries. More... | |
void | reserve_entries (size_t new_cap) |
Reserve enough memory for new_cap entries. More... | |
void | resize_elements (size_t num_elements) |
Resize the buffer to contain num_elements elements. More... | |
void | insert_elements (span< const ValueType > values) |
Inserts values for new elements. More... | |
void | insert_elements (std::initializer_list< const ValueType > values) |
Inserts values for new elements. More... | |
void | insert_elements (size_t count) |
Inserts new elements. More... | |
Attribute access | |
bool | empty () const |
Test whether the attribute is empty (its size is 0). More... | |
size_t | get_num_elements () const |
Gets the number of elements. More... | |
bool | is_external () const |
Checks whether an attribute buffer is external or internally managed. More... | |
bool | is_managed () const |
Checks whether the attribute is managing the lifetime of the underlying buffer. More... | |
bool | is_read_only () const |
Checks whether the attribute is external and pointing to a const buffer. More... | |
ValueType | get (size_t i, size_t c) const |
Gets an entry for element i , at channel p . More... | |
ValueType & | ref (size_t i, size_t c) |
Gets a writable reference to the entry for element i , at channel p . More... | |
ValueType | get (size_t i) const |
Gets an entry for a scalar element i . More... | |
ValueType & | ref (size_t i) |
Gets a writable reference to a scalar element i , at channel p . More... | |
lagrange::span< const ValueType > | get_all () const |
Returns a read-only view of the buffer spanning num elements x num channels. More... | |
lagrange::span< ValueType > | ref_all () |
Returns a writable view of the buffer spanning num elements x num channels. More... | |
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. More... | |
lagrange::span< ValueType > | ref_first (size_t num_elements) |
Returns a writable view of the attribute values for the first elements in the buffer. More... | |
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. More... | |
lagrange::span< ValueType > | ref_last (size_t num_elements) |
Returns a writable view of the attribute values for the last elements in the buffer. More... | |
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. More... | |
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. More... | |
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. More... | |
lagrange::span< ValueType > | ref_row (size_t element) |
Returns a writable view of the attribute values for the element in the buffer. More... | |
Public Member Functions inherited from AttributeBase | |
AttributeBase (AttributeElement element, AttributeUsage usage, size_t num_channels) | |
Constructs a new instance. More... | |
virtual | ~AttributeBase () |
Destroys the object. | |
AttributeBase (AttributeBase &&other)=default | |
Move constructor. More... | |
AttributeBase & | operator= (AttributeBase &&other)=default |
Assignment move operator. More... | |
AttributeBase (const AttributeBase &other)=default | |
Copy constructor. More... | |
AttributeBase & | operator= (const AttributeBase &other)=default |
Assignment copy operator. More... | |
virtual AttributeValueType | get_value_type () const =0 |
Gets the attribute value type. More... | |
AttributeElement | get_element_type () const |
Gets the attribute element type. More... | |
AttributeUsage | get_usage () const |
Gets the attribute usage tag. More... | |
size_t | get_num_channels () const |
Gets the number of channels for the attribute. More... | |
void | unsafe_set_usage (AttributeUsage usage) |
Sets the attribute usage tag. More... | |
void | unsafe_set_element_type (AttributeElement element) |
Sets the attribute element type. More... | |
Static Public Attributes | |
static constexpr bool | IsIndexed = false |
Whether this attribute type is indexed. | |
Protected Attributes | |
std::vector< ValueType > | m_data |
Internal buffer storing the data (when the attribute is not external). | |
std::shared_ptr< const ValueType > | m_owner |
Optional aliased ptr to extend the lifetime of memory owner object of external buffer. | |
ValueType | m_default_value = ValueType(0) |
Default values used to populate buffer when the attribute grows. | |
lagrange::span< ValueType > | m_view |
Writable pointer to the buffer storing the attribute data. More... | |
lagrange::span< const ValueType > | m_const_view |
Read-only pointer to the buffer storing the attribute data. More... | |
AttributeGrowthPolicy | m_growth_policy = AttributeGrowthPolicy::ErrorIfExternal |
Growth policy for external buffers. | |
AttributeShrinkPolicy | m_shrink_policy = AttributeShrinkPolicy::ErrorIfExternal |
Shrink policy for external buffers. | |
AttributeWritePolicy | m_write_policy = AttributeWritePolicy::ErrorIfReadOnly |
Policy for write access to read-only external buffers. | |
AttributeCopyPolicy | m_copy_policy = AttributeCopyPolicy::CopyIfExternal |
Copy policy for external buffers. | |
bool | m_is_external = false |
Flag to determine whether an attribute is using an external or internal buffer. More... | |
bool | m_is_read_only = false |
Flag to determine whether an external attribute is read-only or writable. More... | |
size_t | m_num_elements = 0 |
Number of elements associated with the attribute. | |
Protected Attributes inherited from AttributeBase | |
AttributeElement | m_element |
Element type (vertex, facet, indexed, etc.). | |
AttributeUsage | m_usage |
Attribute usage tag. | |
size_t | m_num_channels = 0 |
Number of channel for each value. | |
Attribute construction | |
Attribute (AttributeElement element, AttributeUsage usage, size_t num_channels) | |
Constructs a new instance. More... | |
~Attribute () override | |
Destroys the object. | |
Attribute (Attribute &&other) noexcept | |
Move constructor. More... | |
Attribute & | operator= (Attribute &&other) noexcept |
Assignment move operator. More... | |
Attribute (const Attribute &other) | |
Copy constructor. More... | |
Attribute & | operator= (const Attribute &other) |
Assignment copy operator. More... | |
template<typename OtherValue > | |
Attribute & | cast_assign (const Attribute< OtherValue > &other) |
Cast assignment operator. More... | |
AttributeValueType | get_value_type () const override |
Gets the attribute value type. More... | |
void | wrap (span< ValueType > buffer, size_t num_elements) |
Wraps an external buffer into the attribute. More... | |
void | wrap (SharedSpan< ValueType > buffer_ptr, size_t num_elements) |
Wraps an external buffer into the attribute. More... | |
void | wrap_const (span< const ValueType > buffer, size_t num_elements) |
Wraps a const external buffer into the attribute. More... | |
void | wrap_const (SharedSpan< const ValueType > shared_buffer, size_t num_elements) |
Wraps a const external buffer into the attribute. More... | |
template<typename OtherValue > | |
static Attribute | cast_copy (const Attribute< OtherValue > &other) |
Cast copy operator. More... | |
Derived attribute class that stores the actual information.
ValueType_ | Attribute value type. |
Attribute | ( | AttributeElement | element, |
AttributeUsage | usage, | ||
size_t | num_channels | ||
) |
Constructs a new instance.
[in] | element | Element type (vertex, facet, etc.). |
[in] | usage | Usage tag. |
[in] | num_channels | Number of channels. |
Move constructor.
other | Instance to move from. |
Copy constructor.
[in] | other | Instance to copy from. |
Assignment move operator.
other | Instance to move from. |
Assignment copy operator.
[in] | other | Instance to copy from. |
Cast copy operator.
Creates an attribute by copying and casting values from another attribute with a different ValueType. Will print a warning if the source and target value types are identical.
[in] | other | The other attribute to copy & cast from. |
OtherValue | Value type for the other attribute. |
Cast assignment operator.
Replace the current attribute by copying and casting values from another attribute with a different ValueType. Will print a warning if the source and target value types are identical.
[in] | other | The other attribute to copy & cast from. |
OtherValue | Value type for the other attribute. |
|
overridevirtual |
void wrap | ( | lagrange::span< ValueType > | buffer, |
size_t | num_elements | ||
) |
Wraps an external buffer into the attribute.
The pointer must remain valid for the lifetime of the attribute. Note that only the number of element is allowed to change when wrapping an external buffer (the number of channels is fixed during the attribute construction).
[in] | buffer | Pointer to an external buffer to be used as storage. The pointed buffer must have a capacity (determined by span::size()) that is large enough to store num elements x num channels entries. The pointed buffer can be larger than required, in which case the padding capacity can be used to grow the attribute, as determined by the AttributeGrowthPolicy. |
[in] | num_elements | New number of elements associated with the attribute. |
void wrap | ( | SharedSpan< ValueType > | buffer_ptr, |
size_t | num_elements | ||
) |
Wraps an external buffer into the attribute.
The buffer ownership is shared with the attribute. Note that only the number of element is allowed to change when wrapping an external buffer (the number of channels is fixed during the attribute construction).
[in] | shared_buffer | Pointer to an external buffer managed by a SharedSpan to be used as storage. This pointer exposes a view of the buffer managed by the owner. The buffer must have a capacity (determined by buffer_ptr.size()) that is large enough to store num elements x num channels entries. The buffer can be larger than required, in which case the padding capacity can be used to grow the attribute, as determined by the AttributeGrowthPolicy. |
[in] | num_elements | New number of elements associated with the attribute. |
void wrap_const | ( | lagrange::span< const ValueType > | buffer, |
size_t | num_elements | ||
) |
Wraps a const external buffer into the attribute.
The pointer must remain valid for the lifetime of the attribute. Note that only the number of element is allowed to change when wrapping an external buffer (the number of channels is fixed during the attribute construction). Following this, any write operation to the attribute will throw an exception.
[in] | buffer | Pointer to an external buffer to be used as storage. The pointed buffer must have a capacity (determined by span::size()) that is large enough to store num elements x num channels entries. The pointed buffer can be larger than required, in which case the padding capacity can be used to grow the attribute, as determined by the AttributeGrowthPolicy. |
[in] | num_elements | New number of elements associated with the attribute. |
void wrap_const | ( | SharedSpan< const ValueType > | shared_buffer, |
size_t | num_elements | ||
) |
Wraps a const external buffer into the attribute.
The buffer ownership is shared with the attribute. Note that only the number of element is allowed to change when wrapping an external buffer (the number of channels is fixed during the attribute construction).
[in] | shared_buffer | Pointer to an external buffer managed by a SharedSpan to be used as storage. This pointer exposes a view of the buffer managed by the owner. The buffer must have a capacity (determined by shared_buffer.size()) that is large enough to store num elements x num channels entries. The buffer can be larger than required, in which case the padding capacity can be used to grow the attribute, as determined by the AttributeGrowthPolicy. |
[in] | num_elements | New number of elements associated with the attribute. |
|
inline |
Sets the default value to use when growing the attribute.
[in] | value | New default value. |
|
inline |
Sets the growth policy for external buffers.
[in] | policy | New policy. |
|
inline |
Gets the growth policy for external buffers.
|
inline |
Sets the shrink policy for external buffers.
[in] | policy | New policy. |
|
inline |
Gets the shrink policy for external buffers.
|
inline |
Sets the write policy for read-only external buffers.
[in] | policy | New policy. |
|
inline |
Gets the write policy for read-only external buffers.
|
inline |
Set copy policy for external buffers.
[in] | policy | New policy |
|
inline |
Get the copy policy for external buffers.
void create_internal_copy |
Creates an internal copy of the attribute data.
The attribute buffer must be external before calling this function. An internal copy of the buffer is created (including padding size). This can be used prior to attribute export to ensure lifetime of the underlying object.
void shrink_to_fit |
Shrink attribute buffer to fit the current number of entries.
If the attribute points to an external buffer, an internal copy will be created if the external buffer capacity exceeds the number of entries in the attribute.
void reserve_entries | ( | size_t | new_cap | ) |
Reserve enough memory for new_cap
entries.
The new capacity does not need to be a multiple of the number of channels (e.g. one may want to pad the last vertex coordinate with a single value).
[in] | new_cap | The new buffer capacity. |
void resize_elements | ( | size_t | num_elements | ) |
Resize the buffer to contain num_elements
elements.
New attribute entries will be zero-initialized.
[in] | num_elements | New number of elements. |
void insert_elements | ( | lagrange::span< const ValueType > | values | ) |
Inserts values for new elements.
[in] | values | Values to be inserted. The span size must be a multiple of number of channels for the attribute. |
void insert_elements | ( | std::initializer_list< const ValueType > | values | ) |
Inserts values for new elements.
[in] | values | Values to be inserted. The span size must be a multiple of number of channels for the attribute. |
void insert_elements | ( | size_t | count | ) |
Inserts new elements.
Use set_default_value to use a non-zero default value to initialize the new elements.
[in] | count | The number elements to insert. |
|
inline |
Test whether the attribute is empty (its size is 0).
|
inline |
Gets the number of elements.
|
inline |
Checks whether an attribute buffer is external or internally managed.
External buffers must remain valid for the lifetime of the attribute object.
|
inline |
Checks whether the attribute is managing the lifetime of the underlying buffer.
This is true for internal attributes (the buffer is allocated by the attribute itself), or for external attributes created by wrapping a SharedSpan object.
|
inline |
Checks whether the attribute is external and pointing to a const buffer.
If the attribute data is internally managed, this always returns false.
ValueType get | ( | size_t | i, |
size_t | c | ||
) | const |
Gets an entry for element i
, at channel p
.
Note that due to internal checks, it is more efficient to first retrieve a span() of the whole buffer, then do access operations on this span inside a for loop.
[in] | i | Element to access. |
[in] | c | Channel to access. |
ValueType & ref | ( | size_t | i, |
size_t | c | ||
) |
Gets a writable reference to the entry for element i
, at channel p
.
Note that due to internal checks, it is more efficient to first retrieve a span() of the whole buffer, then do access operations on this span inside a for loop.
[in] | i | Element to access. |
[in] | c | Channel to access. |
ValueType get | ( | size_t | i | ) | const |
Gets an entry for a scalar element i
.
Note that due to internal checks, it is more efficient to first retrieve a span() of the whole buffer, then do access operations on this span inside a for loop.
[in] | i | Element to access. |
ValueType & ref | ( | size_t | i | ) |
Gets a writable reference to a scalar element i
, at channel p
.
Note that due to internal checks, it is more efficient to first retrieve a span() of the whole buffer, then do access operations on this span inside a for loop.
[in] | i | Element to access. |
lagrange::span< const ValueType > get_all |
Returns a read-only view of the buffer spanning num elements x num channels.
The actual buffer may have a larger capacity (e.g. used for padding).
lagrange::span< ValueType > ref_all |
Returns a writable view of the buffer spanning num elements x num channels.
The actual buffer may have a larger capacity (e.g. used for padding).
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.
[in] | num_elements | Number of elements to retrieve. |
lagrange::span< ValueType > ref_first | ( | size_t | num_elements | ) |
Returns a writable view of the attribute values for the first elements in the buffer.
[in] | num_elements | Number of elements to retrieve. |
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.
[in] | num_elements | Number of elements to retrieve. |
lagrange::span< ValueType > ref_last | ( | size_t | num_elements | ) |
Returns a writable view of the attribute values for the last elements in the buffer.
[in] | num_elements | Number of elements to retrieve. |
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.
[in] | first_element | Index of the first element to retrieve. |
[in] | num_elements | Number of elements to retrieve. |
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.
[in] | first_element | Index of the first element to retrieve. |
[in] | num_elements | Number of elements to retrieve. |
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.
[in] | element | Index of the element to retrieve. |
lagrange::span< ValueType > ref_row | ( | size_t | element | ) |
Returns a writable view of the attribute values for the element in the buffer.
[in] | element | Index of the first element to retrieve. |
|
protected |
Writable pointer to the buffer storing the attribute data.
For internal buffers, this will point to m_data.data(). For external buffers, it will be empty() if we are wrapping a read-only buffer, and point to the external data otherwise.
|
protected |
Read-only pointer to the buffer storing the attribute data.
For internal buffers, this will point to m_data.data(). For external buffers, it will point to the external data.
|
protected |
Flag to determine whether an attribute is using an external or internal buffer.
We need this flag to distinguish if an empty buffer is internally managed or external.
|
protected |
Flag to determine whether an external attribute is read-only or writable.
We need this to distinguish whether a pointer to an empty buffer is read-only or "writable" (in the sense that a call to ref_all() should not produce an error).