1 //
2 // Copyright 2016 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_TOPOLOGY_H
25 #define PXR_USD_USD_SKEL_TOPOLOGY_H
26
27 /// \file usdSkel/topology.h
28
29 #include "pxr/pxr.h"
30 #include "pxr/usd/usdSkel/api.h"
31
32 #include "pxr/base/tf/span.h"
33 #include "pxr/usd/sdf/path.h"
34 #include "pxr/usd/sdf/types.h"
35
36
37 PXR_NAMESPACE_OPEN_SCOPE
38
39
40 /// \class UsdSkelTopology
41 ///
42 /// Object holding information describing skeleton topology.
43 /// This provides the hierarchical information needed to reason about joint
44 /// relationships in a manner suitable to computations.
45 class UsdSkelTopology
46 {
47 public:
48 /// Construct an empty topology.
49 UsdSkelTopology() = default;
50
51 /// Construct a skel topology from \p paths, an array holding ordered joint
52 /// paths as tokens.
53 /// Internally, each token must be converted to an SdfPath. If SdfPath
54 /// objects are already accessible, it is more efficient to use the
55 /// construct taking an SdfPath array.
56 USDSKEL_API
57 UsdSkelTopology(TfSpan<const TfToken> paths);
58
59 /// Construct a skel topology from \p paths, an array of joint paths.
60 USDSKEL_API
61 UsdSkelTopology(TfSpan<const SdfPath> paths);
62
63 /// Construct a skel topology from an array of parent indices.
64 /// For each joint, this provides the parent index of that
65 /// joint, or -1 if none.
66 USDSKEL_API
67 UsdSkelTopology(const VtIntArray& parentIndices);
68
69 /// Validate the topology.
70 /// If validation is unsuccessful, a reason
71 /// why will be written to \p reason, if provided.
72 USDSKEL_API
73 bool Validate(std::string* reason=nullptr) const;
74
GetParentIndices()75 const VtIntArray& GetParentIndices() const { return _parentIndices; }
76
GetNumJoints()77 size_t GetNumJoints() const { return size(); }
78
size()79 size_t size() const { return _parentIndices.size(); }
80
81 /// Returns the parent joint of the \p index'th joint,
82 /// Returns -1 for joints with no parent (roots).
83 inline int GetParent(size_t index) const;
84
85 /// Returns true if the \p index'th joint is a root joint.
IsRoot(size_t index)86 bool IsRoot(size_t index) const { return GetParent(index) < 0; }
87
88 bool operator==(const UsdSkelTopology& o) const;
89
90 bool operator!=(const UsdSkelTopology& o) const {
91 return !(*this == o);
92 }
93
94 private:
95 VtIntArray _parentIndices;
96 };
97
98
99 int
GetParent(size_t index)100 UsdSkelTopology::GetParent(size_t index) const
101 {
102 TF_DEV_AXIOM(index < _parentIndices.size());
103 return _parentIndices[index];
104 }
105
106
107 PXR_NAMESPACE_CLOSE_SCOPE
108
109 #endif // PXR_USD_USD_SKEL_TOPOLOGY_H
110