HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
node.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_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 <hboost/iterator/iterator_facade.hpp>
35 #include <hboost/iterator/reverse_iterator.hpp>
36 
38 
39 class PcpArc;
40 class PcpLayerStackSite;
41 class PcpMapExpression;
42 class PcpNodeRef;
46 typedef std::shared_ptr<PcpErrorBase> PcpErrorBasePtr;
47 
48 TF_DECLARE_REF_PTRS(PcpPrimIndex_Graph);
49 
50 /// \class PcpNodeRef
51 ///
52 /// PcpNode represents a node in an expression tree for compositing
53 /// scene description.
54 ///
55 /// A node represents the opinions from a particular site. In addition,
56 /// it may have child nodes, representing nested expressions that are
57 /// composited over/under this node.
58 ///
59 /// Child nodes are stored and composited in strength order.
60 ///
61 /// Each node holds information about the arc to its parent.
62 /// This captures both the relative strength of the sub-expression
63 /// as well as any value-mapping needed, such as to rename opinions
64 /// from a model to use in a particular instance.
65 ///
67 {
68 public:
71  typedef std::pair<child_const_iterator,
73 
74  PcpNodeRef() : _graph(0), _nodeIdx(PCP_INVALID_INDEX) {}
75 
76  /// \name Operators / Miscellaneous
77  /// @{
78 
79  /// Returns true if this is a valid node reference, false otherwise.
80  typedef size_t PcpNodeRef::*UnspecifiedBoolType;
81  inline operator UnspecifiedBoolType() const {
82  return (_graph && _nodeIdx != PCP_INVALID_INDEX) ? &PcpNodeRef::_nodeIdx : 0;
83  }
84 
85  /// Returns true if this references the same node as \p rhs.
86  inline bool operator==(const PcpNodeRef& rhs) const {
87  return _nodeIdx == rhs._nodeIdx && _graph == rhs._graph;
88  }
89 
90  /// Inequality operator
91  /// \sa PcpNodeRef::operator==(const PcpNodeRef&)
92  inline bool operator!=(const PcpNodeRef& rhs) const {
93  return !(*this == rhs);
94  }
95 
96  /// Returns true if this node is 'less' than \p rhs.
97  /// The ordering of nodes is arbitrary and does not indicate the relative
98  /// strength of the nodes.
99  PCP_API
100  bool operator<(const PcpNodeRef& rhs) const;
101 
102  /// Less than or equal operator
103  /// \sa PcpNodeRef::operator<(const PcpNodeRef&)
104  bool operator<=(const PcpNodeRef& rhs) const {
105  return !(rhs < *this);
106  }
107 
108  /// Greater than operator
109  /// \sa PcpNodeRef::operator<(const PcpNodeRef&)
110  bool operator>(const PcpNodeRef& rhs) const {
111  return rhs < *this;
112  }
113 
114  /// Greater than or equal operator
115  /// \sa PcpNodeRef::operator<(const PcpNodeRef&)
116  bool operator>=(const PcpNodeRef& rhs) const {
117  return !(*this < rhs);
118  }
119 
120  /// Hash functor.
121  struct Hash {
122  size_t operator()(const PcpNodeRef& rhs) const
123  { return (size_t)rhs.GetUniqueIdentifier(); }
124  };
125 
126  /// Returns the graph that this node belongs to.
128  return _graph;
129  }
130 
131  /// Returns a value that uniquely identifies this node.
132  PCP_API
133  void* GetUniqueIdentifier() const;
134 
135  /// @}
136 
137  /// \name Arc information
138  /// Information pertaining to the arcs connecting this node to its
139  /// parent and child nodes.
140  /// @{
141 
142  /// Returns the type of arc connecting this node to its parent node.
143  PCP_API
144  PcpArcType GetArcType() const;
145 
146  /// Returns this node's immediate parent node. Will return NULL if this
147  /// node is a root node.
148  PCP_API
149  PcpNodeRef GetParentNode() const;
150 
151  /// Returns an iterator range over the children nodes in strongest to
152  /// weakest order.
153  PCP_API
155 
156  /// Inserts a new child node for \p site, connected to this node via
157  /// \p arc.
158  PCP_API
159  PcpNodeRef InsertChild(const PcpLayerStackSite& site, const PcpArc& arc,
161 
162  /// Inserts \p subgraph as a child of this node, with the root node of
163  /// \p subtree connected to this node via \p arc.
164  PCP_API
166  const PcpPrimIndex_GraphRefPtr& subgraph, const PcpArc& arc,
168 
169  /// Returns the immediate origin node for this node. The origin node
170  /// is the node that caused this node to be brought into the prim index.
171  /// In most cases, this is the same as the parent node. For implied
172  /// inherits, the origin is the node from which this node was propagated.
173  PCP_API
174  PcpNodeRef GetOriginNode() const;
175 
176  /// Walk up to the root origin node for this node. This is the very
177  /// first node that caused this node to be added to the graph. For
178  /// instance, the root origin node of an implied inherit is the
179  /// original inherit node.
180  PCP_API
182 
183  /// Walk up to the root node of this expression.
184  PCP_API
185  PcpNodeRef GetRootNode() const;
186 
187  /// Returns mapping function used to translate paths and values from
188  /// this node to its parent node.
189  PCP_API
190  const PcpMapExpression& GetMapToParent() const;
191 
192  /// Returns mapping function used to translate paths and values from
193  /// this node directly to the root node.
194  PCP_API
195  const PcpMapExpression& GetMapToRoot() const;
196 
197  /// Returns this node's index among siblings with the same arc type
198  /// at this node's origin.
199  PCP_API
200  int GetSiblingNumAtOrigin() const;
201 
202  /// Returns the absolute namespace depth of the node that introduced
203  /// this node. Note that this does *not* count any variant selections.
204  PCP_API
205  int GetNamespaceDepth() const;
206 
207  /// Return the number of levels of namespace this node's site is
208  /// below the level at which it was introduced by an arc.
209  PCP_API
210  int GetDepthBelowIntroduction() const;
211 
212  /// Returns the path for this node's site when it was introduced.
213  PCP_API
215 
216  /// Get the path that introduced this node.
217  /// Specifically, this is the path the parent node had at the level
218  /// of namespace where this node was added as a child.
219  /// For a root node, this returns the absolute root path.
220  /// See also GetDepthBelowIntroduction().
221  PCP_API
222  SdfPath GetIntroPath() const;
223 
224  /// @}
225 
226  /// \name Node information
227  /// Information pertaining specifically to this node and the opinions
228  /// that it may or may not provide.
229  /// @{
230 
231  /// Get the site this node represents.
232  PCP_API
233  PcpLayerStackSite GetSite() const;
234 
235  /// Returns the path for the site this node represents.
236  PCP_API
237  const SdfPath& GetPath() const;
238 
239  /// Returns the layer stack for the site this node represents.
240  PCP_API
241  const PcpLayerStackRefPtr& GetLayerStack() const;
242 
243  /// Returns true if this node is the root node of the prim index graph.
244  PCP_API
245  bool IsRootNode() const;
246 
247  /// Get/set whether this node was introduced by being copied from its
248  /// namespace ancestor, or directly by an arc at this level of namespace.
249  PCP_API
250  void SetIsDueToAncestor(bool isDueToAncestor);
251  PCP_API
252  bool IsDueToAncestor() const;
253 
254  /// Get/set whether this node provides any symmetry opinions, either
255  /// directly or from a namespace ancestor.
256  PCP_API
257  void SetHasSymmetry(bool hasSymmetry);
258  PCP_API
259  bool HasSymmetry() const;
260 
261  /// Get/set the permission for this node. This indicates whether specs
262  /// on this node can be accessed from other nodes.
263  PCP_API
264  void SetPermission(SdfPermission perm);
265  PCP_API
267 
268  /// Get/set whether this node is inert. An inert node never provides
269  /// any opinions to a prim index. Such a node may exist purely as a
270  /// marker to represent certain composition structure, but should never
271  /// contribute opinions.
272  PCP_API
273  void SetInert(bool inert);
274  PCP_API
275  bool IsInert() const;
276 
277  /// Get/set whether this node is culled. If a node is culled, it and
278  /// all descendant nodes provide no opinions to the index. A culled
279  /// node is also considered inert.
280  PCP_API
281  void SetCulled(bool culled);
282  PCP_API
283  bool IsCulled() const;
284 
285  /// Get/set whether this node is restricted. A restricted node is a
286  /// node that cannot contribute opinions to the index due to permissions.
287  PCP_API
288  void SetRestricted(bool restricted);
289  PCP_API
290  bool IsRestricted() const;
291 
292  /// Returns true if this node is allowed to contribute opinions
293  /// for composition, false otherwise.
294  PCP_API
295  bool CanContributeSpecs() const;
296 
297  /// Returns true if this node has opinions authored
298  /// for composition, false otherwise.
299  PCP_API
300  void SetHasSpecs(bool hasSpecs);
301  PCP_API
302  bool HasSpecs() const;
303 
304  /// @}
305 
306  // Returns a compressed Sd site. For internal use only.
307  Pcp_CompressedSdSite GetCompressedSdSite(size_t layerIndex) const
308  {
309  return Pcp_CompressedSdSite(_nodeIdx, layerIndex);
310  }
311 
312 private:
313  friend class PcpPrimIndex_Graph;
314  friend class PcpNodeIterator;
319 
320  // Private constructor for internal use.
321  PcpNodeRef(PcpPrimIndex_Graph* graph, size_t idx)
322  : _graph(graph), _nodeIdx(idx)
323  {}
324 
325  size_t _GetNodeIndex() const { return _nodeIdx; }
326 
327  inline size_t _GetParentIndex() const;
328  inline size_t _GetOriginIndex() const;
329 
330 private: // Data
331  PcpPrimIndex_Graph* _graph;
332  size_t _nodeIdx;
333 };
334 
335 /// Typedefs and support functions
336 template <typename HashState>
337 inline
338 void
339 TfHashAppend(HashState& h, const PcpNodeRef& x){
340  h.Append((size_t)(x.GetUniqueIdentifier()));
341 }
342 inline
343 size_t
345 {
346  return TfHash{}(x);
347 }
348 
350 typedef std::vector<PcpNodeRef> PcpNodeRefVector;
351 
352 /// \class PcpNodeRef_ChildrenIterator
353 ///
354 /// Object used to iterate over child nodes (not all descendant nodes) of a
355 /// node in the prim index graph in strong-to-weak order.
356 ///
358  : public hboost::iterator_facade<
359  /* Derived = */ PcpNodeRef_ChildrenIterator,
360  /* ValueType = */ PcpNodeRef,
361  /* Category = */ hboost::forward_traversal_tag,
362  /* RefType = */ PcpNodeRef
363  >
364 {
365 public:
366  /// Constructs an invalid iterator.
367  PCP_API
369 
370  /// Constructs an iterator pointing to \p node. Passing a NULL value
371  /// for \p node constructs an end iterator.
372  PCP_API
373  PcpNodeRef_ChildrenIterator(const PcpNodeRef& node, bool end = false);
374 
375 private:
377  PCP_API
378  void increment();
379  bool equal(const PcpNodeRef_ChildrenIterator& other) const
380  {
381  // Note: The default constructed iterator is *not* equal to any
382  // other iterator.
383  return (_node == other._node && _index == other._index);
384  }
385  reference dereference() const
386  {
387  return reference(_node._graph, _index);
388  }
389 
390 private:
391  // Current graph node this iterator is pointing at.
392  PcpNodeRef _node;
393 
394  // Index of current child.
395  size_t _index;
396 
398 };
399 
400 /// \class PcpNodeRef_ChildrenReverseIterator
401 ///
402 /// Object used to iterate over nodes in the prim index graph in weak-to-strong
403 /// order.
404 ///
406  : public hboost::iterator_facade<
407  /* Derived = */ PcpNodeRef_ChildrenReverseIterator,
408  /* ValueType = */ PcpNodeRef,
409  /* Category = */ hboost::forward_traversal_tag,
410  /* RefType = */ PcpNodeRef
411  >
412 {
413 public:
414  /// Constructs an invalid iterator.
415  PCP_API
417 
418  /// Constructs a reverse iterator from a forward iterator.
419  PCP_API
420  PcpNodeRef_ChildrenReverseIterator(const PcpNodeRef_ChildrenIterator&);
421 
422  /// Constructs an iterator pointing to \p node. Passing a NULL value
423  /// for \p node constructs an end iterator.
424  PCP_API
425  PcpNodeRef_ChildrenReverseIterator(const PcpNodeRef& node,bool end = false);
426 
427 private:
429  PCP_API
430  void increment();
431  bool equal(const PcpNodeRef_ChildrenReverseIterator& other) const
432  {
433  // Note: The default constructed iterator is *not* equal to any
434  // other iterator.
435  return (_node == other._node && _index == other._index);
436  }
437  reference dereference() const
438  {
439  return reference(_node._graph, _index);
440  }
441 
442 private:
443  // Current graph node this iterator is pointing at.
444  PcpNodeRef _node;
445 
446  // Index of current child.
447  size_t _index;
448 };
449 
450 template <>
451 struct Tf_IteratorInterface<PcpNodeRef::child_const_range, false> {
454  {
455  return c.first;
456  }
458  {
459  return c.second;
460  }
461 };
462 
463 template <>
464 struct Tf_IteratorInterface<PcpNodeRef::child_const_range, true> {
467  {
468  return c.second;
469  }
471  {
472  return c.first;
473  }
474 };
475 
476 template <>
477 struct Tf_ShouldIterateOverCopy<PcpNodeRef::child_const_range> :
478  std::true_type {};
479 
480 /// Support for range-based for loops for PcpNodeRef children ranges.
481 inline
482 PcpNodeRef_ChildrenIterator
484 {
485  return r.first;
486 }
487 
488 /// Support for range-based for loops for PcpNodeRef children ranges.
489 inline
490 PcpNodeRef_ChildrenIterator
492 {
493  return r.second;
494 }
495 
496 // Helper to count the non-variant path components of a path; equivalent
497 // to path.StripAllVariantSelections().GetPathElementCount() except
498 // this method avoids constructing a new SdfPath value.
500 
502 
503 #endif // PXR_USD_PCP_NODE_H
Definition: arc.h:44
PcpNodeRef::child_const_iterator IteratorType
Definition: node.h:452
PCP_API SdfPath GetPathAtIntroduction() const
Returns the path for this node's site when it was introduced.
PCP_API SdfPermission GetPermission() const
Get the site this node represents.
TfHashSet< PcpNodeRef, PcpNodeRef::Hash > PcpNodeRefHashSet
Definition: node.h:349
PCP_API void SetIsDueToAncestor(bool isDueToAncestor)
friend class PcpPrimIndex_Graph
Definition: node.h:313
PCP_API bool IsRootNode() const
Returns true if this node is the root node of the prim index graph.
bool operator<=(const PcpNodeRef &rhs) const
Definition: node.h:104
static IteratorType End(PcpNodeRef::child_const_range const &c)
Definition: node.h:457
friend class PcpNodeRef_PrivateChildrenConstReverseIterator
Definition: node.h:318
PCP_API PcpNodeRef InsertChild(const PcpLayerStackSite &site, const PcpArc &arc, PcpErrorBasePtr *error)
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
PCP_API PcpNodeRef GetOriginRootNode() const
PCP_API void SetRestricted(bool restricted)
static IteratorType End(PcpNodeRef::child_const_range const &c)
Definition: node.h:470
friend class hboost::iterator_core_access
Definition: node.h:376
size_t PcpNodeRef::* UnspecifiedBoolType
Returns true if this is a valid node reference, false otherwise.
Definition: node.h:80
IMATH_HOSTDEVICE constexpr bool equal(T1 a, T2 b, T3 t) IMATH_NOEXCEPT
Definition: ImathFun.h:105
PcpNodeRef_ChildrenIterator child_const_iterator
Definition: node.h:69
static IteratorType Begin(PcpNodeRef::child_const_range const &c)
Definition: node.h:466
PCP_API int GetDepthBelowIntroduction() const
PCP_API bool operator<(const PcpNodeRef &rhs) const
PCP_API PcpNodeRef GetOriginNode() const
int PcpNode_GetNonVariantPathElementCount(const SdfPath &path)
PCP_API int GetSiblingNumAtOrigin() const
PCP_API void SetHasSymmetry(bool hasSymmetry)
std::shared_ptr< PcpErrorBase > PcpErrorBasePtr
Definition: errors.h:76
< returns > If no error
Definition: snippets.dox:2
Definition: hash.h:504
std::shared_ptr< PcpErrorBase > PcpErrorBasePtr
Definition: node.h:45
TF_DECLARE_REF_PTRS(PcpPrimIndex_Graph)
PCP_API const PcpMapExpression & GetMapToRoot() const
PcpNodeRef()
Definition: node.h:74
PcpNodeRef_ChildrenIterator end(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:491
PcpNodeRef_ChildrenReverseIterator child_const_reverse_iterator
Definition: node.h:70
PCP_API child_const_range GetChildrenRange() const
PCP_API PcpNodeRef GetParentNode() const
size_t operator()(const PcpNodeRef &rhs) const
Definition: node.h:122
GLuint GLuint end
Definition: glcorearb.h:475
PCP_API bool IsCulled() const
Get the site this node represents.
PCP_API bool IsDueToAncestor() const
Get the site this node represents.
bool operator!=(const PcpNodeRef &rhs) const
Definition: node.h:92
PCP_API const SdfPath & GetPath() const
Returns the path for the site this node represents.
Hash functor.
Definition: node.h:121
bool operator>(const PcpNodeRef &rhs) const
Definition: node.h:110
PCP_API void SetInert(bool inert)
friend class hboost::iterator_core_access
Definition: node.h:428
PCP_API PcpLayerStackSite GetSite() const
Get the site this node represents.
Definition: path.h:291
friend class PcpNodeRef_PrivateChildrenConstIterator
Definition: node.h:317
GLint GLenum GLint x
Definition: glcorearb.h:409
PCP_API bool IsRestricted() const
Get the site this node represents.
PcpNodeRef::child_const_reverse_iterator IteratorType
Definition: node.h:465
constexpr size_t PCP_INVALID_INDEX
Definition: types.h:217
std::pair< child_const_iterator, child_const_iterator > child_const_range
Definition: node.h:72
PCP_API const PcpLayerStackRefPtr & GetLayerStack() const
Returns the layer stack for the site this node represents.
PCP_API PcpNodeRef GetRootNode() const
Walk up to the root node of this expression.
PCP_API bool CanContributeSpecs() const
Pcp_CompressedSdSite GetCompressedSdSite(size_t layerIndex) const
Definition: node.h:307
PCP_API const PcpMapExpression & GetMapToParent() const
PCP_API int GetNamespaceDepth() const
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
size_t hash_value(const PcpNodeRef &x)
Definition: node.h:344
std::vector< PcpNodeRef > PcpNodeRefVector
Definition: node.h:350
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
PcpPrimIndex_Graph * GetOwningGraph() const
Returns the graph that this node belongs to.
Definition: node.h:127
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
PCP_API bool HasSpecs() const
Get the site this node represents.
GA_API const UT_StringHolder arc
PCP_API void * GetUniqueIdentifier() const
Returns a value that uniquely identifies this node.
PCP_API bool IsInert() const
Get the site this node represents.
GLboolean r
Definition: glcorearb.h:1222
PCP_API PcpArcType GetArcType() const
Returns the type of arc connecting this node to its parent node.
PCP_API PcpNodeRef_ChildrenReverseIterator()
Constructs an invalid iterator.
static IteratorType Begin(PcpNodeRef::child_const_range const &c)
Definition: node.h:453
void TfHashAppend(HashState &h, const PcpNodeRef &x)
Typedefs and support functions.
Definition: node.h:339
PCP_API SdfPath GetIntroPath() const
PCP_API void SetCulled(bool culled)
PCP_API void SetHasSpecs(bool hasSpecs)
bool operator==(const PcpNodeRef &rhs) const
Returns true if this references the same node as rhs.
Definition: node.h:86
PCP_API PcpNodeRef_ChildrenIterator()
Constructs an invalid iterator.
PCP_API PcpNodeRef InsertChildSubgraph(const PcpPrimIndex_GraphRefPtr &subgraph, const PcpArc &arc, PcpErrorBasePtr *error)
bool operator>=(const PcpNodeRef &rhs) const
Definition: node.h:116
PCP_API void SetPermission(SdfPermission perm)
SdfPermission
Definition: types.h:154
#define PCP_API
Definition: api.h:40
PCP_API bool HasSymmetry() const
Get the site this node represents.
PcpArcType
Definition: types.h:44
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:483