Source code for BigDFT.wahba
from numpy import *
from math import sqrt
from futile.Utils import write as safe_print
[docs]def apply_R(R,A):
"Apply the rotation on the set of vectors A"
A2 = R*A.T
A2 = A2.T
return A2
[docs]def apply_t(t,A):
"Apply a translation on the set of vectors A"
n=A.shape[0]
return A + tile(t, (1,n)).T
[docs]def apply_Rt(R,t,A):
"Rotate the element and apply the translation on the rotated vector"
RA=apply_R(R,A)
return apply_t(t,RA)
# Input: expects Nx3 matrix of points
# Returns R,t
# R = 3x3 rotation matrix
# t = 3x1 column vector
# Test with random data
if __name__ == '__main__':
# Random rotation and translation
R = mat(random.rand(3,3))
t = mat(random.rand(3,1))
# make R a proper rotation matrix, force orthonormal
U, S, Vt = linalg.svd(R)
R = U*Vt
# remove reflection
if linalg.det(R) < 0:
Vt[2,:] *= -1
R = U*Vt
# number of points
n = 10
A = mat(random.rand(n,3));
B = R*A.T + tile(t, (1, n))
B = B.T;
# recover the transformation
ret_R, ret_t = rigid_transform_3D(A, B)
A2 = (ret_R*A.T) + tile(ret_t, (1, n))
A2 = A2.T
# Find the error
err = A2 - B
err = multiply(err, err)
err = sum(err)
rmse = sqrt(err/n);
safe_print( "Points A")
safe_print( A)
safe_print( "")
safe_print( "Points B")
safe_print( B)
safe_print( "")
safe_print( "Rotation")
safe_print( R)
safe_print( "")
safe_print( "Translation")
safe_print( t)
safe_print( "")
safe_print( "RMSE:", rmse)
safe_print( "If RMSE is near zero, the function is correct!")