def calc_dihedral(v1, v2, v3, v4):
 ab = (v1 - v2)
 cb = (v3 - v2)
 db = (v4 - v3)
 u = (ab ** cb)
 v = (db ** cb)
 w = (u ** v)
 angle = u.angle(v)
 try:
  if (cb.angle(w) > 0.001):
   angle = (- angle)
 except ZeroDivisionError:
  pass
 return angle

def calc_dihedral ( v1 , v2 , v3 , v4 ) : ab = ( v1 - v2 ) cb = ( v3 - v2 ) db = ( v4 - v3 ) u = ( ab ** cb ) v = ( db ** cb ) w = ( u ** v ) angle = u . angle ( v ) try : if ( cb . angle ( w ) > 0.001 ) : angle = ( - angle ) except ZeroDivisionError : pass return angle
calculate the dihedral angle between 4 vectors representing 4 connected points .