Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pre-process 2D polygon to speed up checking if a point is inside a polygon #8549

Open
DuoZhangRobotics opened this issue Oct 15, 2024 · 13 comments

Comments

@DuoZhangRobotics
Copy link

Issue Details

Hi,

Thanks in advance for all your help. My code is currently checking whether a point is inside a polygon or not intensively on the same set of polygons. I am wondering if there are any methods that allow me to preprocess the polygon beforehand so I can do the checking a lot faster later. Thanks again.

Duo

Environment

  • Operating system (Windows/Mac/Linux, 32/64 bits): Ubuntu 22.04
  • Compiler: gcc11.4
  • Release or debug mode: Release
  • Specific flags used (if any):
  • CGAL version: 5.6.1
  • Boost version: 1.74
  • Other libraries versions if used (Eigen, TBB, etc.):
@afabri
Copy link
Member

afabri commented Oct 18, 2024

You can create a 2D constrained triangulation, and mark the triangles that are inside the polygons.
See this example.
You then call the cdt.locate(p) function. If you know all points do a spatial_sort() with them, and when you do locate() give the face handle returned by the previous locate() as hint.

@DuoZhangRobotics
Copy link
Author

Thanks! I'll try it out!

@afabri
Copy link
Member

afabri commented Oct 18, 2024

I did not understand what you mean with "or not intensively on the same set of polygons." Maybe a drawing would help.

@DuoZhangRobotics
Copy link
Author

Hi Andreas,

I have a bunch of polygons that are already fixed. And I have a huge set of points. I want to know whether a point is inside of a polygon or not, for every pair of point and polygon. Since all the polygons will be queried multiple times, I was wondering if there's any method to pre-process the polygons so I can speed up all the checking.

@efifogel
Copy link
Member

efifogel commented Oct 19, 2024 via email

@raphaelsulzer
Copy link

raphaelsulzer commented Oct 19, 2024

If I understand correctly, the OP does not only want to know if a point is inside any polygon, but also inside which one. Say, for example, you want to assign LiDAR points to a set of 2D building footprint polygons.

If I am not mistaken, it is not possible to retrieve the input polygons from face handles returned by the locate function of the 2D constraint triangulation / 2D arrangement?

Something like an AABB tree of a set of polygons could help here. But that does not exist either, right?

@efifogel
Copy link
Member

efifogel commented Oct 19, 2024 via email

@afabri
Copy link
Member

afabri commented Oct 19, 2024

I guess it needs more input from @DuoZhangRobotics
Are the polygons small but spread over a big area? Do all polygons intersect many other polygons?
In case there is a representative data set it might be interesting if you shared it with us.

@afabri
Copy link
Member

afabri commented Oct 20, 2024

image

@DuoZhangRobotics
Copy link
Author

image
image
Sorry for the late reply. These are two simple sets of polygons in my case. They are basically some visible areas from the CGAL's 2D Visiblity library. If I use the locate functions from CGAL, it seems a little bit too slow. If I convert the arrangements of the polygons into the polygon class in CGAL, and then call cgal::bounded_side_2() function, it'll be so much faster. But in this way, I can just use plain C++ implementations with float type, then I guess it'll be faster than CGAL in this case. And I don't think I can convert all the polygons to an arrangement and do batch checking here? Because the polygons have too many intersections among them. In this case, AABB tree or BVH wouldn't help too much, but there will be some improvements. I'm also thinking about using Cuda or avx to parallelize everything. But according to my benchmarking, the AVX code doesn't help much when -O3 is turned on compared with my plain c++ code, but they are both approximately 2times faster than bounded_side_2() function. I guess that's because I'm using the exact kernel in CGAL and only using float for AVX and C++ only code.

@afabri
Copy link
Member

afabri commented Oct 20, 2024

I have a hard time to single out the different polygons. Can you give them different colors and maybe not more than 10 just for identifying them.

@efifogel
Copy link
Member

efifogel commented Oct 20, 2024 via email

@efifogel
Copy link
Member

efifogel commented Oct 20, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants