Get triangular meshes for any shape

38 views (last 30 days)
James Johnson
James Johnson on 12 Jan 2021
Answered: Nipun on 31 May 2024
I would like to get triangular meshes for arbitrary shapes, with the most regular triangles possible and the ability to adjust the size of the triangles.
I have an example which is close but unsatisfactory because the triangles are very irregular. The example creates a toroid, then tries to get an alphashape and then tries to convert that into a triangular mesh. It's the only way I found without importing stl files.
I only need the vertices and faces. The rest of the pdemodels stuff is nice, but not necessary.
Examples of the kinds of other shapes I need this for: Cubes, prisms, regular N-gons, spheroid, part-torroids, cones, bowls, twists, etc (with holes possible in each). Nothing is spiky or furry, or rough. All surfaces are smooth.
My emphasis on triangular meshes is just because I need to exactly specify each face, and triagular meshes are efficient way of doing that. Other regular meshes are fine.
R=5; % outer radius of torus
r=2; % inner tube radius
th=linspace(0,2*pi,4*36); % e.g. 36 partitions along perimeter of the tube
phi=linspace(0,2*pi,4*18); % e.g. 18 partitions along azimuth of torus
% we convert our vectors phi and th to [n x n] matrices with meshgrid command:
[Phi,Th]=meshgrid(phi,th);
% now we generate n x n matrices for x,y,z according to eqn of torus
x=(R+r.*cos(Th)).*cos(Phi);
y=(R+r.*cos(Th)).*sin(Phi);
z=r.*sin(Th);
shp=alphaShape(x(:),y(:),z(:));
shp.Alpha=2.5;
[elements,nodes] = boundaryFacets(shp);
nodes = nodes';
elements = elements';
model = createpde();
mesh=geometryFromMesh(model,nodes,elements);
generateMesh(model)
pdeplot3D(model)
  2 Comments
John D'Errico
John D'Errico on 13 Jan 2021
You want an automatic scheme that will create a perfectly regular triangular mesh on any shape. Good luck in that.
James Johnson
James Johnson on 13 Jan 2021
Edited: James Johnson on 13 Jan 2021
@John D'Errico Thanks for showing interest. I'll add some helpful context later. That's not quite what I want. Not "any shapes": All shapes are either N-gons, created through extrusion, or created through revolution (except the holes, which are cut in). Hence the shapes are parameteric, this should make it easier. "perfect" also isn't quite right. I just want "not-garbage" (see the garbage toroid above) blender does this, as does MATLAB's own "vrworld". Any importing from an stl file does this. I just can't do extrusions and revolutions in MATLAB's "vrworld". I'll update my question with exactly why tomorrow.

Sign in to comment.

Answers (1)

Nipun
Nipun on 31 May 2024
Hi James,
I understand that you want to generate triangular meshes for arbitrary shapes with regular triangles and adjustable triangle sizes. Here is a method to create a triangular mesh for a toroid using the alphaShape and generateMesh functions:
R = 5; % outer radius of torus
r = 2; % inner tube radius
th = linspace(0,2*pi,144); % partitions along perimeter of the tube
phi = linspace(0,2*pi,72); % partitions along azimuth of torus
[Phi,Th] = meshgrid(phi,th);
x = (R + r.*cos(Th)).*cos(Phi);
y = (R + r.*cos(Th)).*sin(Phi);
z = r.*sin(Th);
shp = alphaShape(x(:), y(:), z(:));
shp.Alpha = 2.5;
[elements, nodes] = boundaryFacets(shp);
nodes = nodes';
elements = elements';
model = createpde();
geometryFromMesh(model, nodes, elements);
generateMesh(model, 'GeometricOrder', 'linear', 'Hmax', 0.5);
pdeplot3D(model);
For more information on "alphaShape", "boundaryFacets" and "geometryFromMesh" functions, refer to the following MathWorks documentation:
  1. alphaShape function in MATLAB: https://www.mathworks.com/help/matlab/ref/alphashape.html
  2. boundaryFacets function in MATLAB: https://www.mathworks.com/help/matlab/ref/alphashape.boundaryfacets.html
  3. geometryFromMesh function in MATLAB: https://www.mathworks.com/help/matlab/ref/alphashape.boundaryfacets.html
Hope this helps.
Regards,
Nipun

Categories

Find more on Triangulation Representation in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!