prevent double call of _cleanup, which harms usefiles (and is a bad idea in general)
[PyX.git] / test / unit / test_trafo.py
blobf3c5bf60593ded2cbe71d98d97f55c9b36be017b
1 import sys
2 if sys.path[0] != "../..":
3 sys.path.insert(0, "../..")
5 import unittest
7 from pyx import *
9 def isEqual(trafo1, trafo2):
10 return max(list(map(abs,[trafo1.matrix[0][0]-trafo2.matrix[0][0],
11 trafo1.matrix[0][1]-trafo2.matrix[0][1],
12 trafo1.matrix[1][0]-trafo2.matrix[1][0],
13 trafo1.matrix[1][1]-trafo2.matrix[1][1],
14 trafo1.vector[0]-trafo2.vector[0],
15 trafo1.vector[1]-trafo2.vector[1]])))<1e-7
17 def correctOnBasis(t, tesx, tesy):
18 esx = t.apply(1 * unit.t_cm, 0)
19 esy = t.apply(0, 1 * unit.t_cm)
21 esx = unit.tocm(esx[0]), unit.tocm(esx[1])
22 esy = unit.tocm(esy[0]), unit.tocm(esy[1])
24 # print " (1,0) => (%f, %f)" % (esx[0], esx[1])
25 # print " (0,1) => (%f, %f)" % (esy[0], esy[1])
27 return max(list(map(abs,[esx[0]-tesx[0], esx[1]-tesx[1],
28 esy[0]-tesy[0], esy[1]-tesy[1]])))<1e-7
31 class TrafoTestCase(unittest.TestCase):
33 def testInverse(self):
34 "t*t.inverse()=1"
35 t = trafo.translate(-1,-1)*trafo.rotate(72)*trafo.translate(1,1)
36 assert isEqual(t*t.inverse(), trafo.trafo()), \
37 "wrong inverse definition"
39 def testTranslate(self):
40 assert correctOnBasis(trafo.translate(1,0),
41 (2,0), (1,1)), \
42 "wrong definition of trafo.translation"
43 assert correctOnBasis(trafo.translate(0,1),
44 (1,1), (0,2)), \
45 "wrong definition of trafo.translation"
47 def testRotate(self):
48 assert correctOnBasis(trafo.rotate(90),
49 (0, 1), (-1, 0)), \
50 "wrong definition of trafo.rotation"
51 assert isEqual(trafo.rotate(360), trafo.trafo()), \
52 "trafo.rotation by 360 deg is not 1"
53 assert isEqual(trafo.rotate(40)*trafo.rotate(120)*
54 trafo.rotate(90)*trafo.rotate(110),
55 trafo.trafo()), \
56 "successive multiplication by 360 degrees does not yield 1"
58 def testMirror(self):
59 "trafo.mirroring two times must yield 1 and -mirror(phi)=mirror(phi+180)"
60 assert isEqual(trafo.mirror(20)*trafo.mirror(20),
61 trafo.trafo()), \
62 "trafo.mirroring not idempotent"
63 assert isEqual(trafo.mirror(20),
64 trafo.mirror(180+20)), \
65 "trafo.mirroring by 20 degrees unequal to trafo.mirroring by 180+20 degrees"
67 def testScale(self):
68 assert correctOnBasis(trafo.scale(0.5),
69 (0.5,0), (0, 0.5)), \
70 "wrong definition of trafo.scaling"
71 assert correctOnBasis(trafo.scale(0.5, 0.2),
72 (0.5,0), (0, 0.2)), \
73 "wrong definition of trafo.scaling"
74 assert isEqual(trafo.scale(2,3)*trafo.scale(1/2.0, 1/3.0),
75 trafo.trafo()), \
76 "trafo.scaling definition wrong"
78 def testSlant(self):
79 assert correctOnBasis(trafo.slant(0.5),
80 (1,0), (0.5, 1)), \
81 "wrong definition of trafo.slant"
82 assert isEqual(trafo.slant(2)*trafo.slant(-2),
83 trafo.trafo()), \
84 "trafo.slant definition wrong"
86 def testMultVsMethods(self):
87 "test multiplication vs trafo methods"
88 assert isEqual(trafo.rotate(72).translated(1,2),
89 trafo.translate(1,2)*trafo.rotate(72)), \
90 "trafo.translate not consistent with multiplication result"
91 assert isEqual(trafo.mirror(20)*trafo.mirror(20),
92 trafo.mirror(20).mirrored(20)), \
93 "trafo.mirror not consistent with multiplication result"
94 assert isEqual(trafo.translate(1,2).rotated(72).translated(-3,-1),
95 trafo.translate(-3,-1)*
96 trafo.rotate(72)*
97 trafo.translate(1,2)), \
98 "trafo.translate/rotate not consistent with multiplication result"
100 def testTranslateCombined(self):
101 assert correctOnBasis(trafo.translate(1,0)*trafo.rotate(90),
102 (1,1), (0,0)), \
103 "wrong trafo.translation/trafo.rotation definition"
104 assert correctOnBasis(trafo.rotate(90)*trafo.translate(1,0),
105 (0,2), (-1,1)), \
106 "wrong trafo.translation/trafo.rotation definition"
107 assert isEqual(trafo.rotate(72,1,2),
108 trafo.translate(-1,-2).rotated(72).translated(1,2)), \
109 "wrong translate/rotate definition"
111 assert correctOnBasis(trafo.translate(1,0)*
112 trafo.scale(0.5),
113 (1.5, 0), (1.0,0.5)), \
114 "wrong trafo.translation/trafo.scaling definition"
115 assert correctOnBasis(trafo.scale(0.5)*
116 trafo.translate(1,0),
117 (1, 0), (0.5,0.5)), \
118 "wrong trafo.translation/trafo.scaling definition"
120 assert correctOnBasis(trafo.translate(1,0)*
121 trafo.rotate(90)*
122 trafo.scale(0.5),
123 (1,0.5), (0.5,0)), \
124 "wrong trafo.translation/trafo.rotation/trafo.scaling definition"
125 assert correctOnBasis(trafo.translate(1,0)*
126 trafo.scale(0.5)*
127 trafo.rotate(90),
128 (1,0.5), (0.5,0)), \
129 "wrong trafo.translation/trafo.rotation/trafo.scaling definition"
130 assert correctOnBasis(trafo.rotate(90)*trafo.scale(0.5)*trafo.translate(1,0),
131 (0,1), (-0.5,0.5)), \
132 "wrong trafo.translation/trafo.rotation/trafo.scaling definition"
135 if __name__ == "__main__":
136 unittest.main()