6

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.

sevenflow
  • 767
  • 1
  • 6
  • 21
  • Update: I've tried porting a few algorithms now and end up with a parallelogram every single time. There's got to be something I'm forgetting, but I'm at a loss. – sevenflow Feb 01 '12 at 18:07
  • Here is an other way to achieve this: http://stackoverflow.com/questions/9470493/transforming-a-rectangle-image-into-a-quadrilateral-using-a-catransform3d Let me know if this helps. – MonsieurDart Mar 27 '12 at 08:31

1 Answers1

3

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

sevenflow
  • 767
  • 1
  • 6
  • 21
  • 1
    Hi, could you give a little more information about which code exactly you ported and combined? I'm currently trying to solve the exact same problem as you, but I'm not getting anywhere. Thanks! – KPM Feb 25 '12 at 00:54
  • Welcome to Stack Overflow! While this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Bill the Lizard Mar 27 '12 at 10:43
  • Thanks for the feedback. I've open sourced the class I wrote to accomplish this – sevenflow Jun 14 '12 at 17:07