stl to grid error
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’;
endI 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 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 stl, grid, error, export MATLAB Answers — New Questions