How to design a solid Body for even Meshing in Matlab? (Hemisphere)
9 views (last 30 days)
Show older comments
Hello dear Matlab community,
I currently try to:
- create a surface from x y z coordinates (easily done with meshgrid and surf)
- export the surface into FreeCad/Gmsh for creating an even Mesh
The result should look as follows (which was fully created in FreeCad):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1538035/image.png)
Why I try doing all of this ?
- Because I know how to design exact surfaces in Matlab
(In this case I need to exactyl define the contact angle between hemisphere and surface)
- Because I only can create even Meshes in FreeCad
In Matlab I only get an uneven Mesh:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1538040/image.png)
So once againg: Is there a workflow for creating a solid body in Matlab that can be evenly triangulated?
2 Comments
Stefan Kerber
on 16 Nov 2023
Hi Niklas,
if I understand your prpblem correctly, it should be possible to define a regular net using the meshgid function and then interpolate your function to this regular grid. I guess a good staring point for this could be Interpolation for 2-D gridded data in meshgrid format - MATLAB interp2 (mathworks.com)
Does this help?
Accepted Answer
Yatharth
on 29 Nov 2023
Hi Niklas,
I understand that you want to create an even mesh of a hemisphere in MATLAB.
You can create an even mesh using “generateMesh” function. You will be needing a STL file to generate the even mesh.
Here is an example for the same:
1. Creating a hemisphere and ploting it using Triangulation.
% Define hemisphere parameters
radius = 1; % Radius of the hemisphere
resolution = 10; % Number of points along the circumference
% Generate hemisphere coordinates
theta = linspace(0, pi/2, resolution);
phi = linspace(0, 2*pi, 2*resolution);
[theta, phi] = meshgrid(theta, phi);
x = radius * sin(theta) .* cos(phi);
y = radius * sin(theta) .* sin(phi);
z = radius * cos(theta);
points = [x(:), y(:), z(:)];
% Create the triangulation
TR = delaunayTriangulation(points);
% Plot the triangulated hemisphere
tetramesh(TR);
2. You cannot directly use "stlwrite" function to make STL file as Tetrahedron Triangulation is not supported. STL file will eventually act as an input for the "generateMesh" function. . Therefore we will make an "alphaShape" and extract the facets of the "alphaShape" that represents the surface of the AlphaShape using the "boundaryFacets" function
% Making the alphaShape
x = x(:);
y= y(:);
z = z(:);
P = [x y z];
P = unique(P,'rows');
shp = alphaShape(P,1.5);
plot(shp)
axis equal
% Using the boundaryFacets function to make the surface of alphaShape
[tri, xyz] = boundaryFacets(shp);
trisurf(tri,xyz(:,1),xyz(:,2),xyz(:,3),...
'FaceColor','cyan','FaceAlpha',0.3)
axis equal
TR = triangulation(tri,xyz);
3. Now we can simply use the stlwrite function to generate the stl file and generate even mesh using "generateMesh" function
% Write the triangulation object to an STL file
stlwrite(TR,'hemisphere.stl');
% Read the STL file and generate an even mesh
model = createpde;
importGeometry(model, 'hemisphere.stl');
generateMesh(model, 'Hmax', 0.1); % Set maximum element size for even meshing
% Plot the mesh
pdeplot3D(model);
Have a look at the respective documentations for the functions used:
- delaunayTriangulation https://www.mathworks.com/help/matlab/ref/delaunaytriangulation.html
- alphaShape https://www.mathworks.com/help/matlab/ref/alphashape.html
- boundaryFacets https://www.mathworks.com/help/matlab/ref/alphashape.boundaryfacets.html
- triangulation https://www.mathworks.com/help/matlab/ref/triangulation.html
- stlwrite https://www.mathworks.com/help/matlab/ref/stlwrite.html
- generateMesh https://www.mathworks.com/help/pde/ug/pde.pdemodel.generatemesh.html
I hope this helps!
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots 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!