remove access analyser
[PyX/mjg.git] / test / experimental / test_bezier.py
blob622ae7a9594b3ab5ffe115e140c3a855a3b0bfe3
1 #!/usr/bin/env python
2 import sys; sys.path.insert(0, "../..")
4 import math
5 from random import Random
7 from pyx import *
8 from pyx import normpath
9 from pyx.deformer import controldists_from_endgeometry_pt
10 from pyx.normpath import _epsilon
12 def test_reproductions(seed, n_tests, xmax, ymax, accuracy): # <<<
13 # insert xmax, ymax, accuracy in pt!
15 r = Random(seed)
16 n_testpoints = 20
17 testparams = [i / (n_testpoints - 1.0) for i in range(n_testpoints)]
19 # a canvas for drawing
20 can = canvas.canvas()
21 xpos = 0
22 ypos = 0
24 # loop over many different cases
25 n_failures = 0
26 n_successes = 0
27 for cnt in range(n_tests):
29 # the original curve
30 a = r.uniform(0,xmax), r.uniform(0,ymax)
31 b = r.uniform(0,xmax), r.uniform(0,ymax)
32 c = r.uniform(0,xmax), r.uniform(0,ymax)
33 d = r.uniform(0,xmax), r.uniform(0,ymax)
34 origcurve = normpath.normcurve_pt(a[0], a[1], b[0], b[1], c[0], c[1], d[0], d[1])
35 tbeg, tend = origcurve.rotation([0, 1])
36 cbeg, cend = origcurve.curvature_pt([0, 1])
37 # raise an error if one of the params is invalid:
38 tbeg, tend, cbeg, cend
39 tbeg, tend = tbeg.apply_pt(1, 0), tend.apply_pt(1, 0)
41 # the reproduced curve
42 controldistpairs = controldists_from_endgeometry_pt(a, d, tbeg, tend, cbeg, cend, _epsilon)
43 reprocurves = []
44 for controldistpair in controldistpairs:
45 alpha, beta = controldistpair
46 reprocurves.append(normpath.normcurve_pt(
47 a[0], a[1],
48 a[0] + alpha * tbeg[0], a[1] + alpha * tbeg[1],
49 d[0] - beta * tend[0], d[1] - beta * tend[1],
50 d[0], d[1]))
52 # analyse the quality of the reproduction
53 minmaxdist = float("inf")
54 minindex = -1
55 maxdists = []
56 for i, reprocurve in enumerate(reprocurves):
57 maxdist = max([math.hypot(p[0]-q[0], p[1]-q[1])
58 for p,q in zip(origcurve.at_pt(testparams), reprocurve.at_pt(testparams))])
59 if maxdist < minmaxdist:
60 minmaxdist = maxdist
61 minindex = i
62 maxdists.append(maxdist)
64 # print complaints for too bad reproductions
65 if minindex != 0 or minmaxdist > accuracy:
66 n_failures += 1
68 if minmaxdist > accuracy:
69 print "%4d Smallest distance is %f" % (cnt, minmaxdist)
71 if minindex == -1:
72 print "%4d Failure: no solution found" % (cnt)
74 if minindex > 0:
75 print "%4d Wrong sorting: entry %d is the smallest" % (cnt, minindex)
77 if minindex >=0 and not (controldistpairs[minindex][0] >= 0 and controldistpairs[minindex][1] >= 0):
78 print "%4d Failure: signs are wrong" % (cnt)
80 # selectively draw the curves:
81 #if minindex == -1:
82 #if minindex > 0:
83 if minmaxdist > accuracy:
84 # draw the failure curves
85 can.stroke(path.rect_pt(0,0,xmax,ymax), [trafo.translate_pt(xpos, ypos)])
86 can.draw(normpath.normpath([normpath.normsubpath([origcurve])]), [trafo.translate_pt(xpos, ypos), deco.stroked([style.linewidth.THIck]), deco.shownormpath()])
87 if minindex != -1:
88 can.stroke(normpath.normpath([normpath.normsubpath([reprocurves[minindex]])]), [trafo.translate_pt(xpos, ypos), color.rgb.red])
89 can.text(0, 0, r"(%d)" % (cnt), [trafo.translate_pt(xpos+0.5*xmax, ypos), text.halign.center, text.vshift(2.0)])
90 xpos += 1.1*xmax
91 if xpos > 11*xmax:
92 xpos = 0
93 ypos -= 1.1*ymax
94 ypos -= 15
95 else:
96 n_successes += 1
98 print "failures, successes = ", n_failures, ", ", n_successes
99 can.writeEPSfile("test_bezier")
100 # >>>
102 test_reproductions(43, 10000, 100, 100, 1.0e-2)
105 # vim:foldmethod=marker:foldmarker=<<<,>>>