1 | #!/usr/bin/env python |
---|
2 | from __future__ import print_function, division |
---|
3 | |
---|
4 | import sys |
---|
5 | |
---|
6 | import numpy as np |
---|
7 | from numpy import pi, cos, sin, sqrt, exp, degrees, radians |
---|
8 | from scipy.optimize import fmin |
---|
9 | |
---|
10 | # Definition of rotation matrices comes from wikipedia: |
---|
11 | # https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations |
---|
12 | def Rx(angle): |
---|
13 | """Construct a matrix to rotate points about *x* by *angle* degrees.""" |
---|
14 | a = radians(angle) |
---|
15 | R = [[1, 0, 0], |
---|
16 | [0, +cos(a), -sin(a)], |
---|
17 | [0, +sin(a), +cos(a)]] |
---|
18 | return np.matrix(R) |
---|
19 | |
---|
20 | def Ry(angle): |
---|
21 | """Construct a matrix to rotate points about *y* by *angle* degrees.""" |
---|
22 | a = radians(angle) |
---|
23 | R = [[+cos(a), 0, +sin(a)], |
---|
24 | [0, 1, 0], |
---|
25 | [-sin(a), 0, +cos(a)]] |
---|
26 | return np.matrix(R) |
---|
27 | |
---|
28 | def Rz(angle): |
---|
29 | """Construct a matrix to rotate points about *z* by *angle* degrees.""" |
---|
30 | a = radians(angle) |
---|
31 | R = [[+cos(a), -sin(a), 0], |
---|
32 | [+sin(a), +cos(a), 0], |
---|
33 | [0, 0, 1]] |
---|
34 | return np.matrix(R) |
---|
35 | |
---|
36 | |
---|
37 | def transform_angles(theta, phi, psi, qx=0.1, qy=0.1): |
---|
38 | Rold = Rz(-psi)*Rx(theta)*Ry(-(90 - phi)) |
---|
39 | cost = lambda p: np.linalg.norm(Rz(-p[2])*Ry(-p[0])*Rz(-p[1]) - Rold) |
---|
40 | result = fmin(cost, (theta, phi, psi)) |
---|
41 | theta_p, phi_p, psi_p = result |
---|
42 | Rnew = Rz(-psi_p)*Ry(-theta_p)*Rz(-phi_p) |
---|
43 | |
---|
44 | print("old: theta, phi, psi =", ", ".join(str(v) for v in (theta, phi, psi))) |
---|
45 | print("new: theta, phi, psi =", ", ".join(str(v) for v in result)) |
---|
46 | try: |
---|
47 | point = np.matrix([qx, qy, [0]*len(qx)]) |
---|
48 | except TypeError: |
---|
49 | point = np.matrix([[qx],[qy],[0]]) |
---|
50 | for p in point.T: |
---|
51 | print("q abc old for", p, (Rold*p.T).T) |
---|
52 | print("q abc new for", p, (Rnew*p.T).T) |
---|
53 | |
---|
54 | if __name__ == "__main__": |
---|
55 | theta, phi, psi = (float(v) for v in sys.argv[1:]) |
---|
56 | #transform_angles(theta, phi, psi) |
---|
57 | transform_angles(theta, phi, psi, qx=-0.017, qy=0.035) |
---|