1
$\begingroup$

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

my mesh

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.

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

1 Answer 1

1
$\begingroup$

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}]]

Wireframe

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
];
adj = AdjacencyMatrix[graph2];
graph3 = AdjacencyGraph[adj, GraphLayout -> "PlanarEmbedding"];
faces = FindFace[graph3];
meshpolygons = MeshRegion[
  VertexList[graph1][[VertexList[graph2]]],
  Polygon /@ Most[SortBy[faces, Length]]
]

Filled wireframe

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.

$\endgroup$
1
  • 1
    $\begingroup$ Works nice! Thank you very much! $\endgroup$
    – dmitry
    Commented Oct 13, 2015 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.