def test_transform(self): a = PointCollection([(1, 0), (0, 1)], homogenize=True) assert translation(1, 1) * a == PointCollection([(2, 1), (1, 2)], homogenize=True) assert rotation(np.pi / 2) * a == PointCollection([(0, 1), (-1, 0)], homogenize=True)
def test_meet(self): # three planes a = PlaneCollection([Plane(1, 0, 0, 0), Plane(1, 0, 0, -1)]) b = PlaneCollection([Plane(0, 1, 0, 0), Plane(0, 1, 0, -1)]) c = PlaneCollection([Plane(0, 0, 1, 0), Plane(0, 0, 1, -1)]) assert meet(a, b, c) == PointCollection([Point(0, 0, 0), Point(1, 1, 1)]) # two planes l = a.meet(b) m = LineCollection( [Line(Point(0, 0, 0), Point(0, 0, 1)), Line(Point(1, 1, 0), Point(1, 1, 1))] ) assert l == m # two lines in 2D a = LineCollection([Line(0, 1, 0), Line(0, 1, -1)]) b = LineCollection([Line(1, 0, 0), Line(1, 0, -1)]) assert a.meet(b) == PointCollection([Point(0, 0), Point(1, 1)]) # two lines in 3D a = LineCollection( [Line(Point(0, 0, 0), Point(0, 0, 1)), Line(Point(1, 0, 0), Point(1, 0, 1))] ) b = LineCollection( [Line(Point(0, 0, 0), Point(0, 1, 0)), Line(Point(1, 0, 0), Point(1, 1, 0))] ) assert a.meet(b) == PointCollection([Point(0, 0, 0), Point(1, 0, 0)]) # plane and line a = LineCollection( [Line(Point(0, 0, 0), Point(0, 0, 1)), Line(Point(1, 0, 0), Point(1, 0, 1))] ) b = PlaneCollection([Plane(0, 0, 1, 0), Plane(0, 0, 1, -1)]) assert a.meet(b) == PointCollection([Point(0, 0, 0), Point(1, 0, 1)])
def test_angle_bisectors(): a = Point(0, 0) b = Point(1, 1) c = Point(1, 0) l = Line(a, b) m = Line(a, c) q, r = angle_bisectors(l, m) assert is_perpendicular(q, r) assert np.isclose(angle(l, q), angle(q, m)) p1 = Point(0, 0, 0) p2 = Point(0, 1, 0) p3 = Point(1, 0, 0) l = Line(p1, p2) m = Line(p1, p3) q, r = angle_bisectors(l, m) assert is_perpendicular(q, r) assert np.isclose(angle(l, q), angle(q, m)) p1 = PointCollection([(0, 0), (0, 0)], homogenize=True) p2 = PointCollection([(1, 1), (0, 1)], homogenize=True) p3 = PointCollection([(1, 0), (1, 1)], homogenize=True) l = LineCollection(p1, p2) m = LineCollection(p1, p3) q, r = angle_bisectors(l, m) assert all(is_perpendicular(q, r)) assert np.allclose(angle(l, q), angle(q, m))
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]))
def test_perpendicular(self): p1 = PointCollection([(1, 1, 0), (1, 1, 5)], homogenize=True) p2 = PointCollection([(2, 1, 0), (2, 1, 5)], homogenize=True) p3 = PointCollection([(0, 0, 0), (0, 0, 5)], homogenize=True) l = LineCollection(p1, p2) m = l.perpendicular(p1) assert l.meet(m) == p1 assert all(is_perpendicular(l, m)) m = l.perpendicular( p3 + PointCollection([(1, 1, 0), (0, 0, 0)], homogenize=True) ) assert all(is_perpendicular(l, m)) e = PlaneCollection(l, p3) m = e.perpendicular(p1) assert e.meet(m) == p1 assert all(is_perpendicular(l, m)) m = e.perpendicular(p1 + PointCollection([m.direction[0], Point(0, 0, 0)])) assert e.meet(m) == p1 assert all(is_perpendicular(l, m))
def test_project(self): p1 = PointCollection([(1, 1, 0), (1, 1, 5)], homogenize=True) p2 = PointCollection([(2, 1, 0), (2, 1, 5)], homogenize=True) p3 = PointCollection([(0, 0, 0), (0, 0, 5)], homogenize=True) l = LineCollection(p1, p2) assert l.project(p3) == PointCollection([(0, 1, 0), (0, 1, 5)], homogenize=True) e = PlaneCollection([(0, 1, 0, -1), (0, 1, 0, -2)]) assert e.project(p3) == PointCollection([(0, 1, 0), (0, 2, 5)], homogenize=True)
def test_intersect(self): a = PointCollection([(0, 0), (0, 0)], homogenize=True) b = PointCollection([(2, 0), (4, 0)], homogenize=True) c = PointCollection([(2, 2), (4, 4)], homogenize=True) d = PointCollection([(0, 2), (0, 4)], homogenize=True) s1 = SegmentCollection(a, c) s2 = SegmentCollection(b, d) assert s1.intersect(s2) == PointCollection([(1, 1), (2, 2)], homogenize=True) assert s1.intersect(Line(Point(0, 0), Point(1, 1))).size == 0
def test_contains(self): p = PointCollection([(0, 0), (1, 0)], homogenize=True) q = PointCollection([(2, 1), (3, 1)], homogenize=True) s = SegmentCollection(p, q) assert all(s.contains(p)) assert all(s.contains(q)) assert all(s.contains(0.5 * (p + q))) assert not any(s.contains(p - q)) assert not any(s.contains(Point([2, 1, 0])))
def test_arithmetic(self): a = PointCollection([Point(0, 1), Point(0, 1)]) b = PointCollection([Point(1, 0), Point(1, 0)]) c = PointCollection([Point(1, 1), Point(1, 1)]) assert a + b == c assert a - c == -b assert 2 * a + 2 * b == 2 * c assert (2 * a + 2 * b) / 2 == c assert a + Point(1, 0) == c
def test_is_coplanar(): p1 = Point(1, 1, 0) p2 = Point(2, 1, 0) p3 = Point(3, 4, 0) p4 = Point(0, 2, 0) assert is_coplanar(p1, p2, p3, p4) p1 = PointCollection([(1, 0), (1, 1)], homogenize=True) p2 = PointCollection([(2, 0), (2, 1)], homogenize=True) p3 = PointCollection([(3, 0), (3, 1)], homogenize=True) p4 = PointCollection([(4, 0), (4, 1)], homogenize=True) assert all(is_coplanar(p1, p2, p3, p4))
def test_is_collinear(): p1 = Point(1, 0) p2 = Point(2, 0) p3 = Point(3, 0) l = Line(p1, p2) assert l.contains(p3) assert is_collinear(p1, p2, p3) p1 = PointCollection([(1, 0), (1, 1)], homogenize=True) p2 = PointCollection([(2, 0), (2, 1)], homogenize=True) p3 = PointCollection([(3, 0), (3, 1)], homogenize=True) p4 = PointCollection([(4, 0), (4, 1)], homogenize=True) assert all(is_collinear(p1, p2, p3, p4))
def test_intersection(self): c = Circle(Point(0, 2), 2) l = Line(Point(-1, 2), Point(1, 2)) assert c.contains(Point(0, 0)) assert c.intersect(l) == [Point(-2, 2), Point(2, 2)] assert c.intersect(l - Point(0, 2)) == [Point(0, 0)] l = LineCollection( [Line(Point(-1, 2), Point(1, 2)), Line(Point(0, 2), Point(0, 0))]) assert c.intersect(l) == [ PointCollection([Point(-2, 2), Point(0, 0)]), PointCollection([Point(2, 2), Point(0, 4)]) ]
def test_tangent(self): q = QuadricCollection([Sphere(), Sphere(radius=2)]) p = PointCollection([Point(1, 0, 0), Point(2, 0, 0)]) assert all(q.contains(p)) assert q.tangent(at=p) == PlaneCollection([Plane(1, 0, 0, -1), Plane(1, 0, 0, -2)]) assert all(q.is_tangent(q.tangent(at=p)))
def test_intersect(self): s = Sphere(Point(0, 0, 2), 2) l = Line(Point(-1, 0, 2), Point(1, 0, 2)) assert s.contains(Point(0, 0, 0)) assert s.intersect(l) == [Point(-2, 0, 2), Point(2, 0, 2)] assert s.intersect(l - Point(0, 0, 2)) == [Point(0, 0, 0)] l = LineCollection([Line(Point(-1, 0, 2), Point(1, 0, 2)), Line(Point(0, 0, 0), Point(0, 0, 2))]) assert s.intersect(l) == [PointCollection([Point(-2, 0, 2), Point(0, 0, 0)]), PointCollection([Point(2, 0, 2), Point(0, 0, 4)])] s = Sphere(Point(0, 0, 0, 2), 2) l = Line(Point(-1, 0, 0, 2), Point(1, 0, 0, 2)) assert s.contains(Point(0, 0, 0, 0)) assert s.intersect(l) == [Point(2, 0, 0, 2), Point(-2, 0, 0, 2)]
def test_harmonic_set(): a = Point(0, 0) b = Point(1, 1) c = Point(3, 3) d = harmonic_set(a, b, c) assert np.isclose(crossratio(a, b, c, d), -1) a = Point(0, 0, 0) b = Point(1, 1, 0) c = Point(3, 3, 0) d = harmonic_set(a, b, c) assert np.isclose(crossratio(a, b, c, d), -1) p1 = PointCollection([(0, 0), (1, 1)], homogenize=True) p2 = PointCollection([(1, 1), (2, 1)], homogenize=True) p3 = PointCollection([(3, 3), (3, 1)], homogenize=True) p4 = harmonic_set(p1, p2, p3) assert np.allclose(crossratio(p1, p2, p3, p4), -1)
def test_contains(self): a = Point(0, 0) b = Point(0, 2) c = Point(2, 1) t = Triangle(a, b, c) assert t.contains(Point(1, 1)) assert all(t.contains(PointCollection([a, b, c]))) assert not t.contains(Point(-1, 1)) assert not t.contains(Point([1, 1, 0]))
def test_is_cocircular(): p = Point(0, 1) t = rotation(np.pi / 3) assert is_cocircular(p, t * p, t * t * p, t * t * t * p) p = PointCollection([(0, 1), (1, 0)], homogenize=True) t = rotation(np.pi / 3) assert all(is_cocircular(p, t * p, t * t * p, t * t * t * p))
def test_intersect(self): c = Circle(Point(0, 2), 2) l = Line(Point(-1, 2), Point(1, 2)) assert c.contains(Point(0, 0)) assert c.intersect(l) == [Point(-2, 2), Point(2, 2)] assert c.intersect(l - Point(0, 2)) == [Point(0, 0)] l = LineCollection( [Line(Point(-1, 2), Point(1, 2)), Line(Point(0, 2), Point(0, 0))]) assert c.intersect(l) == [ PointCollection([Point(-2, 2), Point(0, 0)]), PointCollection([Point(2, 2), Point(0, 4)]), ] c1 = Circle(Point(0, 0), 5) c2 = Circle(Point(8, 0), 5) intersections = c1.intersect(c2) assert Point(4, 3) in intersections assert Point(4, -3) in intersections
def test_intersect(self): q = QuadricCollection([Circle(Point(0, 1), 1), Circle(Point(0, 2), 2)]) l = LineCollection( [Line(Point(-1, 1), Point(1, 1)), Line(Point(-1, 2), Point(1, 2))]) assert q.intersect(l) == [ PointCollection([Point(1, 1), Point(-2, 2)]), PointCollection([Point(-1, 1), Point(2, 2)]), ] q = QuadricCollection( [Sphere(Point(0, 0, 1), 1), Sphere(Point(0, 0, 2), 2)]) l = LineCollection([ Line(Point(-1, 0, 1), Point(1, 0, 1)), Line(Point(-1, 0, 2), Point(1, 0, 2)), ]) m = Line(Point(-1, 0, 2), Point(1, 0, 2)) assert q.intersect(l) == [ PointCollection([Point(-1, 0, 1), Point(-2, 0, 2)]), PointCollection([Point(1, 0, 1), Point(2, 0, 2)]), ] assert q.intersect(m) == [ PointCollection([Point(0, 0, 2), Point(-2, 0, 2)]), PointCollection([Point(0, 0, 2), Point(2, 0, 2)]), ]
def test_angle(): a = Point(0, 0) b = Point(1, 1) c = Point(1, 0) assert np.isclose(angle(a, b, c), np.pi / 4) assert np.isclose(angle(a, c, b), -np.pi / 4) e1 = Plane(1, 0, 0, 0) e2 = Plane(0, 0, 1, 0) assert np.isclose(abs(angle(e1, e2)), np.pi / 2) p1 = Point(0, 0, 0) p2 = Point(0, 1, 0) p3 = Point(1, 0, 0) assert np.isclose(abs(angle(p1, p2, p3)), np.pi / 2) l = Line(p1, p2) m = Line(p1, p3) assert np.isclose(abs(angle(l, m)), np.pi / 2) p1 = PointCollection([(0, 0), (0, 0)], homogenize=True) p2 = PointCollection([(1, 1), (0, 1)], homogenize=True) p3 = PointCollection([(1, 0), (1, 1)], homogenize=True) assert np.allclose(angle(p1, p2, p3), np.pi / 4) l = LineCollection(p1, p2) m = LineCollection(p1, p3) assert np.allclose(angle(l, m), np.pi / 4) e1 = PlaneCollection([(0, 0, 1, 0), (1, 0, 0, 0)]) e2 = PlaneCollection([(1, 1, 1, 0), (0, 1, 0, 0)]) assert np.allclose(angle(e1, e2), [np.arccos(1 / np.sqrt(3)), np.pi / 2])
def test_intersect(self): c = Circle(Point(0, 2), 2) l = Line(Point(-1, 2), Point(1, 2)) assert c.contains(Point(0, 0)) assert c.intersect(l) == [Point(-2, 2), Point(2, 2)] assert c.intersect(l - Point(0, 2)) == [Point(0, 0)] l = LineCollection([Line(Point(-1, 2), Point(1, 2)), Line(Point(0, 2), Point(0, 0))]) assert c.intersect(l) == [PointCollection([Point(-2, 2), Point(0, 0)]), PointCollection([Point(2, 2), Point(0, 4)])] c1 = Circle(Point(0, 0), 5) c2 = Circle(Point(8, 0), 5) intersections = c1.intersect(c2) assert Point(4, 3) in intersections assert Point(4, -3) in intersections c1 = Circle(Point(0, 0)) c2 = Circle(Point(0.5, 0)) i = c1.intersect(c2) assert len(i) == 4 assert len([p for p in i if p.isreal]) == 2
def test_midpoint(self): p = PointCollection([(0, 0), (2, 1)], homogenize=True) q = PointCollection([(2, 2), (4, 1)], homogenize=True) s = SegmentCollection(p, q) assert s.midpoint == PointCollection([(1, 1), (3, 1)], homogenize=True) p = PointCollection([(0, 0, 0), (1, 1, 1)], homogenize=True) q = PointCollection([(0, 2, 0), (3, 3, 3)], homogenize=True) s = SegmentCollection(p, q) assert s.midpoint == PointCollection([(0, 1, 0), (2, 2, 2)], homogenize=True)
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])
def test_isinf(self): assert np.all(~PointCollection([(1, -2), (0, 0)], homogenize=True).isinf) assert np.all(PointCollection([[1, -2, 0], [1j, -2, 0]]).isinf) assert np.all(PointCollection([I, J]).isinf)
def test_homogenize(self): a = PointCollection([(0, 0), (0, 1)], homogenize=True) b = PointCollection([Point(0, 0), Point(0, 1)]) assert a == b
def test_isreal(self): assert np.all( PointCollection([(1, -2, 1), (0, 0, 1), (1, -2, 0)]).isreal) assert np.all(~PointCollection([Point([1j, -2, 0]), I, J]).isreal)
def test_join(self): # 2 points a = PointCollection([Point(0, 0), Point(0, 1)]) b = PointCollection([Point(1, 0), Point(1, 1)]) assert a.join(b) == LineCollection([Line(0, 1, 0), Line(0, 1, -1)]) # 3 points a = PointCollection([Point(0, 0, 0), Point(0, 0, 1)]) b = PointCollection([Point(1, 0, 0), Point(1, 0, 1)]) c = PointCollection([Point(0, 1, 0), Point(0, 1, 1)]) assert join(a, b, c) == PlaneCollection([Plane(0, 0, 1, 0), Plane(0, 0, 1, -1)]) # two lines l = a.join(b) m = a.join(c) assert join(l, m) == PlaneCollection([Plane(0, 0, 1, 0), Plane(0, 0, 1, -1)]) # point and line assert join(a, b.join(c)) == PlaneCollection( [Plane(0, 0, 1, 0), Plane(0, 0, 1, -1)] )