HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AttributeArrayString.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2017 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 //
29 ///////////////////////////////////////////////////////////////////////////
30 
31 /// @file points/AttributeArrayString.h
32 ///
33 /// @author Dan Bailey
34 ///
35 /// @brief Attribute array storage for string data using Descriptor Metadata.
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 
50 ////////////////////////////////////////
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 
80 ////////////////////////////////////////
81 
82 
84 {
85 public:
86  StringMetaInserter(MetaMap& metadata);
87 
88  /// Insert the string into the metadata
89  void insert(const Name& name);
90 
91  /// Reset the cache from the metadata
92  void resetCache();
93 
94 private:
95  MetaMap& mMetadata;
96  std::vector<Index> mIndices;
97  std::vector<Name> mValues;
98 }; // StringMetaInserter
99 
100 
101 ////////////////////////////////////////
102 
103 
104 template <bool Truncate>
105 template<typename StorageType>
106 inline void
108 {
109  val = static_cast<ValueType>(data);
110 }
111 
112 
113 template <bool Truncate>
114 template<typename StorageType>
115 inline void
117 {
118  data = static_cast<ValueType>(val);
119 }
120 
121 
122 ////////////////////////////////////////
123 
124 
125 inline bool isString(const AttributeArray& array)
126 {
127  return array.isType<StringAttributeArray>();
128 }
129 
130 
131 ////////////////////////////////////////
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 
157 ////////////////////////////////////////
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 
171  /// @brief If this array is uniform, replace it with an array of length size().
172  /// @param fill if true, assign the uniform value to each element of the array.
173  void expand(bool fill = true);
174 
175  /// @brief Set membership for the whole array and attempt to collapse
176  void collapse();
177  /// @brief Set membership for the whole array and attempt to collapse
178  /// @param name Name of the String
179  void collapse(const Name& name);
180 
181  /// Compact the existing array to become uniform if all values are identical
182  bool compact();
183 
184  /// @brief Fill the existing array with the given value.
185  /// @note Identical to collapse() except a non-uniform array will not become uniform.
186  void fill(const Name& name);
187 
188  /// Set the value of the index to @a name
189  void set(Index n, const Name& name);
190  void set(Index n, Index m, const Name& name);
191 
192  /// Reset the value cache from the metadata
193  void resetCache();
194 
195 private:
196  /// Retrieve the index of this string value from the cache
197  /// @note throws if name does not exist in cache
198  Index getIndex(const Name& name);
199 
200  using ValueMap = std::map<std::string, Index>;
201 
202  ValueMap mCache;
204 }; // class StringAttributeWriteHandle
205 
206 
207 ////////////////////////////////////////
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-2017 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 
bool isString(const AttributeArray &array)
Definition: ImfName.h:53
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:836
typename attribute_traits::StringTypeTrait< Truncate >::Type Type
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
static void decode(const StorageType &, ValueType &)
GLdouble n
Definition: glcorearb.h:2007
Base class for storing attribute data.
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
#define OPENVDB_VERSION_NAME
Definition: version.h:43
AttributeHandle< StringIndexType, StringCodec< false > > mHandle
GLboolean * data
Definition: glcorearb.h:130
GLuint const GLchar * name
Definition: glcorearb.h:785
Typed class for storing attribute data.
GLuint GLfloat * val
Definition: glcorearb.h:1607
static void encode(const ValueType &, StorageType &)
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Attribute Array storage templated on type and compression codec.
bool isType() const
Return true if this attribute is of the same type as the template parameter.