All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
attributeFactory.h
1 //
2 // Copyright 2019 Google LLC
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 
25 #ifndef USDDRACO_ATTRIBUTE_FACTORY_H
26 #define USDDRACO_ATTRIBUTE_FACTORY_H
27 
28 #include "attributeDescriptor.h"
29 
30 #include "pxr/pxr.h"
31 #include "pxr/base/tf/token.h"
32 #include "pxr/usd/sdf/valueTypeName.h"
33 #include "pxr/usd/usdGeom/primvarsAPI.h"
34 
35 #include <draco/attributes/geometry_attribute.h>
36 #include <draco/attributes/point_attribute.h>
37 
38 
39 PXR_NAMESPACE_OPEN_SCOPE
40 
41 
49 public:
50  // Creates an attribute according to attribute interface type and a given
51  // descriptor. A given creator object must have a CreateAttribute<T> method
52  // with matching return type that creates an attribute with values
53  // of C++ type T. Used to create attributes of UsdDracoImportAttribute<T>
54  // and UsdDracoExportAttribute<T> types.
55  template <class InterfaceT, class CreatorT>
56  static std::unique_ptr<InterfaceT> CreateAttribute(
57  const UsdDracoAttributeDescriptor &descriptor,
58  const CreatorT &creator);
59 
60  // Returns SDF type name for a given attribute descriptor. Runtime error is
61  // produced for unsupported attribute descriptors.
62  static SdfValueTypeName GetSdfValueTypeName(
63  const UsdDracoAttributeDescriptor &descriptor);
64 
65  // Returns Draco data type corresponding to a given type info.
66  // draco::DT_INVALID is returned for unsupported type infos.
67  static draco::DataType GetDracoDataType(const std::type_info &typeInfo);
68 
69  // Returns data shape corresponding to a given type info.
70  // draco::DT_INVALID is returned for unsupported type infos.
71  static UsdDracoAttributeDescriptor::Shape GetShape(
72  const std::type_info &typeInfo);
73 
74  // Returns a bool indicating whether a given type info corresponds to a
75  // 16-bit floating point data type.
76  static bool IsHalf(const std::type_info &typeInfo);
77 };
78 
79 
80 // Macros to make the switch cases below more compact.
81 #define CASE_FOR_ATTRIBUTE_TYPE(dracoType, valueType) { \
82  case dracoType: \
83  return creator.template CreateAttribute<valueType>(descriptor); \
84 }
85 
86 #define CASE_FOR_ATTRIBUTE_HALF(dracoType, valueType) { \
87  case dracoType: \
88  if (descriptor.GetIsHalf()) \
89  return creator.template CreateAttribute<valueType>(descriptor); \
90  break; \
91 }
92 
93 template <class InterfaceT, class CreatorT>
94 std::unique_ptr<InterfaceT> UsdDracoAttributeFactory::CreateAttribute(
95  const UsdDracoAttributeDescriptor &descriptor, const CreatorT &creator) {
96  // Create attribute from attribute descriptor.
97  switch (descriptor.GetShape()) {
98  case UsdDracoAttributeDescriptor::MATRIX:
99  switch (descriptor.GetNumComponents()) {
100  case 4:
101  switch (descriptor.GetDataType()) {
102  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_FLOAT64, GfMatrix2d);
103  default:
104  break;
105  }
106  break;
107  case 9:
108  switch (descriptor.GetDataType()) {
109  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_FLOAT64, GfMatrix3d);
110  default:
111  break;
112  }
113  break;
114  case 16:
115  switch (descriptor.GetDataType()) {
116  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_FLOAT64, GfMatrix4d);
117  default:
118  break;
119  }
120  break;
121  default:
122  break;
123  }
124  break;
125  case UsdDracoAttributeDescriptor::QUATERNION:
126  if (descriptor.GetNumComponents() != 4)
127  break;
128  switch (descriptor.GetDataType()) {
129  // USD halfs are stored as Draco 16-bit ints.
130  CASE_FOR_ATTRIBUTE_HALF(draco::DT_INT16, GfQuath);
131  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_FLOAT32, GfQuatf);
132  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_FLOAT64, GfQuatd);
133  default:
134  break;
135  }
136  break;
137  case UsdDracoAttributeDescriptor::VECTOR:
138  switch (descriptor.GetNumComponents()) {
139  case 1:
140  switch (descriptor.GetDataType()) {
141  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_UINT8, uint8_t);
142  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_INT32, int32_t);
143  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_UINT32, uint32_t);
144  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_INT64, int64_t);
145  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_UINT64, uint64_t);
146  // USD halfs are stored as Draco 16-bit ints.
147  CASE_FOR_ATTRIBUTE_HALF(draco::DT_INT16, GfHalf);
148  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_FLOAT32, float);
149  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_FLOAT64, double);
150  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_BOOL, bool);
151  default:
152  break;
153  }
154  case 2:
155  switch (descriptor.GetDataType()) {
156  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_INT32, GfVec2i);
157  // USD halfs are stored as Draco 16-bit ints.
158  CASE_FOR_ATTRIBUTE_HALF(draco::DT_INT16, GfVec2h);
159  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_FLOAT32, GfVec2f);
160  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_FLOAT64, GfVec2d);
161  default:
162  break;
163  }
164  case 3:
165  switch (descriptor.GetDataType()) {
166  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_INT32, GfVec3i);
167  // USD halfs are stored as Draco 16-bit ints.
168  CASE_FOR_ATTRIBUTE_HALF(draco::DT_INT16, GfVec3h);
169  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_FLOAT32, GfVec3f);
170  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_FLOAT64, GfVec3d);
171  default:
172  break;
173  }
174  case 4:
175  switch (descriptor.GetDataType()) {
176  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_INT32, GfVec4i);
177  // USD halfs are stored as Draco 16-bit ints.
178  CASE_FOR_ATTRIBUTE_HALF(draco::DT_INT16, GfVec4h);
179  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_FLOAT32, GfVec4f);
180  CASE_FOR_ATTRIBUTE_TYPE(draco::DT_FLOAT64, GfVec4d);
181  default:
182  break;
183  }
184  default:
185  break;
186  }
187  break;
188  default:
189  break;
190  }
191  return nullptr;
192 }
193 
194 
195 PXR_NAMESPACE_CLOSE_SCOPE
196 
197 #endif // USDDRACO_ATTRIBUTE_FACTORY_H
Basic type: a quaternion, a complex number with a real coefficient and three imaginary coefficients...
Definition: quath.h:61
Basic type for a vector of 4 int components.
Definition: vec4i.h:61
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
pxr_half::half GfHalf
A 16-bit floating point data type.
Definition: half.h:42
Basic type for a vector of 3 float components.
Definition: vec3f.h:63
Basic type for a vector of 4 double components.
Definition: vec4d.h:63
Represents a value type name, i.e.
Definition: valueTypeName.h:83
Basic type: a quaternion, a complex number with a real coefficient and three imaginary coefficients...
Definition: quatf.h:60
Basic type for a vector of 2 double components.
Definition: vec2d.h:63
Basic type for a vector of 4 GfHalf components.
Definition: vec4h.h:64
Stores a 2x2 matrix of double elements.
Definition: matrix2d.h:62
Basic type for a vector of 2 GfHalf components.
Definition: vec2h.h:64
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
Basic type for a vector of 3 int components.
Definition: vec3i.h:61
Class for instantiating import and export attributes from a given attribute descriptor.
Basic type for a vector of 4 float components.
Definition: vec4f.h:63
Basic type for a vector of 2 float components.
Definition: vec2f.h:63
Basic type for a vector of 3 double components.
Definition: vec3d.h:63
Stores a 3x3 matrix of double elements.
Definition: matrix3d.h:82
Basic type: a quaternion, a complex number with a real coefficient and three imaginary coefficients...
Definition: quatd.h:60
Basic type for a vector of 3 GfHalf components.
Definition: vec3h.h:64
Stores description of mesh attributes, such as name, data type, shape, time sample, interpolation, etc.