summaryrefslogtreecommitdiffhomepage
path: root/host/inter/trans_matrix.py
blob: dcaab1349c978c8626ad5a8d6cda826133983db9 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# inter - Robot simulation interface. {{{
#
# Copyright (C) 2008 Nicolas Schodet
#
# APBTeam:
#        Web: http://apbteam.org/
#      Email: team AT apbteam DOT org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# }}}
"""TransMatrix utility."""
from math import sin, cos, sqrt, atan2

class TransMatrix:
    """Define a matrix to be used for transformations on the plane.
    
    This is a "special" kind of matrix, because the last column is omitted as
    it is always (0, 0, 1)."""

    IDENTITY = ((1, 0), (0, 1), (0, 0))

    def __init__ (self, *m):
	"""Initialise the matrix, with optional initial value.
	
	>>> TransMatrix ()
	((1, 0), (0, 1), (0, 0))
	>>> TransMatrix ((1, 2), (3, 4), (5, 6))
	((1, 2), (3, 4), (5, 6))
	"""
	if m:
	    assert len (m) == 3
	    for i in m:
		assert len (i) == 2
	    self.matrix = m
	else:
	    self.matrix = self.IDENTITY

    def identity (self):
	"""Set to identity.

	>>> a = TransMatrix ()
	>>> a.translate ((2, 3))
	>>> a.identity (); a
	((1, 0), (0, 1), (0, 0))
	"""
	self.matrix = self.IDENTITY

    def rotate (self, angle):
	"""Transform the current matrix to do a rotation.
	
	>>> from math import pi
	>>> a = TransMatrix ()
	>>> a.rotate (pi / 3); a         # doctest: +ELLIPSIS
	((0.5..., 0.866...), (-0.866..., 0.5...), (0.0, 0.0))
	"""
	s = sin (angle)
	c = cos (angle)
	m = TransMatrix ((c, s), (-s, c), (0, 0))
	self *= m

    def translate (self, by):
	"""Transform the current matrix to do a translation.
	
	>>> a = TransMatrix ()
	>>> a.translate ((2, 3)); a
	((1, 0), (0, 1), (2, 3))
	"""
	m = TransMatrix ((1, 0), (0, 1), by)
	self *= m

    def scale (self, factor):
	"""Transform the current matrix to do a scaling.
	
	>>> a = TransMatrix ()
	>>> a.scale (2); a
	((2, 0), (0, 2), (0, 0))
	"""
	m = TransMatrix ((factor, 0), (0, factor), (0, 0))
	self *= m

    def __imul__ (self, other):
	"""Multiply by an other matrix.
	
	>>> a = TransMatrix ((1, 0), (0, 1), (1, 0))
	>>> b = TransMatrix ((0, 1), (1, 0), (0, 1))
	>>> a *= b; a
	((0, 1), (1, 0), (0, 2))
	"""
	s = self.matrix
	o = other.matrix
	self.matrix = (
		(s[0][0] * o[0][0] + s[0][1] * o[1][0],
		    s[0][0] * o[0][1] + s[0][1] * o[1][1]),
		(s[1][0] * o[0][0] + s[1][1] * o[1][0],
		    s[1][0] * o[0][1] + s[1][1] * o[1][1]),
		(s[2][0] * o[0][0] + s[2][1] * o[1][0] + o[2][0],
		    s[2][0] * o[0][1] + s[2][1] * o[1][1] + o[2][1]))
	return self

    def apply (self, *args):
	"""Apply (multiply) the matrix to all the given arguments.

	>>> m = TransMatrix ((1, 2), (4, 8), (16, 32))
	>>> m.apply ((1, 0))
	(17, 34)
	>>> m.apply ((0, 1), (1, 1))
	((20, 40), (21, 42))
	"""
	r = tuple (
	    (i[0] * self.matrix[0][0] + i[1] * self.matrix[1][0]
		+ self.matrix[2][0],
		i[0] * self.matrix[0][1] + i[1] * self.matrix[1][1]
		+ self.matrix[2][1])
	    for i in args)
	if len (args) == 1:
	    return r[0]
	else:
	    return r

    def apply_angle (self, angle):
	"""Apply the matrix to an angle.

	>>> from math import pi
	>>> a = TransMatrix ()
	>>> a.rotate (pi / 6)
	>>> a.translate ((2, 3))
	>>> a.scale (4)
	>>> a.apply_angle (pi / 6), pi / 3    # doctest: +ELLIPSIS
	(1.0471..., 1.0471...)
	"""
	o, m = self.apply ((0, 0), (cos (angle), sin (angle)))
	v = (m[0] - o[0], m[1] - o[1])
	vl = sqrt (v[0] ** 2 + v[1] ** 2)
	v = (v[0] / vl, v[1] / vl)
	return atan2 (v[1], v[0])

    def apply_distance (self, distance):
	"""Apply the matrix to a distance.

	>>> from math import pi
	>>> a = TransMatrix ()
	>>> a.rotate (pi / 6)
	>>> a.translate ((2, 3))
	>>> a.scale (4)
	>>> round (a.apply_distance (2))
	8.0
	"""
	o, m = self.apply ((0, 0), (distance, 0))
	v = (m[0] - o[0], m[1] - o[1])
	vl = sqrt (v[0] ** 2 + v[1] ** 2)
	return vl

    def __repr__ (self):
	return self.matrix.__repr__ ()

def _test ():
    import doctest
    doctest.testmod ()

if __name__ == '__main__':
    _test()