The "union" function for polyshapes performs an incorrect consolidation of adjacent polyshapes when presented as a vector

I have a polyshape vector "pv" consisting of 4 adjacent triangles:
load tstcase_pv
plot(pv)
Why is it that when the polyshape vector elements are ordered one way, the "union" operation successfully consolidates them, whereas in the reverse order, it does not?
load tstcase_pv
pv1=pv([4,1,2,3]);
plot(union(pv1))
load tstcase_pv
pv1=pv([4,1,2,3]);
plot(union(pv1))
As an additional observation, I find that if the union is performed incrementally on two of the "pv(i)" at a time, using a for-loop, the problem does not manifest. Moreover, this is irrespective of the loop order.
load tstcase_pv
u=polyshape();
for i=randperm(4);
u=union(u,pv(i));
end
plot(u)

1 Comment

As an additional observation, I find that if the union is performed incrementally on two of the pv(i) at a time, using a for-loop, the problem does not manifest. Moreover, this is irrespective of the loop order.
load tstcase_pv
u=polyshape();
for i=randperm(4); u=union(u,pv(i)); end
plot(u)

Sign in to comment.

 Accepted Answer

A limitation has been discovered in the union operation for polyshapes. The development team has been notified about this issue and a fix for this issue may be implemented for a future release of MATLAB. Unfortunately, incremental use of the union operation via loops would be the suggested workaround until the fix is released as demonstrated in the code snippet shared:
load tstcase_pv
u=polyshape();
for i=randperm(4);
u=union(u,pv(i));
end
plot(u)

1 Comment

To follow up on this thread, as of R2025a the polyshape union function should produce the same result for the original order (4, 1, 2, 3) and the reverse (3, 2, 1, 4 - which I believe is what the third code snippet in the original question was going for?):
load tstcase_pv
pv1=pv([4,1,2,3]);
plot(union(pv1))
pv2=pv([3,2,1,4]);
plot(union(pv2))

Sign in to comment.

More Answers (1)

polyshapes contains oriented polygons. A polyshape with its vertices backwards is considered to be reverse direction.
This is important because multiple polyshapes together can describe "holes".
If you have two polyshapes with one insided the other, and the polyshapes are the same orientation, then the union of the two is the outer one. If the polyshapes are different orientation, then the union of the two is the area between the outer shape and the inner shape -- the inner shape will be a hole in the outer shape.

1 Comment

Not sure how that applies here. All of the polygons seem to have vertices in the same counter-clockwise orientation and none of the polygons is nested inside any of the others. I also don't see how it explains the order-dependence of the union() operation.
load tstcase_pv
for i=1:4
figure
hold on
plot(pv(i));
scatlabel( scatter(pv(i).Vertices(:,1), pv(i).Vertices(:,2)) );
hold off
end

Sign in to comment.

Categories

Products

Release

R2023b

Asked:

on 7 May 2024

Commented:

on 10 Jul 2025

Community Treasure Hunt

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

Start Hunting!