OpenVDB  5.2.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 {
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>
65 {
67 
68  template <typename T>
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 
78 
79 
81 
82 
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 
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 
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:
154 }; // class StringAttributeHandle
155 
156 
158 
159 
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 
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;
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 
Definition: AttributeArrayString.h:83
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
std::shared_ptr< StringAttributeHandle > Ptr
Definition: AttributeArrayString.h:137
Write-able version of AttributeHandle.
Definition: AttributeArray.h:715
typename attribute_traits::StringTypeTrait< Truncate >::Type Type
Definition: AttributeArrayString.h:69
uint16_t Type
Definition: AttributeArrayString.h:59
Attribute Array storage templated on type and compression codec.
Definition: AttributeArrayString.h:69
std::string Name
Definition: Name.h:44
Definition: AttributeArrayString.h:160
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
static const char * name()
Definition: AttributeArrayString.h:73
AttributeHandle< StringIndexType, StringCodec< false > > mHandle
Definition: AttributeArrayString.h:152
Definition: AttributeArrayString.h:134
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
bool isString(const AttributeArray &array)
Definition: AttributeArrayString.h:125
Definition: Exceptions.h:40
Index32 Index
Definition: Types.h:61
Definition: AttributeArray.h:644
Base class for storing attribute data.
Definition: AttributeArray.h:118
bool isType() const
Return true if this attribute is of the same type as the template parameter.
Definition: AttributeArray.h:186
StringIndexType ValueType
Definition: AttributeArrayString.h:66
uint32_t StringIndexType
Definition: AttributeArrayString.h:53
Index size() const
Definition: AttributeArrayString.h:145
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:824
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
Definition: AttributeArrayString.h:64
bool isUniform() const
Definition: AttributeArrayString.h:146
const MetaMap & mMetadata
Definition: AttributeArrayString.h:153
StringIndexType Type
Definition: AttributeArrayString.h:58
Typed class for storing attribute data.
Definition: AttributeArray.h:441