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/atomic.h>
34#include <tbb/spin_mutex.h>
35
36#include <atomic>
37#include <memory>
38
39PXR_NAMESPACE_OPEN_SCOPE
40
57{
58public:
61
66 PCP_API
67 const Value & Evaluate() const;
68
70 PcpMapExpression() noexcept = default;
71
72 ~PcpMapExpression() noexcept = default;
73
75 void Swap(PcpMapExpression &other) noexcept {
76 _node.swap(other._node);
77 }
78
80 bool IsNull() const noexcept {
81 return !_node;
82 }
83
86
88 PCP_API
90
92 PCP_API
93 static PcpMapExpression Constant( const Value & constValue );
94
98 class Variable {
99 Variable(Variable const &) = delete;
100 Variable &operator=(Variable const &) = delete;
101 public:
102 Variable() = default;
103 virtual ~Variable();
105 virtual const Value & GetValue() const = 0;
108 virtual void SetValue(Value && value) = 0;
111 virtual PcpMapExpression GetExpression() const = 0;
112 };
113
115 typedef std::unique_ptr<Variable> VariableUniquePtr;
116
123 PCP_API
124 static VariableUniquePtr NewVariable(Value && initialValue);
125
128 PCP_API
130
132 PCP_API
134
137 PCP_API
139
141 bool IsConstantIdentity() const {
142 return _node && _node->key.op == _OpConstant &&
143 _node->key.valueForConstant.IsIdentity();
144 }
145
147
152
155 bool IsIdentity() const {
156 return Evaluate().IsIdentity();
157 }
158
161 SdfPath MapSourceToTarget(const SdfPath &path) const {
162 return Evaluate().MapSourceToTarget(path);
163 }
164
167 SdfPath MapTargetToSource(const SdfPath &path) const {
168 return Evaluate().MapTargetToSource(path);
169 }
170
173 return Evaluate().GetTimeOffset();
174 }
175
178 std::string GetString() const {
179 return Evaluate().GetString();
180 }
181
183
184private:
185 // Allow Pcp_Statistics access to internal data for diagnostics.
186 friend class Pcp_Statistics;
187 friend struct Pcp_VariableImpl;
188
189 class _Node;
190 using _NodeRefPtr = TfDelegatedCountPtr<_Node>;
191
192 explicit PcpMapExpression(const _NodeRefPtr & node) : _node(node) {}
193
194private: // data
195 enum _Op {
196 _OpConstant,
197 _OpVariable,
198 _OpInverse,
199 _OpCompose,
200 _OpAddRootIdentity
201 };
202
203 class _Node {
204 _Node(const _Node&) = delete;
205 _Node& operator=(const _Node&) = delete;
206
207 // Ref-counting ops manage _refCount.
208 // Need to friend them here to have access to _refCount.
209 friend PCP_API void TfDelegatedCountIncrement(_Node*);
210 friend PCP_API void TfDelegatedCountDecrement(_Node*) noexcept;
211 public:
212 // The Key holds all the state needed to uniquely identify
213 // this (sub-)expression.
214 struct Key {
215 const _Op op;
216 const _NodeRefPtr arg1, arg2;
217 const Value valueForConstant;
218
219 Key( _Op op_,
220 const _NodeRefPtr & arg1_,
221 const _NodeRefPtr & arg2_,
222 const Value & valueForConstant_ )
223 : op(op_)
224 , arg1(arg1_)
225 , arg2(arg2_)
226 , valueForConstant(valueForConstant_)
227 {}
228 inline size_t GetHash() const;
229 bool operator==(const Key &key) const;
230 };
231
232 // The Key of a node is const, and established when it is created.
233 const Key key;
234
235 // Whether or not the expression tree up to and including this node
236 // will always include an identity mapping.
237 const bool expressionTreeAlwaysHasIdentity;
238
239 // Factory method to create new nodes.
240 static _NodeRefPtr
241 New( _Op op,
242 const _NodeRefPtr & arg1 = _NodeRefPtr(),
243 const _NodeRefPtr & arg2 = _NodeRefPtr(),
244 const Value & valueForConstant = Value() );
245 ~_Node();
246
247 // Evaluate (and internally cache) the value of this node.
248 const Value & EvaluateAndCache() const;
249
250 // For _OpVariable nodes, sets the variable's value.
251 void SetValueForVariable(Value &&newValue);
252
253 // For _OpVariable nodes, returns the variable's value.
254 const Value & GetValueForVariable() const {
255 return _valueForVariable;
256 }
257
258 private:
259 explicit _Node( const Key &key_ );
260 void _Invalidate();
261 Value _EvaluateUncached() const;
262
263 // Helper to determine if the expression tree indicated by key
264 // will always contains the root identity.
265 static bool _ExpressionTreeAlwaysHasIdentity(const Key& key);
266
267 // Registry of node instances, identified by Key.
268 // Note: variable nodes are not tracked by the registry.
269 struct _NodeMap;
270 static TfStaticData<_NodeMap> _nodeRegistry;
271
272 mutable tbb::atomic<int> _refCount;
273 mutable Value _cachedValue;
274 mutable std::set<_Node*> _dependentExpressions;
275 Value _valueForVariable;
276 mutable tbb::spin_mutex _mutex;
277 mutable std::atomic<bool> _hasCachedValue;
278 };
279
280 // Need to friend them here to have visibility to private class _Node.
281 friend PCP_API void TfDelegatedCountIncrement(_Node*);
282 friend PCP_API void TfDelegatedCountDecrement(_Node*) noexcept;
283
284 _NodeRefPtr _node;
285};
286
287PXR_NAMESPACE_CLOSE_SCOPE
288
289#endif // PXR_USD_PCP_MAP_EXPRESSION_H
A Variable is a mutable memory cell that holds a value.
Definition: mapExpression.h:98
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:57
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:80
void Swap(PcpMapExpression &other) noexcept
Swap this expression with the other.
Definition: mapExpression.h:75
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:60
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