2 import sys
; sys
.path
.insert(0, "../..")
5 from random
import Random
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!
17 testparams
= [i
/ (n_testpoints
- 1.0) for i
in range(n_testpoints
)]
19 # a canvas for drawing
24 # loop over many different cases
27 for cnt
in range(n_tests
):
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
)
44 for controldistpair
in controldistpairs
:
45 alpha
, beta
= controldistpair
46 reprocurves
.append(normpath
.normcurve_pt(
48 a
[0] + alpha
* tbeg
[0], a
[1] + alpha
* tbeg
[1],
49 d
[0] - beta
* tend
[0], d
[1] - beta
* tend
[1],
52 # analyse the quality of the reproduction
53 minmaxdist
= float("inf")
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
:
62 maxdists
.append(maxdist
)
64 # print complaints for too bad reproductions
65 if minindex
!= 0 or minmaxdist
> accuracy
:
68 if minmaxdist
> accuracy
:
69 print "%4d Smallest distance is %f" % (cnt
, minmaxdist
)
72 print "%4d Failure: no solution found" % (cnt
)
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:
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()])
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)])
98 print "failures, successes = ", n_failures
, ", ", n_successes
99 can
.writeEPSfile("test_bezier")
102 test_reproductions(43, 10000, 100, 100, 1.0e-2)
105 # vim:foldmethod=marker:foldmarker=<<<,>>>