is_ancestor_of(), get_common_ancestor()

This commit is contained in:
David Rose 2005-02-26 00:44:53 +00:00
parent 350eafbd02
commit 4a5e1e825e
2 changed files with 43 additions and 0 deletions

View File

@ -316,6 +316,47 @@ is_same_graph(const NodePath &other) const {
return (find_common_ancestor(*this, other, a_count, b_count) != (NodePathComponent *)NULL); 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 // Function: NodePath::get_num_children
// Access: Published // Access: Published

View File

@ -192,6 +192,8 @@ PUBLISHED:
INLINE int get_key() const; INLINE int get_key() const;
INLINE bool is_same_graph(const NodePath &other) 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 // Methods that return collections of NodePaths derived from or
// related to this one. // related to this one.