简体   繁体   English

获取旋转线的坐标

[英]Get coordinates of rotated line

I have 2 lines (A and B) defined by P1 and P2 (xyz) coordinates.我有 2 条线(A 和 B)由 P1 和 P2 (xyz) 坐标定义。

p1_A = [5, 10, 3]
p2_A = [15, 13, 3]

p1_B = [6, 20, 3]
p2_B = [9, 22, 5]

and the corresponding rotation matrix R to rotate line A to B:和相应的旋转矩阵 R 将线 A 旋转到 B:

R = array([[ 0.8468851 , -0.29080408, -0.44521748],
       [ 0.22027131,  0.95385981, -0.20403924],
       [ 0.48401051,  0.07472916,  0.87186546]])

Then, I calculated the dot product to rotate the vector R.dot(vecA) .然后,我计算了点积以旋转向量R.dot(vecA) I don't know how to continue from here.我不知道如何从这里继续。 I need the (xyz) coordinates of the rotated line A.我需要旋转线 A 的 (xyz) 坐标。

EDIT:编辑:

I tried hippozhipos's solution (red dashed line) to rotate line A. I would expect the dashed line to rotate at the midpoint (see green dot) and to be parallel to the blue (reference line).我尝试了 hippozhipos 的解决方案(红色虚线)来旋转 A 线。我希望虚线在中点旋转(见绿点)并与蓝色(参考线)平行。

import matplotlib.pyplot as plt

p1_A = [5, 10, 3]
p2_A = [15, 13, 3]

p1_B = [6, 20, 3]
p2_B = [9, 22, 5]

mid_point = np.array([10. , 11.5,  3. ])

p1_A_rot = multVecMatrix(p1_A, R)
p2_A_rot = multVecMatrix(p2_A, R)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_aspect('equal')
ax.set_xlim([0,23])
ax.set_ylim([0,23])
ax.plot([p1_A[0],p2_A[0]], [p1_A[1],p2_A[1]], 'r')
ax.plot(t0_cent[0], t0_cent[1], 'go')
# Reference
ax.plot([p1_B[0],p2_B[0]], [p1_B[1],p2_B[1]], 'b')

# Rotated
ax.plot([p1_A_rot[0],p2_A_rot[0]], [p1_A_rot[1],p2_A_rot[1]], 'r')

Plot Plot

You have 2 points for line A and a rotation matrix.线 A 和旋转矩阵有 2 个点。 So to obtain the rotation, simply multiply the vectors representing the 2 points with the rotation matrix.因此,要获得旋转,只需将表示 2 个点的向量与旋转矩阵相乘即可。 Here is the function to do just that:这是执行此操作的 function:

def multVecMatrix(vec3, mat3):
    x = vec3[0] * mat3[0][0] + vec3[1] * mat3[1][0] + vec3[2] * mat3[2][0]
    y = vec3[0] * mat3[0][1] + vec3[1] * mat3[1][1] + vec3[2] * mat3[2][1]
    z = vec3[0] * mat3[0][2] + vec3[1] * mat3[1][2] + vec3[2] * mat3[2][2]

So to get the rotated vector for line A:因此,要获得 A 行的旋转向量:

rotatedP1_A = multVecMatrix(P1_A, R)
rotatedP2_A = multVecMatrix(P2_A, R)

Working example:工作示例:

p1_A = [5, 10, 3]
p2_A = [15, 13, 3]

R = [[ 0.8468851 , -0.29080408, -0.44521748],
       [ 0.22027131,  0.95385981, -0.20403924],
       [ 0.48401051,  0.07472916,  0.87186546]]

def multVecMatrix(vec3, mat3):
    x = vec3[0] * mat3[0][0] + vec3[1] * mat3[1][0] + vec3[2] * mat3[2][0]
    y = vec3[0] * mat3[0][1] + vec3[1] * mat3[1][1] + vec3[2] * mat3[2][1]
    z = vec3[0] * mat3[0][2] + vec3[1] * mat3[1][2] + vec3[2] * mat3[2][2]
    return [x, y, z]

rotatedP1_A = multVecMatrix(p1_A, R)
rotatedP2_A = multVecMatrix(p2_A, R)

print(rotatedP1_A, rotatedP2_A)

To make a line parallel to B , you don't need rotation matrix.要使一条线平行于B ,您不需要旋转矩阵。

Calculate direction vector of B line and add it to the middle of A计算B线的方向向量,加到A的中间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM