How to 3d patch between 2 3d shapes (stl attached)

10 views (last 30 days)
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');
Error using stlread
Unable to open file '.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.)
the outer surface is the yellow one and the inner surface is the red one.

Accepted Answer

Star Strider
Star Strider on 12 May 2023
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 Comments
smith
smith on 12 May 2023
Hello, Here is the Zipped stl file, it's not the same geometry no, but what i need on mine is exactly same type of lid that you have did, but if it's possible to cover all the difference between the two surfaces.
zipped stl is uploaded. please check it out.
I'm looking for thickness to cover the in between the surfaces all the way to then extract it as stl file
Star Strider
Star Strider on 13 May 2023
Edited: Star Strider on 13 May 2023
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')
Uzip = 1×1 cell array
{'_geometry.stl'}
TR = stlread(Uzip{1})
TR =
triangulation with properties: Points: [15047×3 double] ConnectivityList: [30090×3 double]
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
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
[cx,cy] = centroid(shp) % Centroid (x,y) Coordinates
cx = 3.6345
cy = -165.9400
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) ]
k = 15047×3
1.0045 -159.4979 -221.6322 0.9985 -159.4696 -221.6729 0.9965 -159.4702 -221.6730 -3.8860 -175.6318 -225.1760 -3.9088 -175.6476 -225.1813 -3.8867 -175.6236 -225.1993 -3.9105 -175.6321 -225.2318 -3.8877 -175.6137 -225.2334 -3.9101 -175.6230 -225.2662 -3.8885 -175.6061 -225.2675
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)])] } )
cummeans = 119×1 cell array
{[ 1 -3.1398 9.1577 -226.6899 1 -3.0900 9.7459 -223.0463]} {[ 2 -3.0877 9.3631 -226.7986 2 -3.0378 9.8350 -223.1397]} {[ 3 -3.0362 9.5813 -226.9612 3 -2.9876 9.9650 -223.3078]} {[ 4 -2.9859 9.8659 -227.0143 4 -2.9377 10.2274 -223.5327]} {[ 5 -2.9356 10.1120 -227.2184 5 -2.8857 10.5571 -223.5094]} {[ 6 -2.8846 10.3680 -227.3669 6 -2.8352 10.7357 -223.6198]} {[ 7 -2.8344 10.4577 -227.5171 7 -2.7845 10.8438 -223.7256]} {[ 8 -2.7838 10.5891 -227.6170 8 -2.7346 10.9115 -223.8862]} {[ 9 -2.7328 10.7849 -227.1203 9 -2.6920 11.0847 -226.0076]} {[10 -2.6769 10.7422 -227.9467 10 -2.6272 11.6406 -224.2873]} {[11 -2.6265 10.8540 -228.0062 11 -2.5770 11.8351 -224.3070]} {[12 -2.5736 10.9024 -228.1967 12 -2.5243 11.8861 -224.4458]} {[13 -2.5228 10.9222 -228.2916 13 -2.4801 11.9337 -224.8925]} {[14 -2.4512 10.9302 -228.4786 14 -2.4062 12.2049 -224.7661]} {[15 -2.3947 10.8965 -228.5691 15 -2.3617 12.2261 -224.8861]} {[16 -2.3351 10.8189 -228.7150 16 -2.2852 12.2276 -224.9881]}
Szv = cellfun(@(x)size(x,2), cummeans);
%
% Szvm = buffer(Szv,12)
mtx = cell2mat(cummeans(Szv==8))
mtx = 119×8
1.0000 -3.1398 9.1577 -226.6899 1.0000 -3.0900 9.7459 -223.0463 2.0000 -3.0877 9.3631 -226.7986 2.0000 -3.0378 9.8350 -223.1397 3.0000 -3.0362 9.5813 -226.9612 3.0000 -2.9876 9.9650 -223.3078 4.0000 -2.9859 9.8659 -227.0143 4.0000 -2.9377 10.2274 -223.5327 5.0000 -2.9356 10.1120 -227.2184 5.0000 -2.8857 10.5571 -223.5094 6.0000 -2.8846 10.3680 -227.3669 6.0000 -2.8352 10.7357 -223.6198 7.0000 -2.8344 10.4577 -227.5171 7.0000 -2.7845 10.8438 -223.7256 8.0000 -2.7838 10.5891 -227.6170 8.0000 -2.7346 10.9115 -223.8862 9.0000 -2.7328 10.7849 -227.1203 9.0000 -2.6920 11.0847 -226.0076 10.0000 -2.6769 10.7422 -227.9467 10.0000 -2.6272 11.6406 -224.2873
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’')
.

Sign in to comment.

More Answers (1)

Shaik
Shaik on 12 May 2023
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.
  1 Comment
smith
smith on 12 May 2023
I’m aware of this, this isn’t an issue, how do you think I imported the vectors and uploaded an image of the data? It’s showing like this on the question form because I can’t upload the stl file

Sign in to comment.

Categories

Find more on Visual Exploration in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!