OpenVDB  8.0.1
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 namespace attribute_traits
29 {
30  template <bool Truncate> struct StringTypeTrait { using Type = Index; };
31  template<> struct StringTypeTrait</*Truncate=*/true> { using Type = uint16_t; };
32 }
33 
34 
35 template <bool Truncate>
37 {
38  using ValueType = Index;
39 
40  template <typename T>
42 
43  template<typename StorageType> static void decode(const StorageType&, ValueType&);
44  template<typename StorageType> static void encode(const ValueType&, StorageType&);
45  static const char* name() { return Truncate ? "str_trnc" : "str"; }
46 };
47 
48 
50 
51 
53 
54 
57 {
58 public:
59  using UniquePtr = std::unique_ptr<StringMetaCache>;
60  using ValueMap = std::unordered_map<Name, Index>;
61 
62  StringMetaCache() = default;
63  explicit StringMetaCache(const MetaMap& metadata);
64 
66  bool empty() const { return mCache.empty(); }
68  size_t size() const { return mCache.size(); }
69 
71  void reset(const MetaMap& metadata);
72 
74  void insert(const Name& key, Index index);
75 
77  const ValueMap& map() const { return mCache; }
78 
79 private:
80  ValueMap mCache;
81 }; // StringMetaCache
82 
83 
85 
86 
89 {
90 public:
91  using UniquePtr = std::unique_ptr<StringMetaInserter>;
92 
93  explicit StringMetaInserter(MetaMap& metadata);
94 
96  bool hasKey(const Name& key) const;
98  bool hasIndex(Index index) const;
99 
106  Index insert(const Name& name, Index hint = Index(0));
107 
109  void resetCache();
110 
111 private:
112  using IndexPairArray = std::deque<std::pair<Index, Index>>;
113 
114  MetaMap& mMetadata;
115  IndexPairArray mIdBlocks;
116  StringMetaCache mCache;
117 }; // StringMetaInserter
118 
119 
121 
122 
123 template <bool Truncate>
124 template<typename StorageType>
125 inline void
126 StringCodec<Truncate>::decode(const StorageType& data, ValueType& val)
127 {
128  val = static_cast<ValueType>(data);
129 }
130 
131 
132 template <bool Truncate>
133 template<typename StorageType>
134 inline void
135 StringCodec<Truncate>::encode(const ValueType& val, StorageType& data)
136 {
137  data = static_cast<ValueType>(val);
138 }
139 
140 
142 
143 
144 inline bool isString(const AttributeArray& array)
145 {
146  return array.isType<StringAttributeArray>();
147 }
148 
149 
151 
152 
154 {
155 public:
156  using Ptr = std::shared_ptr<StringAttributeHandle>;//SharedPtr<StringAttributeHandle>;
157  using UniquePtr = std::unique_ptr<StringAttributeHandle>;
158 
159  static Ptr create(const AttributeArray& array, const MetaMap& metadata, const bool preserveCompression = true);
160 
162  const MetaMap& metadata,
163  const bool preserveCompression = true);
164 
165  Index stride() const { return mHandle.stride(); }
166  Index size() const { return mHandle.size(); }
167 
168  bool isUniform() const { return mHandle.isUniform(); }
169  bool hasConstantStride() const { return mHandle.hasConstantStride(); }
170 
171  Name get(Index n, Index m = 0) const;
172  void get(Name& name, Index n, Index m = 0) const;
173 
175  const AttributeArray& array() const;
176 
177 protected:
180 }; // class StringAttributeHandle
181 
182 
184 
185 
187 {
188 public:
189  using Ptr = std::shared_ptr<StringAttributeWriteHandle>;//SharedPtr<StringAttributeWriteHandle>;
190  using UniquePtr = std::unique_ptr<StringAttributeWriteHandle>;
191 
192  static Ptr create(AttributeArray& array, const MetaMap& metadata, const bool expand = true);
193 
195  const MetaMap& metadata,
196  const bool expand = true);
197 
200  void expand(bool fill = true);
201 
203  void collapse();
206  void collapse(const Name& name);
207 
209  bool compact();
210 
213  void fill(const Name& name);
214 
216  void set(Index n, const Name& name);
217  void set(Index n, Index m, const Name& name);
218 
220  void resetCache();
221 
224 
227  bool contains(const Name& name) const;
228 
229 private:
232  Index getIndex(const Name& name) const;
233 
234  StringMetaCache mCache;
236 }; // class StringAttributeWriteHandle
237 
238 
240 
241 
242 } // namespace points
243 } // namespace OPENVDB_VERSION_NAME
244 } // namespace openvdb
245 
246 #endif // OPENVDB_POINTS_ATTRIBUTE_ARRAY_STRING_HAS_BEEN_INCLUDED
247 
openvdb::v8_0::Index
Index32 Index
Definition: openvdb/Types.h:32
openvdb::v8_0::points::attribute_traits::StringTypeTrait
Definition: AttributeArrayString.h:30
openvdb::v8_0::points::StringAttributeWriteHandle
Definition: AttributeArrayString.h:187
openvdb::v8_0::points::StringAttributeWriteHandle::StringAttributeWriteHandle
StringAttributeWriteHandle(AttributeArray &array, const MetaMap &metadata, const bool expand=true)
openvdb::v8_0::points::StringAttributeHandle
Definition: AttributeArrayString.h:154
openvdb::v8_0::points::StringAttributeWriteHandle::create
static Ptr create(AttributeArray &array, const MetaMap &metadata, const bool expand=true)
openvdb::v8_0::points::StringAttributeHandle::StringAttributeHandle
StringAttributeHandle(const AttributeArray &array, const MetaMap &metadata, const bool preserveCompression=true)
openvdb::v8_0::points::StringAttributeWriteHandle::fill
void fill(const Name &name)
Fill the existing array with the given value.
openvdb::v8_0::points::StringMetaInserter
Class to help with insertion of keyed string values into metadata.
Definition: AttributeArrayString.h:89
openvdb::v8_0::points::AttributeWriteHandle
Write-able version of AttributeHandle.
Definition: AttributeArray.h:915
openvdb::v8_0::points::attribute_traits::StringTypeTrait< true >::Type
uint16_t Type
Definition: AttributeArrayString.h:31
openvdb::v8_0::points::StringAttributeHandle::hasConstantStride
bool hasConstantStride() const
Definition: AttributeArrayString.h:169
openvdb::v8_0::points::isString
bool isString(const AttributeArray &array)
Definition: AttributeArrayString.h:144
openvdb::v8_0::points::StringAttributeHandle::get
Name get(Index n, Index m=0) const
AttributeArray.h
Attribute Array storage templated on type and compression codec.
openvdb::v8_0::points::StringMetaCache::map
const ValueMap & map() const
Retrieve the value map (string -> index)
Definition: AttributeArrayString.h:77
openvdb::v8_0::points::AttributeArray::isType
bool isType() const
Return true if this attribute is of the same type as the template parameter.
Definition: AttributeArray.h:221
openvdb::v8_0::points::StringCodec
Definition: AttributeArrayString.h:37
openvdb::v8_0::points::StringAttributeHandle::create
static Ptr create(const AttributeArray &array, const MetaMap &metadata, const bool preserveCompression=true)
OPENVDB_API
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:208
openvdb::v8_0::points::StringAttributeWriteHandle::array
AttributeArray & array()
Returns a reference to the array held in the Write Handle.
openvdb::v8_0::Name
std::string Name
Definition: Name.h:17
openvdb::v8_0::points::StringAttributeHandle::UniquePtr
std::unique_ptr< StringAttributeHandle > UniquePtr
Definition: AttributeArrayString.h:157
openvdb::v8_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::v8_0::MetaMap
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:20
openvdb::v8_0::points::StringCodec::Storage::Type
typename attribute_traits::StringTypeTrait< Truncate >::Type Type
Definition: AttributeArrayString.h:41
openvdb::v8_0::points::TypedAttributeArray
Typed class for storing attribute data.
Definition: AttributeArray.h:559
openvdb::v8_0::points::StringAttributeHandle::mMetadata
const MetaMap & mMetadata
Definition: AttributeArrayString.h:179
openvdb::v8_0::points::StringMetaCache::size
size_t size() const
Returns the number of string elements in metadata.
Definition: AttributeArrayString.h:68
openvdb::v8_0::points::StringAttributeHandle::stride
Index stride() const
Definition: AttributeArrayString.h:165
openvdb::v8_0::points::AttributeHandle
Definition: AttributeArray.h:844
openvdb::v8_0::points::StringAttributeHandle::get
void get(Name &name, Index n, Index m=0) const
openvdb::v8_0::points::StringAttributeHandle::isUniform
bool isUniform() const
Definition: AttributeArrayString.h:168
openvdb::v8_0::math::Truncate
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:873
openvdb::v8_0::points::StringCodec::ValueType
Index ValueType
Definition: AttributeArrayString.h:38
openvdb::v8_0::points::StringAttributeHandle::Ptr
std::shared_ptr< StringAttributeHandle > Ptr
Definition: AttributeArrayString.h:156
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:153
openvdb::v8_0::points::StringMetaCache::insert
void insert(const Name &key, Index index)
Insert a new element in the cache.
openvdb::v8_0::points::StringMetaCache
Class to compute a string->index map from all string:N metadata.
Definition: AttributeArrayString.h:57
openvdb::v8_0::points::StringMetaCache::ValueMap
std::unordered_map< Name, Index > ValueMap
Definition: AttributeArrayString.h:60
openvdb::v8_0::points::StringCodec::name
static const char * name()
Definition: AttributeArrayString.h:45
openvdb::v8_0::points::StringAttributeWriteHandle::set
void set(Index n, Index m, const Name &name)
openvdb::v8_0::points::StringMetaInserter::resetCache
void resetCache()
Reset the cache from the metadata.
openvdb::v8_0::points::StringAttributeWriteHandle::resetCache
void resetCache()
Reset the value cache from the metadata.
openvdb::v8_0::points::StringMetaInserter::StringMetaInserter
StringMetaInserter(MetaMap &metadata)
openvdb::v8_0::points::StringMetaCache::empty
bool empty() const
Return true if no string elements in metadata.
Definition: AttributeArrayString.h:66
openvdb::v8_0::points::StringAttributeWriteHandle::set
void set(Index n, const Name &name)
Set the value of the index to name.
openvdb::v8_0::points::StringAttributeWriteHandle::collapse
void collapse()
Set membership for the whole array and attempt to collapse.
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:101
openvdb::v8_0::points::StringMetaCache::reset
void reset(const MetaMap &metadata)
Clears and re-populates the cache.
openvdb::v8_0::points::StringMetaInserter::hasIndex
bool hasIndex(Index index) const
Returns true if index exists.
openvdb::v8_0::points::StringAttributeHandle::size
Index size() const
Definition: AttributeArrayString.h:166
openvdb::v8_0::points::attribute_traits::StringTypeTrait::Type
Index Type
Definition: AttributeArrayString.h:30
openvdb::v8_0::points::StringMetaCache::StringMetaCache
StringMetaCache(const MetaMap &metadata)
openvdb::v8_0::points::StringCodec::Storage
Definition: AttributeArrayString.h:41
openvdb::v8_0::points::AttributeArray
Base class for storing attribute data.
Definition: AttributeArray.h:93
openvdb::v8_0::points::StringAttributeWriteHandle::contains
bool contains(const Name &name) const
Returns whether or not the metadata cache contains a given value.
openvdb::v8_0::points::StringAttributeHandle::array
const AttributeArray & array() const
Returns a reference to the array held in the Handle.
openvdb::v8_0::points::StringMetaInserter::hasKey
bool hasKey(const Name &key) const
Returns true if key exists.
openvdb
Definition: openvdb/Exceptions.h:13
openvdb::v8_0::points::StringAttributeWriteHandle::collapse
void collapse(const Name &name)
Set membership for the whole array and attempt to collapse.
openvdb::v8_0::points::StringMetaInserter::UniquePtr
std::unique_ptr< StringMetaInserter > UniquePtr
Definition: AttributeArrayString.h:91
openvdb::v8_0::points::StringAttributeWriteHandle::compact
bool compact()
Compact the existing array to become uniform if all values are identical.
openvdb::v8_0::points::StringAttributeHandle::mHandle
AttributeHandle< Index, StringCodec< false > > mHandle
Definition: AttributeArrayString.h:178
openvdb::v8_0::points::StringAttributeWriteHandle::expand
void expand(bool fill=true)
If this array is uniform, replace it with an array of length size().
openvdb::v8_0::points::StringMetaCache::StringMetaCache
StringMetaCache()=default
openvdb::v8_0::points::StringMetaCache::UniquePtr
std::unique_ptr< StringMetaCache > UniquePtr
Definition: AttributeArrayString.h:59