annotate test/math_test.py @ 3172:d1b367fc08bc tests-returncode

Use a return code of 1 when the tests fail.
author Thomas Kluyver <takowl@gmail.com>
date Wed, 09 Jan 2013 10:13:57 +0000
parents 28685c84e251
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2024
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
1 # -*- coding: utf-8 -*-
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
2
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
3 if __name__ == '__main__':
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
4 import sys
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
5 import os
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
6 pkg_dir = os.path.split(os.path.abspath(__file__))[0]
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
7 parent_dir, pkg_name = os.path.split(pkg_dir)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
8 is_pygame_pkg = (pkg_name == 'tests' and
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
9 os.path.split(parent_dir)[1] == 'pygame')
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
10 if not is_pygame_pkg:
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
11 sys.path.insert(0, parent_dir)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
12 else:
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
13 is_pygame_pkg = __name__.startswith('pygame.tests.')
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
14
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
15 if is_pygame_pkg:
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
16 from pygame.tests.test_utils import test_not_implemented, unittest
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
17 else:
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
18 from test.test_utils import test_not_implemented, unittest
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
19
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
20 import math
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
21 import pygame.math
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
22 from pygame.math import Vector2, Vector3
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
23 from time import clock
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
24 from random import random
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
25 import gc
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
26
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
27 class Vector2TypeTest(unittest.TestCase):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
28 def setUp(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
29 # gc.collect()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
30 self.zeroVec = Vector2()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
31 self.e1 = Vector2(1, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
32 self.e2 = Vector2(0, 1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
33 # self.t1 = (random(), random())
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
34 self.t1 = (1.2, 3.4)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
35 self.l1 = list(self.t1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
36 self.v1 = Vector2(self.t1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
37 # self.t2 = (random(), random())
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
38 self.t2 = (5.6, 7.8)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
39 self.l2 = list(self.t2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
40 self.v2 = Vector2(self.t2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
41 # self.s1 = random()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
42 # self.s2 = random()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
43 self.s1 = 5.6
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
44 self.s2 = 7.8
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
45 def tearDown(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
46 pygame.math.disable_swizzling()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
47
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
48
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
49 def testConstructionDefault(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
50 v = Vector2()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
51 self.assertEqual(v.x, 0.)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
52 self.assertEqual(v.y, 0.)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
53
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
54 def testConstructionXY(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
55 v = Vector2(1.2, 3.4)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
56 self.assertEqual(v.x, 1.2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
57 self.assertEqual(v.y, 3.4)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
58
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
59 def testConstructionTuple(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
60 v = Vector2((1.2, 3.4))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
61 self.assertEqual(v.x, 1.2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
62 self.assertEqual(v.y, 3.4)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
63
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
64 def testConstructionList(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
65 v = Vector2([1.2, 3.4])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
66 self.assertEqual(v.x, 1.2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
67 self.assertEqual(v.y, 3.4)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
68
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
69 def testConstructionVector2(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
70 v = Vector2(Vector2(1.2, 3.4))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
71 self.assertEqual(v.x, 1.2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
72 self.assertEqual(v.y, 3.4)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
73
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
74 def testAttributAccess(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
75 tmp = self.v1.x
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
76 self.assertEqual(tmp, self.v1.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
77 self.assertEqual(tmp, self.v1[0])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
78 tmp = self.v1.y
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
79 self.assertEqual(tmp, self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
80 self.assertEqual(tmp, self.v1[1])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
81 self.v1.x = 3.141
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
82 self.assertEqual(self.v1.x, 3.141)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
83 self.v1.y = 3.141
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
84 self.assertEqual(self.v1.y, 3.141)
2024
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
85 def assign_nonfloat():
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
86 v = Vector2()
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
87 v.x = "spam"
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
88 self.assertRaises(TypeError, assign_nonfloat)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
89
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
90
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
91 def testSequence(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
92 v = Vector2(1.2, 3.4)
2007
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
93 Vector2()[:]
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
94 self.assertEqual(len(v), 2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
95 self.assertEqual(v[0], 1.2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
96 self.assertEqual(v[1], 3.4)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
97 self.assertRaises(IndexError, lambda : v[2])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
98 self.assertEqual(v[-1], 3.4)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
99 self.assertEqual(v[-2], 1.2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
100 self.assertRaises(IndexError, lambda : v[-3])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
101 self.assertEqual(v[:], [1.2, 3.4])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
102 self.assertEqual(v[1:], [3.4])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
103 self.assertEqual(v[:1], [1.2])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
104 self.assertEqual(list(v), [1.2, 3.4])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
105 self.assertEqual(tuple(v), (1.2, 3.4))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
106 v[0] = 5.6
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
107 v[1] = 7.8
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
108 self.assertEqual(v.x, 5.6)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
109 self.assertEqual(v.y, 7.8)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
110 v[:] = [9.1, 11.12]
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
111 self.assertEqual(v.x, 9.1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
112 self.assertEqual(v.y, 11.12)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
113 def overpopulate():
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
114 v = Vector2()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
115 v[:] = [1, 2, 3]
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
116 self.assertRaises(ValueError, overpopulate)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
117 def underpopulate():
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
118 v = Vector2()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
119 v[:] = [1]
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
120 self.assertRaises(ValueError, underpopulate)
2024
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
121 def assign_nonfloat():
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
122 v = Vector2()
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
123 v[0] = "spam"
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
124 self.assertRaises(TypeError, assign_nonfloat)
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
125
2007
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
126 def testExtendedSlicing(self):
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
127 # deletion
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
128 def delSlice(vec, start=None, stop=None, step=None):
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
129 if start is not None and stop is not None and step is not None:
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
130 del vec[start:stop:step]
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
131 elif start is not None and stop is None and step is not None:
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
132 del vec[start::step]
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
133 elif start is None and stop is None and step is not None:
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
134 del vec[::step]
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
135 v = Vector2(self.v1)
2024
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
136 self.assertRaises(TypeError, delSlice, v, None, None, 2)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
137 self.assertRaises(TypeError, delSlice, v, 1, None, 2)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
138 self.assertRaises(TypeError, delSlice, v, 1, 2, 1)
2007
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
139
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
140 # assignment
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
141 v = Vector2(self.v1)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
142 v[::2] = [-1]
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
143 self.assertEqual(v, [-1, self.v1.y])
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
144 v = Vector2(self.v1)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
145 v[::-2] = [10]
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
146 self.assertEqual(v, [self.v1.x, 10])
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
147 v = Vector2(self.v1)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
148 v[::-1] = v
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
149 self.assertEqual(v, [self.v1.y, self.v1.x])
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
150 a = Vector2(self.v1)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
151 b = Vector2(self.v1)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
152 c = Vector2(self.v1)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
153 a[1:2] = [2.2]
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
154 b[slice(1,2)] = [2.2]
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
155 c[1:2:] = (2.2,)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
156 self.assertEqual(a, b)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
157 self.assertEqual(a, c)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
158 self.assertEqual(type(a), type(self.v1))
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
159 self.assertEqual(type(b), type(self.v1))
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
160 self.assertEqual(type(c), type(self.v1))
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
161
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
162 def testAdd(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
163 v3 = self.v1 + self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
164 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
165 self.assertEqual(v3.x, self.v1.x + self.v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
166 self.assertEqual(v3.y, self.v1.y + self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
167 v3 = self.v1 + self.t2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
168 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
169 self.assertEqual(v3.x, self.v1.x + self.t2[0])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
170 self.assertEqual(v3.y, self.v1.y + self.t2[1])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
171 v3 = self.v1 + self.l2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
172 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
173 self.assertEqual(v3.x, self.v1.x + self.l2[0])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
174 self.assertEqual(v3.y, self.v1.y + self.l2[1])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
175 v3 = self.t1 + self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
176 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
177 self.assertEqual(v3.x, self.t1[0] + self.v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
178 self.assertEqual(v3.y, self.t1[1] + self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
179 v3 = self.l1 + self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
180 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
181 self.assertEqual(v3.x, self.l1[0] + self.v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
182 self.assertEqual(v3.y, self.l1[1] + self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
183
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
184 def testSub(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
185 v3 = self.v1 - self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
186 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
187 self.assertEqual(v3.x, self.v1.x - self.v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
188 self.assertEqual(v3.y, self.v1.y - self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
189 v3 = self.v1 - self.t2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
190 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
191 self.assertEqual(v3.x, self.v1.x - self.t2[0])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
192 self.assertEqual(v3.y, self.v1.y - self.t2[1])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
193 v3 = self.v1 - self.l2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
194 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
195 self.assertEqual(v3.x, self.v1.x - self.l2[0])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
196 self.assertEqual(v3.y, self.v1.y - self.l2[1])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
197 v3 = self.t1 - self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
198 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
199 self.assertEqual(v3.x, self.t1[0] - self.v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
200 self.assertEqual(v3.y, self.t1[1] - self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
201 v3 = self.l1 - self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
202 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
203 self.assertEqual(v3.x, self.l1[0] - self.v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
204 self.assertEqual(v3.y, self.l1[1] - self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
205
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
206 def testScalarMultiplication(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
207 v = self.s1 * self.v1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
208 self.assert_(isinstance(v, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
209 self.assertEqual(v.x, self.s1 * self.v1.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
210 self.assertEqual(v.y, self.s1 * self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
211 v = self.v1 * self.s2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
212 self.assertEqual(v.x, self.v1.x * self.s2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
213 self.assertEqual(v.y, self.v1.y * self.s2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
214
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
215 def testScalarDivision(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
216 v = self.v1 / self.s1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
217 self.assert_(isinstance(v, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
218 self.assertAlmostEqual(v.x, self.v1.x / self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
219 self.assertAlmostEqual(v.y, self.v1.y / self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
220 v = self.v1 // self.s2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
221 self.assert_(isinstance(v, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
222 self.assertEqual(v.x, self.v1.x // self.s2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
223 self.assertEqual(v.y, self.v1.y // self.s2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
224
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
225 def testBool(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
226 self.assertEqual(bool(self.zeroVec), False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
227 self.assertEqual(bool(self.v1), True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
228 self.assert_(not self.zeroVec)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
229 self.assert_(self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
230
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
231 def testUnary(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
232 v = +self.v1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
233 self.assert_(isinstance(v, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
234 self.assertEqual(v.x, self.v1.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
235 self.assertEqual(v.y, self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
236 self.assertNotEqual(id(v), id(self.v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
237 v = -self.v1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
238 self.assert_(isinstance(v, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
239 self.assertEqual(v.x, -self.v1.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
240 self.assertEqual(v.y, -self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
241 self.assertNotEqual(id(v), id(self.v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
242
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
243 def testCompare(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
244 int_vec = Vector2(3, -2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
245 flt_vec = Vector2(3.0, -2.0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
246 zero_vec = Vector2(0, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
247 self.assertEqual(int_vec == flt_vec, True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
248 self.assertEqual(int_vec != flt_vec, False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
249 self.assertEqual(int_vec != zero_vec, True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
250 self.assertEqual(flt_vec == zero_vec, False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
251 self.assertEqual(int_vec == (3, -2), True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
252 self.assertEqual(int_vec != (3, -2), False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
253 self.assertEqual(int_vec != [0, 0], True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
254 self.assertEqual(int_vec == [0, 0], False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
255 self.assertEqual(int_vec != 5, True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
256 self.assertEqual(int_vec == 5, False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
257 self.assertEqual(int_vec != [3, -2, 0], True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
258 self.assertEqual(int_vec == [3, -2, 0], False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
259
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
260 def testStr(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
261 v = Vector2(1.2, 3.4)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
262 self.assertEqual(str(v), "[1.2, 3.4]")
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
263
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
264 def testRepr(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
265 v = Vector2(1.2, 3.4)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
266 self.assertEqual(v.__repr__(), "<Vector2(1.2, 3.4)>")
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
267 self.assertEqual(v, Vector2(v.__repr__()))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
268
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
269 def testIter(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
270 it = self.v1.__iter__()
2007
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
271 # support py2.x and 3.x
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
272 if hasattr(it, "next"):
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
273 next_ = it.next
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
274 elif hasattr(it, "__next__"):
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
275 next_ = it.__next__
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
276 else:
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
277 self.fail("Iterator has neither a 'next' nor a '__next__' method.")
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
278 self.assertEqual(next_(), self.v1[0])
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
279 self.assertEqual(next_(), self.v1[1])
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
280 self.assertRaises(StopIteration, lambda : next_())
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
281 it1 = self.v1.__iter__()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
282 it2 = self.v1.__iter__()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
283 self.assertNotEqual(id(it1), id(it2))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
284 self.assertEqual(id(it1), id(it1.__iter__()))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
285 self.assertEqual(list(it1), list(it2));
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
286 self.assertEqual(list(self.v1.__iter__()), self.l1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
287 idx = 0
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
288 for val in self.v1:
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
289 self.assertEqual(val, self.v1[idx])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
290 idx += 1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
291
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
292 def test_rotate(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
293 v1 = Vector2(1, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
294 v2 = v1.rotate(90)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
295 v3 = v1.rotate(90 + 360)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
296 self.assertEqual(v1.x, 1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
297 self.assertEqual(v1.y, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
298 self.assertEqual(v2.x, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
299 self.assertEqual(v2.y, 1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
300 self.assertEqual(v3.x, v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
301 self.assertEqual(v3.y, v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
302 v1 = Vector2(-1, -1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
303 v2 = v1.rotate(-90)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
304 self.assertEqual(v2.x, -1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
305 self.assertEqual(v2.y, 1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
306 v2 = v1.rotate(360)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
307 self.assertEqual(v1.x, v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
308 self.assertEqual(v1.y, v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
309 v2 = v1.rotate(0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
310 self.assertEqual(v1.x, v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
311 self.assertEqual(v1.y, v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
312
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
313 def test_rotate_ip(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
314 v = Vector2(1, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
315 self.assertEqual(v.rotate_ip(90), None)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
316 self.assertEqual(v.x, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
317 self.assertEqual(v.y, 1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
318 v = Vector2(-1, -1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
319 v.rotate_ip(-90)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
320 self.assertEqual(v.x, -1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
321 self.assertEqual(v.y, 1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
322
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
323 def test_normalize(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
324 v = self.v1.normalize()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
325 # length is 1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
326 self.assertAlmostEqual(v.x * v.x + v.y * v.y, 1.)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
327 # v1 is unchanged
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
328 self.assertEqual(self.v1.x, self.l1[0])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
329 self.assertEqual(self.v1.y, self.l1[1])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
330 # v2 is paralell to v1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
331 self.assertAlmostEqual(self.v1.x * v.y - self.v1.y * v.x, 0.)
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
332 self.assertRaises(ValueError, lambda : self.zeroVec.normalize())
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
333
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
334 def test_normalize_ip(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
335 v = +self.v1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
336 # v has length != 1 before normalizing
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
337 self.assertNotEqual(v.x * v.x + v.y * v.y, 1.)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
338 # inplace operations should return None
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
339 self.assertEqual(v.normalize_ip(), None)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
340 # length is 1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
341 self.assertAlmostEqual(v.x * v.x + v.y * v.y, 1.)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
342 # v2 is paralell to v1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
343 self.assertAlmostEqual(self.v1.x * v.y - self.v1.y * v.x, 0.)
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
344 self.assertRaises(ValueError, lambda : self.zeroVec.normalize_ip())
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
345
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
346 def test_is_normalized(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
347 self.assertEqual(self.v1.is_normalized(), False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
348 v = self.v1.normalize()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
349 self.assertEqual(v.is_normalized(), True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
350 self.assertEqual(self.e2.is_normalized(), True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
351 self.assertEqual(self.zeroVec.is_normalized(), False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
352
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
353 def test_cross(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
354 self.assertEqual(self.v1.cross(self.v2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
355 self.v1.x * self.v2.y - self.v1.y * self.v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
356 self.assertEqual(self.v1.cross(self.l2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
357 self.v1.x * self.l2[1] - self.v1.y * self.l2[0])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
358 self.assertEqual(self.v1.cross(self.t2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
359 self.v1.x * self.t2[1] - self.v1.y * self.t2[0])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
360 self.assertEqual(self.v1.cross(self.v2), -self.v2.cross(self.v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
361 self.assertEqual(self.v1.cross(self.v1), 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
362
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
363 def test_dot(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
364 self.assertEqual(self.v1.dot(self.v2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
365 self.v1.x * self.v2.x + self.v1.y * self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
366 self.assertEqual(self.v1.dot(self.l2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
367 self.v1.x * self.l2[0] + self.v1.y * self.l2[1])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
368 self.assertEqual(self.v1.dot(self.t2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
369 self.v1.x * self.t2[0] + self.v1.y * self.t2[1])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
370 self.assertEqual(self.v1.dot(self.v2), self.v2.dot(self.v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
371 self.assertEqual(self.v1.dot(self.v2), self.v1 * self.v2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
372
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
373 def test_angle_to(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
374 self.assertEqual(self.v1.rotate(self.v1.angle_to(self.v2)).normalize(),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
375 self.v2.normalize())
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
376 self.assertEqual(Vector2(1, 1).angle_to((-1, 1)), 90)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
377 self.assertEqual(Vector2(1, 0).angle_to((0, -1)), -90)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
378 self.assertEqual(Vector2(1, 0).angle_to((-1, 1)), 135)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
379 self.assertEqual(abs(Vector2(1, 0).angle_to((-1, 0))), 180)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
380
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
381 def test_scale_to_length(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
382 v = Vector2(1, 1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
383 v.scale_to_length(2.5)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
384 self.assertEqual(v, Vector2(2.5, 2.5) / math.sqrt(2))
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
385 self.assertRaises(ValueError, lambda : self.zeroVec.scale_to_length(1))
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
386 self.assertEqual(v.scale_to_length(0), None)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
387 self.assertEqual(v, self.zeroVec)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
388
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
389 def test_length(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
390 self.assertEqual(Vector2(3, 4).length(), 5)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
391 self.assertEqual(Vector2(-3, 4).length(), 5)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
392 self.assertEqual(self.zeroVec.length(), 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
393
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
394 def test_length_squared(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
395 self.assertEqual(Vector2(3, 4).length_squared(), 25)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
396 self.assertEqual(Vector2(-3, 4).length_squared(), 25)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
397 self.assertEqual(self.zeroVec.length_squared(), 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
398
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
399 def test_reflect(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
400 v = Vector2(1, -1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
401 n = Vector2(0, 1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
402 self.assertEqual(v.reflect(n), Vector2(1, 1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
403 self.assertEqual(v.reflect(3*n), v.reflect(n))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
404 self.assertEqual(v.reflect(-v), -v)
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
405 self.assertRaises(ValueError, lambda : v.reflect(self.zeroVec))
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
406
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
407 def test_reflect_ip(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
408 v1 = Vector2(1, -1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
409 v2 = Vector2(v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
410 n = Vector2(0, 1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
411 self.assertEqual(v2.reflect_ip(n), None)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
412 self.assertEqual(v2, Vector2(1, 1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
413 v2 = Vector2(v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
414 v2.reflect_ip(3*n)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
415 self.assertEqual(v2, v1.reflect(n))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
416 v2 = Vector2(v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
417 v2.reflect_ip(-v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
418 self.assertEqual(v2, -v1)
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
419 self.assertRaises(ValueError, lambda : v2.reflect_ip(Vector2()))
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
420
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
421 def test_distance_to(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
422 diff = self.v1 - self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
423 self.assertEqual(self.e1.distance_to(self.e2), math.sqrt(2))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
424 self.assertEqual(self.v1.distance_to(self.v2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
425 math.sqrt(diff.x * diff.x + diff.y * diff.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
426 self.assertEqual(self.v1.distance_to(self.v1), 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
427 self.assertEqual(self.v1.distance_to(self.v2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
428 self.v2.distance_to(self.v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
429
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
430 def test_distance_squared_to(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
431 diff = self.v1 - self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
432 self.assertEqual(self.e1.distance_squared_to(self.e2), 2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
433 self.assertEqual(self.v1.distance_squared_to(self.v2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
434 diff.x * diff.x + diff.y * diff.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
435 self.assertEqual(self.v1.distance_squared_to(self.v1), 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
436 self.assertEqual(self.v1.distance_squared_to(self.v2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
437 self.v2.distance_squared_to(self.v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
438
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
439 def testSwizzle(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
440 self.assertEquals(hasattr(pygame.math, "enable_swizzling"), True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
441 self.assertEquals(hasattr(pygame.math, "disable_swizzling"), True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
442 # swizzling disabled by default
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
443 self.assertRaises(AttributeError, lambda : self.v1.yx)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
444 pygame.math.enable_swizzling()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
445
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
446 self.assertEqual(self.v1.yx, (self.v1.y, self.v1.x))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
447 self.assertEqual(self.v1.xxyyxy, (self.v1.x, self.v1.x, self.v1.y,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
448 self.v1.y, self.v1.x, self.v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
449 self.v1.xy = self.t2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
450 self.assertEqual(self.v1, self.t2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
451 self.v1.yx = self.t2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
452 self.assertEqual(self.v1, (self.t2[1], self.t2[0]))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
453 self.assertEqual(type(self.v1), Vector2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
454 def invalidSwizzleX():
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
455 Vector2().xx = (1, 2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
456 def invalidSwizzleY():
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
457 Vector2().yy = (1, 2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
458 self.assertRaises(AttributeError, invalidSwizzleX)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
459 self.assertRaises(AttributeError, invalidSwizzleY)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
460 def invalidAssignment():
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
461 Vector2().xy = 3
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
462 self.assertRaises(TypeError, invalidAssignment)
2024
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
463 def unicodeAttribute():
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
464 getattr(Vector2(), "ä")
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
465 self.assertRaises(AttributeError, unicodeAttribute)
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
466
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
467 def test_elementwise(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
468 # behaviour for "elementwise op scalar"
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
469 self.assertEqual(self.v1.elementwise() + self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
470 (self.v1.x + self.s1, self.v1.y + self.s1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
471 self.assertEqual(self.v1.elementwise() - self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
472 (self.v1.x - self.s1, self.v1.y - self.s1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
473 self.assertEqual(self.v1.elementwise() * self.s2,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
474 (self.v1.x * self.s2, self.v1.y * self.s2))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
475 self.assertEqual(self.v1.elementwise() / self.s2,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
476 (self.v1.x / self.s2, self.v1.y / self.s2))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
477 self.assertEqual(self.v1.elementwise() // self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
478 (self.v1.x // self.s1, self.v1.y // self.s1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
479 self.assertEqual(self.v1.elementwise() ** self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
480 (self.v1.x ** self.s1, self.v1.y ** self.s1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
481 self.assertEqual(self.v1.elementwise() % self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
482 (self.v1.x % self.s1, self.v1.y % self.s1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
483 self.assertEqual(self.v1.elementwise() > self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
484 self.v1.x > self.s1 and self.v1.y > self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
485 self.assertEqual(self.v1.elementwise() < self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
486 self.v1.x < self.s1 and self.v1.y < self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
487 self.assertEqual(self.v1.elementwise() == self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
488 self.v1.x == self.s1 and self.v1.y == self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
489 self.assertEqual(self.v1.elementwise() != self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
490 self.v1.x != self.s1 and self.v1.y != self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
491 self.assertEqual(self.v1.elementwise() >= self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
492 self.v1.x >= self.s1 and self.v1.y >= self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
493 self.assertEqual(self.v1.elementwise() <= self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
494 self.v1.x <= self.s1 and self.v1.y <= self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
495 self.assertEqual(self.v1.elementwise() != self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
496 self.v1.x != self.s1 and self.v1.y != self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
497 # behaviour for "scalar op elementwise"
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
498 self.assertEqual(5 + self.v1.elementwise(), Vector2(5, 5) + self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
499 self.assertEqual(3.5 - self.v1.elementwise(), Vector2(3.5, 3.5) - self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
500 self.assertEqual(7.5 * self.v1.elementwise() , 7.5 * self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
501 self.assertEqual(-3.5 / self.v1.elementwise(), (-3.5 / self.v1.x, -3.5 / self.v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
502 self.assertEqual(-3.5 // self.v1.elementwise(), (-3.5 // self.v1.x, -3.5 // self.v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
503 self.assertEqual(-3.5 ** self.v1.elementwise(), (-3.5 ** self.v1.x, -3.5 ** self.v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
504 self.assertEqual(3 % self.v1.elementwise(), (3 % self.v1.x, 3 % self.v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
505 self.assertEqual(2 < self.v1.elementwise(), 2 < self.v1.x and 2 < self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
506 self.assertEqual(2 > self.v1.elementwise(), 2 > self.v1.x and 2 > self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
507 self.assertEqual(1 == self.v1.elementwise(), 1 == self.v1.x and 1 == self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
508 self.assertEqual(1 != self.v1.elementwise(), 1 != self.v1.x and 1 != self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
509 self.assertEqual(2 <= self.v1.elementwise(), 2 <= self.v1.x and 2 <= self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
510 self.assertEqual(-7 >= self.v1.elementwise(), -7 >= self.v1.x and -7 >= self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
511 self.assertEqual(-7 != self.v1.elementwise(), -7 != self.v1.x and -7 != self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
512
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
513 # behaviour for "elementwise op vector"
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
514 self.assertEqual(type(self.v1.elementwise() * self.v2), type(self.v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
515 self.assertEqual(self.v1.elementwise() + self.v2, self.v1 + self.v2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
516 self.assertEqual(self.v1.elementwise() + self.v2, self.v1 + self.v2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
517 self.assertEqual(self.v1.elementwise() - self.v2, self.v1 - self.v2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
518 self.assertEqual(self.v1.elementwise() * self.v2, (self.v1.x * self.v2.x, self.v1.y * self.v2.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
519 self.assertEqual(self.v1.elementwise() / self.v2, (self.v1.x / self.v2.x, self.v1.y / self.v2.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
520 self.assertEqual(self.v1.elementwise() // self.v2, (self.v1.x // self.v2.x, self.v1.y // self.v2.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
521 self.assertEqual(self.v1.elementwise() ** self.v2, (self.v1.x ** self.v2.x, self.v1.y ** self.v2.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
522 self.assertEqual(self.v1.elementwise() % self.v2, (self.v1.x % self.v2.x, self.v1.y % self.v2.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
523 self.assertEqual(self.v1.elementwise() > self.v2, self.v1.x > self.v2.x and self.v1.y > self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
524 self.assertEqual(self.v1.elementwise() < self.v2, self.v1.x < self.v2.x and self.v1.y < self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
525 self.assertEqual(self.v1.elementwise() >= self.v2, self.v1.x >= self.v2.x and self.v1.y >= self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
526 self.assertEqual(self.v1.elementwise() <= self.v2, self.v1.x <= self.v2.x and self.v1.y <= self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
527 self.assertEqual(self.v1.elementwise() == self.v2, self.v1.x == self.v2.x and self.v1.y == self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
528 self.assertEqual(self.v1.elementwise() != self.v2, self.v1.x != self.v2.x and self.v1.y != self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
529 # behaviour for "vector op elementwise"
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
530 self.assertEqual(self.v2 + self.v1.elementwise(), self.v2 + self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
531 self.assertEqual(self.v2 - self.v1.elementwise(), self.v2 - self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
532 self.assertEqual(self.v2 * self.v1.elementwise(), (self.v2.x * self.v1.x, self.v2.y * self.v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
533 self.assertEqual(self.v2 / self.v1.elementwise(), (self.v2.x / self.v1.x, self.v2.y / self.v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
534 self.assertEqual(self.v2 // self.v1.elementwise(), (self.v2.x // self.v1.x, self.v2.y // self.v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
535 self.assertEqual(self.v2 ** self.v1.elementwise(), (self.v2.x ** self.v1.x, self.v2.y ** self.v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
536 self.assertEqual(self.v2 % self.v1.elementwise(), (self.v2.x % self.v1.x, self.v2.y % self.v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
537 self.assertEqual(self.v2 < self.v1.elementwise(), self.v2.x < self.v1.x and self.v2.y < self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
538 self.assertEqual(self.v2 > self.v1.elementwise(), self.v2.x > self.v1.x and self.v2.y > self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
539 self.assertEqual(self.v2 <= self.v1.elementwise(), self.v2.x <= self.v1.x and self.v2.y <= self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
540 self.assertEqual(self.v2 >= self.v1.elementwise(), self.v2.x >= self.v1.x and self.v2.y >= self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
541 self.assertEqual(self.v2 == self.v1.elementwise(), self.v2.x == self.v1.x and self.v2.y == self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
542 self.assertEqual(self.v2 != self.v1.elementwise(), self.v2.x != self.v1.x and self.v2.y != self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
543
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
544 # behaviour for "elementwise op elementwise"
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
545 self.assertEqual(self.v2.elementwise() + self.v1.elementwise(), self.v2 + self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
546 self.assertEqual(self.v2.elementwise() - self.v1.elementwise(), self.v2 - self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
547 self.assertEqual(self.v2.elementwise() * self.v1.elementwise(), (self.v2.x * self.v1.x, self.v2.y * self.v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
548 self.assertEqual(self.v2.elementwise() / self.v1.elementwise(), (self.v2.x / self.v1.x, self.v2.y / self.v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
549 self.assertEqual(self.v2.elementwise() // self.v1.elementwise(), (self.v2.x // self.v1.x, self.v2.y // self.v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
550 self.assertEqual(self.v2.elementwise() ** self.v1.elementwise(), (self.v2.x ** self.v1.x, self.v2.y ** self.v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
551 self.assertEqual(self.v2.elementwise() % self.v1.elementwise(), (self.v2.x % self.v1.x, self.v2.y % self.v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
552 self.assertEqual(self.v2.elementwise() < self.v1.elementwise(), self.v2.x < self.v1.x and self.v2.y < self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
553 self.assertEqual(self.v2.elementwise() > self.v1.elementwise(), self.v2.x > self.v1.x and self.v2.y > self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
554 self.assertEqual(self.v2.elementwise() <= self.v1.elementwise(), self.v2.x <= self.v1.x and self.v2.y <= self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
555 self.assertEqual(self.v2.elementwise() >= self.v1.elementwise(), self.v2.x >= self.v1.x and self.v2.y >= self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
556 self.assertEqual(self.v2.elementwise() == self.v1.elementwise(), self.v2.x == self.v1.x and self.v2.y == self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
557 self.assertEqual(self.v2.elementwise() != self.v1.elementwise(), self.v2.x != self.v1.x and self.v2.y != self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
558
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
559 # other behaviour
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
560 self.assertEqual(abs(self.v1.elementwise()), (abs(self.v1.x), abs(self.v1.y)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
561 self.assertEqual(-self.v1.elementwise(), -self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
562 self.assertEqual(+self.v1.elementwise(), +self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
563 self.assertEqual(bool(self.v1.elementwise()), bool(self.v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
564 self.assertEqual(bool(Vector2().elementwise()), bool(Vector2()))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
565 self.assertEqual(self.zeroVec.elementwise() ** 0, (1, 1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
566 self.assertRaises(ValueError, lambda : pow(Vector2(-1, 0).elementwise(), 1.2))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
567 self.assertRaises(ZeroDivisionError, lambda : self.zeroVec.elementwise() ** -1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
568
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
569 def test_elementwise(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
570 v1 = self.v1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
571 v2 = self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
572 s1 = self.s1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
573 s2 = self.s2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
574 # behaviour for "elementwise op scalar"
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
575 self.assertEqual(v1.elementwise() + s1, (v1.x + s1, v1.y + s1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
576 self.assertEqual(v1.elementwise() - s1, (v1.x - s1, v1.y - s1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
577 self.assertEqual(v1.elementwise() * s2, (v1.x * s2, v1.y * s2))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
578 self.assertEqual(v1.elementwise() / s2, (v1.x / s2, v1.y / s2))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
579 self.assertEqual(v1.elementwise() // s1, (v1.x // s1, v1.y // s1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
580 self.assertEqual(v1.elementwise() ** s1, (v1.x ** s1, v1.y ** s1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
581 self.assertEqual(v1.elementwise() % s1, (v1.x % s1, v1.y % s1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
582 self.assertEqual(v1.elementwise() > s1, v1.x > s1 and v1.y > s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
583 self.assertEqual(v1.elementwise() < s1, v1.x < s1 and v1.y < s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
584 self.assertEqual(v1.elementwise() == s1, v1.x == s1 and v1.y == s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
585 self.assertEqual(v1.elementwise() != s1, v1.x != s1 and v1.y != s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
586 self.assertEqual(v1.elementwise() >= s1, v1.x >= s1 and v1.y >= s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
587 self.assertEqual(v1.elementwise() <= s1, v1.x <= s1 and v1.y <= s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
588 self.assertEqual(v1.elementwise() != s1, v1.x != s1 and v1.y != s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
589 # behaviour for "scalar op elementwise"
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
590 self.assertEqual(s1 + v1.elementwise(), (s1 + v1.x, s1 + v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
591 self.assertEqual(s1 - v1.elementwise(), (s1 - v1.x, s1 - v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
592 self.assertEqual(s1 * v1.elementwise(), (s1 * v1.x, s1 * v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
593 self.assertEqual(s1 / v1.elementwise(), (s1 / v1.x, s1 / v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
594 self.assertEqual(s1 // v1.elementwise(), (s1 // v1.x, s1 // v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
595 self.assertEqual(s1 ** v1.elementwise(), (s1 ** v1.x, s1 ** v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
596 self.assertEqual(s1 % v1.elementwise(), (s1 % v1.x, s1 % v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
597 self.assertEqual(s1 < v1.elementwise(), s1 < v1.x and s1 < v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
598 self.assertEqual(s1 > v1.elementwise(), s1 > v1.x and s1 > v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
599 self.assertEqual(s1 == v1.elementwise(), s1 == v1.x and s1 == v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
600 self.assertEqual(s1 != v1.elementwise(), s1 != v1.x and s1 != v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
601 self.assertEqual(s1 <= v1.elementwise(), s1 <= v1.x and s1 <= v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
602 self.assertEqual(s1 >= v1.elementwise(), s1 >= v1.x and s1 >= v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
603 self.assertEqual(s1 != v1.elementwise(), s1 != v1.x and s1 != v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
604
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
605 # behaviour for "elementwise op vector"
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
606 self.assertEqual(type(v1.elementwise() * v2), type(v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
607 self.assertEqual(v1.elementwise() + v2, v1 + v2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
608 self.assertEqual(v1.elementwise() - v2, v1 - v2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
609 self.assertEqual(v1.elementwise() * v2, (v1.x * v2.x, v1.y * v2.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
610 self.assertEqual(v1.elementwise() / v2, (v1.x / v2.x, v1.y / v2.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
611 self.assertEqual(v1.elementwise() // v2, (v1.x // v2.x, v1.y // v2.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
612 self.assertEqual(v1.elementwise() ** v2, (v1.x ** v2.x, v1.y ** v2.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
613 self.assertEqual(v1.elementwise() % v2, (v1.x % v2.x, v1.y % v2.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
614 self.assertEqual(v1.elementwise() > v2, v1.x > v2.x and v1.y > v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
615 self.assertEqual(v1.elementwise() < v2, v1.x < v2.x and v1.y < v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
616 self.assertEqual(v1.elementwise() >= v2, v1.x >= v2.x and v1.y >= v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
617 self.assertEqual(v1.elementwise() <= v2, v1.x <= v2.x and v1.y <= v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
618 self.assertEqual(v1.elementwise() == v2, v1.x == v2.x and v1.y == v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
619 self.assertEqual(v1.elementwise() != v2, v1.x != v2.x and v1.y != v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
620 # behaviour for "vector op elementwise"
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
621 self.assertEqual(v2 + v1.elementwise(), v2 + v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
622 self.assertEqual(v2 - v1.elementwise(), v2 - v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
623 self.assertEqual(v2 * v1.elementwise(), (v2.x * v1.x, v2.y * v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
624 self.assertEqual(v2 / v1.elementwise(), (v2.x / v1.x, v2.y / v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
625 self.assertEqual(v2 // v1.elementwise(), (v2.x // v1.x, v2.y // v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
626 self.assertEqual(v2 ** v1.elementwise(), (v2.x ** v1.x, v2.y ** v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
627 self.assertEqual(v2 % v1.elementwise(), (v2.x % v1.x, v2.y % v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
628 self.assertEqual(v2 < v1.elementwise(), v2.x < v1.x and v2.y < v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
629 self.assertEqual(v2 > v1.elementwise(), v2.x > v1.x and v2.y > v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
630 self.assertEqual(v2 <= v1.elementwise(), v2.x <= v1.x and v2.y <= v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
631 self.assertEqual(v2 >= v1.elementwise(), v2.x >= v1.x and v2.y >= v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
632 self.assertEqual(v2 == v1.elementwise(), v2.x == v1.x and v2.y == v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
633 self.assertEqual(v2 != v1.elementwise(), v2.x != v1.x and v2.y != v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
634
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
635 # behaviour for "elementwise op elementwise"
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
636 self.assertEqual(v2.elementwise() + v1.elementwise(), v2 + v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
637 self.assertEqual(v2.elementwise() - v1.elementwise(), v2 - v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
638 self.assertEqual(v2.elementwise() * v1.elementwise(), (v2.x * v1.x, v2.y * v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
639 self.assertEqual(v2.elementwise() / v1.elementwise(), (v2.x / v1.x, v2.y / v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
640 self.assertEqual(v2.elementwise() // v1.elementwise(), (v2.x // v1.x, v2.y // v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
641 self.assertEqual(v2.elementwise() ** v1.elementwise(), (v2.x ** v1.x, v2.y ** v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
642 self.assertEqual(v2.elementwise() % v1.elementwise(), (v2.x % v1.x, v2.y % v1.y))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
643 self.assertEqual(v2.elementwise() < v1.elementwise(), v2.x < v1.x and v2.y < v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
644 self.assertEqual(v2.elementwise() > v1.elementwise(), v2.x > v1.x and v2.y > v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
645 self.assertEqual(v2.elementwise() <= v1.elementwise(), v2.x <= v1.x and v2.y <= v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
646 self.assertEqual(v2.elementwise() >= v1.elementwise(), v2.x >= v1.x and v2.y >= v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
647 self.assertEqual(v2.elementwise() == v1.elementwise(), v2.x == v1.x and v2.y == v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
648 self.assertEqual(v2.elementwise() != v1.elementwise(), v2.x != v1.x and v2.y != v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
649
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
650 # other behaviour
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
651 self.assertEqual(abs(v1.elementwise()), (abs(v1.x), abs(v1.y)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
652 self.assertEqual(-v1.elementwise(), -v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
653 self.assertEqual(+v1.elementwise(), +v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
654 self.assertEqual(bool(v1.elementwise()), bool(v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
655 self.assertEqual(bool(Vector2().elementwise()), bool(Vector2()))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
656 self.assertEqual(self.zeroVec.elementwise() ** 0, (1, 1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
657 self.assertRaises(ValueError, lambda : pow(Vector2(-1, 0).elementwise(), 1.2))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
658 self.assertRaises(ZeroDivisionError, lambda : self.zeroVec.elementwise() ** -1)
2024
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
659 self.assertRaises(ZeroDivisionError, lambda : self.zeroVec.elementwise() ** -1)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
660 self.assertRaises(ZeroDivisionError, lambda : Vector2(1,1).elementwise() / 0)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
661 self.assertRaises(ZeroDivisionError, lambda : Vector2(1,1).elementwise() // 0)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
662 self.assertRaises(ZeroDivisionError, lambda : Vector2(1,1).elementwise() % 0)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
663 self.assertRaises(ZeroDivisionError, lambda : Vector2(1,1).elementwise() / self.zeroVec)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
664 self.assertRaises(ZeroDivisionError, lambda : Vector2(1,1).elementwise() // self.zeroVec)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
665 self.assertRaises(ZeroDivisionError, lambda : Vector2(1,1).elementwise() % self.zeroVec)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
666 self.assertRaises(ZeroDivisionError, lambda : 2 / self.zeroVec.elementwise())
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
667 self.assertRaises(ZeroDivisionError, lambda : 2 // self.zeroVec.elementwise())
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
668 self.assertRaises(ZeroDivisionError, lambda : 2 % self.zeroVec.elementwise())
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
669
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
670 def test_slerp(self):
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
671 self.assertRaises(ValueError, lambda : self.zeroVec.slerp(self.v1, .5))
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
672 self.assertRaises(ValueError, lambda : self.v1.slerp(self.zeroVec, .5))
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
673 self.assertRaises(ValueError,
2006
6a532cb218dd removed iterator versions of vector.slerp() and .lerp() in favour of a parametrized version.
lorenz
parents: 1997
diff changeset
674 lambda : self.zeroVec.slerp(self.zeroVec, .5))
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
675 v1 = Vector2(1, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
676 v2 = Vector2(0, 1)
2006
6a532cb218dd removed iterator versions of vector.slerp() and .lerp() in favour of a parametrized version.
lorenz
parents: 1997
diff changeset
677 steps = 10
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
678 angle_step = v1.angle_to(v2) / steps
2007
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
679 for i, u in ((i, v1.slerp(v2, i/float(steps))) for i in range(steps+1)):
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
680 self.assertAlmostEqual(u.length(), 1)
2006
6a532cb218dd removed iterator versions of vector.slerp() and .lerp() in favour of a parametrized version.
lorenz
parents: 1997
diff changeset
681 self.assertAlmostEqual(v1.angle_to(u), i * angle_step)
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
682 self.assertEqual(u, v2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
683
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
684 v1 = Vector2(100, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
685 v2 = Vector2(0, 10)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
686 radial_factor = v2.length() / v1.length()
2007
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
687 for i, u in ((i, v1.slerp(v2, -i/float(steps))) for i in range(steps+1)):
2006
6a532cb218dd removed iterator versions of vector.slerp() and .lerp() in favour of a parametrized version.
lorenz
parents: 1997
diff changeset
688 self.assertAlmostEqual(u.length(), (v2.length() - v1.length()) * (float(i)/steps) + v1.length())
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
689 self.assertEqual(u, v2)
2024
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
690 self.assertEqual(v1.slerp(v1, .5), v1)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
691 self.assertEqual(v2.slerp(v2, .5), v2)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
692 self.assertRaises(ValueError, lambda : v1.slerp(-v1, 0.5))
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
693
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
694 def test_lerp(self):
2006
6a532cb218dd removed iterator versions of vector.slerp() and .lerp() in favour of a parametrized version.
lorenz
parents: 1997
diff changeset
695 """TODO"""
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
696 pass
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
697
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
698 def test_polar(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
699 v = Vector2()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
700 v.from_polar(self.v1.as_polar())
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
701 self.assertEqual(self.v1, v)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
702 self.assertEqual(self.e1.as_polar(), (1, 0))
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
703 self.assertEqual(self.e2.as_polar(), (1, 90))
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
704 self.assertEqual((2 * self.e2).as_polar(), (2, 90))
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
705 self.assertRaises(TypeError, lambda : v.from_polar((None, None)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
706 self.assertRaises(TypeError, lambda : v.from_polar("ab"))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
707 self.assertRaises(TypeError, lambda : v.from_polar((None, 1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
708 self.assertRaises(TypeError, lambda : v.from_polar((1, 2, 3)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
709 self.assertRaises(TypeError, lambda : v.from_polar((1,)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
710 self.assertRaises(TypeError, lambda : v.from_polar(1, 2))
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
711 v.from_polar((.5, 90))
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
712 self.assertEqual(v, .5 * self.e2)
2024
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
713 v.from_polar((1, 0))
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
714 self.assertEqual(v, self.e1)
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
715
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
716
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
717
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
718
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
719
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
720 class Vector3TypeTest(unittest.TestCase):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
721 def setUp(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
722 # gc.collect()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
723 self.zeroVec = Vector3()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
724 self.e1 = Vector3(1, 0, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
725 self.e2 = Vector3(0, 1, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
726 self.e3 = Vector3(0, 0, 1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
727 # self.t1 = (random(), random())
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
728 self.t1 = (1.2, 3.4, 9.6)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
729 self.l1 = list(self.t1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
730 self.v1 = Vector3(self.t1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
731 # self.t2 = (random(), random())
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
732 self.t2 = (5.6, 7.8, 2.1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
733 self.l2 = list(self.t2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
734 self.v2 = Vector3(self.t2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
735 # self.s1 = random()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
736 # self.s2 = random()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
737 self.s1 = 5.6
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
738 self.s2 = 7.8
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
739
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
740 def testConstructionDefault(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
741 v = Vector3()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
742 self.assertEqual(v.x, 0.)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
743 self.assertEqual(v.y, 0.)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
744 self.assertEqual(v.z, 0.)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
745
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
746
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
747 def testConstructionXYZ(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
748 v = Vector3(1.2, 3.4, 9.6)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
749 self.assertEqual(v.x, 1.2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
750 self.assertEqual(v.y, 3.4)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
751 self.assertEqual(v.z, 9.6)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
752
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
753 def testConstructionTuple(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
754 v = Vector3((1.2, 3.4, 9.6))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
755 self.assertEqual(v.x, 1.2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
756 self.assertEqual(v.y, 3.4)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
757 self.assertEqual(v.z, 9.6)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
758
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
759 def testConstructionList(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
760 v = Vector3([1.2, 3.4, -9.6])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
761 self.assertEqual(v.x, 1.2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
762 self.assertEqual(v.y, 3.4)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
763 self.assertEqual(v.z, -9.6)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
764
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
765 def testConstructionVector3(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
766 v = Vector3(Vector3(1.2, 3.4, -9.6))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
767 self.assertEqual(v.x, 1.2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
768 self.assertEqual(v.y, 3.4)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
769 self.assertEqual(v.z, -9.6)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
770
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
771 def testAttributAccess(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
772 tmp = self.v1.x
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
773 self.assertEqual(tmp, self.v1.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
774 self.assertEqual(tmp, self.v1[0])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
775 tmp = self.v1.y
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
776 self.assertEqual(tmp, self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
777 self.assertEqual(tmp, self.v1[1])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
778 tmp = self.v1.z
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
779 self.assertEqual(tmp, self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
780 self.assertEqual(tmp, self.v1[2])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
781 self.v1.x = 3.141
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
782 self.assertEqual(self.v1.x, 3.141)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
783 self.v1.y = 3.141
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
784 self.assertEqual(self.v1.y, 3.141)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
785 self.v1.z = 3.141
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
786 self.assertEqual(self.v1.z, 3.141)
2024
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
787 def assign_nonfloat():
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
788 v = Vector2()
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
789 v.x = "spam"
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
790 self.assertRaises(TypeError, assign_nonfloat)
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
791
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
792 def testSequence(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
793 v = Vector3(1.2, 3.4, -9.6)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
794 self.assertEqual(len(v), 3)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
795 self.assertEqual(v[0], 1.2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
796 self.assertEqual(v[1], 3.4)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
797 self.assertEqual(v[2], -9.6)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
798 self.assertRaises(IndexError, lambda : v[3])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
799 self.assertEqual(v[-1], -9.6)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
800 self.assertEqual(v[-2], 3.4)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
801 self.assertEqual(v[-3], 1.2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
802 self.assertRaises(IndexError, lambda : v[-4])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
803 self.assertEqual(v[:], [1.2, 3.4, -9.6])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
804 self.assertEqual(v[1:], [3.4, -9.6])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
805 self.assertEqual(v[:1], [1.2])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
806 self.assertEqual(v[:-1], [1.2, 3.4])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
807 self.assertEqual(v[1:2], [3.4])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
808 self.assertEqual(list(v), [1.2, 3.4, -9.6])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
809 self.assertEqual(tuple(v), (1.2, 3.4, -9.6))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
810 v[0] = 5.6
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
811 v[1] = 7.8
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
812 v[2] = -2.1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
813 self.assertEqual(v.x, 5.6)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
814 self.assertEqual(v.y, 7.8)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
815 self.assertEqual(v.z, -2.1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
816 v[:] = [9.1, 11.12, -13.41]
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
817 self.assertEqual(v.x, 9.1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
818 self.assertEqual(v.y, 11.12)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
819 self.assertEqual(v.z, -13.41)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
820 def overpopulate():
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
821 v = Vector3()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
822 v[:] = [1, 2, 3, 4]
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
823 self.assertRaises(ValueError, overpopulate)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
824 def underpopulate():
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
825 v = Vector3()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
826 v[:] = [1]
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
827 self.assertRaises(ValueError, underpopulate)
2024
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
828 def assign_nonfloat():
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
829 v = Vector2()
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
830 v[0] = "spam"
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
831 self.assertRaises(TypeError, assign_nonfloat)
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
832
2007
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
833 def testExtendedSlicing(self):
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
834 # deletion
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
835 def delSlice(vec, start=None, stop=None, step=None):
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
836 if start is not None and stop is not None and step is not None:
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
837 del vec[start:stop:step]
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
838 elif start is not None and stop is None and step is not None:
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
839 del vec[start::step]
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
840 elif start is None and stop is None and step is not None:
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
841 del vec[::step]
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
842 v = Vector3(self.v1)
2024
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
843 self.assertRaises(TypeError, delSlice, v, None, None, 2)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
844 self.assertRaises(TypeError, delSlice, v, 1, None, 2)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
845 self.assertRaises(TypeError, delSlice, v, 1, 2, 1)
2007
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
846
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
847 # assignment
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
848 v = Vector3(self.v1)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
849 v[::2] = [-1.1, -2.2]
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
850 self.assertEqual(v, [-1.1, self.v1.y, -2.2])
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
851 v = Vector3(self.v1)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
852 v[::-2] = [10, 20]
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
853 self.assertEqual(v, [20, self.v1.y, 10])
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
854 v = Vector3(self.v1)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
855 v[::-1] = v
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
856 self.assertEqual(v, [self.v1.z, self.v1.y, self.v1.x])
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
857 a = Vector3(self.v1)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
858 b = Vector3(self.v1)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
859 c = Vector3(self.v1)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
860 a[1:2] = [2.2]
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
861 b[slice(1,2)] = [2.2]
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
862 c[1:2:] = (2.2,)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
863 self.assertEqual(a, b)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
864 self.assertEqual(a, c)
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
865 self.assertEqual(type(a), type(self.v1))
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
866 self.assertEqual(type(b), type(self.v1))
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
867 self.assertEqual(type(c), type(self.v1))
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
868
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
869 def testAdd(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
870 v3 = self.v1 + self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
871 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
872 self.assertEqual(v3.x, self.v1.x + self.v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
873 self.assertEqual(v3.y, self.v1.y + self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
874 self.assertEqual(v3.z, self.v1.z + self.v2.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
875 v3 = self.v1 + self.t2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
876 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
877 self.assertEqual(v3.x, self.v1.x + self.t2[0])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
878 self.assertEqual(v3.y, self.v1.y + self.t2[1])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
879 self.assertEqual(v3.z, self.v1.z + self.t2[2])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
880 v3 = self.v1 + self.l2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
881 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
882 self.assertEqual(v3.x, self.v1.x + self.l2[0])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
883 self.assertEqual(v3.y, self.v1.y + self.l2[1])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
884 self.assertEqual(v3.z, self.v1.z + self.l2[2])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
885 v3 = self.t1 + self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
886 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
887 self.assertEqual(v3.x, self.t1[0] + self.v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
888 self.assertEqual(v3.y, self.t1[1] + self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
889 self.assertEqual(v3.z, self.t1[2] + self.v2.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
890 v3 = self.l1 + self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
891 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
892 self.assertEqual(v3.x, self.l1[0] + self.v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
893 self.assertEqual(v3.y, self.l1[1] + self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
894 self.assertEqual(v3.z, self.l1[2] + self.v2.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
895
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
896 def testSub(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
897 v3 = self.v1 - self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
898 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
899 self.assertEqual(v3.x, self.v1.x - self.v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
900 self.assertEqual(v3.y, self.v1.y - self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
901 self.assertEqual(v3.z, self.v1.z - self.v2.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
902 v3 = self.v1 - self.t2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
903 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
904 self.assertEqual(v3.x, self.v1.x - self.t2[0])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
905 self.assertEqual(v3.y, self.v1.y - self.t2[1])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
906 self.assertEqual(v3.z, self.v1.z - self.t2[2])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
907 v3 = self.v1 - self.l2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
908 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
909 self.assertEqual(v3.x, self.v1.x - self.l2[0])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
910 self.assertEqual(v3.y, self.v1.y - self.l2[1])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
911 self.assertEqual(v3.z, self.v1.z - self.l2[2])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
912 v3 = self.t1 - self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
913 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
914 self.assertEqual(v3.x, self.t1[0] - self.v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
915 self.assertEqual(v3.y, self.t1[1] - self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
916 self.assertEqual(v3.z, self.t1[2] - self.v2.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
917 v3 = self.l1 - self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
918 self.assert_(isinstance(v3, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
919 self.assertEqual(v3.x, self.l1[0] - self.v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
920 self.assertEqual(v3.y, self.l1[1] - self.v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
921 self.assertEqual(v3.z, self.l1[2] - self.v2.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
922
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
923 def testScalarMultiplication(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
924 v = self.s1 * self.v1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
925 self.assert_(isinstance(v, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
926 self.assertEqual(v.x, self.s1 * self.v1.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
927 self.assertEqual(v.y, self.s1 * self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
928 self.assertEqual(v.z, self.s1 * self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
929 v = self.v1 * self.s2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
930 self.assertEqual(v.x, self.v1.x * self.s2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
931 self.assertEqual(v.y, self.v1.y * self.s2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
932 self.assertEqual(v.z, self.v1.z * self.s2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
933
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
934 def testScalarDivision(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
935 v = self.v1 / self.s1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
936 self.assert_(isinstance(v, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
937 self.assertAlmostEqual(v.x, self.v1.x / self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
938 self.assertAlmostEqual(v.y, self.v1.y / self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
939 self.assertAlmostEqual(v.z, self.v1.z / self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
940 v = self.v1 // self.s2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
941 self.assert_(isinstance(v, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
942 self.assertEqual(v.x, self.v1.x // self.s2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
943 self.assertEqual(v.y, self.v1.y // self.s2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
944 self.assertEqual(v.z, self.v1.z // self.s2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
945
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
946 def testBool(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
947 self.assertEqual(bool(self.zeroVec), False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
948 self.assertEqual(bool(self.v1), True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
949 self.assert_(not self.zeroVec)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
950 self.assert_(self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
951
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
952 def testUnary(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
953 v = +self.v1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
954 self.assert_(isinstance(v, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
955 self.assertEqual(v.x, self.v1.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
956 self.assertEqual(v.y, self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
957 self.assertEqual(v.z, self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
958 self.assertNotEqual(id(v), id(self.v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
959 v = -self.v1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
960 self.assert_(isinstance(v, type(self.v1)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
961 self.assertEqual(v.x, -self.v1.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
962 self.assertEqual(v.y, -self.v1.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
963 self.assertEqual(v.z, -self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
964 self.assertNotEqual(id(v), id(self.v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
965
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
966 def testCompare(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
967 int_vec = Vector3(3, -2, 13)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
968 flt_vec = Vector3(3.0, -2.0, 13.)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
969 zero_vec = Vector3(0, 0, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
970 self.assertEqual(int_vec == flt_vec, True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
971 self.assertEqual(int_vec != flt_vec, False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
972 self.assertEqual(int_vec != zero_vec, True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
973 self.assertEqual(flt_vec == zero_vec, False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
974 self.assertEqual(int_vec == (3, -2, 13), True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
975 self.assertEqual(int_vec != (3, -2, 13), False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
976 self.assertEqual(int_vec != [0, 0], True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
977 self.assertEqual(int_vec == [0, 0], False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
978 self.assertEqual(int_vec != 5, True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
979 self.assertEqual(int_vec == 5, False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
980 self.assertEqual(int_vec != [3, -2, 0, 1], True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
981 self.assertEqual(int_vec == [3, -2, 0, 1], False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
982
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
983 def testStr(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
984 v = Vector3(1.2, 3.4, 5.6)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
985 self.assertEqual(str(v), "[1.2, 3.4, 5.6]")
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
986
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
987 def testRepr(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
988 v = Vector3(1.2, 3.4, -9.6)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
989 self.assertEqual(v.__repr__(), "<Vector3(1.2, 3.4, -9.6)>")
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
990 self.assertEqual(v, Vector3(v.__repr__()))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
991
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
992 def testIter(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
993 it = self.v1.__iter__()
2007
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
994 # support py2.x and 3.x
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
995 if hasattr(it, "next"):
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
996 next_ = it.next
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
997 elif hasattr(it, "__next__"):
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
998 next_ = it.__next__
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
999 else:
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
1000 self.fail("Iterator has neither a 'next' nor a '__next__' method.")
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
1001 self.assertEqual(next_(), self.v1[0])
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
1002 self.assertEqual(next_(), self.v1[1])
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
1003 self.assertEqual(next_(), self.v1[2])
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
1004 self.assertRaises(StopIteration, lambda : next_())
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1005 it1 = self.v1.__iter__()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1006 it2 = self.v1.__iter__()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1007 self.assertNotEqual(id(it1), id(it2))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1008 self.assertEqual(id(it1), id(it1.__iter__()))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1009 self.assertEqual(list(it1), list(it2));
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1010 self.assertEqual(list(self.v1.__iter__()), self.l1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1011 idx = 0
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1012 for val in self.v1:
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1013 self.assertEqual(val, self.v1[idx])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1014 idx += 1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1015
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1016 def test_rotate(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1017 v1 = Vector3(1, 0, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1018 axis = Vector3(0, 1, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1019 v2 = v1.rotate(90, axis)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1020 v3 = v1.rotate(90 + 360, axis)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1021 self.assertEqual(v1.x, 1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1022 self.assertEqual(v1.y, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1023 self.assertEqual(v1.z, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1024 self.assertEqual(v2.x, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1025 self.assertEqual(v2.y, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1026 self.assertEqual(v2.z, -1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1027 self.assertEqual(v3.x, v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1028 self.assertEqual(v3.y, v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1029 self.assertEqual(v3.z, v2.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1030 v1 = Vector3(-1, -1, -1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1031 v2 = v1.rotate(-90, axis)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1032 self.assertEqual(v2.x, 1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1033 self.assertEqual(v2.y, -1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1034 self.assertEqual(v2.z, -1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1035 v2 = v1.rotate(360, axis)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1036 self.assertEqual(v1.x, v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1037 self.assertEqual(v1.y, v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1038 self.assertEqual(v1.z, v2.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1039 v2 = v1.rotate(0, axis)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1040 self.assertEqual(v1.x, v2.x)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1041 self.assertEqual(v1.y, v2.y)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1042 self.assertEqual(v1.z, v2.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1043
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1044 def test_rotate_ip(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1045 v = Vector3(1, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1046 axis = Vector3(0, 1, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1047 self.assertEqual(v.rotate_ip(90, axis), None)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1048 self.assertEqual(v.x, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1049 self.assertEqual(v.y, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1050 self.assertEqual(v.z, -1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1051 v = Vector3(-1, -1, 1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1052 v.rotate_ip(-90, axis)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1053 self.assertEqual(v.x, -1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1054 self.assertEqual(v.y, -1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1055 self.assertEqual(v.z, -1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1056
1997
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1057 def test_rotate_x(self):
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1058 v1 = Vector3(1, 0, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1059 v2 = v1.rotate_x(90)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1060 v3 = v1.rotate_x(90 + 360)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1061 self.assertEqual(v1.x, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1062 self.assertEqual(v1.y, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1063 self.assertEqual(v1.z, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1064 self.assertEqual(v2.x, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1065 self.assertEqual(v2.y, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1066 self.assertEqual(v2.z, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1067 self.assertEqual(v3.x, v2.x)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1068 self.assertEqual(v3.y, v2.y)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1069 self.assertEqual(v3.z, v2.z)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1070 v1 = Vector3(-1, -1, -1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1071 v2 = v1.rotate_x(-90)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1072 self.assertEqual(v2.x, -1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1073 self.assertAlmostEqual(v2.y, -1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1074 self.assertAlmostEqual(v2.z, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1075 v2 = v1.rotate_x(360)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1076 self.assertAlmostEqual(v1.x, v2.x)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1077 self.assertAlmostEqual(v1.y, v2.y)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1078 self.assertAlmostEqual(v1.z, v2.z)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1079 v2 = v1.rotate_x(0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1080 self.assertEqual(v1.x, v2.x)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1081 self.assertAlmostEqual(v1.y, v2.y)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1082 self.assertAlmostEqual(v1.z, v2.z)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1083
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1084 def test_rotate_x_ip(self):
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1085 v = Vector3(1, 0, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1086 self.assertEqual(v.rotate_x_ip(90), None)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1087 self.assertEqual(v.x, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1088 self.assertEqual(v.y, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1089 self.assertEqual(v.z, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1090 v = Vector3(-1, -1, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1091 v.rotate_x_ip(-90)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1092 self.assertEqual(v.x, -1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1093 self.assertAlmostEqual(v.y, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1094 self.assertAlmostEqual(v.z, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1095
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1096 def test_rotate_y(self):
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1097 v1 = Vector3(1, 0, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1098 v2 = v1.rotate_y(90)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1099 v3 = v1.rotate_y(90 + 360)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1100 self.assertEqual(v1.x, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1101 self.assertEqual(v1.y, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1102 self.assertEqual(v1.z, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1103 self.assertAlmostEqual(v2.x, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1104 self.assertEqual(v2.y, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1105 self.assertAlmostEqual(v2.z, -1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1106 self.assertAlmostEqual(v3.x, v2.x)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1107 self.assertEqual(v3.y, v2.y)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1108 self.assertAlmostEqual(v3.z, v2.z)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1109 v1 = Vector3(-1, -1, -1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1110 v2 = v1.rotate_y(-90)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1111 self.assertAlmostEqual(v2.x, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1112 self.assertEqual(v2.y, -1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1113 self.assertAlmostEqual(v2.z, -1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1114 v2 = v1.rotate_y(360)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1115 self.assertAlmostEqual(v1.x, v2.x)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1116 self.assertEqual(v1.y, v2.y)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1117 self.assertAlmostEqual(v1.z, v2.z)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1118 v2 = v1.rotate_y(0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1119 self.assertEqual(v1.x, v2.x)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1120 self.assertEqual(v1.y, v2.y)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1121 self.assertEqual(v1.z, v2.z)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1122
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1123 def test_rotate_y_ip(self):
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1124 v = Vector3(1, 0, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1125 self.assertEqual(v.rotate_y_ip(90), None)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1126 self.assertAlmostEqual(v.x, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1127 self.assertEqual(v.y, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1128 self.assertAlmostEqual(v.z, -1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1129 v = Vector3(-1, -1, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1130 v.rotate_y_ip(-90)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1131 self.assertAlmostEqual(v.x, -1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1132 self.assertEqual(v.y, -1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1133 self.assertAlmostEqual(v.z, -1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1134
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1135 def test_rotate_z(self):
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1136 v1 = Vector3(1, 0, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1137 v2 = v1.rotate_z(90)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1138 v3 = v1.rotate_z(90 + 360)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1139 self.assertEqual(v1.x, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1140 self.assertEqual(v1.y, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1141 self.assertEqual(v1.z, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1142 self.assertAlmostEqual(v2.x, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1143 self.assertAlmostEqual(v2.y, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1144 self.assertEqual(v2.z, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1145 self.assertAlmostEqual(v3.x, v2.x)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1146 self.assertAlmostEqual(v3.y, v2.y)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1147 self.assertEqual(v3.z, v2.z)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1148 v1 = Vector3(-1, -1, -1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1149 v2 = v1.rotate_z(-90)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1150 self.assertAlmostEqual(v2.x, -1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1151 self.assertAlmostEqual(v2.y, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1152 self.assertEqual(v2.z, -1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1153 v2 = v1.rotate_z(360)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1154 self.assertAlmostEqual(v1.x, v2.x)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1155 self.assertAlmostEqual(v1.y, v2.y)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1156 self.assertEqual(v1.z, v2.z)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1157 v2 = v1.rotate_z(0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1158 self.assertAlmostEqual(v1.x, v2.x)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1159 self.assertAlmostEqual(v1.y, v2.y)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1160 self.assertEqual(v1.z, v2.z)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1161
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1162 def test_rotate_z_ip(self):
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1163 v = Vector3(1, 0, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1164 self.assertEqual(v.rotate_z_ip(90), None)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1165 self.assertAlmostEqual(v.x, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1166 self.assertAlmostEqual(v.y, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1167 self.assertEqual(v.z, 0)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1168 v = Vector3(-1, -1, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1169 v.rotate_z_ip(-90)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1170 self.assertAlmostEqual(v.x, -1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1171 self.assertAlmostEqual(v.y, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1172 self.assertEqual(v.z, 1)
b815414e338f fixed bugs in Vector3 method table
lorenz
parents: 1994
diff changeset
1173
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1174 def test_normalize(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1175 v = self.v1.normalize()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1176 # length is 1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1177 self.assertAlmostEqual(v.x * v.x + v.y * v.y + v.z * v.z, 1.)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1178 # v1 is unchanged
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1179 self.assertEqual(self.v1.x, self.l1[0])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1180 self.assertEqual(self.v1.y, self.l1[1])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1181 self.assertEqual(self.v1.z, self.l1[2])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1182 # v2 is paralell to v1 (tested via cross product)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1183 cross = ((self.v1.y * v.z - self.v1.z * v.y) ** 2 +
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1184 (self.v1.z * v.x - self.v1.x * v.z) ** 2 +
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1185 (self.v1.x * v.y - self.v1.y * v.x) ** 2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1186 self.assertAlmostEqual(cross, 0.)
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
1187 self.assertRaises(ValueError, lambda : self.zeroVec.normalize())
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1188
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1189 def test_normalize_ip(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1190 v = +self.v1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1191 # v has length != 1 before normalizing
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1192 self.assertNotEqual(v.x * v.x + v.y * v.y + v.z * v.z, 1.)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1193 # inplace operations should return None
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1194 self.assertEqual(v.normalize_ip(), None)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1195 # length is 1
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1196 self.assertAlmostEqual(v.x * v.x + v.y * v.y + v.z * v.z, 1.)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1197 # v2 is paralell to v1 (tested via cross product)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1198 cross = ((self.v1.y * v.z - self.v1.z * v.y) ** 2 +
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1199 (self.v1.z * v.x - self.v1.x * v.z) ** 2 +
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1200 (self.v1.x * v.y - self.v1.y * v.x) ** 2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1201 self.assertAlmostEqual(cross, 0.)
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
1202 self.assertRaises(ValueError, lambda : self.zeroVec.normalize_ip())
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1203
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1204 def test_is_normalized(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1205 self.assertEqual(self.v1.is_normalized(), False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1206 v = self.v1.normalize()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1207 self.assertEqual(v.is_normalized(), True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1208 self.assertEqual(self.e2.is_normalized(), True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1209 self.assertEqual(self.zeroVec.is_normalized(), False)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1210
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1211 def test_cross(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1212 def cross(a, b):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1213 return Vector3(a[1] * b[2] - a[2] * b[1],
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1214 a[2] * b[0] - a[0] * b[2],
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1215 a[0] * b[1] - a[1] * b[0])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1216 self.assertEqual(self.v1.cross(self.v2), cross(self.v1, self.v2))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1217 self.assertEqual(self.v1.cross(self.l2), cross(self.v1, self.l2))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1218 self.assertEqual(self.v1.cross(self.t2), cross(self.v1, self.t2))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1219 self.assertEqual(self.v1.cross(self.v2), -self.v2.cross(self.v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1220 self.assertEqual(self.v1.cross(self.v1), self.zeroVec)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1221
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1222 def test_dot(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1223 self.assertAlmostEqual(self.v1.dot(self.v2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1224 self.v1.x * self.v2.x + self.v1.y * self.v2.y + self.v1.z * self.v2.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1225 self.assertAlmostEqual(self.v1.dot(self.l2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1226 self.v1.x * self.l2[0] + self.v1.y * self.l2[1] + self.v1.z * self.l2[2])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1227 self.assertAlmostEqual(self.v1.dot(self.t2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1228 self.v1.x * self.t2[0] + self.v1.y * self.t2[1] + self.v1.z * self.t2[2])
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1229 self.assertAlmostEqual(self.v1.dot(self.v2), self.v2.dot(self.v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1230 self.assertAlmostEqual(self.v1.dot(self.v2), self.v1 * self.v2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1231
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1232 def test_angle_to(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1233 self.assertEqual(Vector3(1, 1, 0).angle_to((-1, 1, 0)), 90)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1234 self.assertEqual(Vector3(1, 0, 0).angle_to((0, 0, -1)), 90)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1235 self.assertEqual(Vector3(1, 0, 0).angle_to((-1, 0, 1)), 135)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1236 self.assertEqual(abs(Vector3(1, 0, 1).angle_to((-1, 0, -1))), 180)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1237 # if we rotate v1 by the angle_to v2 around their cross product
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1238 # we should look in the same direction
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1239 self.assertEqual(self.v1.rotate(self.v1.angle_to(self.v2), self.v1.cross(self.v2)).normalize(),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1240 self.v2.normalize())
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1241
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1242 def test_scale_to_length(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1243 v = Vector3(1, 1, 1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1244 v.scale_to_length(2.5)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1245 self.assertEqual(v, Vector3(2.5, 2.5, 2.5) / math.sqrt(3))
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
1246 self.assertRaises(ValueError, lambda : self.zeroVec.scale_to_length(1))
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1247 self.assertEqual(v.scale_to_length(0), None)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1248 self.assertEqual(v, self.zeroVec)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1249
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1250 def test_length(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1251 self.assertEqual(Vector3(3, 4, 5).length(), math.sqrt(3 * 3 + 4 * 4 + 5 * 5))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1252 self.assertEqual(Vector3(-3, 4, 5).length(), math.sqrt(-3 * -3 + 4 * 4 + 5 * 5))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1253 self.assertEqual(self.zeroVec.length(), 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1254
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1255 def test_length_squared(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1256 self.assertEqual(Vector3(3, 4, 5).length_squared(), 3 * 3 + 4 * 4 + 5 * 5)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1257 self.assertEqual(Vector3(-3, 4, 5).length_squared(), -3 * -3 + 4 * 4 + 5 * 5)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1258 self.assertEqual(self.zeroVec.length_squared(), 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1259
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1260 def test_reflect(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1261 v = Vector3(1, -1, 1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1262 n = Vector3(0, 1, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1263 self.assertEqual(v.reflect(n), Vector3(1, 1, 1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1264 self.assertEqual(v.reflect(3*n), v.reflect(n))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1265 self.assertEqual(v.reflect(-v), -v)
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
1266 self.assertRaises(ValueError, lambda : v.reflect(self.zeroVec))
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1267
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1268 def test_reflect_ip(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1269 v1 = Vector3(1, -1, 1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1270 v2 = Vector3(v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1271 n = Vector3(0, 1, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1272 self.assertEqual(v2.reflect_ip(n), None)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1273 self.assertEqual(v2, Vector3(1, 1, 1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1274 v2 = Vector3(v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1275 v2.reflect_ip(3*n)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1276 self.assertEqual(v2, v1.reflect(n))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1277 v2 = Vector3(v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1278 v2.reflect_ip(-v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1279 self.assertEqual(v2, -v1)
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
1280 self.assertRaises(ValueError, lambda : v2.reflect_ip(self.zeroVec))
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1281
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1282 def test_distance_to(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1283 diff = self.v1 - self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1284 self.assertEqual(self.e1.distance_to(self.e2), math.sqrt(2))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1285 self.assertEqual(self.v1.distance_to(self.v2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1286 math.sqrt(diff.x * diff.x + diff.y * diff.y + diff.z * diff.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1287 self.assertEqual(self.v1.distance_to(self.v1), 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1288 self.assertEqual(self.v1.distance_to(self.v2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1289 self.v2.distance_to(self.v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1290
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1291 def test_distance_squared_to(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1292 diff = self.v1 - self.v2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1293 self.assertEqual(self.e1.distance_squared_to(self.e2), 2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1294 self.assertEqual(self.v1.distance_squared_to(self.v2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1295 diff.x * diff.x + diff.y * diff.y + diff.z * diff.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1296 self.assertEqual(self.v1.distance_squared_to(self.v1), 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1297 self.assertEqual(self.v1.distance_squared_to(self.v2),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1298 self.v2.distance_squared_to(self.v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1299
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1300 def testSwizzle(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1301 self.assertEquals(hasattr(pygame.math, "enable_swizzling"), True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1302 self.assertEquals(hasattr(pygame.math, "disable_swizzling"), True)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1303 # swizzling disabled by default
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1304 self.assertRaises(AttributeError, lambda : self.v1.yx)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1305 pygame.math.enable_swizzling()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1306
2013
3b778c6f8cde fixed bug in swizzle code (you were only able to get x and y so far).
lorenz
parents: 2007
diff changeset
1307 self.assertEqual(self.v1.yxz, (self.v1.y, self.v1.x, self.v1.z))
3b778c6f8cde fixed bug in swizzle code (you were only able to get x and y so far).
lorenz
parents: 2007
diff changeset
1308 self.assertEqual(self.v1.xxyyzzxyz, (self.v1.x, self.v1.x, self.v1.y,
3b778c6f8cde fixed bug in swizzle code (you were only able to get x and y so far).
lorenz
parents: 2007
diff changeset
1309 self.v1.y, self.v1.z, self.v1.z,
3b778c6f8cde fixed bug in swizzle code (you were only able to get x and y so far).
lorenz
parents: 2007
diff changeset
1310 self.v1.x, self.v1.y, self.v1.z))
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1311 self.v1.xyz = self.t2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1312 self.assertEqual(self.v1, self.t2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1313 self.v1.zxy = self.t2
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1314 self.assertEqual(self.v1, (self.t2[1], self.t2[2], self.t2[0]))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1315 self.v1.yz = self.t2[:2]
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1316 self.assertEqual(self.v1, (self.t2[1], self.t2[0], self.t2[1]))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1317 self.assertEqual(type(self.v1), Vector3)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1318 def invalidSwizzleX():
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1319 Vector3().xx = (1, 2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1320 def invalidSwizzleY():
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1321 Vector3().yy = (1, 2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1322 def invalidSwizzleZ():
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1323 Vector3().zz = (1, 2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1324 def invalidSwizzleW():
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1325 Vector3().ww = (1, 2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1326 self.assertRaises(AttributeError, invalidSwizzleX)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1327 self.assertRaises(AttributeError, invalidSwizzleY)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1328 self.assertRaises(AttributeError, invalidSwizzleZ)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1329 self.assertRaises(AttributeError, invalidSwizzleW)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1330 def invalidAssignment():
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1331 Vector3().xy = 3
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1332 self.assertRaises(TypeError, invalidAssignment)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1333
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1334 def test_elementwise(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1335 # behaviour for "elementwise op scalar"
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1336 self.assertEqual(self.v1.elementwise() + self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1337 (self.v1.x + self.s1, self.v1.y + self.s1, self.v1.z + self.s1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1338 self.assertEqual(self.v1.elementwise() - self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1339 (self.v1.x - self.s1, self.v1.y - self.s1, self.v1.z - self.s1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1340 self.assertEqual(self.v1.elementwise() * self.s2,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1341 (self.v1.x * self.s2, self.v1.y * self.s2, self.v1.z * self.s2))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1342 self.assertEqual(self.v1.elementwise() / self.s2,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1343 (self.v1.x / self.s2, self.v1.y / self.s2, self.v1.z / self.s2))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1344 self.assertEqual(self.v1.elementwise() // self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1345 (self.v1.x // self.s1, self.v1.y // self.s1, self.v1.z // self.s1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1346 self.assertEqual(self.v1.elementwise() ** self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1347 (self.v1.x ** self.s1, self.v1.y ** self.s1, self.v1.z ** self.s1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1348 self.assertEqual(self.v1.elementwise() % self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1349 (self.v1.x % self.s1, self.v1.y % self.s1, self.v1.z % self.s1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1350 self.assertEqual(self.v1.elementwise() > self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1351 self.v1.x > self.s1 and self.v1.y > self.s1 and self.v1.z > self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1352 self.assertEqual(self.v1.elementwise() < self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1353 self.v1.x < self.s1 and self.v1.y < self.s1 and self.v1.z < self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1354 self.assertEqual(self.v1.elementwise() == self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1355 self.v1.x == self.s1 and self.v1.y == self.s1 and self.v1.z == self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1356 self.assertEqual(self.v1.elementwise() != self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1357 self.v1.x != self.s1 and self.v1.y != self.s1 and self.v1.z != self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1358 self.assertEqual(self.v1.elementwise() >= self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1359 self.v1.x >= self.s1 and self.v1.y >= self.s1 and self.v1.z >= self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1360 self.assertEqual(self.v1.elementwise() <= self.s1,
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1361 self.v1.x <= self.s1 and self.v1.y <= self.s1 and self.v1.z <= self.s1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1362 # behaviour for "scalar op elementwise"
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1363 self.assertEqual(5 + self.v1.elementwise(), Vector3(5, 5, 5) + self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1364 self.assertEqual(3.5 - self.v1.elementwise(), Vector3(3.5, 3.5, 3.5) - self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1365 self.assertEqual(7.5 * self.v1.elementwise() , 7.5 * self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1366 self.assertEqual(-3.5 / self.v1.elementwise(), (-3.5 / self.v1.x, -3.5 / self.v1.y, -3.5 / self.v1.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1367 self.assertEqual(-3.5 // self.v1.elementwise(), (-3.5 // self.v1.x, -3.5 // self.v1.y, -3.5 // self.v1.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1368 self.assertEqual(-3.5 ** self.v1.elementwise(), (-3.5 ** self.v1.x, -3.5 ** self.v1.y, -3.5 ** self.v1.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1369 self.assertEqual(3 % self.v1.elementwise(), (3 % self.v1.x, 3 % self.v1.y, 3 % self.v1.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1370 self.assertEqual(2 < self.v1.elementwise(), 2 < self.v1.x and 2 < self.v1.y and 2 < self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1371 self.assertEqual(2 > self.v1.elementwise(), 2 > self.v1.x and 2 > self.v1.y and 2 > self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1372 self.assertEqual(1 == self.v1.elementwise(), 1 == self.v1.x and 1 == self.v1.y and 1 == self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1373 self.assertEqual(1 != self.v1.elementwise(), 1 != self.v1.x and 1 != self.v1.y and 1 != self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1374 self.assertEqual(2 <= self.v1.elementwise(), 2 <= self.v1.x and 2 <= self.v1.y and 2 <= self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1375 self.assertEqual(-7 >= self.v1.elementwise(), -7 >= self.v1.x and -7 >= self.v1.y and -7 >= self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1376 self.assertEqual(-7 != self.v1.elementwise(), -7 != self.v1.x and -7 != self.v1.y and -7 != self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1377
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1378 # behaviour for "elementwise op vector"
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1379 self.assertEqual(type(self.v1.elementwise() * self.v2), type(self.v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1380 self.assertEqual(self.v1.elementwise() + self.v2, self.v1 + self.v2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1381 self.assertEqual(self.v1.elementwise() + self.v2, self.v1 + self.v2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1382 self.assertEqual(self.v1.elementwise() - self.v2, self.v1 - self.v2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1383 self.assertEqual(self.v1.elementwise() * self.v2, (self.v1.x * self.v2.x, self.v1.y * self.v2.y, self.v1.z * self.v2.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1384 self.assertEqual(self.v1.elementwise() / self.v2, (self.v1.x / self.v2.x, self.v1.y / self.v2.y, self.v1.z / self.v2.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1385 self.assertEqual(self.v1.elementwise() // self.v2, (self.v1.x // self.v2.x, self.v1.y // self.v2.y, self.v1.z // self.v2.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1386 self.assertEqual(self.v1.elementwise() ** self.v2, (self.v1.x ** self.v2.x, self.v1.y ** self.v2.y, self.v1.z ** self.v2.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1387 self.assertEqual(self.v1.elementwise() % self.v2, (self.v1.x % self.v2.x, self.v1.y % self.v2.y, self.v1.z % self.v2.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1388 self.assertEqual(self.v1.elementwise() > self.v2, self.v1.x > self.v2.x and self.v1.y > self.v2.y and self.v1.z > self.v2.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1389 self.assertEqual(self.v1.elementwise() < self.v2, self.v1.x < self.v2.x and self.v1.y < self.v2.y and self.v1.z < self.v2.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1390 self.assertEqual(self.v1.elementwise() >= self.v2, self.v1.x >= self.v2.x and self.v1.y >= self.v2.y and self.v1.z >= self.v2.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1391 self.assertEqual(self.v1.elementwise() <= self.v2, self.v1.x <= self.v2.x and self.v1.y <= self.v2.y and self.v1.z <= self.v2.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1392 self.assertEqual(self.v1.elementwise() == self.v2, self.v1.x == self.v2.x and self.v1.y == self.v2.y and self.v1.z == self.v2.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1393 self.assertEqual(self.v1.elementwise() != self.v2, self.v1.x != self.v2.x and self.v1.y != self.v2.y and self.v1.z != self.v2.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1394 # behaviour for "vector op elementwise"
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1395 self.assertEqual(self.v2 + self.v1.elementwise(), self.v2 + self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1396 self.assertEqual(self.v2 - self.v1.elementwise(), self.v2 - self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1397 self.assertEqual(self.v2 * self.v1.elementwise(), (self.v2.x * self.v1.x, self.v2.y * self.v1.y, self.v2.z * self.v1.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1398 self.assertEqual(self.v2 / self.v1.elementwise(), (self.v2.x / self.v1.x, self.v2.y / self.v1.y, self.v2.z / self.v1.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1399 self.assertEqual(self.v2 // self.v1.elementwise(), (self.v2.x // self.v1.x, self.v2.y // self.v1.y, self.v2.z // self.v1.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1400 self.assertEqual(self.v2 ** self.v1.elementwise(), (self.v2.x ** self.v1.x, self.v2.y ** self.v1.y, self.v2.z ** self.v1.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1401 self.assertEqual(self.v2 % self.v1.elementwise(), (self.v2.x % self.v1.x, self.v2.y % self.v1.y, self.v2.z % self.v1.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1402 self.assertEqual(self.v2 < self.v1.elementwise(), self.v2.x < self.v1.x and self.v2.y < self.v1.y and self.v2.z < self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1403 self.assertEqual(self.v2 > self.v1.elementwise(), self.v2.x > self.v1.x and self.v2.y > self.v1.y and self.v2.z > self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1404 self.assertEqual(self.v2 <= self.v1.elementwise(), self.v2.x <= self.v1.x and self.v2.y <= self.v1.y and self.v2.z <= self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1405 self.assertEqual(self.v2 >= self.v1.elementwise(), self.v2.x >= self.v1.x and self.v2.y >= self.v1.y and self.v2.z >= self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1406 self.assertEqual(self.v2 == self.v1.elementwise(), self.v2.x == self.v1.x and self.v2.y == self.v1.y and self.v2.z == self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1407 self.assertEqual(self.v2 != self.v1.elementwise(), self.v2.x != self.v1.x and self.v2.y != self.v1.y and self.v2.z != self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1408
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1409 # behaviour for "elementwise op elementwise"
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1410 self.assertEqual(self.v2.elementwise() + self.v1.elementwise(), self.v2 + self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1411 self.assertEqual(self.v2.elementwise() - self.v1.elementwise(), self.v2 - self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1412 self.assertEqual(self.v2.elementwise() * self.v1.elementwise(),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1413 (self.v2.x * self.v1.x, self.v2.y * self.v1.y, self.v2.z * self.v1.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1414 self.assertEqual(self.v2.elementwise() / self.v1.elementwise(),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1415 (self.v2.x / self.v1.x, self.v2.y / self.v1.y, self.v2.z / self.v1.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1416 self.assertEqual(self.v2.elementwise() // self.v1.elementwise(),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1417 (self.v2.x // self.v1.x, self.v2.y // self.v1.y, self.v2.z // self.v1.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1418 self.assertEqual(self.v2.elementwise() ** self.v1.elementwise(),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1419 (self.v2.x ** self.v1.x, self.v2.y ** self.v1.y, self.v2.z ** self.v1.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1420 self.assertEqual(self.v2.elementwise() % self.v1.elementwise(),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1421 (self.v2.x % self.v1.x, self.v2.y % self.v1.y, self.v2.z % self.v1.z))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1422 self.assertEqual(self.v2.elementwise() < self.v1.elementwise(),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1423 self.v2.x < self.v1.x and self.v2.y < self.v1.y and self.v2.z < self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1424 self.assertEqual(self.v2.elementwise() > self.v1.elementwise(),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1425 self.v2.x > self.v1.x and self.v2.y > self.v1.y and self.v2.z > self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1426 self.assertEqual(self.v2.elementwise() <= self.v1.elementwise(),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1427 self.v2.x <= self.v1.x and self.v2.y <= self.v1.y and self.v2.z <= self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1428 self.assertEqual(self.v2.elementwise() >= self.v1.elementwise(),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1429 self.v2.x >= self.v1.x and self.v2.y >= self.v1.y and self.v2.z >= self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1430 self.assertEqual(self.v2.elementwise() == self.v1.elementwise(),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1431 self.v2.x == self.v1.x and self.v2.y == self.v1.y and self.v2.z == self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1432 self.assertEqual(self.v2.elementwise() != self.v1.elementwise(),
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1433 self.v2.x != self.v1.x and self.v2.y != self.v1.y and self.v2.z != self.v1.z)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1434
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1435 # other behaviour
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1436 self.assertEqual(abs(self.v1.elementwise()), (abs(self.v1.x), abs(self.v1.y), abs(self.v1.z)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1437 self.assertEqual(-self.v1.elementwise(), -self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1438 self.assertEqual(+self.v1.elementwise(), +self.v1)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1439 self.assertEqual(bool(self.v1.elementwise()), bool(self.v1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1440 self.assertEqual(bool(Vector3().elementwise()), bool(Vector3()))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1441 self.assertEqual(self.zeroVec.elementwise() ** 0, (1, 1, 1))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1442 self.assertRaises(ValueError, lambda : pow(Vector3(-1, 0, 0).elementwise(), 1.2))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1443 self.assertRaises(ZeroDivisionError, lambda : self.zeroVec.elementwise() ** -1)
2024
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
1444 self.assertRaises(ZeroDivisionError, lambda : Vector3(1,1,1).elementwise() / 0)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
1445 self.assertRaises(ZeroDivisionError, lambda : Vector3(1,1,1).elementwise() // 0)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
1446 self.assertRaises(ZeroDivisionError, lambda : Vector3(1,1,1).elementwise() % 0)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
1447 self.assertRaises(ZeroDivisionError, lambda : Vector3(1,1,1).elementwise() / self.zeroVec)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
1448 self.assertRaises(ZeroDivisionError, lambda : Vector3(1,1,1).elementwise() // self.zeroVec)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
1449 self.assertRaises(ZeroDivisionError, lambda : Vector3(1,1,1).elementwise() % self.zeroVec)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
1450 self.assertRaises(ZeroDivisionError, lambda : 2 / self.zeroVec.elementwise())
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
1451 self.assertRaises(ZeroDivisionError, lambda : 2 // self.zeroVec.elementwise())
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
1452 self.assertRaises(ZeroDivisionError, lambda : 2 % self.zeroVec.elementwise())
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
1453
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
1454
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1455 def test_slerp(self):
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
1456 self.assertRaises(ValueError, lambda : self.zeroVec.slerp(self.v1, .5))
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
1457 self.assertRaises(ValueError, lambda : self.v1.slerp(self.zeroVec, .5))
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
1458 self.assertRaises(ValueError,
2006
6a532cb218dd removed iterator versions of vector.slerp() and .lerp() in favour of a parametrized version.
lorenz
parents: 1997
diff changeset
1459 lambda : self.zeroVec.slerp(self.zeroVec, .5))
6a532cb218dd removed iterator versions of vector.slerp() and .lerp() in favour of a parametrized version.
lorenz
parents: 1997
diff changeset
1460 steps = 10
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1461 angle_step = self.e1.angle_to(self.e2) / steps
2007
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
1462 for i, u in ((i, self.e1.slerp(self.e2, i/float(steps))) for i in range(steps+1)):
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1463 self.assertAlmostEqual(u.length(), 1)
2006
6a532cb218dd removed iterator versions of vector.slerp() and .lerp() in favour of a parametrized version.
lorenz
parents: 1997
diff changeset
1464 self.assertAlmostEqual(self.e1.angle_to(u), i * angle_step)
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1465 self.assertEqual(u, self.e2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1466
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1467 v1 = Vector3(100, 0, 0)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1468 v2 = Vector3(0, 10, 7)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1469 radial_factor = v2.length() / v1.length()
2007
07d8a41f1593 reimplemented Vector2(Vector2().__repr__()) to support unicode (and thus Py3)
lorenz
parents: 2006
diff changeset
1470 for i, u in ((i, v1.slerp(v2, -i/float(steps))) for i in range(steps+1)):
2006
6a532cb218dd removed iterator versions of vector.slerp() and .lerp() in favour of a parametrized version.
lorenz
parents: 1997
diff changeset
1471 self.assertAlmostEqual(u.length(), (v2.length() - v1.length()) * (float(i)/steps) + v1.length())
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1472 self.assertEqual(u, v2)
2024
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
1473 self.assertEqual(v1.slerp(v1, .5), v1)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
1474 self.assertEqual(v2.slerp(v2, .5), v2)
d6f7fe87a2b1 huge cleanup of math module
lorenz
parents: 2013
diff changeset
1475 self.assertRaises(ValueError, lambda : v1.slerp(-v1, 0.5))
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1476
2006
6a532cb218dd removed iterator versions of vector.slerp() and .lerp() in favour of a parametrized version.
lorenz
parents: 1997
diff changeset
1477 def test_lerp(self):
6a532cb218dd removed iterator versions of vector.slerp() and .lerp() in favour of a parametrized version.
lorenz
parents: 1997
diff changeset
1478 """TODO"""
6a532cb218dd removed iterator versions of vector.slerp() and .lerp() in favour of a parametrized version.
lorenz
parents: 1997
diff changeset
1479 pass
6a532cb218dd removed iterator versions of vector.slerp() and .lerp() in favour of a parametrized version.
lorenz
parents: 1997
diff changeset
1480
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1481 def test_spherical(self):
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1482 v = Vector3()
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1483 v.from_spherical(self.v1.as_spherical())
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1484 self.assertEqual(self.v1, v)
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
1485 self.assertEqual(self.e1.as_spherical(), (1, 90, 0))
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
1486 self.assertEqual(self.e2.as_spherical(), (1, 90, 90))
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1487 self.assertEqual(self.e3.as_spherical(), (1, 0, 0))
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
1488 self.assertEqual((2 * self.e2).as_spherical(), (2, 90, 90))
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1489 self.assertRaises(TypeError, lambda : v.from_spherical((None, None, None)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1490 self.assertRaises(TypeError, lambda : v.from_spherical("abc"))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1491 self.assertRaises(TypeError, lambda : v.from_spherical((None, 1, 2)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1492 self.assertRaises(TypeError, lambda : v.from_spherical((1, 2, 3, 4)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1493 self.assertRaises(TypeError, lambda : v.from_spherical((1, 2)))
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1494 self.assertRaises(TypeError, lambda : v.from_spherical(1, 2, 3))
2035
28685c84e251 as/from_polar/spherical now also use degrees instead of radians
lorenz
parents: 2024
diff changeset
1495 v.from_spherical((.5, 90, 90))
1994
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1496 self.assertEqual(v, .5 * self.e2)
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1497
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1498
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1499
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1500
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1501
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1502
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1503 if __name__ == '__main__':
f51d9315c3e5 Merged the branch math_module to trunk.
lorenz
parents:
diff changeset
1504 unittest.main()