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