[Biojava-l] 3d structure rotation code
Tamas Horvath
hotafin at gmail.com
Sun Dec 4 16:06:53 EST 2005
I'd like to show u the following 2 functions that may be valuable in theCalc class:
/**Returns a rotated Structure object (the rotation is around the origo) * * @param ostructure Structure -- the stucture to be rotated * @param from Atom -- the reference Atom's originalcoordinates * @param to Atom -- the reference Atom's desiredcoordinates * @return Structure -- null if there was an error */
public static Structure rotate3D(Structure ostructure,Atom from, Atom to)throws StructureException { Structure nstructure = new StructureImpl();
//calculate the angle of rotation final double angle = radangle(from,to); if (angle == 0 || angle == Math.PI) { throw new StructureException ("The rotation angle is 0 or 180degrees!"); }
//calculate te unit normal vector of the (origo, from, to) pane //which will serve as an arbitary axis for the rotation Atom axisvector = vectorProduct(from,to); axisvector = unitVector(axisvector);
//calculate the trigonometric values final double c = Math.cos(angle); final double s = Math.sin(angle); final double t = 1-Math.cos(angle);
final double x = axisvector.getX(); final double y = axisvector.getY(); final double z = axisvector.getZ();
//and now the matrix double[][] rotationmatrix = new double[3][3]; rotationmatrix[0][0] = t*x*x+c ;rotationmatrix[0][1] =t*x*y+s*z;rotationmatrix[0][2] = t*x*z-s*y; rotationmatrix[1][0] = t*x*y-s*z;rotationmatrix[1][1] =t*y*y+c;rotationmatrix[1][2] = t*y*z+s*x; rotationmatrix[2][0] = t*x*y+s*y;rotationmatrix[2][1] =t*y*z-s*x;rotationmatrix[2][2] = t*z*z+c;
//and now the rotation nstructure = (Structure) ostructure.clone(); try { rotate(nstructure, rotationmatrix); } catch (StructureException e) { System.out.println(e); nstructure = null; }
return nstructure; }
/**Calculates the a,origo,b angle in radians * * @param a Atom * @param b Atom * @return double */
public static double radangle(Atom a, Atom b) {
final double skalar = skalarProduct(a,b); final double radangle = Math.acos( skalar/( amount(a) * amount(b) ));
return radangle; }
More information about the Biojava-l
mailing list