These are C language routines to calculate torsion or dihedral angles, given a set of four points, and to place a point given three reference points, a length, an angle, and a dihedral.
The torsion angle is the angle AB makes with DC, when viewed down the BC axis. Note that some people use an alternative definiton, whereby if CD forms a straight line with AB the torsion is defined as zero, and if AB and DC are anti-parallel the angle is PI. Here, we say that the torsion is PI in the fist case and 0 in the second.
The first routine calculates the torsion. This is achieved by taking a crossproducts to get the normals to the planes ABC and BCD. Then the dot product is calculated to get the angle between them, with a little fix up to get the direction correct.
The second routine is trickier. It is relatively eay to write an inefficient routine that used matrices to translate and rotate about the bonds. However in real applications that use torsion angles the routines to place them can need to be called many thousands if not millions of times a second. This is an implementation of a much better algorithm by Jerod Parsons1. What it does is to construct a point with the correct torsion, length, and simple angle along the y axis. Then it rotates it in a single step to the right position as given by the other points, and all that remains is to translate the base to the point C.
All that are needed are four sines and cosines, to set up the initial vector, and two cross-products, two normalisations, to set up the transformation matrix, and a matrix multiply to put the point in the right position, follwed by a vector addition.
The code is not heavily optimised. It is possible to speed up the normalisations by passing in the lengths of the vectors, if these are already known. It is also unnecessary to call the sine function twice, and the matrix is redundant and simply there for clarity.
float calctorsion(float *A, float *B, float *C, float *D)
void puttor(float *D, float *A, float *B, float *C, float len, float theta, float phi)
The source file is