OpenVDB  7.1.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 
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 
openvdb::v7_1::points::StringMetaCache::map
const ValueMap & map() const
Retrieve the value map (string -> index)
Definition: AttributeArrayString.h:85
openvdb::v7_1::MetaMap
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:20
openvdb::v7_1::points::StringCodec::Storage
Definition: AttributeArrayString.h:49
openvdb::v7_1::points::StringAttributeWriteHandle::set
void set(Index n, const Name &name)
Set the value of the index to name.
openvdb::v7_1::points::StringMetaCache::UniquePtr
std::unique_ptr< StringMetaCache > UniquePtr
Definition: AttributeArrayString.h:67
openvdb::v7_1::points::StringAttributeHandle::stride
Index stride() const
Definition: AttributeArrayString.h:173
openvdb::v7_1::points::StringAttributeWriteHandle::resetCache
void resetCache()
Reset the value cache from the metadata.
openvdb::v7_1::points::AttributeHandle
Definition: AttributeArray.h:849
openvdb::v7_1::points::StringAttributeWriteHandle
Definition: AttributeArrayString.h:195
openvdb::v7_1::points::StringMetaCache
Class to compute a string->index map from all string:N metadata.
Definition: AttributeArrayString.h:65
openvdb::v7_1::points::StringAttributeHandle::Ptr
std::shared_ptr< StringAttributeHandle > Ptr
Definition: AttributeArrayString.h:164
openvdb::v7_1::points::StringCodec::name
static const char * name()
Definition: AttributeArrayString.h:53
openvdb::v7_1::points::StringAttributeWriteHandle::array
AttributeArray & array()
Returns a reference to the array held in the Write Handle.
openvdb::v7_1::points::StringMetaInserter::resetCache
void resetCache()
Reset the cache from the metadata.
openvdb::v7_1::points::StringAttributeWriteHandle::collapse
void collapse()
Set membership for the whole array and attempt to collapse.
openvdb::v7_1::Index
Index32 Index
Definition: Types.h:31
openvdb::v7_1::points::StringMetaCache::StringMetaCache
StringMetaCache(const MetaMap &metadata)
openvdb::v7_1::points::OPENVDB_DEPRECATED
Index OPENVDB_DEPRECATED
Definition: AttributeArrayString.h:33
openvdb::v7_1::Name
std::string Name
Definition: Name.h:17
openvdb::v7_1::points::StringMetaInserter::StringMetaInserter
StringMetaInserter(MetaMap &metadata)
openvdb::v7_1::points::attribute_traits::StringTypeTrait::Type
Index Type
Definition: AttributeArrayString.h:38
openvdb::v7_1::points::isString
bool isString(const AttributeArray &array)
Definition: AttributeArrayString.h:152
openvdb::v7_1::points::StringAttributeHandle::UniquePtr
std::unique_ptr< StringAttributeHandle > UniquePtr
Definition: AttributeArrayString.h:165
openvdb::v7_1::points::attribute_traits::StringTypeTrait< true >::Type
uint16_t Type
Definition: AttributeArrayString.h:39
openvdb::v7_1::points::AttributeWriteHandle
Write-able version of AttributeHandle.
Definition: AttributeArray.h:920
openvdb::v7_1::points::StringAttributeWriteHandle::collapse
void collapse(const Name &name)
Set membership for the whole array and attempt to collapse.
openvdb::v7_1::points::StringCodec::ValueType
Index ValueType
Definition: AttributeArrayString.h:46
AttributeArray.h
Attribute Array storage templated on type and compression codec.
openvdb::v7_1::points::StringMetaInserter::hasIndex
bool hasIndex(Index index) const
Returns true if index exists.
openvdb::v7_1::points::StringAttributeHandle::mHandle
AttributeHandle< Index, StringCodec< false > > mHandle
Definition: AttributeArrayString.h:186
openvdb::v7_1::points::StringMetaInserter
Class to help with insertion of keyed string values into metadata.
Definition: AttributeArrayString.h:97
OPENVDB_API
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:230
openvdb::v7_1::points::StringAttributeHandle::array
const AttributeArray & array() const
Returns a reference to the array held in the Handle.
openvdb::v7_1::points::StringAttributeWriteHandle::set
void set(Index n, Index m, const Name &name)
openvdb::v7_1::points::StringMetaInserter::UniquePtr
std::unique_ptr< StringMetaInserter > UniquePtr
Definition: AttributeArrayString.h:99
openvdb::v7_1::points::StringAttributeWriteHandle::contains
bool contains(const Name &name) const
Returns whether or not the metadata cache contains a given value.
openvdb::v7_1::points::StringMetaInserter::hasKey
bool hasKey(const Name &key) const
Returns true if key exists.
openvdb::v7_1::points::StringCodec::Storage::Type
typename attribute_traits::StringTypeTrait< Truncate >::Type Type
Definition: AttributeArrayString.h:49
openvdb::v7_1::points::TypedAttributeArray
Typed class for storing attribute data.
Definition: AttributeArray.h:566
openvdb::v7_1::points::StringMetaCache::insert
void insert(const Name &key, Index index)
Insert a new element in the cache.
openvdb::v7_1::points::attribute_traits::StringTypeTrait
Definition: AttributeArrayString.h:38
openvdb::v7_1::points::StringAttributeHandle::isUniform
bool isUniform() const
Definition: AttributeArrayString.h:176
openvdb::v7_1::points::StringAttributeHandle::size
Index size() const
Definition: AttributeArrayString.h:174
openvdb::v7_1::points::StringMetaCache::size
size_t size() const
Returns the number of string elements in metadata.
Definition: AttributeArrayString.h:76
openvdb::v7_1::points::StringMetaCache::StringMetaCache
StringMetaCache()=default
openvdb::v7_1::points::StringCodec
Definition: AttributeArrayString.h:45
openvdb::v7_1::points::StringMetaCache::ValueMap
std::unordered_map< Name, Index > ValueMap
Definition: AttributeArrayString.h:68
openvdb::v7_1::points::StringAttributeHandle::hasConstantStride
bool hasConstantStride() const
Definition: AttributeArrayString.h:177
openvdb::v7_1::points::AttributeArray::isType
bool isType() const
Return true if this attribute is of the same type as the template parameter.
Definition: AttributeArray.h:225
openvdb::v7_1::points::StringAttributeHandle::StringAttributeHandle
StringAttributeHandle(const AttributeArray &array, const MetaMap &metadata, const bool preserveCompression=true)
openvdb::v7_1::points::StringAttributeHandle
Definition: AttributeArrayString.h:162
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:146
openvdb::v7_1::points::StringAttributeWriteHandle::expand
void expand(bool fill=true)
If this array is uniform, replace it with an array of length size().
openvdb::v7_1::points::AttributeArray
Base class for storing attribute data.
Definition: AttributeArray.h:93
openvdb::v7_1::points::StringAttributeWriteHandle::compact
bool compact()
Compact the existing array to become uniform if all values are identical.
openvdb::v7_1::points::StringAttributeHandle::create
static Ptr create(const AttributeArray &array, const MetaMap &metadata, const bool preserveCompression=true)
openvdb::v7_1::points::StringAttributeHandle::mMetadata
const MetaMap & mMetadata
Definition: AttributeArrayString.h:187
openvdb::v7_1::math::Truncate
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:837
openvdb::v7_1::points::StringAttributeHandle::get
Name get(Index n, Index m=0) const
openvdb::v7_1::points::StringAttributeWriteHandle::StringAttributeWriteHandle
StringAttributeWriteHandle(AttributeArray &array, const MetaMap &metadata, const bool expand=true)
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:94
openvdb::v7_1::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::v7_1::points::StringMetaCache::empty
bool empty() const
Return true if no string elements in metadata.
Definition: AttributeArrayString.h:74
openvdb::v7_1::points::StringAttributeWriteHandle::fill
void fill(const Name &name)
Fill the existing array with the given value.
openvdb::v7_1::points::StringAttributeWriteHandle::create
static Ptr create(AttributeArray &array, const MetaMap &metadata, const bool expand=true)
openvdb
Definition: Exceptions.h:13
openvdb::v7_1::points::StringMetaCache::reset
void reset(const MetaMap &metadata)
Clears and re-populates the cache.
openvdb::v7_1::points::StringAttributeHandle::get
void get(Name &name, Index n, Index m=0) const