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

16 ビュー (過去 30 日間)
Niklas Kurz 2023 年 11 月 13 日

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 件のコメントなしを表示なしを非表示
Stefan Kerber 2023 年 11 月 16 日
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 2023 年 11 月 17 日
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.

サインインしてコメントする。

### 採用された回答

Yatharth 2023 年 11 月 29 日
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
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);
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:
I hope this helps!
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Niklas Kurz 2023 年 11 月 29 日

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!

サインインしてコメントする。

### カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by