示例#1
0
 def test_intersect(self):
     a = Point(0, 0)
     b = Point(0, 2)
     c = Point(2, 2)
     d = Point(2, 0)
     r = Rectangle(a, b, c, d)
     s = Segment(a, c)
     assert r.intersect(s) == [a, c]
示例#2
0
    def test_contains(self):
        a = Point(0, 0)
        b = Point(0, 2)
        c = Point(2, 2)
        d = Point(2, 0)
        r = Rectangle(a, b, c, d)
        assert r.contains(Point(1, 1))

        a = Point(0, 0, 1)
        b = Point(1, 3, 1)
        c = Point(2, 0, 1)
        d = Point(1, 1, 1)
        p = Polygon(a, b, c, d)

        assert p.contains(Point(0.5, 1, 1))
        assert not p.contains(Point(0.5, 1, 0))
示例#3
0
 def test_edges(self):
     a = Point(0, 0)
     b = Point(0, 2)
     c = Point(2, 2)
     d = Point(2, 0)
     r = Rectangle(a, b, c, d)
     assert r.edges == [Segment(a, b), Segment(b, c), Segment(c, d), Segment(d, a)]
示例#4
0
    def test_getitem(self):
        a = Point(0, 0, 0)
        b = Point(1, 0, 0)
        c = Point(0, 1, 0)
        d = Point(0, 0, 1)
        cube = Cuboid(a, b, c, d)

        x, y, z = b - a, c - a, d - a
        yz = Rectangle(a, a + z, a + y + z, a + y)
        xz = Rectangle(a, a + x, a + x + z, a + z)
        xy = Rectangle(a, a + x, a + x + y, a + y)

        assert isinstance(cube[0], Polygon)
        assert cube[0] == yz
        assert cube[1] == xz
        assert cube[2] == xy
        assert cube[0, 0] == a
示例#5
0
    def test_centroid(self):
        a = Point(0, 0, 1)
        b = Point(2, 0, 1)
        c = Point(2, 2, 1)
        d = Point(0, 2, 1)
        r = Rectangle(a, b, c, d)

        assert r.centroid == Point(1, 1, 1)
示例#6
0
    def test_transformation(self):
        a = Point(0, 0)
        b = Point(0, 1)
        c = Point(2, 1)
        d = Point(2, 0)
        r = Rectangle(a, b, c, d)
        r2 = rotation(np.pi/2)*r

        assert r.area == r2.area
        assert r2.contains(Point(-0.5, 1.5))

        l = Line(Point(0, 0, -10), Point(0, 0, 10))
        r = Rectangle(Point(-10, -10, 0), Point(10, -10, 0), Point(10, 10, 0), Point(-10, 10, 0))
        t = rotation(np.pi/6, Point(1, 0, 0))

        assert r.intersect(l) == [Point(0, 0, 0)]
        assert (t*r).intersect(l) == [Point(0, 0, 0)]
示例#7
0
    def test_copy(self):
        a = Point(0, 0)
        b = Point(0, 2)
        c = Point(2, 2)
        d = Point(2, 0)

        r1 = Rectangle(a, b, c, d)
        p1 = RegularPolygon(a, 1, 6)

        r2 = r1.copy()
        p2 = p1.copy()

        assert r1 == r2
        assert r1 is not r2
        assert r1.vertices == r2.vertices
        assert p1 == p2
        assert p1 is not p2
        assert p1.vertices == p2.vertices
示例#8
0
    def test_getitem(self):
        a = Point(0, 0, 1)
        b = Point(2, 0, 1)
        c = Point(2, 2, 1)
        d = Point(0, 2, 1)
        r = Rectangle(a, b, c, d)

        assert r[0] == a
        assert r[1] == b
        assert r[2] == c
        assert r[3] == d
示例#9
0
    def test_intersect(self):
        a = Point(0, 0)
        b = Point(0, 2)
        c = Point(2, 2)
        d = Point(2, 0)
        r = Rectangle(a, b, c, d)
        s = Segment(a, c)
        assert r.intersect(s) == [a, c]

        foo = Rectangle(
            Point(148.06094049635456, 10.151151779987144, 60.522099063951394),
            Point(129.78569335065157, -42.129870038015355, 60.54878245579997),
            Point(85.91668756014471, -26.79517716452499, 60.41723371984577),
            Point(104.19193470584759, 25.485844653477507, 60.390550327997126),
        )
        bar = Segment(
            Point(-38.9592826559563, -6.703132040294841, 64.78693707404751),
            Point(133.01711836447913, -6.633886165038485, 54.310634812542006),
        )

        assert len(foo.intersect(bar)) == 0

        p1 = Point(0, 0)
        p2 = Point(100, 0)
        p3 = Point(100, 100)
        p4 = Point(0, 100)
        square = Polygon(p1, p2, p3, p4)
        line1 = Line(p3, p4)
        line2 = Line(Point(100, 100 - 1e-8), Point(0, 100 + 1e-8))

        assert square.intersect(line1) == [p3, p4]
        assert len(square.intersect(line2)) == 2
示例#10
0
    def test_contains(self):
        a = Point(0, 0)
        b = Point(0, 2)
        c = Point(2, 2)
        d = Point(2, 0)
        r = Rectangle(a, b, c, d)

        assert r.contains(a)
        assert r.contains(b)
        assert r.contains(c)
        assert r.contains(d)
        assert r.contains(Point(1, 1))
        assert not r.contains(Point([1, 1, 0]))

        a = Point(0, 0, 1)
        b = Point(1, 3, 1)
        c = Point(2, 0, 1)
        d = Point(1, 1, 1)
        p = Polygon(a, b, c, d)

        assert p.contains(Point(0.5, 1, 1))
        assert not p.contains(Point(0.5, 1, 0))
        assert np.all(p.contains(PointCollection([Point(0.5, 1, 1), Point(1.5, 1, 1)])))
        assert np.all(p.contains(PointCollection([a, c, d])))

        a = Point([1, 1, 2, 0])
        b = Point([-1, 1, 2, 0])
        c = Point([-1, -1, 2, 0])
        d = Point([1, -1, 2, 0])
        p = Polygon(a, b, c, d)

        assert all(p.contains(PointCollection([a, b, c, d])))
        assert p.contains(Point([0, 0, 1, 0]))
        assert not p.contains(Point([1, 1, 1, 0]))
示例#11
0
    def test_centroid(self):
        a = Point(0, 0, 1)
        b = Point(2, 0, 1)
        c = Point(2, 2, 1)
        d = Point(0, 2, 1)
        t = Triangle(a, b, c)
        r = Rectangle(a, b, c, d)

        s1, s2, s3 = t.edges
        l1 = s1.midpoint.join(c)
        l2 = s2.midpoint.join(a)

        assert t.centroid == (a + b + c) / 3
        assert t.centroid == l1.meet(l2)
        assert r.centroid == Point(1, 1, 1)
示例#12
0
def test_dist():
    p = Point(0, 0)
    q = Point(1, 0)
    assert np.isclose(dist(p, q), 1)

    p = Point(1000000, 0)
    q = Point(1000001, 0)
    assert np.isclose(dist(p, q), 1)

    p1 = Point(1j, 0, 0, 2j)
    p2 = Point(0, 2j, 0, 0)
    assert np.isclose(dist(p1, p2), 3)

    p1 = Point(1, 0, 0)
    p2 = Point([1, 0, 0, 0])
    assert dist(p1, p2) == dist(p2, p1) == np.inf

    p1 = Point(0, 0, 0)
    p2 = Point(1, 0, 0)
    assert np.isclose(dist(p1, p2), 1)

    e = Plane(1, 0, 0, 0)
    assert np.isclose(dist(e, p2), 1)

    p = Point(1, 2, 0)
    l = Line(Point(0, 0, 0), Point(3, 0, 0))
    assert np.isclose(dist(p, l), 2)

    l = Line(p2, Point(1, 1, 0))
    assert np.isclose(dist(l, e), 1)
    assert np.isclose(dist(l, p1), 1)

    p = Point(0, 0)
    poly = Rectangle(Point(-1, 1), Point(1, 1), Point(1, 2), Point(-1, 2))
    assert np.isclose(dist(p, poly), 1)

    p = Point(0, 0, 0)
    poly = Rectangle(Point(-1, -1, 1), Point(1, -1, 1), Point(1, 1, 1),
                     Point(-1, 1, 1))
    assert np.isclose(dist(p, poly), 1)
    assert np.isclose(dist(Point(-1, -1, 0), poly), 1)
    assert np.isclose(dist(Point(-4, 0, -3), poly), 5)

    a = Point(0, 0, 0)
    b = Point(1, 0, 0)
    c = Point(0, 1, 0)
    d = Point(0, 0, 1)
    cube = Cuboid(a, b, c, d)
    # TODO: speed this up
    assert np.isclose(dist(p, cube), 0)
    assert np.isclose(dist(Point(-1, 0, 0), cube), 1)
    assert np.isclose(dist(Point(0.5, 0.5, 2), cube), 1)

    p = PointCollection([(1, 0, 1), (1, 1, 0)], homogenize=True)
    e = PlaneCollection([(0, 0, 1, 0), (1, 0, 0, 0)])
    s = Segment(Point(1, 0, 1), Point(1, 2, 1))
    # TODO: speed this up
    assert np.allclose(dist(e, p), 1)
    assert np.allclose(dist(p, cube), 0)
    assert np.allclose(dist(p, poly), [0, 1])
    assert np.allclose(dist(p, s), [0, 1])