OpenVDB  7.2.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 <deque>
16 #include <unordered_map>
17 
18 
19 namespace openvdb {
21 namespace OPENVDB_VERSION_NAME {
22 namespace points {
23 
24 
26 
27 
28 // StringIndexType is now deprecated, use Index directly
29 #ifdef _MSC_VER
30 // (deprecations on aliases are not supported on Windows)
31 using StringIndexType = Index;
32 #else
33 using StringIndexType OPENVDB_DEPRECATED = Index;
34 #endif
35 
36 namespace attribute_traits
37 {
38  template <bool Truncate> struct StringTypeTrait { using Type = Index; };
39  template<> struct StringTypeTrait</*Truncate=*/true> { using Type = uint16_t; };
40 }
41 
42 
43 template <bool Truncate>
45 {
46  using ValueType = Index;
47 
48  template <typename T>
50 
51  template<typename StorageType> static void decode(const StorageType&, ValueType&);
52  template<typename StorageType> static void encode(const ValueType&, StorageType&);
53  static const char* name() { return Truncate ? "str_trnc" : "str"; }
54 };
55 
56 
58 
59 
61 
62 
65 {
66 public:
67  using UniquePtr = std::unique_ptr<StringMetaCache>;
68  using ValueMap = std::unordered_map<Name, Index>;
69 
70  StringMetaCache() = default;
71  explicit StringMetaCache(const MetaMap& metadata);
72 
74  bool empty() const { return mCache.empty(); }
76  size_t size() const { return mCache.size(); }
77 
79  void reset(const MetaMap& metadata);
80 
82  void insert(const Name& key, Index index);
83 
85  const ValueMap& map() const { return mCache; }
86 
87 private:
88  ValueMap mCache;
89 }; // StringMetaCache
90 
91 
93 
94 
97 {
98 public:
99  using UniquePtr = std::unique_ptr<StringMetaInserter>;
100 
101  explicit StringMetaInserter(MetaMap& metadata);
102 
104  bool hasKey(const Name& key) const;
106  bool hasIndex(Index index) const;
107 
114  Index insert(const Name& name, Index hint = Index(0));
115 
117  void resetCache();
118 
119 private:
120  using IndexPairArray = std::deque<std::pair<Index, Index>>;
121 
122  MetaMap& mMetadata;
123  IndexPairArray mIdBlocks;
124  StringMetaCache mCache;
125 }; // StringMetaInserter
126 
127 
129 
130 
131 template <bool Truncate>
132 template<typename StorageType>
133 inline void
134 StringCodec<Truncate>::decode(const StorageType& data, ValueType& val)
135 {
136  val = static_cast<ValueType>(data);
137 }
138 
139 
140 template <bool Truncate>
141 template<typename StorageType>
142 inline void
143 StringCodec<Truncate>::encode(const ValueType& val, StorageType& data)
144 {
145  data = static_cast<ValueType>(val);
146 }
147 
148 
150 
151 
152 inline bool isString(const AttributeArray& array)
153 {
154  return array.isType<StringAttributeArray>();
155 }
156 
157 
159 
160 
162 {
163 public:
164  using Ptr = std::shared_ptr<StringAttributeHandle>;//SharedPtr<StringAttributeHandle>;
165  using UniquePtr = std::unique_ptr<StringAttributeHandle>;
166 
167  static Ptr create(const AttributeArray& array, const MetaMap& metadata, const bool preserveCompression = true);
168 
170  const MetaMap& metadata,
171  const bool preserveCompression = true);
172 
173  Index stride() const { return mHandle.stride(); }
174  Index size() const { return mHandle.size(); }
175 
176  bool isUniform() const { return mHandle.isUniform(); }
177  bool hasConstantStride() const { return mHandle.hasConstantStride(); }
178 
179  Name get(Index n, Index m = 0) const;
180  void get(Name& name, Index n, Index m = 0) const;
181 
183  const AttributeArray& array() const;
184 
185 protected:
188 }; // class StringAttributeHandle
189 
190 
192 
193 
195 {
196 public:
197  using Ptr = std::shared_ptr<StringAttributeWriteHandle>;//SharedPtr<StringAttributeWriteHandle>;
198  using UniquePtr = std::unique_ptr<StringAttributeWriteHandle>;
199 
200  static Ptr create(AttributeArray& array, const MetaMap& metadata, const bool expand = true);
201 
203  const MetaMap& metadata,
204  const bool expand = true);
205 
208  void expand(bool fill = true);
209 
211  void collapse();
214  void collapse(const Name& name);
215 
217  bool compact();
218 
221  void fill(const Name& name);
222 
224  void set(Index n, const Name& name);
225  void set(Index n, Index m, const Name& name);
226 
228  void resetCache();
229 
231  AttributeArray& array();
232 
235  bool contains(const Name& name) const;
236 
237 private:
240  Index getIndex(const Name& name) const;
241 
242  StringMetaCache mCache;
244 }; // class StringAttributeWriteHandle
245 
246 
248 
249 
250 } // namespace points
251 } // namespace OPENVDB_VERSION_NAME
252 } // namespace openvdb
253 
254 #endif // OPENVDB_POINTS_ATTRIBUTE_ARRAY_STRING_HAS_BEEN_INCLUDED
255 
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:221
Definition: AttributeArrayString.h:49
Definition: AttributeArrayString.h:194
Attribute Array storage templated on type and compression codec.
bool hasConstantStride() const
Definition: AttributeArrayString.h:177
std::unique_ptr< StringMetaCache > UniquePtr
Definition: AttributeArrayString.h:67
bool isType() const
Return true if this attribute is of the same type as the template parameter.
Definition: AttributeArray.h:225
Typed class for storing attribute data.
Definition: AttributeArray.h:562
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:19
Index size() const
Definition: AttributeArrayString.h:174
std::shared_ptr< StringAttributeHandle > Ptr
Definition: AttributeArrayString.h:164
const MetaMap & mMetadata
Definition: AttributeArrayString.h:187
static const char * name()
Definition: AttributeArrayString.h:53
Base class for storing attribute data.
Definition: AttributeArray.h:92
bool empty() const
Return true if no string elements in metadata.
Definition: AttributeArrayString.h:74
Index Type
Definition: AttributeArrayString.h:38
Definition: AttributeArrayString.h:161
AttributeHandle< Index, StringCodec< false > > mHandle
Definition: AttributeArrayString.h:186
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:94
Definition: openvdb/Exceptions.h:13
uint16_t Type
Definition: AttributeArrayString.h:39
Definition: AttributeArray.h:848
Class to compute a string->index map from all string:N metadata.
Definition: AttributeArrayString.h:64
bool isString(const AttributeArray &array)
Definition: AttributeArrayString.h:152
Definition: AttributeArrayString.h:44
Index stride() const
Definition: AttributeArrayString.h:173
std::unique_ptr< StringMetaInserter > UniquePtr
Definition: AttributeArrayString.h:99
std::string Name
Definition: Name.h:17
Class to help with insertion of keyed string values into metadata.
Definition: AttributeArrayString.h:96
Write-able version of AttributeHandle.
Definition: AttributeArray.h:919
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:863
std::unordered_map< Name, Index > ValueMap
Definition: AttributeArrayString.h:68
const ValueMap & map() const
Retrieve the value map (string -> index)
Definition: AttributeArrayString.h:85
std::unique_ptr< StringAttributeHandle > UniquePtr
Definition: AttributeArrayString.h:165
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:146
size_t size() const
Returns the number of string elements in metadata.
Definition: AttributeArrayString.h:76
Index32 Index
Definition: openvdb/Types.h:32
bool isUniform() const
Definition: AttributeArrayString.h:176
Index OPENVDB_DEPRECATED
Definition: AttributeArrayString.h:33
typename attribute_traits::StringTypeTrait< Truncate >::Type Type
Definition: AttributeArrayString.h:49
Index ValueType
Definition: AttributeArrayString.h:46