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 {
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>
40 {
42 
43  template <typename T>
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 
53 
54 
56 
57 
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 
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 
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:
135 }; // class StringAttributeHandle
136 
137 
139 
140 
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 
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;
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 
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:230
std::string Name
Definition: Name.h:17
StringIndexType Type
Definition: AttributeArrayString.h:33
Attribute Array storage templated on type and compression codec.
Base class for storing attribute data.
Definition: AttributeArray.h:92
StringIndexType ValueType
Definition: AttributeArrayString.h:41
bool isType() const
Return true if this attribute is of the same type as the template parameter.
Definition: AttributeArray.h:224
bool hasConstantStride() const
Definition: AttributeArrayString.h:124
Index size() const
Definition: AttributeArrayString.h:121
Definition: AttributeArrayString.h:109
Definition: AttributeArrayString.h:141
Typed class for storing attribute data.
Definition: AttributeArray.h:566
std::shared_ptr< StringAttributeHandle > Ptr
Definition: AttributeArrayString.h:112
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:19
const MetaMap & mMetadata
Definition: AttributeArrayString.h:134
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
typename attribute_traits::StringTypeTrait< Truncate >::Type Type
Definition: AttributeArrayString.h:44
Definition: Exceptions.h:13
Write-able version of AttributeHandle.
Definition: AttributeArray.h:920
bool isString(const AttributeArray &array)
Definition: AttributeArrayString.h:100
Definition: AttributeArrayString.h:44
uint32_t StringIndexType
Definition: AttributeArrayString.h:28
static const char * name()
Definition: AttributeArrayString.h:48
uint16_t Type
Definition: AttributeArrayString.h:34
Index32 Index
Definition: Types.h:31
Definition: AttributeArrayString.h:58
Index stride() const
Definition: AttributeArrayString.h:120
Definition: AttributeArray.h:849
Definition: AttributeArrayString.h:39
AttributeHandle< StringIndexType, StringCodec< false > > mHandle
Definition: AttributeArrayString.h:133
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
bool isUniform() const
Definition: AttributeArrayString.h:123
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:817