def test_add_path(self): path1 = agg.CompiledPath() path1.move_to(1.0, 1.0) path1.translate_ctm(1.0, 1.0) path1.line_to(2.0, 2.0) # actually (3.0,3.0) path1.scale_ctm(2.0, 2.0) path1.line_to(2.0, 2.0) # actually (5.0,5.0) gc = agg.GraphicsContextArray((100, 100)) gc.move_to(1.0, 1.0) gc.translate_ctm(1.0, 1.0) gc.line_to(2.0, 2.0) # actually (3.0,3.0) sub_path = agg.CompiledPath() sub_path.scale_ctm(2.0, 2.0) sub_path.line_to(2.0, 2.0) gc.add_path(sub_path) path2 = gc._get_path() desired = path1._vertices() actual = path2._vertices() self.assertTrue(allclose(actual, desired)) desired = path1.get_ctm() actual = path2.get_ctm() self.assertEqual(actual, desired)
def test_add_path(self): path1 = agg.CompiledPath() path1.move_to(1.0, 1.0) path1.translate_ctm(1.0, 1.0) path1.line_to(2.0, 2.0) #actually (3.0,3.0) path1.scale_ctm(2.0, 2.0) path1.line_to(2.0, 2.0) # actually (5.0,5.0) path2 = agg.CompiledPath() path2.move_to(1.0, 1.0) path2.translate_ctm(1.0, 1.0) path2.line_to(2.0, 2.0) #actually (3.0,3.0) sub_path = agg.CompiledPath() sub_path.scale_ctm(2.0, 2.0) sub_path.line_to(2.0, 2.0) path2.add_path(sub_path) desired = path1._vertices() actual = path2._vertices() self.assertRavelEqual(actual, desired) desired = path1.get_ctm() actual = path2.get_ctm() self.assertRavelEqual(actual, desired)
def benchmark_symbols_all_at_once(n_pts=1000, sz=(1000, 1000)): """ Renders all the symbols. """ width, height = sz pts = stats.norm.rvs(size=(n_pts, 2)) * array(sz) / 8. + array(sz) / 2. star_path = agg.CompiledPath() star_path.lines(circle_array()) gc = agg.GraphicsContextArray(sz) gc.set_fill_color((1.0, 0.0, 0.0, 0.1)) gc.set_stroke_color((0.0, 1.0, 0.0, 0.6)) path = agg.CompiledPath() t1 = time.clock() for x, y in pts: path.save_ctm() path.translate_ctm(x, y) path.add_path(star_path) path.restore_ctm() gc.add_path(path) t2 = time.clock() gc.draw_path() t3 = time.clock() gc.save("benchmark_symbols2.bmp") build_path_time = t2 - t1 render_path_time = t3 - t2 tot_time = t3 - t1 print('star count, tot,building path, rendering path:', n_pts, \ tot_time, build_path_time,render_path_time) return
def benchmark_compiled_path(cycles=10, n_pts=1000, sz=(1000, 1000)): """ Render a sin wave to a compiled_path then display it repeatedly. """ width, height = sz pts = zeros((n_pts, 2), Float) x = pts[:, 0] y = pts[:, 1] interval = width / float(n_pts) x[:] = arange(0, width, interval) y[:] = height / 2. + height / 2. * sin(x * 2 * pi / n_pts) path = agg.CompiledPath() path.lines(pts) #path.move_to(pts[0,0],pts[0,1]) #for x,y in pts[1:]: # path.line_to(x,y) t1 = time.clock() gc = agg.GraphicsContextBitmap(sz) for i in range(cycles): #gc.clear() gc.add_path(path) gc.stroke_path() t2 = time.clock() tot_time = t2 - t1 print('tot,per cycle:', tot_time, tot_time / cycles) return
def test_rotate_ctm(self): angle = pi / 4. path = agg.CompiledPath() path.rotate_ctm(angle) actual = path.get_ctm() desired = agg.rotation_matrix(angle) self.assertRavelEqual(actual, desired)
def test_begin_path(self): path = agg.CompiledPath() path.move_to(1.0, 1.0) path.begin_path() pt, flag = path._vertex() # !! should get this value from the agg enum value desired = 0 self.assertRavelEqual(flag, desired)
def test_vertices(self): # !! should get this value from the agg enum value path = agg.CompiledPath() path.move_to(1.0, 1.0) desired = array(((1.0, 1.0, 1.0, 0.0), (0.0, 0.0, 0.0, 0.0))) actual = path._vertices() self.assertRavelEqual(actual, desired)
def test_move_to1(self): """ Test that transforms are affecting move_to commands """ path = agg.CompiledPath() path.translate_ctm(1.0, 1.0) path.move_to(1.0, 1.0) actual, flag = path._vertex() desired = array((2.0, 2.0)) self.assertRavelEqual(actual, desired)
def star_path_gen(size=40): star_path = agg.CompiledPath() #spts = circle_array() spts = star_array() #star_path.lines(spts) star_path.move_to(spts[0][0], spts[0][1]) for x, y in spts: star_path.line_to(x, y) star_path.close_path() return star_path
def test_concat_ctm(self): path = agg.CompiledPath() path.translate_ctm(2.0, 2.0) m0 = agg.scaling_matrix(2.0, 2.0) path.concat_ctm(m0) actual = path.get_ctm() # wrapper not working # m0 *= agg.translation_matrix(2.0,2.0) m0.multiply(agg.translation_matrix(2.0, 2.0)) desired = m0 self.assertRavelEqual(actual, desired)
def test_quad_curve_to(self): path = agg.CompiledPath() ctrl = 1.0, 1.0 to = 2.0, 2.0 path.quad_curve_to(ctrl[0], ctrl[1], to[0], to[1]) actual_ctrl, flag = path._vertex() self.assertRavelEqual(actual_ctrl, ctrl) assert (flag == 3) actual_to, flag = path._vertex() assert (actual_to == to) assert (flag == 3)
def test_save_restore_ctm1(self): path = agg.CompiledPath() m0 = agg.translation_matrix(10.0, 10.0) path.set_ctm(m0) path.save_ctm() m1 = agg.translation_matrix(5.0, 5.0) path.set_ctm(m1) m2 = path.get_ctm() self.assertRavelEqual(m1, m2) path.restore_ctm() m3 = path.get_ctm() self.assertRavelEqual(m0, m3)
def test_rect(self): path = agg.CompiledPath() path.rect(1.0, 1.0, 1.0, 1.0) actual = path._vertices() desired = array(( (1.0, 1.0, agg.path_cmd_move_to, agg.path_flags_none), (1.0, 2.0, agg.path_cmd_line_to, agg.path_flags_none), (2.0, 2.0, agg.path_cmd_line_to, agg.path_flags_none), (2.0, 1.0, agg.path_cmd_line_to, agg.path_flags_none), (0.0, 0.0, agg.path_cmd_end_poly, agg.path_flags_close), (0.0, 0.0, agg.path_cmd_stop, agg.path_flags_none), )) self.assertRavelEqual(actual, desired)
def test_rewind(self): # !! should get this value from the agg enum value path = agg.CompiledPath() path.move_to(1.0, 1.0) actual, actual_flag = path._vertex() actual, actual_flag = path._vertex() path._rewind() actual, actual_flag = path._vertex() desired = array((1.0, 1.0)) desired_flag = 1 self.assertRavelEqual(actual, desired) self.assertRavelEqual(actual_flag, desired_flag)
def base_helper_lines(self, lines): path = agg.CompiledPath() path.move_to(1.0, 1.0) path.line_to(2.0, 2.0) # actually (3.0,3.0) path.lines(lines) actual = path._vertices() desired = array(( (1.0, 1.0, agg.path_cmd_move_to, agg.path_flags_none), (2.0, 2.0, agg.path_cmd_line_to, agg.path_flags_none), (3.0, 3.0, agg.path_cmd_move_to, agg.path_flags_none), (4.0, 4.0, agg.path_cmd_line_to, agg.path_flags_none), (0.0, 0.0, agg.path_cmd_stop, agg.path_flags_none), )) self.assertRavelEqual(actual, desired)
def test_curve_to(self): path = agg.CompiledPath() ctrl1 = 1.0, 1.0 ctrl2 = 2.0, 2.0 to = 3.0, 3.0 path.curve_to(ctrl1[0], ctrl1[1], ctrl2[0], ctrl2[1], to[0], to[1]) actual_ctrl1, flag = path._vertex() assert (actual_ctrl1 == ctrl1) assert (flag == 4) actual_ctrl2, flag = path._vertex() assert (actual_ctrl2 == ctrl2) assert (flag == 4) actual_to, flag = path._vertex() assert (actual_to == to) assert (flag == 4)
def test_arc_to(self): gc = agg.GraphicsContextArray((640, 480), "rgba32") axes = agg.CompiledPath() axes.move_to(0.5, 50.5) axes.line_to(100.5, 50.5) axes.move_to(50.5, 0.5) axes.line_to(50.5, 100.5) box = agg.CompiledPath() box.move_to(0.5, 0.5) box.line_to(100.5, 0.5) box.line_to(100.5, 100.5) box.line_to(0.5, 100.5) box.close_path() arc = agg.CompiledPath() arc.move_to(10, 10) arc.line_to(20, 10) arc.arc_to(40, 10, 40, 30, 20.0) arc.line_to(40, 40) whole_shebang = agg.CompiledPath() whole_shebang.save_ctm() whole_shebang.add_path(axes) whole_shebang.add_path(box) whole_shebang.translate_ctm(0.0, 50.5) whole_shebang.add_path(arc) whole_shebang.translate_ctm(50.5, 50.5) whole_shebang.rotate_ctm(-agg.pi / 2) whole_shebang.add_path(arc) whole_shebang.rotate_ctm(agg.pi / 2) whole_shebang.translate_ctm(50.5, -50.5) whole_shebang.rotate_ctm(-agg.pi) whole_shebang.add_path(arc) whole_shebang.rotate_ctm(agg.pi) whole_shebang.translate_ctm(-50.5, -50.5) whole_shebang.rotate_ctm(-3 * agg.pi / 2) whole_shebang.add_path(arc) whole_shebang.restore_ctm() gc.set_stroke_color((1.0, 0.0, 0.0)) gc.set_line_width(1.0) ctm1 = gc.get_ctm() gc.translate_ctm(50.5, 300.5) gc.add_path(whole_shebang) gc.stroke_path() gc.translate_ctm(130.5, 50.0) ctm2 = gc.get_ctm() gc.rotate_ctm(-agg.pi / 6) gc.add_path(whole_shebang) gc.set_stroke_color((0.0, 0.0, 1.0)) gc.stroke_path() gc.set_ctm(ctm2) gc.translate_ctm(130.5, 0.0) ctm2 = gc.get_ctm() gc.rotate_ctm(-agg.pi / 3) gc.scale_ctm(1.0, 2.0) gc.add_path(whole_shebang) gc.stroke_path() gc.set_ctm(ctm1) ctm1 = gc.get_ctm() gc.translate_ctm(150.5, 20.5) draw_arcs(gc, 70.5, 96.5) gc.translate_ctm(300.5, 0) draw_arcs(gc, 160.5, 76.5, 50.0) gc.set_ctm(ctm1) gc.translate_ctm(120.5, 100.5) gc.scale_ctm(-1.0, 1.0) draw_arcs(gc, 70.5, 96.5) gc.translate_ctm(-300.5, 100.5) gc.scale_ctm(0.75, -1.0) draw_arcs(gc, 160.5, 76.5, 50.0) with tempfile.TemporaryDirectory() as dirpath: gc.save(os.path.join(dirpath, "arc_to.png"))
def test_scale_ctm(self): path = agg.CompiledPath() path.scale_ctm(2.0, 2.0) actual = path.get_ctm() desired = agg.scaling_matrix(2.0, 2.0) self.assertRavelEqual(actual, desired)
def test_translate_ctm(self): path = agg.CompiledPath() path.translate_ctm(2.0, 2.0) actual = path.get_ctm() desired = agg.translation_matrix(2.0, 2.0) self.assertRavelEqual(actual, desired)
def test_save_restore_ctm0(self): path = agg.CompiledPath() path.save_ctm() path.restore_ctm()
def test_init(self): path = agg.CompiledPath()
def test_move_to(self): path = agg.CompiledPath() path.move_to(1.0, 1.0) actual, flag = path._vertex() desired = array((1.0, 1.0)) self.assertRavelEqual(actual, desired)