Get triangular meshes for any shape
34 views (last 30 days)
Show older comments
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
on 13 Jan 2021
You want an automatic scheme that will create a perfectly regular triangular mesh on any shape. Good luck in that.
Answers (1)
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:
- alphaShape function in MATLAB: https://www.mathworks.com/help/matlab/ref/alphashape.html
- boundaryFacets function in MATLAB: https://www.mathworks.com/help/matlab/ref/alphashape.boundaryfacets.html
- geometryFromMesh function in MATLAB: https://www.mathworks.com/help/matlab/ref/alphashape.boundaryfacets.html
Hope this helps.
Regards,
Nipun
0 Comments
See Also
Categories
Find more on Triangulation Representation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!