All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
32 #include <atomic>
33 #include <memory>
34 
35 PXR_NAMESPACE_OPEN_SCOPE
36 
81 {
82 public:
84  typedef std::map<SdfPath, SdfPath, SdfPath::FastLessThan> PathMap;
85  typedef std::pair<SdfPath, SdfPath> PathPair;
86  typedef std::vector<PathPair> PathPairVector;
87 
89  PcpMapFunction() = default;
90 
97  PCP_API
98  static PcpMapFunction
99  Create(const PathMap &sourceToTargetMap,
100  const SdfLayerOffset &offset);
101 
103  PCP_API
104  static const PcpMapFunction &Identity();
105 
107  PCP_API
108  static const PathMap &IdentityPathMap();
109 
111  PCP_API
112  void Swap(PcpMapFunction &map);
113  void swap(PcpMapFunction &map) { Swap(map); }
114 
116  PCP_API
117  bool operator==(const PcpMapFunction &map) const;
118 
120  PCP_API
121  bool operator!=(const PcpMapFunction &map) const;
122 
125  PCP_API
126  bool IsNull() const;
127 
130  PCP_API
131  bool IsIdentity() const;
132 
135  bool HasRootIdentity() const { return _data.hasRootIdentity; }
136 
139  PCP_API
140  SdfPath MapSourceToTarget(const SdfPath &path) const;
141 
144  PCP_API
145  SdfPath MapTargetToSource(const SdfPath &path) const;
146 
150  PCP_API
151  PcpMapFunction Compose(const PcpMapFunction &f) const;
152 
156  PCP_API
157  PcpMapFunction ComposeOffset(const SdfLayerOffset &newOffset) const;
158 
162  PCP_API
163  PcpMapFunction GetInverse() const;
164 
166  PCP_API
168 
170  const SdfLayerOffset &GetTimeOffset() const { return _offset; }
171 
174  PCP_API
175  std::string GetString() const;
176 
178  PCP_API
179  size_t Hash() const;
180 
181 private:
182 
183  PCP_API
184  PcpMapFunction(PathPair const *sourceToTargetBegin,
185  PathPair const *sourceToTargetEnd,
186  SdfLayerOffset offset,
187  bool hasRootIdentity);
188 
189 private:
190  friend PcpMapFunction *Pcp_MakeIdentity();
191 
192  static const int _MaxLocalPairs = 2;
193  struct _Data final {
194  _Data() {};
195 
196  _Data(PathPair const *begin, PathPair const *end, bool hasRootIdentity)
197  : numPairs(end-begin)
198  , hasRootIdentity(hasRootIdentity) {
199  if (numPairs == 0)
200  return;
201  if (numPairs <= _MaxLocalPairs) {
202  std::uninitialized_copy(begin, end, localPairs);
203  }
204  else {
205  new (&remotePairs) std::shared_ptr<PathPair>(
206  new PathPair[numPairs], std::default_delete<PathPair[]>());
207  std::copy(begin, end, remotePairs.get());
208  }
209  }
210 
211  _Data(_Data const &other)
212  : numPairs(other.numPairs)
213  , hasRootIdentity(other.hasRootIdentity) {
214  if (numPairs <= _MaxLocalPairs) {
215  std::uninitialized_copy(
216  other.localPairs,
217  other.localPairs + other.numPairs, localPairs);
218  }
219  else {
220  new (&remotePairs) std::shared_ptr<PathPair>(other.remotePairs);
221  }
222  }
223  _Data(_Data &&other)
224  : numPairs(other.numPairs)
225  , hasRootIdentity(other.hasRootIdentity) {
226  if (numPairs <= _MaxLocalPairs) {
227  PathPair *dst = localPairs;
228  PathPair *src = other.localPairs;
229  PathPair *srcEnd = other.localPairs + other.numPairs;
230  for (; src != srcEnd; ++src, ++dst) {
231  ::new (static_cast<void*>(std::addressof(*dst)))
232  PathPair(std::move(*src));
233  }
234  }
235  else {
236  new (&remotePairs)
237  std::shared_ptr<PathPair>(std::move(other.remotePairs));
238  }
239  }
240  _Data &operator=(_Data const &other) {
241  if (this != &other) {
242  this->~_Data();
243  new (this) _Data(other);
244  }
245  return *this;
246  }
247  _Data &operator=(_Data &&other) {
248  if (this != &other) {
249  this->~_Data();
250  new (this) _Data(std::move(other));
251  }
252  return *this;
253  }
254  ~_Data() {
255  if (numPairs <= _MaxLocalPairs) {
256  for (PathPair *p = localPairs; numPairs--; ++p) {
257  p->~PathPair();
258  }
259  }
260  else {
261  remotePairs.~shared_ptr<PathPair>();
262  }
263  }
264 
265  bool IsNull() const {
266  return numPairs == 0 && !hasRootIdentity;
267  }
268 
269  PathPair const *begin() const {
270  return numPairs <= _MaxLocalPairs ? localPairs : remotePairs.get();
271  }
272 
273  PathPair const *end() const {
274  return begin() + numPairs;
275  }
276 
277  bool operator==(_Data const &other) const {
278  return numPairs == other.numPairs &&
279  hasRootIdentity == other.hasRootIdentity &&
280  std::equal(begin(), end(), other.begin());
281  }
282 
283  bool operator!=(_Data const &other) const {
284  return !(*this == other);
285  }
286 
287  union {
288  PathPair localPairs[_MaxLocalPairs > 0 ? _MaxLocalPairs : 1];
289  std::shared_ptr<PathPair> remotePairs;
290  };
291  typedef int PairCount;
292  PairCount numPairs = 0;
293  bool hasRootIdentity = false;
294  };
295 
296  _Data _data;
297  SdfLayerOffset _offset;
298 };
299 
300 // Specialize hash_value for PcpMapFunction.
301 inline
302 size_t hash_value(const PcpMapFunction& x)
303 {
304  return x.Hash();
305 }
306 
307 PXR_NAMESPACE_CLOSE_SCOPE
308 
309 #endif // PXR_USD_PCP_MAP_FUNCTION_H
const SdfLayerOffset & GetTimeOffset() const
The time offset of the mapping.
Definition: mapFunction.h:170
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...
PcpMapFunction()=default
Construct a null function.
PCP_API PathMap GetSourceToTargetMap() const
The set of path mappings, from source to target.
PCP_API bool operator==(const PcpMapFunction &map) const
Equality.
PCP_API SdfPath MapTargetToSource(const SdfPath &path) const
Map a path in the target namespace to the source.
static PCP_API const PathMap & IdentityPathMap()
Returns an identity path mapping.
PCP_API bool operator!=(const PcpMapFunction &map) const
Inequality.
A function that maps values from one namespace (and time domain) to another.
Definition: mapFunction.h:80
bool HasRootIdentity() const
Return true if the map function maps the absolute root path to the absolute root path, false otherwise.
Definition: mapFunction.h:135
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:288
std::map< SdfPath, SdfPath, SdfPath::FastLessThan > PathMap
A mapping from path to path.
Definition: mapFunction.h:84
PCP_API size_t Hash() const
Return a size_t hash for this map function.
PCP_API PcpMapFunction Compose(const PcpMapFunction &f) const
Compose this map over the given map function.
PCP_API void Swap(PcpMapFunction &map)
Swap the contents of this map function with map.
PCP_API PcpMapFunction GetInverse() const
Return the inverse of this map function.
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.
static PCP_API const PcpMapFunction & Identity()
Construct an identity map function.
PCP_API bool IsNull() const
Return true if this map function is the null function.
Represents a time offset and scale between layers.
Definition: layerOffset.h:61
PCP_API std::string GetString() const
Returns a string representation of this mapping for debugging purposes.
static PCP_API PcpMapFunction Create(const PathMap &sourceToTargetMap, const SdfLayerOffset &offset)
Constructs a map function with the given arguments.