/** * Creates a transformation from a set of three control vectors. A control * vector consists of a source point and a destination point, which is the * image of the source point under the desired transformation. Three control * vectors allows defining a fully general affine transformation. * * @param src0 * @param src1 * @param src2 * @param dest0 * @param dest1 * @param dest2 * @return the computed transformation */ public static AffineTransformation createFromControlVectors(Coordinate src0, Coordinate src1, Coordinate src2, Coordinate dest0, Coordinate dest1, Coordinate dest2) { AffineTransformationBuilder builder = new AffineTransformationBuilder(src0, src1, src2, dest0, dest1, dest2); return builder.getTransformation(); }
/** * Computes the {@link AffineTransformation} * determined by the control point mappings, * or <code>null</code> if the control vectors do not determine a well-defined transformation. * * @return an affine transformation, or null if the control vectors do not determine a well-defined transformation */ public AffineTransformation getTransformation() { // compute full 3-point transformation boolean isSolvable = compute(); if (isSolvable) return new AffineTransformation(m00, m01, m02, m10, m11, m12); return null; }
/** * Computes the transformation matrix by * solving the two systems of linear equations * defined by the control point mappings, * if this is possible. * * @return true if the transformation matrix is solvable */ private boolean compute() { double[] bx = new double[] { dest0.x, dest1.x, dest2.x }; double[] row0 = solve(bx); if (row0 == null) return false; m00 = row0[0]; m01 = row0[1]; m02 = row0[2]; double[] by = new double[] { dest0.y, dest1.y, dest2.y }; double[] row1 = solve(by); if (row1 == null) return false; m10 = row1[0]; m11 = row1[1]; m12 = row1[2]; return true; }
void runSingular(double p0x, double p0y, double p1x, double p1y, double p2x, double p2y, double pp0x, double pp0y, double pp1x, double pp1y, double pp2x, double pp2y ) { Coordinate p0 = new Coordinate(p0x, p0y); Coordinate p1 = new Coordinate(p1x, p1y); Coordinate p2 = new Coordinate(p2x, p2y); Coordinate pp0 = new Coordinate(pp0x, pp0y); Coordinate pp1 = new Coordinate(pp1x, pp1y); Coordinate pp2 = new Coordinate(pp2x, pp2y); AffineTransformationBuilder atb = new AffineTransformationBuilder( p0, p1, p2, pp0, pp1, pp2); AffineTransformation trans = atb.getTransformation(); assertEquals(trans, null); }
private void runTransform(AffineTransformation trans, Coordinate p0, Coordinate p1, Coordinate p2) { Coordinate pp0 = trans.transform(p0, new Coordinate()); Coordinate pp1 = trans.transform(p1, new Coordinate()); Coordinate pp2 = trans.transform(p2, new Coordinate()); AffineTransformationBuilder atb = new AffineTransformationBuilder( p0, p1, p2, pp0, pp1, pp2); AffineTransformation atbTrans = atb.getTransformation(); Coordinate dest = new Coordinate(); assertEqualPoint(pp0, atbTrans.transform(p0, dest)); assertEqualPoint(pp1, atbTrans.transform(p1, dest)); assertEqualPoint(pp2, atbTrans.transform(p2, dest)); }
void run(double p0x, double p0y, double p1x, double p1y, double p2x, double p2y, double pp0x, double pp0y, double pp1x, double pp1y, double pp2x, double pp2y ) { Coordinate p0 = new Coordinate(p0x, p0y); Coordinate p1 = new Coordinate(p1x, p1y); Coordinate p2 = new Coordinate(p2x, p2y); Coordinate pp0 = new Coordinate(pp0x, pp0y); Coordinate pp1 = new Coordinate(pp1x, pp1y); Coordinate pp2 = new Coordinate(pp2x, pp2y); AffineTransformationBuilder atb = new AffineTransformationBuilder( p0, p1, p2, pp0, pp1, pp2); AffineTransformation trans = atb.getTransformation(); Coordinate dest = new Coordinate(); assertEqualPoint(pp0, trans.transform(p0, dest)); assertEqualPoint(pp1, trans.transform(p1, dest)); assertEqualPoint(pp2, trans.transform(p2, dest)); }