OpenVDB 5.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
43
44 namespace openvdb {
45 OPENVDB_USE_VERSION_NAMESPACE
46 namespace OPENVDB_VERSION_NAME {
47 namespace points {
48
49
51
52
53 using StringIndexType = uint32_t;
54
55
56 namespace attribute_traits
57 {
58 template <bool Truncate> struct StringTypeTrait { using Type = StringIndexType; };
59 template<> struct StringTypeTrait</*Truncate=*/true> { using Type = uint16_t; };
60 }
61
62
63 template <bool Truncate>
64 struct StringCodec
65 {
66 using ValueType = StringIndexType;
67
68 template <typename T>
69 struct Storage { using Type = typename attribute_traits::StringTypeTrait<Truncate>::Type; };
70
71 template<typename StorageType> static void decode(const StorageType&, ValueType&);
72 template<typename StorageType> static void encode(const ValueType&, StorageType&);
73 static const char* name() { return Truncate ? "str_trnc" : "str"; }
74 };
75
76
77 using StringAttributeArray = TypedAttributeArray<StringIndexType, StringCodec<false>>;
78
79
81
82
83 class OPENVDB_API StringMetaInserter
84 {
85 public:
86 StringMetaInserter(MetaMap& metadata);
87
89 void insert(const Name& name);
90
92 void resetCache();
93
94 private:
95 MetaMap& mMetadata;
96 std::vector<Index> mIndices;
97 std::vector<Name> mValues;
98 }; // StringMetaInserter
99
100
102
103
104 template <bool Truncate>
105 template<typename StorageType>
106 inline void
107 StringCodec<Truncate>::decode(const StorageType& data, ValueType& val)
108 {
109 val = static_cast<ValueType>(data);
110 }
111
112
113 template <bool Truncate>
114 template<typename StorageType>
115 inline void
116 StringCodec<Truncate>::encode(const ValueType& val, StorageType& data)
117 {
118 data = static_cast<ValueType>(val);
119 }
120
121
123
124
125 inline bool isString(const AttributeArray& array)
126 {
127 return array.isType<StringAttributeArray>();
128 }
129
130
132
133
134 class OPENVDB_API StringAttributeHandle
135 {
136 public:
137 using Ptr = std::shared_ptr<StringAttributeHandle>;//SharedPtr<StringAttributeHandle>;
138
139 static Ptr create(const AttributeArray& array, const MetaMap& metadata, const bool preserveCompression = true);
140
141 StringAttributeHandle( const AttributeArray& array,
142 const MetaMap& metadata,
143 const bool preserveCompression = true);
144
145 Index size() const { return mHandle.size(); }
146 bool isUniform() const { return mHandle.isUniform(); }
147
148 Name get(Index n, Index m = 0) const;
149 void get(Name& name, Index n, Index m = 0) const;
150
151 protected:
152 AttributeHandle<StringIndexType, StringCodec<false>> mHandle;
153 const MetaMap& mMetadata;
154 }; // class StringAttributeHandle
155
156
158
159
160 class OPENVDB_API StringAttributeWriteHandle : public StringAttributeHandle
161 {
162 public:
163 using Ptr = std::shared_ptr<StringAttributeWriteHandle>;//SharedPtr<StringAttributeWriteHandle>;
164
165 static Ptr create(AttributeArray& array, const MetaMap& metadata, const bool expand = true);
166
167 StringAttributeWriteHandle( AttributeArray& array,
168 const MetaMap& metadata,
169 const bool expand = true);
170
173 void expand(bool fill = true);
174
176 void collapse();
179 void collapse(const Name& name);
180
182 bool compact();
183
186 void fill(const Name& name);
187
189 void set(Index n, const Name& name);
190 void set(Index n, Index m, const Name& name);
191
193 void resetCache();
194
195 private:
198 Index getIndex(const Name& name);
199
200 using ValueMap = std::map<std::string, Index>;
201
202 ValueMap mCache;
203 AttributeWriteHandle<StringIndexType, StringCodec<false>> mWriteHandle;
204 }; // class StringAttributeWriteHandle
205
206
208
209
210 } // namespace points
211 } // namespace OPENVDB_VERSION_NAME
212 } // namespace openvdb
213
214 #endif // OPENVDB_POINTS_ATTRIBUTE_ARRAY_STRING_HAS_BEEN_INCLUDED
215
216 // Copyright (c) 2012-2018 DreamWorks Animation LLC
217 // All rights reserved. This software is distributed under the
218 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
219
openvdb::v5_1::points::StringAttributeHandle::mMetadata
const MetaMap & mMetadata
Definition: AttributeArrayString.h:153
openvdb::v5_1::points::StringAttributeWriteHandle
Definition: AttributeArrayString.h:160
openvdb::v5_1::points::TypedAttributeArray
Typed class for storing attribute data.
Definition: AttributeArray.h:441
openvdb::v5_1::points::StringMetaInserter
Definition: AttributeArrayString.h:83
OPENVDB_API
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
openvdb::v5_1::points::AttributeArray
Base class for storing attribute data.
Definition: AttributeArray.h:118
openvdb::v5_1::Name
std::string Name
Definition: Name.h:44
openvdb::v5_1::points::StringAttributeHandle::Ptr
std::shared_ptr< StringAttributeHandle > Ptr
Definition: AttributeArrayString.h:137
openvdb::v5_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::v5_1::points::StringCodec::name
static const char * name()
Definition: AttributeArrayString.h:73
AttributeArray.h
Attribute Array storage templated on type and compression codec.
openvdb::v5_1::points::AttributeHandle
Definition: AttributeArray.h:644
openvdb::v5_1::points::attribute_traits::StringTypeTrait< true >::Type
uint16_t Type
Definition: AttributeArrayString.h:59
openvdb::v5_1::points::isString
bool isString(const AttributeArray &array)
Definition: AttributeArrayString.h:125
openvdb::v5_1::points::StringCodec
Definition: AttributeArrayString.h:64
openvdb::v5_1::points::StringAttributeHandle::mHandle
AttributeHandle< StringIndexType, StringCodec< false > > mHandle
Definition: AttributeArrayString.h:152
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
openvdb
Definition: Exceptions.h:40
openvdb::v5_1::points::AttributeWriteHandle
Write-able version of AttributeHandle.
Definition: AttributeArray.h:713
openvdb::v5_1::points::AttributeArray::isType
bool isType() const
Return true if this attribute is of the same type as the template parameter.
Definition: AttributeArray.h:186
openvdb::v5_1::points::attribute_traits::StringTypeTrait
Definition: AttributeArrayString.h:58
openvdb::v5_1::points::StringIndexType
uint32_t StringIndexType
Definition: AttributeArrayString.h:53
openvdb::v5_1::points::StringCodec::Storage::Type
typename attribute_traits::StringTypeTrait< Truncate >::Type Type
Definition: AttributeArrayString.h:69
openvdb::v5_1::points::StringCodec::ValueType
StringIndexType ValueType
Definition: AttributeArrayString.h:66
openvdb::v5_1::points::StringAttributeHandle::size
Index size() const
Definition: AttributeArrayString.h:145
openvdb::v5_1::points::StringAttributeHandle
Definition: AttributeArrayString.h:134
openvdb::v5_1::Index
Index32 Index
Definition: Types.h:61
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
openvdb::v5_1::points::StringAttributeHandle::isUniform
bool isUniform() const
Definition: AttributeArrayString.h:146
openvdb::v5_1::points::StringCodec::Storage
Definition: AttributeArrayString.h:69
openvdb::v5_1::MetaMap
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
openvdb::v5_1::points::attribute_traits::StringTypeTrait::Type
StringIndexType Type
Definition: AttributeArrayString.h:58