2012-01-31 134 views
6

我试图导出一个CATransform3D,它将带有4个角点的四边形映射到具有4个新角点的另一个四边形。我花了一点时间研究这个问题,看起来这些步骤涉及将原始的Quad转换为Square,然后将该Square转换为新的Quad。我的方法是这样的(从here借来的代码):返回CATransform3D将四边形映射到四边形

- (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; 

} 

计算两个矩阵,它们相乘在一起,并在问题分配给视图后,我结束了一个转变的看法,但它是疯狂不正确。事实上,无论我做什么,它似乎都像平行四边形剪切一样。我错过了什么?

UPDATE 12年2月1日

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

+0

更新:我试过现在移植一些算法,并最终得到一个平行四边形每一次。我必须忘记一些事情,但我很茫然。 – sevenflow 2012-02-01 18:07:17

+0

下面是实现此目的的其他方法:http://stackoverflow.com/questions/9470493/transforming-a-rectangle-image-into-a-quadrilateral-using-a-catransform3d让我知道这是否有帮助。 – MonsieurDart 2012-03-27 08:31:15

回答

3

我能够通过移植和组合来自这两个网址,四翘曲和单应的代码来实现这一目标:

更新:我已经开源了一个这样做的小班:https://github.com/dominikhofmann/DHWarpView

+1

嗨,你能否提供一些关于你准确移植和合并的代码的更多信息?我目前正试图解决与你完全相同的问题,但我没有得到任何地方。谢谢! – KPM 2012-02-25 00:54:44

+0

欢迎来到Stack Overflow!虽然这可能会在理论上回答这个问题,[这将是更可取的](http://meta.stackexchange.com/q/8259)在这里包括答案的基本部分,并提供参考链接。 – 2012-03-27 10:43:21

+0

感谢您的反馈。我已经开放了我写的课程来完成这个任务 – sevenflow 2012-06-14 17:07:42