This repository has been archived by the owner on Jan 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
88 lines (59 loc) · 2.6 KB
/
tests.py
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
83
84
85
86
87
88
import unittest
import numpy
import numpy.random
import transformations
from transformations import Affine
EPSILON = 1e-9
class TestAffine(unittest.TestCase):
def randomVector(self):
return numpy.random.normal(size=(3,))
def randomMatrix(self):
return numpy.random.normal(size=(3,3))
def assertVecsEqual( self, a, b ):
a = numpy.ravel(a)
b = numpy.ravel(b)
self.assertTrue( numpy.linalg.norm(a-b) < EPSILON )
def test_identity(self):
vec = self.randomVector()
t = Affine.identity()
self.assertVecsEqual( vec, t(vec) )
self.assertTrue( t.is_identity() )
self.assertFalse( Affine(translation=(1,2,3)).is_identity() )
self.assertFalse( Affine(rotation_axis='y', rotation_deg=10).is_identity() )
rotCcw = Affine(rotation_axis=(1,0,0), rotation_deg=90)
rotCw = Affine(rotation_axis='x', rotation_deg=-90)
self.assertTrue(rotCcw(rotCw).is_identity())
def test_translation(self):
t = Affine(translation=(1,2,3))
vec = (0,-1,-3)
r = t(vec)
self.assertVecsEqual( r, (1,1,0) )
self.assertVecsEqual( t * vec, (1,1,0) )
def test_inverse(self):
t = Affine(linear=self.randomMatrix(), translation=self.randomVector())
inv = t.inverse()
self.assertTrue(inv(t).is_identity())
self.assertTrue((inv * t).is_identity())
def test_roto_translation(self):
t = Affine(rotation_axis='z', rotation_deg=90, translation=(2,0,0))
self.assertVecsEqual( t((1,0,0)), (2,1,0) )
def test_scaling(self):
t = Affine(scaling=(1,2,3))
self.assertVecsEqual( t((1,1,1)), (1,2,3) )
t = Affine(scaling=10)
self.assertVecsEqual( t((1,2,3)), (10,20,30) )
def test_orthogonality(self):
self.assertTrue( Affine.identity().is_orthogonal() )
self.assertTrue( Affine(
rotation_axis=self.randomVector(),
rotation_deg=numpy.random.rand()*360).is_orthogonal() )
self.assertFalse( Affine(scaling=2.0).is_orthogonal() )
def test_rotation_aligning_vectors(self):
vec1 = (2,0,0)
vec2 = (0,3,0)
mat = transformations.rotation_aligning_vectors(vec1, vec2)
self.assertVecsEqual( numpy.dot(mat, vec1), (0,2,0) )
mat0 = transformations.rotation_aligning_vectors(vec1, vec1)
self.assertTrue( Affine(linear=mat0).is_identity() )
if __name__ == '__main__':
unittest.main()