Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

returned cap from CapBound for Polyline is too big #38

Closed
djadala opened this issue Apr 12, 2018 · 4 comments
Closed

returned cap from CapBound for Polyline is too big #38

djadala opened this issue Apr 12, 2018 · 4 comments

Comments

@djadala
Copy link

djadala commented Apr 12, 2018

Hi,
i have case that performance using CapBound is in times slower compared to suggested replacement.

func (p *Polyline) CapBound() Cap {

Please change from

// CapBound returns the bounding Cap for this Polyline.
func (p *Polyline) CapBound() Cap {
	return p.RectBound().CapBound()
}

to

// CapBound returns the bounding Cap for this Polyline.
func (p *Polyline) CapBound() Cap {
	c := s2.EmptyCap()
	for _, v := range *p {
		c.AddPoint(v)
	}
	return c
}
@dsymonds
Copy link
Contributor

/cc @rsned

@djadala
Copy link
Author

djadala commented Apr 12, 2018

While truing to write small reproducer, i discover that when i use built-in CapBound, there is zero point, erroneously inserted at start of polyline , and that is reason for huge cap differences.
Now there is no more ' times slower' performance, but suggested change still return smaller caps, and as i understand, computational complexity is the same.

@djadala
Copy link
Author

djadala commented Apr 13, 2018

Here is benchmark, proposed change is faster!(i don't expect such difference):

goos: linux
goarch: amd64
pkg: jambo/tests/geos2
BenchmarkCap1-4   	   20000	     61406 ns/op
BenchmarkCap2-4   	 1000000	      1289 ns/op
package main

import (
	"math/rand"
	"testing"

	"github.com/golang/geo/s2"
)

///////////////////////////////////atoi

const N = 200

var polyline s2.Polyline

func init() {
	const s = 100000
	for i := 0; i < N; i++ {
		t := float64(rand.Int63n(90*s)) / s
		g := float64(rand.Int63n(180*s)) / s
		polyline = append(polyline, s2.PointFromLatLng(s2.LatLngFromDegrees(t, g)))
	}
}

func BenchmarkCap1(b *testing.B) {

	for i := 0; i < b.N; i++ {
		_ = polyline.CapBound()
	}

}

func BenchmarkCap2(b *testing.B) {

	for i := 0; i < b.N; i++ {
		_ = CapBound(polyline)
	}

}

func CapBound(p s2.Polyline) s2.Cap {
	c := s2.EmptyCap()
	for _, v := range p {
		c.AddPoint(v)
	}
	return c
}

@dsymonds
Copy link
Contributor

The C++ code uses code like

S2Cap S2Polyline::GetCapBound() const {
  return GetRectBound().GetCapBound();
}

It is unlikely we will diverge from C++ algorithmically.

But note that I believe your code may be incorrect for large polylines. If an edge of the polyline is long enough, adding its endpoints to a cap progressively may end up with a cap that doesn't include the line in its entirety.

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

No branches or pull requests

2 participants