OpenVDB  6.2.1
AttributeArrayString.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 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 {
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>
67 {
69 
70  template <typename T>
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 
80 
81 
83 
84 
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 
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 
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:
162 }; // class StringAttributeHandle
163 
164 
166 
167 
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 
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;
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) 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 
bool isString(const AttributeArray &array)
Definition: AttributeArrayString.h:127
Definition: AttributeArrayString.h:71
AttributeHandle< StringIndexType, StringCodec< false > > mHandle
Definition: AttributeArrayString.h:160
uint16_t Type
Definition: AttributeArrayString.h:61
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:288
Typed class for storing attribute data.
Definition: AttributeArray.h:608
Index size() const
Definition: AttributeArrayString.h:148
const MetaMap & mMetadata
Definition: AttributeArrayString.h:161
bool isUniform() const
Definition: AttributeArrayString.h:150
Definition: AttributeArrayString.h:85
Attribute Array storage templated on type and compression codec.
uint32_t StringIndexType
Definition: AttributeArrayString.h:55
typename attribute_traits::StringTypeTrait< Truncate >::Type Type
Definition: AttributeArrayString.h:71
Index32 Index
Definition: Types.h:61
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
Write-able version of AttributeHandle.
Definition: AttributeArray.h:944
static const char * name()
Definition: AttributeArrayString.h:75
Definition: AttributeArrayString.h:66
Definition: AttributeArrayString.h:136
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:844
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:128
std::string Name
Definition: Name.h:44
Index stride() const
Definition: AttributeArrayString.h:147
Definition: Exceptions.h:40
std::shared_ptr< StringAttributeHandle > Ptr
Definition: AttributeArrayString.h:139
StringIndexType Type
Definition: AttributeArrayString.h:60
Definition: AttributeArray.h:873
Definition: AttributeArrayString.h:168
StringIndexType ValueType
Definition: AttributeArrayString.h:68
bool isType() const
Return true if this attribute is of the same type as the template parameter.
Definition: AttributeArray.h:270
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:180
Base class for storing attribute data.
Definition: AttributeArray.h:118
bool hasConstantStride() const
Definition: AttributeArrayString.h:151