Skip to content

Instantly share code, notes, and snippets.

@Mahdisadjadi
Last active August 8, 2018 01:13
Show Gist options
  • Save Mahdisadjadi/652a6331ae65be54fee9fed987ddb328 to your computer and use it in GitHub Desktop.
Save Mahdisadjadi/652a6331ae65be54fee9fed987ddb328 to your computer and use it in GitHub Desktop.
Alignment of two sets of points on the 2D plane
def pointAlignment(ref, move):
"""
Mahd Sadjadi (2017)
This is a function which takes two sets of
points each with N points (both inputs are assumd to be
numpy arrays of N*2 shape). The first input is
the reference grid and the second is the point set to
be aligned with the reference.
This function shifts and rotate points to
make the two sets aligned and returns the
result (with the shape N*2).
"""
N = ref.shape[0] # number of atoms
IM = np.ones(N) #array of ones
XG,YG,ZG = ref.T # ref coordinates
XM,YM,ZM = move.T # move coordinates
XGA,YGA,ZGA = np.mean(ref,axis=0) # center of mass
XMA,YMA,ZMA = np.mean(move,axis=0) # center of mass
XGS = XG - XGA * IM # std xg
YGS = YG - YGA * IM # std yg
XMS = XM - XMA * IM
YMS = YM - YMA * IM
A = np.sum(XGS * XMS + YGS * YMS)/N
B = np.sum(XGS * YMS - YGS * XMS)/N
R = np.sqrt(A**2 + B**2)
sin = B/R
cos = A/R
a = XGA * cos - YGA * sin - XMA
b = XGA * sin + YGA * cos - YMA
theta = np.arctan(B/A)
deltax = (a * cos + b * sin)*IM - (1-cos)*XM + sin*YM
deltay = (-a * sin + b * cos)*IM - sin*XM -(1-cos)*YM
results = np.column_stack((XM+deltax,YM+deltay,ZM))
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment