Clear Filters
Clear Filters

How to design a solid Body for even Meshing in Matlab? (Hemisphere)

11 views (last 30 days)
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):
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:
So once againg: Is there a workflow for creating a solid body in Matlab that can be evenly triangulated?
  2 Comments
Stefan Kerber
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?
Niklas Kurz
Niklas Kurz on 17 Nov 2023
Hi Stefan,
thank you for you suggestions. I tinkered for a while with it, but interp2 doesn't seem to work with the way I defined the hemisphere
R = 1;
n = 30;
phi = linspace(0,2*pi,n);
theta = linspace(0,pi/2,n);
[phigrid,thetagrid] = meshgrid(phi,theta);
xsphere = R*cos(phigrid).*sin(thetagrid);
ysphere = R*sin(phigrid).*sin(thetagrid);
zsphere = R*cos(thetagrid);
My aim still persisted in extracting merely the surface of the hemisphere in order to perform proper (in the sense of 'even') meshing, but none of the solutions that I have browsed (like surf2stl or as you said interp2) seem to do the job - the wireframe is always uneven as you can observe in the Matlab plot above .
However, that's not as bad, since I managed to control the sphere parameters in FreeCad now.
It also may be done in Matlab, but of course Matlab was not designed for Modeling objects.

Sign in to comment.

Accepted Answer

Yatharth
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);
Warning: Duplicate data points have been detected and removed.
The Triangulation indices are defined with respect to the unique set of points in delaunayTriangulation.
% 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:
  1. delaunayTriangulation https://www.mathworks.com/help/matlab/ref/delaunaytriangulation.html
  2. alphaShape https://www.mathworks.com/help/matlab/ref/alphashape.html
  3. boundaryFacets https://www.mathworks.com/help/matlab/ref/alphashape.boundaryfacets.html
  4. triangulation https://www.mathworks.com/help/matlab/ref/triangulation.html
  5. stlwrite https://www.mathworks.com/help/matlab/ref/stlwrite.html
  6. generateMesh https://www.mathworks.com/help/pde/ug/pde.pdemodel.generatemesh.html
I hope this helps!
  1 Comment
Niklas Kurz
Niklas Kurz on 29 Nov 2023
Edited: Niklas Kurz on 29 Nov 2023
Awesome, exactly what I was hoping for! Matlab ist able to do it. (Of cousre it is). Also thanks for the really detailed answer, it is all clear to me right now. I finally hope that your answer will provide help for others who ran into the same problem (I feel like I was not the only one)
Cheers!

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!