I am looking to convert a simple Graphics object that is defined by overlapping polygons into a MeshRegion object.
For example, here is code that will create three overlapping triangles
Table[Polygon@Map[{Cos[#], Sin[#]} &,
{RandomReal[{0, 2 Pi/3}],
RandomReal[{2 Pi/3, 4 Pi/3}],
RandomReal[{4 Pi/3, 2 Pi}]}], 3]
Graphics[{EdgeForm[Black], White, %}]
For the instance
{Polygon[{{-0.334165, 0.942514}, {-0.67166, 0.740859}, {-0.455064, -0.890459}}],
Polygon[{{0.274586, 0.961563}, {-0.99882, -0.0485668}, {-0.454045, -0.890979}}],
Polygon[{{0.832368, 0.554224}, {-0.977216, -0.212246}, {0.451922, -0.892057}}]}
I would like to convert this to a simple 1D MeshRegion Object with vertices at each intersection of two edges and line segments between them, but I am at a loss for how to do it efficiently.
For a small figure with three triangles, it is possible to do manually by finding the intersection points and the incidences, but when a Graphics object has dozens of polygons with many sides (possibly approximating circles), an automated function would be desirable!
One thing that I have tried and it has worked in a minimal yet extremely complicated manner was to convert each polygon to a 2D BoundaryMeshRegion object and use RegionDifference commands. I've tried other things like ImageMesh which has only given me more mess, but I feel there must be a simpler way, or at least a reason why there is no simple way.
Thanks in advance!
Edit: Here is some more information about how I approached this using MeshRegion operations.
After defining the variable triangles to be this set of three triangles, I turned each of the triangles into a BoundaryMeshRegion.
Table[triregion[i] = BoundaryDiscretizeGraphics[Graphics[triangles[[i]]]],
{i, 1, Length[triangles]}]
I then defined the order of the layers: The third triangle is above the second triangle is above the first triangle:
layerorder = {3, 2, 1}
Then I calculated which of the layers intersect which of the other layers. Here we see that layer 3 intersects layer 2, layer 3 intersects layer 1, and layer 2 intersects layer 1.
intersections = Flatten[
Table[If[Length[
RegionIntersection[
triregion[layerorder[[i]]],
triregion[layerorder[[j]]]]
] == 0, {layerorder[[i]], layerorder[[j]]}, Nothing],
{i, 1, 3}, {j, i + 1, 3}], 1]
Out: {{3, 2}, {3, 1}, {2, 1}}
Now define new MeshRegions that are the original MeshRegion subtracting out each intersecting MeshRegion using RegionDifference
Table[newregion[i] = triregion[i], {i, 20}];
Map[(newregion[#[[2]]] = RegionDifference[newregion[#[[2]]], triregion[#[[1]]]]) &,
intersections];
Here is the result.
To get the 1D frame I use MeshPrimitives
boundaries = Map[MeshPrimitives[#, 1] &, {newregion[1], newregion[2], newregion[3]}];
Show[Graphics /@ boundaries]
But now certain line segments are traversed by two different edges with different vertices, which is not what I need. I'm just hoping to have the 1D wireframe defined as a MeshRegion Object.