MATLAB Answers

Union of polyshapes ignoring empty ones

6 views (last 30 days)
Hi all,
I am using Matlab to measure the area of the union of multiple polyshapes. I am encountering issues when including polyshapes that are empty, meaning that the XY coordinates of the polyshapes are all equal to 0. The reason why I want to keep these empty polyshapes is that I will use this code structure to read coordinates from an Excel file in which, for some polyshapes, coordinates will be equal to 0.
I would therefore like the union to ignore those empty polyshapes and report the area of the union of the remaining polyshapes. Please see the example below which is giving me problems:
pol1=polyshape([0 0 0 0],[0 0 0 0])
pol2=polyshape([0 0 0 0],[0 0 0 0])
pol3=polyshape([-442 -442 1742 1742],[-266.5 1917.5 1917.5 -266.5])
pol4=polyshape([-992 -992 1192 1192],[-992.5 1191.5 1191.5 -992.5])
pol5=polyshape([-717 -717 1467 1467],[-992.5 1191.5 1191.5 -992.5])
pol6=polyshape([-442 -442 1742 1742],[-992.5 1191.5 1191.5 -992.5])
poly1=[pol1 pol2 pol3 pol4 pol5 pol6]
polyout=union(poly1)
area_polyout=area(polyout)
The above reports the following result:
area_polyout =
0
If I delete pol1 and pol2 from the script, I obtain the following:
area_polyout =
7556640
Which is what I'm after.
Would anybody know how to implement an IF statement or something similar to ignore polyshapes when empty from the union?
Many thanks,
Emilio

  0 Comments

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 18 May 2020
When given an array of polyshape objects, area returns an array of the same size. This means you can use it for logical indexing.
A = area(poly1);
U = union(poly1(A > 0))
area(U)
You could encapsulate the union-skipping-empties step into your own function.
function outputPolyshape = unionSkippingEmpties(inputPolyshapes)
A = area(inputPolyshapes);
outputPolyshape = union(inputPolyshapes(A > 0));
end
You could even have this accept a threshold for what counts as an "empty" / "trivial" polyshape that should be excluded from the union.

  1 Comment

Emilio Bolanos
Emilio Bolanos on 18 May 2020
That is precisely what I was looking for. Thanks very much, Steven.

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!