返回 CATransform3D 将四边形映射到四边形

发布于 2024-12-29 22:21:05 字数 2849 浏览 3 评论 0原文

我正在尝试派生一个 CATransform3D,它将具有 4 个角点的四边形映射到另一个具有 4 个新角点的四边形。我花了一点时间研究这个,似乎步骤涉及将原始四边形转换为正方形,然后将该正方形转换为新的四边形。我的方法如下所示(从此处借用的代码):

- (CATransform3D)quadFromSquare_x0:(float)x0 y0:(float)y0 x1:(float)x1 y1:(float)y1 x2:(float)x2 y2:(float)y2 x3:(float)x3 y3:(float)y3 {

    float dx1 = x1 - x2,    dy1 = y1 - y2;
    float dx2 = x3 - x2,    dy2 = y3 - y2;
    float sx = x0 - x1 + x2 - x3;
    float sy = y0 - y1 + y2 - y3;
    float g = (sx * dy2 - dx2 * sy) / (dx1 * dy2 - dx2 * dy1);
    float h = (dx1 * sy - sx * dy1) / (dx1 * dy2 - dx2 * dy1);
    float a = x1 - x0 + g * x1;
    float b = x3 - x0 + h * x3;
    float c = x0;
    float d = y1 - y0 + g * y1;
    float e = y3 - y0 + h * y3;
    float f = y0;

    CATransform3D mat;

    mat.m11 = a;
    mat.m12 = b;
    mat.m13 = 0;
    mat.m14 = c;

    mat.m21 = d;
    mat.m22 = e;
    mat.m23 = 0;
    mat.m24 = f;

    mat.m31 = 0;
    mat.m32 = 0;
    mat.m33 = 1;
    mat.m34 = 0;

    mat.m41 = g;
    mat.m42 = h;
    mat.m43 = 0;
    mat.m44 = 1;

    return mat;

}

- (CATransform3D)squareFromQuad_x0:(float)x0 y0:(float)y0 x1:(float)x1 y1:(float)y1 x2:(float)x2 y2:(float)y2 x3:(float)x3 y3:(float)y3 {

    CATransform3D mat = [self quadFromSquare_x0:x0 y0:y0 x1:x1 y1:y1 x2:x2 y2:y2 x3:x3 y3:y3];

    // invert through adjoint

    float a = mat.m11,      d = mat.m21,    /* ignore */            g = mat.m41;
    float b = mat.m12,      e = mat.m22,    /* 3rd col*/            h = mat.m42;
    /* ignore 3rd row */
    float c = mat.m14,      f = mat.m24;

    float A =     e - f * h;
    float B = c * h - b;
    float C = b * f - c * e;
    float D = f * g - d;
    float E =     a - c * g;
    float F = c * d - a * f;
    float G = d * h - e * g;
    float H = b * g - a * h;
    float I = a * e - b * d;

    // Probably unnecessary since 'I' is also scaled by the determinant,
    //   and 'I' scales the homogeneous coordinate, which, in turn,
    //   scales the X,Y coordinates.
    // Determinant  =   a * (e - f * h) + b * (f * g - d) + c * (d * h - e * g);
    float idet = 1.0f / (a * A           + b * D           + c * G);

    mat.m11 = A * idet;     mat.m21 = D * idet;     mat.m31 = 0;    mat.m41 = G * idet;
    mat.m12 = B * idet;     mat.m22 = E * idet;     mat.m32 = 0;    mat.m42 = H * idet;
    mat.m13 = 0       ;     mat.m23 = 0       ;     mat.m33 = 1;    mat.m43 = 0       ;
    mat.m14 = C * idet;     mat.m24 = F * idet;     mat.m34 = 0;    mat.m44 = I * idet;

    return mat;

}

在计算两个矩阵、将它们相乘并分配给相关视图之后,我最终得到了一个转换后的视图,但它是非常不正确的。事实上,无论我做什么,它似乎都像平行四边形一样被剪切。我缺少什么?

更新 2/1/12

看来我遇到问题的原因可能是我需要将 FOV 和焦距容纳到模型视图矩阵中(这是我可以直接更改的唯一矩阵) (在 Quartz 中。)不过,我没有运气在网上找到有关如何计算正确矩阵的文档。

I'm trying to derive a CATransform3D that will map a quad with 4 corner points to another quad with 4 new corner points. I've spent a little bit of time researching this and it seems the steps involve converting the original Quad to a Square, and then converting that Square to the new Quad. My methods look like this (code borrowed from here):

- (CATransform3D)quadFromSquare_x0:(float)x0 y0:(float)y0 x1:(float)x1 y1:(float)y1 x2:(float)x2 y2:(float)y2 x3:(float)x3 y3:(float)y3 {

    float dx1 = x1 - x2,    dy1 = y1 - y2;
    float dx2 = x3 - x2,    dy2 = y3 - y2;
    float sx = x0 - x1 + x2 - x3;
    float sy = y0 - y1 + y2 - y3;
    float g = (sx * dy2 - dx2 * sy) / (dx1 * dy2 - dx2 * dy1);
    float h = (dx1 * sy - sx * dy1) / (dx1 * dy2 - dx2 * dy1);
    float a = x1 - x0 + g * x1;
    float b = x3 - x0 + h * x3;
    float c = x0;
    float d = y1 - y0 + g * y1;
    float e = y3 - y0 + h * y3;
    float f = y0;

    CATransform3D mat;

    mat.m11 = a;
    mat.m12 = b;
    mat.m13 = 0;
    mat.m14 = c;

    mat.m21 = d;
    mat.m22 = e;
    mat.m23 = 0;
    mat.m24 = f;

    mat.m31 = 0;
    mat.m32 = 0;
    mat.m33 = 1;
    mat.m34 = 0;

    mat.m41 = g;
    mat.m42 = h;
    mat.m43 = 0;
    mat.m44 = 1;

    return mat;

}

- (CATransform3D)squareFromQuad_x0:(float)x0 y0:(float)y0 x1:(float)x1 y1:(float)y1 x2:(float)x2 y2:(float)y2 x3:(float)x3 y3:(float)y3 {

    CATransform3D mat = [self quadFromSquare_x0:x0 y0:y0 x1:x1 y1:y1 x2:x2 y2:y2 x3:x3 y3:y3];

    // invert through adjoint

    float a = mat.m11,      d = mat.m21,    /* ignore */            g = mat.m41;
    float b = mat.m12,      e = mat.m22,    /* 3rd col*/            h = mat.m42;
    /* ignore 3rd row */
    float c = mat.m14,      f = mat.m24;

    float A =     e - f * h;
    float B = c * h - b;
    float C = b * f - c * e;
    float D = f * g - d;
    float E =     a - c * g;
    float F = c * d - a * f;
    float G = d * h - e * g;
    float H = b * g - a * h;
    float I = a * e - b * d;

    // Probably unnecessary since 'I' is also scaled by the determinant,
    //   and 'I' scales the homogeneous coordinate, which, in turn,
    //   scales the X,Y coordinates.
    // Determinant  =   a * (e - f * h) + b * (f * g - d) + c * (d * h - e * g);
    float idet = 1.0f / (a * A           + b * D           + c * G);

    mat.m11 = A * idet;     mat.m21 = D * idet;     mat.m31 = 0;    mat.m41 = G * idet;
    mat.m12 = B * idet;     mat.m22 = E * idet;     mat.m32 = 0;    mat.m42 = H * idet;
    mat.m13 = 0       ;     mat.m23 = 0       ;     mat.m33 = 1;    mat.m43 = 0       ;
    mat.m14 = C * idet;     mat.m24 = F * idet;     mat.m34 = 0;    mat.m44 = I * idet;

    return mat;

}

After calculating both matrices, multiplying them together, and assigning to the view in question, I end up with a transformed view, but it is wildly incorrect. In fact, it seems to be sheared like a parallelogram no matter what I do. What am I missing?

UPDATE 2/1/12

It seems the reason I'm running into issues may be that I need to accommodate for FOV and focal length into the model view matrix (which is the only matrix I can alter directly in Quartz.) I'm not having any luck finding documentation online on how to calculate the proper matrix, though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗心人 2025-01-05 22:21:05

我能够通过移植和组合来自这两个 URL 的四边形变形和单应性代码来实现此目的:

更新:我开源了一个小类来执行此操作: https://github.com/dominikhofmann/DHWarpView

I was able to achieve this by porting and combining the quad warping and homography code from these two URLs:

UPDATE: I've open sourced a small class that does this: https://github.com/dominikhofmann/DHWarpView

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文