polybool function works wrong
8 views (last 30 days)
Show older comments
Dmitry on 3 May 2015
Commented: Kelly Kearney on 6 May 2015
Looks like the polybool function can give completely wrong results in some cases. The simplest example is the following:
p1x = [-0.38638747761632497000 0.19712615255652799000 0.11632212129588278000];
p1y = [-0.19990841690264510000 0.44255449769997601000 -0.32295394513363201000];
p2x = [-0.63774227707713371000 0.09164976063543484000 0.61903172020561337000];
p2y = [-0.13838565278600026000 0.66469299048631436000 -0.44599947336401247000];
[p3x p3y] = polybool('-', p1x,p1y, p2x, p2y);
plot(p1x([1:end 1]), p1y([1:end 1]), 'o-black')
plot(p2x([1:end 1]), p2y([1:end 1]), '.-g')
plot(p3x([1:end 1]), p3y([1:end 1]), '*-r')
Here the subtranction should give empty polygon, while it gives something strange (see red polygon)...
So the question is: are there any alternatives for polybool function for subtracting and merging polygons?
Kelly Kearney on 5 May 2015
Edited: Kelly Kearney on 5 May 2015
I see the correct empty result in R2015a, but the same not-quite-right results that I assume the original poster was seeing when I try the example in R2013a.
So if you have the option of updating to R2015a, try that first. Otherwise, you might try looking at these options on the FEX: Polygon Clipper (which uses the same underlying GPC library as polybool, or Polygon Clipping and Offsetting, which uses a different mexed library.
Nalini Vishnoi on 5 May 2015
Edited: Nalini Vishnoi on 5 May 2015
Which version of MATLAB are you currently using? I tried the sample code in MATLAB R2013b (and later versions) and it returned p3x and p3y as empty arrays (which is the expected answer). However, it looks like there was some issue with versions between R2009b and R2013a and the sample code returns unexpected results of polybool subtraction in these versions. Again, the versions before R2009b (R2009a and earlier) return the correct expected result.
Please try the sample code on a recent version of MATLAB and confirm this.
Dmitry on 6 May 2015
Kelly Kearney on 6 May 2015
Interesting. The problem definitely seems to be due to the duplicated point. My license for 2012a isn't valid anymore, so I can't test that, but the code for polybool.m doesn't appear to have changed at all between the two versions, nor does any of the code in the setup subfunctions ( checkxy.m, vectorsToGPC.m, etc). However, the gpcmex file itself seems significantly smaller in 2012a than in 2015a, so something must have changed under the hood there.
Are these examples you're posting typical ones for your application? Because they seem to be pushing the limits of polygon clipping algorithms. Perhaps adding filters to check for and remove unnecessary duplicated vertices will help. If these are typical input data, though, you might want to rethink your overall algorithm.
Find more on Colormaps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!