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 PCP_MAPFUNCTION_H
25 #define PCP_MAPFUNCTION_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/pcp/api.h"
29 #include "pxr/usd/sdf/path.h"
30 #include "pxr/usd/sdf/layerOffset.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 GetInverse() const;
158 
160  PCP_API
162 
164  const SdfLayerOffset &GetTimeOffset() const { return _offset; }
165 
168  PCP_API
169  std::string GetString() const;
170 
172  PCP_API
173  size_t Hash() const;
174 
175 private:
176 
177  PCP_API
178  PcpMapFunction(PathPair const *sourceToTargetBegin,
179  PathPair const *sourceToTargetEnd,
180  SdfLayerOffset offset,
181  bool hasRootIdentity);
182 
183 private:
184  friend PcpMapFunction *Pcp_MakeIdentity();
185 
186  static const int _MaxLocalPairs = 2;
187  struct _Data final {
188  _Data() {};
189 
190  _Data(PathPair const *begin, PathPair const *end, bool hasRootIdentity)
191  : numPairs(end-begin)
192  , hasRootIdentity(hasRootIdentity) {
193  if (numPairs == 0)
194  return;
195  if (numPairs <= _MaxLocalPairs) {
196  std::uninitialized_copy(begin, end, localPairs);
197  }
198  else {
199  new (&remotePairs) std::shared_ptr<PathPair>(
200  new PathPair[numPairs], std::default_delete<PathPair[]>());
201  std::copy(begin, end, remotePairs.get());
202  }
203  }
204 
205  _Data(_Data const &other)
206  : numPairs(other.numPairs)
207  , hasRootIdentity(other.hasRootIdentity) {
208  if (numPairs <= _MaxLocalPairs) {
209  std::uninitialized_copy(
210  other.localPairs,
211  other.localPairs + other.numPairs, localPairs);
212  }
213  else {
214  new (&remotePairs) std::shared_ptr<PathPair>(other.remotePairs);
215  }
216  }
217  _Data(_Data &&other)
218  : numPairs(other.numPairs)
219  , hasRootIdentity(other.hasRootIdentity) {
220  if (numPairs <= _MaxLocalPairs) {
221  PathPair *dst = localPairs;
222  PathPair *src = other.localPairs;
223  PathPair *srcEnd = other.localPairs + other.numPairs;
224  for (; src != srcEnd; ++src, ++dst) {
225  ::new (static_cast<void*>(std::addressof(*dst)))
226  PathPair(std::move(*src));
227  }
228  }
229  else {
230  new (&remotePairs)
231  std::shared_ptr<PathPair>(std::move(other.remotePairs));
232  }
233  }
234  _Data &operator=(_Data const &other) {
235  if (this != &other) {
236  this->~_Data();
237  new (this) _Data(other);
238  }
239  return *this;
240  }
241  _Data &operator=(_Data &&other) {
242  if (this != &other) {
243  this->~_Data();
244  new (this) _Data(std::move(other));
245  }
246  return *this;
247  }
248  ~_Data() {
249  if (numPairs <= _MaxLocalPairs) {
250  for (PathPair *p = localPairs; numPairs--; ++p) {
251  p->~PathPair();
252  }
253  }
254  else {
255  remotePairs.~shared_ptr<PathPair>();
256  }
257  }
258 
259  bool IsNull() const {
260  return numPairs == 0 && !hasRootIdentity;
261  }
262 
263  PathPair const *begin() const {
264  return numPairs <= _MaxLocalPairs ? localPairs : remotePairs.get();
265  }
266 
267  PathPair const *end() const {
268  return begin() + numPairs;
269  }
270 
271  bool operator==(_Data const &other) const {
272  return numPairs == other.numPairs &&
273  hasRootIdentity == other.hasRootIdentity &&
274  std::equal(begin(), end(), other.begin());
275  }
276 
277  bool operator!=(_Data const &other) const {
278  return !(*this == other);
279  }
280 
281  union {
282  PathPair localPairs[_MaxLocalPairs > 0 ? _MaxLocalPairs : 1];
283  std::shared_ptr<PathPair> remotePairs;
284  };
285  typedef int PairCount;
286  PairCount numPairs = 0;
287  bool hasRootIdentity = false;
288  };
289 
290  _Data _data;
291  SdfLayerOffset _offset;
292 };
293 
294 // Specialize hash_value for PcpMapFunction.
295 inline
296 size_t hash_value(const PcpMapFunction& x)
297 {
298  return x.Hash();
299 }
300 
301 PXR_NAMESPACE_CLOSE_SCOPE
302 
303 #endif // PCP_MAPFUNCTION_H
const SdfLayerOffset & GetTimeOffset() const
The time offset of the mapping.
Definition: mapFunction.h:164
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:287
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.