OpenVDB: AttributeArrayString.h Source File

OpenVDB 7.0.0

AttributeArrayString.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
9
10 #ifndef OPENVDB_POINTS_ATTRIBUTE_ARRAY_STRING_HAS_BEEN_INCLUDED
11 #define OPENVDB_POINTS_ATTRIBUTE_ARRAY_STRING_HAS_BEEN_INCLUDED
12
13 #include "AttributeArray.h"
14 #include <memory>
15 #include <unordered_set>
16 #include <unordered_map>
17
18
19 namespace openvdb {
20 OPENVDB_USE_VERSION_NAMESPACE
21 namespace OPENVDB_VERSION_NAME {
22 namespace points {
23
24
26
27
28 using StringIndexType = uint32_t;
29
30
31 namespace attribute_traits
32 {
33 template <bool Truncate> struct StringTypeTrait { using Type = StringIndexType; };
34 template<> struct StringTypeTrait</*Truncate=*/true> { using Type = uint16_t; };
35 }
36
37
38 template <bool Truncate>
39 struct StringCodec
40 {
41 using ValueType = StringIndexType;
42
43 template <typename T>
44 struct Storage { using Type = typename attribute_traits::StringTypeTrait<Truncate>::Type; };
45
46 template<typename StorageType> static void decode(const StorageType&, ValueType&);
47 template<typename StorageType> static void encode(const ValueType&, StorageType&);
48 static const char* name() { return Truncate ? "str_trnc" : "str"; }
49 };
50
51
52 using StringAttributeArray = TypedAttributeArray<StringIndexType, StringCodec<false>>;
53
54
56
57
58 class OPENVDB_API StringMetaInserter
59 {
60 public:
61 StringMetaInserter(MetaMap& metadata);
62
64 void insert(const Name& name);
65
67 void resetCache();
68
69 private:
70 MetaMap& mMetadata;
71 std::vector<std::pair<Index, Index>> mIdBlocks;
72 std::unordered_set<Name> mValues;
73 }; // StringMetaInserter
74
75
77
78
79 template <bool Truncate>
80 template<typename StorageType>
81 inline void
82 StringCodec<Truncate>::decode(const StorageType& data, ValueType& val)
83 {
84 val = static_cast<ValueType>(data);
85 }
86
87
88 template <bool Truncate>
89 template<typename StorageType>
90 inline void
91 StringCodec<Truncate>::encode(const ValueType& val, StorageType& data)
92 {
93 data = static_cast<ValueType>(val);
94 }
95
96
98
99
100 inline bool isString(const AttributeArray& array)
101 {
102 return array.isType<StringAttributeArray>();
103 }
104
105
107
108
109 class OPENVDB_API StringAttributeHandle
110 {
111 public:
112 using Ptr = std::shared_ptr<StringAttributeHandle>;//SharedPtr<StringAttributeHandle>;
113
114 static Ptr create(const AttributeArray& array, const MetaMap& metadata, const bool preserveCompression = true);
115
116 StringAttributeHandle( const AttributeArray& array,
117 const MetaMap& metadata,
118 const bool preserveCompression = true);
119
120 Index stride() const { return mHandle.stride(); }
121 Index size() const { return mHandle.size(); }
122
123 bool isUniform() const { return mHandle.isUniform(); }
124 bool hasConstantStride() const { return mHandle.hasConstantStride(); }
125
126 Name get(Index n, Index m = 0) const;
127 void get(Name& name, Index n, Index m = 0) const;
128
130 const AttributeArray& array() const;
131
132 protected:
133 AttributeHandle<StringIndexType, StringCodec<false>> mHandle;
134 const MetaMap& mMetadata;
135 }; // class StringAttributeHandle
136
137
139
140
141 class OPENVDB_API StringAttributeWriteHandle : public StringAttributeHandle
142 {
143 public:
144 using Ptr = std::shared_ptr<StringAttributeWriteHandle>;//SharedPtr<StringAttributeWriteHandle>;
145
146 static Ptr create(AttributeArray& array, const MetaMap& metadata, const bool expand = true);
147
148 StringAttributeWriteHandle( AttributeArray& array,
149 const MetaMap& metadata,
150 const bool expand = true);
151
154 void expand(bool fill = true);
155
157 void collapse();
160 void collapse(const Name& name);
161
163 bool compact();
164
167 void fill(const Name& name);
168
170 void set(Index n, const Name& name);
171 void set(Index n, Index m, const Name& name);
172
174 void resetCache();
175
177 AttributeArray& array();
178
181 bool contains(const Name& name) const;
182
183 private:
186 Index getIndex(const Name& name) const;
187
188 using ValueMap = std::unordered_map<std::string, Index>;
189
190 ValueMap mCache;
191 AttributeWriteHandle<StringIndexType, StringCodec<false>> mWriteHandle;
192 }; // class StringAttributeWriteHandle
193
194
196
197
198 } // namespace points
199 } // namespace OPENVDB_VERSION_NAME
200 } // namespace openvdb
201
202 #endif // OPENVDB_POINTS_ATTRIBUTE_ARRAY_STRING_HAS_BEEN_INCLUDED
203
openvdb::v7_0::points::StringAttributeHandle::mHandle
AttributeHandle< StringIndexType, StringCodec< false > > mHandle
Definition: AttributeArrayString.h:133
openvdb::v7_0::points::StringAttributeHandle::stride
Index stride() const
Definition: AttributeArrayString.h:120
openvdb::v7_0::points::StringAttributeHandle
Definition: AttributeArrayString.h:109
openvdb::v7_0::points::AttributeArray
Base class for storing attribute data.
Definition: AttributeArray.h:92
openvdb::v7_0::points::StringCodec
Definition: AttributeArrayString.h:39
openvdb::v7_0::points::StringAttributeHandle::Ptr
std::shared_ptr< StringAttributeHandle > Ptr
Definition: AttributeArrayString.h:112
openvdb::v7_0::points::StringIndexType
uint32_t StringIndexType
Definition: AttributeArrayString.h:28
openvdb::v7_0::points::attribute_traits::StringTypeTrait< true >::Type
uint16_t Type
Definition: AttributeArrayString.h:34
openvdb::v7_0::points::StringCodec::Storage::Type
typename attribute_traits::StringTypeTrait< Truncate >::Type Type
Definition: AttributeArrayString.h:44
AttributeArray.h
Attribute Array storage templated on type and compression codec.
openvdb::v7_0::points::StringMetaInserter
Definition: AttributeArrayString.h:58
OPENVDB_API
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:230
openvdb::v7_0::points::attribute_traits::StringTypeTrait::Type
StringIndexType Type
Definition: AttributeArrayString.h:33
openvdb::v7_0::MetaMap
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:19
openvdb::v7_0::points::StringAttributeWriteHandle
Definition: AttributeArrayString.h:141
openvdb::v7_0::points::AttributeWriteHandle
Write-able version of AttributeHandle.
Definition: AttributeArray.h:920
openvdb::v7_0::points::StringAttributeHandle::isUniform
bool isUniform() const
Definition: AttributeArrayString.h:123
openvdb::v7_0::points::TypedAttributeArray
Typed class for storing attribute data.
Definition: AttributeArray.h:566
openvdb::v7_0::points::attribute_traits::StringTypeTrait
Definition: AttributeArrayString.h:33
openvdb::v7_0::points::StringCodec::ValueType
StringIndexType ValueType
Definition: AttributeArrayString.h:41
openvdb::v7_0::points::StringAttributeHandle::size
Index size() const
Definition: AttributeArrayString.h:121
openvdb::v7_0::points::StringAttributeHandle::hasConstantStride
bool hasConstantStride() const
Definition: AttributeArrayString.h:124
openvdb::v7_0::points::AttributeHandle
Definition: AttributeArray.h:849
openvdb::v7_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_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
openvdb::v7_0::points::StringCodec::name
static const char * name()
Definition: AttributeArrayString.h:48
openvdb::v7_0::points::StringAttributeHandle::mMetadata
const MetaMap & mMetadata
Definition: AttributeArrayString.h:134
openvdb::v7_0::Index
Index32 Index
Definition: Types.h:31
openvdb::v7_0::Name
std::string Name
Definition: Name.h:17
openvdb::v7_0::points::StringCodec::Storage
Definition: AttributeArrayString.h:44
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:108
openvdb
Definition: Exceptions.h:13
openvdb::v7_0::points::isString
bool isString(const AttributeArray &array)
Definition: AttributeArrayString.h:100
openvdb::v7_0::math::Truncate
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:817