node.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_NODE_H
25 #define PXR_USD_PCP_NODE_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/pcp/api.h"
29 #include "pxr/usd/pcp/types.h"
30 #include "pxr/usd/sdf/types.h"
31 #include "pxr/base/tf/iterator.h"
32 #include "pxr/base/tf/hashset.h"
33 
34 #include <boost/operators.hpp>
35 #include <boost/iterator/iterator_facade.hpp>
36 #include <boost/iterator/reverse_iterator.hpp>
37 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
40 class PcpArc;
41 class PcpLayerStackSite;
42 class PcpMapExpression;
43 class PcpNodeRef;
44 class PcpNodeRef_ChildrenIterator;
45 class PcpNodeRef_ChildrenReverseIterator;
46 class PcpErrorBase;
47 typedef std::shared_ptr<PcpErrorBase> PcpErrorBasePtr;
48 
49 TF_DECLARE_REF_PTRS(PcpPrimIndex_Graph);
50 
67 class PcpNodeRef :
68  public boost::totally_ordered<PcpNodeRef>
69 {
70 public:
71  typedef PcpNodeRef_ChildrenIterator child_const_iterator;
72  typedef PcpNodeRef_ChildrenReverseIterator child_const_reverse_iterator;
73  typedef std::pair<child_const_iterator,
74  child_const_iterator> child_const_range;
75 
76  PcpNodeRef() : _graph(0), _nodeIdx(PCP_INVALID_INDEX) {}
77 
80 
82  typedef size_t PcpNodeRef::*UnspecifiedBoolType;
83  inline operator UnspecifiedBoolType() const {
84  return (_graph && _nodeIdx != PCP_INVALID_INDEX) ? &PcpNodeRef::_nodeIdx : 0;
85  }
86 
88  inline bool operator==(const PcpNodeRef& rhs) const {
89  return _nodeIdx == rhs._nodeIdx && _graph == rhs._graph;
90  }
91 
95  PCP_API
96  bool operator<(const PcpNodeRef& rhs) const;
97 
99  struct Hash {
100  size_t operator()(const PcpNodeRef& rhs) const
101  { return (size_t)rhs.GetUniqueIdentifier(); }
102  };
103 
105  PcpPrimIndex_Graph *GetOwningGraph() const {
106  return _graph;
107  }
108 
110  PCP_API
111  void* GetUniqueIdentifier() const;
112 
114 
119 
121  PCP_API
122  PcpArcType GetArcType() const;
123 
126  PCP_API
127  PcpNodeRef GetParentNode() const;
128 
131  PCP_API
132  child_const_range GetChildrenRange() const;
133 
136  PCP_API
137  PcpNodeRef InsertChild(const PcpLayerStackSite& site, const PcpArc& arc,
138  PcpErrorBasePtr *error);
139 
142  PCP_API
144  const PcpPrimIndex_GraphRefPtr& subgraph, const PcpArc& arc,
145  PcpErrorBasePtr *error);
146 
151  PCP_API
152  PcpNodeRef GetOriginNode() const;
153 
158  PCP_API
160 
162  PCP_API
163  PcpNodeRef GetRootNode() const;
164 
167  PCP_API
168  const PcpMapExpression& GetMapToParent() const;
169 
172  PCP_API
173  const PcpMapExpression& GetMapToRoot() const;
174 
177  PCP_API
178  int GetSiblingNumAtOrigin() const;
179 
182  PCP_API
183  int GetNamespaceDepth() const;
184 
187  PCP_API
188  int GetDepthBelowIntroduction() const;
189 
191  PCP_API
193 
199  PCP_API
200  SdfPath GetIntroPath() const;
201 
203 
208 
210  PCP_API
211  PcpLayerStackSite GetSite() const;
212 
214  PCP_API
215  const SdfPath& GetPath() const;
216 
218  PCP_API
219  const PcpLayerStackRefPtr& GetLayerStack() const;
220 
222  PCP_API
223  bool IsRootNode() const;
224 
227  PCP_API
228  void SetIsDueToAncestor(bool isDueToAncestor);
229  PCP_API
230  bool IsDueToAncestor() const;
231 
234  PCP_API
235  void SetHasSymmetry(bool hasSymmetry);
236  PCP_API
237  bool HasSymmetry() const;
238 
241  PCP_API
242  void SetPermission(SdfPermission perm);
243  PCP_API
244  SdfPermission GetPermission() const;
245 
250  PCP_API
251  void SetInert(bool inert);
252  PCP_API
253  bool IsInert() const;
254 
258  PCP_API
259  void SetCulled(bool culled);
260  PCP_API
261  bool IsCulled() const;
262 
265  PCP_API
266  void SetRestricted(bool restricted);
267  PCP_API
268  bool IsRestricted() const;
269 
272  PCP_API
273  bool CanContributeSpecs() const;
274 
277  PCP_API
278  void SetHasSpecs(bool hasSpecs);
279  PCP_API
280  bool HasSpecs() const;
281 
283 
284  // Returns a compressed Sd site. For internal use only.
285  Pcp_CompressedSdSite GetCompressedSdSite(size_t layerIndex) const
286  {
287  return Pcp_CompressedSdSite(_nodeIdx, layerIndex);
288  }
289 
290 private:
291  friend class PcpPrimIndex_Graph;
292  friend class PcpNodeIterator;
293  friend class PcpNodeRef_ChildrenIterator;
294  friend class PcpNodeRef_ChildrenReverseIterator;
295  friend class PcpNodeRef_PrivateChildrenConstIterator;
296  friend class PcpNodeRef_PrivateChildrenConstReverseIterator;
297 
298  // Private constructor for internal use.
299  PcpNodeRef(PcpPrimIndex_Graph* graph, size_t idx)
300  : _graph(graph), _nodeIdx(idx)
301  {}
302 
303  size_t _GetNodeIndex() const { return _nodeIdx; }
304 
305  inline size_t _GetParentIndex() const;
306  inline size_t _GetOriginIndex() const;
307 
308 private: // Data
309  PcpPrimIndex_Graph* _graph;
310  size_t _nodeIdx;
311 };
312 
314 template <typename HashState>
315 inline
316 void
317 TfHashAppend(HashState& h, const PcpNodeRef& x){
318  h.Append((size_t)(x.GetUniqueIdentifier()));
319 }
320 inline
321 size_t
322 hash_value(const PcpNodeRef& x)
323 {
324  return TfHash{}(x);
325 }
326 
327 typedef TfHashSet<PcpNodeRef, PcpNodeRef::Hash> PcpNodeRefHashSet;
328 typedef std::vector<PcpNodeRef> PcpNodeRefVector;
329 
335 class PcpNodeRef_ChildrenIterator
336  : public boost::iterator_facade<
337  /* Derived = */ PcpNodeRef_ChildrenIterator,
338  /* ValueType = */ PcpNodeRef,
339  /* Category = */ boost::forward_traversal_tag,
340  /* RefType = */ PcpNodeRef
341  >
342 {
343 public:
345  PCP_API
346  PcpNodeRef_ChildrenIterator();
347 
350  PCP_API
351  PcpNodeRef_ChildrenIterator(const PcpNodeRef& node, bool end = false);
352 
353 private:
354  friend class boost::iterator_core_access;
355  PCP_API
356  void increment();
357  bool equal(const PcpNodeRef_ChildrenIterator& other) const
358  {
359  // Note: The default constructed iterator is *not* equal to any
360  // other iterator.
361  return (_node == other._node && _index == other._index);
362  }
363  reference dereference() const
364  {
365  return reference(_node._graph, _index);
366  }
367 
368 private:
369  // Current graph node this iterator is pointing at.
370  PcpNodeRef _node;
371 
372  // Index of current child.
373  size_t _index;
374 
375  friend class PcpNodeRef_ChildrenReverseIterator;
376 };
377 
383 class PcpNodeRef_ChildrenReverseIterator
384  : public boost::iterator_facade<
385  /* Derived = */ PcpNodeRef_ChildrenReverseIterator,
386  /* ValueType = */ PcpNodeRef,
387  /* Category = */ boost::forward_traversal_tag,
388  /* RefType = */ PcpNodeRef
389  >
390 {
391 public:
393  PCP_API
394  PcpNodeRef_ChildrenReverseIterator();
395 
397  PCP_API
398  PcpNodeRef_ChildrenReverseIterator(const PcpNodeRef_ChildrenIterator&);
399 
402  PCP_API
403  PcpNodeRef_ChildrenReverseIterator(const PcpNodeRef& node,bool end = false);
404 
405 private:
406  friend class boost::iterator_core_access;
407  PCP_API
408  void increment();
409  bool equal(const PcpNodeRef_ChildrenReverseIterator& other) const
410  {
411  // Note: The default constructed iterator is *not* equal to any
412  // other iterator.
413  return (_node == other._node && _index == other._index);
414  }
415  reference dereference() const
416  {
417  return reference(_node._graph, _index);
418  }
419 
420 private:
421  // Current graph node this iterator is pointing at.
422  PcpNodeRef _node;
423 
424  // Index of current child.
425  size_t _index;
426 };
427 
428 template <>
429 struct Tf_IteratorInterface<PcpNodeRef::child_const_range, false> {
430  typedef PcpNodeRef::child_const_iterator IteratorType;
431  static IteratorType Begin(PcpNodeRef::child_const_range const &c)
432  {
433  return c.first;
434  }
435  static IteratorType End(PcpNodeRef::child_const_range const &c)
436  {
437  return c.second;
438  }
439 };
440 
441 template <>
442 struct Tf_IteratorInterface<PcpNodeRef::child_const_range, true> {
443  typedef PcpNodeRef::child_const_reverse_iterator IteratorType;
444  static IteratorType Begin(PcpNodeRef::child_const_range const &c)
445  {
446  return c.second;
447  }
448  static IteratorType End(PcpNodeRef::child_const_range const &c)
449  {
450  return c.first;
451  }
452 };
453 
454 template <>
455 struct Tf_ShouldIterateOverCopy<PcpNodeRef::child_const_range> :
456  boost::true_type {};
457 
459 inline
460 PcpNodeRef_ChildrenIterator
461 begin(const PcpNodeRef::child_const_range& r)
462 {
463  return r.first;
464 }
465 
467 inline
468 PcpNodeRef_ChildrenIterator
469 end(const PcpNodeRef::child_const_range& r)
470 {
471  return r.second;
472 }
473 
474 // Helper to count the non-variant path components of a path; equivalent
475 // to path.StripAllVariantSelections().GetPathElementCount() except
476 // this method avoids constructing a new SdfPath value.
477 int PcpNode_GetNonVariantPathElementCount(const SdfPath &path);
478 
479 PXR_NAMESPACE_CLOSE_SCOPE
480 
481 #endif // PXR_USD_PCP_NODE_H
Represents an arc connecting two nodes in the prim index.
Definition: arc.h:44
An expression that yields a PcpMapFunction value.
Definition: mapExpression.h:56
PCP_API bool CanContributeSpecs() const
Returns true if this node is allowed to contribute opinions for composition, false otherwise.
PCP_API const PcpMapExpression & GetMapToParent() const
Returns mapping function used to translate paths and values from this node to its parent node.
PCP_API const PcpMapExpression & GetMapToRoot() const
Returns mapping function used to translate paths and values from this node directly to the root node.
size_t PcpNodeRef::* UnspecifiedBoolType
Returns true if this is a valid node reference, false otherwise.
Definition: node.h:82
A site specifies a path in a layer stack of scene description.
Definition: site.h:117
PCP_API int GetNamespaceDepth() const
Returns the absolute namespace depth of the node that introduced this node.
A simple iterator adapter for STL containers.
PCP_API PcpNodeRef GetRootNode() const
Walk up to the root node of this expression.
PcpNode represents a node in an expression tree for compositing scene description.
Definition: node.h:67
PCP_API PcpNodeRef InsertChildSubgraph(const PcpPrimIndex_GraphRefPtr &subgraph, const PcpArc &arc, PcpErrorBasePtr *error)
Inserts subgraph as a child of this node, with the root node of subtree connected to this node via ar...
PCP_API void SetRestricted(bool restricted)
Get/set whether this node is restricted.
PCP_API const PcpLayerStackRefPtr & GetLayerStack() const
Returns the layer stack for the site this node represents.
PCP_API PcpNodeRef GetOriginNode() const
Returns the immediate origin node for this node.
PCP_API int GetSiblingNumAtOrigin() const
Returns this node's index among siblings with the same arc type at this node's origin.
PCP_API const SdfPath & GetPath() const
Returns the path for the site this node represents.
Basic Sdf data types.
PCP_API bool IsRootNode() const
Returns true if this node is the root node of the prim index graph.
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:504
PCP_API PcpNodeRef GetParentNode() const
Returns this node's immediate parent node.
PCP_API bool operator<(const PcpNodeRef &rhs) const
Returns true if this node is 'less' than rhs.
PCP_API int GetDepthBelowIntroduction() const
Return the number of levels of namespace this node's site is below the level at which it was introduc...
PCP_API SdfPath GetPathAtIntroduction() const
Returns the path for this node's site when it was introduced.
Object used to iterate over nodes in the prim index graph in strong-to-weak order.
Definition: iterator.h:51
Hash functor.
Definition: node.h:99
PcpPrimIndex_Graph * GetOwningGraph() const
Returns the graph that this node belongs to.
Definition: node.h:105
bool operator==(const PcpNodeRef &rhs) const
Returns true if this references the same node as rhs.
Definition: node.h:88
PCP_API child_const_range GetChildrenRange() const
Returns an iterator range over the children nodes in strongest to weakest order.
PCP_API PcpNodeRef GetOriginRootNode() const
Walk up to the root origin node for this node.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
PCP_API void SetCulled(bool culled)
Get/set whether this node is culled.
constexpr size_t PCP_INVALID_INDEX
A value which indicates an invalid index.
Definition: types.h:197
PCP_API void SetPermission(SdfPermission perm)
Get/set the permission for this node.
PCP_API PcpArcType GetArcType() const
Returns the type of arc connecting this node to its parent node.
PCP_API void * GetUniqueIdentifier() const
Returns a value that uniquely identifies this node.
PCP_API PcpLayerStackSite GetSite() const
Get the site this node represents.
size_t hash_value(const half h)
Overload hash_value for half.
Definition: half.h:45
#define TF_DECLARE_REF_PTRS(type)
Define standard ref pointer types.
Definition: declarePtrs.h:75
PCP_API void SetIsDueToAncestor(bool isDueToAncestor)
Get/set whether this node was introduced by being copied from its namespace ancestor,...
PCP_API void SetInert(bool inert)
Get/set whether this node is inert.
PCP_API PcpNodeRef InsertChild(const PcpLayerStackSite &site, const PcpArc &arc, PcpErrorBasePtr *error)
Inserts a new child node for site, connected to this node via arc.
PCP_API SdfPath GetIntroPath() const
Get the path that introduced this node.
PCP_API void SetHasSymmetry(bool hasSymmetry)
Get/set whether this node provides any symmetry opinions, either directly or from a namespace ancesto...
PCP_API void SetHasSpecs(bool hasSpecs)
Returns true if this node has opinions authored for composition, false otherwise.
SdfPermission
An enum that defines permission levels.
Definition: types.h:155
Base class for all error types.
Definition: errors.h:88
PcpArcType
Describes the type of arc connecting two nodes in the prim index.
Definition: types.h:46