Skip to content

Commit 25c47de

Browse files
committed
Port methods from Calc to new Matrices class biojava#558
1 parent 523920e commit 25c47de

3 files changed

Lines changed: 91 additions & 42 deletions

File tree

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

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package org.biojava.nbio.structure;
2424

2525
import org.biojava.nbio.structure.geometry.CalcPoint;
26+
import org.biojava.nbio.structure.geometry.Matrices;
2627
import org.biojava.nbio.structure.geometry.SuperPositionSVD;
2728
import org.biojava.nbio.structure.jama.Matrix;
2829
import org.slf4j.Logger;
@@ -998,7 +999,7 @@ public static final Atom createVirtualCBAtom(AminoAcid amino)
998999
SuperPositionSVD svd = new SuperPositionSVD(false);
9991000

10001001
Matrix4d transform = svd.superpose(arr1, arr2);
1001-
Matrix rotMatrix = getRotationMatrix(transform);
1002+
Matrix rotMatrix = Matrices.getRotationJAMA(transform);
10021003
Atom tranMatrix = getTranslationVector(transform);
10031004

10041005
Calc.rotate(aCB, rotMatrix);
@@ -1182,23 +1183,6 @@ public static void shift(Atom[] ca, Atom b) {
11821183
Calc.shift(atom, b);
11831184
}
11841185

1185-
/**
1186-
* Convert JAMA rotation and translation to a Vecmath transformation matrix.
1187-
* Because the JAMA matrix is a pre-multiplication matrix and the Vecmath
1188-
* matrix is a post-multiplication one, the rotation matrix is transposed to
1189-
* ensure that the transformation they produce is the same.
1190-
*
1191-
* @param rot
1192-
* 3x3 Rotation matrix
1193-
* @param trans
1194-
* 3x1 Translation matrix
1195-
* @return 4x4 transformation matrix
1196-
*/
1197-
public static Matrix4d getTransformation(Matrix rot, Matrix trans) {
1198-
return new Matrix4d(new Matrix3d(rot.getColumnPackedCopy()),
1199-
new Vector3d(trans.getColumnPackedCopy()), 1.0);
1200-
}
1201-
12021186
/**
12031187
* Convert JAMA rotation and translation to a Vecmath transformation matrix.
12041188
* Because the JAMA matrix is a pre-multiplication matrix and the Vecmath
@@ -1215,30 +1199,9 @@ public static Matrix4d getTransformation(Matrix rot, Atom trans) {
12151199
return new Matrix4d(new Matrix3d(rot.getColumnPackedCopy()),
12161200
new Vector3d(trans.getCoords()), 1.0);
12171201
}
1218-
1219-
/**
1220-
* Convert Vecmath transformation into a JAMA rotation matrix. Because the
1221-
* JAMA matrix is a pre-multiplication matrix and the Vecmath matrix is a
1222-
* post-multiplication one, the rotation matrix is transposed to ensure that
1223-
* the transformation they produce is the same.
1224-
*
1225-
* @param transform
1226-
* Matrix4d with transposed rotation matrix
1227-
* @return
1228-
*/
1229-
public static Matrix getRotationMatrix(Matrix4d transform) {
1230-
1231-
Matrix rot = new Matrix(3, 3);
1232-
for (int i = 0; i < 3; i++) {
1233-
for (int j = 0; j < 3; j++) {
1234-
rot.set(j, i, transform.getElement(i, j)); // transposed
1235-
}
1236-
}
1237-
return rot;
1238-
}
1239-
1202+
12401203
/**
1241-
* Extract the translational vector of a Vecmath transformation.
1204+
* Extract the translational vector as an Atom of a transformation matrix.
12421205
*
12431206
* @param transform
12441207
* Matrix4d
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.biojava.nbio.structure.geometry;
2+
3+
import javax.vecmath.Matrix3d;
4+
import javax.vecmath.Matrix4d;
5+
import javax.vecmath.Vector3d;
6+
7+
import org.biojava.nbio.structure.jama.Matrix;
8+
9+
/**
10+
* Matrices contains static methods to operate and transform matrices used in 3D
11+
* geometry (transformation matrices and rotation matrices).
12+
* <p>
13+
* This class complements and extends the functionallity of vecmath and jama.
14+
*
15+
* @author Aleix Lafita
16+
* @since 5.0.0
17+
*
18+
*/
19+
public class Matrices {
20+
21+
/**
22+
* Convert a transformation matrix into a JAMA rotation matrix. Because the
23+
* JAMA matrix is a pre-multiplication matrix and the Vecmath matrix is a
24+
* post-multiplication one, the rotation matrix is transposed to ensure that
25+
* the transformation they produce is the same.
26+
*
27+
* @param transform
28+
* Matrix4d with transposed rotation matrix
29+
* @return rotation matrix as JAMA object
30+
*/
31+
public static Matrix getRotationJAMA(Matrix4d transform) {
32+
33+
Matrix rot = new Matrix(3, 3);
34+
for (int i = 0; i < 3; i++) {
35+
for (int j = 0; j < 3; j++) {
36+
rot.set(j, i, transform.getElement(i, j)); // transposed
37+
}
38+
}
39+
return rot;
40+
}
41+
42+
/**
43+
* Convert a transformation matrix into a rotation matrix.
44+
*
45+
* @param transform
46+
* Matrix4d
47+
* @return rotation matrix
48+
*/
49+
public static Matrix3d getRotationMatrix(Matrix4d transform) {
50+
51+
Matrix3d rot = new Matrix3d();
52+
transform.setRotationScale(rot);
53+
return rot;
54+
}
55+
56+
/**
57+
* Extract the translational vector of a transformation matrix.
58+
*
59+
* @param transform
60+
* Matrix4d
61+
* @return Vector3d translation vector
62+
*/
63+
public static Vector3d getTranslationVector(Matrix4d transform) {
64+
Vector3d transl = new Vector3d();
65+
transform.get(transl);
66+
return transl;
67+
}
68+
69+
/**
70+
* Convert JAMA rotation and translation to a Vecmath transformation matrix.
71+
* Because the JAMA matrix is a pre-multiplication matrix and the Vecmath
72+
* matrix is a post-multiplication one, the rotation matrix is transposed to
73+
* ensure that the transformation they produce is the same.
74+
*
75+
* @param rot
76+
* 3x3 Rotation matrix
77+
* @param trans
78+
* 3x1 Translation matrix
79+
* @return 4x4 transformation matrix
80+
*/
81+
public static Matrix4d getTransformation(Matrix rot, Matrix trans) {
82+
return new Matrix4d(new Matrix3d(rot.getColumnPackedCopy()),
83+
new Vector3d(trans.getColumnPackedCopy()), 1.0);
84+
}
85+
86+
}

biojava-structure/src/main/java/org/biojava/nbio/structure/geometry/SuperPositionSVD.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public Matrix4d superpose(Point3d[] fixed, Point3d[] moved) {
135135
Matrix cb_tmp = centroidB.times(rot);
136136
Matrix tran = centroidA.minus(cb_tmp);
137137

138-
return Calc.getTransformation(rot, tran);
138+
return Matrices.getTransformation(rot, tran);
139139

140140
}
141141

0 commit comments

Comments
 (0)