为什么我的观点变换出来了?

时间:2013-03-20 03:45:37

标签: matrix transform perspective

我正在尝试将单位平方变换为任意四边形。这是给出透视变换矩阵的代码:

bool Math::GetProjectiveMapping(const FPoint& p0, const FPoint& p1, 
                                const FPoint& p2, const FPoint& p3, 
                                Matrix33& m)
{
    /*  This function maps a unit square onto a quadrilateral
        specified by the four points p0-p3.  */

    /*
        Calculate the projective mapping

        p0      p3
          ┌────┐            | a b c |
          │    │        M = | d e f |
          └────┘            | g h i |
        p1      p2
    */

    double Σx = p0.X - p1.X + p2.X - p3.X;
    double Σy = p0.Y - p1.Y + p2.Y - p3.Y;

    double a, b, c, d, e, f, g, h, i = 1;

    if (Σx == 0 && Σy == 0) // affine
    {
        a = p1.X - p0.X;
        b = p2.X - p1.X;
        c = p0.X;
        d = p1.Y - p0.Y;
        e = p2.Y - p1.Y;
        f = p0.Y;
        g = 0;
        h = 0;
    }
    else // perspective
    {
        double Δx1 = p1.X - p2.X;
        double Δx2 = p3.X - p2.X;
        double Δy1 = p1.Y - p2.Y;
        double Δy2 = p3.Y - p2.Y;

        double del = Δx1 * Δy2 - Δy1 * Δx2;

        if (del == 0)
            return false;

        g = (Σx * Δy2 - Σy * Δx2) / del;
        h = (Δx1 * Σy - Δy1 * Σx) / del;

        a = p1.X - p0.X + g * p1.X;
        b = p3.X - p0.X + h * p3.X;
        c = p0.X;
        d = p1.Y - p0.Y + g * p1.Y;
        e = p3.Y - p0.Y + h * p3.Y;
        f = p0.Y;
    }

    m = Matrix33(
        a, b, c,
        d, e, f,
        g, h, i);

    return true;
}

但是当我去改造广场时,结果出来了(橙色是应该的地方):

perspective transform comes out affine

发生了什么事?

1 个答案:

答案 0 :(得分:2)

没关系,这里的数学是正确的,我只是没有对结果向量进行归一化。