matrix - 为什么我的视角变换结果是仿射的?

标签 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

发生什么事了?

最佳答案

没关系,这里的数学是正确的,我只是没有标准化结果向量。

关于matrix - 为什么我的视角变换结果是仿射的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15514905/

相关文章:

perspective - CATransform3D 的 m34 的含义

java - 脚本错误: "An error has occured in the script on the page"

hadoop - 在mahout中输出项目 Material 相似度矩阵

matlab - 在 Matlab 中求解矩阵的未知数

javascript - 使用 D3.js 更改 SVG 中的变换值

html - 旋转的 div 不粘在视口(viewport)的右边界上(向左平坦)

c++ - 在构造函数中初始化对矩阵

python - 创建一个每个元素都相同的大矩阵

html - 如何在不破坏布局的 "flow"的情况下使用 css 旋转元素?

eclipse - 在 Debug模式下启动服务器时阻止 Eclipse 自动打开调试透视图