Python Numpy没有给我关于平面之间角度的预期结果

时间:2018-12-20 12:27:54

标签: python numpy 3d

我在一个平面上有3个点,在另一个平面上有3个点。我想要平面之间的角度,所以我使用这些点来制作矢量,使用叉积查找法线,使用点积并除以幅度和反余弦以找到角度。

从这些点开始,我期望平面法线之间的夹角为45或0.785398。我得到0.6154797086703875

我的逻辑肯定有问题,有人可以帮忙吗?谢谢。

 import numpy as np

 """

 y axis up page
 x axis to the right on page
 z axis towards user out of page

 point1 -1,1,0
 point2 1,1,0
 point3 1,1,1
 so horizontal plane at y=1

 point4 0,0,0
 point5 1,1,0
 point6 1,1,1
 plane at 45 degrees meeting other plane at 1,1,n

 """

 #use points to make vectors
 #point2 - point1
 vx1 = 2#1--1
 vy1 = 0#1-1
 vz1 = 0#0-0
 #point3 - point1
 vx2 = 2#1--1
 vy2 = 0#1-1
 vz2 = 1#1-0
 #cross product to find normal
 plane1 = np.cross([vx1,vy1,vz1],[vx2,vy2,vz2])

 #use points to make vectors
 #point5 - point4
 vx1 = 1#1-0
 vy1 = 1#1-0
 vz1 = 0#0-0
 #point6 - point4
 vx2 = 1#1-0
 vy2 = 1#1-0
 vz2 = 1#1-0
 #cross product to find normal
 plane2 = np.cross([vx1,vy1,vz1],[vx2,vy2,vz2])

 #angle between the two normals
 #dot product
 ang = np.dot(plane1,plane2)
 #divide by magnitude of vectors
 ang = ang / (np.sqrt((vx1*vx1)+(vy1*vy1)+(vz1*vz1))*np.sqrt((vx2*vx2)+(vy2*vy2)+(vz2*vz2)))
 #inverse cos to find angle
 ang = np.arccos(ang)
 #should be 45 or 0.785398
 print("Angle calculated",ang)
 if (ang < 95) and (ang > 85):
      print("RIGHT ANGLE")
 else:
      print("OTHER ANGLE")

2 个答案:

答案 0 :(得分:0)

使用np.linalg.norm

ang = np.arccos(np.dot(plane1, plane2) / (np.linalg.norm(plane1) * np.linalg.norm(plane2)))
ang
>> 0.78539816

答案 1 :(得分:0)

如评论中所述,错误是您使用错误的向量进行归一化。

我想补充一点,Binet-Cauchy在这里也可以使用,并且比使用叉积更快。

import math

def angle_BC(plane1, plane2):
    planes = np.concatenate((plane1[:2]-plane1[2], plane2[:2]-plane2[2]), axis=0)
    a = (planes@planes.T).ravel().tolist()
    return math.acos((a[2]*a[7]-a[3]*a[6]) / math.sqrt((a[0]*a[5]-a[1]*a[1])*(a[10]*a[15]-a[11]*a[11])))... 

def angle_CP(plane1, plane2):
    n1 = np.cross(*plane1[:2]-plane1[2])
    n2 = np.cross(*plane2[:2]-plane2[2])
    return math.acos(n1@n2 / math.sqrt(n1@n1 * n2@n2))

plane1, plane2 = np.array(((-1,1,0),(1,1,0),(1,1,1))), np.array(((0,0,0),(1,1,0),(1,1,1)))
angle_BC(plane1, plane2), angle_CP(plane1, plane2), np.pi/4
# (0.7853981633974484, 0.7853981633974484, 0.7853981633974483)

from timeit import repeat
min(repeat(lambda: angle_BC(plane1, plane2), number=10000))
# 0.08035301300697029
min(repeat(lambda: angle_CP(plane1, plane2), number=10000))
# 0.9213669009623118