4
$\begingroup$

I have a collection of lines ordered by pairs of points, as below, and wish to get a MeshRegion from them, with the mesh edges being the given lines. (That is, the 2-dimensional mesh should be composed of the polygons shown below, with the big space inside taken out.)

Now, I don't want to write explicitly the points and mesh primitive cells because it's too large. Is there a direct way to do it ?

data={{{0.968246, -0.25}, {0.866025, -0.5}}, {{0.968246, -0.25}, {1., 0.}}, {{0.968246, -0.25}, {0.968246, -0.25}}, ... };
Graphics[{Green, Line[data]}]

enter image description here

(complete data set downloadable here or below)

 {{{0.968246, -0.25}, {0.866025, -0.5}}, {{0.968246, -0.25}, {1.,0.}}, 
 {{0.866025, -0.5}, {0.75, -0.661438}}, {{0.75, -0.661438}, \
 {0.661438, -0.75}}, {{0.661438, -0.75}, {0.5, -0.866025}}, {{0.5, \
      -0.866025}, {0.25, -0.968246}}, {{0.25, -0.968246}, {0., -1.}}, 
  {{0., \
 -1.}, {-0.25, -0.968246}}, {{-0.25, -0.968246}, {-0.5, -0.866025}}, \
 {{-0.5, -0.866025}, {-0.661438, -0.75}}, {{-0.661438, -0.75}, {-0.75, \
 -0.661438}}, {{-0.75, -0.661438}, {-0.866025, -0.5}}, {{-0.866025, \
 -0.5}, {-0.968246, -0.25}}, {{-0.968246, -0.25}, {-1., 0.}}, {{-1., 
    0.}, {-0.968246, 0.25}}, {{-0.968246, 0.25}, {-0.866025, 
    0.5}}, {{-0.866025, 0.5}, {-0.75, 0.661438}}, {{-0.75, 
    0.661438}, {-0.661438, 0.75}}, {{-0.661438, 0.75}, {-0.5, 
    0.866025}}, {{-0.5, 0.866025}, {-0.25, 0.968246}}, {{-0.25, 
    0.968246}, {0., 1.}}, {{0., 1.}, {0.25, 0.968246}}, {{0.25, 
    0.968246}, {0.5, 0.866025}}, {{0.5, 0.866025}, {0.661438, 
    0.75}}, {{0.661438, 0.75}, {0.75, 0.661438}}, {{0.75, 
    0.661438}, {0.866025, 0.5}}, {{0.866025, 0.5}, {0.968246, 
    0.25}}, {{0.968246, 0.25}, {1., 
    0.}}, {{-0.75, -0.5}, {-0.5, -0.5}}, {{-0.75, -0.5}, {-0.75, \
      -0.25}}, {{-0.75, -0.5}, {-0.75, -0.661438}}, {{-0.75, -0.5}, \
      {-0.866025, -0.5}}, {{-0.5, -0.5}, {-0.5, -0.75}}, {{-0.5, -0.75},           \
 {-0.25, -0.75}}, {{-0.5, -0.75}, {-0.5, -0.866025}}, {{-0.5, -0.75}, \
 {-0.661438, -0.75}}, {{-0.25, -0.75}, {0., -0.75}}, {{-0.25, -0.75}, \
 {-0.25, -0.968246}}, {{0., -0.75}, {0.25, -0.75}}, {{0., -0.75}, {0., \
 -1.}}, {{0.25, -0.75}, {0.5, -0.75}}, {{0.25, -0.75}, {0.25, \
 -0.968246}}, {{0.5, -0.75}, {0.5, -0.5}}, {{0.5, -0.75}, {0.661438, \
 -0.75}}, {{0.5, -0.75}, {0.5, -0.866025}}, {{0.5, -0.5}, {0.75, \
 -0.5}}, {{0.75, -0.5}, {0.75, -0.25}}, {{0.75, -0.5}, {0.866025, \
 -0.5}}, {{0.75, -0.5}, {0.75, -0.661438}}, {{0.75, -0.25}, {0.75, 
    0.}}, {{0.75, -0.25}, {0.968246, -0.25}}, {{0.75, 0.}, {0.75, 
    0.25}}, {{0.75, 0.}, {1., 0.}}, {{0.75, 0.25}, {0.75, 
    0.5}}, {{0.75, 0.25}, {0.968246, 0.25}}, {{0.75, 0.5}, {0.5, 
    0.5}}, {{0.75, 0.5}, {0.75, 0.661438}}, {{0.75, 0.5}, {0.866025, 
    0.5}}, {{0.5, 0.5}, {0.5, 0.75}}, {{0.5, 0.75}, {0.25, 
    0.75}}, {{0.5, 0.75}, {0.5, 0.866025}}, {{0.5, 0.75}, {0.661438, 
    0.75}}, {{0.25, 0.75}, {0., 0.75}}, {{0.25, 0.75}, {0.25, 
    0.968246}}, {{0., 0.75}, {-0.25, 0.75}}, {{0., 0.75}, {0., 
    1.}}, {{-0.25, 0.75}, {-0.5, 0.75}}, {{-0.25, 0.75}, {-0.25, 
    0.968246}}, {{-0.5, 0.75}, {-0.5, 0.5}}, {{-0.5, 0.75}, {-0.661438,
     0.75}}, {{-0.5, 0.75}, {-0.5, 0.866025}}, {{-0.5, 0.5}, {-0.75, 
    0.5}}, {{-0.75, 0.5}, {-0.75, 0.25}}, {{-0.75, 0.5}, {-0.866025, 
    0.5}}, {{-0.75, 0.5}, {-0.75, 0.661438}}, {{-0.75, 0.25}, {-0.75, 
    0.}}, {{-0.75, 0.25}, {-0.968246, 0.25}}, {{-0.75, 
    0.}, {-0.75, -0.25}}, {{-0.75, 0.}, {-1., 
    0.}}, {{-0.75, -0.25}, {-0.968246, -0.25}}}
$\endgroup$
4
  • $\begingroup$ Would you please provide complete data as a list? Thanks! $\endgroup$ Commented Feb 22, 2023 at 15:01
  • $\begingroup$ @UlrichNeumann It was very long to post it here, but you can find it in the link below the image. Thank you, $\endgroup$ Commented Feb 22, 2023 at 15:02
  • $\begingroup$ It's a list of ~50 points. Not all users trust download-links. $\endgroup$ Commented Feb 22, 2023 at 15:04
  • $\begingroup$ @UlrichNeumann RIght. Please see edit. $\endgroup$ Commented Feb 22, 2023 at 15:18

2 Answers 2

7
$\begingroup$
  • data[[3]], the two endpoints is the same, so it is the degenerate line, Mathematica auto remove it.
Clear[pts];
pts = DeleteDuplicates[Flatten[data, {2, 1}]];
MeshRegion[pts, {Point /@ Range@Length@pts, 
  Table[Line[Flatten[FirstPosition[pts, #] & /@ d]], {d, data}]}]

enter image description here

$Version

"13.1.0 for Microsoft Windows (64-bit) (June 16, 2022)"

  • We use Planar graph to find such small polygons.
  • We delete the longest two circles.
Clear[pts,edges, g, faces, reg];
pts = DeleteDuplicates[Flatten[data, {2, 1}]];
edges = Table[
   UndirectedEdge @@ Flatten[FirstPosition[pts, #] & /@ d], {d, data}];
g = Graph[edges, VertexCoordinates -> Thread[Range@Length@pts -> pts]];
faces = PlanarFaceList[g];
faces = SortBy[faces, Length][[1 ;; -3]];
reg = MeshRegion[GraphEmbedding[g], 
  Polygon[faces /. First /@ PositionIndex[VertexList[g]]]]
MeshPrimitives[reg, 2]

enter image description here

enter image description here

$\endgroup$
6
  • $\begingroup$ Thank you ! I, however want it filled. I mean, this kind of annular region with the edges as shown (that is, dimension 2 mesh). The problem is to write the polygons, tho. $\endgroup$ Commented Feb 22, 2023 at 14:41
  • $\begingroup$ @DanielCastro PlanarFaceList introduced in 13.0 . We can test above code in wolfram cloud. $\endgroup$
    – cvgmt
    Commented Feb 22, 2023 at 16:12
  • $\begingroup$ I see. Thank you. $\endgroup$ Commented Feb 22, 2023 at 16:13
  • $\begingroup$ Thank you. For some reason PlanarFaceList does not work when the number of cells is large (more than 200 at least). $\endgroup$ Commented Mar 5, 2023 at 11:29
  • $\begingroup$ @DanielCastro maybe post a link about such large data to test. $\endgroup$
    – cvgmt
    Commented Mar 5, 2023 at 11:41
3
$\begingroup$

First, discretize it:

mesh = DiscretizeGraphics[Graphics[Line[data]]]

enter image description here

Find polygon faces:

face = Select[PlanarFaceList[MeshConnectivityGraph[mesh]], 
    Length[#] <= 5 &][[All, All, 2]];

Construct mesh:

MeshRegion[MeshCoordinates[mesh], Polygon[face]]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you. For some reason PlanarFaceList does not work when the number of cells is large (more than 200 at least). $\endgroup$ Commented Mar 1, 2023 at 12:11

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.