# Cut a 2D domain or a 2D mesh with a curve

**URL:** <https://community.freefem.org/t/cut-a-2d-domain-or-a-2d-mesh-with-a-curve/1351>\
**Category:** General Discussion\
**Created:** [November 26, 2021, 10:41am UTC](https://community.freefem.org/t/cut-a-2d-domain-or-a-2d-mesh-with-a-curve/1351 "2021-11-26T10:41:50Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [November 26, 2021, 10:41am UTC](https://community.freefem.org/t/cut-a-2d-domain-or-a-2d-mesh-with-a-curve/1351/1 "2021-11-26T10:41:50Z")

</div>

![Mesh](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/749ad244d4aa08aa55ee3b14bb4bfa2e093d4c8e.png)  
Hello all,

I am trying to make some perforations (circular) on an existing 2D meshes that I have previously built. For information, I can consider this mesh or only the triangular domain (without the elements inside).

For this, I have tried to make the summation of this mesh with a disc and then truncate the given mesh with an indicator function, but it does not work.

Is it possible to cut a mesh or a domain according to a curve for example ?

Basically I am looking for a function “MakeCut”, which for example works like this:  
mesh TH=MakeCut(TH, x^2-y^2 \< R^2) and returns the mesh TH cut by a circle.

Thank you for your help,

Best regards,

Loïc,

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [November 26, 2021, 9:40pm UTC](https://community.freefem.org/t/cut-a-2d-domain-or-a-2d-mesh-with-a-curve/1351/2 "2021-11-26T21:40:18Z")

</div>

To day, they a no tools to do this,  
but you can build the border of the intersection.  
See exemple isoline.edp ie: [FreeFem-sources/isoline.edp at master · FreeFem/FreeFem-sources · GitHub](https://github.com/FreeFem/FreeFem-sources/blob/master/examples/plugin/isoline.edp)

This not exactly what you want but it will given a not to bad mesh.

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [November 29, 2021, 12:50pm UTC](https://community.freefem.org/t/cut-a-2d-domain-or-a-2d-mesh-with-a-curve/1351/3 "2021-11-29T12:50:45Z")

</div>

Thank you for you reply,

In this example, we have:

```auto
border Curve1(t=0,1)
{ int c =1;
  int i0 = be[2*c], i1 = be[2*c+1]-1;
  P=Curve(xy,i0,i1,t);
  label=1;
}
plot(Curve1(100), wait=1);
mesh Th= buildmesh(Curve1(-100));
plot(Th,wait=1);

```

which gives this two figures :

 ![circle_in_square](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/a3c80b93ea8c9188a3cef4156fa106440571b7f4.png)  
 ![circle](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/8063cd960e0d5715bc100cab9a4a433e7fb6a25e.png)

However, I want to keep the rectangular shape and delete the circle instead.  
How could I do that ?

Thank you in advance for your help,

Best regards,

Loïc,

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [November 30, 2021, 1:53pm UTC](https://community.freefem.org/t/cut-a-2d-domain-or-a-2d-mesh-with-a-curve/1351/4 "2021-11-30T13:53:52Z")

</div>

an example:

> Blockquote

border a(t=-2,2){x=t; y=-2;label=1;};  
border b(t=-2,2){x=2; y=t;label=1;};  
border c(t=2,-2){x=t; y=2;label=1;};  
border d(t=2,-2){x=-2; y=t;label=1;};  
border cc(t=2\*pi,0){ x=cos(t); y=sin(t);label=2;}

int n = 20;  
plot(a(2_n)+b(2_n)+c(2_n)+d(2_n)+cc(pi_n),wait=1,ps=“squarebb.eps”);  
mesh th= buildmesh(a(2_n)+b(2_n)+c(2_n)+d(2_n)+cc(pi_n));  
plot(th,wait=1);

> Blockquote

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [November 30, 2021, 2:18pm UTC](https://community.freefem.org/t/cut-a-2d-domain-or-a-2d-mesh-with-a-curve/1351/5 "2021-11-30T14:18:21Z")

</div>

Hello,

Thank you for you reply,  
Yes sure, I know this method, but I was looking for the same kind of thing for the isoline method that you proposed, since I want to consider more general case for example when the borders of the 2 meshes considered are crossing.

Thank you,

Best regards,

Loïc,

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [November 30, 2021, 3:13pm UTC](https://community.freefem.org/t/cut-a-2d-domain-or-a-2d-mesh-with-a-curve/1351/6 "2021-11-30T15:13:25Z")

</div>

Question, Why you want to do this.

Because, in general the mesh created is no so pretty., This build mesh have some small element , bad angle, …

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [November 30, 2021, 3:28pm UTC](https://community.freefem.org/t/cut-a-2d-domain-or-a-2d-mesh-with-a-curve/1351/7 "2021-11-30T15:28:00Z")

</div>

Yes, I see that.  
I succeed to generate this kind of mesh with the isoline method:

 ![perforation](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/fcba702148295c2dfcdced45ddf20b91faa7e9db.png)  
But it gives bad angles as you say.

Originally I wanted to apply a kind of patch to an existing mesh to create a mesh with perforations. But I think this method is not suitable for my case.

I will find another solution to this (computing the intersection or use another tool for the mesh such as mmg).

Thank you,

Best regards,

Loïc,

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [December 14, 2021, 3:46pm UTC](https://community.freefem.org/t/cut-a-2d-domain-or-a-2d-mesh-with-a-curve/1351/8 "2021-12-14T15:46:06Z")

</div>

1. it is not so hard to add an angle criteria in isoline to and separate the isoline plugin by add  
un curer (border if the angle is not good, like corner).

=\> no optimisation of the curve at this points  
and change the array `be` in isoline

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [December 15, 2021, 6:41am UTC](https://community.freefem.org/t/cut-a-2d-domain-or-a-2d-mesh-with-a-curve/1351/9 "2021-12-15T06:41:42Z")

</div>

Thanks you for your reply,

Pierre Jolivet @prj proposes another method with the use of mmg2d to adapt the mesh to the isoline solution before truncating the mesh according to the isoline with FreeFEM. My new perforated mesh is given by :

```auto
> ` savemesh(Th,"./mesh_temp/Th.mesh");
> savesol("./mesh_temp/Th.sol",Th,iso,order=1);
> exec("mmg2d_O3 ./mesh_temp/Th.mesh -ls -hmax 0.01 -hausd 0.001 -hmin 0.001");
> Th=readmesh("./mesh_temp/Th.o.mesh");
> Th=change(Th,rmInternalEdges=1);
> Th=trunc(Th,(x-xgi)^2+(y-ygj)^2>R^2, label=9);}

```

It is work well,

Loïc,

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [December 15, 2021, 7:22am UTC](https://community.freefem.org/t/cut-a-2d-domain-or-a-2d-mesh-with-a-curve/1351/10 "2021-12-15T07:22:57Z")

</div>

> I have found

No… please give credits where credits are due.

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [December 15, 2021, 9:16am UTC](https://community.freefem.org/t/cut-a-2d-domain-or-a-2d-mesh-with-a-curve/1351/11 "2021-12-15T09:16:05Z")

</div>

Yes sure, sorry, I have forgotten to mention it.  
Effectively, Pierre Jolivet proposed this solution.

@prj is for Pierre Jolivet ?

Thanks,

Loïc,

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [December 15, 2021, 9:32am UTC](https://community.freefem.org/t/cut-a-2d-domain-or-a-2d-mesh-with-a-curve/1351/12 "2021-12-15T09:32:33Z")

</div>

No one in the FreeFEM community is named that… But yes, I’m the person that gave you this code.

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [December 15, 2021, 9:36am UTC](https://community.freefem.org/t/cut-a-2d-domain-or-a-2d-mesh-with-a-curve/1351/13 "2021-12-15T09:36:14Z")

</div>

Sorry for my mistake, I wanted to say Pierre .

---

<div class="post-metadata">

**Author:** ![thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@thomas](https://community.freefem.org/u/thomas)\
**Post date:** [February 23, 2022, 11:13am UTC](https://community.freefem.org/t/cut-a-2d-domain-or-a-2d-mesh-with-a-curve/1351/14 "2022-02-23T11:13:26Z")

</div>

Hello all,

Here is another solution using meshL :

```auto

load "msh3"

mesh Th=square(20,20); 
plot(Th,wait=1);

// Parametrization of the circles

int nc=3; // Number of circles
real[int] r=[0.1,0.2,0.1]; // radius of the circles

real[int] xc=[0.15,0.5,0.85]; // center of the circles
real[int] yc=[0.5,0.5,0.5];

int[int] reg=[1,2,3];
int[int] Nc(nc);
Nc=50;

// Border of the circles 

border circles(t=0,2*pi;i){
    x=xc[i]+r[i]*cos(t);
    y=yc[i]+r[i]*sin(t);
    region=reg[i];
}

// meshL

int[int] lsquare=[1,2,3,4];
meshL ThL=extract(Th,refedge=lsquare);

// plot(ThL,dim=2,wait=1);

meshL ThCirclesL=buildmeshL(circles(Nc));

// plot(ThCirclesL,dim=2,wait=1);

ThL=ThL+ThCirclesL;
plot(ThL,dim=2,wait=1);

Th=buildmesh(ThL);
plot(Th,wait=1);

// Truncation along the circles 

func int InCircleQ(real xx, real yy){
    int res=0;
    for(int i=0;i<nc;i++){
        if((xx-xc[i])^2+(yy-yc[i])^2<r[i]^2){
            res=1;
            break;
        }
    }
	return res;
}

Th = trunc(Th,!InCircleQ(x,y), split=1);

plot(Th,wait=1);

```

I think this solution works only with the FreeFem version 4.9 (and later) where the buildmesh function was generalized to meshL types of mesh.
