HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
blendShapeQuery.h
Go to the documentation of this file.
1 //
2 // Copyright 2019 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_USD_SKEL_BLEND_SHAPE_QUERY_H
8 #define PXR_USD_USD_SKEL_BLEND_SHAPE_QUERY_H
9 
10 /// \file usdSkel/blendShapeQuery.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/usdSkel/api.h"
16 
17 #include "pxr/base/gf/vec3f.h"
18 #include "pxr/base/tf/span.h"
19 #include "pxr/base/vt/array.h"
20 
21 
23 
24 
25 class UsdSkelBindingAPI;
26 
27 
28 /// \class UsdSkelBlendShapeQuery
29 ///
30 /// Helper class used to resolve blend shape weights, including
31 /// inbetweens.
33 {
34 public:
35 
36  UsdSkelBlendShapeQuery() = default;
37 
39 
40  /// Return true if this query is valid.
41  bool IsValid() const { return bool(_prim); }
42 
43  /// Boolean conversion operator. Equivalent to IsValid().
44  explicit operator bool() const { return IsValid(); }
45 
46  /// Returns the prim the blend shapes apply to.
47  const UsdPrim& GetPrim() const { return _prim; }
48 
49  /// Returns the blend shape corresponding to \p blendShapeIndex.
50  USDSKEL_API UsdSkelBlendShape GetBlendShape(size_t blendShapeIndex) const;
51 
52  /// Returns the inbetween shape corresponding to sub-shape \p i, if any.
53  USDSKEL_API UsdSkelInbetweenShape GetInbetween(size_t subShapeIndex) const;
54 
55  /// Returns the blend shape index corresponding to the \p i'th sub-shape.
56  USDSKEL_API size_t GetBlendShapeIndex(size_t subShapeIndex) const;
57 
58  size_t GetNumBlendShapes() const { return _blendShapes.size(); }
59 
60  size_t GetNumSubShapes() const { return _subShapes.size(); }
61 
62  /// Compute an array holding the point indices of all shapes.
63  /// This is indexed by the _blendShapeIndices_ returned by
64  /// ComputeSubShapes().
65  /// Since the _pointIndices_ property of blend shapes is optional,
66  /// some of the arrays may be empty.
67  USDSKEL_API std::vector<VtIntArray>
69 
70  /// Compute an array holding the point offsets of all sub-shapes.
71  /// This includes offsets of both primary shapes -- those stored directly
72  /// on a BlendShape primitive -- as well as those of inbetween shapes.
73  /// This is indexed by the _subShapeIndices_ returned by
74  /// ComputeSubShapeWeights().
75  USDSKEL_API std::vector<VtVec3fArray>
77 
78  /// Compute an array holding the normal offsets of all sub-shapes.
79  /// This includes offsets of both primary shapes -- those stored directly
80  /// on a BlendShape primitive -- as well as those of inbetween shapes.
81  /// This is indexed by the _subShapeIndices_ returned by
82  /// ComputeSubShapeWeights().
83  /// Normal offsets are optional. An empty array is stored for shapes that
84  /// do not specify normal offsets.
85  USDSKEL_API std::vector<VtVec3fArray>
87 
88  /// Compute the resolved weights for all sub-shapes bound to this prim.
89  /// The \p weights values are initial weight values, ordered according
90  /// to the _skel:blendShapeTargets_ relationship of the prim this query
91  /// is associated with. If there are any inbetween shapes, a new set
92  /// of weights is computed, providing weighting of the relevant inbetweens.
93  ///
94  /// All computed arrays shared the same size. Elements of the same index
95  /// identify which sub-shape of which blend shape a given weight value
96  /// is mapped to.
97  USDSKEL_API bool
99  VtFloatArray* subShapeWeights,
100  VtUIntArray* blendShapeIndices,
101  VtUIntArray* subShapeIndices) const;
102 
103 
104  /// Compute a flattened array of weights for all sub-shapes.
105  USDSKEL_API bool
107  VtFloatArray* subShapeWeights) const;
108 
109  /// Deform \p points using the resolved sub-shapes given by
110  /// \p subShapeWeights, \p blendShapeIndices and \p subShapeIndices.
111  /// The \p blendShapePointIndices and \p blendShapePointOffsets
112  /// arrays both provide the pre-computed point offsets and indices
113  /// of each sub-shape, as computed by ComputeBlendShapePointIndices()
114  /// and ComputeSubShapePointOffsets().
115  USDSKEL_API bool
117  const TfSpan<const float> subShapeWeights,
118  const TfSpan<const unsigned> blendShapeIndices,
119  const TfSpan<const unsigned> subShapeIndices,
120  const std::vector<VtIntArray>& blendShapePointIndices,
121  const std::vector<VtVec3fArray>& subShapePointOffsets,
122  TfSpan<GfVec3f> points) const;
123 
124  /// Deform \p normals using the resolved sub-shapes given by
125  /// \p subShapeWeights, \p blendShapeIndices and \p subShapeIndices.
126  /// The \p blendShapePointIndices and \p blendShapeNormalOffsets
127  /// arrays both provide the pre-computed normal offsets and indices
128  /// of each sub-shape, as computed by ComputeBlendShapePointIndices()
129  /// and ComputeSubShapeNormalOffsets().
130  /// This is equivalent to ComputeDeformedPoints(), except that the
131  /// resulting points are normalized after deformation.
132  USDSKEL_API bool
134  const TfSpan<const float> subShapeWeights,
135  const TfSpan<const unsigned> blendShapeIndices,
136  const TfSpan<const unsigned> subShapeIndices,
137  const std::vector<VtIntArray>& blendShapePointIndices,
138  const std::vector<VtVec3fArray>& subShapeNormalOffsets,
139  TfSpan<GfVec3f> noramls) const;
140 
141  /// Compute a packed shape table combining all sub-shapes.
142  /// This is intended to help encode blend shapes in a GPU-friendly form.
143  /// The resulting \p offsets array holds contiguous runs of
144  /// offsets for every point. The array holds all of the offsets of
145  /// every blend shape for point 0, followed by the offset of every
146  /// blend shape for point 1, and so forth.
147  /// Offsets are stored as a GfVec4f. The first three components of each
148  /// offset holds the actual offset value, while the last value holds
149  /// the sub-shape index, as a float. The sub-shape index can be used
150  /// to lookup a corresponding weight value in the 'subShapeWeights'
151  /// array returned by ComputeSubShapeWeights.
152  USDSKEL_API bool
153  ComputePackedShapeTable(VtVec4fArray* offsets,
154  VtVec2iArray* ranges) const;
155 
157  std::string GetDescription() const;
158 
159 private:
160 
161  /// Object identifying a general subshape.
162  struct _SubShape {
163  _SubShape() = default;
164 
165  _SubShape(unsigned blendShapeIndex, int inbetweenIndex, float weight)
166  : _blendShapeIndex(blendShapeIndex),
167  _inbetweenIndex(inbetweenIndex),
168  _weight(weight) {}
169 
170  unsigned GetBlendShapeIndex() const { return _blendShapeIndex; }
171 
172  int GetInbetweenIndex() const { return _inbetweenIndex; }
173 
174  bool IsInbetween() const { return _inbetweenIndex >= 0; }
175  bool IsNullShape() const { return _weight == 0.0f; }
176  bool IsPrimaryShape() const { return _weight == 1.0f; }
177 
178  float GetWeight() const { return _weight; }
179 
180  private:
181  unsigned _blendShapeIndex = 0;
182  int _inbetweenIndex = 0;
183  float _weight = 0;
184  };
185 
186  struct _SubShapeCompareByWeight {
187  bool operator()(const _SubShape& lhs, const _SubShape& rhs) const
188  { return lhs.GetWeight() < rhs.GetWeight(); }
189 
190  bool operator()(float lhs, const _SubShape& rhs) const
191  { return lhs < rhs.GetWeight(); }
192  };
193 
194  struct _BlendShape {
195  UsdSkelBlendShape shape;
196  size_t firstSubShape = 0;
197  size_t numSubShapes = 0;
198  };
199 
200  UsdPrim _prim;
201  std::vector<_SubShape> _subShapes;
202  std::vector<_BlendShape> _blendShapes;
203  std::vector<UsdSkelInbetweenShape> _inbetweens;
204 };
205 
207 
208 #endif // PXR_USD_USD_SKEL_BLEND_SHAPE_QUERY_H
USDSKEL_API UsdSkelBlendShape GetBlendShape(size_t blendShapeIndex) const
Returns the blend shape corresponding to blendShapeIndex.
GLdouble GLdouble GLint GLint const GLdouble * points
Definition: glad.h:2676
USDSKEL_API bool ComputeDeformedNormals(const TfSpan< const float > subShapeWeights, const TfSpan< const unsigned > blendShapeIndices, const TfSpan< const unsigned > subShapeIndices, const std::vector< VtIntArray > &blendShapePointIndices, const std::vector< VtVec3fArray > &subShapeNormalOffsets, TfSpan< GfVec3f > noramls) const
USDSKEL_API bool ComputeFlattenedSubShapeWeights(const TfSpan< const float > &weights, VtFloatArray *subShapeWeights) const
Compute a flattened array of weights for all sub-shapes.
USDSKEL_API std::vector< VtVec3fArray > ComputeSubShapePointOffsets() const
USDSKEL_API bool ComputePackedShapeTable(VtVec4fArray *offsets, VtVec2iArray *ranges) const
OutGridT const XformOp bool bool
UsdSkelBlendShapeQuery()=default
GLuint GLsizei const GLuint const GLintptr * offsets
Definition: glcorearb.h:2621
USDSKEL_API bool ComputeSubShapeWeights(const TfSpan< const float > &weights, VtFloatArray *subShapeWeights, VtUIntArray *blendShapeIndices, VtUIntArray *subShapeIndices) const
const UsdPrim & GetPrim() const
Returns the prim the blend shapes apply to.
Definition: span.h:70
USDSKEL_API bool ComputeDeformedPoints(const TfSpan< const float > subShapeWeights, const TfSpan< const unsigned > blendShapeIndices, const TfSpan< const unsigned > subShapeIndices, const std::vector< VtIntArray > &blendShapePointIndices, const std::vector< VtVec3fArray > &subShapePointOffsets, TfSpan< GfVec3f > points) const
USDSKEL_API std::string GetDescription() const
Definition: prim.h:116
#define USDSKEL_API
Definition: api.h:23
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
size_t GetNumBlendShapes() const
USDSKEL_API std::vector< VtVec3fArray > ComputeSubShapeNormalOffsets() const
USDSKEL_API size_t GetBlendShapeIndex(size_t subShapeIndex) const
Returns the blend shape index corresponding to the i'th sub-shape.
bool IsValid() const
Return true if this query is valid.
size_t GetNumSubShapes() const
USDSKEL_API std::vector< VtIntArray > ComputeBlendShapePointIndices() const
USDSKEL_API UsdSkelInbetweenShape GetInbetween(size_t subShapeIndex) const
Returns the inbetween shape corresponding to sub-shape i, if any.