Skip to content

Commit 38c1e33

Browse files
committed
Unify Vecmath to JAMA conversion methods in Calc
The conversion between the two needs a transposition, because the Vecmath is a post-multiplication matrix and the JAMA is a pre-multiplication. When the conversion between the two is needed, use the methods in Calc that ensure the proper transposition so that they produce the same transformation.
1 parent 8f2ef09 commit 38c1e33

File tree

2 files changed

+62
-24
lines changed

2 files changed

+62
-24
lines changed

biojava-structure/src/main/java/org/biojava/nbio/structure/Calc.java

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,12 @@ public static final double amount(Atom a){
180180
*/
181181
public static final double angle(Atom a, Atom b){
182182

183-
183+
184184
Vector3d va = new Vector3d(a.getCoords());
185185
Vector3d vb = new Vector3d(b.getCoords());
186-
186+
187187
return Math.toDegrees(va.angle(vb));
188-
188+
189189
}
190190

191191
/**
@@ -388,7 +388,7 @@ public static final void rotate(Group group, double[][] rotationmatrix) throws S
388388
public static final void rotate(Atom atom, Matrix m){
389389

390390
double x = atom.getX();
391-
double y = atom.getY() ;
391+
double y = atom.getY();
392392
double z = atom.getZ();
393393
double[][] ad = new double[][]{{x,y,z}};
394394

@@ -434,7 +434,7 @@ public static final void rotate(Structure structure, Matrix m){
434434
}
435435

436436
}
437-
437+
438438
/**
439439
* Transform an array of atoms at once.
440440
* @param ca array of Atoms to shift
@@ -535,7 +535,7 @@ public static final void translate (Group group, Vector3d v) {
535535

536536
}
537537
}
538-
538+
539539
/**
540540
* Translates a chain object, given a Vector3d (i.e. the vecmath library
541541
* double-precision 3-d vector)
@@ -1053,7 +1053,7 @@ public static void main(String[] args){
10531053
public static void rotate(Atom[] ca, Matrix matrix) {
10541054
for (Atom atom : ca) Calc.rotate(atom, matrix);
10551055
}
1056-
1056+
10571057
/**
10581058
* Shift an array of atoms at once.
10591059
* @param ca array of Atoms to shift
@@ -1062,28 +1062,70 @@ public static void rotate(Atom[] ca, Matrix matrix) {
10621062
public static void shift(Atom[] ca, Atom b) {
10631063
for (Atom atom : ca) Calc.shift(atom, b);
10641064
}
1065-
1065+
10661066
/**
1067-
* Convert JAMA rotation and translation to a Vecmath transformation matrix
1067+
* Convert JAMA rotation and translation to a Vecmath transformation matrix.
1068+
* Because the JAMA matrix is a pre-multiplication matrix and the Vecmath
1069+
* matrix is a post-multiplication one, the rotation matrix is transposed to
1070+
* ensure that the transformation they produce is the same.
1071+
*
10681072
* @param rot 3x3 Rotation matrix
10691073
* @param trans 3x1 Translation matrix
10701074
* @return 4x4 transformation matrix
10711075
*/
10721076
public static Matrix4d getTransformation(Matrix rot, Matrix trans) {
1073-
return new Matrix4d( new Matrix3d(rot.getRowPackedCopy()),
1077+
return new Matrix4d( new Matrix3d(rot.getColumnPackedCopy()),
10741078
new Vector3d(trans.getColumnPackedCopy()),
10751079
1.0);
10761080
}
1077-
1081+
10781082
/**
1079-
* Convert JAMA rotation and translation to a Vecmath transformation matrix
1083+
* Convert JAMA rotation and translation to a Vecmath transformation matrix.
1084+
* Because the JAMA matrix is a pre-multiplication matrix and the Vecmath
1085+
* matrix is a post-multiplication one, the rotation matrix is transposed to
1086+
* ensure that the transformation they produce is the same.
1087+
*
10801088
* @param rot 3x3 Rotation matrix
1081-
* @param trans 3x1 Translation matrix
1089+
* @param trans 3x1 translation vector in Atom coordinates
10821090
* @return 4x4 transformation matrix
10831091
*/
10841092
public static Matrix4d getTransformation(Matrix rot, Atom trans) {
1085-
return new Matrix4d( new Matrix3d(rot.getRowPackedCopy()),
1093+
return new Matrix4d( new Matrix3d(rot.getColumnPackedCopy()),
10861094
new Vector3d(trans.getCoords()),
10871095
1.0);
10881096
}
1097+
1098+
/**
1099+
* Convert Vecmath transformation into a JAMA rotation matrix.
1100+
* Because the JAMA matrix is a pre-multiplication matrix and the Vecmath
1101+
* matrix is a post-multiplication one, the rotation matrix is transposed to
1102+
* ensure that the transformation they produce is the same.
1103+
*
1104+
* @param transform Matrix4d with transposed rotation matrix
1105+
* @return
1106+
*/
1107+
public static Matrix getRotationMatrix(Matrix4d transform){
1108+
1109+
Matrix rot = new Matrix(3,3);
1110+
for (int i=0;i<3;i++) {
1111+
for (int j=0;j<3;j++) {
1112+
rot.set(j, i, transform.getElement(i, j)); //transposed
1113+
}
1114+
}
1115+
return rot;
1116+
}
1117+
1118+
/**
1119+
* Extract the translational vector of a Vecmath transformation.
1120+
*
1121+
* @param transform Matrix4d
1122+
* @return Atom shift vector
1123+
*/
1124+
public static Atom getTranslationVector(Matrix4d transform){
1125+
1126+
Atom transl = new AtomImpl();
1127+
double[] coords = {transform.m03, transform.m13, transform.m23};
1128+
transl.setCoords(coords);
1129+
return transl;
1130+
}
10891131
}

biojava-structure/src/main/java/org/biojava/nbio/structure/align/util/RotationAxis.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,15 @@ public RotationAxis(Matrix rotation, Atom translation) {
163163
}
164164

165165
/**
166-
* Create a rotation axis from a Matrix4d containing a rotational component and a translational component
166+
* Create a rotation axis from a Matrix4d containing a rotational
167+
* component and a translational component.
168+
*
167169
* @param transform
168170
*/
169171
public RotationAxis(Matrix4d transform) {
170-
Atom transl = new AtomImpl();
171-
double[] coords = {transform.m03, transform.m13, transform.m23};
172-
transl.setCoords(coords);
173-
Matrix rot = new Matrix(3,3);
174-
for (int i=0;i<3;i++) {
175-
for (int j=0;j<3;j++) {
176-
rot.set(i, j, transform.getElement(i, j));
177-
}
178-
}
172+
173+
Matrix rot = Calc.getRotationMatrix(transform);
174+
Atom transl = Calc.getTranslationVector(transform);
179175
init(rot,transl);
180176
}
181177

0 commit comments

Comments
 (0)