Loading...
Searching...
No Matches
mapFunction.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_FUNCTION_H
8#define PXR_USD_PCP_MAP_FUNCTION_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/pcp/api.h"
12#include "pxr/usd/sdf/path.h"
14#include "pxr/usd/sdf/pathExpression.h"
15
16#include <atomic>
17#include <memory>
18
19PXR_NAMESPACE_OPEN_SCOPE
20
65{
66public:
68 typedef std::map<SdfPath, SdfPath, SdfPath::FastLessThan> PathMap;
69 typedef std::pair<SdfPath, SdfPath> PathPair;
70 typedef std::vector<PathPair> PathPairVector;
71
73 PcpMapFunction() = default;
74
81 PCP_API
82 static PcpMapFunction
83 Create(const PathMap &sourceToTargetMap,
84 const SdfLayerOffset &offset);
85
91 PCP_API
92 static PcpMapFunction
93 ImpliedClass(const PcpMapFunction& transferFunc,
94 const PcpMapFunction& classArc);
95
97 PCP_API
98 static const PcpMapFunction &Identity();
99
101 PCP_API
102 static const PathMap &IdentityPathMap();
103
105 PCP_API
107 void swap(PcpMapFunction &map) { Swap(map); }
108
110 PCP_API
111 bool operator==(const PcpMapFunction &map) const;
112
114 PCP_API
115 bool operator!=(const PcpMapFunction &map) const;
116
119 PCP_API
120 bool IsNull() const;
121
124 PCP_API
125 bool IsIdentity() const;
126
130 PCP_API
132
135 bool HasRootIdentity() const { return _data.hasRootIdentity; }
136
139 PCP_API
141
144 PCP_API
146
160 PCP_API
163 const SdfPathExpression &pathExpr,
164 std::vector<SdfPathExpression::PathPattern>
165 *unmappedPatterns = nullptr,
166 std::vector<SdfPathExpression::ExpressionReference>
167 *unmappedRefs = nullptr
168 ) const;
169
183 PCP_API
186 const SdfPathExpression &pathExpr,
187 std::vector<SdfPathExpression::PathPattern>
188 *unmappedPatterns = nullptr,
189 std::vector<SdfPathExpression::ExpressionReference>
190 *unmappedRefs = nullptr
191 ) const;
192
196 PCP_API
198
202 PCP_API
204
208 PCP_API
210
212 PCP_API
214
216 const SdfLayerOffset &GetTimeOffset() const { return _offset; }
217
220 PCP_API
221 std::string GetString() const;
222
224 PCP_API
225 size_t Hash() const;
226
227private:
228
229 PCP_API
230 PcpMapFunction(PathPair const *sourceToTargetBegin,
231 PathPair const *sourceToTargetEnd,
232 SdfLayerOffset offset,
233 bool hasRootIdentity);
234
235 PCP_API
237 _MapPathExpressionImpl(
238 bool invert,
239 const SdfPathExpression &pathExpr,
240 std::vector<SdfPathExpression::PathPattern> *unmappedPatterns,
241 std::vector<SdfPathExpression::ExpressionReference> *unmappedRefs
242 ) const;
243
244private:
245 friend PcpMapFunction *Pcp_MakeIdentity();
246
247 static const int _MaxLocalPairs = 2;
248 struct _Data final {
249 _Data() {};
250
251 _Data(PathPair const *begin, PathPair const *end, bool hasRootIdentity)
252 : numPairs(end-begin)
253 , hasRootIdentity(hasRootIdentity) {
254 if (numPairs == 0)
255 return;
256 if (numPairs <= _MaxLocalPairs) {
257 std::uninitialized_copy(begin, end, localPairs);
258 }
259 else {
260 new (&remotePairs) std::shared_ptr<PathPair>(
261 new PathPair[numPairs], std::default_delete<PathPair[]>());
262 std::copy(begin, end, remotePairs.get());
263 }
264 }
265
266 _Data(_Data const &other)
267 : numPairs(other.numPairs)
268 , hasRootIdentity(other.hasRootIdentity) {
269 if (numPairs <= _MaxLocalPairs) {
270 std::uninitialized_copy(
271 other.localPairs,
272 other.localPairs + other.numPairs, localPairs);
273 }
274 else {
275 new (&remotePairs) std::shared_ptr<PathPair>(other.remotePairs);
276 }
277 }
278 _Data(_Data &&other)
279 : numPairs(other.numPairs)
280 , hasRootIdentity(other.hasRootIdentity) {
281 if (numPairs <= _MaxLocalPairs) {
282 PathPair *dst = localPairs;
283 PathPair *src = other.localPairs;
284 PathPair *srcEnd = other.localPairs + other.numPairs;
285 for (; src != srcEnd; ++src, ++dst) {
286 ::new (static_cast<void*>(std::addressof(*dst)))
287 PathPair(std::move(*src));
288 }
289 }
290 else {
291 new (&remotePairs)
292 std::shared_ptr<PathPair>(std::move(other.remotePairs));
293 }
294 }
295 _Data &operator=(_Data const &other) {
296 if (this != &other) {
297 this->~_Data();
298 new (this) _Data(other);
299 }
300 return *this;
301 }
302 _Data &operator=(_Data &&other) {
303 if (this != &other) {
304 this->~_Data();
305 new (this) _Data(std::move(other));
306 }
307 return *this;
308 }
309 ~_Data() {
310 if (numPairs <= _MaxLocalPairs) {
311 for (PathPair *p = localPairs; numPairs--; ++p) {
312 p->~PathPair();
313 }
314 }
315 else {
316 remotePairs.~shared_ptr<PathPair>();
317 }
318 }
319
320 bool IsNull() const {
321 return numPairs == 0 && !hasRootIdentity;
322 }
323
324 PathPair const *begin() const {
325 return numPairs <= _MaxLocalPairs ? localPairs : remotePairs.get();
326 }
327
328 PathPair const *end() const {
329 return begin() + numPairs;
330 }
331
332 bool operator==(_Data const &other) const {
333 return numPairs == other.numPairs &&
334 hasRootIdentity == other.hasRootIdentity &&
335 std::equal(begin(), end(), other.begin());
336 }
337
338 bool operator!=(_Data const &other) const {
339 return !(*this == other);
340 }
341
342 template <class HashState>
343 friend void TfHashAppend(HashState &h, _Data const &data){
344 h.Append(data.hasRootIdentity);
345 h.Append(data.numPairs);
346 h.AppendRange(std::begin(data), std::end(data));
347 }
348
349 union {
350 PathPair localPairs[_MaxLocalPairs > 0 ? _MaxLocalPairs : 1];
351 std::shared_ptr<PathPair> remotePairs;
352 };
353 typedef int PairCount;
354 PairCount numPairs = 0;
355 bool hasRootIdentity = false;
356 };
357
358 // Specialize TfHashAppend for PcpMapFunction.
359 template <typename HashState>
360 friend inline
361 void TfHashAppend(HashState& h, const PcpMapFunction& x){
362 h.Append(x._data);
363 h.Append(x._offset);
364 }
365
366 _Data _data;
367 SdfLayerOffset _offset;
368};
369
370// Specialize hash_value for PcpMapFunction.
371inline
372size_t hash_value(const PcpMapFunction& x)
373{
374 return TfHash{}(x);
375}
376
377PXR_NAMESPACE_CLOSE_SCOPE
378
379#endif // PXR_USD_PCP_MAP_FUNCTION_H
A function that maps values from one namespace (and time domain) to another.
Definition: mapFunction.h:65
static PCP_API PcpMapFunction Create(const PathMap &sourceToTargetMap, const SdfLayerOffset &offset)
Constructs a map function with the given arguments.
PCP_API void Swap(PcpMapFunction &map)
Swap the contents of this map function with map.
PCP_API SdfPathExpression MapTargetToSource(const SdfPathExpression &pathExpr, std::vector< SdfPathExpression::PathPattern > *unmappedPatterns=nullptr, std::vector< SdfPathExpression::ExpressionReference > *unmappedRefs=nullptr) const
Map all path pattern prefix paths and expression reference paths in the target namespace to the sourc...
PCP_API SdfPath MapSourceToTarget(const SdfPath &path) const
Map a path in the source namespace to the target.
bool HasRootIdentity() const
Return true if the map function maps the absolute root path to the absolute root path,...
Definition: mapFunction.h:135
static PCP_API PcpMapFunction ImpliedClass(const PcpMapFunction &transferFunc, const PcpMapFunction &classArc)
Constructs a map function that is equivalent to.
PCP_API bool IsIdentity() const
Return true if the map function is the identity function.
PCP_API size_t Hash() const
Return a size_t hash for this map function.
PCP_API SdfPathExpression MapSourceToTarget(const SdfPathExpression &pathExpr, std::vector< SdfPathExpression::PathPattern > *unmappedPatterns=nullptr, std::vector< SdfPathExpression::ExpressionReference > *unmappedRefs=nullptr) const
Map all path pattern prefix paths and expression reference paths in the source namespace to the targe...
PCP_API std::string GetString() const
Returns a string representation of this mapping for debugging purposes.
static PCP_API const PathMap & IdentityPathMap()
Returns an identity path mapping.
PcpMapFunction()=default
Construct a null function.
PCP_API SdfPath MapTargetToSource(const SdfPath &path) const
Map a path in the target namespace to the source.
PCP_API bool operator!=(const PcpMapFunction &map) const
Inequality.
const SdfLayerOffset & GetTimeOffset() const
The time offset of the mapping.
Definition: mapFunction.h:216
std::map< SdfPath, SdfPath, SdfPath::FastLessThan > PathMap
A mapping from path to path.
Definition: mapFunction.h:68
static PCP_API const PcpMapFunction & Identity()
Construct an identity map function.
PCP_API PcpMapFunction ComposeOffset(const SdfLayerOffset &newOffset) const
Compose this map function over a hypothetical map function that has an identity path mapping and offs...
PCP_API PcpMapFunction GetInverse() const
Return the inverse of this map function.
PCP_API PcpMapFunction Compose(const PcpMapFunction &f) const
Compose this map over the given map function.
PCP_API bool IsIdentityPathMapping() const
Return true if the map function uses the identity path mapping.
PCP_API bool operator==(const PcpMapFunction &map) const
Equality.
PCP_API bool IsNull() const
Return true if this map function is the null function.
PCP_API PathMap GetSourceToTargetMap() const
The set of path mappings, from source to target.
Represents a time offset and scale between layers.
Definition: layerOffset.h:44
Objects of this class represent a logical expression syntax tree consisting of SdfPathPattern s,...
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:281
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:472
std::size_t hash_value(const half h)
Overload hash_value for half.
Definition: half.h:30