convert to new normpath creation scheme
[PyX/mjg.git] / test / unit / test_path.py
blobae79dd40808b1f603f28e81132b7918d77ec0e3e
1 import sys
2 if sys.path[0] != "../..":
3 sys.path.insert(0, "../..")
5 import unittest
7 from pyx import *
8 from pyx.path import *
10 epsilon = 1e-5
11 def isEqual(l1, l2):
12 return abs(unit.topt(l1-l2))<epsilon
15 class NormpathTestCase(unittest.TestCase):
16 def testsplit(self):
17 p = normsubpath([normline(0, -1, 1, 0.1),
18 normline(1, 0.1, 2, -1),
19 normline(2, -1, 1, -1),
20 normline(1, -1, 1, 1)])
22 self.failUnlessEqual(len(p.split([0.9, 1.1, 3.5])), 4)
25 # p = path(moveto(0,0), lineto(1,0), moveto(2,0), lineto(3,0))
26 # np = normpath(p)
28 # # one split parameter
29 # sp = np.split([0])
30 # assert len(sp)==2 and sp[0] is None and isEqual(sp[1].arclen(), 2)
32 # sp = np.split([0.5])
33 # assert len(sp)==2 and isEqual(sp[0].arclen(), 0.5) and isEqual(sp[1].arclen(), 1.5)
35 # sp = np.split([1])
36 # assert len(sp)==2 and isEqual(sp[0].arclen(), 1) and isEqual(sp[1].arclen(), 1)
38 # sp = np.split([1.5])
39 # assert len(sp)==2 and isEqual(sp[0].arclen(), 1.5) and isEqual(sp[1].arclen(), 0.5)
41 # sp = np.split([2])
42 # assert len(sp)==2 and isEqual(sp[0].arclen(), 2) and sp[1] is None
44 # # two split parameters
45 # sp = np.split([0, 0.5])
46 # assert len(sp)==3 and sp[0] is None and isEqual(sp[1].arclen(), 0.5) and isEqual(sp[2].arclen(), 1.5)
48 # sp = np.split([0, 1])
49 # assert len(sp)==3 and sp[0] is None and isEqual(sp[1].arclen(), 1) and isEqual(sp[2].arclen(), 1)
51 # sp = np.split([0, 1.5])
52 # assert len(sp)==3 and sp[0] is None and isEqual(sp[1].arclen(), 1.5) and isEqual(sp[2].arclen(), 0.5)
54 # sp = np.split([0, 2])
55 # assert len(sp)==3 and sp[0] is None and isEqual(sp[1].arclen(), 2) and sp[2] is None
57 # sp = np.split([0.5, 1])
58 # assert len(sp)==3 and isEqual(sp[0].arclen(), 0.5) and isEqual(sp[1].arclen(), 0.5) and isEqual(sp[2].arclen(), 1)
60 # sp = np.split([0.5, 1.5])
61 # assert len(sp)==3 and isEqual(sp[0].arclen(), 0.5) and isEqual(sp[1].arclen(), 1) and isEqual(sp[2].arclen(), 0.5)
63 # sp = np.split([0.5, 2])
64 # assert len(sp)==3 and isEqual(sp[0].arclen(), 0.5) and isEqual(sp[1].arclen(), 1.5) and sp[2] is None
66 # sp = np.split([1, 1.5])
67 # assert len(sp)==3 and isEqual(sp[0].arclen(), 1) and isEqual(sp[1].arclen(), 0.5) and isEqual(sp[2].arclen(), 0.5)
69 # sp = np.split([1, 2])
70 # assert len(sp)==3 and isEqual(sp[0].arclen(), 1) and isEqual(sp[1].arclen(), 1) and sp[2] is None
72 # sp = np.split([1.5, 2])
73 # assert len(sp)==3 and isEqual(sp[0].arclen(), 1.5) and isEqual(sp[1].arclen(), 0.5) and sp[2] is None
75 # # three split parameters
76 # sp = np.split([0, 0.5, 1])
77 # assert len(sp)==4 and sp[0] is None and isEqual(sp[1].arclen(), 0.5) and isEqual(sp[2].arclen(), 0.5) and isEqual(sp[3].arclen(), 1)
79 # sp = np.split([0, 0.5, 1.5])
80 # assert len(sp)==4 and sp[0] is None and isEqual(sp[1].arclen(), 0.5) and isEqual(sp[2].arclen(), 1) and isEqual(sp[3].arclen(), 0.5)
82 # sp = np.split([0, 0.5, 2])
83 # assert len(sp)==4 and sp[0] is None and isEqual(sp[1].arclen(), 0.5) and isEqual(sp[2].arclen(), 1.5) and sp[3] is None
85 # sp = np.split([0, 1, 1.5])
86 # assert len(sp)==4 and sp[0] is None and isEqual(sp[1].arclen(), 1) and isEqual(sp[2].arclen(), 0.5) and isEqual(sp[3].arclen(), 0.5)
88 # sp = np.split([0, 1, 2])
89 # assert len(sp)==4 and sp[0] is None and isEqual(sp[1].arclen(), 1) and isEqual(sp[2].arclen(), 1) and sp[3] is None
92 # sp = np.split([0, 1.5, 2])
93 # assert len(sp)==4 and sp[0] is None and isEqual(sp[1].arclen(), 1.5) and isEqual(sp[2].arclen(), 0.5) and sp[3] is None
95 # sp = np.split([0.5, 1, 1.5])
96 # assert len(sp)==4 and isEqual(sp[0].arclen(), 0.5) and isEqual(sp[1].arclen(), 0.5) and isEqual(sp[2].arclen(), 0.5) and isEqual(sp[3].arclen(), 0.5)
98 # sp = np.split([0.5, 1.5, 2])
99 # assert len(sp)==4 and isEqual(sp[0].arclen(), 0.5) and isEqual(sp[1].arclen(), 1) and isEqual(sp[2].arclen(), 0.5) and sp[3] is None
101 # sp = np.split([1, 1.5, 2])
102 # assert len(sp)==4 and isEqual(sp[0].arclen(), 1) and isEqual(sp[1].arclen(), 0.5) and isEqual(sp[2].arclen(), 0.5) and sp[3] is None
105 # # four split parameters
106 # sp = np.split([0, 0.5, 1, 1.5])
107 # assert len(sp)==5 and sp[0] is None and isEqual(sp[1].arclen(), 0.5) and isEqual(sp[2].arclen(), 0.5) and isEqual(sp[3].arclen(), 0.5) and isEqual(sp[4].arclen(), 0.5)
109 # sp = np.split([0, 0.5, 1, 2])
110 # assert len(sp)==5 and sp[0] is None and isEqual(sp[1].arclen(), 0.5) and isEqual(sp[2].arclen(), 0.5) and isEqual(sp[3].arclen(), 1) and sp[4] is None
112 # sp = np.split([0, 0.5, 1.5, 2])
113 # assert len(sp)==5 and sp[0] is None and isEqual(sp[1].arclen(), 0.5) and isEqual(sp[2].arclen(), 1) and isEqual(sp[3].arclen(), 0.5) and sp[4] is None
115 # sp = np.split([0, 1, 1.5, 2])
116 # assert len(sp)==5 and sp[0] is None and isEqual(sp[1].arclen(), 1) and isEqual(sp[2].arclen(), 0.5) and isEqual(sp[3].arclen(), 0.5) and sp[4] is None
118 # sp = np.split([0.5, 1, 1.5, 2])
119 # assert len(sp)==5 and isEqual(sp[0].arclen(), 0.5) and isEqual(sp[1].arclen(), 0.5) and isEqual(sp[2].arclen(), 0.5) and isEqual(sp[3].arclen(), 0.5) and sp[4] is None
122 # # five split parameters
123 # sp = np.split([0, 0.5, 1, 1.5, 2])
124 # assert len(sp)==6 and sp[0] is None and isEqual(sp[1].arclen(), 0.5) and isEqual(sp[2].arclen(), 0.5) and isEqual(sp[3].arclen(), 0.5) and isEqual(sp[4].arclen(), 0.5) and sp[5] is None
126 def testshortnormsubpath(self):
127 sp = normsubpath(epsilon=1)
128 sp.append(normline(0, 0, 0.5, 0))
129 sp.append(normline(0.5, 0, 1.5, 0))
131 sp.append(normline(1.5, 0, 1.5, 0.3))
132 sp.append(normline(1.5, 0.3, 1.5, 0.6))
133 sp.append(normline(1.5, 0.6, 1.5, 0.9))
134 sp.append(normline(1.5, 0.9, 1.5, 1.2))
136 sp.append(normline(1.2, 1.5, 1.3, 1.6))
137 sp.append(normcurve(1.3, 1.6, 1.4, 1.7, 1.3, 1.7, 1.3, 1.8))
138 sp.append(normcurve(1.3, 1.8, 2.4, 2.7, 3.3, 3.7, 1.4, 1.8))
140 self.failUnlessEqual(str(sp), "subpath(open, [normline(0, 0, 1.5, 0), normline(1.5, 0, 1.5, 1.2), normcurve(1.2, 1.5, 2.4, 2.7, 3.3, 3.7, 1.4, 1.8)])")
142 def testintersectnormsubpath(self):
143 smallposy = 0.09
144 smallnegy = -0.01
145 p1 = normsubpath([normline(-1, 0, 1, 0)])
146 p2 = normsubpath([normline(0, smallposy, 0, smallnegy),
147 normline(0, smallnegy, 0, 1+smallnegy),
148 normline(0, 1+smallnegy, 0, smallnegy),
149 normline(0, smallnegy, 0, smallposy)], closed=0)
150 p1.epsilon = p2.epsilon = 0.05
151 intersect = p2.intersect(p1)
152 self.failUnlessEqual(len(intersect[0]), 2)
153 self.failUnlessAlmostEqual(intersect[0][0], 0.9)
154 self.failUnlessAlmostEqual(intersect[0][1], 2.99)
156 smallposy = 0.09
157 smallnegy = -0.01
158 p1 = normsubpath([normline(-1, 0, 1, 0)])
159 p2 = normsubpath([normline(0, smallposy, 0, smallnegy),
160 normline(0, smallnegy, 0, 1+smallnegy),
161 normline(0, 1+smallnegy, 0, smallnegy),
162 normline(0, smallnegy, 0, smallposy)], closed=1)
163 p1.epsilon = p2.epsilon = 0.05
164 intersect = p2.intersect(p1)
165 self.failUnlessEqual(len(intersect[0]), 2)
166 self.failUnlessAlmostEqual(intersect[0][0], 0.9)
167 self.failUnlessAlmostEqual(intersect[0][1], 2.99)
169 smallposy = 0.01
170 smallnegy = -0.09
171 p1 = normsubpath([normline(-1, 0, 1, 0)])
172 p2 = normsubpath([normline(0, smallposy, 0, smallnegy),
173 normline(0, smallnegy, 0, 1+smallnegy),
174 normline(0, 1+smallnegy, 0, smallnegy),
175 normline(0, smallnegy, 0, smallposy)], closed=0)
176 p1.epsilon = p2.epsilon = 0.05
177 intersect = p2.intersect(p1)
178 self.failUnlessEqual(len(intersect[0]), 4)
179 self.failUnlessAlmostEqual(intersect[0][0], 0.1)
180 self.failUnlessAlmostEqual(intersect[0][1], 1.09)
181 self.failUnlessAlmostEqual(intersect[0][2], 2.91)
182 self.failUnlessAlmostEqual(intersect[0][3], 3.9)
184 smallposy = 0.01
185 smallnegy = -0.09
186 p1 = normsubpath([normline(-1, 0, 1, 0)])
187 p2 = normsubpath([normline(0, smallposy, 0, smallnegy),
188 normline(0, smallnegy, 0, 1+smallnegy),
189 normline(0, 1+smallnegy, 0, smallnegy),
190 normline(0, smallnegy, 0, smallposy)], closed=1)
191 p1.epsilon = p2.epsilon = 0.05
192 intersect = p2.intersect(p1)
193 self.failUnlessEqual(len(intersect[0]), 3)
194 self.failUnlessAlmostEqual(intersect[0][0], 0.1)
195 self.failUnlessAlmostEqual(intersect[0][1], 1.09)
196 self.failUnlessAlmostEqual(intersect[0][2], 2.91)
198 smallposy = 0.01
199 smallnegy = -0.01
200 p1 = normsubpath([normline(-1, 0, 1, 0)])
201 p2 = normsubpath([normline(0, smallposy, 0, smallnegy),
202 normline(0, smallnegy, 0, 1+smallnegy),
203 normline(0, 1+smallnegy, 0, smallnegy),
204 normline(0, smallnegy, 0, smallposy)], closed=0)
205 p1.epsilon = p2.epsilon = 0.05
206 intersect = p2.intersect(p1)
207 self.failUnlessEqual(len(intersect[0]), 2)
208 self.failUnlessAlmostEqual(intersect[0][0], 0.5)
209 self.failUnlessAlmostEqual(intersect[0][1], 2.99)
211 smallposy = 0.01
212 smallnegy = -0.01
213 p1 = normsubpath([normline(-1, 0, 1, 0)])
214 p2 = normsubpath([normline(0, smallposy, 0, smallnegy),
215 normline(0, smallnegy, 0, 1+smallnegy),
216 normline(0, 1+smallnegy, 0, smallnegy),
217 normline(0, smallnegy, 0, smallposy)], closed=1)
218 p1.epsilon = p2.epsilon = 0.05
219 intersect = p2.intersect(p1)
220 self.failUnlessEqual(len(intersect[0]), 1)
221 self.failUnlessAlmostEqual(intersect[0][0], 0.5)
223 smallposy = 0.1
224 smallnegy = -0.1
225 p1 = normsubpath([normline(-1, 0, 1, 0)])
226 p2 = normsubpath([normline(0, smallposy, 0, smallnegy),
227 normline(0, smallnegy, 0, 1+smallnegy),
228 normline(0, 1+smallnegy, 0, smallnegy),
229 normline(0, smallnegy, 0, smallposy)], closed=0)
230 p1.epsilon = p2.epsilon = 0.05
231 intersect = p2.intersect(p1)
232 self.failUnlessEqual(len(intersect[0]), 4)
233 self.failUnlessAlmostEqual(intersect[0][0], 0.5)
234 self.failUnlessAlmostEqual(intersect[0][1], 1.1)
235 self.failUnlessAlmostEqual(intersect[0][2], 2.9)
236 self.failUnlessAlmostEqual(intersect[0][3], 3.5)
239 if __name__ == "__main__":
240 unittest.main()