浏览代码

is_ancestor_of(), get_common_ancestor()

David Rose 21 年之前
父节点
当前提交
4a5e1e825e
共有 2 个文件被更改,包括 43 次插入0 次删除
  1. 41 0
      panda/src/pgraph/nodePath.I
  2. 2 0
      panda/src/pgraph/nodePath.h

+ 41 - 0
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

+ 2 - 0
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.