Browse Source

add count_num_descendants()

David Rose 18 years ago
parent
commit
8566b388fb

+ 13 - 0
panda/src/pgraph/nodePath.I

@@ -392,6 +392,19 @@ get_child(int n, Thread *current_thread) const {
   return child;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: NodePath::count_num_descendants
+//       Access: Published
+//  Description: Returns the number of nodes at and below this level.
+////////////////////////////////////////////////////////////////////
+INLINE int NodePath::
+count_num_descendants() const {
+  if (is_empty()) {
+    return 0;
+  }
+  return _head->get_node()->count_num_descendants();
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: NodePath::has_parent
 //       Access: Published

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

@@ -206,6 +206,8 @@ PUBLISHED:
   INLINE NodePath get_child(int n, Thread *current_thread = Thread::get_current_thread()) const;
   NodePathCollection get_stashed_children(Thread *current_thread = Thread::get_current_thread()) const;
 
+  INLINE int count_num_descendants() const;
+
   INLINE bool has_parent(Thread *current_thread = Thread::get_current_thread()) const;
   INLINE NodePath get_parent(Thread *current_thread = Thread::get_current_thread()) const;
   int get_sort(Thread *current_thread = Thread::get_current_thread()) const;

+ 19 - 0
panda/src/pgraph/pandaNode.cxx

@@ -572,6 +572,25 @@ copy_subgraph(Thread *current_thread) const {
   return r_copy_subgraph(inst_map, current_thread);
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: PandaNode::count_num_descendants
+//       Access: Published
+//  Description: Returns the number of nodes at and below this level.
+////////////////////////////////////////////////////////////////////
+int PandaNode::
+count_num_descendants() const {
+  int count = 1;
+  Children children = get_children();
+  int num_children = children.get_num_children();
+
+  for (int i = 0; i < num_children; ++i) {
+    PandaNode *child = children.get_child(i);
+    count += child->count_num_descendants();
+  }
+
+  return count;
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: PandaNode::add_child
 //       Access: Published

+ 2 - 0
panda/src/pgraph/pandaNode.h

@@ -124,6 +124,8 @@ PUBLISHED:
   INLINE int get_child_sort(int n, Thread *current_thread = Thread::get_current_thread()) const;
   INLINE int find_child(PandaNode *node, Thread *current_thread = Thread::get_current_thread()) const;
 
+  int count_num_descendants() const;
+
   void add_child(PandaNode *child_node, int sort = 0,
                  Thread *current_thread = Thread::get_current_thread());
   void remove_child(int child_index, Thread *current_thread = Thread::get_current_thread());