OpenVDB: AttributeArrayString.h Source File

OpenVDB 6.1.0

AttributeArrayString.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2018 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30
36
37 #ifndef OPENVDB_POINTS_ATTRIBUTE_ARRAY_STRING_HAS_BEEN_INCLUDED
38 #define OPENVDB_POINTS_ATTRIBUTE_ARRAY_STRING_HAS_BEEN_INCLUDED
39
40 #include "AttributeArray.h"
41 #include <memory>
42 #include <unordered_set>
43 #include <unordered_map>
44
45
46 namespace openvdb {
47 OPENVDB_USE_VERSION_NAMESPACE
48 namespace OPENVDB_VERSION_NAME {
49 namespace points {
50
51
53
54
55 using StringIndexType = uint32_t;
56
57
58 namespace attribute_traits
59 {
60 template <bool Truncate> struct StringTypeTrait { using Type = StringIndexType; };
61 template<> struct StringTypeTrait</*Truncate=*/true> { using Type = uint16_t; };
62 }
63
64
65 template <bool Truncate>
66 struct StringCodec
67 {
68 using ValueType = StringIndexType;
69
70 template <typename T>
71 struct Storage { using Type = typename attribute_traits::StringTypeTrait<Truncate>::Type; };
72
73 template<typename StorageType> static void decode(const StorageType&, ValueType&);
74 template<typename StorageType> static void encode(const ValueType&, StorageType&);
75 static const char* name() { return Truncate ? "str_trnc" : "str"; }
76 };
77
78
79 using StringAttributeArray = TypedAttributeArray<StringIndexType, StringCodec<false>>;
80
81
83
84
85 class OPENVDB_API StringMetaInserter
86 {
87 public:
88 StringMetaInserter(MetaMap& metadata);
89
91 void insert(const Name& name);
92
94 void resetCache();
95
96 private:
97 MetaMap& mMetadata;
98 std::vector<std::pair<Index, Index>> mIdBlocks;
99 std::unordered_set<Name> mValues;
100 }; // StringMetaInserter
101
102
104
105
106 template <bool Truncate>
107 template<typename StorageType>
108 inline void
109 StringCodec<Truncate>::decode(const StorageType& data, ValueType& val)
110 {
111 val = static_cast<ValueType>(data);
112 }
113
114
115 template <bool Truncate>
116 template<typename StorageType>
117 inline void
118 StringCodec<Truncate>::encode(const ValueType& val, StorageType& data)
119 {
120 data = static_cast<ValueType>(val);
121 }
122
123
125
126
127 inline bool isString(const AttributeArray& array)
128 {
129 return array.isType<StringAttributeArray>();
130 }
131
132
134
135
136 class OPENVDB_API StringAttributeHandle
137 {
138 public:
139 using Ptr = std::shared_ptr<StringAttributeHandle>;//SharedPtr<StringAttributeHandle>;
140
141 static Ptr create(const AttributeArray& array, const MetaMap& metadata, const bool preserveCompression = true);
142
143 StringAttributeHandle( const AttributeArray& array,
144 const MetaMap& metadata,
145 const bool preserveCompression = true);
146
147 Index stride() const { return mHandle.stride(); }
148 Index size() const { return mHandle.size(); }
149
150 bool isUniform() const { return mHandle.isUniform(); }
151 bool hasConstantStride() const { return mHandle.hasConstantStride(); }
152
153 Name get(Index n, Index m = 0) const;
154 void get(Name& name, Index n, Index m = 0) const;
155
157 const AttributeArray& array() const;
158
159 protected:
160 AttributeHandle<StringIndexType, StringCodec<false>> mHandle;
161 const MetaMap& mMetadata;
162 }; // class StringAttributeHandle
163
164
166
167
168 class OPENVDB_API StringAttributeWriteHandle : public StringAttributeHandle
169 {
170 public:
171 using Ptr = std::shared_ptr<StringAttributeWriteHandle>;//SharedPtr<StringAttributeWriteHandle>;
172
173 static Ptr create(AttributeArray& array, const MetaMap& metadata, const bool expand = true);
174
175 StringAttributeWriteHandle( AttributeArray& array,
176 const MetaMap& metadata,
177 const bool expand = true);
178
181 void expand(bool fill = true);
182
184 void collapse();
187 void collapse(const Name& name);
188
190 bool compact();
191
194 void fill(const Name& name);
195
197 void set(Index n, const Name& name);
198 void set(Index n, Index m, const Name& name);
199
201 void resetCache();
202
204 AttributeArray& array();
205
208 bool contains(const Name& name) const;
209
210 private:
213 Index getIndex(const Name& name) const;
214
215 using ValueMap = std::unordered_map<std::string, Index>;
216
217 ValueMap mCache;
218 AttributeWriteHandle<StringIndexType, StringCodec<false>> mWriteHandle;
219 }; // class StringAttributeWriteHandle
220
221
223
224
225 } // namespace points
226 } // namespace OPENVDB_VERSION_NAME
227 } // namespace openvdb
228
229 #endif // OPENVDB_POINTS_ATTRIBUTE_ARRAY_STRING_HAS_BEEN_INCLUDED
230
231 // Copyright (c) 2012-2018 DreamWorks Animation LLC
232 // All rights reserved. This software is distributed under the
233 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
234
OPENVDB_API
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:288
openvdb::v6_1::points::attribute_traits::StringTypeTrait< true >::Type
uint16_t Type
Definition: AttributeArrayString.h:61
openvdb::v6_1::points::StringAttributeHandle::mHandle
AttributeHandle< StringIndexType, StringCodec< false > > mHandle
Definition: AttributeArrayString.h:160
openvdb::v6_1::points::TypedAttributeArray
Typed class for storing attribute data.
Definition: AttributeArray.h:584
openvdb::v6_1::points::StringCodec::name
static const char * name()
Definition: AttributeArrayString.h:75
openvdb::v6_1::points::StringMetaInserter
Definition: AttributeArrayString.h:85
AttributeArray.h
Attribute Array storage templated on type and compression codec.
openvdb::v6_1::points::AttributeHandle
Definition: AttributeArray.h:846
openvdb::v6_1::points::StringIndexType
uint32_t StringIndexType
Definition: AttributeArrayString.h:55
openvdb::v6_1::points::StringAttributeHandle::mMetadata
const MetaMap & mMetadata
Definition: AttributeArrayString.h:161
openvdb::v6_1::points::isString
bool isString(const AttributeArray &array)
Definition: AttributeArrayString.h:127
openvdb::v6_1::math::Truncate
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:824
openvdb::v6_1::points::AttributeArray
Base class for storing attribute data.
Definition: AttributeArray.h:118
openvdb::v6_1::Name
std::string Name
Definition: Name.h:44
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:125
openvdb::v6_1::points::StringAttributeWriteHandle
Definition: AttributeArrayString.h:168
openvdb::v6_1::Index
Index32 Index
Definition: Types.h:61
openvdb::v6_1::points::attribute_traits::StringTypeTrait
Definition: AttributeArrayString.h:60
openvdb
Definition: Exceptions.h:40
openvdb::v6_1::points::StringAttributeHandle::Ptr
std::shared_ptr< StringAttributeHandle > Ptr
Definition: AttributeArrayString.h:139
openvdb::v6_1::points::StringAttributeHandle::size
Index size() const
Definition: AttributeArrayString.h:148
openvdb::v6_1::points::StringAttributeHandle::stride
Index stride() const
Definition: AttributeArrayString.h:147
openvdb::v6_1::points::StringAttributeHandle::hasConstantStride
bool hasConstantStride() const
Definition: AttributeArrayString.h:151
openvdb::v6_1::points::attribute_traits::StringTypeTrait::Type
StringIndexType Type
Definition: AttributeArrayString.h:60
openvdb::v6_1::points::StringCodec::Storage
Definition: AttributeArrayString.h:71
openvdb::v6_1::points::StringAttributeHandle::isUniform
bool isUniform() const
Definition: AttributeArrayString.h:150
openvdb::v6_1::points::StringCodec::ValueType
StringIndexType ValueType
Definition: AttributeArrayString.h:68
openvdb::v6_1::points::StringAttributeHandle
Definition: AttributeArrayString.h:136
openvdb::v6_1::points::AttributeArray::isType
bool isType() const
Return true if this attribute is of the same type as the template parameter.
Definition: AttributeArray.h:259
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:177
openvdb::v6_1::points::AttributeWriteHandle
Write-able version of AttributeHandle.
Definition: AttributeArray.h:917
openvdb::v6_1::points::StringCodec
Definition: AttributeArrayString.h:66
openvdb::v6_1::MetaMap
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
openvdb::v6_1::points::StringCodec::Storage::Type
typename attribute_traits::StringTypeTrait< Truncate >::Type Type
Definition: AttributeArrayString.h:71