diff --git a/panda/src/pgraph/nodePath.I b/panda/src/pgraph/nodePath.I index 3f70b00832..377f9c4a4e 100644 --- a/panda/src/pgraph/nodePath.I +++ b/panda/src/pgraph/nodePath.I @@ -316,6 +316,47 @@ is_same_graph(const NodePath &other) const { return (find_common_ancestor(*this, other, a_count, b_count) != (NodePathComponent *)NULL); } +//////////////////////////////////////////////////////////////////// +// Function: NodePath::is_ancestor_of +// Access: Published +// Description: Returns true if the node represented by this NodePath +// is a parent or other ancestor of the other NodePath, +// or false if it is not. +//////////////////////////////////////////////////////////////////// +INLINE bool NodePath:: +is_ancestor_of(const NodePath &other) const { + int a_count, b_count; + if (find_common_ancestor(*this, other, a_count, b_count) == (NodePathComponent *)NULL) { + // Not related. + return false; + } + + // They are related; now b is descended from a only if a is the + // common ancestor (which is to say, a_count == 0). + return (a_count == 0); +} + +//////////////////////////////////////////////////////////////////// +// Function: NodePath::get_common_ancestor +// Access: Published +// Description: Returns the lowest NodePath that both of these two +// NodePaths have in common: the first ancestor that +// both of them share. If the two NodePaths are +// unrelated, returns NodePath::not_found(). +//////////////////////////////////////////////////////////////////// +INLINE NodePath NodePath:: +get_common_ancestor(const NodePath &other) const { + int a_count, b_count; + NodePathComponent *common = find_common_ancestor(*this, other, a_count, b_count); + if (common == (NodePathComponent *)NULL) { + return NodePath::not_found(); + } + + NodePath result; + result._head = common; + return result; +} + //////////////////////////////////////////////////////////////////// // Function: NodePath::get_num_children // Access: Published diff --git a/panda/src/pgraph/nodePath.h b/panda/src/pgraph/nodePath.h index b95c008f9b..61cf8ac4e3 100644 --- a/panda/src/pgraph/nodePath.h +++ b/panda/src/pgraph/nodePath.h @@ -192,6 +192,8 @@ PUBLISHED: INLINE int get_key() const; INLINE bool is_same_graph(const NodePath &other) const; + INLINE bool is_ancestor_of(const NodePath &other) const; + INLINE NodePath get_common_ancestor(const NodePath &other) const; // Methods that return collections of NodePaths derived from or // related to this one.