Skip to content

Search with equal priority in both z-order directions - #93

Merged
jfirebaugh merged 1 commit into
masterfrom
bidirectional
Jan 3, 2018
Merged

jfirebaugh merged 1 commit into
masterfrom
bidirectional

Conversation

@jfirebaugh

Copy link
Copy Markdown
Contributor

This gives a slight speedup for more complex polygons.

Before:

typical OSM building (15 vertices): x 644,598 ops/sec ±0.96% (89 runs sampled)
dude shape (94 vertices): x 46,829 ops/sec ±1.39% (96 runs sampled)
dude shape with holes (104 vertices): x 37,816 ops/sec ±1.20% (91 runs sampled)
complex OSM water (2523 vertices): x 588 ops/sec ±0.75% (91 runs sampled)

After:

typical OSM building (15 vertices): x 632,252 ops/sec ±1.60% (89 runs sampled)
dude shape (94 vertices): x 48,956 ops/sec ±1.45% (94 runs sampled)
dude shape with holes (104 vertices): x 39,665 ops/sec ±1.51% (95 runs sampled)
complex OSM water (2523 vertices): x 659 ops/sec ±0.75% (91 runs sampled)

Results in gl-js:

https://bl.ocks.org/anonymous/raw/c415471d58758dd81650dec97d35b2f8/

@jfirebaugh
jfirebaugh requested a review from mourner January 2, 2018 21:13
@jfirebaugh

Copy link
Copy Markdown
Contributor Author

I also tried searching in order of increasing z-distance from the current point -- i.e. advance only one of p and n in the main loop, whichever is currently closer in z to ear.z. But it benchmarked slower in all the variations that I tried.

@mourner mourner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!

Can we eliminate the “remainder” loops by changing the conditions in the main one?

@jfirebaugh

Copy link
Copy Markdown
Contributor Author

Adding additional branches to the loop seems to negate the performance improvement. E.g.:

    var p = ear.prevZ,
        n = ear.nextZ,
        hasPrev = p && p.z >= minZ,
        hasNext = n && n.z <= maxZ;

    // look for points inside the triangle in both directions
    while (hasPrev || hasNext) {
        if (hasPrev) {
            if (p !== ear.prev && p !== ear.next &&
                pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
                area(p.prev, p, p.next) >= 0) return false;
            p = p.prevZ;
            hasPrev = p && p.z >= minZ;
        }

        if (hasNext) {
            if (n !== ear.prev && n !== ear.next &&
                pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, n.x, n.y) &&
                area(n.prev, n, n.next) >= 0) return false;
            n = n.nextZ;
            hasNext = n && n.z <= maxZ;
        }
    }
typical OSM building (15 vertices): x 606,847 ops/sec ±1.16% (87 runs sampled)
dude shape (94 vertices): x 29,195 ops/sec ±1.19% (91 runs sampled)
dude shape with holes (104 vertices): x 23,432 ops/sec ±1.06% (92 runs sampled)
complex OSM water (2523 vertices): x 359 ops/sec ±0.72% (87 runs sampled)

@jfirebaugh
jfirebaugh merged commit 11d2d36 into master Jan 3, 2018
@jfirebaugh
jfirebaugh deleted the bidirectional branch January 3, 2018 22:03
@mourner

mourner commented Jan 3, 2018

Copy link
Copy Markdown
Member

Published as v2.1.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants