コード例 #1
0
ファイル: test-affine.py プロジェクト: snoyer/lib2geom
    def test_eigen(self):
        #TODO looks like bug in eigen - (1, 0) should be eigenvector too
        #~ S = Scale(1, 2)
        #~ E_S = Eigen(S)
        #~ print E_S.vectors, E_S.values
        #~ print Affine(S)
        #~ for i in E_S.vectors:
        #~ print i, i*S, Point(1, 0) * S

        B = Affine(-2, 2, 2, 1, 0, 0)
        G1 = Eigen(B)
        G2 = Eigen([[-2, 2], [2, 1]])

        self.assertAlmostEqual(min(G1.values), min(G2.values))
        self.assertAlmostEqual(max(G1.values), max(G2.values))

        if Point.are_near(G1.vectors[0] * G1.values[0], G1.vectors[0] * B):
            self.assertTrue(
                Point.are_near(G1.vectors[1] * G1.values[1],
                               G1.vectors[1] * B))
        else:
            self.assertTrue(
                Point.are_near(G1.vectors[1] * G1.values[0],
                               G1.vectors[1] * B))
            self.assertTrue(
                Point.are_near(G1.vectors[0] * G1.values[1],
                               G1.vectors[0] * B))
コード例 #2
0
ファイル: test-affine.py プロジェクト: snoyer/lib2geom
    def test_rotate(self):
        R = Rotate()
        S = Rotate(pi / 3)
        T = Rotate(Point(1, 1))
        U = Rotate(-1, 1)

        self.assertTrue(S.vector(), Point(cos(pi / 3), sin(pi / 3)))
        self.assertEqual(Point(T[0], T[1]), T.vector())
        self.assertTrue(Affine.are_near(Rotate.from_degrees(60), S))
        self.assertEqual(R, Rotate.identity())
        self.assertTrue(
            Point.are_near((S * T).vector(),
                           Point(cos(pi / 3 + pi / 4), sin(pi / 3 + pi / 4))))

        self.affine(Affine(R), Affine(S))
        self.affine(Affine(S), Affine(T))
        self.affine(Affine(T), Affine(U))
        self.affine(Affine(U), Affine(R))
コード例 #3
0
ファイル: test-affine.py プロジェクト: snoyer/lib2geom
    def test_affine(self):
        al = []
        for i in range(10):
            al.append(
                Affine(uniform(-10, 10), uniform(-10, 10), uniform(-10, 10),
                       uniform(-10, 10), uniform(-10, 10), uniform(-10, 10)))
        for A in al:
            for B in al:
                self.affine(A, B)

        o = Point(2, 4)
        v = Point(-1, 1) / sqrt(2)
        l = Line.from_origin_and_versor(o, v)

        R = Affine.reflection(v, o)
        for i in range(100):
            p = Point(randint(0, 100), randint(0, 100))
            self.assertAlmostEqual(Line.distance(p, l),
                                   Line.distance(p * R, l))
        self.assertTrue(Affine.are_near(R, R.inverse()))

        self.affine(R, R.inverse())
コード例 #4
0
ファイル: test-affine.py プロジェクト: abrock/lib2geom
    def test_affine(self):
        al = []
        for i in range(10):
            al.append(Affine(   uniform(-10, 10),
                                uniform(-10, 10),
                                uniform(-10, 10),
                                uniform(-10, 10),
                                uniform(-10, 10),
                                uniform(-10, 10)))
        for A in al:
            for B in al:
                self.affine(A, B)

        o = Point(2, 4)
        v = Point(-1, 1)/sqrt(2)
        l = Line.from_origin_and_versor(o, v)
        
        R = Affine.reflection(v, o)
        for i in range(100):
            p = Point(randint(0, 100), randint(0, 100))
            self.assertAlmostEqual(Line.distance(p, l), Line.distance(p*R, l))
        self.assertTrue( Affine.are_near( R, R.inverse() ) )
        
        self.affine(R, R.inverse())
コード例 #5
0
ファイル: test-affine.py プロジェクト: abrock/lib2geom
    def test_rotate(self):
        R = Rotate()
        S = Rotate(pi/3)
        T = Rotate(Point( 1, 1 ))
        U = Rotate( -1, 1 )

        self.assertTrue(S.vector(), Point(cos(pi/3), sin(pi/3)) )
        self.assertEqual( Point(T[0], T[1]), T.vector() )
        self.assertTrue( Affine.are_near( Rotate.from_degrees(60), S ) )
        self.assertEqual(R, Rotate.identity())
        self.assertTrue( Point.are_near(  ( S * T ).vector(),
        Point( cos( pi/3 + pi/4 ), sin( pi/3 + pi/4 ) ) ) )

        self.affine( Affine(R), Affine(S))
        self.affine( Affine(S), Affine(T))
        self.affine( Affine(T), Affine(U))
        self.affine( Affine(U), Affine(R))
コード例 #6
0
ファイル: test-affine.py プロジェクト: snoyer/lib2geom
    def test_translate(self):
        T = Translate()
        U = Translate(Point(2, 4))
        V = Translate(1, -9)

        self.assertTrue(Affine(T).is_translation())
        self.assertTrue(Affine(U).is_nonzero_translation())

        self.assertEqual((U * V).vector(), U.vector() + V.vector())
        self.assertEqual(U.inverse().vector(), -U.vector())
        self.assertEqual(T, Translate.identity())
        self.assertEqual(U.vector(), Point(U[0], U[1]))

        self.affine(Affine(V), Affine(U))
        self.affine(Affine(U), Affine(V))

        r = Rect.from_points(Point(0, 2), Point(4, 8))

        self.assertEqual((r * (U * V)).min(),
                         r.min() + U.vector() + V.vector())
コード例 #7
0
ファイル: test-path.py プロジェクト: snoyer/lib2geom
    def path(self, P):
        for curve in P:
            self.assertIsInstance(curve, Curve)

        self.assertAlmostEqual(P(0), P.front()(0))
        self.curves_equal(P.front(), P[0])

        self.curves_equal(P.back_default(), P[P.size_default() - 1])
        self.curves_equal(P.back_open(), P.back())
        self.assertEqual(P.size_open(), P.size())

        self.assertFalse(P.empty() ^ (P.size() == 0))

        exact = P.bounds_exact().Rect
        exact.expand_by(1e-5)

        fast = P.bounds_fast().Rect
        fast.expand_by(1e-5)
        A1 = Affine(3, 1, 8, 3, 9, 9)
        A2 = Rotate(0.231)

        for i in range(100 * P.size_open() + 1):
            t = i / 100.0
            self.assertTrue(exact.contains(P(t)))
            self.assertTrue(fast.contains(P(t)))
            self.assertAlmostEqual((P * A1)(t), P(t) * A1)
            self.assertAlmostEqual((P * A2)(t), P(t) * A2)

            self.assertAlmostEqual(P(t), P.point_at(t))
            self.assertAlmostEqual(P(t).x, P.value_at(t, 0))
            self.assertAlmostEqual(P(t).y, P.value_at(t, 1))

        if P.closed():
            self.curves_equal(P.back_default(), P.back_closed())
            self.assertEqual(P.size_default(), P.size_closed())
        else:
            self.curves_equal(P.back_default(), P.back_open())
            self.assertEqual(P.size_default(), P.size_open())

        for i in range(10):
            for root in P.roots(i, 0):
                if root < P.size_default():
                    self.assertAlmostEqual(P.value_at(root, 0), i)
            for root in P.roots(i, 1):
                if root < P.size_default():
                    self.assertAlmostEqual(P.value_at(root, 1), i)

        for t in P.all_nearest_times(P(0)):
            self.assertAlmostEqual(P(t), P(0))
        self.assertAlmostEqual(min(P.all_nearest_times(P(0))), 0)
        self.assertAlmostEqual(P.nearest_time(P(0), 0, 0.2), 0)
        self.assertEqual(len(P.nearest_time_per_curve(Point())),
                         P.size_default())

        t, distSq = P.nearest_time_and_dist_sq(Point(-1, -1), 0, P.size())
        self.assertAlmostEqual(distSq**0.5, abs(P(t) - Point(-1, -1)))

        self.assertAlmostEqual(P.portion(0.3, 0.4)(0), P(0.3))
        self.assertAlmostEqual(
            P.portion(interval=Interval(P.size(),
                                        P.size() * 2) / 3)(0),
            P(P.size() / 3.0))

        self.assertAlmostEqual(P(0.23), P.reverse()(P.size() - 0.23))

        self.assertAlmostEqual(P.initial_point(), P(0))
        self.assertAlmostEqual(P.final_point(), P(P.size()))
コード例 #8
0
ファイル: test-affine.py プロジェクト: abrock/lib2geom
    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) ) )
コード例 #9
0
    def curve(self, C): 
        self.assertAlmostEqual(C.initial_point(), C(0))
        self.assertAlmostEqual(C.final_point(), C.point_at(1))
        #Doesn't have to be true
        #~ if C.length() > 0.01:
            #~ self.assertFalse(C.is_degenerate())

        if C.is_degenerate():
            #trivial special case
            return
        
        for i in range(11):
            t = i/10.0
            self.assertAlmostEqual(C(t).x, C.point_at(t).x)
            self.assertAlmostEqual(C(t).y, C.value_at(t, 1))
            self.assertEqual( C(t), C.point_and_derivatives(t, 1)[0] )
            self.assertTrue( C.bounds_exact().contains(C(t)) )
            self.assertTrue( C.bounds_fast().contains(C(t)) )
            #TODO why this works only with degree = 0?
            if      C.bounds_local(OptInterval(t-0.05, t+0.05), 0
                ) and  (
                    C.bounds_local(OptInterval(t-0.05, t+0.05), 0).Rect.area() > 1e-10):
                #ruling out too small rectangles, they have problems with precision
                self.assertTrue( C.bounds_local( OptInterval(t-0.05, t+0.05), 0 ).Rect.contains(C(t)))
        D = C.duplicate()
        
        D.set_initial(Point())
        self.assertAlmostEqual(D.initial_point(), Point())
        
        D.set_final(Point(1, 1))
        self.assertAlmostEqual(D.final_point(), Point(1, 1))
        
        A = Affine( uniform(-10, 10),
                    uniform(-10, 10),
                    uniform(-10, 10),
                    uniform(-10, 10),
                    uniform(-10, 10),
                    uniform(-10, 10))
        E = C.transformed(A)
        for i in range(11):
            t = i/10.0
      #      self.assertAlmostEqual( E(t), C(t)*A )
        G1 = C.portion(0.2, 0.8)
        G2 = C.portion( interval=Interval(2, 8)/10 )
        self.assertAlmostEqual( G1(0), C(0.2) )
        self.assertAlmostEqual( G2(0.5), C( lerp(0.5, 0.2, 0.8) ))
        self.assertAlmostEqual( G1(1), G2(1) )
        
        for i in range(11):
            t = i/10.0
            self.assertAlmostEqual( C.reverse()(t), C(1-t) )
        self.assertAlmostEqual( C.point_and_derivatives(0.3, 1)[1], C.derivative()(0.3) )
        
        self.assertAlmostEqual( C.nearest_time(C(0)), 0 )
        self.assertAlmostEqual( C( C.nearest_time(C(0.5), interval=Interval(0.2, 0.5)) ), C(0.5) )
        self.assertAlmostEqual( C( C.nearest_time(C(0.5), 0.2, 0.5) ), C(0.5) )
        for p in C.all_nearest_times( C(0), 0, 1):
            self.assertEqual(C(p), C(0))
        for p in C.all_nearest_times( C(1), interval=Interval(0, 1)):
            self.assertEqual(C(p), C(1))
        for r in C.roots(0, 0):
            self.assertAlmostEqual(C.value_at(r, 0), 0)
        
        self.assertGreaterEqual(C.length(), abs(C(1) - C(0)))
        self.assertEqual(C.winding(Point()), int(C.winding(Point())) )
        self.assertAlmostEqual( C.unit_tangent_at(0.5), 
                                Point.unit_vector(C.derivative()(0.5)) )
        self.assertTrue(isinstance(C.to_SBasis()[0], SBasis))
コード例 #10
0
ファイル: test-affine.py プロジェクト: snoyer/lib2geom
    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)))
コード例 #11
0
ファイル: test-affine.py プロジェクト: snoyer/lib2geom
    def test_scale(self):
        S = Scale()
        T = Scale(Point(3, 8))
        U = Scale(-3, 1)
        V = Scale(sqrt(2))

        self.assertTrue(Affine(T).is_scale())
        self.assertTrue(Affine(T).is_nonzero_scale())
        self.assertTrue(Affine(V).is_nonzero_uniform_scale())

        self.assertEqual((T * V).vector(), T.vector() * sqrt(2))
        self.assertEqual((T * U)[0], T[0] * U[0])
        self.assertAlmostEqual(1 / U.inverse()[1], U[1])

        r = Rect.from_points(Point(0, 2), Point(4, 8))
        self.assertAlmostEqual((r * V).area(), 2 * r.area())
        self.assertFalse(Affine(U).preserves_area())
        self.assertTrue(Affine(V).preserves_angles())

        self.affine(Affine(T), Affine(U))
        self.affine(Affine(U), Affine(V))
        self.affine(Affine(V), Affine(T))