We have tetrahedra mesh we went to extract a sphere from this mesh and adabting the mesh
6 views (last 30 days)
Show older comments
We do this code, but we found a problem in the output tri
fid = fopen('nodes10.dat');
data = fscanf(fid, '%g %g %g', [3, Inf]);
A = data';
fclose(fid);
A;
n=size(A);
kk=n(1);
R=1.5;
MM=zeros(kk,1);
for k=1:kk
MM(k)=0;
for i=1:3
MM(k)= MM(k)+ A(k,i)*A(k,i);
end
MM(k)=sqrt(MM(k));
end
M = A(MM >R,:);
[x1,y1,z1] = sphere(1);
Psphere = [R*x1(:) R*y1(:) R*z1(:)];
Psphere = unique(Psphere,'rows');
P = [M;Psphere];
plot3(P(:,1),P(:,2),P(:,3),'.')
shp= alphaShape(P(:,1),P(:,2),P(:,3),1);
plot(shp)
shp.Alpha = 2;
[tri,loc] = alphaTriangulation(shp);
fid = fopen('nodess.dat','w');
fprintf(fid,'%i\t %i\t %i\n',loc);
fclose(fid);
fid = fopen('tetreades.dat','w');
fprintf(fid,'%i\t %i\t %i\t %i\n',tri);
fclose(fid);
numtetrahedra = size(tri,1);
numnodes = size(loc);
% model = createpde;
% [gCube,mshC]=geometryFromMesh(model,loc',tri');
% %save ('nodesn.txt', 'tri')
% tri
% loc;
axis equal
3 Comments
Answers (2)
William Rose
on 27 Apr 2025
I had to fixthe filename. You attached file nodes10.txt, but your code calls nodes10.dat. After I fixed that, the script ran without error and it generates a 3D plot of a tetrahedron that looks like it is made of many small tetrahedra. See screenshot.

What else do you want?
7 Comments
William Rose
on 28 Apr 2025
I am sorry to say that I do not understand exactly what the problem is. You said in your comment "look at the picture below", and you provided a screenshot. I cannot tell from that screenshot what the problem is.
Is @Walter Roberson correct when he says "I interpret the remarks as indicating that the original poster wants a tetrahedral output rather than a tri output."? The commented-out lines of your code indicate that you plan to solve a partial differential equation (PDE) model. Are you solving a PDE in a covolume or on the surface (or surfaces)? Are you trying to create a tetrahedral mesh for a solid tetrahedron with a hollow sherical cavity in
the center?
You also said "I think the problem is in sphere(n), i do not know how to choose n, can you help me please?" sphere(n) generates a sphere compsosed of n-by-n faces, where each face is a quadrilateral. The faces at the north pole and south pole look like triangles, because one side of the quadrilateral has length zero. The code blow demonstrates spheres with n=3,4,6,8. The colors are based on the approximate z-value of each face.
subplot(221);sphere(3); axis equal
subplot(222);sphere(4); axis equal
subplot(223);sphere(6); axis equal
subplot(224);sphere(8); axis equal
When I change sphere(1) to sphere(8) in the script, and then rotate and drag the object to see inside, I get the results shown below:

When I use sphere(40) I get the result below:

William Rose
on 10 May 2025
Edited: William Rose
on 11 May 2025
[Edit: Upload modified version of script - see comment below.]
A modified version of your script is attached. This version generates a triangulation that works with geometryFromMesh(), which means you can use the triangulation for a PDE model. The comments in the script explain the changes I made.
I have also written and uploaded a function to diagnose problems with 2D and 3D triangulations. See Analyze triangulation at the File Exchange.
4 Comments
See Also
Categories
Find more on Geometry and Mesh 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!

