HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
xformCache.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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_GEOM_XFORM_CACHE_H
8 #define PXR_USD_USD_GEOM_XFORM_CACHE_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/usd/usdGeom/api.h"
13 #include "pxr/usd/usd/prim.h"
14 
16 
17 #include "pxr/base/gf/matrix4d.h"
18 #include "pxr/base/tf/hash.h"
19 #include "pxr/base/tf/hashmap.h"
20 #include "pxr/base/tf/token.h"
21 
23 
24 
25 /// \class UsdGeomXformCache
26 ///
27 /// A caching mechanism for transform matrices. For best performance, this
28 /// object should be reused for multiple CTM queries.
29 ///
30 /// Instances of this type can be copied, though using Swap() may result in
31 /// better performance.
32 ///
33 /// It is valid to cache prims from multiple stages in a single XformCache.
34 ///
35 /// WARNING: this class does not automatically invalidate cached values based
36 /// on changes to the stage from which values were cached. Additionally, a
37 /// separate instance of this class should be used per-thread, calling the Get*
38 /// methods from multiple threads is not safe, as they mutate internal state.
39 ///
41 {
42 public:
43  /// Construct a new XformCache for the specified \p time.
45  explicit UsdGeomXformCache(const UsdTimeCode time);
46 
47  /// Construct a new XformCache for UsdTimeCode::Default().
50 
51  /// Compute the transformation matrix for the given \p prim, including the
52  /// transform authored on the Prim itself, if present.
53  ///
54  /// \note This method may mutate internal cache state and is not thread
55  /// safe.
58 
59  /// Compute the transformation matrix for the given \p prim, but do NOT
60  /// include the transform authored on the prim itself.
61  ///
62  /// \note This method may mutate internal cache state and is not thread
63  /// safe.
66 
67  /// Returns the local transformation of the prim. Uses the cached
68  /// XformQuery to compute the result quickly. The \p resetsXformStack
69  /// pointer must be valid. It will be set to true if \p prim resets
70  /// the transform stack.
71  /// The result of this call is cached.
74  bool *resetsXformStack);
75 
76  /// Returns the result of concatenating all transforms beneath \p ancestor
77  /// that affect \p prim. This includes the local transform of \p prim
78  /// itself, but not the local transform of \p ancestor. If \p ancestor is
79  /// not an ancestor of \p prim, the resulting transform is the
80  /// local-to-world transformation of \p prim.
81  /// The \p resetXformTsack pointer must be valid. If any intermediate prims
82  /// reset the transform stack, \p resetXformStack will be set to true.
83  /// Intermediate transforms are cached, but the result of this call itself
84  /// is not cached.
87  const UsdPrim &ancestor,
88  bool *resetXformStack);
89 
90  /// Whether the attribute named \p attrName, belonging to the
91  /// given \p prim affects the local transform value at the prim.
92  ///
93  /// \note This method may mutate internal cache state and is not thread
94  /// safe.
97  const TfToken &attrName);
98 
99  /// Whether the local transformation value at the prim may vary over time.
100  ///
101  /// \note This method may mutate internal cache state and is not thread
102  /// safe.
104  bool TransformMightBeTimeVarying(const UsdPrim &prim);
105 
106  /// Whether the xform stack is reset at the given prim.
107  ///
108  /// \note This method may mutate internal cache state and is not thread
109  /// safe.
111  bool GetResetXformStack(const UsdPrim &prim);
112 
113  /// Clears all pre-cached values.
115  void Clear();
116 
117  /// Use the new \p time when computing values and may clear any existing
118  /// values cached for the previous time. Setting \p time to the current time
119  /// is a no-op.
121  void SetTime(UsdTimeCode time);
122 
123  /// Get the current time from which this cache is reading values.
124  UsdTimeCode GetTime() { return _time; }
125 
126  /// Swap the contents of this XformCache with \p other.
128  void Swap(UsdGeomXformCache& other);
129 
130 private:
131 
132  // Traverses backwards the hierarchy starting from prim
133  // all the way to the root and computes the ctm
134  GfMatrix4d const* _GetCtm(const UsdPrim& prim);
135 
136  // Map of cached values.
137  struct _Entry {
138  _Entry() = default;
139  _Entry(const UsdGeomXformable::XformQuery & query_,
140  const GfMatrix4d& ctm_,
141  bool ctmIsValid_)
142  : query(query_)
143  , ctm(ctm_)
144  , ctmIsValid(ctmIsValid_)
145  { }
146 
148  GfMatrix4d ctm;
149  bool ctmIsValid;
150  };
151 
152  // Helper function to get or create a new entry for a prim in the ctm cache.
153  _Entry * _GetCacheEntryForPrim(const UsdPrim &prim);
154 
155  typedef TfHashMap<UsdPrim, _Entry, TfHash> _PrimHashMap;
156  _PrimHashMap _ctmCache;
157 
158  // The time at which this stack is querying and caching attribute values.
159  UsdTimeCode _time;
160 };
161 
162 #define USDGEOM_XFORM_CACHE_API_VERSION 1
163 
164 
166 
167 #endif // PXR_USD_USD_GEOM_XFORM_CACHE_H
GLenum query
Definition: glad.h:2772
USDGEOM_API GfMatrix4d GetLocalTransformation(const UsdPrim &prim, bool *resetsXformStack)
GT_API const UT_StringHolder time
UsdTimeCode GetTime()
Get the current time from which this cache is reading values.
Definition: xformCache.h:124
USDGEOM_API void SetTime(UsdTimeCode time)
USDGEOM_API bool GetResetXformStack(const UsdPrim &prim)
Definition: token.h:70
USDGEOM_API void Clear()
Clears all pre-cached values.
USDGEOM_API bool TransformMightBeTimeVarying(const UsdPrim &prim)
USDGEOM_API GfMatrix4d GetLocalToWorldTransform(const UsdPrim &prim)
Definition: prim.h:116
USDGEOM_API UsdGeomXformCache()
Construct a new XformCache for UsdTimeCode::Default().
USDGEOM_API GfMatrix4d GetParentToWorldTransform(const UsdPrim &prim)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
USDGEOM_API GfMatrix4d ComputeRelativeTransform(const UsdPrim &prim, const UsdPrim &ancestor, bool *resetXformStack)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
USDGEOM_API bool IsAttributeIncludedInLocalTransform(const UsdPrim &prim, const TfToken &attrName)
#define USDGEOM_API
Definition: api.h:23
USDGEOM_API void Swap(UsdGeomXformCache &other)
Swap the contents of this XformCache with other.