HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mapFunction.h
Go to the documentation of this file.
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"
15 
16 #include <atomic>
17 #include <memory>
18 
20 
21 /// \class PcpMapFunction
22 ///
23 /// A function that maps values from one namespace (and time domain) to
24 /// another. It represents the transformation that an arc such as a reference
25 /// arc applies as it incorporates values across the arc.
26 ///
27 /// Take the example of a reference arc, where a source path
28 /// </Model> is referenced as a target path, </Model_1>.
29 /// The source path </Model> is the source of the opinions;
30 /// the target path </Model_1> is where they are incorporated in the scene.
31 /// Values in the model that refer to paths relative to </Model> must be
32 /// transformed to be relative to </Model_1> instead.
33 /// The PcpMapFunction for the arc provides this service.
34 ///
35 /// Map functions have a specific \em domain, or set of values they can
36 /// operate on. Any values outside the domain cannot be mapped.
37 /// The domain precisely tracks what areas of namespace can be
38 /// referred to across various forms of arcs.
39 ///
40 /// Map functions can be chained to represent a series of map
41 /// operations applied in sequence. The map function represent the
42 /// cumulative effect as efficiently as possible. For example, in
43 /// the case of a chained reference from </Model> to </Model>
44 /// to </Model> to </Model_1>, this is effectively the same as
45 /// a mapping directly from </Model> to </Model_1>. Representing
46 /// the cumulative effect of arcs in this way is important for
47 /// handling larger scenes efficiently.
48 ///
49 /// Map functions can be \em inverted. Formally, map functions are
50 /// bijections (one-to-one and onto), which ensures that they can
51 /// be inverted. Put differently, no information is lost by applying
52 /// a map function to set of values within its domain; they retain
53 /// their distinct identities and can always be mapped back.
54 ///
55 /// One analogy that may or may not be helpful:
56 /// In the same way a geometric transform maps a model's points in its
57 /// rest space into the world coordinates for a particular instance,
58 /// a PcpMapFunction maps values about a referenced model into the
59 /// composed scene for a particular instance of that model. But rather
60 /// than translating and rotating points, the map function shifts the
61 /// values in namespace (and time).
62 ///
63 ///
65 {
66 public:
67  /// A mapping from path to path.
68  typedef std::map<SdfPath, SdfPath, SdfPath::FastLessThan> PathMap;
69  typedef std::pair<SdfPath, SdfPath> PathPair;
70  typedef std::vector<PathPair> PathPairVector;
71 
72  /// Construct a null function.
73  PcpMapFunction() = default;
74 
75  /// Constructs a map function with the given arguments.
76  /// Returns a null map function on error (see IsNull()).
77  ///
78  /// \param sourceToTargetMap The map from source paths to target paths.
79  /// \param offset The time offset to apply from source to target.
80  ///
81  PCP_API
82  static PcpMapFunction
83  Create(const PathMap &sourceToTargetMap,
84  const SdfLayerOffset &offset);
85 
86  /// Construct an identity map function.
87  PCP_API
88  static const PcpMapFunction &Identity();
89 
90  /// Returns an identity path mapping.
91  PCP_API
92  static const PathMap &IdentityPathMap();
93 
94  /// Swap the contents of this map function with \p map.
95  PCP_API
96  void Swap(PcpMapFunction &map);
97  void swap(PcpMapFunction &map) { Swap(map); }
98 
99  /// Equality.
100  PCP_API
101  bool operator==(const PcpMapFunction &map) const;
102 
103  /// Inequality.
104  PCP_API
105  bool operator!=(const PcpMapFunction &map) const;
106 
107  /// Return true if this map function is the null function.
108  /// For a null function, MapSourceToTarget() always returns an empty path.
109  PCP_API
110  bool IsNull() const;
111 
112  /// Return true if the map function is the identity function.
113  /// The identity function has an identity path mapping and time offset.
114  PCP_API
115  bool IsIdentity() const;
116 
117  /// Return true if the map function uses the identity path mapping.
118  /// If true, MapSourceToTarget() always returns the path unchanged.
119  /// However, this map function may have a non-identity time offset.
120  PCP_API
121  bool IsIdentityPathMapping() const;
122 
123  /// Return true if the map function maps the absolute root path to the
124  /// absolute root path, false otherwise.
125  bool HasRootIdentity() const { return _data.hasRootIdentity; }
126 
127  /// Map a path in the source namespace to the target.
128  /// If the path is not in the domain, returns an empty path.
129  PCP_API
130  SdfPath MapSourceToTarget(const SdfPath &path) const;
131 
132  /// Map a path in the target namespace to the source.
133  /// If the path is not in the co-domain, returns an empty path.
134  PCP_API
135  SdfPath MapTargetToSource(const SdfPath &path) const;
136 
137  /// Map all path pattern prefix paths and expression reference paths in the
138  /// source namespace to the target. For any references or patterns with
139  /// prefix paths that are not in the domain, replace with an
140  /// SdfPathPattern::Nothing() subexpression, to be simplified.
141  ///
142  /// For example, if the mapping specifies /Foo -> /World/Foo_1, and the
143  /// expression is '/Foo/Bar//Baz + /Something/Else//Entirely', the resulting
144  /// expression will be '/World/Foo_1/Bar//Baz', since the
145  /// /Something/Else prefix is outside the domain.
146  ///
147  /// If \p excludedPatterns and/or \p excludedReferences are supplied, they
148  /// are populated with those patterns & references that could not be
149  /// translated and were replaced with SdfPathPattern::Nothing().
150  PCP_API
153  const SdfPathExpression &pathExpr,
154  std::vector<SdfPathExpression::PathPattern>
155  *unmappedPatterns = nullptr,
156  std::vector<SdfPathExpression::ExpressionReference>
157  *unmappedRefs = nullptr
158  ) const;
159 
160  /// Map all path pattern prefix paths and expression reference paths in the
161  /// target namespace to the source. For any references or patterns with
162  /// prefix paths that are not in the co-domain, replace with an
163  /// SdfPathPattern::Nothing() subexpression, to be simplified.
164  ///
165  /// For example, if the mapping specifies /World/Foo_1 -> /Foo, and the
166  /// expression is '/World/Foo_1/Bar//Baz + /World/Bar//', the resulting
167  /// expression will be '/Foo/Bar//Baz', since the /World/Bar prefix is
168  /// outside the co-domain.
169  ///
170  /// If \p excludedPatterns and/or \p excludedReferences are supplied, they
171  /// are populated with those patterns & references that could not be
172  /// translated and were replaced with SdfPathPattern::Nothing().
173  PCP_API
176  const SdfPathExpression &pathExpr,
177  std::vector<SdfPathExpression::PathPattern>
178  *unmappedPatterns = nullptr,
179  std::vector<SdfPathExpression::ExpressionReference>
180  *unmappedRefs = nullptr
181  ) const;
182 
183  /// Compose this map over the given map function.
184  /// The result will represent the application of f followed by
185  /// the application of this function.
186  PCP_API
187  PcpMapFunction Compose(const PcpMapFunction &f) const;
188 
189  /// Compose this map function over a hypothetical map function that has an
190  /// identity path mapping and \p offset. This is equivalent to building
191  /// such a map function and invoking Compose(), but is faster.
192  PCP_API
193  PcpMapFunction ComposeOffset(const SdfLayerOffset &newOffset) const;
194 
195  /// Return the inverse of this map function.
196  /// This returns a true inverse \p inv: for any path p in this function's
197  /// domain that it maps to p', inv(p') -> p.
198  PCP_API
199  PcpMapFunction GetInverse() const;
200 
201  /// The set of path mappings, from source to target.
202  PCP_API
204 
205  /// The time offset of the mapping.
206  const SdfLayerOffset &GetTimeOffset() const { return _offset; }
207 
208  /// Returns a string representation of this mapping for debugging
209  /// purposes.
210  PCP_API
211  std::string GetString() const;
212 
213  /// Return a size_t hash for this map function.
214  PCP_API
215  size_t Hash() const;
216 
217 private:
218 
219  PCP_API
220  PcpMapFunction(PathPair const *sourceToTargetBegin,
221  PathPair const *sourceToTargetEnd,
223  bool hasRootIdentity);
224 
225  PCP_API
227  _MapPathExpressionImpl(
228  bool invert,
229  const SdfPathExpression &pathExpr,
230  std::vector<SdfPathExpression::PathPattern> *unmappedPatterns,
231  std::vector<SdfPathExpression::ExpressionReference> *unmappedRefs
232  ) const;
233 
234 private:
236 
237  static const int _MaxLocalPairs = 2;
238  struct _Data final {
239  _Data() {};
240 
241  _Data(PathPair const *begin, PathPair const *end, bool hasRootIdentity)
242  : numPairs(end-begin)
243  , hasRootIdentity(hasRootIdentity) {
244  if (numPairs == 0)
245  return;
246  if (numPairs <= _MaxLocalPairs) {
247  std::uninitialized_copy(begin, end, localPairs);
248  }
249  else {
250  new (&remotePairs) std::shared_ptr<PathPair>(
251  new PathPair[numPairs], std::default_delete<PathPair[]>());
252  std::copy(begin, end, remotePairs.get());
253  }
254  }
255 
256  _Data(_Data const &other)
257  : numPairs(other.numPairs)
258  , hasRootIdentity(other.hasRootIdentity) {
259  if (numPairs <= _MaxLocalPairs) {
260  std::uninitialized_copy(
261  other.localPairs,
262  other.localPairs + other.numPairs, localPairs);
263  }
264  else {
265  new (&remotePairs) std::shared_ptr<PathPair>(other.remotePairs);
266  }
267  }
268  _Data(_Data &&other)
269  : numPairs(other.numPairs)
270  , hasRootIdentity(other.hasRootIdentity) {
271  if (numPairs <= _MaxLocalPairs) {
272  PathPair *dst = localPairs;
273  PathPair *src = other.localPairs;
274  PathPair *srcEnd = other.localPairs + other.numPairs;
275  for (; src != srcEnd; ++src, ++dst) {
276  ::new (static_cast<void*>(std::addressof(*dst)))
277  PathPair(std::move(*src));
278  }
279  }
280  else {
281  new (&remotePairs)
282  std::shared_ptr<PathPair>(std::move(other.remotePairs));
283  }
284  }
285  _Data &operator=(_Data const &other) {
286  if (this != &other) {
287  this->~_Data();
288  new (this) _Data(other);
289  }
290  return *this;
291  }
292  _Data &operator=(_Data &&other) {
293  if (this != &other) {
294  this->~_Data();
295  new (this) _Data(std::move(other));
296  }
297  return *this;
298  }
299  ~_Data() {
300  if (numPairs <= _MaxLocalPairs) {
301  for (PathPair *p = localPairs; numPairs--; ++p) {
302  p->~PathPair();
303  }
304  }
305  else {
306  remotePairs.~shared_ptr<PathPair>();
307  }
308  }
309 
310  bool IsNull() const {
311  return numPairs == 0 && !hasRootIdentity;
312  }
313 
314  PathPair const *begin() const {
315  return numPairs <= _MaxLocalPairs ? localPairs : remotePairs.get();
316  }
317 
318  PathPair const *end() const {
319  return begin() + numPairs;
320  }
321 
322  bool operator==(_Data const &other) const {
323  return numPairs == other.numPairs &&
324  hasRootIdentity == other.hasRootIdentity &&
325  std::equal(begin(), end(), other.begin());
326  }
327 
328  bool operator!=(_Data const &other) const {
329  return !(*this == other);
330  }
331 
332  template <class HashState>
333  friend void TfHashAppend(HashState &h, _Data const &data){
334  h.Append(data.hasRootIdentity);
335  h.Append(data.numPairs);
336  h.AppendRange(std::begin(data), std::end(data));
337  }
338 
339  union {
340  PathPair localPairs[_MaxLocalPairs > 0 ? _MaxLocalPairs : 1];
341  std::shared_ptr<PathPair> remotePairs;
342  };
343  typedef int PairCount;
344  PairCount numPairs = 0;
345  bool hasRootIdentity = false;
346  };
347 
348  // Specialize TfHashAppend for PcpMapFunction.
349  template <typename HashState>
350  friend inline
351  void TfHashAppend(HashState& h, const PcpMapFunction& x){
352  h.Append(x._data);
353  h.Append(x._offset);
354  }
355 
356  _Data _data;
357  SdfLayerOffset _offset;
358 };
359 
360 // Specialize hash_value for PcpMapFunction.
361 inline
363 {
364  return TfHash{}(x);
365 }
366 
368 
369 #endif // PXR_USD_PCP_MAP_FUNCTION_H
PCP_API size_t Hash() const
Return a size_t hash for this map function.
PCP_API bool IsIdentityPathMapping() const
PCP_API PathMap GetSourceToTargetMap() const
The set of path mappings, from source to target.
static PCP_API const PcpMapFunction & Identity()
Construct an identity map function.
GLboolean invert
Definition: glcorearb.h:549
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
PCP_API bool IsIdentity() const
static PCP_API const PathMap & IdentityPathMap()
Returns an identity path mapping.
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
PCP_API bool operator!=(const PcpMapFunction &map) const
Inequality.
PCP_API SdfPath MapSourceToTarget(const SdfPath &path) const
static PCP_API PcpMapFunction Create(const PathMap &sourceToTargetMap, const SdfLayerOffset &offset)
friend PcpMapFunction * Pcp_MakeIdentity()
IMATH_HOSTDEVICE constexpr bool equal(T1 a, T2 b, T3 t) IMATH_NOEXCEPT
Definition: ImathFun.h:105
PCP_API bool operator==(const PcpMapFunction &map) const
Equality.
const SdfLayerOffset & GetTimeOffset() const
The time offset of the mapping.
Definition: mapFunction.h:206
Definition: hash.h:472
GLfloat f
Definition: glcorearb.h:1926
GLintptr offset
Definition: glcorearb.h:665
friend void TfHashAppend(HashState &h, const PcpMapFunction &x)
Definition: mapFunction.h:351
std::map< SdfPath, SdfPath, SdfPath::FastLessThan > PathMap
A mapping from path to path.
Definition: mapFunction.h:68
std::pair< SdfPath, SdfPath > PathPair
Definition: mapFunction.h:69
GLuint GLuint end
Definition: glcorearb.h:475
Definition: path.h:273
GLint GLenum GLint x
Definition: glcorearb.h:409
void swap(PcpMapFunction &map)
Definition: mapFunction.h:97
std::vector< PathPair > PathPairVector
Definition: mapFunction.h:70
PCP_API PcpMapFunction Compose(const PcpMapFunction &f) const
PCP_API void Swap(PcpMapFunction &map)
Swap the contents of this map function with map.
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
GLenum GLenum dst
Definition: glcorearb.h:1793
PCP_API PcpMapFunction GetInverse() const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
PcpMapFunction()=default
Construct a null function.
LeafData & operator=(const LeafData &)=delete
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
bool HasRootIdentity() const
Definition: mapFunction.h:125
size_t hash_value(const PcpMapFunction &x)
Definition: mapFunction.h:362
PCP_API std::string GetString() const
PCP_API SdfPath MapTargetToSource(const SdfPath &path) const
PCP_API bool IsNull() const
PCP_API PcpMapFunction ComposeOffset(const SdfLayerOffset &newOffset) const
Definition: format.h:1821
#define PCP_API
Definition: api.h:23
GLenum src
Definition: glcorearb.h:1793
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:566