Browse Source

tests: add various test cases to test egg loading and transforms

Used to track down and reproduce the issue in #228.
rdb 7 years ago
parent
commit
838d238f6e

+ 134 - 0
tests/egg/test_egg_transform.py

@@ -0,0 +1,134 @@
+import pytest
+from panda3d import core
+
+# Skip these tests if we can't import egg.
+egg = pytest.importorskip("panda3d.egg")
+
+
+EGG_TRANSFORM_MISSING = """
+<CoordinateSystem> { %s }
+<Group> {
+}
+"""
+
+EGG_TRANSFORM_EMPTY = """
+<CoordinateSystem> { %s }
+<Group> {
+    <Transform> {
+    }
+}
+"""
+
+EGG_TRANSFORM_IDENT = """
+<CoordinateSystem> { %s }
+<Group> {
+    <Transform> {
+        <Matrix4> {
+            1 0 0 0
+            0 1 0 0
+            0 0 1 0
+            0 0 0 1
+        }
+    }
+}
+"""
+
+EGG_TRANSFORM_MATRIX = """
+<CoordinateSystem> { %s }
+<Group> {
+    <Transform> {
+        <Matrix4> {
+            5 2 -3 4
+            5 6 7 8
+            9 1 -3 2
+            5 2 5 2
+        }
+    }
+}
+"""
+
+COORD_SYSTEMS = {
+    core.CS_zup_right: "zup-right",
+    core.CS_yup_right: "yup-right",
+    core.CS_zup_left: "zup-left",
+    core.CS_yup_left: "yup-left",
+}
+
+def read_egg_string(string):
+    """Reads an EggData from a string."""
+    stream = core.StringStream(string.encode('utf-8'))
+    data = egg.EggData()
+    assert data.read(stream)
+    return data
+
+
[email protected]("coordsys", COORD_SYSTEMS.keys())
+def test_egg_transform_missing(coordsys):
+    data = read_egg_string(EGG_TRANSFORM_MISSING % COORD_SYSTEMS[coordsys])
+    assert data.get_coordinate_system() == coordsys
+
+    child, = data.get_children()
+    assert not child.has_transform3d()
+    assert child.transform_is_identity()
+
+    assert child.get_vertex_frame() == core.Mat4D.ident_mat()
+    assert child.get_node_frame() == core.Mat4D.ident_mat()
+    assert child.get_vertex_frame_inv() == core.Mat4D.ident_mat()
+    assert child.get_node_frame_inv() == core.Mat4D.ident_mat()
+    assert child.get_vertex_to_node() == core.Mat4D.ident_mat()
+    assert child.get_node_to_vertex() == core.Mat4D.ident_mat()
+
+
[email protected]("coordsys", COORD_SYSTEMS.keys())
+def test_egg_transform_empty(coordsys):
+    data = read_egg_string(EGG_TRANSFORM_EMPTY % COORD_SYSTEMS[coordsys])
+    assert data.get_coordinate_system() == coordsys
+
+    child, = data.get_children()
+    assert not child.has_transform3d()
+    assert child.transform_is_identity()
+
+    assert child.get_vertex_frame() == core.Mat4D.ident_mat()
+    assert child.get_node_frame() == core.Mat4D.ident_mat()
+    assert child.get_vertex_frame_inv() == core.Mat4D.ident_mat()
+    assert child.get_node_frame_inv() == core.Mat4D.ident_mat()
+    assert child.get_vertex_to_node() == core.Mat4D.ident_mat()
+    assert child.get_node_to_vertex() == core.Mat4D.ident_mat()
+
+
[email protected]("coordsys", COORD_SYSTEMS.keys())
+def test_egg_transform_ident(coordsys):
+    data = read_egg_string(EGG_TRANSFORM_IDENT % COORD_SYSTEMS[coordsys])
+    assert data.get_coordinate_system() == coordsys
+
+    child, = data.get_children()
+    assert child.has_transform3d()
+    assert child.transform_is_identity()
+
+    assert child.get_vertex_frame() == core.Mat4D.ident_mat()
+    assert child.get_node_frame() == core.Mat4D.ident_mat()
+    assert child.get_vertex_frame_inv() == core.Mat4D.ident_mat()
+    assert child.get_node_frame_inv() == core.Mat4D.ident_mat()
+    assert child.get_vertex_to_node() == core.Mat4D.ident_mat()
+    assert child.get_node_to_vertex() == core.Mat4D.ident_mat()
+
+
[email protected]("coordsys", COORD_SYSTEMS.keys())
+def test_egg_transform_matrix(coordsys):
+    data = read_egg_string(EGG_TRANSFORM_MATRIX % COORD_SYSTEMS[coordsys])
+    assert data.get_coordinate_system() == coordsys
+
+    mat = core.Mat4D(5, 2, -3, 4, 5, 6, 7, 8, 9, 1, -3, 2, 5, 2, 5, 2)
+    mat_inv = core.invert(mat)
+
+    child, = data.get_children()
+    assert child.has_transform3d()
+    assert not child.transform_is_identity()
+    assert child.get_transform3d() == mat
+
+    assert child.get_vertex_frame() == core.Mat4D.ident_mat()
+    assert child.get_node_frame() == mat
+    assert child.get_vertex_frame_inv() == core.Mat4D.ident_mat()
+    assert child.get_node_frame_inv() == mat_inv
+    assert child.get_vertex_to_node() == mat_inv
+    assert child.get_node_to_vertex() == mat

+ 103 - 0
tests/egg2pg/test_egg_coordsys.py

@@ -0,0 +1,103 @@
+import pytest
+from panda3d import core
+
+# Skip these tests if we can't import egg.
+egg = pytest.importorskip("panda3d.egg")
+
+
+COORD_SYSTEMS = [core.CS_zup_right, core.CS_yup_right, core.CS_zup_left, core.CS_yup_left]
+
+
[email protected]("egg_coordsys", COORD_SYSTEMS)
[email protected]("coordsys", COORD_SYSTEMS)
+def test_egg2pg_transform_ident(egg_coordsys, coordsys):
+    # Ensures that an identity matrix always remains untouched.
+    group = egg.EggGroup("group")
+    group.add_matrix4(core.Mat4D.ident_mat())
+    assert group.transform_is_identity()
+
+    assert group.get_vertex_frame() == core.Mat4D.ident_mat()
+    assert group.get_node_frame() == core.Mat4D.ident_mat()
+    assert group.get_vertex_frame_inv() == core.Mat4D.ident_mat()
+    assert group.get_node_frame_inv() == core.Mat4D.ident_mat()
+    assert group.get_vertex_to_node() == core.Mat4D.ident_mat()
+    assert group.get_node_to_vertex() == core.Mat4D.ident_mat()
+
+    data = egg.EggData()
+    data.set_coordinate_system(egg_coordsys)
+    data.add_child(group)
+
+    root = egg.load_egg_data(data, coordsys)
+    assert root
+    node, = root.children
+
+    assert node.transform.is_identity()
+
+
[email protected]("coordsys", COORD_SYSTEMS)
+def test_egg2pg_transform_mat_unchanged(coordsys):
+    # Ensures that the matrix remains unchanged if coordinate system is same.
+    mat = (5, 2, -3, 4, 5, 6, 7, 8, 9, 1, -3, 2, 5, 2, 5, 2)
+    group = egg.EggGroup("group")
+    group.add_matrix4(mat)
+    assert not group.transform_is_identity()
+
+    assert group.get_vertex_frame() == core.Mat4D.ident_mat()
+    assert group.get_node_frame() == mat
+    assert group.get_vertex_frame_inv() == core.Mat4D.ident_mat()
+    assert group.get_node_frame_inv() == core.invert(mat)
+    assert group.get_vertex_to_node() == core.invert(mat)
+    assert group.get_node_to_vertex() == mat
+
+    data = egg.EggData()
+    data.set_coordinate_system(coordsys)
+    data.add_child(group)
+
+    root = egg.load_egg_data(data, coordsys)
+    assert root
+    node, = root.children
+
+    assert node.transform.mat == mat
+
+
[email protected]("egg_coordsys", COORD_SYSTEMS)
[email protected]("coordsys", COORD_SYSTEMS)
+def test_egg2pg_transform_pos3d(egg_coordsys, coordsys):
+    vpool = egg.EggVertexPool("vpool")
+    vtx = vpool.make_new_vertex(core.Point3D.rfu(-8, 0.5, 4.5, egg_coordsys))
+
+    point = egg.EggPoint()
+    point.add_vertex(vtx)
+
+    group = egg.EggGroup("group")
+    group.add_translate3d(core.Point3D.rfu(1, 2, 3, egg_coordsys))
+    assert not group.transform_is_identity()
+    group.add_child(point)
+
+    mat = group.get_transform3d()
+    assert group.get_vertex_frame() == core.Mat4D.ident_mat()
+    assert group.get_node_frame() == mat
+    assert group.get_vertex_frame_inv() == core.Mat4D.ident_mat()
+    assert group.get_node_frame_inv() == core.invert(mat)
+    assert group.get_vertex_to_node() == core.invert(mat)
+    assert group.get_node_to_vertex() == mat
+
+    assert group.get_vertex_frame_ptr() is None
+    assert group.get_vertex_frame_inv_ptr() is None
+
+    data = egg.EggData()
+    data.set_coordinate_system(egg_coordsys)
+    data.add_child(vpool)
+    data.add_child(group)
+
+    root = egg.load_egg_data(data, coordsys)
+    assert root
+    node, = root.children
+
+    # Ensure the node has the expected position.
+    assert node.transform.pos == core.Point3.rfu(1, 2, 3, coordsys)
+
+    # Get the location of the vertex.  This is a quick, hacky way to get it.
+    point = core.NodePath(node).get_tight_bounds()[0]
+    assert point == core.Point3.rfu(-8, 0.5, 4.5, coordsys)
+

+ 20 - 0
tests/linmath/test_matrix_invert.py

@@ -0,0 +1,20 @@
+import pytest
+from panda3d import core
+
+
[email protected]("type", (core.Mat4, core.Mat4D))
+def test_mat4_invert(type):
+    mat = type((1, 0, 0, 0,
+                0, 1, 0, 0,
+                0, 0, 1, 0,
+                1, 2, 3, 1))
+    inv = type()
+    assert inv.invert_from(mat)
+
+    assert inv == type(( 1,  0,  0, 0,
+                         0,  1,  0, 0,
+                         0,  0,  1, 0,
+                        -1, -2, -3, 1))
+
+    assert (mat * inv).is_identity()
+    assert (inv * mat).is_identity()