Advertisement
Guest User

Untitled

a guest
May 12th, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import random
  3. import numpy as np
  4. import numpy.matlib as npmatlib
  5. import numpy.linalg as nplinalg
  6. from math import sin, cos, sqrt
  7.  
  8. def random_rotation(seed=None):
  9.     if seed is not None:
  10.         rng = random.Random(seed)
  11.     else:
  12.         rng = random.Random()
  13.    
  14.     # Generate an (not too short) axis and angle
  15.     n = 0.0
  16.     while n < 0.1:
  17.         t = rng.random() * 3.14159265358979323846
  18.         x = rng.random()
  19.         y = rng.random()
  20.         z = rng.random()
  21.         n = x*x + y*y + z*z
  22.     # and scale the axis to unit length.
  23.     n = sqrt(n)
  24.     x = x / n
  25.     y = y / n
  26.     z = z / n
  27.  
  28.     # Return rotation matrix for this axis and angle.
  29.     s = sin(t)
  30.     c = cos(t)
  31.     u = 1.0 - c
  32.     R = np.array([[ c + x*x*u,   x*y*u - z*s, x*z*u + y*s ],
  33.                   [ y*x*u + z*s, c + y*y*u,   y*z*u - x*s ],
  34.                   [ z*x*u - y*s, z*y*u + x*s, c + z*z*u   ]])
  35.     return np.asmatrix(R)
  36.  
  37.  
  38. if __name__ == '__main__':
  39.     # We'll apply a "secret" random rotation S,
  40.     S = random_rotation()
  41.     print("Actual rotation matrix used:")
  42.     print("    [ %9.6f %9.6f %9.6f ]" % (S[0,0], S[0,1], S[0,2]))
  43.     print("    [ %9.6f %9.6f %9.6f ]" % (S[1,0], S[1,1], S[1,2]))
  44.     print("    [ %9.6f %9.6f %9.6f ]" % (S[2,0], S[2,1], S[2,2]))
  45.     print("")
  46.  
  47.     # to a hundred random 3D points b,
  48.     b = npmatlib.rand(100, 3)
  49.  
  50.     # as a:
  51.     a = b.dot(S)
  52.  
  53.     # To solve for S knowing only a and b, we first build B:
  54.     B = np.asmatrix(npmatlib.zeros((3, 3)))
  55.     for i in range(0, len(b)):
  56.         # B += np.dot((b[i]).T, (a[i]))
  57.         for row in range(0, 3):
  58.             for col in range(0, 3):
  59.                 B[row, col] += b[i,row] * a[i,col]
  60.    
  61.  
  62.     # Then we do Singular Value Decomposition on B.
  63.     u, s, vh = nplinalg.svd(B, full_matrices=True)
  64.  
  65.     # Next, we construct T.
  66.     T = npmatlib.eye(3)
  67.     T[2,2] = nplinalg.det(u) * nplinalg.det(vh.T)
  68.  
  69.     # Finally, we construct the solution R.
  70.     R = np.asmatrix(nplinalg.multi_dot([u, T, vh]))
  71.     print("Rotation matrix found:")
  72.     print("    [ %9.6f %9.6f %9.6f ]" % (R[0,0], R[0,1], R[0,2]))
  73.     print("    [ %9.6f %9.6f %9.6f ]" % (R[1,0], R[1,1], R[1,2]))
  74.     print("    [ %9.6f %9.6f %9.6f ]" % (R[2,0], R[2,1], R[2,2]))
  75.     print("")
  76.  
  77.     # For the output, we also print the rotated coordinates.
  78.     r = b.dot(S)
  79.  
  80.     print("Before   After   UsingSolution")
  81.     for i in range(0, len(b)):
  82.         print("%9.6f %9.6f %9.6f   %9.6f %9.6f %9.6f   %9.6f %9.6f %9.6f" %
  83.               (b[i,0],b[i,1],b[i,2], a[i,0],a[i,1],a[i,2], r[i,0],r[i,1],r[i,2]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement