# How to generate a mesh in an area with curves inside

I would like to know how to generate a mesh inside an area, and be able to find the original curve outline inside the area from the mesh.

For example: There is a rectangle inside which there is a curve with an implicit expression such as a segmented arc or a spline.

I mean let the red area generate a mesh and keep the shape of the inner curve.

What is the solution?

Thanks

• Do you want the mesh elements to follow a curve, or does the curve have a 'thickness' that you want to remove or mesh separately? Apr 14 at 8:29
• @user21 In fact, what I want is a curve without thickness
– OlW
Apr 14 at 8:50

Here is one way:

Needs["NDSolveFEM"]
pts = {{1, 1}, {2, 3}, {3, -1}, {4, 1}, {5, 0}};
f = BSplineFunction[pts]; bm1 =
ToBoundaryMesh[DiscretizeGraphics[ParametricPlot[f[t], {t, 0, 1}]]];
bm2 = ToBoundaryMesh[Rectangle[{0, -1/2}, {6, 2}]];
Needs["FEMAddOns"]
bm = BoundaryElementMeshJoin[bm1, bm2];
ToElementMesh[bm]["Wireframe"]


Another approach can be seen in the documentation here:

img = Import["https://i.stack.imgur.com/Hp0Z3.png"];
bmr = ImageMesh@img;
ToElementMesh[bmr]["Wireframe"]
ToElementMesh[bmr, "RegionHoles" -> None]["Wireframe"]


The point I am trying to make is that you can decide if the inner region is to be excluded or not; in other words if it's a material region of an outside or just a curve the mesh should follow, because you need measurements along that curve.

• The first answer is very good, but the fly in the ointment is that the mesh around the curve is too dense, the second answer looks a lot more reasonable, but this causes the limit of the generated mesh to become a closed area (whether hollow or non-hollow)
– OlW
Apr 14 at 8:56
• @OIW, unfortunately, I do not understand your comment. Can you reword this? Apr 14 at 9:21
• What I mean is that this curve has no thickness, it doesn't look thick in the picture (ie wireless thin)
– OlW
Apr 14 at 10:46
img = Import["https://i.stack.imgur.com/Hp0Z3.png"];

ImageMesh @ img


DiscretizeRegion @ ImageMesh @ img


ImageMesh @ DeleteBorderComponents[ColorNegate @ Binarize @ img]


DiscretizeRegion[
ImageMesh @ DeleteBorderComponents[ColorNegate @ Binarize @ img],
MaxCellMeasure -> {"Area" -> 10}]


SeedRandom[1];
f = BSplineFunction[RandomReal[1, {15, 2}], SplineClosed -> True];
pp = ParametricPlot[f[u], {u, 0, 1}, Frame -> True,
FrameTicks -> False, ImagePadding -> 0,
PlotStyle -> Directive[Black, AbsoluteThickness[3]],
FrameStyle -> AbsoluteThickness[10], Background -> Red]


ImageMesh @ Image @ pp


DiscretizeRegion @ ImageMesh @ Image @ pp


• This looks great, but I want the curve to have no thickness, which means a non-closed curve (the pic I uploaded the curve has thickness lol)
– OlW
Apr 14 at 8:52

Something like e.g.:

pts = {{1, 1}, {2, 3}, {3, -1}, {4, 1}, {5, 0}};
f = BSplineFunction[pts]; Show[
ParametricPlot[f[t], {t, 0, 1}, PlotTheme -> "Business"],
Graphics[{Red, Opacity[0.1], Rectangle[{0, -.2}, {5.5, 2}]}]]
`

• I have some problems with my instructions, the mesh refers to triangular elements such as the mesh generated by the method ToElementMesh.
– OlW
Apr 14 at 7:53