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 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 
32 #include <atomic>
33 #include <memory>
34 
36 
37 /// \class PcpMapFunction
38 ///
39 /// A function that maps values from one namespace (and time domain) to
40 /// another. It represents the transformation that an arc such as a reference
41 /// arc applies as it incorporates values across the arc.
42 ///
43 /// Take the example of a reference arc, where a source path
44 /// </Model> is referenced as a target path, </Model_1>.
45 /// The source path </Model> is the source of the opinions;
46 /// the target path </Model_1> is where they are incorporated in the scene.
47 /// Values in the model that refer to paths relative to </Model> must be
48 /// transformed to be relative to </Model_1> instead.
49 /// The PcpMapFunction for the arc provides this service.
50 ///
51 /// Map functions have a specific \em domain, or set of values they can
52 /// operate on. Any values outside the domain cannot be mapped.
53 /// The domain precisely tracks what areas of namespace can be
54 /// referred to across various forms of arcs.
55 ///
56 /// Map functions can be chained to represent a series of map
57 /// operations applied in sequence. The map function represent the
58 /// cumulative effect as efficiently as possible. For example, in
59 /// the case of a chained reference from </Model> to </Model>
60 /// to </Model> to </Model_1>, this is effectively the same as
61 /// a mapping directly from </Model> to </Model_1>. Representing
62 /// the cumulative effect of arcs in this way is important for
63 /// handling larger scenes efficiently.
64 ///
65 /// Map functions can be \em inverted. Formally, map functions are
66 /// bijections (one-to-one and onto), which ensures that they can
67 /// be inverted. Put differently, no information is lost by applying
68 /// a map function to set of values within its domain; they retain
69 /// their distinct identities and can always be mapped back.
70 ///
71 /// One analogy that may or may not be helpful:
72 /// In the same way a geometric transform maps a model's points in its
73 /// rest space into the world coordinates for a particular instance,
74 /// a PcpMapFunction maps values about a referenced model into the
75 /// composed scene for a particular instance of that model. But rather
76 /// than translating and rotating points, the map function shifts the
77 /// values in namespace (and time).
78 ///
79 ///
81 {
82 public:
83  /// A mapping from path to path.
84  typedef std::map<SdfPath, SdfPath, SdfPath::FastLessThan> PathMap;
85  typedef std::pair<SdfPath, SdfPath> PathPair;
86  typedef std::vector<PathPair> PathPairVector;
87 
88  /// Construct a null function.
89  PcpMapFunction() = default;
90 
91  /// Constructs a map function with the given arguments.
92  /// Returns a null map function on error (see IsNull()).
93  ///
94  /// \param sourceToTargetMap The map from source paths to target paths.
95  /// \param offset The time offset to apply from source to target.
96  ///
97  PCP_API
98  static PcpMapFunction
99  Create(const PathMap &sourceToTargetMap,
100  const SdfLayerOffset &offset);
101 
102  /// Construct an identity map function.
103  PCP_API
104  static const PcpMapFunction &Identity();
105 
106  /// Returns an identity path mapping.
107  PCP_API
108  static const PathMap &IdentityPathMap();
109 
110  /// Swap the contents of this map function with \p map.
111  PCP_API
112  void Swap(PcpMapFunction &map);
113  void swap(PcpMapFunction &map) { Swap(map); }
114 
115  /// Equality.
116  PCP_API
117  bool operator==(const PcpMapFunction &map) const;
118 
119  /// Inequality.
120  PCP_API
121  bool operator!=(const PcpMapFunction &map) const;
122 
123  /// Return true if this map function is the null function.
124  /// For a null function, MapSourceToTarget() always returns an empty path.
125  PCP_API
126  bool IsNull() const;
127 
128  /// Return true if the map function is the identity function.
129  /// The identity function has an identity path mapping and time offset.
130  PCP_API
131  bool IsIdentity() const;
132 
133  /// Return true if the map function uses the identity path mapping.
134  /// If true, MapSourceToTarget() always returns the path unchanged.
135  /// However, this map function may have a non-identity time offset.
136  PCP_API
137  bool IsIdentityPathMapping() const;
138 
139  /// Return true if the map function maps the absolute root path to the
140  /// absolute root path, false otherwise.
141  bool HasRootIdentity() const { return _data.hasRootIdentity; }
142 
143  /// Map a path in the source namespace to the target.
144  /// If the path is not in the domain, returns an empty path.
145  PCP_API
146  SdfPath MapSourceToTarget(const SdfPath &path) const;
147 
148  /// Map a path in the target namespace to the source.
149  /// If the path is not in the co-domain, returns an empty path.
150  PCP_API
151  SdfPath MapTargetToSource(const SdfPath &path) const;
152 
153  /// Compose this map over the given map function.
154  /// The result will represent the application of f followed by
155  /// the application of this function.
156  PCP_API
157  PcpMapFunction Compose(const PcpMapFunction &f) const;
158 
159  /// Compose this map function over a hypothetical map function that has an
160  /// identity path mapping and \p offset. This is equivalent to building
161  /// such a map function and invoking Compose(), but is faster.
162  PCP_API
163  PcpMapFunction ComposeOffset(const SdfLayerOffset &newOffset) const;
164 
165  /// Return the inverse of this map function.
166  /// This returns a true inverse \p inv: for any path p in this function's
167  /// domain that it maps to p', inv(p') -> p.
168  PCP_API
169  PcpMapFunction GetInverse() const;
170 
171  /// The set of path mappings, from source to target.
172  PCP_API
174 
175  /// The time offset of the mapping.
176  const SdfLayerOffset &GetTimeOffset() const { return _offset; }
177 
178  /// Returns a string representation of this mapping for debugging
179  /// purposes.
180  PCP_API
181  std::string GetString() const;
182 
183  /// Return a size_t hash for this map function.
184  PCP_API
185  size_t Hash() const;
186 
187 private:
188 
189  PCP_API
190  PcpMapFunction(PathPair const *sourceToTargetBegin,
191  PathPair const *sourceToTargetEnd,
193  bool hasRootIdentity);
194 
195 private:
197 
198  static const int _MaxLocalPairs = 2;
199  struct _Data final {
200  _Data() {};
201 
202  _Data(PathPair const *begin, PathPair const *end, bool hasRootIdentity)
203  : numPairs(end-begin)
204  , hasRootIdentity(hasRootIdentity) {
205  if (numPairs == 0)
206  return;
207  if (numPairs <= _MaxLocalPairs) {
208  std::uninitialized_copy(begin, end, localPairs);
209  }
210  else {
211  new (&remotePairs) std::shared_ptr<PathPair>(
212  new PathPair[numPairs], std::default_delete<PathPair[]>());
213  std::copy(begin, end, remotePairs.get());
214  }
215  }
216 
217  _Data(_Data const &other)
218  : numPairs(other.numPairs)
219  , hasRootIdentity(other.hasRootIdentity) {
220  if (numPairs <= _MaxLocalPairs) {
221  std::uninitialized_copy(
222  other.localPairs,
223  other.localPairs + other.numPairs, localPairs);
224  }
225  else {
226  new (&remotePairs) std::shared_ptr<PathPair>(other.remotePairs);
227  }
228  }
229  _Data(_Data &&other)
230  : numPairs(other.numPairs)
231  , hasRootIdentity(other.hasRootIdentity) {
232  if (numPairs <= _MaxLocalPairs) {
233  PathPair *dst = localPairs;
234  PathPair *src = other.localPairs;
235  PathPair *srcEnd = other.localPairs + other.numPairs;
236  for (; src != srcEnd; ++src, ++dst) {
237  ::new (static_cast<void*>(std::addressof(*dst)))
238  PathPair(std::move(*src));
239  }
240  }
241  else {
242  new (&remotePairs)
243  std::shared_ptr<PathPair>(std::move(other.remotePairs));
244  }
245  }
246  _Data &operator=(_Data const &other) {
247  if (this != &other) {
248  this->~_Data();
249  new (this) _Data(other);
250  }
251  return *this;
252  }
253  _Data &operator=(_Data &&other) {
254  if (this != &other) {
255  this->~_Data();
256  new (this) _Data(std::move(other));
257  }
258  return *this;
259  }
260  ~_Data() {
261  if (numPairs <= _MaxLocalPairs) {
262  for (PathPair *p = localPairs; numPairs--; ++p) {
263  p->~PathPair();
264  }
265  }
266  else {
267  remotePairs.~shared_ptr<PathPair>();
268  }
269  }
270 
271  bool IsNull() const {
272  return numPairs == 0 && !hasRootIdentity;
273  }
274 
275  PathPair const *begin() const {
276  return numPairs <= _MaxLocalPairs ? localPairs : remotePairs.get();
277  }
278 
279  PathPair const *end() const {
280  return begin() + numPairs;
281  }
282 
283  bool operator==(_Data const &other) const {
284  return numPairs == other.numPairs &&
285  hasRootIdentity == other.hasRootIdentity &&
286  std::equal(begin(), end(), other.begin());
287  }
288 
289  bool operator!=(_Data const &other) const {
290  return !(*this == other);
291  }
292 
293  template <class HashState>
294  friend void TfHashAppend(HashState &h, _Data const &data){
295  h.Append(data.hasRootIdentity);
296  h.Append(data.numPairs);
297  h.AppendRange(std::begin(data), std::end(data));
298  }
299 
300  union {
301  PathPair localPairs[_MaxLocalPairs > 0 ? _MaxLocalPairs : 1];
302  std::shared_ptr<PathPair> remotePairs;
303  };
304  typedef int PairCount;
305  PairCount numPairs = 0;
306  bool hasRootIdentity = false;
307  };
308 
309  // Specialize TfHashAppend for PcpMapFunction.
310  template <typename HashState>
311  friend inline
312  void TfHashAppend(HashState& h, const PcpMapFunction& x){
313  h.Append(x._data);
314  h.Append(x._offset);
315  }
316 
317  _Data _data;
318  SdfLayerOffset _offset;
319 };
320 
321 // Specialize hash_value for PcpMapFunction.
322 inline
324 {
325  return TfHash{}(x);
326 }
327 
329 
330 #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.
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
PCP_API bool IsIdentity() const
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
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:176
Definition: hash.h:504
GLfloat f
Definition: glcorearb.h:1926
GLintptr offset
Definition: glcorearb.h:665
friend void TfHashAppend(HashState &h, const PcpMapFunction &x)
Definition: mapFunction.h:312
std::map< SdfPath, SdfPath, SdfPath::FastLessThan > PathMap
A mapping from path to path.
Definition: mapFunction.h:84
std::pair< SdfPath, SdfPath > PathPair
Definition: mapFunction.h:85
GLuint GLuint end
Definition: glcorearb.h:475
Definition: path.h:291
GLint GLenum GLint x
Definition: glcorearb.h:409
void swap(PcpMapFunction &map)
Definition: mapFunction.h:113
std::vector< PathPair > PathPairVector
Definition: mapFunction.h:86
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:1441
PcpMapFunction()=default
Construct a null function.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
bool HasRootIdentity() const
Definition: mapFunction.h:141
size_t hash_value(const PcpMapFunction &x)
Definition: mapFunction.h:323
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:895
#define PCP_API
Definition: api.h:40
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:483