Loading...
Searching...
No Matches
mapExpression.h
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_PCP_MAP_EXPRESSION_H
8#define PXR_USD_PCP_MAP_EXPRESSION_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/pcp/api.h"
12#include "pxr/usd/pcp/mapFunction.h"
13
14#include "pxr/base/tf/delegatedCountPtr.h"
15
16#include <tbb/spin_mutex.h>
17
18#include <atomic>
19#include <memory>
20
21PXR_NAMESPACE_OPEN_SCOPE
22
39{
40public:
43
48 PCP_API
49 const Value & Evaluate() const;
50
52 PcpMapExpression() noexcept = default;
53
54 ~PcpMapExpression() noexcept = default;
55
57 void Swap(PcpMapExpression &other) noexcept {
58 _node.swap(other._node);
59 }
60
62 bool IsNull() const noexcept {
63 return !_node;
64 }
65
68
70 PCP_API
72
74 PCP_API
75 static PcpMapExpression Constant( const Value & constValue );
76
80 class Variable {
81 Variable(Variable const &) = delete;
82 Variable &operator=(Variable const &) = delete;
83 public:
84 Variable() = default;
85 virtual ~Variable();
87 virtual const Value & GetValue() const = 0;
90 virtual void SetValue(Value && value) = 0;
93 virtual PcpMapExpression GetExpression() const = 0;
94 };
95
97 typedef std::unique_ptr<Variable> VariableUniquePtr;
98
105 PCP_API
106 static VariableUniquePtr NewVariable(Value && initialValue);
107
113 PCP_API
115 const PcpMapExpression& transferFunc,
116 const PcpMapExpression& classArc);
117
120 PCP_API
122
124 PCP_API
126
129 PCP_API
131
133 bool IsConstantIdentity() const {
134 return _node && _node->key.op == _OpConstant &&
135 _node->key.valueForConstant.IsIdentity();
136 }
137
139
144
147 bool IsIdentity() const {
148 return Evaluate().IsIdentity();
149 }
150
153 SdfPath MapSourceToTarget(const SdfPath &path) const {
154 return Evaluate().MapSourceToTarget(path);
155 }
156
159 SdfPath MapTargetToSource(const SdfPath &path) const {
160 return Evaluate().MapTargetToSource(path);
161 }
162
165 return Evaluate().GetTimeOffset();
166 }
167
170 std::string GetString() const {
171 return Evaluate().GetString();
172 }
173
175
176private:
177 // Allow Pcp_Statistics access to internal data for diagnostics.
178 friend class Pcp_Statistics;
179 friend struct Pcp_VariableImpl;
180
181 class _Node;
182 using _NodeRefPtr = TfDelegatedCountPtr<_Node>;
183
184 explicit PcpMapExpression(const _NodeRefPtr & node) : _node(node) {}
185
186private: // data
187 enum _Op {
188 _OpConstant,
189 _OpVariable,
190 _OpInverse,
191 _OpCompose,
192 _OpAddRootIdentity,
193 _OpImpliedClass
194 };
195
196 class _Node {
197 _Node(const _Node&) = delete;
198 _Node& operator=(const _Node&) = delete;
199
200 // Ref-counting ops manage _refCount.
201 // Need to friend them here to have access to _refCount.
202 friend PCP_API void TfDelegatedCountIncrement(_Node*);
203 friend PCP_API void TfDelegatedCountDecrement(_Node*) noexcept;
204 public:
205 // The Key holds all the state needed to uniquely identify
206 // this (sub-)expression.
207 struct Key {
208 const _Op op;
209 const _NodeRefPtr arg1, arg2;
210 const Value valueForConstant;
211
212 Key( _Op op_,
213 const _NodeRefPtr & arg1_,
214 const _NodeRefPtr & arg2_,
215 const Value & valueForConstant_ )
216 : op(op_)
217 , arg1(arg1_)
218 , arg2(arg2_)
219 , valueForConstant(valueForConstant_)
220 {}
221 inline size_t GetHash() const;
222 bool operator==(const Key &key) const;
223 };
224
225 // The Key of a node is const, and established when it is created.
226 const Key key;
227
228 // Whether or not the expression tree up to and including this node
229 // will always include an identity mapping.
230 const bool expressionTreeAlwaysHasIdentity;
231
232 // Factory method to create new nodes.
233 static _NodeRefPtr
234 New( _Op op,
235 const _NodeRefPtr & arg1 = _NodeRefPtr(),
236 const _NodeRefPtr & arg2 = _NodeRefPtr(),
237 const Value & valueForConstant = Value() );
238 ~_Node();
239
240 // Evaluate (and internally cache) the value of this node.
241 const Value & EvaluateAndCache() const;
242
243 // For _OpVariable nodes, sets the variable's value.
244 void SetValueForVariable(Value &&newValue);
245
246 // For _OpVariable nodes, returns the variable's value.
247 const Value & GetValueForVariable() const {
248 return _valueForVariable;
249 }
250
251 private:
252 explicit _Node( const Key &key_ );
253 void _Invalidate();
254 Value _EvaluateUncached() const;
255
256 // Helper to determine if the expression tree indicated by key
257 // will always contains the root identity.
258 static bool _ExpressionTreeAlwaysHasIdentity(const Key& key);
259
260 // Registry of node instances, identified by Key.
261 // Note: variable nodes are not tracked by the registry.
262 struct _NodeMap;
263 static TfStaticData<_NodeMap> _nodeRegistry;
264
265 mutable std::atomic<int> _refCount;
266 mutable Value _cachedValue;
267 mutable std::set<_Node*> _dependentExpressions;
268 Value _valueForVariable;
269 mutable tbb::spin_mutex _mutex;
270 mutable std::atomic<bool> _hasCachedValue;
271 };
272
273 // Need to friend them here to have visibility to private class _Node.
274 friend PCP_API void TfDelegatedCountIncrement(_Node*);
275 friend PCP_API void TfDelegatedCountDecrement(_Node*) noexcept;
276
277 _NodeRefPtr _node;
278};
279
280PXR_NAMESPACE_CLOSE_SCOPE
281
282#endif // PXR_USD_PCP_MAP_EXPRESSION_H
A Variable is a mutable memory cell that holds a value.
Definition: mapExpression.h:80
virtual PcpMapExpression GetExpression() const =0
Return an expression representing the value of this variable.
virtual const Value & GetValue() const =0
Return the current value.
virtual void SetValue(Value &&value)=0
Mutate the variable to have the new value.
An expression that yields a PcpMapFunction value.
Definition: mapExpression.h:39
SdfPath MapTargetToSource(const SdfPath &path) const
Map a path in the target namespace to the source.
bool IsIdentity() const
Return true if the evaluated map function is the identity function.
bool IsConstantIdentity() const
Return true if the map function is the constant identity function.
bool IsNull() const noexcept
Return true if this is a null expression.
Definition: mapExpression.h:62
void Swap(PcpMapExpression &other) noexcept
Swap this expression with the other.
Definition: mapExpression.h:57
static PCP_API PcpMapExpression Identity()
Return an expression representing PcpMapFunction::Identity().
static PCP_API PcpMapExpression Constant(const Value &constValue)
Create a new constant.
std::unique_ptr< Variable > VariableUniquePtr
Variables are held by reference.
Definition: mapExpression.h:97
PCP_API PcpMapExpression Inverse() const
Create a new PcpMapExpression representing the inverse of f.
std::string GetString() const
Returns a string representation of this mapping for debugging purposes.
PCP_API const Value & Evaluate() const
Evaluate this expression, yielding a PcpMapFunction value.
static PCP_API PcpMapExpression ImpliedClass(const PcpMapExpression &transferFunc, const PcpMapExpression &classArc)
Create a new PcpMapExpression equivalent to:
PCP_API PcpMapExpression AddRootIdentity() const
Return a new expression representing this expression with an added (if necessary) mapping from </> to...
PCP_API PcpMapExpression Compose(const PcpMapExpression &f) const
Create a new PcpMapExpression representing the application of f's value, followed by the application ...
const SdfLayerOffset & GetTimeOffset() const
The time offset of the mapping.
static PCP_API VariableUniquePtr NewVariable(Value &&initialValue)
Create a new variable.
PcpMapFunction Value
The value type of PcpMapExpression is a PcpMapFunction.
Definition: mapExpression.h:42
SdfPath MapSourceToTarget(const SdfPath &path) const
Map a path in the source namespace to the target.
PcpMapExpression() noexcept=default
Default-construct a NULL expression.
A function that maps values from one namespace (and time domain) to another.
Definition: mapFunction.h:65
PCP_API SdfPath MapSourceToTarget(const SdfPath &path) const
Map a path in the source namespace to the target.
PCP_API bool IsIdentity() const
Return true if the map function is the identity function.
PCP_API std::string GetString() const
Returns a string representation of this mapping for debugging purposes.
PCP_API SdfPath MapTargetToSource(const SdfPath &path) const
Map a path in the target namespace to the source.
const SdfLayerOffset & GetTimeOffset() const
The time offset of the mapping.
Definition: mapFunction.h:216
Represents a time offset and scale between layers.
Definition: layerOffset.h:44
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:281
Stores a pointer to a ValueType which uses TfDelegatedCountIncrement and TfDelegatedCountDecrement to...
void swap(TfDelegatedCountPtr &other) noexcept
Swap this object's held pointer with other's.
Create or return a previously created object instance of global data.
Definition: staticData.h:96