How to export 3D sphere at stl format
9 views (last 30 days)
Show older comments
Hi all!
I created an ellipsoid from 3 matrixes of x,y, and z. The ellipsoid is tilted upward and eastward if it changes something. I'm trying to export the ellipsoid in STL format. I've tried using the write stl function but without success.
Any help in this problem would be amazing, attached is my code.
Thanks,
Yaad
% Clear the workspace
clear
clc
% Define the dimensions of the sphere
semiMajorAxis = 1; % Semi-major axis
semiMinorAxis = 0.5; % Semi-minor axis (ratio of 0.9)
% Define the number of points for sphere meshing
numPoints = 100;
% Create a meshgrid for the sphere
phi = linspace(0, pi, numPoints);
theta = linspace(0, 2*pi, numPoints);
[phi, theta] = meshgrid(phi, theta);
% Calculate the coordinates of the points on the ellipsoid
x = semiMajorAxis * sin(phi) .* cos(theta);
y = semiMajorAxis * sin(phi) .* sin(theta);
z = semiMinorAxis * cos(phi);
%%
% Define the tilt angle in radians (45 degrees upward)
tiltAngle = deg2rad(90);
% Define the rotation matrix for the tilt
R = [1, 0, 0; 0, cos(tiltAngle), -sin(tiltAngle); 0, sin(tiltAngle), cos(tiltAngle)];
% Apply the rotation matrix to the coordinates
rotatedCoords = R * [x(:)'; y(:)'; z(:)'];
% Reshape the rotated coordinates back to a grid
xRotated = reshape(rotatedCoords(1, :), size(x));
yRotated = reshape(rotatedCoords(2, :), size(y));
zRotated = reshape(rotatedCoords(3, :), size(z));
%%
% Create a 3D plot of the tilted ellipsoid
subplot(1,2,1)
surf(xRotated, yRotated, zRotated);
axis equal; % Equal aspect ratio
xlabel('X');
ylabel('Y');
zlabel('Z');
title('3D Sphere with Ratio 1:0.9 (45-Degree Upward Tilt)');
grid on;
%%
% Clear the workspace
% Define the dimensions of the sphere
semiMajorAxis = 1; % Semi-major axis
semiMinorAxis = 0.5; % Semi-minor axis (ratio of 0.9)
% Define the number of points for sphere meshing
numPoints = 100;
% Create a meshgrid for the sphere
phi = linspace(0, pi, numPoints);
theta = linspace(0, 2*pi, numPoints);
[phi, theta] = meshgrid(phi, theta);
% Calculate the coordinates of the points on the ellipsoid
x = semiMajorAxis * sin(phi) .* cos(theta);
y = semiMajorAxis * sin(phi) .* sin(theta);
z = semiMinorAxis * cos(phi);
% Just one sample ;
% Define the tilt angles in radians (45 degrees upward and 30 degrees east)
upwardTiltAngle = deg2rad(45);
eastTiltAngle = deg2rad(90);
% Define the rotation matrices for the tilts
R_upward = [1, 0, 0; 0, cos(upwardTiltAngle), -sin(upwardTiltAngle); 0, sin(upwardTiltAngle), cos(upwardTiltAngle)];
R_east = [cos(eastTiltAngle), 0, sin(eastTiltAngle); 0, 1, 0; -sin(eastTiltAngle), 0, cos(eastTiltAngle)];
% Combine the rotation matrices to apply both tilts
R_combined = R_upward * R_east;
% Apply the combined rotation matrix to the coordinates
rotatedCoords = R_combined * [x(:)'; y(:)'; z(:)'];
% Reshape the rotated coordinates back to a grid
xRotated = reshape(rotatedCoords(1, :), size(x));
yRotated = reshape(rotatedCoords(2, :), size(y));
zRotated = reshape(rotatedCoords(3, :), size(z));
% Create a 3D plot of the tilted ellipsoid
subplot(1,2,2)
surf(xRotated, yRotated, zRotated);
axis equal; % Equal aspect ratio
xlabel('X');
ylabel('Y');
zlabel('Z');
title('3D Sphere with Ratio 1:0.9 (45-Degree Upward Tilt and 30-Degree East Tilt)');
filename= ['Sphere_dxf/' 'UPaE']
writedxf(filename, xRotated, yRotated, zRotated);
grid on;
0 Comments
Answers (1)
Chris
on 3 Dec 2023
Edited: Chris
on 3 Dec 2023
You need a triangulation.
stlwrite only works with triangles (vs tetrahedrons), which maybe has to do with an stl only defining the surface of an object.
To get triangles defining the ellipsoid, I used convhull() .
points = rotatedCoords.';
ConnectivityList = convhull(points);
TR = triangulation(ConnectivityList,points);
filename = 'Sphere_stl/UPaE.stl';
stlwrite(TR,filename)
% To view the triangulation:
figure
trisurf(TR)
axis equal
2 Comments
Chris
on 5 Dec 2023
@Yaad Ben Lulu that's simply transposing the rotatedcoords matrix to be compatible with the convhull and triangulation functions (the functions expect an n x 3 matrix)
I use .' out of habit, since the transpose for a complex matrix is different than simply moving numbers around (it gives the complex conjugate). The dot tells Matlab to leave the values alone. In your case, I don't think it will make a difference.
points = rotatedCoords';
should work just as well.
See Also
Categories
Find more on Delaunay Triangulation 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!