Make valid STL file from overlapping spheres

6 次查看(过去 30 天)
I'm trying to make an STL file from surface data of 2 overlapping spheres (made from [x,y,z] = sphere(n)) by using surf2stl.m available in file exchange but the stl file created is invalid since surfaces are self-intersecting. How to remove faces inside the other sphere and consider bounding surfaces only for the creation of the STL file?

回答(1 个)

Shubham
Shubham 2024-9-3
Hi Sabyasachi,
Creating a valid STL file from overlapping spheres involves removing the intersecting parts and keeping only the outer boundary. Here’s a step-by-step approach to achieve this:
1. Generate the Spheres
First, create the two spheres using MATLAB's sphere function. You can translate one of them to ensure they overlap:
n = 50; % Resolution of the spheres
[x1, y1, z1] = sphere(n);
[x2, y2, z2] = sphere(n);
% Translate the second sphere
x2 = x2 + 1.5; % Adjust translation to ensure overlap
2. Convert to Point Clouds
Convert the surface data of the spheres into point clouds:
points1 = [x1(:), y1(:), z1(:)];
points2 = [x2(:), y2(:), z2(:)];
3. Use Alpha Shape for Boundary Extraction
Use the alphaShape function to extract the boundary of the combined spheres:
% Combine points
combinedPoints = [points1; points2];
% Create alpha shape
alphaValue = 1.0; % Adjust this value as needed
shp = alphaShape(combinedPoints, alphaValue);
% Extract boundary facets
[faces, vertices] = boundaryFacets(shp);
4. Create a Triangulation and Export to STL
Create a triangulation object and export it using a custom STL export function:
TR = triangulation(faces, vertices);
% Custom function to write STL
function writeSTL(filename, TR)
fid = fopen(filename, 'w');
fprintf(fid, 'solid OpenSCAD_Model\n');
for i = 1:size(TR.ConnectivityList, 1)
face = TR.ConnectivityList(i, :);
v1 = TR.Points(face(1), :);
v2 = TR.Points(face(2), :);
v3 = TR.Points(face(3), :);
normal = cross(v2 - v1, v3 - v1);
normal = normal / norm(normal);
fprintf(fid, ' facet normal %.7f %.7f %.7f\n', normal);
fprintf(fid, ' outer loop\n');
fprintf(fid, ' vertex %.7f %.7f %.7f\n', v1);
fprintf(fid, ' vertex %.7f %.7f %.7f\n', v2);
fprintf(fid, ' vertex %.7f %.7f %.7f\n', v3);
fprintf(fid, ' endloop\n');
fprintf(fid, ' endfacet\n');
end
fprintf(fid, 'endsolid OpenSCAD_Model\n');
fclose(fid);
end
% Write to STL
writeSTL('output.stl', TR);
5. Validate the STL File
After exporting, validate the STL file using software like MeshLab or an STL viewer to ensure there are no self-intersections and that the mesh represents only the outer boundary.
Notes
  • Alpha Value Tuning: The alphaValue in alphaShape is crucial. It should be small enough to capture the detail but large enough to merge the two spheres correctly.
  • Mesh Quality: You may need additional post-processing if the mesh quality is not satisfactory. Tools like MeshLab can help with smoothing and repairing meshes.
  • Custom STL Export: The provided function is a simple ASCII STL writer. For large datasets, consider using binary STL for efficiency.
By following these steps, you should be able to create a valid STL file representing the outer boundary of the overlapping spheres.

Community Treasure Hunt

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

Start Hunting!

Translated by