All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
attributeDescriptor.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_DESCRIPTOR_H
26 #define USDDRACO_ATTRIBUTE_DESCRIPTOR_H
27 
28 #include "pxr/pxr.h"
29 #include "pxr/base/tf/token.h"
30 #include "pxr/usd/sdf/valueTypeName.h"
31 #include "pxr/usd/usdGeom/mesh.h"
32 #include "pxr/usd/usdGeom/primvarsAPI.h"
33 
34 #include <draco/attributes/geometry_attribute.h>
35 #include <draco/attributes/point_attribute.h>
36 #include <draco/mesh/mesh.h>
37 
38 
39 PXR_NAMESPACE_OPEN_SCOPE
40 
41 
50 public:
51  // The status indicates whether the descriptor is valid or invalid, as well
52  // as whether the corresponding attribute is absent from the mesh.
53  enum Status { VALID, INVALID, ABSENT };
54 
55  // Describes attribute data shape, such as vector, matrix, or quaternion.
56  // Scalar data types are assumed to be a special case of a vector.
57  enum Shape { VECTOR, MATRIX, QUATERNION};
58 
59  // Keys for storing of attribute description items in Draco metadata, for
60  // description items that are not directly supported by Draco file format.
61  static const std::string METADATA_NAME_KEY;
62  static const std::string METADATA_SHAPE_KEY;
63  static const std::string METADATA_HALF_KEY;
64  static const std::string METADATA_VALUES_TIME_KEY;
65  static const std::string METADATA_INDICES_TIME_KEY;
66  static const std::string METADATA_INTERPOLATION_KEY;
67 
68  // Methods for creating descriptors for specific named attributes from USD
69  // or Draco mesh. While some of the description items for named attributes
70  // are known, e.g., a texture coordinate is always a length-two vector,
71  // other description items need to be obtained from mesh, susch as
72  // interpolation, time sample, shape, etc.
73  static UsdDracoAttributeDescriptor ForPositions(const UsdGeomMesh &mesh);
74  static UsdDracoAttributeDescriptor ForTexCoords(const UsdGeomMesh &mesh);
75  static UsdDracoAttributeDescriptor ForNormals(const UsdGeomMesh &mesh);
76  static UsdDracoAttributeDescriptor ForPositions(const draco::Mesh &mesh);
77  static UsdDracoAttributeDescriptor ForTexCoords(const draco::Mesh &mesh);
78  static UsdDracoAttributeDescriptor ForNormals(const draco::Mesh &mesh);
79 
80  // Descriptors for helper attributes that enable support of USD mesh
81  // features that are not supported directly by Draco, such as quads, holes,
82  // and subdivision surfaces.
83  static UsdDracoAttributeDescriptor ForHoleFaces();
84  static UsdDracoAttributeDescriptor ForAddedEdges();
85  static UsdDracoAttributeDescriptor ForPosOrder();
86 
87  // Creates attribute descriptor from Draco attribute and its metadata.
88  static UsdDracoAttributeDescriptor FromDracoAttribute(
89  const draco::PointAttribute &attribute,
90  const draco::AttributeMetadata &metadata, bool isPrimvar);
91 
92  // Creates attribute descriptor from a given USD mesh primvar.
93  static UsdDracoAttributeDescriptor FromUsdPrimvar(
94  const UsdGeomPrimvar &primvar,
95  draco::GeometryAttribute::Type attributeType);
96 
97  // Craetes Draco metadata representation of attribute descriptor.
98  std::unique_ptr<draco::AttributeMetadata> ToMetadata() const;
99 
100  // Getters for individual attribute description items.
101  Status GetStatus() const { return _status; }
102  draco::GeometryAttribute::Type GetAttributeType() const { return _attributeType; }
103  const TfToken &GetName() const { return _name; }
104  draco::DataType GetDataType() const { return _dataType; }
105  bool GetIsPrimvar() const { return _isPrimvar; }
106  size_t GetNumComponents() const { return _numComponents; }
107  Shape GetShape() const { return _shape; }
108  bool GetIsHalf() const { return _isHalf; }
109  UsdTimeCode GetValuesTime() const { return _valuesTime; }
110  UsdTimeCode GetIndicesTime() const { return _indicesTime; }
111  const TfToken &GetInterpolation() const { return _interpolation; }
112 
113  // Default values for Draco metadata entries.
114  static Shape GetDefaultShape() { return VECTOR; }
115  static bool GetDefaultHalf() { return false; }
116  static UsdTimeCode GetDefaultTime() { return UsdTimeCode::Default(); }
117  static TfToken GetDefaultInterpolation() {
118  return UsdGeomTokens->faceVarying;
119  }
120 
121  // Names of non-generic attributes.
122  static TfToken GetPositionsName() { return UsdGeomTokens->points; }
123  static TfToken GetTexCoordsName() { return TfToken("primvars:Texture_uv"); }
124  static TfToken GetNormalsName() { return TfToken("primvars:normals"); }
125  static TfToken GetHoleFacesName() { return TfToken("hole_faces"); }
126  static TfToken GetAddedEdgesName() { return TfToken("added_edges"); }
127  static TfToken GetPointOrderName() { return TfToken("point_order"); }
128 
129  // Indicates whether the attribute is generic.
130  bool IsGeneric() const;
131 
132 private:
133  // Creates an attribute description with an invalid status.
135 
136  // Creates an attribute descriptor with a given status.
137  UsdDracoAttributeDescriptor(Status status);
138 
139  // Creates an sttribute descriptor with all the given items.
140  UsdDracoAttributeDescriptor(Status status,
141  draco::GeometryAttribute::Type attributeType,
142  TfToken name,
143  draco::DataType dataType,
144  bool isPrimvar,
145  size_t numComponents,
146  Shape shape,
147  bool isHalf,
148  UsdTimeCode valuesTime,
149  UsdTimeCode indicesTime,
150  TfToken interpolation);
151 
152  // Static helper methods for creating attribute desccriptor.
153  static UsdDracoAttributeDescriptor Invalid();
154  static UsdDracoAttributeDescriptor Absent();
155  static UsdDracoAttributeDescriptor Create(
156  draco::GeometryAttribute::Type attributeType, TfToken name,
157  draco::DataType dataType, bool isPrimvar, size_t numComponents,
158  Shape shape, bool isHalf, UsdTimeCode valuesTime,
159  UsdTimeCode indicesTime, TfToken interpolation);
160 
161  // Methods for creating attribute descriptor from Draco and USD meshes.
162  static UsdDracoAttributeDescriptor FromDracoMesh(
163  const draco::Mesh &mesh, draco::GeometryAttribute::Type attributeType,
164  TfToken name, bool isPrimvar);
165  static UsdDracoAttributeDescriptor FromUsdMesh(
166  const UsdGeomMesh &mesh, draco::GeometryAttribute::Type attributeType,
167  TfToken name);
168  static UsdDracoAttributeDescriptor FromUsdAttribute(
169  const UsdAttribute &attribute,
170  draco::GeometryAttribute::Type attributeType, const TfToken &name,
171  bool isPrimvar, const UsdTimeCode &indicesTime,
172  const TfToken &interpolation);
173 
174  // Helper methods.
175  static const std::set<TfToken> &GetSupportedInterpolations();
176  std::string GetShapeText() const;
177  static std::string GetShapeText(Shape shape);
178 
179  // Template method for extracting a single time sample for attribute values
180  // or primvar indices. Supported types are UsdGeomPrimvar and UsdAttribute.
181  // Returns true on success.
182  template <class T>
183  static bool GetTimeFrom(const T &item, double *time) {
184  // Try to get time samples and return in case of error.
185  std::vector<double> times;
186  if (!item.GetTimeSamples(&times))
187  return false;
188 
189  // Multiple time samples are not supported.
190  if (times.size() > 1) {
191  return false;
192  }
193 
194  // Use default time if there are no time samples.
195  if (times.empty()) {
196  *time = GetDefaultTime().GetValue();
197  return true;
198  }
199 
200  // Use a single time samples.
201  *time = times[0];
202  return true;
203  }
204 
205 private:
206  const Status _status;
207  const draco::GeometryAttribute::Type _attributeType;
208  const TfToken _name;
209  const draco::DataType _dataType;
210  const bool _isPrimvar;
211  const size_t _numComponents;
212  const Shape _shape;
213  // Draco has no direct support for USD's 16-bit floating point primvars,
214  // i.e., half, half2, half3, half4, and quath. Such primvars are stored in
215  // place of 16-bit integer Draco attributes. To distinguish such attributes
216  // from genuine 16-bit integers, Draco metadata entry is used.
217  const bool _isHalf;
218  // Draco has no animation support. USD attributes are exported to Draco if
219  // their values and indices are only defined at a single time sample.
220  // Otherwise, animated attributes remain in USD mesh.
221  const UsdTimeCode _valuesTime;
222  const UsdTimeCode _indicesTime;
223  const TfToken _interpolation;
224 };
225 
226 
227 PXR_NAMESPACE_CLOSE_SCOPE
228 
229 #endif // USDDRACO_ATTRIBUTE_DESCRIPTOR_H
Scenegraph object for authoring and retrieving numeric, string, and array valued data, sampled over time.
Definition: attribute.h:176
double GetValue() const
Return the numeric value for this time.
Definition: timeCode.h:152
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:85
USDGEOM_API TfStaticData< UsdGeomTokensType > UsdGeomTokens
A global variable with static, efficient TfTokens for use in all public USD API.
static constexpr UsdTimeCode Default()
Produce a UsdTimeCode representing the sentinel value for &#39;default&#39;.
Definition: timeCode.h:113
Schema wrapper for UsdAttribute for authoring and introspecting attributes that are primvars...
Definition: primvar.h:260
Encodes a mesh surface whose definition and feature-set will converge with that of OpenSubdiv...
Definition: mesh.h:86
Stores description of mesh attributes, such as name, data type, shape, time sample, interpolation, etc.