curvature testing adjusted
[PyX/mjg.git] / test / functional / test_path.py
blobe38e5512da4e189f510991d464be8e3a6eab45d0
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 # bbox=cmd.bbox()
9 # return rect("%f t pt" % bbox.llx, "%f t pt" % bbox.lly,
10 # "%f t pt" % (bbox.urx-bbox.llx), "%f t pt" % (bbox.ury-bbox.lly))
11 return cmd.bbox().rect()
14 def dotest(c, x, y, test):
15 c2 = c.insert(canvas.canvas(trafo.translate(x, y)))
16 eval("%s(c2)" % test)
17 c.stroke(bboxrect(c2))
20 class cross(path):
21 def __init__(self, x, y):
22 self.path=[moveto(x,y),
23 rmoveto(-0.1, -0.1),
24 rlineto(0.2, 0.2),
25 rmoveto(-0.1, -0.1),
26 rmoveto(-0.1, +0.1),
27 rlineto(0.2, -0.2)]
30 def drawpathwbbox(c, p):
31 c.stroke(p, [color.rgb.red])
32 np=normpath(p)
33 c.stroke(np, [color.rgb.green, style.linestyle.dashed])
34 c.stroke(bboxrect(p))
37 def testarcs(c):
38 def testarc(c, x, y, phi1, phi2):
39 p=path(arc(x,y, 0.5, phi1, phi2))
40 np=normpath(p)
41 c.stroke(p, [color.rgb.red])
42 c.stroke(np, [color.rgb.green, style.linestyle.dashed])
44 def testarcn(c, x, y, phi1, phi2):
45 p=path(arcn(x,y, 0.5, phi1, phi2))
46 np=normpath(p)
47 c.stroke(p, [color.rgb.red])
48 c.stroke(np, [color.rgb.green, style.linestyle.dashed])
50 def testarct(c, r, x0, y0, dx1, dy1, dx2, dy2):
51 p=path(moveto(x0,y0), arct(x0+dx1,y0+dy1, x0+dx2, y0+dy2, r), rlineto(dx2-dx1, dy2-dy1), closepath())
52 np=normpath(p)
53 c.stroke(p, [color.rgb.red, style.linewidth.Thick])
54 c.stroke(np, [color.rgb.green, style.linewidth.THin, deco.filled([color.rgb.green])])
56 testarc(c, 1, 2, 0, 90)
57 testarc(c, 2, 2, -90, 90)
58 testarc(c, 3, 2, 270, 90)
59 testarc(c, 4, 2, 90, -90)
60 testarc(c, 5, 2, 90, 270)
61 testarc(c, 4, 3, 45, -90)
62 testarc(c, 2, 3, 45, -90-2*360)
63 testarc(c, 1, 3, 45, +90+2*360)
65 testarcn(c, 1, 5, 0, 90)
66 testarcn(c, 2, 5, -90, 90)
67 testarcn(c, 3, 5, 270, 90)
68 testarcn(c, 4, 5, 90, -90)
69 testarcn(c, 5, 5, 90, 270)
70 testarcn(c, 4, 6, 45, -90)
71 testarcn(c, 2, 6, 45, -90-360)
72 testarcn(c, 1, 6, 45, -90+360)
74 testarct(c, 0.5, 1, 8, 0, 1, 1, 1)
75 testarct(c, 0.5, 3, 8, 1, 1, 1, 2)
76 testarct(c, 0.5, 5, 8, 1, 0, 2, 1)
77 testarct(c, 0.5, 7, 8, 1, 0, 2, 0)
78 testarct(c, 0.0, 9, 8, 0, 1, 1, 1)
80 # testarct(c, 0.5, 11, 8, 0, 1, 0, 0) # not allowed
83 def testintersectbezier(c):
84 p=normpath(path(moveto(0,0), curveto(2,6,4,5,2,9)), epsilon=1e-4)
85 q=normpath(path(moveto(2,0), curveto(2,6,4,12,1,6)), epsilon=1e-4)
87 c.stroke(q, [style.linewidth.THIN])
88 c.stroke(p, [style.linewidth.THIN])
90 isect = p.intersect(q)
92 for i in isect[0]:
93 x, y = p.at(i)
94 c.stroke(cross(x, y), [style.linewidth.THIN])
96 def testintersectcircle(c):
97 k=circle(0, 0, 2)
98 l=line(0,0, 3, 0)
99 c.stroke(k, [style.linewidth.THIN])
100 c.stroke(l, [style.linewidth.THIN])
102 isect = k.intersect(l)
103 assert len(isect[0])==1, "double count of intersections"
105 for i in isect[0]:
106 x, y = k.at(i)
107 c.stroke(cross(x, y), [style.linewidth.THIN])
109 def testintersectline(c):
110 l1=line(0, 0, 1, 1)
111 l2=line(0, 1, 1, 0)
112 c.stroke(l1, [style.linewidth.THIN])
113 c.stroke(l2, [style.linewidth.THIN])
115 isect = l1.intersect(l2)
117 for i in isect[0]:
118 x, y = l1.at(i)
119 c.stroke(circle(x, y, 0.1), [style.linewidth.THIN])
122 def testnormpathtrafo(c):
123 p = path(moveto(0, 5),
124 curveto(2, 1, 4, 0, 2, 4),
125 rcurveto(-3, 2, 1, 2, 3, 6),
126 rlineto(0, 3),
127 closepath())
129 c.stroke(p)
130 c.stroke(normpath(p), [color.rgb.green, style.linestyle.dashed])
131 c.stroke(p.transformed(trafo.translate(3,1)), [color.rgb.red])
132 c.insert(canvas.canvas(trafo.translate(3,1))).stroke(p,
133 [color.rgb.green,
134 style.linestyle.dashed])
136 c.stroke(p.reversed(), [color.rgb.blue, style.linestyle.dotted, style.linewidth.THick])
138 c.stroke(cross(*(p.at(0))))
139 c.stroke(cross(*(p.reversed().at(0))))
141 p1, p2 = p.split([1.0, 2.1])
142 c.stroke(p1, [color.rgb.red, style.linestyle.dashed])
143 c.stroke(p2, [color.rgb.green, style.linestyle.dashed])
145 circ1 = circle(0, 10, 1)
146 circ2 = circle(1.7, 10, 1)
148 c.stroke(circ1)
149 c.stroke(circ2)
151 isectcirc1, isectcirc2 = circ1.intersect(circ2)
152 segment1 = circ1.split(isectcirc1)[0]
153 segment2 = circ2.split(isectcirc2)[1]
155 segment = segment1 << segment2
156 segment.append(closepath())
158 c.stroke(segment, [style.linewidth.THick, deco.filled([color.rgb.green])])
160 def testtangent(c):
161 p=path(moveto(0,5),
162 curveto(2,1,4,0,2,4),
163 rcurveto(-3,2,1,2,3,6),
164 rlineto(2,3))+circle(5,5,1)
165 c.stroke(p, [style.linewidth.THick])
166 for i in range(int(p.range())*2):
167 c.stroke(p.tangent(i/2.0, length="20 t pt"), [color.rgb.blue, deco.earrow.normal])
168 c.stroke(line(0, 0, 1, 0).transformed(p.trafo(i/2.0)), [color.rgb.green, deco.earrow.normal])
169 c.stroke(line(0, 0, 0, 1).transformed(p.trafo(i/2.0)), [color.rgb.red, deco.earrow.normal])
171 # test the curvature
172 cc = canvas.canvas()
173 cc.insert(p)
174 cc = canvas.canvas(canvas.clip(cc.bbox().path()))
175 for i in range(int(p.range())*2):
176 radius = p.curvradius(i/2.0)
177 if radius is not None:
178 radius = unit.tocm(radius)
179 pos = p.trafo(i/2.0).apply(0,radius*radius/abs(radius))
180 cc.stroke(circle(0,0,unit.t_cm(abs(radius))), [color.grey(0.5), trafo.translate(*pos)])
181 c.insert(cc)
184 def testarcbbox(c):
185 for phi in range(0,360,30):
186 drawpathwbbox(c,path(arc(phi*0.1, phi*0.1, 1, 0, phi)))
188 for phi in range(0,360,30):
189 drawpathwbbox(c,path(arc(phi*0.1, 5+phi*0.1, 1, phi, 360)))
191 for phi in range(0,360,30):
192 drawpathwbbox(c,path(arc(phi*0.1, 10+phi*0.1, 1, phi, phi+30)))
194 for phi in range(0,360,30):
195 drawpathwbbox(c,path(arc(phi*0.1, 15+phi*0.1, 1, phi, phi+120)))
197 for phi in range(0,360,30):
198 drawpathwbbox(c,path(arc(phi*0.1, 20+phi*0.1, 1, phi, phi+210)))
200 for phi in range(0,360,30):
201 drawpathwbbox(c,path(arc(phi*0.1, 25+phi*0.1, 1, phi, phi+300)))
203 for phi in range(0,360,30):
204 drawpathwbbox(c,path(arc(phi*0.1, 30+phi*0.1, 1, phi, phi+390)))
207 for phi in range(0,360,30):
208 drawpathwbbox(c,path(moveto(20+phi*0.1, phi*0.09),
209 arc(20+phi*0.1, phi*0.1, 1, 0, phi)))
211 for phi in range(0,360,30):
212 drawpathwbbox(c,path(moveto(20+phi*0.1, 5+phi*0.11),
213 arc(20+phi*0.1, 5+phi*0.1, 1, 0, phi)))
215 for phi in range(0,360,30):
216 drawpathwbbox(c,path(moveto(20+phi*0.1, 10+phi*0.09),
217 arcn(20+phi*0.1, 10+phi*0.1, 1, 0, phi)))
219 for phi in range(0,360,30):
220 drawpathwbbox(c,path(moveto(20+phi*0.1, 15+phi*0.11),
221 arcn(20+phi*0.1, 15+phi*0.1, 1, 0, phi)))
223 for phi in range(0,360,30):
224 drawpathwbbox(c,path(moveto(50+phi*0.1, phi*0.09),
225 arc(50+phi*0.1, phi*0.1, 1, 0, phi),
226 rlineto(1,1)))
228 for phi in range(0,360,30):
229 drawpathwbbox(c,path(moveto(50+phi*0.1, 5+phi*0.11),
230 arc(50+phi*0.1, 5+phi*0.1, 1, 0, phi),
231 rlineto(1,1)))
233 for phi in range(0,360,30):
234 drawpathwbbox(c,path(moveto(50+phi*0.1, 10+phi*0.09),
235 arcn(50+phi*0.1, 10+phi*0.1, 1, 0, phi),
236 rlineto(1,1)))
238 for phi in range(0,360,30):
239 drawpathwbbox(c,path(moveto(50+phi*0.1, 15+phi*0.11),
240 arcn(50+phi*0.1, 15+phi*0.1, 1, 0, phi),
241 rlineto(1,1)))
244 def testcurvetobbox(c):
245 drawpathwbbox(c,path(moveto(10,60), curveto(12,66,14,65,12,69)))
248 def testtrafobbox(c):
249 sc=c.insert(canvas.canvas(trafo.translate(0,40).rotated(10)))
251 p=path(moveto(10,10), curveto(12,16,14,15,12,19)); drawpathwbbox(sc,p)
252 p=path(moveto(5,17), curveto(6,18, 5,16, 7,15)); drawpathwbbox(sc,p)
255 def testclipbbox(c):
256 clip=canvas.clip(rect(11,11,10,5))
258 p1=path(moveto(10,10), curveto(12,16,14,15,12,19));
259 p2=path(moveto(12,12), curveto(6,18, 5,16, 7,15));
261 # just a simple test for clipping
262 sc=c.insert(canvas.canvas(clip))
263 drawpathwbbox(sc,p1)
264 drawpathwbbox(sc,p2)
266 # more complicated operations
268 # 1. transformation followed by clipping:
269 # in this case, the clipping path will be evaluated in the
270 # context of the already transformed canvas, so that the
271 # actually displayed portion of the path should be the same
273 sc=c.insert(canvas.canvas(trafo.translate(5,0), clip))
274 drawpathwbbox(sc,p1)
275 drawpathwbbox(sc,p2)
277 # 2. clipping followed by transformation
278 # in this case, the clipping path will not be transformed, so
279 # that the display portionof the path should change
281 sc=c.insert(canvas.canvas(clip, trafo.translate(1,1)))
282 drawpathwbbox(sc,p1)
283 drawpathwbbox(sc,p2)
285 def testarclentoparam(c):
286 curve=path(moveto(0,0), lineto(0,5), curveto(5,0,0,10,5,5), closepath(),
287 moveto(5,0), lineto(10,5))
288 ll = curve.arclen()
289 # 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]
290 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]
291 cols=[color.gray.black, color.gray(0.3), color.gray(0.7), color.rgb.red,
292 color.rgb.green, color.rgb.blue, color.cmyk(1,0,0,0),
293 color.cmyk(0,1,0,0), color.cmyk(0,0,1,0), color.gray.black]
294 t=curve.arclentoparam(l)
295 c.stroke(curve)
296 for i in range(len(t)):
297 c.draw(path(circle(curve.at(t[i])[0], curve.at(t[i])[1], 0.1)), [deco.filled([cols[i]]), deco.stroked()])
300 c=canvas.canvas()
301 dotest(c, 0, 0, "testarcs")
302 dotest(c, 2, 12, "testintersectbezier")
303 dotest(c, 10,11, "testnormpathtrafo")
304 dotest(c, 12, -4, "testtangent")
305 dotest(c, 5, -4, "testintersectcircle")
306 dotest(c, 9, -4, "testintersectline")
307 dotest(c, 21, 12, "testarclentoparam")
308 c.writeEPSfile("test_path", paperformat="a4", rotated=0, fittosize=1)
310 c=canvas.canvas()
311 testarcbbox(c)
312 testcurvetobbox(c)
313 testtrafobbox(c)
314 testclipbbox(c)
315 c.writeEPSfile("test_bbox", paperformat="a4", rotated=1, fittosize=1)
316 #c.writeEPSfile("test_bbox")