Ejemplo n.º 1
0
    def test_translation(self):
        p = Point(0, 1)
        t = translation(0, -1)
        assert t * p == Point(0, 0)

        l = Line(Point(0, 0, 1), Point(1, 0, 1))
        t = translation(0, 0, -1)
        assert t * l == Line(Point(0, 0, 0), Point(1, 0, 0))
Ejemplo n.º 2
0
    def test_parallel(self):
        p = Point(0, 1)
        q = Point(1, 1)
        r = Point(0, 0)
        l = Line(p, q)
        m = l.parallel(through=r)

        assert m == Line(0, 1, 0)
        assert l.is_parallel(m)
Ejemplo n.º 3
0
    def test_intersect(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)
        l = Line(Point(2, 0.5, 0.5), Point(-1, 0.5, 0.5))
        assert cube.intersect(l) == [Point(0, 0.5, 0.5), Point(1, 0.5, 0.5)]

        l = Line(a, Point(-1, 0, 0))
        assert cube.intersect(l) == [Point(0, 0, 0), Point(1, 0, 0)]
Ejemplo n.º 4
0
    def test_intersection(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)]

        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)]
Ejemplo n.º 5
0
    def test_intersect(self):
        c = Circle(Point(0, 0), 1)
        i = c.intersect(Line(0, 1, 0))
        assert len(i) == 2
        assert Point(1, 0) in i
        assert Point(-1, 0) in i

        c2 = Circle(Point(0, 2), 1)
        assert Point(0, 1) in c.intersect(c2)

        c3 = Conic.from_lines(Line(1, 0, 0), Line(0, 1, 0))
        assert Point(1, 0) in c.intersect(c3)
        assert Point(0, 1) in c.intersect(c3)
        assert Point(-1, 0) in c.intersect(c3)
        assert Point(0, -1) in c.intersect(c3)
Ejemplo n.º 6
0
    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)])]
Ejemplo n.º 7
0
    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)])
        ]
Ejemplo n.º 8
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)
Ejemplo n.º 9
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])]
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
0
    def __detect_and_draw_faces_and_eyes_better(self, frame, gray_frame):
        faces = self.__detect_faces(gray_frame)
        # self.__draw_faces(faces, frame)

        if len(faces) == 0:
            # eyes = self.__detect_eyes(gray_frame)
            # self.__draw_eyes(eyes, frame)
            pass
        else:
            for face in faces:
                (x, y, w, h) = face
                roi_gray = gray_frame[y:y + h, x:x + w]
                roi_color = frame[y:y + h, x:x + w]
                eyes = self.__detect_eyes(roi_gray)

                if len(eyes) == 2:
                    self.__draw_faces([face], frame)
                    for (ex, ey, ew, eh) in eyes:
                        cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)

                    eyes_centers = [(ex + ew // 2, ey + eh // 2) for (ex, ey, ew, eh) in eyes]

                    cv2.line(roi_color, (eyes_centers[0][0], eyes_centers[0][1]),
                             (eyes_centers[1][0], eyes_centers[1][1]), (66, 215, 244), 1)

                    eyes_centers_as_points = [Point(x, y) for (x, y) in eyes_centers]

                    middle_point_eyes = ((eyes_centers[0][0] + eyes_centers[1][0]) // 2,
                                         (eyes_centers[0][1] + eyes_centers[1][1]) // 2)

                    middle_point_eyes_as_point = Point(middle_point_eyes[0], middle_point_eyes[1])

                    line_centering_eyes = Line(eyes_centers_as_points[0], eyes_centers_as_points[1])

                    vertical_axis = line_centering_eyes.perpendicular(through=middle_point_eyes_as_point)
Ejemplo n.º 13
0
    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)]
Ejemplo n.º 14
0
    def test_project(self):
        p1 = Point(1, 1, 0)
        p2 = Point(2, 1, 0)
        l = Line(p1, p2)
        assert l.project(Point(0, 0, 0)) == Point(0, 1, 0)

        e = Plane(0, 0, 1, 0)
        assert e.project(Point(1, 1, 5)) == p1
Ejemplo n.º 15
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)
        ]
Ejemplo n.º 16
0
    def test_intersections(self):
        c = Circle(Point(0, 0), 1)
        i = c.intersect(Line(0, 1, 0))
        assert len(i) == 2
        assert Point(1, 0) in i
        assert Point(-1, 0) in i

        c2 = Circle(Point(0, 2), 1)
        assert Point(0, 1) in c.intersect(c2)
Ejemplo n.º 17
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
Ejemplo n.º 18
0
    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)
Ejemplo n.º 19
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)
Ejemplo n.º 20
0
    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)
Ejemplo n.º 21
0
    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
Ejemplo n.º 22
0
    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)
Ejemplo n.º 23
0
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])
Ejemplo n.º 24
0
    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
Ejemplo n.º 25
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)]
        )
Ejemplo n.º 26
0
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))
Ejemplo n.º 27
0
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))
Ejemplo n.º 28
0
    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)
Ejemplo n.º 29
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)]
Ejemplo n.º 30
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)