Skip to content

Commit

Permalink
Add initial start of Benty Ottmann algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
peterstace committed Aug 2, 2021
1 parent 8df530d commit 8eb48b1
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
51 changes: 51 additions & 0 deletions internal/exact/bently_ottmann.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package exact

import "sort"

type IntersectionReport struct {
Indexes [2]int
Dimension int // 0 or 1
}

func BentlyOttmann(
segments []Segment,
callback func(IntersectionReport) bool,
) {
var queue eventQueue
for range segments {
queue.push(event{})
}
// TODO
}

type event struct {
// TODO
}

func (e event) cmp(o event) int {
// TODO
return -1
}

// TODO: the event queue implemented here is inefficient and just a
// placeholder.
type eventQueue struct {
events []event
}

func (q *eventQueue) empty() bool {
return len(q.events) == 0
}

func (q *eventQueue) push(e event) {
q.events = append(q.events, e)
sort.Slice(q.events, func(i, j int) bool {
return q.events[i].cmp(q.events[j]) > 0
})
}

func (q *eventQueue) pop() event {
e := q.events[len(q.events)-1]
q.events = q.events[:len(q.events)-1]
return e
}
73 changes: 73 additions & 0 deletions internal/exact/bently_ottmann_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package exact_test

import (
"testing"

"github.com/peterstace/simplefeatures/internal/exact"
)

func TestBentlyOttmann(t *testing.T) {
for _, tc := range []struct {
description string
segments []exact.Segment
want []exact.IntersectionReport
}{
{
description: "no segments",
segments: nil,
want: nil,
},
{
description: "single segments",
segments: []exact.Segment{
{
A: exact.XY64{
X: 0,
Y: 0,
},
B: exact.XY64{
X: 1,
Y: 1,
},
},
},
want: nil,
},
} {
t.Run(tc.description, func(t *testing.T) {
var got []exact.IntersectionReport
exact.BentlyOttmann(
tc.segments,
func(ir exact.IntersectionReport) bool {
got = append(got, ir)
return true
},
)

show := func() {
t.Logf("want: len=%d", len(tc.want))
for i, w := range tc.want {
t.Logf(" w[%d]: %v", i, w)
}
t.Logf("got: len=%d", len(got))
for i, g := range got {
t.Logf(" g[%d]: %v", i, g)
}
}
if len(tc.want) != len(got) {
t.Fatal("length mismatch")
show()
}
for i := range tc.want {
var any bool
if tc.want[i] != got[i] {
t.Errorf("mismatch at %d", i)
any = true
}
if any {
show()
}
}
})
}
}

0 comments on commit 8eb48b1

Please sign in to comment.