Mapping coordinates from plane given by normal vector to XY plane
Asked Answered
S

2

6

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?

Stringent answered 8/1, 2012 at 19:45 Comment(3)
I suspect what you want to do is either projection from on pane (your cross-section) to another (x,y) or a coordinate transformation from one to another - which one?Decosta
Currently I have set of 3D points (x, y, z) (well actually (x,y,z,1)) in 3D coordinate system. What i want is set of 2D points (x', y') (well, again actually (x',y', 1)) such as the (x',y') in XY plane matches (x, y, z) in the 3D plane given by the normal vector. So far I've thought tha if I could rotate the plane given by the vector I could just project the points this way: (x,y,z,1) -> (x,y,1)Stringent
@EugenRieck Hey Eugen, what would you say is the difference between "either projection from on pane (your cross-section) to another (x,y) or a coordinate transformation from one to another"Chameleon
D
8

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.

Decosta answered 8/1, 2012 at 21:17 Comment(4)
Very nice explanation! I faced the same problem and to express the 3D points on the computed basis vectors, I projected them on the basis vectors by using the dot product. x' = pt3d.dot(bv1) y' = pt3d.dot(bv2) Is it correct?Valetudinary
Hi, I think there is a minor error here: b1=(1,0,yn/zn) b2=(0,1,xn/zn) Actually b1 * n ~=0, you miss minus. b1=(1,0,-yn/zn) b2=(0,1,-xn/zn)Tolbert
I think there's a slight mistake in the solution of b1 and b2. You wrote zn1=xn/zn but then substituting it for yn/zn and did the same mistake mirrored with zn2. You also missed some minuses at the cross product. It sould be (-zb1, -zb2, 1). Therefore, the solution should be b1=(1, 0, -xn/zn) and b2=(0, 1, -yn/xn).Thirteen
And I would like to add for novice like me (who took some time to understand this). To actually calculate x' and y', you need to the solve the linear equation system given by: P0 + M*X=B where P0 is the aforementioned anchor, M is a 3x2 matrix where the first column is b1 and the second is b2, X is (x', y')^T, and B is the actual point in question you want to map.Thirteen
T
1

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) and b2=(0, 0, 1).
  • A different approach: If both xn and yn equal 0, take b1=(1, 0, 0) and b2=(0, 1, 0), otherwise take b1=(-yn, xn, 0) and b2=cross_product(b1, n).

In both solutions, use an anchor vector P0 as described by the aforementioned answer.

Thirteen answered 21/4, 2022 at 11:37 Comment(1)
any code to share? thanks!Prana

© 2022 - 2024 — McMap. All rights reserved.