'Mapping coordinates from plane given by normal vector to XY plane
So, I have this algorithm to calculate cross-section of 3D shape with plane given with normal vector.
However, my current problem is, that the cross-section is set of 3D points (all lying on that given plane) and to display it I need to map this coordinates to XY plane.
This works perfect if the plane normal is something like (0,0,c) - I just copy x and y coordinates discarding z.
And here is my question: Since I have no idea how to convert any other plain could anybody give me any hint as to what should I do now?
Solution 1:[1]
Your pane is defined by a normal vector
n=(xn,yn,zn)
For coordination transformation we need 2 base vectors and a zero point for the pane
Base vectors
We chose those "naturally" fitting to the x/y pane (see later for edge case):
b1=(1,0,zb1)
b2=(0,1,zb2)
And we want
b1 x b2 = n*c
(c const scalar)
to make sure these two are really bases
Now solve this:
b1 x b2= (0*zb2-zb1*1,zb1*0-1*zb2,1*1-0*0) = (zb1,zb2,1)
zb1*c=xn
zb2*c=yn
1*c=zn
c=zn,
zb2=yn/c=yn/zn
zb1=xn/c=xn/zn
b1=(1,0,yn/zn)
b2=(0,1,xn/zn)
and normalize it
bv1=(1,0,yn/zn)*sqrt(1+(yn/zn*yn/zn))
bv2=(0,1,yn/zn)*sqrt(1+(xn/zn*xn/zn))
An edge case is, when zn=0: In this case the normal vector is parallel to the x/y pane and no natural base vectors exist, ind this case you have to chose base b1 and b2 vectors by an esthetic POV and go through the same solution process or just chose bv1 and bv2.
Zero point
you spoke of no anchor point for your pane in the OQ, but it is necessary to differentiate your pane from the infinite family of parallel panes.
If your anchor point is (0,0,0) this is a perfect anchor point for the coordinate transformation and your pane has
x*xn+y*yn+z*zn=0,
(y0,y0,z0)=(0,0,0)
If not, I assume you have an anchor point of (xa,ya,za) and your pane has
x*xn+y*yn+z*zn=d
with d const scalar. A natural fit would be the point of the pane, that is defined by normal projection of the original zero point onto the pane:
P0=(x0,y0,z0)
with
(x0, y0, z0) = c * (xn,yn,zn)
Solving this against
x*xn+y*yn+z*zn=d
gives
c*xn*xn+c*yn*yn+c*zn*zn=d
and
c=d/(xn*xn+yn*yn+zn*zn)
thus
P0=(x0,y0,z0)=c*(xn,yn,zn)
is found.
Final transformation
is achieved by representing every point of your pane (i.e. those points you want to show) as
P0+x'*bv1+y'*bv2
with x' and y' being the new coordinates. Since we know P0, bv1 and bv2 this is quite trivial. If we are not on the edge case, we have zeroes in bv1.y and bv2.x further reducing the problem.
x' and y' are the new coordinates you want.
Solution 2:[2]
I would like to add to Eugen's answer, a suggestion for the case where zn=0
extending his answer and also offer an alternative solution (which is similar).
In the case of zn=0
, you can actually think of all the planes as points in a circle around the z-axis and the radius depends on the parameters of the plane.
Any vector orthogonal to the radius should be parallel to the plane, while the radius being the normal of the plane.
So in some way, the problem is reduced to a 2D-space.
The normal to the plane is (xn, yn, 0)
.
By using a technique to find orthogonal vectors in 2D, we get that a base vector could therefore be (-yn, xn, 0)
.
The second base vector is (0, 0, 1)
which is just the normalized vector of their cross product. We can see that by developing the following expression:corss_product((-yn, xn, 0), (xn, yn, 0)) =
(xn*0 - 0*yn, 0*xn - (-yn)*0, (-b)*b - a*a) =
(0, 0, -(xn^2 + yn^2))
.
Which after normalizing and negating becomes (0, 0, 1)
.
From here, I suggest b1=normalize(-yn, xn, 0)
and b2=(0, 0, 1)
.
Now, there's an even more general solution using this approach.
If you'll develop the dot product of (-yn, xn, 0)
and (xn, yn, zn)
, you'll see that they are orthogonal for any zn
while (-yn, xn, 0)
also being part of the plane in question (when d=0
). Thus, this actually works as long at least one of xn
and yn
is not zero (because otherwise (-yn, xn, 0)
is actually just (0, 0, 0)
).
Just to make sure it's clear, the second base vector is again their cross product, that is: b1=(-yn, xn, 0)
and b2=cross_product(b1, n)
.
Well then, what about the case where both xn
and yn
are zero? In this case the plane is parallel to the xy plane. Now that's an easy one, just choose b1=(1, 0, 0)
and b2=(0, 1, 0)
.
And as the other approach, use an anchor vector when d
is not 0, exactly as it is described there, no changes needed.
Summary: 2 different solutions:
- Use Eugen's answer answer and for the case of
zn=0
, take:b1=(-yn, xn, 0)
andb2=(0, 0, 1)
. - A different approach: If both
xn
andyn
equal 0, takeb1=(1, 0, 0)
andb2=(0, 1, 0)
, otherwise takeb1=(-yn, xn, 0)
andb2=cross_product(b1, n)
.
In both solutions, use an anchor vector P0
as described by the aforementioned answer.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eugen Rieck |
Solution 2 |