I have a polygon that lies on a 3D plane. i want to get all points enclosed by 开发者_运维问答this polygon.can anyone help me ? i can make a 3D scan line algorithm by replacing the scan lines by planes and get the intersection of planes with my polygon but i want a faster solution. Thanks in advance.
One approach that I can see right now is to shoot an random arbitrary ray from each point that you want to verify and count how many times it intersects with the surface of your 3d mesh. If it is odd - it is inside, if even - outside.
If your polygon is convex, then you can use the following approach:
Each face of the polygon is part of a plane given by the equation ax + by + cz = d
. Find this equation for all faces, modify them to < d
or > d
depending on which relation describes points on the inside of the polygon, then solve this system of linear inequalities. This should give you a set of relations for x, y, and z that only points inside the polygon satisfy.
CygnusX1's answer is the standard point-in-a-polygon test, and can be modified for a variety of different systems (eg. I have a version coded for working on a spheroid). The main trick when adapting it is deciding on the ray direction. 'Abitrary' s a much better word than 'random'. For 2d Euclidean work I would shoot it in a direction parallel to one of the axes (and perpendicular to the other). For a sphere, you use one of the poles. For a 2d planar polygon in 3d, then I would be tempted to choose a line that is perpendicular to one of the axes.
Or I would transform my coordinates so that all calculations are in the plane. This will greatly simplify the actual point-in-polygon test. I think this will also be faster (significantly so for large polygons and many tests): each polygon corner only needs to be transformed once, but it will be used in two tests per point-in-polygon test.
"yes it's a convex 3p polygon ,but it all its points lie in the same plane"
In that case - just convert the polygon and all the test points into 2D local coordinates of the plane and use a 2D algorithm:
2D ray shooting: You can still use similar algorithm to my 3d suggestion - shoot 2D rays originating from your test point and count how many times you hit the border of your polygon.
Linear inequalities: If your polygon is convex, you can follow suszterpatt's approach, with your polygon defined as an intersection of halfplanes
ax+by<d
Further reading:
- http://en.wikipedia.org/wiki/Point_in_polygon
- http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
- http://paulbourke.net/geometry/insidepoly/
Project your polygon onto the (x,y)-plane. Now it's a 2-D problem. Each point (x,y) inside the 2-D projection represents a point (x,y,z) inside the 3-D polygon. Note: If your plane is perpendicular to the (x,y)-plane, this won't work; and if it is nearly perpendicluar, you will get a loss of precision. So in practice, you would project onto the coordinate plane that is least perpendicular to your 3-D plane.
I have seen something similar implemented using one of matplotlib/scipy/numpy. I can't exactly remember the algorithm however, see whether this helps.
精彩评论