public static Poly2Transform corrPointsToPoly2Transform( double[] fromPoints,
                                                             int fromPointsOff,
                                                             double[] toPoints,
                                                             int toPointsOff,
                                                             int nPoints,
                                                             int[] errorOut )
    {
        //For 3rd degree polynomial:
        // u = A0 + B0x + C0y + D0xy + E0xx + F0yy
        // v = A1 + B1x + C1y + D1xy + E1xx + F1yy
        //
        //Separate knows from unknows.
        // [u0 v0] = [1.0 x0 y0 x0*y0 x0*x0 y0y0] * [A0 B0 C0 D0 E0 F0]'
        // [u1 v1] = [1.0 x1 y1 x1*y1 x1*x1 y1y1]   [A1 B1 C1 D1 E1 F1]
        //   ...   =    ...                                                             
        // [un vn] = [1.0 xn yn xn*yn xn*xn ynyn]
        //
        //Restated:
        // postPoints_nx2 = xMat_nx6 * coeffs_6x2
        //
        //Coefficients can then be solved using least squares.
        // coeffs6x2 = xMat_nx6 \ postPoints_nx2
        
        final int k = 6; //Coefficient count. 

        if( nPoints < k ) {
            if( errorOut != null ) {
                errorOut[0] = ERR_INSUFFICIENT_RANK;
            }
            return null;
        }


        final int m    = nPoints;
        final int n    = k;
        final int dim0 = Math.min( m, n );
        final int dim1 = Math.max( m, n );
        
        final double[] xMat = new double[m * n];
        final double[] uMat = new double[m * 2];
        
        
        for(int i = 0; i < m; i++) {
            double xx = fromPoints[i*2   + fromPointsOff];
            double yy = fromPoints[i*2+1 + fromPointsOff];
            
            xMat[i+m*0] = 1.0;
            xMat[i+m*1] = xx;
            xMat[i+m*2] = xx * xx;
            xMat[i+m*3] = yy;
            xMat[i+m*4] = yy * xx;
            xMat[i+m*5] = yy * yy;
            
            uMat[i  ] = toPoints[i*2   + toPointsOff];
            uMat[i+m] = toPoints[i*2+1 + toPointsOff];
        }
        
        //System.out.println(MatFormat.formatToScreen(m, n, xMat, 0, m));
        //System.out.println(MatFormat.formatToScreen(m, 1, uMat, 0, m));
        
        //Tedious workspace allocations.
        final int smlsiz = Ilaenv.ilaenv(9, "DGELSD", "", 0, 0, 0, 0);
        final int nrhs   = 2;
        final int nlvl   = Math.max( 0, (int)( Math.log(dim0 / (smlsiz+1.0)) / LOG_2 )) + 1;
        final int lwork  = 12 * dim0 + 2 * dim0 * smlsiz + 8 * dim0 * nlvl + dim0 * nrhs + (smlsiz + 1) * (smlsiz + 1);
        final int liwork = 3 * dim0 * nlvl + 11 * dim0;
        
        //Allocate stuff.
        final double[] sMat    = new double[dim0];
        final double rcond     = MACHINE_EPSILON_DOUBLE;
        final intW rankBox     = new intW(0);
        final double[] workMat = new double[lwork];
        final int[] iWork      = new int[liwork];
        final intW infoBox     = new intW(0);
        
        //Solve Least squares using divide-and-conquer SVD.
        //Output will be stored in uMat.
        Dgelsd.dgelsd( m,
                       n,
                       nrhs,
                       xMat,
                       0,
                       m,
                       uMat,
                       0,
                       m,
                       sMat,
                       0,
                       rcond,
                       rankBox,
                       workMat,
                       0,
                       workMat.length,
                       iWork,
                       0,
                       infoBox );
        
        //Check if error or if input data had insufficient rank.
        if(infoBox.val != 0 || rankBox.val < k) {
            if(errorOut != null) {
                if(infoBox.val == 0) {
                    errorOut[0] = ERR_INSUFFICIENT_RANK;
                }else{
                    errorOut[0] = infoBox.val < 0 ? ERR_ILLEGAL_VALUE : ERR_DID_NOT_CONVERGE;
                }
            }
            
            return null;
        }
        
        //Place coeffs into different matrix, if necessary.
        double[] coeffs;
        if( m == k ) {
            coeffs = uMat;
        } else {
            coeffs = new double[k * 2];
            System.arraycopy( uMat, 0, coeffs, 0, k );
            System.arraycopy( uMat, m, coeffs, k, k );
        }
        
        //System.out.println(MatFormat.formatToScreen( m, 2, uMat, 0, m ));
        return new Poly2Transform( coeffs, null );
    }