The problem can be expressed as finding the 3-by-3 matrix M
such that the coordinates of a point P
can be converted between the old coordinate system (P_old
, 3 rows) and the new coordinate system (P_new
, 3 rows). This is an affine transformation:
P_old = Center + M * P_new (1)
The (matrix-vector) multiplication with M
orientates it back to the old system, and adding Center
's coordinates translates it back to the old origin.
The equation (1) can then be turned into:
P_new = M^{-1} * (P_old - Center) (2)
where M^{-1}
is the inverse of M
, to compute the new coordinates from the old ones (the third row will have a 0 if the point belongs to the plane of the triangle).
The matrix M
is made of the coordinates of the new basis in the old system, one basis vector in each column. One must now find such a basis.
This basis can be taken from (this is all pseudo-code):
Renormalizing AB
AB
V1 = ______
|| AB ||
AB
here is meant as the vector AB
(with an arrow on top):
|b_x - a_x|
|b_y - a_y|
|b_z - a_z|
|| . ||
is the Euclidian norm (^2
means the square, not xor):
|| V || = sqrt(V_x^2 + V_y^2 + V_z^2)
AC
(also a vector, defined like AB
), but minus its projection on V1
to make it orthogonal to V1
, and renormalized (this will fail with a division by zero if the triangle is not really a triangle):
AC - (AC.V_1) V1
V2 = _______________________
|| AC - (AC.V_1) V1 ||
M.N
is the scalar product:
M.N = M_x * N_x + M_y * N_y + M_z * N_z
(AC.V_1) V1
is simply the product of a scalar, (AC.V_1)
, with a vector, V1
A third vector that can be taken as the cross product to get a Cartesian coordinate system:
V3 = V1 x V2
Then M can be taken as |V1 V2 V3|
(each Vx
is on 3 rows) and, then its inverse computed to use formula (2).
This transformation (with the inverted M) should both generate new coordinates for the points on the plane of the triangle that have 0 on the third axis (which makes it 2D-coordinates on that plane), and preserve the size in terms of Euclidian norm.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…