Ejemplo n.º 1
0
def test_transforms():
    # test transform mapping between nodes
    root = Node()
    n1 = Node(parent=root)
    n2 = Node(parent=n1)
    n3 = Node(parent=root)
    n4 = Node(parent=n3)
    
    n1.transform = STTransform(scale=(0.1, 0.1), translate=(7, 6))
    n2.transform = STTransform(scale=(0.2, 0.3), translate=(5, 4))
    n3.transform = STTransform(scale=(0.4, 0.5), translate=(3, 2))
    n4.transform = STTransform(scale=(0.6, 0.7), translate=(1, 0))

    assert np.allclose(n1.transform.map((0, 0))[:2], (7, 6))
    assert np.allclose(n1.node_transform(root).map((0, 0))[:2], (7, 6))
    assert np.allclose(n2.transform.map((0, 0))[:2], (5, 4))
    assert np.allclose(n2.node_transform(root).map((0, 0))[:2], 
                       (5*0.1+7, 4*0.1+6))
    assert np.allclose(root.node_transform(n1).map((0, 0))[:2],
                       (-7/0.1, -6/0.1))
    assert np.allclose(root.node_transform(n2).map((0, 0))[:2],
                       ((-7/0.1-5)/0.2, (-6/0.1-4)/0.3))

    # just check that we can assemble transforms correctly mapping across the
    # scenegraph
    assert n2.node_path(n4) == ([n2, n1, root], [n3, n4])
    assert n4.node_path(n2) == ([n4, n3, root], [n1, n2])
    assert n2.node_path(root) == ([n2, n1, root], [])
    assert root.node_path(n4) == ([root], [n3, n4])
    assert n2.node_path_transforms(n4) == [n4.transform.inverse, 
                                           n3.transform.inverse, 
                                           n1.transform, n2.transform]
    assert n4.node_path_transforms(n2) == [n2.transform.inverse,
                                           n1.transform.inverse,
                                           n3.transform, n4.transform]
    
    pts = np.array([[0, 0], [1, 1], [-56.3, 800.2]])
    assert np.all(n2.node_transform(n1).map(pts) == n2.transform.map(pts))
    assert np.all(n2.node_transform(root).map(pts) == 
                  n1.transform.map(n2.transform.map(pts)))
    assert np.all(n1.node_transform(n3).map(pts) == 
                  n3.transform.inverse.map(n1.transform.map(pts)))
    assert np.all(n2.node_transform(n3).map(pts) == 
                  n3.transform.inverse.map(
                      n1.transform.map(n2.transform.map(pts))))
    assert np.all(n2.node_transform(n4).map(pts) == 
                  n4.transform.inverse.map(n3.transform.inverse.map(
                      n1.transform.map(n2.transform.map(pts)))))

    # test transforms still work after reparenting
    n3.parent = n1
    assert np.all(n2.node_transform(n4).map(pts) == n4.transform.inverse.map(
        n3.transform.inverse.map(n2.transform.map(pts))))
    
    # test transform simplification
    assert np.all(n2.node_transform(n4).map(pts) == 
                  n2.node_transform(n4).simplified.map(pts))    
Ejemplo n.º 2
0
def test_transforms():
    # test transform mapping between nodes
    root = Node()
    n1 = Node(parent=root)
    n2 = Node(parent=n1)
    n3 = Node(parent=root)
    n4 = Node(parent=n3)

    n1.transform = STTransform(scale=(0.1, 0.1), translate=(7, 6))
    n2.transform = STTransform(scale=(0.2, 0.3), translate=(5, 4))
    n3.transform = STTransform(scale=(0.4, 0.5), translate=(3, 2))
    n4.transform = STTransform(scale=(0.6, 0.7), translate=(1, 0))

    assert np.allclose(n1.transform.map((0, 0))[:2], (7, 6))
    assert np.allclose(n1.node_transform(root).map((0, 0))[:2], (7, 6))
    assert np.allclose(n2.transform.map((0, 0))[:2], (5, 4))
    assert np.allclose(n2.node_transform(root).map((0, 0))[:2], 
                       (5*0.1+7, 4*0.1+6))
    assert np.allclose(root.node_transform(n1).map((0, 0))[:2],
                       (-7/0.1, -6/0.1))
    assert np.allclose(root.node_transform(n2).map((0, 0))[:2],
                       ((-7/0.1-5)/0.2, (-6/0.1-4)/0.3))

    # just check that we can assemble transforms correctly mapping across the
    # scenegraph
    assert n2.node_path(n4) == ([n2, n1, root], [n3, n4])
    assert n4.node_path(n2) == ([n4, n3, root], [n1, n2])
    assert n2.node_path(root) == ([n2, n1, root], [])
    assert root.node_path(n4) == ([root], [n3, n4])
    assert n2.node_path_transforms(n4) == [n4.transform.inverse, 
                                           n3.transform.inverse, 
                                           n1.transform, n2.transform]
    assert n4.node_path_transforms(n2) == [n2.transform.inverse,
                                           n1.transform.inverse,
                                           n3.transform, n4.transform]

    pts = np.array([[0, 0], [1, 1], [-56.3, 800.2]])
    assert np.all(n2.node_transform(n1).map(pts) == n2.transform.map(pts))
    assert np.all(n2.node_transform(root).map(pts) == 
                  n1.transform.map(n2.transform.map(pts)))
    assert np.all(n1.node_transform(n3).map(pts) == 
                  n3.transform.inverse.map(n1.transform.map(pts)))
    assert np.all(n2.node_transform(n3).map(pts) == 
                  n3.transform.inverse.map(
                      n1.transform.map(n2.transform.map(pts))))
    assert np.all(n2.node_transform(n4).map(pts) == 
                  n4.transform.inverse.map(n3.transform.inverse.map(
                      n1.transform.map(n2.transform.map(pts)))))

    # test transforms still work after reparenting
    n3.parent = n1
    assert np.all(n2.node_transform(n4).map(pts) == n4.transform.inverse.map(
        n3.transform.inverse.map(n2.transform.map(pts))))

    # test transform simplification
    assert np.all(n2.node_transform(n4).map(pts) == 
                  n2.node_transform(n4).simplified.map(pts))    
Ejemplo n.º 3
0
def test_graph():
    # Graph looks like:
    #
    #  a --- b --- c --- d --- g
    #         \            /
    #          --- e --- f
    #
    a = Node(name='a')
    b = Node(name='b', parent=a)
    c = Node(name='c', parent=b)
    d = Node(name='d', parent=c)
    e = Node(name='e', parent=b)
    f = Node(name='f', parent=e)
    g = Node(name='g', )
    g.parents = (f, d)

    assert a.parent is None
    assert b.node_path(a) == ([b, a], [])
    assert a.node_path(b) == ([a], [b])
    assert c.node_path(a) == ([c, b, a], [])
    assert a.node_path(c) == ([a], [b, c])
    assert d.node_path(f) == ([d, c, b], [e, f])
    assert f.node_path(d) == ([f, e, b], [c, d])
    try:
        g.node_path(b)
        raise Exception("Should have raised RuntimeError")
    except RuntimeError:
        pass
Ejemplo n.º 4
0
def test_graph():
    # Graph looks like:
    # 
    #  a --- b --- c --- d --- g
    #         \            /
    #          --- e --- f 
    #
    a = Node(name='a')
    b = Node(name='b', parent=a)
    c = Node(name='c', parent=b)
    d = Node(name='d', parent=c)
    e = Node(name='e', parent=b)
    f = Node(name='f', parent=e)
    g = Node(name='g', )
    g.parents = (f, d)
    
    assert a.parent is None
    assert b.node_path(a) == ([b, a], [])
    assert a.node_path(b) == ([a], [b])
    assert c.node_path(a) == ([c, b, a], [])
    assert a.node_path(c) == ([a], [b, c])
    assert d.node_path(f) == ([d, c, b], [e, f])
    assert f.node_path(d) == ([f, e, b], [c, d])
    try:
        g.node_path(b)
        raise Exception("Should have raised RuntimeError")
    except RuntimeError:
        pass