CATransform 3D 绝对可以完成您尝试使用它的目的。我测试了您链接到的代码,它对我来说非常有效。请记住,像这样的变换矩阵仅定义为一个比例,因为它位于齐次坐标中。一旦你用他的方程生成矩阵,将每个元素除以右下角的元素。我能想到你需要转置的唯一原因是因为他给出的变换是行主要顺序的。如果您要填充列主要变换矩阵(我相信 CATransform3D 是),则需要在填充后对其进行转置。
这是我用来测试它的代码,它使用来自 openCV 的矩阵类并且在 c++ 中,但应该证明这一点
cv::Matx41d rect_tl(-10,-10,0,1);
cv::Matx41d rect_tr(10,-10,0,1);
cv::Matx41d rect_bl(-10,10,0,1);
cv::Matx41d rect_br(10,10,0,1);
cv::Matx41d quad_tl(2,2,0,1);
cv::Matx41d quad_tr(4,6,0,1);
cv::Matx41d quad_bl(2,-1,0,1);
cv::Matx41d quad_br(3,5,0,1);
double X = rect_tl(0);
double Y = rect_tl(0);
double W = 20;
double H = 20;
double x1a = quad_tl(0);
double y1a = quad_tl(1);
double x2a = quad_tr(0);
double y2a = quad_tr(1);
double x3a = quad_bl(0);
double y3a = quad_bl(1);
double x4a = quad_br(0);
double y4a = quad_br(1);
double y21 = y2a - y1a,
y32 = y3a - y2a,
y43 = y4a - y3a,
y14 = y1a - y4a,
y31 = y3a - y1a,
y42 = y4a - y2a;
double a = -H*(x2a*x3a*y14 + x2a*x4a*y31 - x1a*x4a*y32 + x1a*x3a*y42);
double b = W*(x2a*x3a*y14 + x3a*x4a*y21 + x1a*x4a*y32 + x1a*x2a*y43);
double c = H*X*(x2a*x3a*y14 + x2a*x4a*y31 - x1a*x4a*y32 + x1a*x3a*y42) - H*W*x1a*(x4a*y32 - x3a*y42 + x2a*y43) - W*Y*(x2a*x3a*y14 + x3a*x4a*y21 + x1a*x4a*y32 + x1a*x2a*y43);
double d = H*(-x4a*y21*y3a + x2a*y1a*y43 - x1a*y2a*y43 - x3a*y1a*y4a + x3a*y2a*y4a);
double e = W*(x4a*y2a*y31 - x3a*y1a*y42 - x2a*y31*y4a + x1a*y3a*y42);
double f = -(W*(x4a*(Y*y2a*y31 + H*y1a*y32) - x3a*(H + Y)*y1a*y42 + H*x2a*y1a*y43 + x2a*Y*(y1a - y3a)*y4a + x1a*Y*y3a*(-y2a + y4a)) - H*X*(x4a*y21*y3a - x2a*y1a*y43 + x3a*(y1a - y2a)*y4a + x1a*y2a*(-y3a + y4a)));
double g = H*(x3a*y21 - x4a*y21 + (-x1a + x2a)*y43);
double h = W*(-x2a*y31 + x4a*y31 + (x1a - x3a)*y42);
double i = W*Y*(x2a*y31 - x4a*y31 - x1a*y42 + x3a*y42) + H*(X*(-(x3a*y21) + x4a*y21 + x1a*y43 - x2a*y43) + W*(-(x3a*y2a) + x4a*y2a + x2a*y3a - x4a*y3a - x2a*y4a + x3a*y4a));
cv::Matx44d matrix(a,b,0,c
,d,e,0,f
,0,0,1,0
,g,h,0,i);
matrix = matrix*(1/matrix(15));
//You may need a transpose here
cv::Matx41d test_tl = matrix*rect_tl;
test_tl *= (1/test_tl(3));
cv::Matx41d test_tr = matrix*rect_tr;
test_tr *= (1/test_tr(3));
cv::Matx41d test_bl = matrix*rect_bl;
test_bl *= (1/test_bl(3));
cv::Matx41d test_br = matrix*rect_br;
test_br *= (1/test_br(3));
执行后,底部的所有测试变量都与四边形对应项完美匹配。希望这可以解决问题。