Loading...
Searching...
No Matches
mapExpression.h
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_PCP_MAP_EXPRESSION_H
25#define PXR_USD_PCP_MAP_EXPRESSION_H
26
27#include "pxr/pxr.h"
28#include "pxr/usd/pcp/api.h"
29#include "pxr/usd/pcp/mapFunction.h"
30
31#include "pxr/base/tf/delegatedCountPtr.h"
32
33#include <tbb/spin_mutex.h>
34
35#include <atomic>
36#include <memory>
37
38PXR_NAMESPACE_OPEN_SCOPE
39
56{
57public:
60
65 PCP_API
66 const Value & Evaluate() const;
67
69 PcpMapExpression() noexcept = default;
70
71 ~PcpMapExpression() noexcept = default;
72
74 void Swap(PcpMapExpression &other) noexcept {
75 _node.swap(other._node);
76 }
77
79 bool IsNull() const noexcept {
80 return !_node;
81 }
82
85
87 PCP_API
89
91 PCP_API
92 static PcpMapExpression Constant( const Value & constValue );
93
97 class Variable {
98 Variable(Variable const &) = delete;
99 Variable &operator=(Variable const &) = delete;
100 public:
101 Variable() = default;
102 virtual ~Variable();
104 virtual const Value & GetValue() const = 0;
107 virtual void SetValue(Value && value) = 0;
110 virtual PcpMapExpression GetExpression() const = 0;
111 };
112
114 typedef std::unique_ptr<Variable> VariableUniquePtr;
115
122 PCP_API
123 static VariableUniquePtr NewVariable(Value && initialValue);
124
127 PCP_API
129
131 PCP_API
133
136 PCP_API
138
140 bool IsConstantIdentity() const {
141 return _node && _node->key.op == _OpConstant &&
142 _node->key.valueForConstant.IsIdentity();
143 }
144
146
151
154 bool IsIdentity() const {
155 return Evaluate().IsIdentity();
156 }
157
160 SdfPath MapSourceToTarget(const SdfPath &path) const {
161 return Evaluate().MapSourceToTarget(path);
162 }
163
166 SdfPath MapTargetToSource(const SdfPath &path) const {
167 return Evaluate().MapTargetToSource(path);
168 }
169
172 return Evaluate().GetTimeOffset();
173 }
174
177 std::string GetString() const {
178 return Evaluate().GetString();
179 }
180
182
183private:
184 // Allow Pcp_Statistics access to internal data for diagnostics.
185 friend class Pcp_Statistics;
186 friend struct Pcp_VariableImpl;
187
188 class _Node;
189 using _NodeRefPtr = TfDelegatedCountPtr<_Node>;
190
191 explicit PcpMapExpression(const _NodeRefPtr & node) : _node(node) {}
192
193private: // data
194 enum _Op {
195 _OpConstant,
196 _OpVariable,
197 _OpInverse,
198 _OpCompose,
199 _OpAddRootIdentity
200 };
201
202 class _Node {
203 _Node(const _Node&) = delete;
204 _Node& operator=(const _Node&) = delete;
205
206 // Ref-counting ops manage _refCount.
207 // Need to friend them here to have access to _refCount.
208 friend PCP_API void TfDelegatedCountIncrement(_Node*);
209 friend PCP_API void TfDelegatedCountDecrement(_Node*) noexcept;
210 public:
211 // The Key holds all the state needed to uniquely identify
212 // this (sub-)expression.
213 struct Key {
214 const _Op op;
215 const _NodeRefPtr arg1, arg2;
216 const Value valueForConstant;
217
218 Key( _Op op_,
219 const _NodeRefPtr & arg1_,
220 const _NodeRefPtr & arg2_,
221 const Value & valueForConstant_ )
222 : op(op_)
223 , arg1(arg1_)
224 , arg2(arg2_)
225 , valueForConstant(valueForConstant_)
226 {}
227 inline size_t GetHash() const;
228 bool operator==(const Key &key) const;
229 };
230
231 // The Key of a node is const, and established when it is created.
232 const Key key;
233
234 // Whether or not the expression tree up to and including this node
235 // will always include an identity mapping.
236 const bool expressionTreeAlwaysHasIdentity;
237
238 // Factory method to create new nodes.
239 static _NodeRefPtr
240 New( _Op op,
241 const _NodeRefPtr & arg1 = _NodeRefPtr(),
242 const _NodeRefPtr & arg2 = _NodeRefPtr(),
243 const Value & valueForConstant = Value() );
244 ~_Node();
245
246 // Evaluate (and internally cache) the value of this node.
247 const Value & EvaluateAndCache() const;
248
249 // For _OpVariable nodes, sets the variable's value.
250 void SetValueForVariable(Value &&newValue);
251
252 // For _OpVariable nodes, returns the variable's value.
253 const Value & GetValueForVariable() const {
254 return _valueForVariable;
255 }
256
257 private:
258 explicit _Node( const Key &key_ );
259 void _Invalidate();
260 Value _EvaluateUncached() const;
261
262 // Helper to determine if the expression tree indicated by key
263 // will always contains the root identity.
264 static bool _ExpressionTreeAlwaysHasIdentity(const Key& key);
265
266 // Registry of node instances, identified by Key.
267 // Note: variable nodes are not tracked by the registry.
268 struct _NodeMap;
269 static TfStaticData<_NodeMap> _nodeRegistry;
270
271 mutable std::atomic<int> _refCount;
272 mutable Value _cachedValue;
273 mutable std::set<_Node*> _dependentExpressions;
274 Value _valueForVariable;
275 mutable tbb::spin_mutex _mutex;
276 mutable std::atomic<bool> _hasCachedValue;
277 };
278
279 // Need to friend them here to have visibility to private class _Node.
280 friend PCP_API void TfDelegatedCountIncrement(_Node*);
281 friend PCP_API void TfDelegatedCountDecrement(_Node*) noexcept;
282
283 _NodeRefPtr _node;
284};
285
286PXR_NAMESPACE_CLOSE_SCOPE
287
288#endif // PXR_USD_PCP_MAP_EXPRESSION_H
A Variable is a mutable memory cell that holds a value.
Definition: mapExpression.h:97
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:56
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:79
void Swap(PcpMapExpression &other) noexcept
Swap this expression with the other.
Definition: mapExpression.h:74
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.
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.
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:59
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:82
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:223
Represents a time offset and scale between layers.
Definition: layerOffset.h:61
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
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:113