-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_functions.py
74 lines (59 loc) · 1.6 KB
/
pg_functions.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
import numpy as np
from scipy.sparse import coo_matrix
from scipy.sparse import csc_matrix
from scipy.sparse import csr_matrix
from scipy.sparse.linalg import spsolve
from numpy.linalg import inv
def t2v(A):
"""
Homogeneous transformation to vector
A = H = [R d
0 1]
Rotation matrix:
R = [+cos(theta), -sin(theta)
+sin(theta), +cos(theta)]
translation vector:
d = [x y]'
"""
v = np.zeros((3,1), dtype = np.float64)
v[0] = A[0,2] # x
v[1] = A[1,2] # y
v[2] = np.arctan2(A[1,0], A[0,0]) # theta
return v
def v2t(v):
"""
Vector to Homogeneous transformation
A = H = [R d
0 1]
Rotation matrix:
R = [+cos(theta), -sin(theta)
+sin(theta), +cos(theta)]
translation vector:
d = [x y]'
"""
x = v[0]
y = v[1]
theta = v[2]
A = np.array([[+np.cos(theta), -np.sin(theta), x],
[+np.sin(theta), +np.cos(theta), y],
[ 0, 0, 1]])
return A
def solve(H, b, sparse_solve):
"""
Solve sparse linear system H * dX = -b
"""
# Keep first node fixed
H[:3,:3] += np.eye(3)
if sparse_solve:
# Transformation to sparse matrix form
H_sparse = csr_matrix(H)
# Solve sparse system
dX = spsolve(H_sparse, b)
else:
# Solve linear system
dX = np.linalg.solve(H, b)
# Keep first node fixed
dX[:3] = [0, 0, 0]
# Check NAN
dX[np.isnan(dX)] = 0
return dX