stlwrite error using reshape

조회 수: 3 (최근 30일)
shahaf wagner
shahaf wagner 2017년 2월 16일
편집: DGM 2025년 10월 8일
Hey im having issue with my code, getting the error:
Error using reshape Product of known dimensions, 9, not divisible into total number of elements, 28362.
Error in stlwrite (line 81) facets = reshape(facets(:,faces'), 3, 3, []);
I tried to change it from 3,3 so it whould work but it mess everything even more...
Here is my code:
clf
file = 'pattern3';
switch file
case 'pattern2'
P = imread('Pattern2.png');
P = repmat(P,3);
case 'pattern3'
P = imread('Pattern3.png');
P = P(400:3400, 400:3400);
P = 255 - P*6;
case 'pattern4'
P = imread('Pattern4.jpg');
case 'banksy'
P = imread('Banksy.png');
P = padarray(P, [500 500], 255);
end
P = 1-im2bw(P);
Nmin = min(size(P));
P = P(1:Nmin, 1:Nmin);
[xg, yg] = meshgrid(1:Nmin, 1:Nmin);
P((xg - Nmin/2).^2 + (yg - Nmin/2).^2 > 0.99*0.25*Nmin^2) = 0;
P = padarray(P, [1 1], 0);
CC = bwconncomp(P);
dtheta = pi/24;
theta = (-pi:dtheta:(pi-dtheta))';
nodeouter = [1.1*cos(theta) 1.1*sin(theta)];
Nnodes = length(nodeouter);
nodelist = (1:Nnodes)';
allnodes = nodeouter;
alledges = [nodelist , mod(nodelist, Nnodes)+1];
for n = 1:CC.NumObjects
%for n = 2:2
newP = zeros(size(P));
newP(CC.PixelIdxList{1,n}(:)) = 1;
newP = filter2(fspecial('average',5),newP);
C = contourc(newP,[0.2 0.2]);
C = C(:,2:end)';
C2 = dpsimplify(C,1);
m = 1;
while m <= length(C2(:,1))
if(C2(m,1) == 1 || C2(m,2) == 1)
C2(m,:) = [];
else
m = m + 1;
end
end
C2 = (C2 - Nmin/2)/(Nmin/2);
C = (C - Nmin/2)/(Nmin/2);
figure(1)
hold all
plot(C2(:,1), C2(:,2))
axis image xy
drawnow
nodeinner = C2;
Nnodeshole = length(nodeinner);
nodelist = (1:Nnodeshole)';
edgelist = [nodelist , mod(nodelist, Nnodeshole)+1];
edgelist = edgelist + Nnodes;
allnodes = [allnodes; nodeinner];
alledges = [alledges; edgelist];
Nnodes = Nnodes + Nnodeshole;
n
end
hdata.fun = @(x,y) 0.05*(1 + ((x.^2 + y.^2)/a^2)).^2;
[p,t] = refine2(allnodes, alledges);
as = 0.5;
for n = 1:length(as)
a = as(n);
h = 0;
x = p(:,1);
y = p(:,2);
z = zeros(size(x));
r = sqrt(x.^2 + y.^2);
phi = atan2(y,x);
theta = atan(r/(a+h));
alpha = 2*theta;
xnew = a*sin(alpha).*cos(phi);
ynew = a*sin(alpha).*sin(phi);
znew = -a*cos(alpha);
p2 = [xnew, ynew, znew];
stlwrite('Test.stl', t, p2)
fv.faces = t;
fv.vertices = p2;
clf
figure(3)
patch(fv, 'FaceColor', [1 1 1], 'EdgeColor', 'black', 'LineWidth', 0.1)
axis equal
axis off
xlim([-a a])
ylim([-a a])
zlim([-a a])
camlight head
view(58,28)
zoom(1.5)
drawnow
%print(gcf, [num2str(n) '.png'], '-dpng', '-r250')
end
  댓글 수: 1
KSSV
KSSV 2017년 2월 16일
Try to take number elements which are exactly divisible by 9.

댓글을 달려면 로그인하십시오.

답변 (1개)

DGM
DGM 2025년 10월 8일
편집: DGM 2025년 10월 8일
This problem happens because you're trying to write an STL file consisting of line segments, not triangles. Early versions of FEX #20922 (and others) will fail with a reshaping error if the mesh is not triangular.
The second output of refine2() is not a list of triangular faces; it's a 2-column edge list. You probably need the third argument instead.
[p,~,t] = refine2(allnodes, alledges);
That said, there are no comments and no input data, so I don't know whether that solves all the problems. Without knowing what it's supposed to do, I can't know if it's doing it correctly.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by