2012-01-31 132 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