From 8566b388fbdad9054d9b0026d5685fd6a9acc698 Mon Sep 17 00:00:00 2001 From: David Rose Date: Tue, 17 Apr 2007 18:21:29 +0000 Subject: [PATCH] add count_num_descendants() --- panda/src/pgraph/nodePath.I | 13 +++++++++++++ panda/src/pgraph/nodePath.h | 2 ++ panda/src/pgraph/pandaNode.cxx | 19 +++++++++++++++++++ panda/src/pgraph/pandaNode.h | 2 ++ 4 files changed, 36 insertions(+) diff --git a/panda/src/pgraph/nodePath.I b/panda/src/pgraph/nodePath.I index 0407c79565..2de287ba97 100644 --- a/panda/src/pgraph/nodePath.I +++ b/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 diff --git a/panda/src/pgraph/nodePath.h b/panda/src/pgraph/nodePath.h index db485969f3..deb238fb53 100644 --- a/panda/src/pgraph/nodePath.h +++ b/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; diff --git a/panda/src/pgraph/pandaNode.cxx b/panda/src/pgraph/pandaNode.cxx index 3164ac13f3..213bba2ac0 100644 --- a/panda/src/pgraph/pandaNode.cxx +++ b/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 diff --git a/panda/src/pgraph/pandaNode.h b/panda/src/pgraph/pandaNode.h index 83461984ba..bd792466fe 100644 --- a/panda/src/pgraph/pandaNode.h +++ b/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());