diff --git a/panda/src/pgraph/nodePath.cxx b/panda/src/pgraph/nodePath.cxx index 71bbb3a561..41e02b0821 100644 --- a/panda/src/pgraph/nodePath.cxx +++ b/panda/src/pgraph/nodePath.cxx @@ -71,6 +71,7 @@ #include "bam.h" #include "bamWriter.h" #include "datagramBuffer.h" +#include "weakNodePath.h" // stack seems to overflow on Intel C++ at 7000. If we need more than 7000, // need to increase stack size. @@ -5164,6 +5165,55 @@ get_stashed_ancestor(Thread *current_thread) const { return not_found(); } +/** + * Returns true if the two paths are equivalent; that is, if they contain the + * same list of nodes in the same order. + */ +bool NodePath:: +operator == (const WeakNodePath &other) const { + return _head == other._head; +} + +/** + * Returns true if the two paths are not equivalent. + */ +bool NodePath:: +operator != (const WeakNodePath &other) const { + return _head != other._head; +} + +/** + * Returns true if this NodePath sorts before the other one, false otherwise. + * The sorting order of two nonequivalent NodePaths is consistent but + * undefined, and is useful only for storing NodePaths in a sorted container + * like an STL set. + */ +bool NodePath:: +operator < (const WeakNodePath &other) const { + return _head < other._head; +} + +/** + * Returns a number less than zero if this NodePath sorts before the other + * one, greater than zero if it sorts after, or zero if they are equivalent. + * + * Two NodePaths are considered equivalent if they consist of exactly the same + * list of nodes in the same order. Otherwise, they are different; different + * NodePaths will be ranked in a consistent but undefined ordering; the + * ordering is useful only for placing the NodePaths in a sorted container + * like an STL set. + */ +int NodePath:: +compare_to(const WeakNodePath &other) const { + // Nowadays, the NodePathComponents at the head are pointerwise equivalent + // if and only if the NodePaths are equivalent. So we only have to compare + // pointers. + if (_head != other._head) { + return _head < other._head ? -1 : 1; + } + return 0; +} + /** * Returns true if all of the nodes described in the NodePath are connected, * or false otherwise. diff --git a/panda/src/pgraph/nodePath.h b/panda/src/pgraph/nodePath.h index c5a3902c32..11d7437113 100644 --- a/panda/src/pgraph/nodePath.h +++ b/panda/src/pgraph/nodePath.h @@ -63,6 +63,7 @@ class SamplerState; class Shader; class ShaderBuffer; class ShaderInput; +class WeakNodePath; // // A NodePath is the fundamental unit of high-level interaction with the scene @@ -879,6 +880,11 @@ PUBLISHED: INLINE bool operator < (const NodePath &other) const; INLINE int compare_to(const NodePath &other) const; + bool operator == (const WeakNodePath &other) const; + bool operator != (const WeakNodePath &other) const; + bool operator < (const WeakNodePath &other) const; + int compare_to(const WeakNodePath &other) const; + // Miscellaneous bool verify_complete(Thread *current_thread = Thread::get_current_thread()) const; diff --git a/tests/pgraph/test_nodepath.py b/tests/pgraph/test_nodepath.py index a625533634..9c97d4260a 100644 --- a/tests/pgraph/test_nodepath.py +++ b/tests/pgraph/test_nodepath.py @@ -79,3 +79,19 @@ def test_nodepath_transform_composition(): leg2 = node1.get_transform().compose(node3.get_transform()) relative_transform = leg1.get_inverse().compose(leg2) assert np1.get_transform(np2) == relative_transform + + +def test_weak_nodepath_comparison(): + from panda3d.core import NodePath, WeakNodePath + + path = NodePath("node") + weak = WeakNodePath(path) + + assert path == weak + assert weak == path + assert weak <= path + assert path <= weak + + assert hash(path) == hash(weak) + assert weak.get_node_path() == path + assert weak.node() == path.node()