stl to grid error

6 vues (au cours des 30 derniers jours)
donghoo
donghoo le 17 Juil 2024
I tired to use above code to make grid [xgridvector,ygridvector,zheights] use for simscape grid surface.
when I use 'terrain.stl', it works well. But when I use 'untitle.stl' the resault shows like this. please help me T_T..
%%code starts here
[stlData, ~] = stlread('untitled.stl');
vertices = stlData.vertices;
% %
[uniqueVertices, ~, ic] = unique(vertices(:, 1:2), 'rows');
averageZ = accumarray(ic, vertices(:, 3), [], @mean);
vertices = [uniqueVertices, averageZ];
xGridVector = linspace(min(vertices(:,1)), max(vertices(:,1)), 100);
yGridVector = linspace(min(vertices(:,2)), max(vertices(:,2)), 100);
[X, Y] = ndgrid(xGridVector, yGridVector);
F = scatteredInterpolant(vertices(:,1), vertices(:,2), vertices(:,3), 'linear', 'none');
ZHeights = F(X, Y);
ZHeights(isnan(ZHeights)) = min(vertices(:,3));
figure;
surf(X, Y, ZHeights);
title('Grid Surface from STL File');
xlabel('X (meters)');
ylabel('Y (meters)');
zlabel('Z (meters)');
grid on;
function [stlData, units] = stlread(filename)
fid = fopen(filename, 'r');
if fid == -1
error('File could not be opened, check name or path.')
end
M = fread(fid, inf, 'uint8=>uint8');
fclose(fid);
[pathstr, name, ext] = fileparts(filename);
if strcmpi(ext, '.stl') ~= 1
error('Filename must have a .stl extension');
end
if isempty(M)
error('File is empty.');
end
M = char(M');
if (strncmp(M, 'solid', 5))
[stlData.vertices, stlData.faces, units] = stlReadAscii(filename);
else
[stlData.vertices, stlData.faces, units] = stlReadBinary(filename);
end
end
% ASCII 형식 STL 파일 읽기 (단순화된 예시)
function [vertices, faces, units] = stlReadAscii(filename)
fid = fopen(filename, 'r');
vertices = [];
while ~feof(fid)
line = fgetl(fid);
if startsWith(line, 'vertex')
vertex = sscanf(line, 'vertex %f %f %f');
vertices = [vertices; vertex'];
end
end
fclose(fid);
faces = reshape(1:size(vertices, 1), 3, [])';
units = 'mm';
end
% Binary 형식 STL 파일 읽기 (단순화된 예시)
function [vertices, faces, units] = stlReadBinary(filename)
fid = fopen(filename, 'rb');
fseek(fid, 80, 'bof');
numFaces = fread(fid, 1, 'uint32');
faces = zeros(numFaces, 3);
vertices = zeros(numFaces*3, 3);
for i = 1:numFaces
fread(fid, 3, 'float32');
vertices((i-1)*3+1:i*3, :) = fread(fid, [3, 3], 'float32')';
fread(fid, 1, 'uint16');
faces(i, :) = (i-1)*3+1:i*3;
end
fclose(fid);
units = 'mm';
end
  2 commentaires
Kilsu Kim
Kilsu Kim le 22 Juil 2024
Hello Donghoo,
Could you elaborate on your goal? Do you want to visualize the surface of the STL file? Then you can simply run the following code.
% Read the STL file
stlFile = 'untitled.stl'; % Replace with your STL file name
stlData = stlread(stlFile);
% Plot the STL file
figure;
trisurf(stlData.ConnectivityList, stlData.Points(:,1), stlData.Points(:,2), stlData.Points(:,3), ...
'FaceAlpha',0.9,'EdgeColor', 'none');
axis equal;
title('Grid Surface from STL File');
xlabel('X (meters)');
ylabel('Y (meters)');
zlabel('Z (meters)');
grid on; grid minor;
Hope this helps.
donghoo
donghoo le 22 Juil 2024
Thanks for your answering.
The desired goal is to load an stl file and extract data to be used as the xgridVector, ygridVector and zheights parameters used in the grid surface block in simscape multibody.

Connectez-vous pour commenter.

Réponses (1)

Cris LaPierre
Cris LaPierre le 22 Juil 2024
Your files are not the same size. How were they created?
  • terrain.STL is 6.2 MB (127508 faces, 382524 vertices)
  • untitled.stl is 0.1 MB (1856 faces, 5568 vertices)
This translates to a difference in the number of faces and vertices in your files, ultimately meaning the grid in untilted.stl is much coarser than in terrain.STL. You could try resampling you mesh, but you would already be working with reduced resolution data. Is there an way to increase the number of vertices and faces when generating untitled.stl?

Community Treasure Hunt

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

Start Hunting!

Translated by