unit initializing by strings disabled
[PyX/mjg.git] / test / functional / test_path.py
blob307e380c4867a784f256dbef5330bbf6cf8c650d
1 #!/usr/bin/env python
2 import sys; sys.path[:0] = ["../.."]
4 from pyx import *
5 from pyx.path import *
7 def bboxrect(cmd):
8 return cmd.bbox().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))
17 class cross(path):
18 def __init__(self, x, y):
19 self.path=[moveto(x,y),
20 rmoveto(-0.1, -0.1),
21 rlineto(0.2, 0.2),
22 rmoveto(-0.1, -0.1),
23 rmoveto(-0.1, +0.1),
24 rlineto(0.2, -0.2)]
27 def drawpathwbbox(c, p):
28 c.stroke(p, [color.rgb.red])
29 np=normpath(p)
30 c.stroke(np, [color.rgb.green, style.linestyle.dashed])
31 c.stroke(bboxrect(p))
34 def testarcs(c):
35 def testarc(c, x, y, phi1, phi2):
36 p=path(arc(x,y, 0.5, phi1, phi2))
37 np=normpath(p)
38 c.stroke(p, [color.rgb.red])
39 c.stroke(np, [color.rgb.green, style.linestyle.dashed])
41 def testarcn(c, x, y, phi1, phi2):
42 p=path(arcn(x,y, 0.5, phi1, phi2))
43 np=normpath(p)
44 c.stroke(p, [color.rgb.red])
45 c.stroke(np, [color.rgb.green, style.linestyle.dashed])
47 def testarct(c, r, x0, y0, dx1, dy1, dx2, dy2):
48 p=path(moveto(x0,y0), arct(x0+dx1,y0+dy1, x0+dx2, y0+dy2, r), rlineto(dx2-dx1, dy2-dy1), closepath())
49 np=normpath(p)
50 c.stroke(p, [color.rgb.red, style.linewidth.Thick])
51 c.stroke(np, [color.rgb.green, style.linewidth.THin, deco.filled([color.rgb.green])])
53 testarc(c, 1, 2, 0, 90)
54 testarc(c, 2, 2, -90, 90)
55 testarc(c, 3, 2, 270, 90)
56 testarc(c, 4, 2, 90, -90)
57 testarc(c, 5, 2, 90, 270)
58 testarc(c, 4, 3, 45, -90)
59 testarc(c, 2, 3, 45, -90-2*360)
60 testarc(c, 1, 3, 45, +90+2*360)
62 testarcn(c, 1, 5, 0, 90)
63 testarcn(c, 2, 5, -90, 90)
64 testarcn(c, 3, 5, 270, 90)
65 testarcn(c, 4, 5, 90, -90)
66 testarcn(c, 5, 5, 90, 270)
67 testarcn(c, 4, 6, 45, -90)
68 testarcn(c, 2, 6, 45, -90-360)
69 testarcn(c, 1, 6, 45, -90+360)
71 testarct(c, 0.5, 1, 8, 0, 1, 1, 1)
72 testarct(c, 0.5, 3, 8, 1, 1, 1, 2)
73 testarct(c, 0.5, 5, 8, 1, 0, 2, 1)
74 testarct(c, 0.5, 7, 8, 1, 0, 2, 0)
75 testarct(c, 0.0, 9, 8, 0, 1, 1, 1)
77 # testarct(c, 0.5, 11, 8, 0, 1, 0, 0) # not allowed
80 def testintersectbezier(c):
81 p=normpath(path(moveto(0,0), curveto(2,6,4,5,2,9)), epsilon=1e-4)
82 q=normpath(path(moveto(2,0), curveto(2,6,4,12,1,6)), epsilon=1e-4)
84 c.stroke(q, [style.linewidth.THIN])
85 c.stroke(p, [style.linewidth.THIN])
87 isect = p.intersect(q)
89 for i in isect[0]:
90 x, y = p.at(i)
91 c.stroke(cross(x, y), [style.linewidth.THIN])
93 def testintersectcircle(c):
94 k=circle(0, 0, 2)
95 l=line(0,0, 3, 0)
96 c.stroke(k, [style.linewidth.THIN])
97 c.stroke(l, [style.linewidth.THIN])
99 isect = k.intersect(l)
100 assert len(isect[0])==1, "double count of intersections"
102 for i in isect[0]:
103 x, y = k.at(i)
104 c.stroke(cross(x, y), [style.linewidth.THIN])
106 def testintersectline(c):
107 l1=line(0, 0, 1, 1)
108 l2=line(0, 1, 1, 0)
109 c.stroke(l1, [style.linewidth.THIN])
110 c.stroke(l2, [style.linewidth.THIN])
112 isect = l1.intersect(l2)
114 for i in isect[0]:
115 x, y = l1.at(i)
116 c.stroke(circle(x, y, 0.1), [style.linewidth.THIN])
119 def testnormpathtrafo(c):
120 p = path(moveto(0, 5),
121 curveto(2, 1, 4, 0, 2, 4),
122 rcurveto(-3, 2, 1, 2, 3, 6),
123 rlineto(0, 3),
124 closepath())
126 c.stroke(p)
127 c.stroke(normpath(p), [color.rgb.green, style.linestyle.dashed])
128 c.stroke(p.transformed(trafo.translate(3,1)), [color.rgb.red])
129 c.insert(canvas.canvas([trafo.translate(3,1)])).stroke(p,
130 [color.rgb.green,
131 style.linestyle.dashed])
133 c.stroke(p.reversed(), [color.rgb.blue, style.linestyle.dotted, style.linewidth.THick])
135 c.stroke(cross(*(p.at(0))))
136 c.stroke(cross(*(p.reversed().at(0))))
138 p1, p2 = p.split([1.0, 2.1])
139 c.stroke(p1, [color.rgb.red, style.linestyle.dashed])
140 c.stroke(p2, [color.rgb.green, style.linestyle.dashed])
142 circ1 = circle(0, 10, 1)
143 circ2 = circle(1.7, 10, 1)
145 c.stroke(circ1)
146 c.stroke(circ2)
148 isectcirc1, isectcirc2 = circ1.intersect(circ2)
149 segment1 = circ1.split(isectcirc1)[0]
150 segment2 = circ2.split(isectcirc2)[1]
152 segment = segment1 << segment2
153 segment.append(closepath())
155 c.stroke(segment, [style.linewidth.THick, deco.filled([color.rgb.green])])
157 def testtangent(c):
158 p=path(moveto(0,5),
159 curveto(2,1,4,0,2,4),
160 rcurveto(-3,2,1,2,3,6),
161 rlineto(2,3))+circle(5,5,1)
162 c.stroke(p, [style.linewidth.THick])
163 for i in range(int(p.range())*2):
164 c.stroke(p.tangent(i/2.0, length=20*unit.t_pt), [color.rgb.blue, deco.earrow.normal])
165 c.stroke(line(0, 0, 1, 0).transformed(p.trafo(i/2.0)), [color.rgb.green, deco.earrow.normal])
166 c.stroke(line(0, 0, 0, 1).transformed(p.trafo(i/2.0)), [color.rgb.red, deco.earrow.normal])
168 # test the curvature
169 cc = canvas.canvas()
170 cc.insert(p)
171 cc = canvas.canvas([canvas.clip(cc.bbox().path())])
172 for i in range(int(p.range())*2):
173 radius = p.curvradius(i/2.0)
174 if radius is not None:
175 radius = unit.tocm(radius)
176 pos = p.trafo(i/2.0).apply(0,radius*radius/abs(radius))
177 cc.stroke(circle(0, 0,unit.t_cm * abs(radius)), [color.grey(0.5), trafo.translate(*pos)])
178 c.insert(cc)
181 def testarcbbox(c):
182 for phi in range(0,360,30):
183 drawpathwbbox(c,path(arc(phi*0.1, phi*0.1, 1, 0, phi)))
185 for phi in range(0,360,30):
186 drawpathwbbox(c,path(arc(phi*0.1, 5+phi*0.1, 1, phi, 360)))
188 for phi in range(0,360,30):
189 drawpathwbbox(c,path(arc(phi*0.1, 10+phi*0.1, 1, phi, phi+30)))
191 for phi in range(0,360,30):
192 drawpathwbbox(c,path(arc(phi*0.1, 15+phi*0.1, 1, phi, phi+120)))
194 for phi in range(0,360,30):
195 drawpathwbbox(c,path(arc(phi*0.1, 20+phi*0.1, 1, phi, phi+210)))
197 for phi in range(0,360,30):
198 drawpathwbbox(c,path(arc(phi*0.1, 25+phi*0.1, 1, phi, phi+300)))
200 for phi in range(0,360,30):
201 drawpathwbbox(c,path(arc(phi*0.1, 30+phi*0.1, 1, phi, phi+390)))
204 for phi in range(0,360,30):
205 drawpathwbbox(c,path(moveto(20+phi*0.1, phi*0.09),
206 arc(20+phi*0.1, phi*0.1, 1, 0, phi)))
208 for phi in range(0,360,30):
209 drawpathwbbox(c,path(moveto(20+phi*0.1, 5+phi*0.11),
210 arc(20+phi*0.1, 5+phi*0.1, 1, 0, phi)))
212 for phi in range(0,360,30):
213 drawpathwbbox(c,path(moveto(20+phi*0.1, 10+phi*0.09),
214 arcn(20+phi*0.1, 10+phi*0.1, 1, 0, phi)))
216 for phi in range(0,360,30):
217 drawpathwbbox(c,path(moveto(20+phi*0.1, 15+phi*0.11),
218 arcn(20+phi*0.1, 15+phi*0.1, 1, 0, phi)))
220 for phi in range(0,360,30):
221 drawpathwbbox(c,path(moveto(50+phi*0.1, phi*0.09),
222 arc(50+phi*0.1, phi*0.1, 1, 0, phi),
223 rlineto(1,1)))
225 for phi in range(0,360,30):
226 drawpathwbbox(c,path(moveto(50+phi*0.1, 5+phi*0.11),
227 arc(50+phi*0.1, 5+phi*0.1, 1, 0, phi),
228 rlineto(1,1)))
230 for phi in range(0,360,30):
231 drawpathwbbox(c,path(moveto(50+phi*0.1, 10+phi*0.09),
232 arcn(50+phi*0.1, 10+phi*0.1, 1, 0, phi),
233 rlineto(1,1)))
235 for phi in range(0,360,30):
236 drawpathwbbox(c,path(moveto(50+phi*0.1, 15+phi*0.11),
237 arcn(50+phi*0.1, 15+phi*0.1, 1, 0, phi),
238 rlineto(1,1)))
241 def testcurvetobbox(c):
242 drawpathwbbox(c,path(moveto(10,60), curveto(12,66,14,65,12,69)))
245 def testtrafobbox(c):
246 sc=c.insert(canvas.canvas([trafo.translate(0,40).rotated(10)]))
248 p=path(moveto(10,10), curveto(12,16,14,15,12,19)); drawpathwbbox(sc,p)
249 p=path(moveto(5,17), curveto(6,18, 5,16, 7,15)); drawpathwbbox(sc,p)
252 def testclipbbox(c):
253 clip=canvas.clip(rect(11,11,10,5))
255 p1=path(moveto(10,10), curveto(12,16,14,15,12,19));
256 p2=path(moveto(12,12), curveto(6,18, 5,16, 7,15));
258 # just a simple test for clipping
259 sc=c.insert(canvas.canvas([clip]))
260 drawpathwbbox(sc,p1)
261 drawpathwbbox(sc,p2)
263 # more complicated operations
265 # 1. transformation followed by clipping:
266 # in this case, the clipping path will be evaluated in the
267 # context of the already transformed canvas, so that the
268 # actually displayed portion of the path should be the same
270 sc=c.insert(canvas.canvas([trafo.translate(5,0), clip]))
271 drawpathwbbox(sc,p1)
272 drawpathwbbox(sc,p2)
274 # 2. clipping followed by transformation
275 # in this case, the clipping path will not be transformed, so
276 # that the display portionof the path should change
278 sc=c.insert(canvas.canvas([clip, trafo.translate(1,1)]))
279 drawpathwbbox(sc,p1)
280 drawpathwbbox(sc,p2)
282 def testarclentoparam(c):
283 curve=path(moveto(0,0), lineto(0,5), curveto(5,0,0,10,5,5), closepath(),
284 moveto(5,0), lineto(10,5))
285 ll = curve.arclen()
286 # l=[-0.8*ll, -0.6*ll, -0.4*ll, -0.2*ll, 0, 0.1*ll, 0.3*ll, 0.5*ll, 0.7*ll, 0.9*ll]
287 l=[0, 0.1*ll, 0.2*ll, 0.3*ll, 0.4*ll, 0.5*ll, 0.6*ll, 0.7*ll, 0.8*ll, 0.9*ll]
288 cols=[color.gray.black, color.gray(0.3), color.gray(0.7), color.rgb.red,
289 color.rgb.green, color.rgb.blue, color.cmyk(1,0,0,0),
290 color.cmyk(0,1,0,0), color.cmyk(0,0,1,0), color.gray.black]
291 t=curve.arclentoparam(l)
292 c.stroke(curve)
293 for i in range(len(t)):
294 c.draw(path(circle(curve.at(t[i])[0], curve.at(t[i])[1], 0.1)), [deco.filled([cols[i]]), deco.stroked()])
297 c = canvas.canvas()
298 dotest(c, 0, 0, "testarcs")
299 dotest(c, 2, 12, "testintersectbezier")
300 dotest(c, 10,11, "testnormpathtrafo")
301 dotest(c, 12, -4, "testtangent")
302 dotest(c, 5, -4, "testintersectcircle")
303 dotest(c, 9, -4, "testintersectline")
304 dotest(c, 21, 12, "testarclentoparam")
305 c.writeEPSfile("test_path", paperformat="a4", rotated=0, fittosize=1)
306 c.writePDFfile("test_path")
308 c = canvas.canvas()
309 testarcbbox(c)
310 testcurvetobbox(c)
311 testtrafobbox(c)
312 testclipbbox(c)
313 c.writeEPSfile("test_bbox", paperformat="a4", rotated=1, fittosize=1)
314 c.writePDFfile("test_bbox")
315 #c.writeEPSfile("test_bbox")