def test_meet(self): p1 = Plane(1, 0, 0, 0) p2 = Plane(0, 0, 1, 0) p3 = Plane(0, 1, 0, 0) # three planes assert meet(p1, p2, p3) == Point(0, 0, 0) # two planes l = p1.meet(p2) m = Line(Point(0, 0, 0), Point(0, 1, 0)) assert l == m # two lines m = Line(Point(0, 0, 0), Point(1, 2, 5)) assert l.meet(m) == Point(0, 0, 0) # plane and line assert p3.meet(l) == Point(0, 0, 0)
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): p1 = Point(1, 1, 0) p2 = Point(2, 1, 0) p3 = Point(3, 4, 0) p4 = Point(0, 2, 0) p = Plane(p1, p2, p3) l = Line(p1, p2) assert p.contains(p4) assert p.contains(l)
def test_perpendicular(self): p = Point(1, 1) l = Line(1, 1, 0) m = l.perpendicular(p) assert m == Line(-1, 1, 0) m = l.perpendicular(Point(0, 0)) assert m == Line(-1, 1, 0) p = Point(1, 1, 0) q = Point(0, 0, 1) l = Line(p, q) m = l.perpendicular(p) assert is_perpendicular(l, m)
def test_components(self): l = Line(1, 2, 3) m = Line(4, 5, 6) g = Line(3, 2, 1) h = Line(6, 5, 4) q = QuadricCollection([Conic.from_lines(l, m), Conic.from_lines(g, h)]) assert q.components == [LineCollection([m, g]), LineCollection([l, h])] e = Plane(1, 2, 3, 4) f = Plane(4, 3, 2, 1) g = Plane(5, 6, 7, 8) h = Plane(8, 7, 6, 5) q = QuadricCollection( [Quadric.from_planes(e, f), Quadric.from_planes(g, h)]) assert q.components == [ PlaneCollection([e, g]), PlaneCollection([f, h]) ]
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_from_points(self): p1 = Point(0, 0) p2 = Point(1, 0) p3 = Point(0, 1) p4 = Point(3, 5) l = Line(p1, p3) M = Transformation.from_points((p1, p1 + Point(1, 1)), (p2, p2 + Point(1, 1)), (p3, p3 + Point(1, 1)), (p4, p4 + Point(1, 1))) assert M*p3 == Point(1, 2) assert (M*l).contains(M*p1) assert (M*l).contains(M*p3)
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_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_is_perpendicular(): l = Line(0, 1, 0) m = Line(1, 0, 0) assert is_perpendicular(l, m) p1 = Point(0, 0, 0) p2 = Point(0, 1, 0) p3 = Point(1, 0, 0) l = Line(p1, p2) m = Line(p1, p3) assert is_perpendicular(l, m) e1 = Plane(p1, p2, p3) e2 = Plane(p1, p2, Point(0, 0, 1)) assert is_perpendicular(e1, e2) l = LineCollection([(0, 1, 0), (0, 1, 0)]) m = LineCollection([(1, 0, 0), (1, -1, 0)]) assert list(is_perpendicular(l, m)) == [True, False] e1 = PlaneCollection([(0, 0, 1, 0), (1, 0, 0, 0)]) e2 = PlaneCollection([(0, 1, 0, 0), (0, 0, 1, 0)]) assert all(is_perpendicular(e1, e2))
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)] )
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_from_tangent(self): a = Point(-1.5, 0.5) b = Point(0, -1) c = Point(1.5, 0.5) d = Point(1.5, -0.5) l = Line(0, 1, -1) conic = Conic.from_tangent(l, a, b, c, d) assert conic.contains(a) assert conic.contains(b) assert conic.contains(c) assert conic.contains(d) assert conic.is_tangent(l)
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)]
def test_join(self): p1 = Point(1, 1, 0) p2 = Point(2, 1, 0) p3 = Point(3, 4, 0) p4 = Point(0, 2, 0) # 3 points assert join(p1, p2, p3).contains(p4) # 2 points l = p1.join(p2) assert l.contains(Point(3, 1, 0)) # two lines m = Line(Point(0, 0, 0), Point(1, 2, 0)) assert join(l, m) == Plane(0, 0, 1, 0) # point and line p = join(l, p3) assert p.contains(p4)
def test_reflection(self): p = Point(-1, 1) r = reflection(Line(1, -1, 1)) assert r * p == Point(0, 0)
def test_intersect(self): c = Cone(vertex=Point(1, 1, 1), base_center=Point(2, 2, 2), radius=2) a = np.sqrt(2) l = Line(Point(0, 4, 2), Point(4, 0, 2)) assert c.intersect(l) == [Point(2 - a, 2 + a, 2), Point(2 + a, 2 - a, 2)]
def test_intersect(self): c = Cylinder() l = Line(Point(-2, -2, 2), Point(2, 2, 2)) s = np.sqrt(2) / 2 assert c.intersect(l) == [Point(s, s, 2), Point(-s, -s, 2)]
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_meet(self): l = Line(-1, -1, 2) m = Line(1, -1, 0) assert l.meet(m) == Point(1, 1)
def test_project(self): p1 = Point(1, 0, 0, 0) p2 = Point(0, 1, 0, 0) l = Line(p1, p2) assert l.project(Point(0, 0, 0, 0)) == Point(0.5, 0.5, 0, 0)
def test_join(self): p = Point(1, 0) q = Point(0, 1) assert p.join(q) == Line(-1, -1, 1)
def test_is_coplanar(self): l = Line(Point(1, 1, 0), Point(2, 1, 0)) m = Line(Point(0, 0, 0), Point(1, 2, 0)) assert l.is_coplanar(m)
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_tangent(self): curve = AlgebraicCurve(x**2 + y**2 - 1, symbols=[x, y, z]) assert curve.tangent(at=Point(1, 0)) == Line(1, 0, 0) + Point(1, 0) assert curve.is_tangent(Line(1, 0, 0) + Point(1, 0))
def test_circle(self): c = Circle(Point(0, 1), 1) assert c.contains(Point(0, 2)) assert c.contains(Point(1, 1)) assert c.tangent(at=Point(0, 0)) == Line(0, 1, 0) assert c.center == Point(0, 1)