def dihedral_angle(a, b, c, d):
"""
Calculate the dihedral angle between 4 vectors,
representing 4 connected points. The angle is in range [-180, 180].
@param a: the four points that define the dihedral angle
@type a: array
@return: angle in [-180, 180]
"""
v = b - c
m = numpy.cross((a - b), v)
m /= norm(m)
n = numpy.cross((d - c), v)
n /= norm(n)
c = numpy.dot(m, n)
s = numpy.dot(numpy.cross(n, m), v) / norm(v)
angle = math.degrees(math.atan2(s, c))
if angle > 0:
return numpy.fmod(angle + 180, 360) - 180
else:
return numpy.fmod(angle - 180, 360) + 180
评论列表
文章目录