# How to add Polygons (using existing Lines and Points) to MeshRegion?

I have a MeshRegion N with Points and Lines, but without Polygons. That means that MeshCells[N, 2] returns {}. My mesh looks like this:

How can I add Polygons, made of closed set of Lines, to my MeshRegion N? I need it to calculate area of each domain in my mesh.

• I'm assuming you want to find the white-space polygon regions in the above (not ALL possible polygons?) – Peter Roberge Oct 13 '15 at 0:17
• It's advisable not to use capital N as a variable name, as it's the name of the built-in function N. – Taiki Oct 13 '15 at 3:16
• @Taiki, I'm sorry. It should be script capital N. – gidman Oct 13 '15 at 12:51
• @PeterRoberge, yes. Only white-space ones. – gidman Oct 13 '15 at 12:54

I think this may work though I'm not sure why. I made it up through trial and error, aiding by #18514. You'll need nextCandidate and FindFace from there.

First of all since you haven't provide some example data, let me generate some wireframe:

mesh = MeshRegion[
MeshCoordinates[#],
MeshCells[#, 1]
]& @ VoronoiMesh[RandomReal[{0, 1}, {100, 2}]]


Then,

graph1 = Graph[
MeshCoordinates[mesh],
MeshCells[mesh, 1] /. Line[{start_, end_}] -> {start, end},
VertexCoordinates -> MeshCoordinates[mesh],
GraphLayout -> "PlanarEmbedding"
];
graph2 = Graph[
MeshCells[mesh, 1] /. Line[{start_, end_}] -> start <-> end
];
faces = FindFace[graph3];
meshpolygons = MeshRegion[
VertexList[graph1][[VertexList[graph2]]],
Polygon /@ Most[SortBy[faces, Length]]
]


Then you can obtain the area of each polygon as follows:

Area /@ (MeshCells[meshpolygons, 2] /. x_Integer :> MeshCoordinates[meshpolygons][[x]])


Let's see if the method works for your specific wireframe.

Some explanation:

• Basically your problem is about detecting smallest polygons from connected lines. This requires some clever algorithm. Thankfully we have #18514. We can turn our wireframe into a graph and use the method there.

• graph1 is the straightforward conversion. It doesn't have any crossings, but for some reason it doesn't work with FindFace. (I haven't taken time to study it.) Possibly the function requires its argument's structure to be of certain canonical form, so I convert graph1 to an adjacency matrix first and then obtain graph3.

• graph1 contains the actual positions of the vertices, so we need VertexList[graph1] in meshpolygons, but the ordering of the vertices used in faces is from graph2, hence VertexList[graph1][[VertexList[graph2]]].

• faces also includes the largest, encompassing polygon. I assume that it has the most vertices and remove it with Most after sorting the list of faces by the number of their vertices.

• Works nice! Thank you very much! – gidman Oct 13 '15 at 15:46