# 3D mesh from surface meshes

**URL:** <https://community.freefem.org/t/3d-mesh-from-surface-meshes/1795>\
**Category:** General Discussion\
**Created:** [June 2, 2022, 5:52pm UTC](https://community.freefem.org/t/3d-mesh-from-surface-meshes/1795 "2022-06-02T17:52:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![felric](https://avatars.discourse-cdn.com/v4/letter/f/5f8ce5/32.png) [@felric](https://community.freefem.org/u/felric)\
**Post date:** [June 2, 2022, 5:52pm UTC](https://community.freefem.org/t/3d-mesh-from-surface-meshes/1795/1 "2022-06-02T17:52:57Z")

</div>

Dear sir,

I need to simulate a complex 3D region.  
I was able to produce the surface mesh, but I don’t know how to create 3D mesh (e.g. tetrahedrons) from it.  
I tried tetgen (tetg) but it fails when the edge contains a point (gives “PLC Error: A vertex lies in a segment”).  
Due to the geometry (see attached file) I am not able to prepare 2D mesh so that on the coincident edges of two adjacent surfaces the number of vertexes is the same.  
I have been struggling on this for a while, also looked in the Documentation, Examples, tutorials, General discussion but no solution found…

Thank you very much in advance for your help!  
Riccardo

[meshtest2.edp](https://community.freefem.org/uploads/short-url/gCQf9A7jo6uPMBow3Kz5YCNNmQU.edp) (5.7 KB)

---

<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:** [June 7, 2022, 3:19pm UTC](https://community.freefem.org/t/3d-mesh-from-surface-meshes/1795/2 "2022-06-07T15:19:23Z")

</div>

I sorry , I try you test , It seems to be good!

---

<div class="post-metadata">

**Author:** ![felric](https://avatars.discourse-cdn.com/v4/letter/f/5f8ce5/32.png) [@felric](https://community.freefem.org/u/felric)\
**Post date:** [June 7, 2022, 9:40pm UTC](https://community.freefem.org/t/3d-mesh-from-surface-meshes/1795/3 "2022-06-07T21:40:45Z")

</div>

Dear Frédéric Hecht,

sorry I think I did not describe the problem clearly.  
I attach a new file (meshtest3.edp), where I simply added few lines to show the meshes before glueing:

ThG = buildBdMesh(ThG);  
medit(“First\_mesh”,Th33,wait=1);  
medit(“Second\_mesh”,ThLat3,wait=1);  
medit(“Union”,ThG,wait=1);  
meshS ThGS = ThG.Gamma;  
medit(“Surface\_mesh”,ThGS,wait=1);  
mesh3 Thfinal = tetg(ThGS,switch=“pqaAAYYQ”);

As you can see, the two meshes do not share the same nodes at the boundary, so they actually are not “glued”. To solve the problem (in my case, elasticity), I need to fuse the two meshes together.

I thought to use tetg to accomplish this task, by giving the surface mesh as an input; however tetg requires to have the same nodes on the common edge shared by two surfaces.

To show this, I also tried tetg on a simpler geometry: your example “cylinder-with-tetgen.edp”.  
I attached a slightly modified version, where the lateral surface of the cylinder is composed of two surfaces (the first, Thcarre1, between [0, pi] and the other, Thcarre2, between [pi, 2pi]).

[cylinder-with-tetgen.edp](https://community.freefem.org/uploads/short-url/StqIOByySmPwYMNb5nQWAJtFcT.edp) (2.1 KB)

The code does not work because the two lateral surfaces have a different number of nodes on the length (see line 25 and 26: nx versus nx/2). However if I put the same number of nodes (e.g., nx) the code works fine.

To sum up, I need either to merge together two 3D meshes, or create a unique 3D mesh from the boundary of several 2D meshes, but I was not successful (see meshtest3.edp) and I have not idea how to do it.

Thanks a lot for your help  
Riccardo

[meshtest3.edp](https://community.freefem.org/uploads/short-url/nlK6XSl8cZdMOtsm7a78O7L1Bki.edp) (5.9 KB)

---

<div class="post-metadata">

**Author:** ![felric](https://avatars.discourse-cdn.com/v4/letter/f/5f8ce5/32.png) [@felric](https://community.freefem.org/u/felric)\
**Post date:** [June 15, 2022, 2:55pm UTC](https://community.freefem.org/t/3d-mesh-from-surface-meshes/1795/4 "2022-06-15T14:55:12Z")

</div>

Dear sir,

I wonder if there is any method to glue together two adjacent 3D meshes having non-coincident nodes on the surface separating the two meshes, see my previous post.  
Thanks in advance

Best regards  
Riccardo

---

<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:** [June 19, 2022, 3:15pm UTC](https://community.freefem.org/t/3d-mesh-from-surface-meshes/1795/5 "2022-06-19T15:15:39Z")

</div>

Remark, You error is the compatibly mesh in angle ( you have to set in line 103à)

```auto
mesh ThLat=buildmesh(B600(MaxLayersT) + B601(MaxLayersT) + B602(MaxLayersT) + B603(8*nn) + B604(MaxLayersT) + B605(8*nn),fixedborder=1);

```

and the vertical number are only wrong !!!
