问题
I have a point cloud, all the points lie on a plane in 3D Space. I need to convert each point to 2D Coordinates and vice versa.
(x,y,z) in Coordinate System A => Transformation Matrix (T1) => (x,y) in Coordinate System B
(x,y) in Coordinate System B => Transformation Matrix (T2) => (x,y,z) in Coordinate System A
I need T1 and T2. The coordinate system B can be any arbitrary reference frame.
回答1:
As far as I understand, all points lie in the same plane, and you want to reduce dimension and later restore coordinates.
Get three non-collinear points A, B, C. Make vectors AB and AC.
Normal to that plane is
N = AB x AC //cross product
Now normalize vectors AB and N getting unit U = uAB
and uN
. Build the second base vector (it is unit and lies in the plane)
V = U x uN
Now you have four basis points A, u=A+U, v=A+V, n=A+uN
Tranformation should map these points into quadruplet (0,0,0), (1,0,0), (0,1,0), (0,0,1)
correspondingly.
Now about affine transformation matrix to make this mapping:
[Ax ux vx nx] [0 1 0 0]
M * [Ay uy vy ny] = [0 0 1 0]
[Az uz vz nz] [0 0 0 1]
[1 1 1 1 ] [1 1 1 1]
or
M * S = D
M * S * Sinv = D * Sinv
M = D * Sinv
So calculate inverse matrix for S=[Ax ux...]
and get needed matrix M.
Application of M to any point in the plane gives new coordinates with zero z-component.
Application of inverse of M to (x,y,0) results 3D coordinates in given plane.
Maple sheet with points A=1,1,1 B=2,1,1 C=1,1,2 (in plane Y=1)
new coordinates AA, BB, CC have zero z-component.
For arbitrary point in the same plane z-component after mapping is zero too.
P:=vector([-2,1,7,1]);
> PP := multiply(M, P);
PP := [-3, 6, 0, 1]
回答2:
To help clarify the process of moving from 3d to 2d and back from 2d to 3d in Visual Studio flavor and DxMath to transform 3d coordinates on a plane to 2d coordinates. For example, to help use 2d triangulation algorithms in the 3d world.
XMVECTOR pt1; // A
XMVECTOR pt2; // B
XMVECTOR pt3; // C
pt1 = XMLoadFloat3((const XMFLOAT3*) &lpPoints[0]);
pt2 = XMLoadFloat3((const XMFLOAT3*) &lpPoints[1]);
pt3 = XMLoadFloat3((const XMFLOAT3*) &lpPoints[2]);
TRACE("A = [ %g %g %g ] \n", XMVectorGetX(pt1), XMVectorGetY(pt1), XMVectorGetZ(pt1));
TRACE("B = [ %g %g %g ] \n", XMVectorGetX(pt2), XMVectorGetY(pt2), XMVectorGetZ(pt2));
TRACE("C = [ %g %g %g ] \n", XMVectorGetX(pt3), XMVectorGetY(pt3), XMVectorGetZ(pt3));
XMVECTOR AB = XMVectorSubtract(pt2, pt1);
TRACE("AB = [ %g %g %g ] \n", XMVectorGetX(AB), XMVectorGetY(AB), XMVectorGetZ(AB));
XMVECTOR AC = XMVectorSubtract(pt3, pt1);
TRACE("AC = [ %g %g %g ] \n", XMVectorGetX(AC), XMVectorGetY(AC), XMVectorGetZ(AC));
XMVECTOR N = XMVector3Cross(AB, AC);
XMVECTOR Nnormal = XMVector3Normalize(N);
XMVECTOR ABnormal = XMVector3Normalize(AB);
XMVECTOR V = XMVector3Cross(ABnormal, Nnormal);
TRACE("Nnormalized = [ %g %g %g ] \n", XMVectorGetX(Nnormal), XMVectorGetY(Nnormal), XMVectorGetZ(Nnormal));
TRACE("ABnormalized = [ %g %g %g ] \n", XMVectorGetX(ABnormal), XMVectorGetY(ABnormal), XMVectorGetZ(ABnormal));
TRACE("V=AB cross N = [ %g %g %g ] \n\n", XMVectorGetX(V), XMVectorGetY(V), XMVectorGetZ(V));
XMMATRIX D;
D._11 = 0;
D._12 = 1.0F;
D._13 = 0;
D._14 = 0;
D._21 = 0;
D._22 = 0;
D._23 = 1.0F;
D._24 = 0;
D._31 = 0;
D._32 = 0;
D._33 = 0;
D._34 = 1.0F;
D._41 = 1.0F;
D._42 = 1.0F;
D._43 = 1.0F;
D._44 = 1.0F;
XMMATRIX S;
S._11 = XMVectorGetX(pt1);
S._12 = XMVectorGetX(pt1) + XMVectorGetX(ABnormal);
S._13 = XMVectorGetX(pt1) + XMVectorGetX(V);
S._14 = XMVectorGetX(pt1) + XMVectorGetX(Nnormal);
S._21 = XMVectorGetY(pt1);
S._22 = XMVectorGetY(pt1) + XMVectorGetY(ABnormal);
S._23 = XMVectorGetY(pt1) + XMVectorGetY(V);
S._24 = XMVectorGetY(pt1) + XMVectorGetY(Nnormal);
S._31 = XMVectorGetZ(pt1);
S._32 = XMVectorGetZ(pt1) + XMVectorGetZ(ABnormal);
S._33 = XMVectorGetZ(pt1) + XMVectorGetZ(V);
S._34 = XMVectorGetZ(pt1) + XMVectorGetZ(Nnormal);
S._41 = 1.0F;
S._42 = 1.0F;
S._43 = 1.0F;
S._44 = 1.0F;
TRACE("S = [ %g %g %g %g ] \n", S._11, S._12, S._13, S._14);
TRACE(" [ %g %g %g %g ] \n", S._21, S._22, S._23, S._24);
TRACE(" [ %g %g %g %g ] \n", S._31, S._32, S._33, S._34);
TRACE(" [ %g %g %g %g ] \n\n", S._41, S._42, S._43, S._44);
TRACE("D = [ %g %g %g %g ] \n", D._11, D._12, D._13, D._14);
TRACE(" [ %g %g %g %g ] \n", D._21, D._22, D._23, D._24);
TRACE(" [ %g %g %g %g ] \n", D._31, D._32, D._33, D._34);
TRACE(" [ %g %g %g %g ] \n\n", D._41, D._42, D._43, D._44);
XMMATRIX Sinv;
XMVECTOR det;
Sinv = XMMatrixInverse(&det, S);
TRACE("Sinv = [ %g %g %g %g ] \n", Sinv._11, Sinv._12, Sinv._13, Sinv._14);
TRACE(" [ %g %g %g %g ] \n", Sinv._21, Sinv._22, Sinv._23, Sinv._24);
TRACE(" [ %g %g %g %g ] \n", Sinv._31, Sinv._32, Sinv._33, Sinv._34);
TRACE(" [ %g %g %g %g ] \n\n", Sinv._41, Sinv._42, Sinv._43, Sinv._44);
XMMATRIX M;
M = XMMatrixMultiply(D, Sinv);
TRACE("M = [ %g %g %g %g ] \n", M._11, M._12, M._13, M._14);
TRACE(" [ %g %g %g %g ] \n", M._21, M._22, M._23, M._24);
TRACE(" [ %g %g %g %g ] \n", M._31, M._32, M._33, M._34);
TRACE(" [ %g %g %g %g ] \n\n", M._41, M._42, M._43, M._44);
M = XMMatrixTranspose(M); // DxMath, without this XMVector4Transform below won't work
XMMATRIX Minverse;
Minverse = XMMatrixInverse(&det, M);
list<MyPoly> testpolys;
MyPoly polystwod;
polystwod.Init(nPoints);
XMVECTOR twod;
for (int p = 0; p < nPoints; p++)
{
XMVECTOR pt = XMLoadFloat3((const XMFLOAT3*)&lpPoints[p]);
pt = XMVectorSetW(pt, 1.0F);
twod = XMVector4Transform(pt, M);
TRACE("Initial %g %g %g ", XMVectorGetX(pt), XMVectorGetY(pt), XMVectorGetZ(pt));
TRACE("Initial * M = %g %g %g \n", XMVectorGetX(twod), XMVectorGetY(twod), XMVectorGetZ(twod));
// pass 2D data
polystwod[p].x = XMVectorGetX(twod);
polystwod[p].y = XMVectorGetY(twod);
}
testpolys.push_back(polystwod);
// pass to your 2d triangulation routine
// then back solve 2d points into the original 3d coordinate system
XMVECTOR threed;
list<MyPoly>::iterator iter;
for (iter = testpolys.begin(); iter != testpolys.end(); iter++)
{
MyPoly w;
w = *iter;
ThreeFloats w3;
int i, cntpoints;
cntpoints = w.GetSize();
for (i = 0; i < cntpoints; i++)
{
w3.X = w[i].x; // initialize a 2d point
w3.Y = w[i].y;
w3.Z = 0.0F;
XMVECTOR pt = XMLoadFloat3((const XMFLOAT3*)&w3); pt = XMVectorSetW(pt, 1.0F);
threed = XMVector4Transform(pt, Minverse);
TRACE("Back Solving Back %g %g %g ", w3.X, w3.Y, w3.Z);
TRACE("Back * Minverse = %g %g %g \n", XMVectorGetX(threed), XMVectorGetY(threed), XMVectorGetZ(threed));
// variable threed is now back in original 3d coordinate system
}
}
IMMEDIATE WINDOW OUTPUT
A = [ 0.3 -0.45 0.5 ]
B = [ 0.1 -0.45 0.5 ]
C = [ -0.1 0.15 0.5 ]
AB = [ -0.2 0 0 ]
AC = [ -0.4 0.6 0 ]
Nnormalized = [ 0 0 -1 ]
ABnormalized = [ -1 0 0 ]
V=AB cross N = [ -0 -1 -0 ]
S = [ 0.3 -0.7 0.3 0.3 ]
[ -0.45 -0.45 -1.45 -0.45 ]
[ 0.5 0.5 0.5 -0.5 ]
[ 1 1 1 1 ]
D = [ 0 1 0 0 ]
[ 0 0 1 0 ]
[ 0 0 0 1 ]
[ 1 1 1 1 ]
Sinv = [ 1 1 1 0.65 ]
[ -1 0 0 0.3 ]
[ 0 -1 0 -0.45 ]
[ 0 0 -1 0.5 ]
M = [ -1 0 0 0.3 ]
[ 0 -1 0 -0.45 ]
[ 0 0 -1 0.5 ]
[ 0 0 0 1 ]
Initial 0.3 -0.45 0.5 Initial * M = 0 0 0
Initial 0.1 -0.45 0.5 Initial * M = 0.2 0 0
Initial -0.1 0.15 0.5 Initial * M = 0.4 -0.6 0
Initial 0.1 0.35 0.5 Initial * M = 0.2 -0.8 0
Initial 0.3 0.35 0.5 Initial * M = 0 -0.8 0
Back Solving Back 0 0 0 Back * Minverse = 0.3 -0.45 0.5
Back Solving Back 0.2 0 0 Back * Minverse = 0.1 -0.45 0.5
Back Solving Back 0.4 -0.6 0 Back * Minverse = -0.1 0.15 0.5
Back Solving Back 0.2 -0.8 0 Back * Minverse = 0.1 0.35 0.5
Back Solving Back 0 -0.8 0 Back * Minverse = 0.3 0.35 0.5
SWAPPING Z AND X
A = [ 0.5 -0.45 0.3 ]
B = [ 0.5 -0.45 0.1 ]
C = [ 0.5 0.15 -0.1 ]
AB = [ 0 0 -0.2 ]
AC = [ 0 0.6 -0.4 ]
Nnormalized = [ 1 0 0 ]
ABnormalized = [ 0 0 -1 ]
V=AB cross N = [ 0 -1 0 ]
S = [ 0.5 0.5 0.5 1.5 ]
[ -0.45 -0.45 -1.45 -0.45 ]
[ 0.3 -0.7 0.3 0.3 ]
[ 1 1 1 1 ]
D = [ 0 1 0 0 ]
[ 0 0 1 0 ]
[ 0 0 0 1 ]
[ 1 1 1 1 ]
Sinv = [ -1 1 1 1.65 ]
[ 0 0 -1 0.3 ]
[ 0 -1 0 -0.45 ]
[ 1 0 -0 -0.5 ]
M = [ 0 0 -1 0.3 ]
[ 0 -1 0 -0.45 ]
[ 1 0 0 -0.5 ]
[ 0 0 0 1 ]
Initial 0.5 -0.45 0.3 Initial * M = 0 2.98023e-008 0
Initial 0.5 -0.45 0.1 Initial * M = 0.2 2.98023e-008 0
Initial 0.5 0.15 -0.1 Initial * M = 0.4 -0.6 0
Initial 0.5 0.35 0.1 Initial * M = 0.2 -0.8 0
Initial 0.5 0.35 0.3 Initial * M = 0 -0.8 0
Back Solving Back 0 2.98023e-008 0 Back * Minverse = 0.5 -0.45 0.3
Back Solving Back 0.2 2.98023e-008 0 Back * Minverse = 0.5 -0.45 0.1
Back Solving Back 0.4 -0.6 0 Back * Minverse = 0.5 0.15 -0.1
Back Solving Back 0.2 -0.8 0 Back * Minverse = 0.5 0.35 0.1
Back Solving Back 0 -0.8 0 Back * Minverse = 0.5 0.35 0.3
ANOTHER TEST CONSTANT Y
A = [ -0.9 0.25 0.3 ]
B = [ -0.9 0.25 0.1 ]
C = [ 0.3 0.25 -0.1 ]
AB = [ 0 0 -0.2 ]
AC = [ 1.2 0 -0.4 ]
Nnormalized = [ 0 -1 0 ]
ABnormalized = [ 0 0 -1 ]
V=AB cross N = [ -1 -0 -0 ]
S = [ -0.9 -0.9 -1.9 -0.9 ]
[ 0.25 0.25 0.25 -0.75 ]
[ 0.3 -0.7 0.3 0.3 ]
[ 1 1 1 1 ]
D = [ 0 1 0 0 ]
[ 0 0 1 0 ]
[ 0 0 0 1 ]
[ 1 1 1 1 ]
Sinv = [ 1 1 1 1.35 ]
[ 0 2.98023e-008 -1 0.3 ]
[ -1 0 0 -0.9 ]
[ 0 -1 0 0.25 ]
M = [ 0 2.98023e-008 -1 0.3 ]
[ -1 0 0 -0.9 ]
[ 0 -1 0 0.25 ]
[ 0 0 0 1 ]
Initial -0.9 0.25 0.3 Initial * M = -2.23517e-008 0 0
Initial -0.9 0.25 0.1 Initial * M = 0.2 0 0
Initial 0.3 0.25 -0.1 Initial * M = 0.4 -1.2 0
Initial 0.7 0.25 0.1 Initial * M = 0.2 -1.6 0
Initial 0.7 0.25 0.3 Initial * M = -2.23517e-008 -1.6 0
Back Solving Back -2.23517e-008 0 0 Back * Minverse = -0.9 0.25 0.3
Back Solving Back 0.2 0 0 Back * Minverse = -0.9 0.25 0.1
Back Solving Back 0.4 -1.2 0 Back * Minverse = 0.3 0.25 -0.1
Back Solving Back 0.2 -1.6 0 Back * Minverse = 0.7 0.25 0.1
Back Solving Back -2.23517e-008 -1.6 0 Back * Minverse = 0.7 0.25 0.3
ONE FINAL TEST
A = [ 1 1 1 ]
B = [ 2 1 1 ]
C = [ 1 1 2 ]
AB = [ 1 0 0 ]
AC = [ 0 0 1 ]
Nnormalized = [ 0 -1 0 ]
ABnormalized = [ 1 0 0 ]
V=AB cross N = [ 0 0 -1 ]
S = [ 1 2 1 1 ]
[ 1 1 1 0 ]
[ 1 1 0 1 ]
[ 1 1 1 1 ]
D = [ 0 1 0 0 ]
[ 0 0 1 0 ]
[ 0 0 0 1 ]
[ 1 1 1 1 ]
Sinv = [ -1 1 1 0 ]
[ 1 0 0 -1 ]
[ 0 0 -1 1 ]
[ 0 -1 0 1 ]
M = [ 1 0 0 -1 ]
[ 0 0 -1 1 ]
[ 0 -1 0 1 ]
[ 0 0 0 1 ]
Initial 1 1 1 Initial * M = 0 0 0
Initial 2 1 1 Initial * M = 1 0 0
Initial 1 1 2 Initial * M = 0 -1 0
Initial -2 1 7 Initial * M = -3 -6 0
Back Solving Back 0 0 0 Back * Minverse = 1 1 1
Back Solving Back 1 0 0 Back * Minverse = 2 1 1
Back Solving Back 0 -1 0 Back * Minverse = 1 1 2
Back Solving Back -3 -6 0 Back * Minverse = -2 1 7
来源:https://stackoverflow.com/questions/49769459/convert-points-on-a-3d-plane-to-2d-coordinates