Esempio n. 1
0
    def test_ray(self):
        r = Ray(Point(1, 1), pi / 4)
        self.assertAlmostEqual(r.origin(), Point(1, 1))
        self.assertAlmostEqual(r.versor(), Point(1, 1) / sqrt(2))
        self.assertAlmostEqual(r.angle(), pi / 4)

        r.set_origin(Point(4, 3))
        #TODO this should maybe normalize the versor!
        r.set_versor(Point(1, -1) / sqrt(2))
        self.assertAlmostEqual(r.origin(), Point(4, 3))
        self.assertAlmostEqual(r.versor(), Point(1, -1) / sqrt(2))
        self.assertAlmostEqual(r.angle(), -pi / 4)

        r.set_points(Point(1, 1), Point(1, 3))
        self.assertFalse(r.is_degenerate())
        self.assertFalse(Ray().is_degenerate())
        self.assertAlmostEqual(r.point_at(4), Point(1, 5))

        #TODO I think this should be expected behaviour
        #        self.assertAlmostEqual(
        #            r.pointAt(-3),
        #            Point(1, 1)))
        self.assertAlmostEqual(r.value_at(4, 0), 1)
        self.assertAlmostEqual(r.value_at(4, 1), 5)

        roots = r.roots(3, 1)
        for root in roots:
            self.assertAlmostEqual(r.value_at(root, 1), 3)

        self.assertAlmostEqual(
            r.point_at(3) - r.origin(),
            r.origin() - r.reverse().point_at(3))

        self.assertAlmostEqual(Ray.distance(Point(), r), sqrt(2))
        self.assertAlmostEqual(Ray.distance(Point() + r.versor(), r), 1)

        self.assertTrue(Ray.are_near(Point(), r, 2))
        self.assertFalse(Ray.are_near(Point(), r))
        self.assertTrue(Ray.are_same(r, r))

        q = Ray(r.origin(), r.angle())
        self.assertTrue(Ray.are_same(r, q))

        q.set_origin(r.origin() + Point(0, 1))
        self.assertFalse(Ray.are_same(r, q))
        #TODO shouldn't this really be 0?
        self.assertAlmostEqual(Ray.angle_between(r, q), 2 * pi)

        q.set_versor(Point(1, 0))
        q.set_origin(r.origin())
        self.assertAlmostEqual(
            Point(1, 1) / sqrt(2),
            Ray.make_angle_bisector_ray(q, r).versor())

        q.set_angle(pi / 7)
        self.assertAlmostEqual(q.angle(), pi / 7)

        self.assertIsInstance(q.portion(2, 4), Curve)
        self.assertAlmostEqual(q.portion(2, 4)(0), q.point_at(2))

        self.assertIsInstance(q.segment(1, 5), LineSegment)

        self.assertAlmostEqual(q.segment(1, 5)(1), q.point_at(5))
Esempio n. 2
0
    def test_ray(self):
        r = Ray(Point(1,1), pi/4)
        self.assertAlmostEqual(r.origin(), Point(1, 1))
        self.assertAlmostEqual(r.versor(), Point(1, 1)/sqrt(2))
        self.assertAlmostEqual(r.angle(), pi/4)

        r.set_origin(Point(4, 3))
        #TODO this should maybe normalize the versor!
        r.set_versor(Point(1, -1)/sqrt(2))
        self.assertAlmostEqual(r.origin(), Point(4, 3))
        self.assertAlmostEqual(r.versor(), Point(1, -1)/sqrt(2))
        self.assertAlmostEqual(r.angle(), -pi/4)

        r.set_points(Point(1, 1), Point(1, 3))
        self.assertFalse(r.is_degenerate())
        self.assertFalse(Ray().is_degenerate())
        self.assertAlmostEqual(r.point_at(4), Point(1, 5))

        #TODO I think this should be expected behaviour
#        self.assertAlmostEqual(
#            r.pointAt(-3),
#            Point(1, 1)))
        self.assertAlmostEqual(r.value_at(4, 0), 1)
        self.assertAlmostEqual(r.value_at(4, 1), 5)

        roots = r.roots( 3, 1 )
        for root in roots:
            self.assertAlmostEqual( r.value_at(root, 1), 3)

        self.assertAlmostEqual(
            r.point_at(3) - r.origin(),
            r.origin()-r.reverse().point_at(3))

        self.assertAlmostEqual(Ray.distance(Point(), r), sqrt(2))
        self.assertAlmostEqual(Ray.distance(Point()+r.versor(), r), 1)

        self.assertTrue(Ray.are_near(Point(), r, 2))
        self.assertFalse(Ray.are_near(Point(), r))
        self.assertTrue(Ray.are_same(r, r))

        q = Ray(r.origin(), r.angle())
        self.assertTrue(Ray.are_same(r, q))

        q.set_origin(r.origin()+Point(0, 1))
        self.assertFalse(Ray.are_same(r, q))
        #TODO shouldn't this really be 0?
        self.assertAlmostEqual(Ray.angle_between(r, q), 2*pi)

        q.set_versor(Point(1, 0))
        q.set_origin(r.origin())
        self.assertAlmostEqual(
            Point(1, 1)/sqrt(2),
            Ray.make_angle_bisector_ray(q, r).versor())

        q.set_angle(pi/7)
        self.assertAlmostEqual(q.angle(), pi/7)

        self.assertIsInstance(q.portion(2, 4), Curve)
        self.assertAlmostEqual(q.portion(2, 4)(0), q.point_at(2))

        self.assertIsInstance(q.segment(1, 5), LineSegment)

        self.assertAlmostEqual(q.segment(1, 5)(1), q.point_at(5))
Esempio n. 3
0
    def affine(self, A, B):
        c0, c1, c2, c3, c4, c5 = A[0], A[1], A[2], A[3], A[4], A[5]
        C = Affine(c0, c1, c2, c3, c4, c5)
        self.assertEqual(C, A)
        E = Affine.identity()
        self.assertEqual(C, C*E)
        self.assertEqual(E*B, B)
        self.assertEqual(E.det(), 1)

        self.assertAlmostEqual(A.det(), c0*c3-c1*c2)
        self.assertAlmostEqual(abs(A.det()), A.descrim2())
        self.assertAlmostEqual(abs(A.det())**0.5, A.descrim())
        #xor
        self.assertFalse( A.flips() ^ (A.det() < 0) )

        if A.is_singular():
            self.assertAlmostEqual(A.det(), 0)
        else:
            self.assertTrue( Affine.are_near (A*A.inverse(), E) )
            self.assertAlmostEqual(A.det(), 1/A.inverse().det())
        self.assertEqual( A.x_axis(), Point(c0, c1) )
        self.assertEqual( A.y_axis(), Point(c2, c3) )
        self.assertEqual( A.translation(), Point(c4, c5) )

        self.assertAlmostEqual(A.expansion_X(), A.x_axis().length())
        self.assertAlmostEqual(A.expansion_Y(), A.y_axis().length())
        
        if abs(A.expansion_X()) > 1e-7 and abs(A.expansion_Y()) > 1e-7:
            A.set_expansion_X(2)
            A.set_expansion_Y(3)
            self.assertAlmostEqual(A.expansion_X(), 2)
            self.assertAlmostEqual(A.expansion_Y(), 3)

        A.set_identity()

        self.assertTrue(A.is_identity())
        self.assertTrue(A.is_translation())
        self.assertFalse(A.is_nonzero_translation())
        self.assertTrue(A.is_scale())
        self.assertTrue(A.is_uniform_scale())
        self.assertFalse(A.is_nonzero_scale())
        self.assertFalse(A.is_nonzero_uniform_scale())
        self.assertTrue(A.is_rotation())
        self.assertFalse(A.is_nonzero_rotation())
        self.assertTrue(A.is_HShear())
        self.assertTrue(A.is_VShear())
        self.assertFalse(A.is_nonzero_HShear())
        self.assertFalse(A.is_nonzero_VShear())
        self.assertTrue(A.is_zoom())

        self.assertTrue(A.preserves_area() and A.preserves_angles() and A.preserves_distances())

        self.assertFalse( A.flips() )
        self.assertFalse( A.is_singular() )

        A.set_X_axis(Point(c0, c1))
        A.set_Y_axis(Point(c2, c3))

        self.assertEqual(A.without_translation(), A)

        A.set_translation(Point(c4, c5))
        self.assertEqual(C, A)

        self.assertAlmostEqual( (A*B).det(), A.det()*B.det() )

        self.assertEqual( A.translation(), Point()*A )
        self.assertEqual( Point(1, 1)*A, Point( c0+c2+c4, c1+c3+c5 ))

        l = Line(Point(1, 1), 2)
        self.assertEqual( (l.transformed(A)).origin(), l.origin()*A )
        self.assertTrue( Line.are_near( l.point_at(3)*A, l.transformed(A) ) )

        r = Ray(Point(2, 3), 4)
        self.assertEqual( (r.transformed(A)).origin(), r.origin()*A )
        self.assertTrue( Ray.are_near( r.point_at(3)*A, r.transformed(A) ) )
Esempio n. 4
0
    def affine(self, A, B):
        c0, c1, c2, c3, c4, c5 = A[0], A[1], A[2], A[3], A[4], A[5]
        C = Affine(c0, c1, c2, c3, c4, c5)
        self.assertEqual(C, A)
        E = Affine.identity()
        self.assertEqual(C, C * E)
        self.assertEqual(E * B, B)
        self.assertEqual(E.det(), 1)

        self.assertAlmostEqual(A.det(), c0 * c3 - c1 * c2)
        self.assertAlmostEqual(abs(A.det()), A.descrim2())
        self.assertAlmostEqual(abs(A.det())**0.5, A.descrim())
        #xor
        self.assertFalse(A.flips() ^ (A.det() < 0))

        if A.is_singular():
            self.assertAlmostEqual(A.det(), 0)
        else:
            self.assertTrue(Affine.are_near(A * A.inverse(), E))
            self.assertAlmostEqual(A.det(), 1 / A.inverse().det())
        self.assertEqual(A.x_axis(), Point(c0, c1))
        self.assertEqual(A.y_axis(), Point(c2, c3))
        self.assertEqual(A.translation(), Point(c4, c5))

        self.assertAlmostEqual(A.expansion_X(), A.x_axis().length())
        self.assertAlmostEqual(A.expansion_Y(), A.y_axis().length())

        if abs(A.expansion_X()) > 1e-7 and abs(A.expansion_Y()) > 1e-7:
            A.set_expansion_X(2)
            A.set_expansion_Y(3)
            self.assertAlmostEqual(A.expansion_X(), 2)
            self.assertAlmostEqual(A.expansion_Y(), 3)

        A.set_identity()

        self.assertTrue(A.is_identity())
        self.assertTrue(A.is_translation())
        self.assertFalse(A.is_nonzero_translation())
        self.assertTrue(A.is_scale())
        self.assertTrue(A.is_uniform_scale())
        self.assertFalse(A.is_nonzero_scale())
        self.assertFalse(A.is_nonzero_uniform_scale())
        self.assertTrue(A.is_rotation())
        self.assertFalse(A.is_nonzero_rotation())
        self.assertTrue(A.is_HShear())
        self.assertTrue(A.is_VShear())
        self.assertFalse(A.is_nonzero_HShear())
        self.assertFalse(A.is_nonzero_VShear())
        self.assertTrue(A.is_zoom())

        self.assertTrue(A.preserves_area() and A.preserves_angles()
                        and A.preserves_distances())

        self.assertFalse(A.flips())
        self.assertFalse(A.is_singular())

        A.set_X_axis(Point(c0, c1))
        A.set_Y_axis(Point(c2, c3))

        self.assertEqual(A.without_translation(), A)

        A.set_translation(Point(c4, c5))
        self.assertEqual(C, A)

        self.assertAlmostEqual((A * B).det(), A.det() * B.det())

        self.assertEqual(A.translation(), Point() * A)
        self.assertEqual(Point(1, 1) * A, Point(c0 + c2 + c4, c1 + c3 + c5))

        l = Line(Point(1, 1), 2)
        self.assertEqual((l.transformed(A)).origin(), l.origin() * A)
        self.assertTrue(Line.are_near(l.point_at(3) * A, l.transformed(A)))

        r = Ray(Point(2, 3), 4)
        self.assertEqual((r.transformed(A)).origin(), r.origin() * A)
        self.assertTrue(Ray.are_near(r.point_at(3) * A, r.transformed(A)))