Skip to content
Advertisement

how to find perpendicular projection of point on a surface in python

I have a bunch of points in 3d space (x,y and z) and want to find their perpendicular projection on a surface in python. My surface is created by four points using the following function:

JavaScript

These are my input points stored as list for creating the surface:

JavaScript

I want to ind the perpedicular projection of the following points in the created surface:

JavaScript

I tried the following code to do so, but it is giving me the horizontal projection while i want to find the perpendilar projection:

JavaScript

In fig I visualized what I want. I just used different colurs for points to make the figure more readable. For the upper thre point I showed arrows to visualize the projection. My code is giving me the points which are at the end of red arrows but I want to find the projection point that are perpendicular to the surface. In fact, my code is only calculating a new x for the projection_point. In the fig green arrows show the direction I want. I want to do so for all the points existing in projection_point. In advance, I do appreciate any help.

enter image description here

Advertisement

Answer

One way to define a plane is by three points P, Q and R. (Four points do not necesarrily lie in the same plane, but yout four points do.) Altenatively, you can define a plane by one point P in the plane and a normal vector n, which you can determine via the cross product.

    n = (Q − P) × (R – P)

Normalize the norml vector, so that you have a unit vector u of length 1:

    u = n   ∕   | n |

You can get the distance d of a point S to the plane from the dot product of the unit normal u with the difference vector from the point in the plane P and S:

    d = (S − P) · u

The distance is signed: It is positive when S is on the side of the plane where u faces and negative when it is on the other side. (It is zero, it S is in the plane, of course.)

You can get the point S′, which is S pojected to the plane, by subtracting d · u from S:

    S′ = S − d · u = S − ((S − P) · u) · u

So, now let’s put that into Python. First, Point and Vector classes. (Aren’t they the sameß You can see it that way, but I find distingishig between them useful: The difference of two Points is a Vector; a Point plus a Vector is a Point; Dot and cross products make sense for Vectors, but nor for Points. In any case I prefer to have a class with x, y and z members over tuples for spatial Vectors.)

Anyway, here goes:

JavaScript

These are just the operations we need for your case. Multiplication does double duty: Multiplying two Vectors yields a scalar dot product; multiplying a Vector and a scalar number yields a scaled Vector.

Your plane, reduced to three points:

JavaScript

The points you want to project:

JavaScript

And the projection code:

JavaScript

This will yield the points projected into the plane:

JavaScript

That plane is infinitely large, so that the projected points don’t necessarily lie between the four points of your surface.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement