assert (axis1 < testdim);
assert (axis2 < testdim);
// don't change the angle; we'll be using that executing the rotation
// three times will be identity (approximately)
double angle = Math.toRadians(360 / 3);
AffineTransformation t = new AffineTransformation(testdim);
assertTrue(t.getDimensionality() == testdim);
Matrix tm = t.getTransformation();
assertNotSame("getTransformation is expected to return a new copy", tm, t.getTransformation());
assertEquals("initial transformation matrix should be unity", tm, Matrix.unitMatrix(testdim + 1));
// rotation matrix
double[][] rm = new double[testdim][testdim];
for(int i = 0; i < testdim; i++) {
rm[i][i] = 1;
}
// add the rotation
rm[axis1][axis1] = +Math.cos(angle);
rm[axis1][axis2] = -Math.sin(angle);
rm[axis2][axis1] = +Math.sin(angle);
rm[axis2][axis2] = +Math.cos(angle);
t.addMatrix(new Matrix(rm));
Matrix tm2 = t.getTransformation();
// We know that we didn't do any translations and tm is the unity matrix
// so we can manually do the rotation on it, too.
tm.set(axis1, axis1, +Math.cos(angle));
tm.set(axis1, axis2, -Math.sin(angle));
tm.set(axis2, axis1, +Math.sin(angle));
tm.set(axis2, axis2, +Math.cos(angle));
// Compare the results
assertEquals("Rotation wasn't added correctly to matrix.", tm, tm2);
// test application to a vector
double[] dv = new double[testdim];
for(int i = 0; i < testdim; i++) {
dv[i] = i * i + testdim;
}
Vector v1 = new Vector(dv);
Vector v2 = t.apply(v1);
Vector v3 = t.applyInverse(v2);
assertTrue("Forward-Backward didn't work correctly.", v1.minus(v3).euclideanLength() < 0.0001);
Vector v4 = t.apply(t.apply(t.apply(v1)));
assertTrue("Triple-Rotation by 120 degree didn't work", v1.minus(v4).euclideanLength() < 0.0001);
// Rotation shouldn't disagree for relative vectors.
// (they just are not affected by translation!)
assertEquals("Relative vectors were affected differently by pure rotation!", v2, t.applyRelative(v1));
// should do the same as built-in rotation!
AffineTransformation t2 = new AffineTransformation(testdim);
t2.addRotation(axis1, axis2, angle);
Vector t2v2 = t2.apply(v1);
assertTrue("Manual rotation and AffineTransformation.addRotation disagree.", v2.minus(t2v2).euclideanLength() < 0.0001);
}