finding which polygon edge intersects with a given line - cgal

I am doing this to find the polygon edge which intersects with a given line:
step(1) use shapely.intersection to find the intersection point(s) between a polygon and a line.
step(2) use shapely.intersects and brute-force iterate all polygon edges to see which polygon edge(s) intersects with the line.
I think I can optimize the "polygon edge iteration" by using algo like binary search etc.
But is there any existing function that all ready does this in shapely or CGAL or GEOS or any other lib in any language? thanks!

Related

CGAL Mean_curvature_flow_skeletonization contract_until_convergence function produces branches that does not exist in input polygon

I use contract_until_convergence function from CGAL Mean_curvature_flow_skeletonization to produce skeleton from input polygon.
https://doc.cgal.org/latest/Surface_mesh_skeletonization/classCGAL_1_1Mean__curvature__flow__skeletonization.html
In some cases the skeleton creates branches (see top of the image above, skeleton in red color) that does not exist in input polygon. Is there some parameters to set to prevent this ?
using Skeletonization = CGAL::Mean_curvature_flow_skeletonization<Polyhedron>;
Skeletonization mean_curve_skeletonizer(polyhedron);
mean_curve_skeletonizer.contract_until_convergence();
There are two parameters controlling the quality of the skeleton:
quality_speed_tradeoff()
medially_centered_speed_tradeoff()
Also one thing that affect the skeleton is the sampling of the input surface that is used to compute Voronoi poles. In the original papers, it is said: Given a sufficiently good sampling, the Voronoi poles [ACK00] form a provably convergent sampling of the medial axis.
[ACK00]AMENTAN., CHOIS., KOLLURIR. K.: The powercrust, unions of balls, and the medial axis transform. Computational Geometry: Theory and Applications 19(2000), 127–153.3
You can use the function isotropic_remeshing with a sufficiently small target edge length to improve the Voronoi pole computation.

How to convert a path to a CGAL structure

Given a path composed of the following statements:
moveto
lineto
closepath
How would you convert a path to a list of CGAL::Polygon_with_holes_2?
To be more concrete. The path may be the output of the linearization of the outlines of a string of glyphs. Consider e.g. the text string "xo" turned into a such a path. This will result in 3 disjoined closed polygon:
A counterclockwise polygon corresponding to the "x"
A counter clockwise (almost circular) polygon corresponding to the "o"
A clockwise (also almost circular, but smaller) path corresponding to the hole in "o"
If I understand the documentation of CGAL correctly this may be stored in CGAL as two CGAL::Polygon_with_holes2. But how do you you construct these given the path with the three polygons as described above? Is there a convenience function for this or do I have to check for cross intersection of all path polygons?
You can use the following constructor that takes the outer boundary polygon (which must be counterclockwise oriented) and the range of holes (that are also Polygon_2 object but clockwise oriented).
You can construct a Polygon_2 using a range of 2D points.

Matplotlib Polygon gets fill outside of polygon

I have a collection of polygons, created with scipy.spatial.Voronoi (specifically, a subset of the Voronoi regions), which I'd like to plot with matplotlib. However, it seems like there are some constraints on the vertex order of the matplotlib polygons, since some of the polygons end up with the fill on the outside of the polygon rather than the inside. In these cases, reversing the order the vertices are specified seems to fix the problem, so it seems to me like a winding issue (even if the docs don't mention anything like this).
However, since some polygons are in the right order and some are in the wrong order, I can't just reverse all the vertex lists, so is there a way I can detect the incorrectly wound lists and fix only those or alternatively a way to get matplotlib to do the equivalent thing automatically?
ImportanceOfBeingErnest's comment put me on the right track, which in turn led me to How to determine if a list of polygon points are in clockwise order?. Basically, we find the bottom-rightmost point in the polygon P, the point A before P, and the point B after P. The sign of the cross product AP x PB gives the winding: positive in case of CCW winding and negative for CW winding.

How to map the node identities of my resulting surface mesh generated from Poisson_surface_reconstruction_3 into my starting point sets?

thanks for reading this question. My title is basically what I'm trying to achieve. I did a poisson surface mesh generation using Poisson_surface_reconstruction_3(cgal). I can't figure out how to map the node identities of my resulting surface mesh into my starting point sets?
The output of my poisson surface generation is produced by the following lines:
CGAL::facets_in_complex_2_to_triangle_mesh(c2t3, output_mesh);
out << output_mesh;
In my output file, there are some x y z coordinates, followed by a set of 3 integers each line, I think they indicates which nodes form a delaunay triangle. The problem is that the output points do not correspond to my initial point set, since not any x y z value match to any of my original points. Yet I'm trying to figure out which points are forming a delaunay triangles in my original point set.
Could someone suggest me how can I do this in cgal?
Many thanks.
The poisson recontruction algorithm consist in meshing an implicit function that somehow fits you input points. In practice, it means that you input point will no belong to the set of points of the output surface, and won't even lie exactly on triangles of the output surface. However, they should not be too far from the output surface (except if you have some really sparse sampling parts).
What you can do to locate your input points with the output surface is to use the function closest_point_and_primitive() from the AABB-tree class.
Here is an example of how to build the tree from a mesh.

Is polygon operation in CGAL working with polygon that has duplicated edge?

I have a polygon as such:
{0}, {1}, {2}, {3},... denote the sequence of the points on the polygon.
I wonder whether CGAL polygon boolean set-operations work with polygon as such?
From the user manual, the input polygon must be simple or relatively simple in order for the CGAL polygon boolean set-operations to work:
A relatively simple polygon allows vertices with a degree >2, but all
of its edges are disjoint in their interior. Furthermore, it must be
an orientable polygon. Namely when it is inserted into an arrangement
and its outer boundary is traversed, the same face is adjacent to all
of the halfedges (no crossing over any curve during the traversal).
Note that while polygon C has the same curves as polygon B, traversal
of the curves leads to crossing over a previously traversed curve, and
is therefore neither simple nor relatively simple.
Not sure what it really means, but my guess is that the polygon is still ( at least) relatively simple, and hence the CGAL polygon boolean operation still works... am I right?
If you look at the condition for a valid polygon in the user manual, you'll see that the input polygon must be simple. Yours is not since you have a duplicated edge.