Skip to content

Commit df93f78

Browse files
authored
Merge pull request #4 from josemduarte/superpose
TestSuperPosition now tests rmsd of non-centered points.
2 parents cc79186 + 1d43a42 commit df93f78

File tree

6 files changed

+49
-29
lines changed

6 files changed

+49
-29
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import javax.vecmath.Matrix4d;
44
import javax.vecmath.Point3d;
5+
import javax.vecmath.Vector3d;
56

67
import org.biojava.nbio.structure.jama.Matrix;
78

@@ -28,7 +29,7 @@ private CalcPoint() {
2829
public static void center(Point3d[] x) {
2930
Point3d center = centroid(x);
3031
center.negate();
31-
translate(center, x);
32+
translate(new Vector3d(center), x);
3233
}
3334

3435
/**
@@ -52,8 +53,8 @@ public static Point3d centroid(Point3d[] x) {
5253
*
5354
* @param rotTrans
5455
* 4x4 transformation matrix
55-
* @param array
56-
* of points. Point objects will be modified
56+
* @param x
57+
* array of points. Point objects will be modified
5758
*/
5859
public static void transform(Matrix4d rotTrans, Point3d[] x) {
5960
for (Point3d p : x) {
@@ -64,12 +65,12 @@ public static void transform(Matrix4d rotTrans, Point3d[] x) {
6465
/**
6566
* Translate all points with a translation vector.
6667
*
67-
* @param rotTrans
68-
* 4x4 transformation matrix
69-
* @param array
70-
* of points. Point objects will be modified
68+
* @param trans
69+
* the translation vector to apply
70+
* @param x
71+
* array of points. Point objects will be modified
7172
*/
72-
public static void translate(Point3d trans, Point3d[] x) {
73+
public static void translate(Vector3d trans, Point3d[] x) {
7374
for (Point3d p : x) {
7475
p.add(trans);
7576
}
@@ -90,7 +91,7 @@ public static Point3d[] clonePoint3dArray(Point3d[] x) {
9091
return clone;
9192
}
9293

93-
/**
94+
/*
9495
* Peter can you document this method? TODO
9596
*
9697
* @param moved

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,13 @@ private void calcRmsd(Point3d[] x, Point3d[] y) {
261261
xtrans = CalcPoint.centroid(xref);
262262
logger.debug("x centroid: " + xtrans);
263263
xtrans.negate();
264-
CalcPoint.translate(xtrans, xref);
264+
CalcPoint.translate(new Vector3d(xtrans), xref);
265265

266266
yref = CalcPoint.clonePoint3dArray(y);
267267
ytrans = CalcPoint.centroid(yref);
268268
logger.debug("y centroid: " + ytrans);
269269
ytrans.negate();
270-
CalcPoint.translate(ytrans, yref);
270+
CalcPoint.translate(new Vector3d(ytrans), yref);
271271
innerProduct(yref, xref);
272272
}
273273
calcRmsd(wsum);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public Matrix4d superpose(Point3d[] fixed, Point3d[] moved) {
5959
Point3d[] xref = CalcPoint.clonePoint3dArray(fixed);
6060
Point3d xtrans = CalcPoint.centroid(xref);
6161
xtrans.negate();
62-
CalcPoint.translate(xtrans, xref);
62+
CalcPoint.translate(new Vector3d(xtrans), xref);
6363

6464
Point3d[] yref = CalcPoint.clonePoint3dArray(moved);
6565
Point3d ytrans = CalcPoint.centroid(yref);
6666
ytrans.negate();
67-
CalcPoint.translate(ytrans, yref);
67+
CalcPoint.translate(new Vector3d(ytrans), yref);
6868

6969
// calculate rotational component (rotation around origin)
7070
Quat4d q = UnitQuaternions.relativeOrientation(xref, yref);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import javax.vecmath.Matrix4d;
2626
import javax.vecmath.Point3d;
27+
import javax.vecmath.Vector3d;
2728

2829
import org.biojava.nbio.structure.jama.Matrix;
2930
import org.biojava.nbio.structure.jama.SingularValueDecomposition;
@@ -73,10 +74,10 @@ public Matrix4d superpose(Point3d[] fixed, Point3d[] moved) {
7374
cenb.negate();
7475

7576
Point3d[] ats1 = CalcPoint.clonePoint3dArray(fixed);
76-
CalcPoint.translate(cena, ats1);
77+
CalcPoint.translate(new Vector3d(cena), ats1);
7778

7879
Point3d[] ats2 = CalcPoint.clonePoint3dArray(moved);
79-
CalcPoint.translate(cenb, ats2);
80+
CalcPoint.translate(new Vector3d(cenb), ats2);
8081

8182
double[][] coordSet1 = new double[ats1.length][3];
8283
double[][] coordSet2 = new double[ats2.length][3];

biojava-structure/src/main/java/org/biojava/nbio/structure/symmetry/core/C2RotationSolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ private void solve() {
7575
// SuperPosition.center(y);
7676

7777
Point3d[] x = CalcPoint.clonePoint3dArray(traces.get(0));
78-
CalcPoint.translate(new Point3d(trans), x);
78+
CalcPoint.translate(trans, x);
7979
Point3d[] y = CalcPoint.clonePoint3dArray(traces.get(1));
80-
CalcPoint.translate(new Point3d(trans), y);
80+
CalcPoint.translate(trans, y);
8181

8282
// TODO implement this piece of code using at origin superposition
8383
Quat4d quat = UnitQuaternions.relativeOrientation(

biojava-structure/src/test/java/org/biojava/nbio/structure/geometry/TestSuperPosition.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,24 @@
2525
* algorithm for RMSD and Superposition calculations.
2626
*
2727
* @author Aleix Lafita
28+
* @author Jose Duarte
2829
* @since 5.0.0
2930
*
3031
*/
3132
public class TestSuperPosition {
3233

33-
private static final Logger logger = LoggerFactory
34-
.getLogger(TestSuperPosition.class);
34+
private static final Logger LOGGER = LoggerFactory.getLogger(TestSuperPosition.class);
3535

36-
private List<Point3d[]> cloud1;
37-
private List<Point3d[]> cloud2;
36+
private static List<Point3d[]> cloud1;
37+
private static List<Point3d[]> cloud2;
3838

39-
private AxisAngle4d rotAxis;
40-
private Vector3d translation;
41-
private Matrix4d transform;
39+
// the transformation to apply to cloud points 1 that needs to be recovered by the superposition code
40+
private static final AxisAngle4d rotAxis = new AxisAngle4d(0.440, 0.302, 0.845, 1.570);
41+
private static final Vector3d translation = new Vector3d(0.345, 2.453, 5.324);;
42+
private static Matrix4d transform;
43+
44+
// a translation to apply to cloud point 2 for the rmsd test only
45+
private static final Vector3d translation2 = new Vector3d(1.32, -1.03, 6.28);
4246

4347
/**
4448
* Generate two clouds of random points of different sizes to test
@@ -54,8 +58,6 @@ public void setUp() throws StructureException {
5458

5559
Random rnd = new Random(0);
5660

57-
rotAxis = new AxisAngle4d(0.440, 0.302, 0.845, 1.570);
58-
translation = new Vector3d(0.345, 2.453, 5.324);
5961
transform = new Matrix4d();
6062
transform.set(rotAxis);
6163
transform.setTranslation(translation);
@@ -82,10 +84,15 @@ public void setUp() throws StructureException {
8284
CalcPoint.center(c1);
8385
CalcPoint.center(c2);
8486

85-
CalcPoint.transform(transform, c1);
87+
CalcPoint.transform(transform, c1);
8688

8789
cloud1.add(c1);
8890
cloud2.add(c2);
91+
92+
Point3d centroid1 = CalcPoint. centroid(c1);
93+
Point3d centroid2 = CalcPoint. centroid(c2);
94+
LOGGER.debug("Centroid c1 (size %d): (%.2f, %.2f, %.2f)\n", size, centroid1.x, centroid1.y, centroid1.z);
95+
LOGGER.debug("Centroid c2 (size %d): (%.2f, %.2f, %.2f)\n", size, centroid2.x, centroid2.y, centroid2.z);
8996
}
9097

9198
}
@@ -116,7 +123,7 @@ public void testSuperposition() {
116123
Matrix4d qcpTransform = qcp.superpose(cloud1.get(c), cloud2.get(c));
117124
long qcpTime = (System.nanoTime() - qcpStart) / 1000;
118125

119-
logger.error(String.format("Transformation Matrix %d points: "
126+
LOGGER.info(String.format("Transformation Matrix %d points: "
120127
+ "SVD time %d us, SP time: %d us, QCP time: %d us",
121128
cloud1.get(c).length, svdTime, quatTime, qcpTime));
122129

@@ -133,6 +140,16 @@ public void testSuperposition() {
133140
*/
134141
@Test
135142
public void testRMSD() {
143+
144+
// for the rmsd test we first make sure that both cloud points are not centered in origin so that the centering is tested too
145+
// first cloud points are already centered, we translate cloud2 only
146+
for (int c=0; c<cloud2.size(); c++) {
147+
CalcPoint.translate(translation2, cloud2.get(c));
148+
149+
Point3d centroid2 = CalcPoint. centroid(cloud2.get(c));
150+
LOGGER.debug("Centroid c2 (index %d): (%.2f, %.2f, %.2f)\n", c, centroid2.x, centroid2.y, centroid2.z);
151+
}
152+
136153

137154
for (int c = 0; c < cloud1.size(); c++) {
138155

@@ -154,11 +171,12 @@ public void testRMSD() {
154171
double qcprmsd = qcp.getRmsd(cloud1.get(c), cloud2.get(c));
155172
long qcpTime = (System.nanoTime() - qcpStart) / 1000;
156173

157-
logger.error(String.format("RMSD %d points: SVD time %d us, "
174+
LOGGER.info(String.format("RMSD %d points: SVD time %d us, "
158175
+ "Quat time: %d us, QCP time: %d us", cloud1.get(c).length,
159176
svdTime, quatTime, qcpTime));
160177

161178
// Check that the returned RMSDs are equal
179+
assertEquals(quatrmsd, qcprmsd, 0.001);
162180
assertEquals(svdrmsd, quatrmsd, 0.001);
163181
assertEquals(svdrmsd, qcprmsd, 0.001);
164182
}

0 commit comments

Comments
 (0)