Skip to content

Commit fbbe50e

Browse files
committed
Merge branch 'benchmark'
2 parents 691ac06 + b94bf5b commit fbbe50e

36 files changed

Lines changed: 364624 additions & 288 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
notes.txt
2+
test/debug/
3+
debug.go
4+
debug_test.go

benchmark_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package polygol
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func BenchmarkAsiaUnion(b *testing.B) {
8+
geoms, err := loadGeoms("test/end-to-end/countries-asia/args.geojson")
9+
if err != nil {
10+
b.Fatal(err)
11+
}
12+
_, err = Union(Geom{}, geoms...)
13+
if err != nil {
14+
b.Fatal(err)
15+
}
16+
}
17+
18+
func BenchmarkAfricaUnion(b *testing.B) {
19+
geoms, err := loadGeoms("test/end-to-end/countries-africa/args.geojson")
20+
if err != nil {
21+
b.Fatal(err)
22+
}
23+
_, err = Union(Geom{}, geoms...)
24+
if err != nil {
25+
b.Fatal(err)
26+
}
27+
}
28+
29+
func BenchmarkEuropeUnion(b *testing.B) {
30+
geoms, err := loadGeoms("test/end-to-end/countries-europe/args.geojson")
31+
if err != nil {
32+
b.Fatal(err)
33+
}
34+
_, err = Union(Geom{}, geoms...)
35+
if err != nil {
36+
b.Fatal(err)
37+
}
38+
}
39+
40+
func BenchmarkNorthAmericaUnion(b *testing.B) {
41+
geoms, err := loadGeoms("test/end-to-end/countries-north-america/args.geojson")
42+
if err != nil {
43+
b.Fatal(err)
44+
}
45+
_, err = Union(Geom{}, geoms...)
46+
if err != nil {
47+
b.Fatal(err)
48+
}
49+
}
50+
51+
func BenchmarkSouthAmericaUnion(b *testing.B) {
52+
geoms, err := loadGeoms("test/end-to-end/countries-south-america/args.geojson")
53+
if err != nil {
54+
b.Fatal(err)
55+
}
56+
_, err = Union(Geom{}, geoms...)
57+
if err != nil {
58+
b.Fatal(err)
59+
}
60+
}

end-to-end_test.go

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"strings"
1111
"testing"
1212

13-
"github.com/engelsjk/polygol/geojson"
13+
geojson "github.com/engelsjk/polygol/geojson"
1414
)
1515

1616
const (
@@ -27,7 +27,7 @@ var (
2727
opsSkip = []string{}
2828
)
2929

30-
type TestCase struct {
30+
type testCase struct {
3131
Name string
3232
OperationType string
3333
ResultPath string
@@ -52,10 +52,9 @@ func TestEndToEnd(t *testing.T) {
5252
}
5353

5454
targetDir := path.Join(endToEndDir, target.Name())
55-
5655
argsPath := path.Join(targetDir, "args.geojson")
5756

58-
args, err := loadGeoms(argsPath, false)
57+
args, err := loadGeoms(argsPath)
5958
if err != nil {
6059
t.Fatal(err)
6160
}
@@ -65,7 +64,7 @@ func TestEndToEnd(t *testing.T) {
6564
log.Fatal(err)
6665
}
6766

68-
testCases := []TestCase{}
67+
testCases := []testCase{}
6968

7069
for _, f := range files {
7170
if f.Name() == "args.geojson" {
@@ -79,13 +78,13 @@ func TestEndToEnd(t *testing.T) {
7978
opType := strings.TrimSuffix(fn, ext)
8079
fp := filepath.Join(targetDir, fn)
8180
if opType != "all" {
82-
testCases = append(testCases, TestCase{
83-
Name: fmt.Sprintf("%s-all", target.Name()),
81+
testCases = append(testCases, testCase{
82+
Name: fmt.Sprintf("%s-%s", target.Name(), opType),
8483
OperationType: opType,
8584
ResultPath: fp,
8685
})
8786
} else {
88-
testCases = []TestCase{
87+
testCases = []testCase{
8988
{
9089
Name: fmt.Sprintf("%s-union", target.Name()),
9190
OperationType: "union",
@@ -97,7 +96,7 @@ func TestEndToEnd(t *testing.T) {
9796
ResultPath: fp,
9897
},
9998
{
100-
Name: fmt.Sprintf("%s-all", target.Name()),
99+
Name: fmt.Sprintf("%s-xor", target.Name()),
101100
OperationType: "xor",
102101
ResultPath: fp,
103102
},
@@ -119,7 +118,8 @@ func TestEndToEnd(t *testing.T) {
119118
if contains(opsSkip, testCase.OperationType) {
120119
fmt.Printf("skipping op type %s...\n", testCase.OperationType)
121120
}
122-
geoms, err := loadGeoms(testCase.ResultPath, true)
121+
122+
geoms, err := loadGeoms(testCase.ResultPath)
123123
if err != nil {
124124
t.Fatal(err)
125125
}
@@ -130,6 +130,7 @@ func TestEndToEnd(t *testing.T) {
130130
if err != nil {
131131
t.Error(err)
132132
}
133+
133134
expect(t, equalMultiPoly(expected, result))
134135
})
135136
}
@@ -145,8 +146,9 @@ func contains(s []string, str string) bool {
145146
return false
146147
}
147148

148-
func loadGeoms(filepath string, singleFeature bool) ([]Geom, error) {
149+
func loadGeoms(filepath string) ([]Geom, error) {
149150

151+
fmt.Println(filepath)
150152
f, err := os.Open(filepath)
151153
if err != nil {
152154
return nil, err
@@ -158,25 +160,11 @@ func loadGeoms(filepath string, singleFeature bool) ([]Geom, error) {
158160
return nil, err
159161
}
160162

161-
var features []*geojson.Feature
162-
163-
if singleFeature {
164-
f, err := geojson.UnmarshalFeature(b)
165-
if err != nil {
166-
return nil, err
167-
}
168-
features = append(features, f)
169-
} else {
170-
fc, err := geojson.UnmarshalFeatureCollection(b)
171-
if err != nil {
172-
return nil, err
173-
}
174-
features = append(features, fc.Features...)
175-
}
163+
newFeatures := unmarshalFeatureOrFeatureCollection(b)
176164

177-
geoms := make([]Geom, len(features))
178-
for i := range features {
179-
fg := features[i].Geometry
165+
geoms := make([]Geom, len(newFeatures))
166+
for i := range newFeatures {
167+
fg := newFeatures[i].Geometry
180168
switch fg.Type {
181169
case "Polygon":
182170
geoms[i] = Geom{fg.Polygon}
@@ -189,3 +177,18 @@ func loadGeoms(filepath string, singleFeature bool) ([]Geom, error) {
189177

190178
return geoms, nil
191179
}
180+
181+
func unmarshalFeatureOrFeatureCollection(b []byte) []*geojson.Feature {
182+
feature, err := geojson.UnmarshalFeature(b)
183+
if err != nil {
184+
return nil
185+
}
186+
if feature.Type != "FeatureCollection" {
187+
return []*geojson.Feature{feature}
188+
}
189+
fc, err := geojson.UnmarshalFeatureCollection(b)
190+
if err != nil {
191+
return nil
192+
}
193+
return fc.Features
194+
}

flp.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package polygol
33
import "math"
44

55
const (
6-
epsilon = float64(7.)/3 - float64(4.)/3 - float64(1.)
6+
// epsilon = float64(7.)/3 - float64(4.)/3 - float64(1.)
7+
epsilon = 2e-12
78
)
89

910
var (

flp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package polygol
22

33
import "testing"
44

5-
func TestCompare(t *testing.T) {
5+
func TestFlpCompare(t *testing.T) {
66
var a, b float64
77

88
// exactly equal

geojson/geojson.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,12 @@ func decodePolygonSet(data interface{}) ([][][][]float64, error) {
116116
}
117117

118118
type Feature struct {
119+
Type string `json:"type"`
119120
Geometry *Geometry `json:"geometry"`
120121
}
121122

122123
type FeatureCollection struct {
124+
Type string `json:"type"`
123125
Features []*Feature `json:"features"`
124126
}
125127

geom-in_test.go

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"testing"
55
)
66

7-
func TestRingIn(t *testing.T) {
7+
func TestGeomInRingIn(t *testing.T) {
88
var ringIn *ringIn
99
var err error
1010
var ring [][]float64
@@ -44,7 +44,7 @@ func TestRingIn(t *testing.T) {
4444
expect(t, !ringIn.isExterior)
4545
}
4646

47-
func TestPolyIn(t *testing.T) {
47+
func TestGeomInPolyIn(t *testing.T) {
4848

4949
op := newOperation("")
5050

@@ -66,7 +66,7 @@ func TestPolyIn(t *testing.T) {
6666
expect(t, len(polyIn.getSweepEvents()) == 22)
6767
}
6868

69-
func TestmultiPolyIn(t *testing.T) {
69+
func TestGeomInMultiPolyIn(t *testing.T) {
7070

7171
var multiPolyIn *multiPolyIn
7272
var err error
@@ -119,3 +119,54 @@ func TestmultiPolyIn(t *testing.T) {
119119
}, false)
120120
expect(t, err != nil)
121121
}
122+
123+
func TestGeomInRingInIndexOf(t *testing.T) {
124+
r1 := &ringIn{}
125+
r2 := &ringIn{}
126+
r3 := &ringIn{}
127+
r4 := &ringIn{}
128+
r5 := &ringIn{}
129+
r6 := &ringIn{}
130+
131+
ringIns := []*ringIn{r1, r2, r3, r4, r5}
132+
expect(t, r1.indexOf(ringIns) == 0)
133+
expect(t, r2.indexOf(ringIns) == 1)
134+
expect(t, r3.indexOf(ringIns) == 2)
135+
expect(t, r4.indexOf(ringIns) == 3)
136+
expect(t, r5.indexOf(ringIns) == 4)
137+
expect(t, r6.indexOf(ringIns) == -1)
138+
}
139+
140+
func TestGeomInPolyInIndexOf(t *testing.T) {
141+
p1 := &polyIn{}
142+
p2 := &polyIn{}
143+
p3 := &polyIn{}
144+
p4 := &polyIn{}
145+
p5 := &polyIn{}
146+
p6 := &polyIn{}
147+
148+
polyIns := []*polyIn{p1, p2, p3, p4, p5}
149+
expect(t, p1.indexOf(polyIns) == 0)
150+
expect(t, p2.indexOf(polyIns) == 1)
151+
expect(t, p3.indexOf(polyIns) == 2)
152+
expect(t, p4.indexOf(polyIns) == 3)
153+
expect(t, p5.indexOf(polyIns) == 4)
154+
expect(t, p6.indexOf(polyIns) == -1)
155+
}
156+
157+
func TestGeomInMultiPolyInIndexOf(t *testing.T) {
158+
mp1 := &multiPolyIn{}
159+
mp2 := &multiPolyIn{}
160+
mp3 := &multiPolyIn{}
161+
mp4 := &multiPolyIn{}
162+
mp5 := &multiPolyIn{}
163+
mp6 := &multiPolyIn{}
164+
165+
multiPolyIns := []*multiPolyIn{mp1, mp2, mp3, mp4, mp5}
166+
expect(t, mp1.indexOf(multiPolyIns) == 0)
167+
expect(t, mp2.indexOf(multiPolyIns) == 1)
168+
expect(t, mp3.indexOf(multiPolyIns) == 2)
169+
expect(t, mp4.indexOf(multiPolyIns) == 3)
170+
expect(t, mp5.indexOf(multiPolyIns) == 4)
171+
expect(t, mp6.indexOf(multiPolyIns) == -1)
172+
}

geom-out.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ func newRingOutFromSegments(allSegments []*segment) ([]*ringOut, error) {
3838
ringsOut := []*ringOut{}
3939

4040
for i := 0; i < len(allSegments); i++ {
41+
4142
segment := allSegments[i]
43+
4244
if !segment.isInResult() || segment.ringOut != nil {
4345
continue
4446
}
@@ -347,5 +349,3 @@ func (mpo *multiPolyOut) composePolys(rings []*ringOut) []*polyOut {
347349
}
348350
return polys
349351
}
350-
351-
//////////////

0 commit comments

Comments
 (0)