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, 0) q = Point(0, 0, 1) l = Line(p, q) m = l.perpendicular(p) assert is_perpendicular(l, m) r = Point(1, 2, 3) e = Plane(p, q, r) m = e.perpendicular(p) assert is_perpendicular(l, m)
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_perpendicular(self): p = Point(1, 1, 0) q = Point(0, 0, 1) r = Point(1, 2, 3) l = Line(p, q) m = l.perpendicular(p) assert l.meet(m) == p assert is_perpendicular(l, m) m = l.perpendicular(r) assert is_perpendicular(l, m) e = Plane(l, r) m = e.perpendicular(p) assert e.meet(m) == p assert is_perpendicular(l, m) m = e.perpendicular(p + m.direction) assert e.meet(m) == p assert is_perpendicular(l, m) f = e.perpendicular(l) assert e.meet(f) == l assert is_perpendicular(e, f)
def test_join(self): p1 = Point(1, 1, 4, 0) p2 = Point(2, 1, 5, 0) p3 = Point(3, 4, 6, 0) p4 = Point(0, 2, 7, 0) p5 = Point(1, 5, 8, 0) # 4 points assert join(p1, p2, p3, p4).contains(p5) # 3 points assert join(p1, p2, p3).contains(p3) # two lines l = Line(p1, p2) m = Line(p3, p4) assert join(l, m) == Plane(p1, p2, p3, p4) # coplanar lines l = Line(p1, p2) m = Line(p1, p3) assert join(l, m).contains(p3) # point and line p = join(l, p3) assert p == join(p1, p2, p3) # 2 points l = p1.join(p2) assert l.contains(Point(3, 1, 6, 0))
def test_tangent(self): q = Quadric([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, -1]]) assert q.contains(Point(1, 0, 0)) assert q.tangent(at=Point(1, 0, 0)) == Plane(Point(1, 0, 0), Point(1, 0, 1), Point(1, 1, 0))
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_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): 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_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_meet(self): p1 = Plane(1, 0, 0, 0, 0) p2 = Plane(0, 1, 0, 0, 0) p3 = Plane(0, 0, 1, 0, 0) p4 = Plane(0, 0, 0, 1, 0) # four hyperplanes assert meet(p1, p2, p3, p4) == Point(0, 0, 0, 0) # hyperplane and line l = Line(Point(0, 0, 0, 0), Point(0, 0, 1, 0)) assert p3.meet(l) == Point(0, 0, 0, 0) # two lines m = Line(Point(0, 0, 0, 0), Point(1, 2, 5, 6)) assert l.meet(m) == Point(0, 0, 0, 0)
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_basis_matrix(self): a = PlaneCollection([Plane(1, 0, 0, 0), Plane(0, 1, 0, 0), Plane(0, 0, 1, 0)]) assert a.basis_matrix.shape == (3, 3, 4) assert np.allclose(np.matmul(a.basis_matrix, a.array[..., None]), 0)
def test_components(self): e = Plane(1, 2, 3, 4) f = Plane(4, 3, 2, 1) q = Quadric.from_planes(e, f) assert q.components == [e, f]
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])