HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AttributeArrayString.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 /// @file points/AttributeArrayString.h
5 ///
6 /// @author Dan Bailey
7 ///
8 /// @brief Attribute array storage for string data using Descriptor Metadata.
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 
25 ////////////////////////////////////////
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 
60 ////////////////////////////////////////
61 
62 
63 /// Class to compute a string->index map from all string:N metadata
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 
73  /// Return @c true if no string elements in metadata
74  bool empty() const { return mCache.empty(); }
75  /// Returns the number of string elements in metadata
76  size_t size() const { return mCache.size(); }
77 
78  /// Clears and re-populates the cache
79  void reset(const MetaMap& metadata);
80 
81  /// Insert a new element in the cache
82  void insert(const Name& key, Index index);
83 
84  /// Retrieve the value map (string -> index)
85  const ValueMap& map() const { return mCache; }
86 
87 private:
88  ValueMap mCache;
89 }; // StringMetaCache
90 
91 
92 ////////////////////////////////////////
93 
94 
95 /// Class to help with insertion of keyed string values into metadata
97 {
98 public:
99  using UniquePtr = std::unique_ptr<StringMetaInserter>;
100 
101  explicit StringMetaInserter(MetaMap& metadata);
102 
103  /// Returns @c true if key exists
104  bool hasKey(const Name& key) const;
105  /// Returns @c true if index exists
106  bool hasIndex(Index index) const;
107 
108  /// @brief Insert the string into the metadata using the hint if non-zero
109  /// @param name the string to insert
110  /// @param hint requested index to use if non-zero and not already in use
111  /// @note the hint can be used to insert non-sequentially so as to avoid an
112  /// expensive re-indexing of string keys
113  /// @return the chosen index which will match hint if the hint was used
114  Index insert(const Name& name, Index hint = Index(0));
115 
116  /// Reset the cache from the metadata
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 
128 ////////////////////////////////////////
129 
130 
131 template <bool Truncate>
132 template<typename StorageType>
133 inline void
135 {
136  val = static_cast<ValueType>(data);
137 }
138 
139 
140 template <bool Truncate>
141 template<typename StorageType>
142 inline void
144 {
145  data = static_cast<ValueType>(val);
146 }
147 
148 
149 ////////////////////////////////////////
150 
151 
152 inline bool isString(const AttributeArray& array)
153 {
154  return array.isType<StringAttributeArray>();
155 }
156 
157 
158 ////////////////////////////////////////
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 
182  /// @brief Returns a reference to the array held in the Handle.
183  const AttributeArray& array() const;
184 
185 protected:
188 }; // class StringAttributeHandle
189 
190 
191 ////////////////////////////////////////
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 
206  /// @brief If this array is uniform, replace it with an array of length size().
207  /// @param fill if true, assign the uniform value to each element of the array.
208  void expand(bool fill = true);
209 
210  /// @brief Set membership for the whole array and attempt to collapse
211  void collapse();
212  /// @brief Set membership for the whole array and attempt to collapse
213  /// @param name Name of the String
214  void collapse(const Name& name);
215 
216  /// Compact the existing array to become uniform if all values are identical
217  bool compact();
218 
219  /// @brief Fill the existing array with the given value.
220  /// @note Identical to collapse() except a non-uniform array will not become uniform.
221  void fill(const Name& name);
222 
223  /// Set the value of the index to @a name
224  void set(Index n, const Name& name);
225  void set(Index n, Index m, const Name& name);
226 
227  /// Reset the value cache from the metadata
228  void resetCache();
229 
230  /// @brief Returns a reference to the array held in the Write Handle.
232 
233  /// @brief Returns whether or not the metadata cache contains a given value.
234  /// @param name Name of the String.
235  bool contains(const Name& name) const;
236 
237 private:
238  /// Retrieve the index of this string value from the cache
239  /// @note throws if name does not exist in cache
240  Index getIndex(const Name& name) const;
241 
242  StringMetaCache mCache;
244 }; // class StringAttributeWriteHandle
245 
246 
247 ////////////////////////////////////////
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 
bool isString(const AttributeArray &array)
const ValueMap & map() const
Retrieve the value map (string -> index)
vbool4 insert(const vbool4 &a, bool val)
Helper: substitute val for a[i].
Definition: simd.h:3340
AttributeHandle< Index, StringCodec< false > > mHandle
Definition: ImfName.h:54
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:837
GLuint const GLchar * name
Definition: glew.h:1814
bool empty() const
Return true if no string elements in metadata.
GLuint index
Definition: glew.h:1814
ImageBuf OIIO_API fill(cspan< float > values, ROI roi, int nthreads=0)
GLuint const GLfloat * val
Definition: glew.h:2794
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:166
const GLdouble * m
Definition: glew.h:9124
typename attribute_traits::StringTypeTrait< Truncate >::Type Type
GLboolean reset
Definition: glew.h:4959
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:19
static void decode(const StorageType &, ValueType &)
Base class for storing attribute data.
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:230
Class to compute a string->index map from all string:N metadata.
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
Class to help with insertion of keyed string values into metadata.
GLsizei n
Definition: glew.h:4040
bool OIIO_API contains(string_view a, string_view b)
Does 'a' contain the string 'b' within it?
Typed class for storing attribute data.
GLuint GLdouble GLdouble GLint GLint const GLdouble * points
Definition: glew.h:3446
static void encode(const ValueType &, StorageType &)
GLenum array
Definition: glew.h:9066
size_t size() const
Returns the number of string elements in metadata.
Attribute Array storage templated on type and compression codec.
bool isType() const
Return true if this attribute is of the same type as the template parameter.
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:112