How to triangulate between two closed curves? - algorithm

How can i triangulate between two closed wires in 2D/3D?
For the start case how can i triangulate between two convex closed wires in 2D?
I have tried following:
If we have two convex closed wires in 2D
we can for each convex curve find the diagonal that connects two most far vertices. Then we can split that curve in the lower and upper part.
Now we can separate algorithm in two parts: upper and lower part.
Let's take the upper parts from the both curves.
We can make projection on the x-axis like following:
first curve: {x_1, 0}, {x_2, 0}, ...., {x_n, 0}
second curve: {x_1', 2}, {x_2', 2}, ...., {x_m', 2}
Notice the fixed y-axis. We place fixed y-axis for both projection and we will return the regular y-coordinate after applying triangulation.
Now we can apply some triangulation algorithm let's say delaunay. After that as said above we can return the regular y-coordinate from map that we generate before projection.
The main flaw in this algorithm i think is the boundaries vertices. On the next picture can be seen the problem. We can see that the boundaries vertex have a lot of connection:
How can i solve this flaw and also is there the better way to triangulate between two curves for start case in the 2D convex closed curves?
The second thinking is to densify the numbers of points on both curves and to make the numbers same. Then to start from arbitary vertex from the first curve and connect to the closest vertex on the second curve (step 1.). Then to go vertex by vertex and make triangles like on the following image:
Is this okay way?
UPDATE:
I managed to do following for the 2D curve
First make the number of both curves same
Then for every point from the one curves connect to the closest one on the second curve
There can be polygons like this:
We can align them in the same line like on the picture and do the triangulation (delaunay). We can see that no points from the same line connects except the neighbour ones:
First make the number of the both curves same.

Related

Find common tangents of two convex polygons

Given two convex polygons P, Q separated by a line, how can I find their common tangents?
There should be 4 total.
Geometry isn't my strong side so any help will be appreciated :)
Any tangent will be generated from (at least) one point on each polygon. The tangent forms a half-space for each polygon, containing that entire polygon; different tangents are generated by requiring that each polygon be above or below the tangent line.
Whenever you have a problem involving two convex polygons, the algorithm probably involves "pick a point on each one, then iterate." This algorithm is no different.
What you do, basically, is start with a random guess, and refine. Pick a vertex on each polygon and calculate the line through the two vertices. Look at polygon A and see whether either of the two neighboring vertices is on the wrong side of the line. If one is, replace the current vertex with that vertex. Then do the same check on polygon B. Then, if you updated either vertex, repeat. Eventually the lines will converge to tangents with each vertex the extremal one in the desired direction.

3D mesh direction detection

I have a 3D mesh consisting of triangle polygons. My mesh can be either oriented left or right:
I'm looking for a method to detect mesh direction: right vs left.
So far I tried to use mesh centroid:
Compare centroid to bounding-box (b-box) center
See if centroid is located left of b-box center
See if centroid is located right of b-box center
But the problem is that the centroid and b-box center don't have a reliable difference in most cases.
I wonder what is a quick algorithm to detect my mesh direction.
Update
An idea proposed by #collapsar is ordering Convex Hull points in clockwise order and investigating the longest edge:
UPDATE
Another approach as suggested by #YvesDaoust is to investigate two specific regions of the mesh:
Count the vertices in two predefined regions of the bounding box. This is a fairly simple O(N) procedure.
Unless your dataset is sorted in some way, you can't be faster than O(N). But if the point density allows it, you can subsample by taking, say, every tenth point while applying the procedure.
You can as well keep your idea of the centroid, but applying it also in a subpart.
The efficiency of an algorithm to solve your problem will depend on the data structures that represent your mesh. You might need to be more specific about them in order to obtain a sufficiently performant procedure.
The algorithms are presented in an informal way. For a more rigorous analysis, math.stackexchange might be a more suitable place to ask (or another contributor is more adept to answer ...).
The algorithms are heuristic by nature. Proposals 1 and 3 will work fine for meshes whose local boundary's curvature is mostly convex locally (skipping a rigorous mathematical definition here). Proposal 2 should be less dependent on the mesh shape (and can be easily tuned to cater for ill-behaved shapes).
Proposal 1 (Convex Hull, 2D)
Let M be the set of mesh points, projected onto a 'suitable' plane as suggested by the graphics you supplied.
Compute the convex hull CH(M) of M.
Order the n points of CH(M) in clockwise order relative to any point inside CH(M) to obtain a point sequence seq(P) = (p_0, ..., p_(n-1)), with p_0 being an arbitrary element of CH(M). Note that this is usually a by-product of the convex hull computation.
Find the longest edge of the convex polygon implied by CH(M).
Specifically, find k, such that the distance d(p_k, p_((k+1) mod n)) is maximal among all d(p_i, p_((i+1) mod n)); 0 <= i < n;
Consider the vector (p_k, p_((k+1) mod n)).
If the y coordinate of its head is greater than that of its tail (ie. its projection onto the line ((0,0), (0,1)) is oriented upwards) then your mesh opens to the left, otherwise to the right.
Step 3 exploits the condition that the mesh boundary be mostly locally convex. Thus the convex hull polygon sides are basically short, with the exception of the side that spans the opening of the mesh.
Proposal 2 (bisector sampling, 2D)
Order the mesh points by their x coordinates int a sequence seq(M).
split seq(M) into 2 halves, let seq_left(M), seq_right(M) denote the partition elements.
Repeat the following steps for both point sets.
3.1. Select randomly 2 points p_0, p_1 from the point set.
3.2. Find the bisector p_01 of the line segment (p_0, p_1).
3.3. Test whether p_01 lies within the mesh.
3.4. Keep a count on failed tests.
Statistically, the mesh point subset that 'contains' the opening will produce more failures for the same given number of tests run on each partition. Alternative test criteria will work as well, eg. recording the average distance d(p_0, p_1) or the average length of (p_0, p_1) portions outside the mesh (both higher on the mesh point subset with the opening). Cut off repetition of step 3 if the difference of test results between both halves is 'sufficiently pronounced'. For ill-behaved shapes, increase the number of repetitions.
Proposal 3 (Convex Hull, 3D)
For the sake of completeness only, as your problem description suggests that the analysis effectively takes place in 2D.
Similar to Proposal 1, the computations can be performed in 3D. The convex hull of the mesh points then implies a convex polyhedron whose faces should be ordered by area. Select the face with the maximum area and compute its outward-pointing normal which indicates the direction of the opening from the perspective of the b-box center.
The computation gets more complicated if there is much variation in the side lengths of minimal bounding box of the mesh points, ie. if there is a plane in which most of the variation of mesh point coordinates occurs. In the graphics you've supplied that would be the plane in which the mesh points are rendered assuming that their coordinates do not vary much along the axis perpendicular to the plane.
The solution is to identify such a plane and project the mesh points onto it, then resort to proposal 1.

How to compute the intersection between a convex polyhedron and another polyhedron?

The problem at hand is part of a scientific simulation concerned with 2D growth within 3D space. The 2D shape grows by adding (triangular) segments to the previously grown shape.
Note that the actual segments in 3D have a thickness, thus, my code actually works with triangular prisms.
At one point, these 2D shapes (with whatever relative orientation and position) collide.
If one of the new triangular prisms intersect with previously inserted segments, I only want to insert the "part" of a segment which does not intersect with the previously inserted segments. As shown below for the segments labelled T1 and T2.
In the first step, I calculated all intersections edges to faces. I then used the CGAL Delaunay Triangulation package in 3D to mesh the resultant point set in a tetrahedral mesh. As a last step, I throw away all those tetrahedrons which intersect with previously inserted segments.
This works beautifully in most cases - but I am convinced by now that the idea cannot work for fundamental reasons.
What's a more reliable way to compute this?
So you're looking to find the intersection of 2 convex hulls?
I think your correct that your approach won't work in all cases, but only in certain degenerate cases. For example if one convex hull is entirely inside the other then there are no face/edge intersections.
A more obviously robust approach is to start off with a convex hull big enough to enclose both your hulls (so basically representing all of space), and then for each plane in both your convex hulls partition this hull by that plane, throwing away the part on the "outside".
The result will be the intersection - which may be nothing.

calculate intersection area of two triangle

I have been trying to find an algorithm which computes the intersecting area of two triangles but I failed to find any. Can anybody give a clue how to write this algorithm?
I would like something like:
double getAreaOfIntersection(Vector2 p1,Vector2 p2, Vector2 p3,Vector2 p4,Vector2 p5,Vector2 p6 )
where pX represents the 2 triangles.
You could first compute the polygon which describes the intersection area by a clipping algorithm, e.g.:
Sutherland-Hodgman algorithm
Then you would compute the area of the resulting convex polygon, which is rather easy, see, e.g., here:
Area of a Convex Polygon
Determining wether or not a point lies within a given polygon is easy (and even easier for triangles since those are simple polygons). You can use the winding number algorithm (and the crossing number algorithm for simple polygons) which is implemented and well explained here.
Using this you can obtain all the vertices of your intersection polygon:
The vertices pX of a triangle that are contained in the other triangle as well
The points where the two triangles intersect (see intersection of line segments)
You will need to loop over your edges to find all the intersection points, so this should be quick enough as long as you only want to determine intersections of triangles but i would not suggest to try to find intersections of arbitrary polygons this way.

Extracting polygons from a polygon with collinear edges

How can I extract simple polygons out of a polygon which contains collinear edges?
For the very simple case below, edge 2-3 and 6-0 are collinear. I want to separate this as 0, 1, 2 and 3, 4, 5, 6.
I could compare collinearity of every edge against every other edge, but that is a slow O(n^2) approach. Is there a faster method?
Find a bounding circle. Compute the upper/right intersection between the bounding circle and the line each edge lies on. This is O(n). Now sort each edge by the tuple of its angle and the angular position of its intersection with the bounding circle. That's O(nlogn), and will group the collinear edges together in your sorted list.
If you're unlikely to have lots of parallel but non-collinear edges then you can skip the bounding circle thing and just sort by angle. If there are lots of parallel non-collinear angles then just using angle still "works", it just doesn't buy you nearly as much efficiency improvement.
Can you find the intersection of 1-2 and 6-0? If so, you can generate a graph of edges and vertices. Then, finding the all non-overlapping polygons is simple.

Resources