Exemplo n.º 1
0
def _norm_grid(A: Affine, tol=1e-8) -> Union[F4, F6]:
    """Compute normalised grid.

    Different grids related via whole pixel translation will have the same
    normalised grid.

    Scale and Translation only
    --------------------------

    Normalised grid is defined by:

       sx, sy, tx, ty

    Where sx, sy is resolution (CRS units per pixel)
    and (tx, ty) is sub-pixel translation in (-0.5, 0.5)

    Order is: subpix translate -> scale

    General Case
    ------------

    Normalised grid is defined by:

       sx, sy, tx, ty, ca, w

    Order is: subpix translate -> scale -> rotate+shear

    WARNING: general case is not currently implemented
    """

    R, W, S = decompose_rws(A)

    (ca, _, tx, _, _, ty, *_) = R

    (_, w, *_) = W

    (sx, _, _, _, sy, *_) = S

    is_st = abs(ca - 1) < tol and abs(w) < tol

    if is_st:
        # No rotation no shear -- commonest case
        # sx, sy, tx, ty fully defines the grid

        # Tw*S*Xp == S*Tp*Xp
        #
        # Here `p` is Pixel, `w` is World

        # convert from: <Scale pixels to World, then translate>
        # into        : <Translate pixels then scale to World>
        tx_p, ty_p = tx / sx, ty / sy

        # Remove whole pixel translation to normalise grid
        _, (tx_p, ty_p) = split_translation((tx_p, ty_p))

        return (maybe_int(sx, tol), maybe_int(sy, tol), maybe_zero(tx_p, tol),
                maybe_zero(ty_p, tol))
    else:
        raise NotImplementedError('TODO: rotated grids')
Exemplo n.º 2
0
    def run_test(a, scale, shear=0, translation=(0, 0), tol=1e-8):
        A = mkA(a, scale=scale, shear=shear, translation=translation)

        R, W, S = decompose_rws(A)

        assert get_diff(A, R * W * S) < tol
        assert get_diff(S, mkA(0, scale)) < tol
        assert get_diff(R, mkA(a, translation=translation)) < tol