MATLAB: Convert a tetrahedron mesh to triangular mesh or surface mesh.

generatemeshgeometryimportgeometrymeshpdepdemeshpdetoolboxstltetrahedrontriangular

Hi,
I have a stl file and generated the 3d mesh using generatemesh from PDE toolbox. But the output is tetrahedron mesh, I need only the surface mesh for that stl. I need to do with pdetoolbox only, because generatemesh is giving finer details of the 3d object. Basic code which I have tried is below. msh has faces of (n,4). So I need a surface mesh from this, I need a output of (n,3) which row indicate a connection of 3 vertices on surface. Any help is much appreciated.
model = createpde(3);
importGeometry(model,'example.stl');
msh = generaeMesh(model,'GeometricOrder','linear');

Best Answer

Easy. Assume that tess is an nx4 array, composed of references into the set of vertices.
Then surely is it true that:
facets = [tess(:,[1 2 3]);tess(:,[1 2 4]);tess(:,[1 3 4]);tess(:,[2 3 4])];
is a set of facets of those simplexes? So facets is the set of triangular facets as requested. But I think you asked for the outer boundary surface of the mesh.
The only problem is that some of those facets are replicated. That is, facets that are shared between a pair of touching simplexes will happen exactly twice. A facet that is on the surface will occur exactly once.
But in fact, these replicates are actually a good thing! If you are only looking for the surface triangulation, then you can discard those facets that occurred twice. It is important to sort them though. (Think! why?)
facets = sort(facets,2);
Now, how do we discard those duplicated?
facets = sortrows(facets);
duploc = find(all(diff(facets) == 0,2));
facets([duploc;duploc + 1],:) = [];
Related Question