added more complicated smoothing cases
[PyX/mjg.git] / test / functional / test_deformer.py
blobfaec24d4f1374c673dccf26cf5a849a642e68614
1 #!/usr/bin/env python
2 import sys; sys.path[:0] = ["../.."]
3 from pyx import *
4 from pyx.deformer import *
6 ##### helpers ##############################################################
8 def bboxrect(cmd):
9 return cmd.bbox().enlarged(5*unit.t_mm).rect()
11 def dotest(c, x, y, test):
12 c2 = c.insert(canvas.canvas([trafo.translate(x, y)]))
13 eval("%s(c2)" % test)
14 c.stroke(bboxrect(c2))
16 def drawpathwbbox(c, p):
17 c.stroke(p, [color.rgb.red])
18 np = p.normpath()
19 c.stroke(np, [color.rgb.green, style.linestyle.dashed])
20 c.stroke(bboxrect(p))
22 ##### tests ################################################################
24 def testcycloid(c):
26 # dependence on turnangle
27 p = path.line(0, 0, 3, 0)
28 c.stroke(p, [style.linewidth.THIN])
29 cyc = cycloid(halfloops=3, skipfirst=0.5, skiplast=0.5, curvesperhloop=2)
30 c.stroke(p, [cyc(turnangle=00)])
31 c.stroke(p, [cyc(turnangle=22), color.rgb.red])
32 c.stroke(p, [cyc(turnangle=45), color.rgb.green])
33 c.stroke(p, [cyc(turnangle=67), color.rgb.blue])
34 c.stroke(p, [cyc(turnangle=90), color.cmyk.Cyan])
36 # dependence on curvesperloop
37 p = path.curve(5, 0, 8, 0, 6, 4, 9, 4)
38 c.stroke(p)
39 cyc = cycloid(halfloops=16, skipfirst=0, skiplast=0, curvesperhloop=1)
40 c.stroke(p, [cyc(curvesperhloop=2)])
41 c.stroke(p, [cyc(curvesperhloop=3), color.rgb.red])
42 c.stroke(p, [cyc(curvesperhloop=4), color.rgb.green])
43 c.stroke(p, [cyc(curvesperhloop=10), color.rgb.blue])
45 # extremely curved path
46 p = path.curve(0,2, 0.5,5, 1,6, 2,2)
47 c.stroke(p)
48 cyc = cycloid(radius=0.7, halfloops=7, skipfirst=0, skiplast=0, curvesperhloop=1)
49 c.stroke(p, [cyc(curvesperhloop=2)])
50 c.stroke(p, [cyc(curvesperhloop=3), color.rgb.red])
51 c.stroke(p, [cyc(curvesperhloop=4), color.rgb.green])
52 c.stroke(p, [cyc(curvesperhloop=50), color.rgb.blue])
55 def testsmoothed(c):
56 p = path.path(
57 path.moveto(0,0),
58 path.lineto(3,0),
59 path.lineto(5,7),
60 path.curveto(0,10, -2,8, 0,6),
61 path.lineto(0,4),
62 # horrible overshooting with obeycurv=1
63 #path.lineto(-4,4), path.curveto(-7,5, -4,2, -5,2),
64 path.lineto(-4,3), path.curveto(-7,5, -4,2, -5,2),
65 #path.arct(-6,4, -5,1, 1.5),
66 #path.arc(-5, 3, 0.5, 0, 180),
67 path.lineto(-5,1),
68 path.lineto(-0.2,0.2),
69 path.closepath()
70 ) + path.circle(0,0,2)
72 c.stroke(p, [color.gray(0.8), style.linewidth.THICk])
73 c.stroke(p.normpath(), [color.gray(0.8), style.linewidth.THICk])
74 c.stroke(p, [smoothed(radius=0.85, softness=1, obeycurv=1), style.linewidth.Thin])
75 c.stroke(p, [smoothed(radius=0.85, softness=1, obeycurv=0), color.rgb.red])
76 c.stroke(p, [smoothed(radius=0.20, softness=1, obeycurv=0), color.rgb.green])
77 c.stroke(p, [smoothed(radius=1.20, softness=1, obeycurv=0), color.rgb.blue])
79 p = path.path(
80 path.moveto(0,10),
81 path.curveto(1,10, 4,12, 2,11),
82 path.curveto(4,8, 4,12, 0,11)
84 c.stroke(p, [color.gray(0.8), style.linewidth.THICk])
85 c.stroke(p.normpath(), [color.gray(0.8), style.linewidth.THICk])
86 c.stroke(p, [smoothed(radius=0.85, softness=1, obeycurv=1), style.linewidth.Thin])
87 c.stroke(p, [smoothed(radius=0.85, softness=1, obeycurv=0), color.rgb.red])
88 c.stroke(p, [smoothed(radius=0.20, softness=1, obeycurv=0), color.rgb.green])
89 c.stroke(p, [smoothed(radius=1.20, softness=1, obeycurv=0), color.rgb.blue])
91 c=canvas.canvas()
92 dotest(c, 0, 0, "testcycloid")
93 dotest(c, 17, 0, "testsmoothed")
94 c.writeEPSfile("test_deformer", paperformat=document.paperformat.A4, rotated=0, fittosize=1)
95 c.writePDFfile("test_deformer")