Esempio n. 1
0
 def test_linear(self):
     L = Linear(0, 1)
     M = Linear(2)
     N = Linear()
     self.assertEqual( (L+M), L+2 )
     self.assertEqual( (L-M), L-2 )
     self.assertAlmostEqual(L(0.5), lerp(.5, 0, 1))
     #~ self.assertTrue(N.is_zero())
     self.assertTrue(M.is_constant())
     self.assertTrue(L.is_finite())
     self.assertAlmostEqual(L(0), L.at0())
     self.assertAlmostEqual(L(1), L.at1())
     self.assertAlmostEqual(L.value_at(0.3), L(0.3))
     self.assertTrue( isinstance(M.to_SBasis(), SBasis ))
     
     self.assertAlmostEqual(L.tri(), L(1) - L(0))
     self.assertAlmostEqual(L.hat(), (L(1) + L(0))/2)
     
     for i in range(11):
         t = i/10.0
         self.assertTrue(L.bounds_exact().Interval.contains(L(t)))
         self.assertTrue(L.bounds_fast().Interval.contains(L(t)))
         self.assertTrue(L.bounds_local(t-0.05, t+0.05).Interval.contains(L(t)))
         self.assertAlmostEqual(lerp(t, 0, 4), t*4)
         self.assertAlmostEqual(L(t), cy2geom.reverse(L)(1-t))
         self.assertAlmostEqual( L(t)*t, (L*t)(t) )
         self.assertAlmostEqual( L(t)+t, (L+t)(t) )
         self.assertAlmostEqual( L(t)-t, (L-t)(t) )
         self.assertAlmostEqual( -( L(t) ), (-L)(t) )
         self.assertAlmostEqual( (L/2)(t), L(t)/2 )
Esempio n. 2
0
 def test_bezier(self):
     B = Bezier()
     C = Bezier(2)
     D = Bezier(2, 4)
     E = Bezier(1, 3, 9)
     F = Bezier(-2, 5, -1, 2)
     self.assertTrue( B.is_zero() )
     self.assertTrue( C.is_constant() )
     self.assertTrue( D.is_finite() )
     C.clear()
     self.assertEqual(D.degree(), 1)
     self.assertEqual(E.at0(), 1)
     self.assertEqual(E.at1(), 9)
     self.assertEqual(E[2], 9)
     for i in range(11):
         t = i/10.0
         self.assertAlmostEqual( D(t), lerp(t, 2, 4) )
         self.assertAlmostEqual( D(t), D.value_at(t))
         self.assertAlmostEqual( D.value_and_derivatives(t, 0)[0], D(t) )
         self.assertAlmostEqual( D.value_and_derivatives(t, 1)[1], Bezier.derivative(D)(t) )
         self.assertAlmostEqual( Bezier.integral(D).value_and_derivatives(t, 1)[1], D(t) )
         #~ self.assertAlmostEqual( D.elevate_degree().reduce_degree()(t), D(t) )
         self.assertAlmostEqual( (D+2)(t), D(t)+2 )
         self.assertAlmostEqual( (D-1)(t), D(t)-1 )
         self.assertAlmostEqual( (D*2)(t), D(t)*2 )
         self.assertAlmostEqual( (D/4)(t), D(t)/4 )
         self.assertTrue( Bezier.bounds_fast(F).Interval.contains(F(t)) )
         self.assertTrue( Bezier.bounds_exact(F).Interval.contains(F(t)) )
         self.assertTrue( Bezier.bounds_local(F, OptInterval(t-0.05, t+0.05)).Interval.contains(F(t)) )
     for r in F.roots():
         self.assertAlmostEqual(F(r), 0)
     #TODO: bug in 2geom?
     #~ for r in F.roots(Interval(0.1, 0.8)):
         #~ self.assertAlmostEqual(F(r), 0)
         #~ self.assertTrue( 0.1 <= r <= 0.8 )
     self.assertIsInstance(F.forward_difference(1), Bezier)
     self.assertIsInstance(F.elevate_degree(), Bezier)
     self.assertIsInstance(E.reduce_degree(), Bezier)
     #F.reduce_degree() fails with
     # *** glibc detected *** python2: malloc(): memory corruption:
     self.assertIsInstance(F.elevate_to_degree(4), Bezier)
     self.assertIsInstance(F.deflate(), Bezier)
     S = F.to_SBasis()
     self.assertIsInstance(S, SBasis)
     for i in range(11):
         t = i/10.0
         self.assertAlmostEqual(S(t), F(t))
Esempio n. 3
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))