Search code examples
python3dgeometrypoint-in-polygon

How to determine if a point lies inside a polygon in 3D space?


I have a 3D point P(x,y,z) and a polygon S defined by arbitrary amount of 3D points. The polygon is not necessary convex (so in my opinion, the solutions provided here do not answer my question), but P is in the same plane as S. Now, I need to determine if the P lies inside of S.

For example:

polygon = np.array([[6173.953125  , 9689.90136719,  298.03326416],
       [6173.95410156, 9689.90136719,  298.09350586],
       [6173.89355469, 9689.86621094,  298.23690796],
       [6173.89355469, 9689.86621094,  298.23690796],
       [6173.83496094, 9689.83398438,  298.5083313 ],
       [6173.89453125, 9689.86816406,  298.38320923],
       [6173.89697266, 9689.87011719,  298.44348145],
       [6173.89697266, 9689.87011719,  298.44348145],
       [6173.953125  , 9689.90136719,  298.03326416],
       [6173.89355469, 9689.86621094,  298.23690796],
       ...
       ])

point = np.array([6171.37079656, 9688.35796064,  309.00229108])

Example Image: Point outside Polygon: enter image description here

In this example, it is obvious that the result should be "False" (point is outside).

Using the point:

point2 = np.array([6173.83496094, 9689.83398438, 297.72579346])

the result should be "True" (point is in polygon).

Example Image: Point inside Polygon: enter image description here

I know, I basically have to solve a simple Point-in-Polygon (PiP) problem. However, is there a package that includes such a function directly? Or how can I transform all the points to apply PiP algorithms provided by shapely or similar packages?


Solution

  • When P is in the same plane as S is guaranteed, make (virtual) projection of polygon and point onto any coordinate plane (not perpendicular to polygon plane). For example, to make projection onto OXZ (suitable for the second example), just ignore Y-components.

    Then use any algorithm intended for 2D (pnpoly)