Lagrange
Loading...
Searching...
No Matches
Attribute< ValueType_ > Class Template Reference

Derived attribute class that stores the actual information. More...

#include <lagrange/Attribute.h>

Inheritance diagram for Attribute< ValueType_ >:
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.
 
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.
 
AttributeGrowthPolicy get_growth_policy () const
 Gets the growth policy for external buffers.
 
void set_shrink_policy (AttributeShrinkPolicy policy)
 Sets the shrink policy for external buffers.
 
AttributeShrinkPolicy get_shrink_policy () const
 Gets the shrink policy for external buffers.
 
void set_write_policy (AttributeWritePolicy policy)
 Sets the write policy for read-only external buffers.
 
AttributeWritePolicy get_write_policy () const
 Gets the write policy for read-only external buffers.
 
void set_copy_policy (AttributeCopyPolicy policy)
 Sets the copy policy for external buffers.
 
AttributeCopyPolicy get_copy_policy () const
 Gets the copy policy for external buffers.
 
void set_cast_policy (AttributeCastPolicy policy)
 Sets the cast policy.
 
AttributeCastPolicy get_cast_policy () const
 Gets the cast policy.
 
void create_internal_copy ()
 Creates an internal copy of the attribute data.
 
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.
 
void reserve_entries (size_t new_cap)
 Reserve enough memory for new_cap entries.
 
void resize_elements (size_t num_elements)
 Resize the buffer to contain num_elements elements.
 
void insert_elements (span< const ValueType > values)
 Inserts values for new elements.
 
void insert_elements (std::initializer_list< const ValueType > values)
 Inserts values for new elements.
 
void insert_elements (size_t count)
 Inserts new elements.
 
Attribute access
bool empty () const
 Test whether the attribute is empty (its size is 0).
 
size_t get_num_elements () const
 Gets the number of elements.
 
bool is_external () const
 Checks whether an attribute buffer is external or internally managed.
 
bool is_managed () const
 Checks whether the attribute is managing the lifetime of the underlying buffer.
 
bool is_read_only () const
 Checks whether the attribute is external and pointing to a const buffer.
 
ValueType get (size_t i, size_t c) const
 Gets an entry for element i, at channel p.
 
ValueTyperef (size_t i, size_t c)
 Gets a writable reference to the entry for element i, at channel p.
 
ValueType get (size_t i) const
 Gets an entry for a scalar element i.
 
ValueTyperef (size_t i)
 Gets a writable reference to a scalar element i, at channel p.
 
lagrange::span< const ValueTypeget_all () const
 Returns a read-only view of the buffer spanning num elements x num channels.
 
lagrange::span< ValueTyperef_all ()
 Returns a writable view of the buffer spanning num elements x num channels.
 
lagrange::span< const ValueTypeget_first (size_t num_elements) const
 Returns a read-only view of the attribute values for the first elements in the buffer.
 
lagrange::span< ValueTyperef_first (size_t num_elements)
 Returns a writable view of the attribute values for the first elements in the buffer.
 
lagrange::span< const ValueTypeget_last (size_t num_elements) const
 Returns a read-only view of the attribute values for the last elements in the buffer.
 
lagrange::span< ValueTyperef_last (size_t num_elements)
 Returns a writable view of the attribute values for the last elements in the buffer.
 
lagrange::span< const ValueTypeget_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.
 
lagrange::span< ValueTyperef_middle (size_t first_element, size_t num_elements)
 Returns a writable view of the attribute values for the middle elements in the buffer.
 
lagrange::span< const ValueTypeget_row (size_t element) const
 Returns a read-only view of the attribute values for the element in the buffer.
 
lagrange::span< ValueTyperef_row (size_t element)
 Returns a writable view of the attribute values for the element in the buffer.
 
- Public Member Functions inherited from AttributeBase
 AttributeBase (AttributeElement element, AttributeUsage usage, size_t num_channels)
 Constructs a new instance.
 
virtual ~AttributeBase ()
 Destroys the object.
 
 AttributeBase (AttributeBase &&other)=default
 Move constructor.
 
AttributeBaseoperator= (AttributeBase &&other)=default
 Assignment move operator.
 
 AttributeBase (const AttributeBase &other)=default
 Copy constructor.
 
AttributeBaseoperator= (const AttributeBase &other)=default
 Assignment copy operator.
 
AttributeElement get_element_type () const
 Gets the attribute element type.
 
AttributeUsage get_usage () const
 Gets the attribute usage tag.
 
size_t get_num_channels () const
 Gets the number of channels for the attribute.
 
void unsafe_set_usage (AttributeUsage usage)
 Sets the attribute usage tag.
 
void unsafe_set_element_type (AttributeElement element)
 Sets the attribute element type.
 

Static Public Attributes

static constexpr bool IsIndexed = false
 Whether this attribute type is indexed.
 

Protected Attributes

std::vector< ValueTypem_data
 Internal buffer storing the data (when the attribute is not external).
 
std::shared_ptr< const ValueTypem_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< ValueTypem_view
 Writable pointer to the buffer storing the attribute data.
 
lagrange::span< const ValueTypem_const_view
 Read-only pointer to the buffer storing the attribute data.
 
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.
 
AttributeCastPolicy m_cast_policy = AttributeCastPolicy::RemapInvalidIndices
 Cast policy when converting from this attribute.
 
bool m_is_external = false
 Flag to determine whether an attribute is using an external or internal buffer.
 
bool m_is_read_only = false
 Flag to determine whether an external attribute is read-only or writable.
 
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.
 
 ~Attribute () override
 Destroys the object.
 
 Attribute (Attribute &&other) noexcept
 Move constructor.
 
Attributeoperator= (Attribute &&other) noexcept
 Assignment move operator.
 
 Attribute (const Attribute &other)
 Copy constructor.
 
Attributeoperator= (const Attribute &other)
 Assignment copy operator.
 
template<typename OtherValue>
Attributecast_assign (const Attribute< OtherValue > &other)
 Cast assignment operator.
 
AttributeValueType get_value_type () const override
 Gets the attribute value type.
 
void wrap (span< ValueType > buffer, size_t num_elements)
 Wraps an external buffer into the attribute.
 
void wrap (SharedSpan< ValueType > buffer_ptr, size_t num_elements)
 Wraps an external buffer into the attribute.
 
void wrap_const (span< const ValueType > buffer, size_t num_elements)
 Wraps a const external buffer into the attribute.
 
void wrap_const (SharedSpan< const ValueType > shared_buffer, size_t num_elements)
 Wraps a const external buffer into the attribute.
 
template<typename OtherValue>
static Attribute cast_copy (const Attribute< OtherValue > &other)
 Cast copy operator.
 

Detailed Description

template<typename ValueType_>
class lagrange::Attribute< ValueType_ >

Derived attribute class that stores the actual information.

Template Parameters
ValueType_Attribute value type.

Constructor & Destructor Documentation

◆ Attribute() [1/3]

template<typename ValueType>
Attribute ( AttributeElement element,
AttributeUsage usage,
size_t num_channels )

Constructs a new instance.

Parameters
[in]elementElement type (vertex, facet, etc.).
[in]usageUsage tag.
[in]num_channelsNumber of channels.

◆ Attribute() [2/3]

template<typename ValueType_>
Attribute ( Attribute< ValueType_ > && other)
noexcept

Move constructor.

Parameters
otherInstance to move from.

◆ Attribute() [3/3]

template<typename ValueType_>
Attribute ( const Attribute< ValueType_ > & other)
explicit

Copy constructor.

Parameters
[in]otherInstance to copy from.

Member Function Documentation

◆ operator=() [1/2]

template<typename ValueType_>
Attribute & operator= ( Attribute< ValueType_ > && other)
noexcept

Assignment move operator.

Parameters
otherInstance to move from.
Returns
The result of the assignment.

◆ operator=() [2/2]

template<typename ValueType_>
Attribute & operator= ( const Attribute< ValueType_ > & other)

Assignment copy operator.

Parameters
[in]otherInstance to copy from.
Returns
The result of the assignment.

◆ cast_copy()

template<typename ValueType_>
template<typename OtherValue>
static Attribute cast_copy ( const Attribute< OtherValue > & other)
static

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.

Parameters
[in]otherThe other attribute to copy & cast from.
Template Parameters
OtherValueValue type for the other attribute.
Returns
A new attribute object whose values have been cast to the desired type.

◆ cast_assign()

template<typename ValueType_>
template<typename OtherValue>
Attribute & cast_assign ( const Attribute< OtherValue > & other)

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.

Parameters
[in]otherThe other attribute to copy & cast from.
Template Parameters
OtherValueValue type for the other attribute.
Returns
A reference to the assignment result.

◆ get_value_type()

template<typename ValueType_>
AttributeValueType get_value_type ( ) const
nodiscardoverridevirtual

Gets the attribute value type.

Returns
An enum describing the value type.

Implements AttributeBase.

◆ wrap() [1/2]

template<typename ValueType>
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).

Parameters
[in]bufferPointer 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_elementsNew number of elements associated with the attribute.

◆ wrap() [2/2]

template<typename ValueType>
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).

Parameters
[in]shared_bufferPointer 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_elementsNew number of elements associated with the attribute.

◆ wrap_const() [1/2]

template<typename ValueType>
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.

Parameters
[in]bufferPointer 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_elementsNew number of elements associated with the attribute.

◆ wrap_const() [2/2]

template<typename ValueType>
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).

Parameters
[in]shared_bufferPointer 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_elementsNew number of elements associated with the attribute.

◆ set_default_value()

template<typename ValueType_>
void set_default_value ( ValueType value)
inline

Sets the default value to use when growing the attribute.

Parameters
[in]valueNew default value.

◆ set_growth_policy()

template<typename ValueType_>
void set_growth_policy ( AttributeGrowthPolicy policy)
inline

Sets the growth policy for external buffers.

Parameters
[in]policyNew policy.

◆ get_growth_policy()

template<typename ValueType_>
AttributeGrowthPolicy get_growth_policy ( ) const
inline

Gets the growth policy for external buffers.

Returns
The growth policy.

◆ set_shrink_policy()

template<typename ValueType_>
void set_shrink_policy ( AttributeShrinkPolicy policy)
inline

Sets the shrink policy for external buffers.

Parameters
[in]policyNew policy.

◆ get_shrink_policy()

template<typename ValueType_>
AttributeShrinkPolicy get_shrink_policy ( ) const
inline

Gets the shrink policy for external buffers.

Returns
The shrink policy.

◆ set_write_policy()

template<typename ValueType_>
void set_write_policy ( AttributeWritePolicy policy)
inline

Sets the write policy for read-only external buffers.

Parameters
[in]policyNew policy.

◆ get_write_policy()

template<typename ValueType_>
AttributeWritePolicy get_write_policy ( ) const
inline

Gets the write policy for read-only external buffers.

Returns
The attribute write policy.

◆ set_copy_policy()

template<typename ValueType_>
void set_copy_policy ( AttributeCopyPolicy policy)
inline

Sets the copy policy for external buffers.

Parameters
[in]policyNew policy

◆ get_copy_policy()

template<typename ValueType_>
AttributeCopyPolicy get_copy_policy ( ) const
inline

Gets the copy policy for external buffers.

Returns
The attribute copy policy.

◆ set_cast_policy()

template<typename ValueType_>
void set_cast_policy ( AttributeCastPolicy policy)
inline

Sets the cast policy.

Parameters
[in]policyNew policy.

◆ get_cast_policy()

template<typename ValueType_>
AttributeCastPolicy get_cast_policy ( ) const
inline

Gets the cast policy.

Returns
The attribute cast policy.

◆ create_internal_copy()

template<typename ValueType>
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.

◆ shrink_to_fit()

template<typename ValueType>
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.

◆ reserve_entries()

template<typename ValueType>
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).

Parameters
[in]new_capThe new buffer capacity.

◆ resize_elements()

template<typename ValueType>
void resize_elements ( size_t num_elements)

Resize the buffer to contain num_elements elements.

New attribute entries will be zero-initialized.

Parameters
[in]num_elementsNew number of elements.

◆ insert_elements() [1/3]

template<typename ValueType>
void insert_elements ( lagrange::span< const ValueType > values)

Inserts values for new elements.

Parameters
[in]valuesValues to be inserted. The span size must be a multiple of number of channels for the attribute.

◆ insert_elements() [2/3]

template<typename ValueType>
void insert_elements ( std::initializer_list< const ValueType > values)

Inserts values for new elements.

Parameters
[in]valuesValues to be inserted. The span size must be a multiple of number of channels for the attribute.

◆ insert_elements() [3/3]

template<typename ValueType>
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.

See also
set_default_value
Parameters
[in]countThe number elements to insert.

◆ empty()

template<typename ValueType_>
bool empty ( ) const
inlinenodiscard

Test whether the attribute is empty (its size is 0).

Returns
Whether the attribute is empty.

◆ get_num_elements()

template<typename ValueType_>
size_t get_num_elements ( ) const
inlinenodiscard

Gets the number of elements.

Returns
The number of elements.

◆ is_external()

template<typename ValueType_>
bool is_external ( ) const
inlinenodiscard

Checks whether an attribute buffer is external or internally managed.

External buffers must remain valid for the lifetime of the attribute object.

Returns
True if the buffer is external, False otherwise.

◆ is_managed()

template<typename ValueType_>
bool is_managed ( ) const
inlinenodiscard

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.

Returns
True if the buffer is managed, False otherwise.

◆ is_read_only()

template<typename ValueType_>
bool is_read_only ( ) const
inlinenodiscard

Checks whether the attribute is external and pointing to a const buffer.

If the attribute data is internally managed, this always returns false.

Returns
True if the data is read only, False otherwise.

◆ get() [1/2]

template<typename ValueType>
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.

Parameters
[in]iElement to access.
[in]cChannel to access.
Returns
Value of the element attribute for the given channel.

◆ ref() [1/2]

template<typename ValueType>
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.

Parameters
[in]iElement to access.
[in]cChannel to access.
Returns
Reference to the element attribute for the given channel.

◆ get() [2/2]

template<typename ValueType>
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.

Parameters
[in]iElement to access.
Returns
Value of the element attribute.

◆ ref() [2/2]

template<typename ValueType>
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.

Parameters
[in]iElement to access.
Returns
Reference to the element attribute.

◆ get_all()

template<typename ValueType>
lagrange::span< const ValueType > get_all ( ) const

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).

Returns
A read-only view of the attribute buffer.

◆ ref_all()

template<typename ValueType>
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).

Returns
A writable view of the attribute buffer.

◆ get_first()

template<typename ValueType>
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.

Parameters
[in]num_elementsNumber of elements to retrieve.
Returns
A read-only view of the attribute values.

◆ ref_first()

template<typename ValueType>
lagrange::span< ValueType > ref_first ( size_t num_elements)

Returns a writable view of the attribute values for the first elements in the buffer.

Parameters
[in]num_elementsNumber of elements to retrieve.
Returns
A writable view of the attribute values.

◆ get_last()

template<typename ValueType>
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.

Parameters
[in]num_elementsNumber of elements to retrieve.
Returns
A read-only view of the attribute values.

◆ ref_last()

template<typename ValueType>
lagrange::span< ValueType > ref_last ( size_t num_elements)

Returns a writable view of the attribute values for the last elements in the buffer.

Parameters
[in]num_elementsNumber of elements to retrieve.
Returns
A writable view of the attribute values.

◆ get_middle()

template<typename ValueType>
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.

Parameters
[in]first_elementIndex of the first element to retrieve.
[in]num_elementsNumber of elements to retrieve.
Returns
A read-only view of the attribute values.

◆ ref_middle()

template<typename ValueType>
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.

Parameters
[in]first_elementIndex of the first element to retrieve.
[in]num_elementsNumber of elements to retrieve.
Returns
A writable view of the attribute values.

◆ get_row()

template<typename ValueType>
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.

Parameters
[in]elementIndex of the element to retrieve.
Returns
A read-only view of the attribute values.

◆ ref_row()

template<typename ValueType>
lagrange::span< ValueType > ref_row ( size_t element)

Returns a writable view of the attribute values for the element in the buffer.

Parameters
[in]elementIndex of the first element to retrieve.
Returns
A writable view of the attribute values.

Member Data Documentation

◆ m_view

template<typename ValueType_>
lagrange::span<ValueType> m_view
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.

◆ m_const_view

template<typename ValueType_>
lagrange::span<const ValueType> m_const_view
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.

◆ m_is_external

template<typename ValueType_>
bool m_is_external = false
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.

◆ m_is_read_only

template<typename ValueType_>
bool m_is_read_only = false
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).


The documentation for this class was generated from the following files: