-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial start of Benty Ottmann algorithm
- Loading branch information
1 parent
8df530d
commit 8eb48b1
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
}) | ||
} | ||
} |