David Rose 17 lat temu
rodzic
commit
d2c5866507
2 zmienionych plików z 39 dodań i 1 usunięć
  1. 38 1
      panda/src/pgraph/nodePath.cxx
  2. 1 0
      panda/src/pgraph/nodePath.h

+ 38 - 1
panda/src/pgraph/nodePath.cxx

@@ -98,6 +98,9 @@ get_num_nodes(Thread *current_thread) const {
 //               return the same thing as get_node(0) (since the
 //               bottom node is the most important node in the
 //               NodePath, and is the one most frequently referenced).
+//
+//               Note that this function returns the same thing as
+//               get_ancestor(index).node().
 ////////////////////////////////////////////////////////////////////
 PandaNode *NodePath::
 get_node(int index, Thread *current_thread) const {
@@ -120,6 +123,40 @@ get_node(int index, Thread *current_thread) const {
   return comp->get_node();
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: NodePath::get_ancestor
+//       Access: Published
+//  Description: Returns the nth ancestor of the path, where 0 is the
+//               NodePath itself and get_num_nodes() - 1 is get_top().
+//               This requires iterating through the path.
+//
+//               Also see get_node(), which returns the same thing as
+//               a PandaNode pointer, not a NodePath.
+////////////////////////////////////////////////////////////////////
+NodePath NodePath::
+get_ancestor(int index, Thread *current_thread) const {
+  nassertr(index >= 0 && index < get_num_nodes(), NodePath::fail());
+
+  int pipeline_stage = current_thread->get_pipeline_stage();
+
+  NodePathComponent *comp = _head;
+  while (index > 0) {
+    // If this assertion fails, the index was out of range; the
+    // component's length must have been invalid.
+    nassertr(comp != (NodePathComponent *)NULL, NodePath::fail());
+    comp = comp->get_next(pipeline_stage, current_thread);
+    index--;
+  }
+
+  // If this assertion fails, the index was out of range; the
+  // component's length must have been invalid.
+  nassertr(comp != (NodePathComponent *)NULL, NodePath::fail());
+
+  NodePath result;
+  result._head = comp;
+  return result;
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: NodePath::get_top
 //       Access: Published
@@ -137,7 +174,7 @@ get_top(Thread *current_thread) const {
   NodePathComponent *comp = _head;
   while (!comp->is_top_node(pipeline_stage, current_thread)) {
     comp = comp->get_next(pipeline_stage, current_thread);
-    nassertr(comp != (NodePathComponent *)NULL, NULL);
+    nassertr(comp != (NodePathComponent *)NULL, NodePath::fail());
   }
 
   NodePath top;

+ 1 - 0
panda/src/pgraph/nodePath.h

@@ -180,6 +180,7 @@ PUBLISHED:
   INLINE bool is_singleton(Thread *current_thread = Thread::get_current_thread()) const;
   int get_num_nodes(Thread *current_thread = Thread::get_current_thread()) const;
   PandaNode *get_node(int index, Thread *current_thread = Thread::get_current_thread()) const;
+  NodePath get_ancestor(int index, Thread *current_thread = Thread::get_current_thread()) const;
 
   INLINE ErrorType get_error_type() const;