FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ThinkRubyBuild/python_thinking/Frame.py at master · learnbyexample/ThinkRubyBuild · GitHub
learnbyexample
/
ThinkRubyBuild
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
32
Code
Issues
7
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
ThinkRubyBuild
/
python_thinking
/
Frame.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
311 lines (230 loc) · 7.52 KB
Breadcrumbs
ThinkRubyBuild
/
python_thinking
/
Frame.py
Copy path
File metadata and controls
311 lines (230 loc) · 7.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""Exploration of Vectors and Frames.
Copyright 2012 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""
from
__future__
import
print_function
,
division
import
sys
import
numpy
import
math
def
println
(
s
):
print
(
s
,
'
\n
'
)
class
FrameError
(
ValueError
):
"""Represents a problem with frame of reference."""
class
Vector
:
def
__init__
(
self
,
array
,
frame
=
None
):
"""A vector is an array of coordinates and a frame of reference.
array:
frame: Frame object
"""
self
.
array
=
array
self
.
frame
=
frame
def
__str__
(
self
):
if
self
.
frame
==
None
:
return
'^{O}%s'
%
(
str
(
self
.
array
), )
else
:
return
'^{%s}%s'
%
(
str
(
self
.
frame
),
str
(
self
.
array
))
def
__add__
(
self
,
other
):
if
self
.
frame
!=
other
.
frame
:
raise
FrameError
(
"Vectors must be relative to the same frame."
)
return
Vector
(
self
.
array
+
other
.
array
,
self
.
frame
)
@
staticmethod
def
from_list
(
t
,
frame
=
None
):
"""Makes a vector from a list.
t: list of coordinates
frame: reference Frame
"""
return
Vector
(
numpy
.
array
(
t
),
frame
)
class
Rotation
:
def
__init__
(
self
,
array
):
self
.
array
=
array
def
__str__
(
self
):
return
'Rotation
\n
%s'
%
str
(
self
.
array
)
def
__neg__
(
self
):
return
Rotation
(
-
self
.
array
)
def
__mul__
(
self
,
other
):
"""Apply the rotation to a Vector."""
return
numpy
.
dot
(
self
.
array
,
other
.
array
)
__call__
=
__mul__
@
staticmethod
def
from_axis
(
axis
,
theta
):
x
,
y
,
z
=
numpy
.
ravel
(
axis
.
array
)
c
=
math
.
cos
(
theta
)
u
=
1.0
-
c
s
=
math
.
sqrt
(
1.0
-
c
*
c
)
xu
,
yu
,
zu
=
x
*
u
,
y
*
u
,
z
*
u
v1
=
[
x
*
xu
+
c
,
x
*
yu
-
z
*
s
,
x
*
zu
+
y
*
s
]
v2
=
[
x
*
yu
+
z
*
s
,
y
*
yu
+
c
,
y
*
zu
-
x
*
s
]
v3
=
[
x
*
zu
-
y
*
s
,
y
*
zu
+
x
*
s
,
z
*
zu
+
c
]
return
Rotation
(
numpy
.
array
([
v1
,
v2
,
v3
]))
def
to_axis
(
self
):
# return the equivalent angle-axis as (khat, theta)
pass
def
transpose
(
self
):
return
Rotation
(
numpy
.
transpose
(
self
.
array
))
inverse
=
transpose
class
Transform
:
"""Represents a transform from one Frame to another."""
def
__init__
(
self
,
rot
,
org
,
source
=
None
):
"""Instantiates a Transform.
rot: Rotation object
org: origin Vector
source: source Frame
"""
self
.
rot
=
rot
self
.
org
=
org
self
.
dest
=
org
.
frame
self
.
source
=
source
self
.
source
.
add_transform
(
self
)
def
__str__
(
self
):
"""Returns a string representation of the Transform."""
if
self
.
dest
==
None
:
return
'%s'
%
self
.
source
.
name
return
'_{%s}^{O}T'
%
self
.
source
.
name
else
:
return
'_{%s}^{%s}T'
%
(
self
.
source
.
name
,
self
.
dest
.
name
)
def
__mul__
(
self
,
other
):
"""Applies a Transform to a Vector or Transform."""
if
isinstance
(
other
,
Vector
):
return
self
.
mul_vector
(
other
)
if
isinstance
(
other
,
Transform
):
return
self
.
mul_transform
(
other
)
__call__
=
__mul__
def
mul_vector
(
self
,
p
):
"""Applies a Transform to a Vector.
p: Vector
Returns: Vector
"""
if
p
.
frame
!=
self
.
source
:
raise
FrameError
(
"The frame of the vector must be the source of the transform"
)
return
Vector
(
self
.
rot
*
p
,
self
.
dest
)
+
self
.
org
def
mul_transform
(
self
,
other
):
"""Applies a Transform to another Transform.
other: Transform
Returns Transform
"""
if
other
.
dest
!=
self
.
source
:
raise
FrameError
(
"This frames source must be the other frame's destination."
)
rot
=
Rotation
(
self
.
rot
*
other
.
rot
)
t
=
Transform
(
rot
,
self
*
other
.
org
,
other
.
source
)
return
t
def
inverse
(
self
):
"""Computes the inverse transform.
Returns: Transform
"""
irot
=
self
.
rot
.
inverse
()
iorg
=
Vector
(
-
(
irot
*
self
.
org
),
self
.
source
)
t
=
Transform
(
irot
,
iorg
,
self
.
dest
)
return
t
class
Frame
:
"""Represents a frame of reference."""
# list of Frames
roster
=
[]
def
__init__
(
self
,
name
):
"""Instantiate a Frame.
name: string
"""
self
.
name
=
name
self
.
transforms
=
{}
Frame
.
roster
.
append
(
self
)
def
__str__
(
self
):
return
self
.
name
def
add_transform
(
self
,
transform
):
"""A frames is defined by a Transform relative to another Frame.
transform: Transform object
"""
if
transform
.
source
!=
self
:
raise
FrameError
(
"Source of the transform must be this Frame."
)
if
transform
.
dest
:
self
.
transforms
[
transform
.
dest
]
=
transform
def
dests
(
self
):
"""Returns a list of the Frames we know how to Transform to."""
return
self
.
transforms
.
keys
()
class
Vertex
:
"""Represents a node in a graph."""
def
__init__
(
self
,
frame
):
self
.
frame
=
frame
self
.
dist
=
1000000
self
.
out
=
[]
def
__str__
(
self
):
return
'%s %d'
%
(
self
.
frame
.
name
,
self
.
dist
)
def
shortest_path
(
start
,
frames
):
"""For a given list of frames and a starting frame,
find the shortest path of transforms from the
starting frame to all other frames.
The 'distance' is the number of inverse transformations
that must be calculated.
The result is a dictionary of vertices, where
each vertex is labeled with the frame it corresponds
to, the distance from the starting frame, and the prev
frame along the path from start. """
map
=
dict
([(
f
,
Vertex
(
f
))
for
f
in
frames
])
length
=
{}
for
v
in
map
.
values
():
for
dest
in
v
.
frame
.
transforms
:
w
=
map
[
dest
]
v
.
out
.
append
(
w
)
length
[(
v
,
w
)]
=
0
w
.
out
.
append
(
v
)
length
[(
w
,
v
)]
=
1
s
=
map
[
start
]
s
.
dist
=
0
queue
=
[
s
]
while
queue
:
v
=
queue
.
pop
()
for
w
in
v
.
out
:
d
=
v
.
dist
+
length
[(
v
,
w
)]
if
d
<
w
.
dist
:
w
.
dist
=
d
w
.
prev
=
v
if
w
not
in
queue
:
queue
.
append
(
w
)
return
map
def
print_shortest_path
(
map
):
for
source
,
v
in
map
.
items
():
try
:
v
.
prev
print
(
source
,
v
.
dist
,
v
.
prev
.
frame
)
except
:
print
(
source
,
v
.
dist
)
def
print_length
(
length
):
for
v
,
w
in
length
:
print
(
v
.
frame
.
name
,
w
.
frame
.
name
,
length
[(
v
,
w
)])
print
()
def
main
(
name
):
theta
=
math
.
pi
/
2
#v_o = Vector.from_list([0, 0, 0], None)
origin
=
Frame
(
'O'
)
#o_trans = Transform(None, v_o, origin)
xhat
=
Vector
.
from_list
([
1
,
0
,
0
],
origin
)
rx
=
Rotation
.
from_axis
(
xhat
,
theta
)
a
=
Frame
(
'A'
)
t_ao
=
Transform
(
rx
,
xhat
,
a
)
yhat
=
Vector
.
from_list
([
0
,
1
,
0
],
a
)
ry
=
Rotation
.
from_axis
(
yhat
,
theta
)
b
=
Frame
(
'B'
)
t_ba
=
Transform
(
ry
,
yhat
,
b
)
zhat
=
Vector
.
from_list
([
0
,
0
,
1
],
b
)
rz
=
Rotation
.
from_axis
(
zhat
,
theta
)
c
=
Frame
(
'C'
)
t_cb
=
Transform
(
rz
,
zhat
,
c
)
p_c
=
Vector
.
from_list
([
1
,
1
,
1
],
c
)
println
(
p_c
)
p_b
=
t_cb
(
p_c
)
println
(
p_b
)
p_a
=
t_ba
(
p_b
)
println
(
p_a
)
p
=
t_ao
(
p_a
)
println
(
p
)
map
=
shortest_path
(
origin
,
Frame
.
roster
)
print_shortest_path
(
map
)
cbao
=
t_ao
(
t_ba
(
t_cb
))
p
=
cbao
(
p_c
)
println
(
p
)
inv
=
cbao
.
inverse
()
p_c
=
inv
(
p
)
println
(
p_c
)
map
=
shortest_path
(
origin
,
Frame
.
roster
)
print_shortest_path
(
map
)
if
__name__
==
'__main__'
:
main
(
*
sys
.
argv
)
Back
|
FazBrowse Home
|
New Git URL