Cylinder with gyroid infill
10 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I have following matlab code
% Clear all previous commands
clear all
close all
clc
% Variables you can change
SizeL = 20; %average length of RVE
Def = 40; %definition
% Variables you shouldn´t change
SFact = (SizeL/2)/pi; %size factor of RVE
A = SFact*pi; %lowest and max coordinates of meshgrid
D = A/Def; %definition factor
% Generation of gyroids
[X,Y,Z] = meshgrid(-A:D:A); %creates mesh grid
% Gyroid equation
OBJ = cos(X/SFact).* sin(Y/SFact) + cos(Y/SFact).* sin(Z/SFact)...
+ cos(Z/SFact).* sin(X/SFact)+(0);
T = 0.5;
OBJ = (OBJ-T).*(OBJ+T);
% Isosurface and isocap
[F1,V1] = isosurface(X,Y,Z,OBJ,0);
[F2,V2] = isocaps(X,Y,Z,OBJ,0,'below');
%Combines isosurface and isocaps into one
F3 = [F1;F2+length(V1(:,1))];
V3 = [V1;V2];
% Visualization
P = patch('Vertices',V3,'Faces',F3,'FaceColor', ...
'red','EdgeColor','none');
view(3)
camlight
% STL export of independet files per gyroid.
% Change current file path with desired file path.
stlwrite(['C:\...filepath...\G1-T05' num2str(1) '.stl'],F3,V3);
It generates cubical unit cell with gyroid infill. I would need to change it to produce the cylindrical unit cell with height H and radius R with the same infill. I am failing to change the code. Can you help me?
Thank you
回答 (2 件)
Arun
2024 年 6 月 17 日
Hi @Premysi,
I understand that you are looking to convert a cubical unit cell to a cylindrical unit cell with height H and radius R.
To achieve this transformation, utilize the “meshgrid” function and apply mask to the gyroid structure so that it fits within the specified cylindrical domain.
Please find the modified code which might be helpful in producing the desired figure.
% Clear all previous commands
clear all
close all
clc
% Cylinder dimensions
R = 10; % Radius of the cylinder
H = 20; % Height of the cylinder
% Variables you can change
SizeL = 20; %average length of RVE
Def = 40; %definition
% Variables you shouldn´t change
SFact = (SizeL/2)/pi; %size factor of RVE
A = SFact*pi; %lowest and max coordinates of meshgrid
D = A/Def; %definition factor
% Generation of gyroids
[X,Y,Z] = meshgrid(-A:D:A, -A:D:A, linspace(-H/2, H/2, Def)); % Adjusted for cylindrical height
% Convert to cylindrical coordinates to apply mask
[~, rho] = cart2pol(X, Y);
% Gyroid equation
OBJ = cos(X/SFact).* sin(Y/SFact) + cos(Y/SFact).* sin(Z/SFact)...
+ cos(Z/SFact).* sin(X/SFact)+(0);
T = 0.5;
OBJ = (OBJ-T).*(OBJ+T);
% Apply cylindrical mask
OBJ(rho > R) = NaN; % This makes points outside the cylinder NaN, effectively removing them
% Isosurface and isocap
[F1,V1] = isosurface(X,Y,Z,OBJ,0);
[F2,V2] = isocaps(X,Y,Z,OBJ,0,'below');
%Combines isosurface and isocaps into one
F3 = [F1;F2+length(V1(:,1))];
V3 = [V1;V2];
figure;
% Visualization
hold on
P = patch('Vertices',V3,'Faces',F3,'FaceColor', ...
'red','EdgeColor','none');
view(3)
camlight
For additional information related to the “cart2pol” function, please refer to the following MathWorks documentation link: https://www.mathworks.com/help/matlab/ref/cart2pol.html
Hope this helps!
Regards
Arun
0 件のコメント
DGM
2025 年 7 月 18 日
編集済み: DGM
2025 年 7 月 19 日
If your goal is to print the part, then you need a closed surface. Simply deleting faces beyond a certain radius opens the surface and leaves a ragged open edge.
If all you want is to print a trinket gyroid cylinder, then you don't need to do any of this. You can do the whole thing entirely in the slicer. Just throw down a cylinder primitive, set the infill to gyroid, and set the number of shells to 0. For a cylinder, it should work, but for other shapes (and depending on the slicer), it might make some floating geometry that won't print right (like the corners in your cube).
Let's say you wanted more control over everything, or maybe you were a masochist who really wanted to do it in MATLAB for no good reason. Is there a way an easy way to just cut the gyroid block into a cylinder without leaving open edges? I don't know of one that's MATLAB-ish. In other software, you'd do this with CSG, but there really aren't first-party tools for that.
There are some third-party tools on the File Exchange, and I personally resort to using system() calls to use OpenSCAD externally. Most of the FEX tools also rely on external libraries to do the work, though there is one that appears to do everything in m-code. You're free to review those options here:
For this example, I'm going to use the same scad_op() that I used in #561428, though I must again emphasize that this code will not work without access to OpenSCAD. The other custom tools are attached, though the dependency means I can't actually run this in the forum editor. If you were to install one of the options (e.g. GPToolbox's mesh_boolean()), then that is what you'd use instead of scad_op().
% Variables you can change
width = 20; % width of gyroid cube
N = 30; % resolution of the triangulation
T = 0.5; % wall thickness
nc = 2; % number of cycles over width
% a gyroid solid %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% setup
SFact = (width/2)/(nc*pi); % size factor of RVE
A = width/2; % lowest and max coordinates of meshgrid
D = A/N; % definition factor
[X,Y,Z] = meshgrid(-A:D:A); %creates mesh grid
% 3D scalar field
OBJ = cos(X/SFact).*sin(Y/SFact) ...
+ cos(Y/SFact).*sin(Z/SFact) ...
+ cos(Z/SFact).*sin(X/SFact);
OBJ = (OBJ-T).*(OBJ+T);
% Isosurface and isocap
[F1,V1] = isosurface(X,Y,Z,OBJ,0);
[F2,V2] = isocaps(X,Y,Z,OBJ,0,'below');
% Combine isosurface and isocaps into one
% and prune the redundant vertices
[Fg Vg] = tricat({{F1 V1},{F2 V2}}); % attached
% Visualization
patch('faces',Fg,'vertices',Vg,'facecolor','w','edgecolor','none');
view(3); camlight; view(10,33)
axis equal; grid on

%% a cylinder solid %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% generate ordered 2D vertices (a circle)
theta = linsegment(0,2*pi,N*2).'; % CCW sequence
Vc2 = width/2*[cos(theta) sin(theta)];
% construct the edge list in the same direction
v = 1:size(Vc2,1);
E = [v; circshift(v,-1)].';
% do a constrained triangulation to generate the triangles
T = delaunayTriangulation(Vc2,E);
Fc = T.ConnectivityList(isInterior(T),:);
Vc = T.Points;
% extrude the part into 3D
% make it arbitrarily taller than the cube
[Fc Vc] = extrude(Fc,Vc,width*2); % attached
% offset to center on z=0
Vc(:,3) = Vc(:,3) - width;
% display it using patch()
hold on;
patch('faces',Fc,'vertices',Vc,'facecolor','r','edgecolor','none', ...
'facealpha',0.4);

%% combine the models %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% do CSG on the two models
% this will be quite slow since the model is very large
% and the roundabout way we're using CGAL defeats geometry caching
[F V] = scad_op(Fc,Vc,Fg,Vg,'intersection'); % attached
% display it using patch()
figure
patch('faces',F,'vertices',V,'facecolor','w','edgecolor','none');
view(3); camlight; view(10,33)
axis equal; grid on
% Since R2018b, MATLAB has STL tools
% you don't need to go get old ones.
stlwrite(triangulation(F,V),'test.stl') % about 6MB in binary encoding!

This is a fairly heavy model, with 115k faces, even as small as it is.
FWIW, I did the cylinder generation in MATLAB just to be as MATLABish as I could. It would be way easier to just save the gyroid cube, throw it in OpenSCAD and intersect it with a cylinder. Hell, since we're already calling it, it'd almost be easier to generate the cylinder in SCAD via the MATLAB-generated SCAD file. I suppose it would be quite a bit uglier to write, though.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Volume Visualization についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


