File: test_matrix.py

package info (click to toggle)
python-box2d 2.3.2~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,596 kB
  • ctags: 5,116
  • sloc: python: 14,384; cpp: 13,393; makefile: 8; sh: 6
file content (82 lines) | stat: -rw-r--r-- 3,275 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
from Box2D import b2Vec2, b2Vec3, b2Mat22, b2Mat33

class testMatrix (unittest.TestCase):
    def checkAlmostEqual(self, v1, v2, msg):
        for a, b in zip(v1, v2):
            self.assertAlmostEqual(a, b, places=3,
                    msg="%s, a=%f b=%f from %s, %s" % (msg, a, b, v1, v2))

    def test_mat22_identity(self):
        i2 = b2Mat22()
        self.checkAlmostEqual(i2.col1, (1.0, 0.0), msg='mat22 col1')
        self.checkAlmostEqual(i2.col2, (0.0, 1.0), msg='mat22 col2')

    def test_matrix(self):
        x, y, z = 1.0, 2.0, 3.0
        v2 = b2Vec2(x, y)

        self.checkAlmostEqual(v2.skew, (-v2.y, v2.x), msg='skew')

        m2 = b2Mat22((x, y), (y, x))
        # Note that you can't do:
        # m2 = b2Mat22(col1=(x, y), col2=(y, x))
        # as SWIG will not allow the kwargs option to be used when there are multiple constructors

        m = m2 + m2
        self.checkAlmostEqual(m.col1, (x+x, y+y), msg='b2Mat22 +')
        self.checkAlmostEqual(m.col2, (y+y, x+x), msg='b2Mat22 +')
        m = m2 - m2
        self.checkAlmostEqual(m.col1, (0,0), msg='b2Mat22 -')
        self.checkAlmostEqual(m.col2, (0,0), msg='b2Mat22 -')
        # x y  * x
        # y x    y
        v = m2 * v2
        self.checkAlmostEqual(v, (x*x + y*y, y*x + y*x), msg='b2Mat22 * b2Vec2')
        i=m2.inverse
        i=m2.angle
        m = m2 * m2
        self.checkAlmostEqual(m.col1, (x*x + y*y, y*x + y*x), msg='b2Mat22 * b2Mat22')
        self.checkAlmostEqual(m.col2, (x*y + y*x, y*y + x*x), msg='b2Mat22 * b2Mat22')

        m2 += m2
        self.checkAlmostEqual(m2.col1, (x+x, y+y), msg='b2Mat22 +=')
        self.checkAlmostEqual(m2.col2, (y+y, x+x), msg='b2Mat22 +=')
        m2 -= m2
        self.checkAlmostEqual(m2.col1, (0,0), msg='b2Mat22 -=')
        self.checkAlmostEqual(m2.col2, (0,0), msg='b2Mat22 -=')

    def test_mat33_identity(self):
        i3 = b2Mat33()
        self.checkAlmostEqual(i3.col1, (1.0, 0.0, 0.0), msg='mat33 col1')
        self.checkAlmostEqual(i3.col2, (0.0, 1.0, 0.0), msg='mat33 col2')
        self.checkAlmostEqual(i3.col3, (0.0, 0.0, 1.0), msg='mat33 col3')

    def test_mat33(self):
        x, y, z = 1.0, 2.0, 3.0
        v3 = b2Vec3(x, y, z)
        m3 = b2Mat33((x, y, z), (z, y, x), (y, x, z))
        m = m3 + m3
        self.checkAlmostEqual(m.col1, (x+x, y+y, z+z), msg='b2Mat33 +')
        self.checkAlmostEqual(m.col2, (z+z, y+y, x+x), msg='b2Mat33 +')
        self.checkAlmostEqual(m.col3, (y+y, x+x, z+z), msg='b2Mat33 +')
        m = m3 - m3
        self.checkAlmostEqual(m.col1, (0,0,0), msg='b2Mat33 -')
        self.checkAlmostEqual(m.col2, (0,0,0), msg='b2Mat33 -')
        self.checkAlmostEqual(m.col3, (0,0,0), msg='b2Mat33 -')
        m3 += m3
        self.checkAlmostEqual(m3.col1, (x+x, y+y, z+z), msg='b2Mat33 +=')
        self.checkAlmostEqual(m3.col2, (z+z, y+y, x+x), msg='b2Mat33 +=')
        self.checkAlmostEqual(m3.col3, (y+y, x+x, z+z), msg='b2Mat33 +=')
        m3 -= m3
        self.checkAlmostEqual(m3.col1, (0,0,0), msg='b2Mat33 -=')
        self.checkAlmostEqual(m3.col2, (0,0,0), msg='b2Mat33 -=')
        self.checkAlmostEqual(m3.col3, (0,0,0), msg='b2Mat33 -=')


if __name__ == '__main__':
    unittest.main()