All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
valueUtils.h
1 //
2 // Copyright 2019 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef _GUSD_VALUE_UTILS_H_
25 #define _GUSD_VALUE_UTILS_H_
26 
27 #include "api.h"
28 #include "UT_Gf.h"
29 
30 #include "pxr/pxr.h"
31 
32 #include "pxr/base/gf/traits.h"
33 #include "pxr/base/tf/span.h"
34 #include "pxr/base/vt/array.h"
35 #include "pxr/usd/sdf/types.h"
36 #include "pxr/usd/sdf/valueTypeName.h"
37 
38 #include <GA/GA_Types.h>
39 #include <UT/UT_Array.h>
40 
41 
42 class GA_Attribute;
43 class GA_Range;
44 class GEO_Detail;
45 class UT_Options;
46 class UT_StringHolder;
47 
48 
49 PXR_NAMESPACE_OPEN_SCOPE
50 
51 
55 template <class T>
56 GUSD_API
57 bool GusdReadUsdValuesFromAttr(const GA_Attribute& attr,
58  const TfSpan<const GA_Offset>& offsets,
59  const TfSpan<T>& values);
60 
61 template <class T>
62 GUSD_API
63 bool GusdWriteUsdValuesToAttr(GA_Attribute& attr,
64  const GA_Range& range,
65  const TfSpan<const GA_Index>& rangeIndices,
66  const TfSpan<const T>& values);
67 
68 
70 GUSD_API
71 SdfValueTypeName GusdGetSdfTypeNameForAttr(const GA_Attribute& attr);
72 
73 
78 GUSD_API
79 GA_TypeInfo GusdGetTypeInfoForUsdRole(const TfToken& role,
80  const int tupleSize=-1);
81 
82 
100 template <class T>
101 GUSD_API
102 GA_Attribute*
103 GusdCreateAttrForUsdValueType(GEO_Detail& gd,
104  const GA_AttributeScope scope,
105  const GA_AttributeOwner owner,
106  const UT_StringHolder& name,
107  const UT_Options* creationArgs=nullptr);
108 
109 
110 template <class T, class T2=void>
111 struct GusdUsdValueTypeAttrStorage
112 {
113  static const GA_Storage value = GA_STORE_INVALID;
114 };
115 
116 #define GUSD_DEFINE_USD_ATTR_STORAGE(Type, Storage) \
117  template <> \
118  struct GusdUsdValueTypeAttrStorage<Type> \
119  { \
120  static const GA_Storage value = Storage; \
121  };
122 
123 GUSD_DEFINE_USD_ATTR_STORAGE(bool, GA_STORE_BOOL);
124 GUSD_DEFINE_USD_ATTR_STORAGE(unsigned char, GA_STORE_UINT8);
125 GUSD_DEFINE_USD_ATTR_STORAGE(int, GA_STORE_INT32);
126 GUSD_DEFINE_USD_ATTR_STORAGE(unsigned int, GA_STORE_INT32);
127 GUSD_DEFINE_USD_ATTR_STORAGE(long, GA_STORE_INT64);
128 GUSD_DEFINE_USD_ATTR_STORAGE(unsigned long, GA_STORE_INT64);
129 GUSD_DEFINE_USD_ATTR_STORAGE(GfHalf, GA_STORE_REAL16);
130 GUSD_DEFINE_USD_ATTR_STORAGE(float, GA_STORE_REAL32);
131 GUSD_DEFINE_USD_ATTR_STORAGE(double, GA_STORE_REAL64);
132 GUSD_DEFINE_USD_ATTR_STORAGE(std::string, GA_STORE_STRING);
133 GUSD_DEFINE_USD_ATTR_STORAGE(TfToken, GA_STORE_STRING);
134 GUSD_DEFINE_USD_ATTR_STORAGE(SdfAssetPath, GA_STORE_STRING);
135 
136 // For vectors, derive storage from the scalar type.
137 template <class T>
138 struct GusdUsdValueTypeAttrStorage<
139  T, typename std::enable_if<GfIsGfVec<T>::value||
140  GfIsGfQuat<T>::value||
141  GfIsGfMatrix<T>::value>::type>
142 {
143  static const GA_Storage value =
144  GusdUsdValueTypeAttrStorage<typename T::ScalarType>::value;
145 };
146 
147 
148 // For arrays, derive storage from the element type.
149 template <class T>
150 struct GusdUsdValueTypeAttrStorage<
151  T, typename std::enable_if<VtIsArray<T>::value>::type>
152 {
153  static const GA_Storage value =
154  GusdUsdValueTypeAttrStorage<typename T::value_type>::value;
155 };
156 
157 
159 template <class T>
160 constexpr GA_Storage
161 GusdGetUsdValueTypeAttrStorage()
162 {
163  return GusdUsdValueTypeAttrStorage<T>::value;
164 }
165 
166 
171 template <class T, class T2=void>
173 {
174  const bool value = false;
175 };
176 
177 // Matrices may have roles...
178 template <class T>
180  T, typename std::enable_if<GfIsGfMatrix<T>::value>::type>
181 {
182  const bool value = true;
183 };
184 
185 // Non-integer vectors may have roles.
186 template <class T>
188  T, typename std::enable_if<GfIsGfVec<T>::value>::type>
189 {
190  using ScalarType = typename T::ScalarType;
191  const bool value = (SYSisSame<ScalarType,GfHalf>() ||
192  SYSisSame<ScalarType,float>() ||
193  SYSisSame<ScalarType,double>());
194 };
195 
196 
199 template <class T>
200 constexpr bool GusdUsdValueTypeMayHaveRole()
201 {
203 }
204 
205 
206 template <class T, class T2=void>
207 struct GusdUsdValueTypeTupleSize
208 {
209  static const int value = GusdGetTupleSize<T>();
210 };
211 
212 
213 template <class T>
214 struct GusdUsdValueTypeTupleSize<
215  T, typename std::enable_if<VtIsArray<T>::value>::type>
216 {
217  static const int value =
218  GusdUsdValueTypeTupleSize<typename T::value_type>::value;
219 };
220 
221 
222 template <class T>
223 constexpr int GusdGetUsdValueTypeTupleSize()
224 {
225  return GusdUsdValueTypeTupleSize<T>::value;
226 }
227 
228 
229 PXR_NAMESPACE_CLOSE_SCOPE
230 
231 
232 #endif // GUSD_VALUE_UTILS_H
Struct for determining whether or not an SdfValueTypeName corresponding to a USD value type might hol...
Definition: valueUtils.h:172
pxr_half::half GfHalf
A 16-bit floating point data type.
Definition: half.h:42
Represents a value type name, i.e.
Definition: valueTypeName.h:83
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
Represents a range of contiguous elements.
Definition: span.h:87
Contains an asset path and an optional resolved path.
Definition: assetPath.h:43