Generated point cloud to STL and Mesh representations.

Hi everyone.
Quite a long one here, but I could really do with some the help.
For part of my PhD I am trying to generate geometries via an optimisation scheme and then 3D-print them. I am struggling with taking my generated points and moving them to formats I need, namely STL and perhaps mesh (I am a little confused which would be better). The STL (or mesh) is first used in a PDE solver and this informs optimisation. The prevailing geometry is then manufactured via 3D printing, so I need an STL here. I've been frantically searching forums but to no avail.
Using Boundary and trisurf I can see the geometry (here generated by random inputs):
Now I just need it in the right format for meshing, simualtions and printing. STLwrite demands a triangulation object and trisurf is apparently not one.
Below is my test bed code for generating geometry. It works by creating m regularly placed fans of n equipartioned vectors rotated about the z axis at points along a length L. I included functionality to propogate interstitial vectors to pad the geometry, represented by r, but that may not be necessary. The end product is a point cloud, Q.
%% Setup
clear
clc
close
L = 100; % Length
n = 10; % No. vectors per fan
m = 10; % No. Fans
W = 0 + (10-0).*rand(n,m); % Weights [vec x fan]
r = 10; % No. Interstital Vectors
z = linspace(0,L,m); % Propogate unit vectors along length
U = zeros(3,m); % unit vectors at each z
for i = drange(1:m)
U(1,i) = 1;
U(3,i) = z(i);
end
theta = 2*pi/n;
P = zeros(3,m,n); % Point Cloud pre-interpolation [dim x fans x vectors in fans]
M = m+((m-1)*r); % Number of fans w/ interpolated
Q = zeros(3,(M*n)); % Point Cloud post-interpolation [dim x fans x vectors in fans]
%% Vector scaling
for i = drange(1:m) % fan
for j = drange(1:n) % Vector in fan
w = [W(j,i) 0 0; 0 W(j,i) 0; 0 0 1];% diagonal weight matrix to scale unit vector
rot = [cos((j-1)*theta) sin((j-1)*theta) 0; sin((j-1)*theta) cos((j-1)*theta) 0; 0 0 1]; % rotation matrix
u = w*U(:,i); % scaled u into jth vector
p = rot*u; % rotated jth vector into position
for k = drange(1:3)
P(k,i,j) = p(k); %insert into P
end
end
end
%% Reshape p for graph
%pp = reshape(P,[3,m*n]);
%% Interpolation
k = 1;
for j = drange(1,n) %Every vector in a fan
for i = drange(1,m-1) %Every vector fan in P, -1 is for not going past last fan as this will be the "cap" of the object
a = P(:,i,j); %Set start vector as fan1
b = P(:,i+1,j); %set end vector as fan 2 equivalent
d = b-a;
s = (1/(r+1))*(P(3,i+1,j) - P(3,i,j)); %step size
for ii = drange(0,r+1) % sweep the line between until hits next fan, placing vectors at regular intervals along line
Q(:,k) = a + (ii/(r+1))*d; %Place vector at set intervals between a and b
% k = k+1;
if ii == r+1 && Q(3,k) ~= 100
continue
else
k = k+1;
end
end
end
end
Q(Q>-0.00005 & Q < 0.00005) = 0; % Smooth out numerical error
%% Mesh pointcloud Q [BROKEN - Need Help]
QQ = Q.'; %Reshape Q for plot
% This shows the representation I want but I can't use it for STL I don't
% think
plot3(QQ(:,1),QQ(:,2),QQ(:,3),'.','MarkerSize',10)
grid on
hold on
plot3(U(1,:),U(2,:),U(3,:))
k = boundary(QQ);
k = trisurf(k,QQ(:,1),QQ(:,2),QQ(:,3));
hold off
% My attempt from another thread which doesnt work.
x = QQ(:,1);
y = QQ(:,2);
z = QQ(:,3);
xg = linspace(min(x), max(x), 100); % coordinates of grid
yg = linspace(min(y), max(y), 100);
zg = linspace(min(z), max(z), 100);
[Xg,Yg,Zg] = meshgrid(xg,yg,zg);
surf(Xg,Yg,Zg)
I aslo tried this:
QQ = boundary(QQ); % AND with this as simply QQ, not boundary
plot3(QQ(:,1),QQ(:,2),QQ(:,3),'.','MarkerSize',10)
x = QQ(:,1);
y = QQ(:,2);
z = QQ(:,3);
tri = delaunay(x,y);
h = trisurf(tri, x, y, z);
axis vis3d
However the results are nonsense:

Réponses (1)

Harrison McAleese
Harrison McAleese le 28 Mar 2022

1 vote

In the interest of potentially helping others with similar problems, I am going to write a custom STL meshing script to counter this problem. Will update progress.

1 commentaire

I also have this problem, although it can be implemented through MeshLab, but it requires more steps

Connectez-vous pour commenter.

Produits

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by