単位正方形を任意の四角形に変換しようとしています。これが私に遠近変換行列を与えるコードです:
bool Math::GetProjectiveMapping(const FPoint& p0, const FPoint& p1,
const FPoint& p2, const FPoint& p3,
Matrix33& m)
{
/* This function maps a unit square onto a quadrilateral
specified by the four points p0-p3. */
/*
Calculate the projective mapping
p0 p3
┌────┐ | a b c |
│ │ M = | d e f |
└────┘ | g h i |
p1 p2
*/
double Σx = p0.X - p1.X + p2.X - p3.X;
double Σy = p0.Y - p1.Y + p2.Y - p3.Y;
double a, b, c, d, e, f, g, h, i = 1;
if (Σx == 0 && Σy == 0) // affine
{
a = p1.X - p0.X;
b = p2.X - p1.X;
c = p0.X;
d = p1.Y - p0.Y;
e = p2.Y - p1.Y;
f = p0.Y;
g = 0;
h = 0;
}
else // perspective
{
double Δx1 = p1.X - p2.X;
double Δx2 = p3.X - p2.X;
double Δy1 = p1.Y - p2.Y;
double Δy2 = p3.Y - p2.Y;
double del = Δx1 * Δy2 - Δy1 * Δx2;
if (del == 0)
return false;
g = (Σx * Δy2 - Σy * Δx2) / del;
h = (Δx1 * Σy - Δy1 * Σx) / del;
a = p1.X - p0.X + g * p1.X;
b = p3.X - p0.X + h * p3.X;
c = p0.X;
d = p1.Y - p0.Y + g * p1.Y;
e = p3.Y - p0.Y + h * p3.Y;
f = p0.Y;
}
m = Matrix33(
a, b, c,
d, e, f,
g, h, i);
return true;
}
しかし、正方形を変換しようとすると、結果はアフィンになります(オレンジは本来あるべき場所です)。
どうしたの?