Loading...
Searching...
No Matches
AttributeArrayString.h
Go to the documentation of this file.
// Copyright Contributors to the OpenVDB Project
// SPDX-License-Identifier: MPL-2.0
/// @file points/AttributeArrayString.h
///
/// @author Dan Bailey
///
/// @brief Attribute array storage for string data using Descriptor Metadata.
#ifndef OPENVDB_POINTS_ATTRIBUTE_ARRAY_STRING_HAS_BEEN_INCLUDED
#define OPENVDB_POINTS_ATTRIBUTE_ARRAY_STRING_HAS_BEEN_INCLUDED
#include "AttributeArray.h"
#include <memory>
#include <deque>
#include <unordered_map>
namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
namespace OPENVDB_VERSION_NAME {
namespace points {
////////////////////////////////////////
namespace attribute_traits
{
template <bool Truncate> struct StringTypeTrait { using Type = Index; };
template<> struct StringTypeTrait</*Truncate=*/true> { using Type = uint16_t; };
}
template <bool Truncate>
struct StringCodec
{
using ValueType = Index;
template <typename T>
struct Storage { using Type = typename attribute_traits::StringTypeTrait<Truncate>::Type; };
template<typename StorageType> static void decode(const StorageType&, ValueType&);
template<typename StorageType> static void encode(const ValueType&, StorageType&);
static const char* name() { return Truncate ? "str_trnc" : "str"; }
};
using StringAttributeArray = TypedAttributeArray<Index, StringCodec<false>>;
////////////////////////////////////////
/// Class to compute a string->index map from all string:N metadata
class OPENVDB_API StringMetaCache
{
public:
using UniquePtr = std::unique_ptr<StringMetaCache>;
using ValueMap = std::unordered_map<Name, Index>;
StringMetaCache() = default;
explicit StringMetaCache(const MetaMap& metadata);
/// Return @c true if no string elements in metadata
bool empty() const { return mCache.empty(); }
/// Returns the number of string elements in metadata
size_t size() const { return mCache.size(); }
/// Clears and re-populates the cache
void reset(const MetaMap& metadata);
/// Insert a new element in the cache
void insert(const Name& key, Index index);
/// Retrieve the value map (string -> index)
const ValueMap& map() const { return mCache; }
private:
ValueMap mCache;
}; // StringMetaCache
////////////////////////////////////////
/// Class to help with insertion of keyed string values into metadata
class OPENVDB_API StringMetaInserter
{
public:
using UniquePtr = std::unique_ptr<StringMetaInserter>;
explicit StringMetaInserter(MetaMap& metadata);
/// Returns @c true if key exists
bool hasKey(const Name& key) const;
/// Returns @c true if index exists
bool hasIndex(Index index) const;
/// @brief Insert the string into the metadata using the hint if non-zero
/// @param name the string to insert
/// @param hint requested index to use if non-zero and not already in use
/// @note the hint can be used to insert non-sequentially so as to avoid an
/// expensive re-indexing of string keys
/// @return the chosen index which will match hint if the hint was used
Index insert(const Name& name, Index hint = Index(0));
/// Reset the cache from the metadata
void resetCache();
private:
using IndexPairArray = std::deque<std::pair<Index, Index>>;
MetaMap& mMetadata;
IndexPairArray mIdBlocks;
StringMetaCache mCache;
}; // StringMetaInserter
////////////////////////////////////////
template <bool Truncate>
template<typename StorageType>
inline void
StringCodec<Truncate>::decode(const StorageType& data, ValueType& val)
{
val = static_cast<ValueType>(data);
}
template <bool Truncate>
template<typename StorageType>
inline void
StringCodec<Truncate>::encode(const ValueType& val, StorageType& data)
{
data = static_cast<ValueType>(val);
}
////////////////////////////////////////
inline bool isString(const AttributeArray& array)
{
return array.isType<StringAttributeArray>();
}
////////////////////////////////////////
class OPENVDB_API StringAttributeHandle
{
public:
using Ptr = std::shared_ptr<StringAttributeHandle>;//SharedPtr<StringAttributeHandle>;
using UniquePtr = std::unique_ptr<StringAttributeHandle>;
static Ptr create(const AttributeArray& array, const MetaMap& metadata, const bool preserveCompression = true);
StringAttributeHandle( const AttributeArray& array,
const MetaMap& metadata,
const bool preserveCompression = true);
Index stride() const { return mHandle.stride(); }
Index size() const { return mHandle.size(); }
bool isUniform() const { return mHandle.isUniform(); }
bool hasConstantStride() const { return mHandle.hasConstantStride(); }
Name get(Index n, Index m = 0) const;
void get(Name& name, Index n, Index m = 0) const;
/// @brief Returns a reference to the array held in the Handle.
const AttributeArray& array() const;
protected:
AttributeHandle<Index, StringCodec<false>> mHandle;
const MetaMap& mMetadata;
}; // class StringAttributeHandle
////////////////////////////////////////
class OPENVDB_API StringAttributeWriteHandle : public StringAttributeHandle
{
public:
using Ptr = std::shared_ptr<StringAttributeWriteHandle>;//SharedPtr<StringAttributeWriteHandle>;
using UniquePtr = std::unique_ptr<StringAttributeWriteHandle>;
static Ptr create(AttributeArray& array, const MetaMap& metadata, const bool expand = true);
StringAttributeWriteHandle( AttributeArray& array,
const MetaMap& metadata,
const bool expand = true);
/// @brief If this array is uniform, replace it with an array of length size().
/// @param fill if true, assign the uniform value to each element of the array.
void expand(bool fill = true);
/// @brief Set membership for the whole array and attempt to collapse
void collapse();
/// @brief Set membership for the whole array and attempt to collapse
/// @param name Name of the String
void collapse(const Name& name);
/// Compact the existing array to become uniform if all values are identical
bool compact();
/// @brief Fill the existing array with the given value.
/// @note Identical to collapse() except a non-uniform array will not become uniform.
void fill(const Name& name);
/// Set the value of the index to @a name
void set(Index n, const Name& name);
void set(Index n, Index m, const Name& name);
/// Reset the value cache from the metadata
void resetCache();
/// @brief Returns a reference to the array held in the Write Handle.
AttributeArray& array();
/// @brief Returns whether or not the metadata cache contains a given value.
/// @param name Name of the String.
bool contains(const Name& name) const;
private:
/// Retrieve the index of this string value from the cache
/// @note throws if name does not exist in cache
Index getIndex(const Name& name) const;
StringMetaCache mCache;
AttributeWriteHandle<Index, StringCodec<false>> mWriteHandle;
}; // class StringAttributeWriteHandle
////////////////////////////////////////
} // namespace points
} // namespace OPENVDB_VERSION_NAME
} // namespace openvdb
#endif // OPENVDB_POINTS_ATTRIBUTE_ARRAY_STRING_HAS_BEEN_INCLUDED
AttributeArray.h
Attribute Array storage templated on type and compression codec.
OPENVDB_API
#define OPENVDB_API
Definition Platform.h:274
openvdb::v11_0::MetaMap
Container that maps names (strings) to values of arbitrary types.
Definition MetaMap.h:20
openvdb::v11_0::points::AttributeArray
Base class for storing attribute data.
Definition AttributeArray.h:93
openvdb::v11_0::points::AttributeArray::isType
bool isType() const
Return true if this attribute is of the same type as the template parameter.
Definition AttributeArray.h:224
openvdb::v11_0::points::AttributeHandle
Definition AttributeArray.h:833
openvdb::v11_0::points::AttributeWriteHandle
Write-able version of AttributeHandle.
Definition AttributeArray.h:904
openvdb::v11_0::points::StringAttributeHandle::mHandle
AttributeHandle< Index, StringCodec< false > > mHandle
Definition AttributeArrayString.h:178
openvdb::v11_0::points::StringAttributeHandle::size
Index size() const
Definition AttributeArrayString.h:166
openvdb::v11_0::points::StringAttributeHandle::UniquePtr
std::unique_ptr< StringAttributeHandle > UniquePtr
Definition AttributeArrayString.h:157
openvdb::v11_0::points::StringAttributeHandle::stride
Index stride() const
Definition AttributeArrayString.h:165
openvdb::v11_0::points::StringAttributeHandle::get
Name get(Index n, Index m=0) const
openvdb::v11_0::points::StringAttributeHandle::Ptr
std::shared_ptr< StringAttributeHandle > Ptr
Definition AttributeArrayString.h:156
openvdb::v11_0::points::StringAttributeHandle::create
static Ptr create(const AttributeArray &array, const MetaMap &metadata, const bool preserveCompression=true)
openvdb::v11_0::points::StringAttributeHandle::mMetadata
const MetaMap & mMetadata
Definition AttributeArrayString.h:179
openvdb::v11_0::points::StringAttributeHandle::StringAttributeHandle
StringAttributeHandle(const AttributeArray &array, const MetaMap &metadata, const bool preserveCompression=true)
openvdb::v11_0::points::StringAttributeHandle::array
const AttributeArray & array() const
Returns a reference to the array held in the Handle.
openvdb::v11_0::points::StringAttributeHandle::isUniform
bool isUniform() const
Definition AttributeArrayString.h:168
openvdb::v11_0::points::StringAttributeHandle::hasConstantStride
bool hasConstantStride() const
Definition AttributeArrayString.h:169
openvdb::v11_0::points::StringAttributeHandle::get
void get(Name &name, Index n, Index m=0) const
openvdb::v11_0::points::StringAttributeWriteHandle::fill
void fill(const Name &name)
Fill the existing array with the given value.
openvdb::v11_0::points::StringAttributeWriteHandle::contains
bool contains(const Name &name) const
Returns whether or not the metadata cache contains a given value.
openvdb::v11_0::points::StringAttributeWriteHandle::compact
bool compact()
Compact the existing array to become uniform if all values are identical.
openvdb::v11_0::points::StringAttributeWriteHandle::collapse
void collapse()
Set membership for the whole array and attempt to collapse.
openvdb::v11_0::points::StringAttributeWriteHandle::resetCache
void resetCache()
Reset the value cache from the metadata.
openvdb::v11_0::points::StringAttributeWriteHandle::collapse
void collapse(const Name &name)
Set membership for the whole array and attempt to collapse.
openvdb::v11_0::points::StringAttributeWriteHandle::Ptr
std::shared_ptr< StringAttributeWriteHandle > Ptr
Definition AttributeArrayString.h:189
openvdb::v11_0::points::StringAttributeWriteHandle::expand
void expand(bool fill=true)
If this array is uniform, replace it with an array of length size().
openvdb::v11_0::points::StringAttributeWriteHandle::array
AttributeArray & array()
Returns a reference to the array held in the Write Handle.
openvdb::v11_0::points::StringAttributeWriteHandle::set
void set(Index n, Index m, const Name &name)
openvdb::v11_0::points::StringAttributeWriteHandle::UniquePtr
std::unique_ptr< StringAttributeWriteHandle > UniquePtr
Definition AttributeArrayString.h:190
openvdb::v11_0::points::StringAttributeWriteHandle::set
void set(Index n, const Name &name)
Set the value of the index to name.
openvdb::v11_0::points::StringAttributeWriteHandle::create
static Ptr create(AttributeArray &array, const MetaMap &metadata, const bool expand=true)
openvdb::v11_0::points::StringAttributeWriteHandle::StringAttributeWriteHandle
StringAttributeWriteHandle(AttributeArray &array, const MetaMap &metadata, const bool expand=true)
openvdb::v11_0::points::StringMetaCache
Class to compute a string->index map from all string:N metadata.
Definition AttributeArrayString.h:57
openvdb::v11_0::points::StringMetaCache::insert
void insert(const Name &key, Index index)
Insert a new element in the cache.
openvdb::v11_0::points::StringMetaCache::UniquePtr
std::unique_ptr< StringMetaCache > UniquePtr
Definition AttributeArrayString.h:59
openvdb::v11_0::points::StringMetaCache::size
size_t size() const
Returns the number of string elements in metadata.
Definition AttributeArrayString.h:68
openvdb::v11_0::points::StringMetaCache::StringMetaCache
StringMetaCache()=default
openvdb::v11_0::points::StringMetaCache::empty
bool empty() const
Return true if no string elements in metadata.
Definition AttributeArrayString.h:66
openvdb::v11_0::points::StringMetaCache::StringMetaCache
StringMetaCache(const MetaMap &metadata)
openvdb::v11_0::points::StringMetaCache::ValueMap
std::unordered_map< Name, Index > ValueMap
Definition AttributeArrayString.h:60
openvdb::v11_0::points::StringMetaCache::reset
void reset(const MetaMap &metadata)
Clears and re-populates the cache.
openvdb::v11_0::points::StringMetaCache::map
const ValueMap & map() const
Retrieve the value map (string -> index)
Definition AttributeArrayString.h:77
openvdb::v11_0::points::StringMetaInserter::hasIndex
bool hasIndex(Index index) const
Returns true if index exists.
openvdb::v11_0::points::StringMetaInserter::resetCache
void resetCache()
Reset the cache from the metadata.
openvdb::v11_0::points::StringMetaInserter::insert
Index insert(const Name &name, Index hint=Index(0))
Insert the string into the metadata using the hint if non-zero.
openvdb::v11_0::points::StringMetaInserter::UniquePtr
std::unique_ptr< StringMetaInserter > UniquePtr
Definition AttributeArrayString.h:91
openvdb::v11_0::points::StringMetaInserter::StringMetaInserter
StringMetaInserter(MetaMap &metadata)
openvdb::v11_0::points::StringMetaInserter::hasKey
bool hasKey(const Name &key) const
Returns true if key exists.
openvdb::v11_0::points::TypedAttributeArray
Typed class for storing attribute data.
Definition AttributeArray.h:545
openvdb::v11_0::points::index
Definition IndexIterator.h:34
openvdb::v11_0::points::isString
bool isString(const AttributeArray &array)
Definition AttributeArrayString.h:144
openvdb::v11_0::points::StringAttributeArray
TypedAttributeArray< Index, StringCodec< false > > StringAttributeArray
Definition AttributeArrayString.h:49
openvdb::v11_0::Name
std::string Name
Definition Name.h:19
openvdb::v11_0::Index
Index32 Index
Definition Types.h:54
openvdb
Definition Exceptions.h:13
openvdb::v11_0::points::StringCodec::Storage
Definition AttributeArrayString.h:41
openvdb::v11_0::points::StringCodec::Storage::Type
typename attribute_traits::StringTypeTrait< Truncate >::Type Type
Definition AttributeArrayString.h:41
openvdb::v11_0::points::StringCodec
Definition AttributeArrayString.h:37
openvdb::v11_0::points::StringCodec::encode
static void encode(const ValueType &, StorageType &)
Definition AttributeArrayString.h:135
openvdb::v11_0::points::StringCodec::decode
static void decode(const StorageType &, ValueType &)
Definition AttributeArrayString.h:126
openvdb::v11_0::points::StringCodec::name
static const char * name()
Definition AttributeArrayString.h:45
openvdb::v11_0::points::StringCodec::ValueType
Index ValueType
Definition AttributeArrayString.h:38
openvdb::v11_0::points::attribute_traits::StringTypeTrait< true >::Type
uint16_t Type
Definition AttributeArrayString.h:31
openvdb::v11_0::points::attribute_traits::StringTypeTrait
Definition AttributeArrayString.h:30
openvdb::v11_0::points::attribute_traits::StringTypeTrait::Type
Index Type
Definition AttributeArrayString.h:30
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:212