How to 3d patch between 2 3d shapes (stl attached)
4 次查看(过去 30 天)
显示 更早的评论
Hello, I'm trying to fill in or create 2 lids on top and bottom sides for this 3d shape and then extract it as stl, but I'm not sure how.
close all
clc
clear all
TR = stlread ('.stl');
x = TR.Points(:,1); % inner surface
y = TR.Points(:,2); % inner surface
z = TR.Points(:,3); % inner surface
xyz = [(mean (x)) ; (mean (y)); (mean(z))]; %centroid
n=size(x);
scatter3(mean(x),mean(y),mean(z));
hold on
% x(1:n)=x(1:n)+1;
% y(1:n)=y(1:n)+1;
% z(1:n)=z(1:n)+1;
scatter3(x,y,z);
% vector definitions
x1 = x(:,1) -xyz(1);
y1 = y(:,1) - xyz(2);
z1 = z(:,1) - xyz(3);
%length of the vector
x2 = sqrt ((x1(:,1).^2) + (y1(:,1).^2) + (z1(:,1).^2));
thickness = 2; % desired thickness
x2mod = x2(:,1) + thickness; %%length + demanded thickness
SF = x2mod(:,1)./x2(:,1); % scaling factor
% accounting for different references
X =x(:,1).*(SF(:,1));
X = X + xyz(1).*(1- SF); %% final X coordinates of the outer surface
Y =y(:,1).*(SF(:,1));
Y = Y + xyz(2).*(1- SF); %% final Y coordinates of the outer surface
Z =z(:,1).*(SF(:,1));
Z = Z + xyz(3).*(1- SF); %% final Z coordinates of the outer surface
hold on
scatter3(X,Y,Z); % final plot
i = TR.ConnectivityList % connectivity list
k= [ X(:,1) , Y(:,1), Z(:,1) ];
TR1 = triangulation(k,X,Y,Z); %%isn't working and showing this error (The input triangulation must contain index values. Entries with fractional parts are invalid.)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1381459/image.png)
0 个评论
采纳的回答
Star Strider
2023-5-12
Use the zip function to zip the .stl file. Then (unless it’s greater than the 5 MB liimit), upload the .zip file.
That problem aside —
t = linspace(0, 2*pi, 500).'; % Assume Column Vectors
r = [5 7];
x = cos(t)*r; % Size: numel(t) x numel(r)
y = sin(t)*r; % Size: numel(t) x numel(r)
z = sin(t*6)+randn(numel(t),2)/5; % Size: numel(t) x numel(r)
figure
plot3(x(:,1), y(:,1), z(:,1), 'LineWidth',1) % Inner Circle
hold on
plot3(x(:,2), y(:,2), z(:,2), 'LineWidth',1) % Outer Circle
hold off
grid
axis('equal')
title('Original')
figure
plot3(x(:,1), y(:,1), z(:,1), 'LineWidth',1) % Inner Circle
hold on
plot3(x(:,2), y(:,2), z(:,2), 'LineWidth',1) % Inner Circle
patch([x(:,1); x(:,2)], [y(:,1); y(:,2)], [z(:,1); z(:,2)], 'g', 'EdgeColor','none') % 'Hat'
hold off
grid
axis('equal')
title('With ‘Hat’')
% view(0,90)
I don’t understand what your ‘X’, ‘Y’, and ‘Z’ matrices are, however if they resemble mine (that I hope are straightforward to understand), they might work with my code without further modification. If it’s not possible for you to upload your data, it might be possible for you to share all (or downdampled versios of) your ‘X’, ‘Y’, and ‘Z’ matrices.
.
2 个评论
Star Strider
2023-5-13
编辑:Star Strider
2023-5-13
I’m going to need help to work with this. I was hoping for two well-defined sets of coordinates for the inner and outer circles, similar to what I created, that are ordered.
I have no experience with .stl data, so I simply do not understand what the data are, or what they represent. I have no idea how to work with them.
EDIT — (13 May 2023 at 14:58)
This is the best I can do with these data. I’m not sure how easy it will be to extract them for STL use, since my primary objective is simply to produce the surfaces.
Try this —
Uzip = unzip('_geometry.zip')
TR = stlread(Uzip{1})
x = TR.Points(:,1); % inner surface
y = TR.Points(:,2); % inner surface
z = TR.Points(:,3); % inner surface
xyz = [(mean (x)) ; (mean (y)); (mean(z))]; %centroid
n=size(x);
scatter3(mean(x),mean(y),mean(z));
hold on
% x(1:n)=x(1:n)+1;
% y(1:n)=y(1:n)+1;
% z(1:n)=z(1:n)+1;
scatter3(x,y,z);
% vector definitions
x1 = x(:,1) -xyz(1);
y1 = y(:,1) - xyz(2);
z1 = z(:,1) - xyz(3);
%length of the vector
x2 = sqrt ((x1(:,1).^2) + (y1(:,1).^2) + (z1(:,1).^2));
thickness = 2; % desired thickness
x2mod = x2(:,1) + thickness; %%length + demanded thickness
SF = x2mod(:,1)./x2(:,1); % scaling factor
% accounting for different references
X =x(:,1).*(SF(:,1));
X = X + xyz(1).*(1- SF); %% final X coordinates of the outer surface
Y =y(:,1).*(SF(:,1));
Y = Y + xyz(2).*(1- SF); %% final Y coordinates of the outer surface
Z =z(:,1).*(SF(:,1));
Z = Z + xyz(3).*(1- SF); %% final Z coordinates of the outer surface
hold on
scatter3(X,Y,Z, 0.1, Z/max(Z), '.'); % final plot
xlabel('X')
ylabel('Y')
zlabel('Z')
axis('equal')
shp = polyshape(X,Y); % Use 'polyshape' To Get Centroid
[cx,cy] = centroid(shp) % Centroid (x,y) Coordinates
figure
scatter3(X,Y,Z, 0.1, Z/max(Z), '.'); % final plot
hold on
scatter3(cx, cy, mean(Z), 150, [1 0 1], 'p', 'filled')
hold off
axis('equal')
xlabel('X')
ylabel('Y')
zlabel('Z')
view(0,90)
i = TR.ConnectivityList; % connectivity list
k= [ X(:,1) , Y(:,1), Z(:,1) ]
x = X;
y = Y;
z = Z;
th = atan2(Y-cy, X-cx);
r = hypot(Y-cy, X-cx);
[Uth,thia,thic] = uniquetol(th, 0.05, 'DataScale',1);
cummeans = accumarray(thic, (1:numel(thic)).', [], @(x) { [min([thic(x) th(x) r(x) Z(x)]) max([thic(x) th(x) r(x) Z(x)])] } )
Szv = cellfun(@(x)size(x,2), cummeans);
%
% Szvm = buffer(Szv,12)
mtx = cell2mat(cummeans(Szv==8))
minc = sortrows(mtx(:,2:4),1);
[x1,y1,z1] = pol2cart(minc(:,1), minc(:,2), minc(:,3));
maxc = sortrows(mtx(:,6:8),1);
[x2,y2,z2] = pol2cart(maxc(:,1), maxc(:,2), maxc(:,3));
x = [x1 x2];
y = [y1 y2];
z = [z1 z2];
figure
plot3(x(:,1), y(:,1), z(:,1), 'LineWidth',1) % Inner Circle
hold on
plot3(x(:,1), y(:,1), z(:,2), 'LineWidth',1)
plot3(x(:,2), y(:,2), z(:,1), 'LineWidth',1)
plot3(x(:,2), y(:,2), z(:,2), 'LineWidth',1) % Inner Circle
patch([x(:,1); (x(:,2))], [y(:,1); (y(:,2))], [z(:,1); (z(:,1))], 'g', 'EdgeColor','none', 'FaceAlpha',0.5) % 'Shoes'
patch([x(:,1); (x(:,2))], [y(:,1); (y(:,2))], [z(:,2); (z(:,2))], 'r', 'EdgeColor','none', 'FaceAlpha',0.5) % 'Hat'
patch([x(:,1); (x(:,1))], [y(:,1); (y(:,1))], [z(:,1); (z(:,2))], 'c', 'EdgeColor','none', 'FaceAlpha',0.5) % 'Inner Surface'
patch([x(:,2); (x(:,2))], [y(:,2); (y(:,2))], [z(:,1); (z(:,2))], 'm', 'EdgeColor','none', 'FaceAlpha',0.5) % 'Outer Surface'
% % patch([x(:,1); (x(:,2))], [y(:,1); (y(:,2))], [z(:,1); (z(:,2))], 'g', 'EdgeColor','none', 'FaceAlpha',0.5) % 'Hat'
hold off
grid on
axis('equal')
xlabel('X')
ylabel('Y')
zlabel('Z')
title('With ‘Hat’')
[az,el] = view;
% return
figure
plot3(x(:,1), y(:,1), z(:,1), 'LineWidth',1) % Inner Circle
hold on
plot3(x(:,1), y(:,1), z(:,2), 'LineWidth',1)
plot3(x(:,2), y(:,2), z(:,1), 'LineWidth',1)
plot3(x(:,2), y(:,2), z(:,2), 'LineWidth',1) % Inner Circle
patch([x(:,1); (x(:,2))], [y(:,1); (y(:,2))], [z(:,1); (z(:,1))], 'g', 'EdgeColor','none', 'FaceAlpha',0.5) % 'Shoes'
patch([x(:,1); (x(:,2))], [y(:,1); (y(:,2))], [z(:,2); (z(:,2))], 'r', 'EdgeColor','none', 'FaceAlpha',0.5) % 'Hat'
patch([x(:,1); (x(:,1))], [y(:,1); (y(:,1))], [z(:,1); (z(:,2))], 'c', 'EdgeColor','none', 'FaceAlpha',0.5) % 'Inner Surface'
patch([x(:,2); (x(:,2))], [y(:,2); (y(:,2))], [z(:,1); (z(:,2))], 'm', 'EdgeColor','none', 'FaceAlpha',0.5) % 'Outer Surface'
% % patch([x(:,1); (x(:,2))], [y(:,1); (y(:,2))], [z(:,1); (z(:,2))], 'g', 'EdgeColor','none', 'FaceAlpha',0.5) % 'Hat'
hold off
grid on
axis('equal')
xlabel('X')
ylabel('Y')
zlabel('Z')
title('With ‘Hat’')
view(az, 10)
figure
plot3(x(:,1), y(:,1), z(:,1), 'LineWidth',1) % Inner Circle
hold on
plot3(x(:,1), y(:,1), z(:,2), 'LineWidth',1)
plot3(x(:,2), y(:,2), z(:,1), 'LineWidth',1)
plot3(x(:,2), y(:,2), z(:,2), 'LineWidth',1) % Inner Circle
patch([x(:,1); (x(:,2))], [y(:,1); (y(:,2))], [z(:,1); (z(:,1))], 'g', 'EdgeColor','none', 'FaceAlpha',0.5) % 'Shoes'
patch([x(:,1); (x(:,2))], [y(:,1); (y(:,2))], [z(:,2); (z(:,2))], 'r', 'EdgeColor','none', 'FaceAlpha',0.5) % 'Hat'
patch([x(:,1); (x(:,1))], [y(:,1); (y(:,1))], [z(:,1); (z(:,2))], 'c', 'EdgeColor','none', 'FaceAlpha',0.5) % 'Inner Surface'
patch([x(:,2); (x(:,2))], [y(:,2); (y(:,2))], [z(:,1); (z(:,2))], 'm', 'EdgeColor','none', 'FaceAlpha',0.5) % 'Outer Surface'
% % patch([x(:,1); (x(:,2))], [y(:,1); (y(:,2))], [z(:,1); (z(:,2))], 'g', 'EdgeColor','none', 'FaceAlpha',0.5) % 'Hat'
hold off
grid on
axis('equal')
xlabel('X')
ylabel('Y')
zlabel('Z')
title('With ‘Hat’')
view(az, 80)
figure
plot3(x(:,1), y(:,1), z(:,1), 'LineWidth',1) % Inner Circle
hold on
plot3(x(:,1), y(:,1), z(:,2), 'LineWidth',1)
plot3(x(:,2), y(:,2), z(:,1), 'LineWidth',1)
plot3(x(:,2), y(:,2), z(:,2), 'LineWidth',1) % Inner Circle
patch([x(:,1); (x(:,2))], [y(:,1); (y(:,2))], [z(:,1); (z(:,1))], 'g', 'EdgeColor','none', 'FaceAlpha',0.99) % 'Shoes'
patch([x(:,1); (x(:,2))], [y(:,1); (y(:,2))], [z(:,2); (z(:,2))], 'r', 'EdgeColor','none', 'FaceAlpha',0.99) % 'Hat'
patch([x(:,1); (x(:,1))], [y(:,1); (y(:,1))], [z(:,1); (z(:,2))], 'c', 'EdgeColor','none', 'FaceAlpha',0.99) % 'Inner Surface'
patch([x(:,2); (x(:,2))], [y(:,2); (y(:,2))], [z(:,1); (z(:,2))], 'm', 'EdgeColor','none', 'FaceAlpha',0.99) % 'Outer Surface'
% % patch([x(:,1); (x(:,2))], [y(:,1); (y(:,2))], [z(:,1); (z(:,2))], 'g', 'EdgeColor','none', 'FaceAlpha',0.5) % 'Hat'
hold off
grid on
axis('equal')
xlabel('X')
ylabel('Y')
zlabel('Z')
title('With ‘Hat’')
.
更多回答(1 个)
Shaik
2023-5-12
The error "unable to open file .stl" suggests that MATLAB is unable to find the STL file you are trying to import.
In the first line of your code, you should replace '.stl' with the actual name of the STL file you want to import, including the file extension.
So, for example, if your STL file is named my_stl_file.stl and is located in the same directory as your MATLAB script, you would modify the first line of your code to:
TR = stlread ('my_stl_file.stl');
Make sure to replace my_stl_file.stl with the actual name of your STL file. If the file is located in a different directory, you will need to specify the full file path in single quotes.
If the file still cannot be found, make sure to check that the name and location of the STL file are correct, and that you have read permissions for the file and directory.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Visual Exploration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!