Loading...
Searching...
No Matches
mapFunction.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_FUNCTION_H
25#define PXR_USD_PCP_MAP_FUNCTION_H
26
27#include "pxr/pxr.h"
28#include "pxr/usd/pcp/api.h"
29#include "pxr/usd/sdf/path.h"
31#include "pxr/usd/sdf/pathExpression.h"
32
33#include <atomic>
34#include <memory>
35
36PXR_NAMESPACE_OPEN_SCOPE
37
82{
83public:
85 typedef std::map<SdfPath, SdfPath, SdfPath::FastLessThan> PathMap;
86 typedef std::pair<SdfPath, SdfPath> PathPair;
87 typedef std::vector<PathPair> PathPairVector;
88
90 PcpMapFunction() = default;
91
98 PCP_API
99 static PcpMapFunction
100 Create(const PathMap &sourceToTargetMap,
101 const SdfLayerOffset &offset);
102
104 PCP_API
105 static const PcpMapFunction &Identity();
106
108 PCP_API
109 static const PathMap &IdentityPathMap();
110
112 PCP_API
114 void swap(PcpMapFunction &map) { Swap(map); }
115
117 PCP_API
118 bool operator==(const PcpMapFunction &map) const;
119
121 PCP_API
122 bool operator!=(const PcpMapFunction &map) const;
123
126 PCP_API
127 bool IsNull() const;
128
131 PCP_API
132 bool IsIdentity() const;
133
137 PCP_API
139
142 bool HasRootIdentity() const { return _data.hasRootIdentity; }
143
146 PCP_API
148
151 PCP_API
153
167 PCP_API
170 const SdfPathExpression &pathExpr,
171 std::vector<SdfPathExpression::PathPattern>
172 *unmappedPatterns = nullptr,
173 std::vector<SdfPathExpression::ExpressionReference>
174 *unmappedRefs = nullptr
175 ) const;
176
190 PCP_API
193 const SdfPathExpression &pathExpr,
194 std::vector<SdfPathExpression::PathPattern>
195 *unmappedPatterns = nullptr,
196 std::vector<SdfPathExpression::ExpressionReference>
197 *unmappedRefs = nullptr
198 ) const;
199
203 PCP_API
205
209 PCP_API
211
215 PCP_API
217
219 PCP_API
221
223 const SdfLayerOffset &GetTimeOffset() const { return _offset; }
224
227 PCP_API
228 std::string GetString() const;
229
231 PCP_API
232 size_t Hash() const;
233
234private:
235
236 PCP_API
237 PcpMapFunction(PathPair const *sourceToTargetBegin,
238 PathPair const *sourceToTargetEnd,
239 SdfLayerOffset offset,
240 bool hasRootIdentity);
241
242 PCP_API
244 _MapPathExpressionImpl(
245 bool invert,
246 const SdfPathExpression &pathExpr,
247 std::vector<SdfPathExpression::PathPattern> *unmappedPatterns,
248 std::vector<SdfPathExpression::ExpressionReference> *unmappedRefs
249 ) const;
250
251private:
252 friend PcpMapFunction *Pcp_MakeIdentity();
253
254 static const int _MaxLocalPairs = 2;
255 struct _Data final {
256 _Data() {};
257
258 _Data(PathPair const *begin, PathPair const *end, bool hasRootIdentity)
259 : numPairs(end-begin)
260 , hasRootIdentity(hasRootIdentity) {
261 if (numPairs == 0)
262 return;
263 if (numPairs <= _MaxLocalPairs) {
264 std::uninitialized_copy(begin, end, localPairs);
265 }
266 else {
267 new (&remotePairs) std::shared_ptr<PathPair>(
268 new PathPair[numPairs], std::default_delete<PathPair[]>());
269 std::copy(begin, end, remotePairs.get());
270 }
271 }
272
273 _Data(_Data const &other)
274 : numPairs(other.numPairs)
275 , hasRootIdentity(other.hasRootIdentity) {
276 if (numPairs <= _MaxLocalPairs) {
277 std::uninitialized_copy(
278 other.localPairs,
279 other.localPairs + other.numPairs, localPairs);
280 }
281 else {
282 new (&remotePairs) std::shared_ptr<PathPair>(other.remotePairs);
283 }
284 }
285 _Data(_Data &&other)
286 : numPairs(other.numPairs)
287 , hasRootIdentity(other.hasRootIdentity) {
288 if (numPairs <= _MaxLocalPairs) {
289 PathPair *dst = localPairs;
290 PathPair *src = other.localPairs;
291 PathPair *srcEnd = other.localPairs + other.numPairs;
292 for (; src != srcEnd; ++src, ++dst) {
293 ::new (static_cast<void*>(std::addressof(*dst)))
294 PathPair(std::move(*src));
295 }
296 }
297 else {
298 new (&remotePairs)
299 std::shared_ptr<PathPair>(std::move(other.remotePairs));
300 }
301 }
302 _Data &operator=(_Data const &other) {
303 if (this != &other) {
304 this->~_Data();
305 new (this) _Data(other);
306 }
307 return *this;
308 }
309 _Data &operator=(_Data &&other) {
310 if (this != &other) {
311 this->~_Data();
312 new (this) _Data(std::move(other));
313 }
314 return *this;
315 }
316 ~_Data() {
317 if (numPairs <= _MaxLocalPairs) {
318 for (PathPair *p = localPairs; numPairs--; ++p) {
319 p->~PathPair();
320 }
321 }
322 else {
323 remotePairs.~shared_ptr<PathPair>();
324 }
325 }
326
327 bool IsNull() const {
328 return numPairs == 0 && !hasRootIdentity;
329 }
330
331 PathPair const *begin() const {
332 return numPairs <= _MaxLocalPairs ? localPairs : remotePairs.get();
333 }
334
335 PathPair const *end() const {
336 return begin() + numPairs;
337 }
338
339 bool operator==(_Data const &other) const {
340 return numPairs == other.numPairs &&
341 hasRootIdentity == other.hasRootIdentity &&
342 std::equal(begin(), end(), other.begin());
343 }
344
345 bool operator!=(_Data const &other) const {
346 return !(*this == other);
347 }
348
349 template <class HashState>
350 friend void TfHashAppend(HashState &h, _Data const &data){
351 h.Append(data.hasRootIdentity);
352 h.Append(data.numPairs);
353 h.AppendRange(std::begin(data), std::end(data));
354 }
355
356 union {
357 PathPair localPairs[_MaxLocalPairs > 0 ? _MaxLocalPairs : 1];
358 std::shared_ptr<PathPair> remotePairs;
359 };
360 typedef int PairCount;
361 PairCount numPairs = 0;
362 bool hasRootIdentity = false;
363 };
364
365 // Specialize TfHashAppend for PcpMapFunction.
366 template <typename HashState>
367 friend inline
368 void TfHashAppend(HashState& h, const PcpMapFunction& x){
369 h.Append(x._data);
370 h.Append(x._offset);
371 }
372
373 _Data _data;
374 SdfLayerOffset _offset;
375};
376
377// Specialize hash_value for PcpMapFunction.
378inline
379size_t hash_value(const PcpMapFunction& x)
380{
381 return TfHash{}(x);
382}
383
384PXR_NAMESPACE_CLOSE_SCOPE
385
386#endif // PXR_USD_PCP_MAP_FUNCTION_H
A function that maps values from one namespace (and time domain) to another.
Definition: mapFunction.h:82
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:142
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:223
std::map< SdfPath, SdfPath, SdfPath::FastLessThan > PathMap
A mapping from path to path.
Definition: mapFunction.h:85
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:61
Objects of this class represent a logical expression syntax tree consisting of SdfPath matching patte...
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:477
size_t hash_value(const half h)
Overload hash_value for half.
Definition: half.h:45