How to export 3D sphere at stl format
8 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
Chris
2023 年 12 月 3 日
編集済み: Chris
2023 年 12 月 3 日
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 件のコメント
Chris
2023 年 12 月 5 日
@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.
参考
カテゴリ
Help Center および File Exchange で Delaunay Triangulation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!