Page MenuHome

BLI_math: optimize fill_poly_v2i_n
ClosedPublic

Authored by Campbell Barton (campbellbarton) on Aug 24 2016, 2:02 PM.

Details

Summary

This function is fine for basic polygons but doesn't scale well since its checking all coordinates for every y-pixel.

Heres an optimized version. Basic logic remains the same this just maintains an ordered list of intersections,
tracking in-out points, to avoid re-computing every row, this means sorting is only done when out of order segments are found and once sorted, the segments only need to be re-ordered when the cross each other.

It also makes a minor change, rounding the value when converting to an int,
so the result doesn't change for positive/negative polygons.

Using the lasso tool is a quick way to see its working.
I have some tests here but not in C/C++, ideally this patch would include gtests too.

Simple timing test shows 7.5x speedup (11x if the call to round is removed) for 10k points over a 2560x1580 region.

Also, the function is getting a bit involved and IMHO is outside the scope of BLI_math,
would suggest to move this and to a different module (BLI_raster ?), along with plot_line_v2v2i.


Note, one weakness in this method is that newly intersected nodes are added to the end of the array and need to be re-sorted.
For more efficient handling of dense polygons (100 - 1000+ intersections at once on the Y axis), it might be better to use a b-tree however that would make the patch a lot more involved.
Nevertheless, it improves over sorting every time.

Diff Detail

Repository
rB Blender
Branch
arcpatch-D2173
Build Status
Buildable 127
Build 127: arc lint + arc unit

Event Timeline

The legend returns :)
tested on MSVC 2015, error below

source/blender/blenlib/intern/math_geom.c
3038

Couldn't find definition compiling on windows
Maybe BLI_qsort_r or something to do with GLIBC?

Allocate number of coords + 1 for total possible intersections (matching previous code) & use BLI_qsort_r

The case this would be needed likely involves some degenerate data, since previous code was well tested I rather not risk introducing off-by-one allocation.

This revision is now accepted and ready to land.Oct 26 2016, 10:23 AM