Skip to content

Instantly share code, notes, and snippets.

@alisterburt
Created October 7, 2022 17:20
Show Gist options
  • Select an option

  • Save alisterburt/dae22671e82dcf4bc71a36c1fff3ae4e to your computer and use it in GitHub Desktop.

Select an option

Save alisterburt/dae22671e82dcf4bc71a36c1fff3ae4e to your computer and use it in GitHub Desktop.
projection of vector onto xy plane of an oriented point
import numpy as np
from scipy.spatial.transform import Rotation as R
# set up initial orientations of two particles
p0 = np.eye(3) # oriented same as basis vectors of coord system
p1 = R.from_euler(seq='XYZ', angles=[10, 10, 60], degrees=True) # slightly rotated out of plane, in plane quite different
# take y vector from p0, project onto y and y from p1
# take y vector because is easier for construction of x-vector with the cross product
p0_y = p0[:, 1]
p1_x = p1[:, 0]
p1_y = p1[:, 1]
p1_z = p1[:, 2]
# make sure p1_y is normalised first, for rotation matrices this is already the case
p0_y_on_p1_x = np.dot(p0_y, p1_y)
p0_y_on_p1_y = np.dot(p0_y, p1_y)
# calculate new y as linear combination of projections and existing basis for xy plane of p1
p1_new_y = p0_y_on_p1_x * p1_x + p0_y_on_p1_y * p1_y
p1_new_x = np.cross(p1_y, p1_z)
p1_final = np.empty((3, 3))
p1_final[:, 0] = p1_new_x
p1_final[:, 1] = p1_new_y
p1_final[:, 2] = p1_z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment