-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy patharound_bound_test.go
105 lines (99 loc) · 3.46 KB
/
around_bound_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package smartclip
import (
"reflect"
"testing"
"github.com/paulmach/orb"
)
func TestNexts(t *testing.T) {
for i, next := range nexts[orb.CW] {
if next == -1 {
continue
}
if i != nexts[orb.CCW][next] {
t.Errorf("incorrect %d: %d != %d", i, i, nexts[orb.CCW][next])
}
}
}
func TestAroundBound(t *testing.T) {
cases := []struct {
name string
box orb.Bound
input orb.Ring
output orb.Ring
expected orb.Orientation
}{
{
name: "simple ccw",
box: orb.Bound{Min: orb.Point{-1, -1}, Max: orb.Point{1, 1}},
input: orb.Ring{{-1, -1}, {1, 1}},
output: orb.Ring{{-1, -1}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}},
expected: orb.CCW,
},
{
name: "simple cw",
box: orb.Bound{Min: orb.Point{-1, -1}, Max: orb.Point{1, 1}},
input: orb.Ring{{-1, -1}, {1, 1}},
output: orb.Ring{{-1, -1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}},
expected: orb.CW,
},
{
name: "wrap edge around whole box ccw",
box: orb.Bound{Min: orb.Point{1, 1}, Max: orb.Point{6, 6}},
input: orb.Ring{{1, 3}, {1, 2}},
output: orb.Ring{{1, 3}, {1, 2}, {1, 1}, {3.5, 1}, {6, 1}, {6, 3.5}, {6, 6}, {3.5, 6}, {1, 6}, {1, 3}},
expected: orb.CCW,
},
{
name: "wrap around whole box ccw",
box: orb.Bound{Min: orb.Point{-1, -1}, Max: orb.Point{1, 1}},
input: orb.Ring{{-1, 0.5}, {0, 0.5}, {0, -0.5}, {-1, -0.5}},
output: orb.Ring{{-1, 0.5}, {0, 0.5}, {0, -0.5}, {-1, -0.5}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0.5}},
expected: orb.CCW,
},
{
name: "wrap around whole box cw",
box: orb.Bound{Min: orb.Point{-1, -1}, Max: orb.Point{1, 1}},
input: orb.Ring{{-1, -0.5}, {0, -0.5}, {0, 0.5}, {-1, 0.5}},
output: orb.Ring{{-1, -0.5}, {0, -0.5}, {0, 0.5}, {-1, 0.5}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, -0.5}},
expected: orb.CW,
},
{
name: "already cw with endpoints in same section",
box: orb.Bound{Min: orb.Point{-1, -1}, Max: orb.Point{1, 1}},
input: orb.Ring{{-1, 0.5}, {0, 0.5}, {0, -0.5}, {-1, -0.5}},
output: orb.Ring{{-1, 0.5}, {0, 0.5}, {0, -0.5}, {-1, -0.5}, {-1, 0.5}},
expected: orb.CW,
},
{
name: "cw but want ccw with endpoints in same section",
box: orb.Bound{Min: orb.Point{-1, -1}, Max: orb.Point{1, 1}},
input: orb.Ring{{-1, 0.5}, {0, 0.5}, {0, -0.5}, {-1, -0.5}},
output: orb.Ring{{-1, 0.5}, {0, 0.5}, {0, -0.5}, {-1, -0.5}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0.5}},
expected: orb.CCW,
},
{
name: "one point on edge ccw",
box: orb.Bound{Min: orb.Point{-1, -1}, Max: orb.Point{1, 1}},
input: orb.Ring{{-1, 0.0}, {-0.5, -0.5}, {0, 0}, {-0.5, 0.5}, {-1, 0.0}},
output: orb.Ring{{-1, 0.0}, {-0.5, -0.5}, {0, 0}, {-0.5, 0.5}, {-1, 0.0}},
expected: orb.CCW,
},
{
name: "one point on edge cw",
box: orb.Bound{Min: orb.Point{-1, -1}, Max: orb.Point{1, 1}},
input: orb.Ring{{-1, 0.0}, {-0.5, -0.5}, {0, 0}, {-0.5, 0.5}, {-1, 0.0}},
output: orb.Ring{{-1, 0.0}, {-0.5, -0.5}, {0, 0}, {-0.5, 0.5}, {-1, 0.0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}},
expected: orb.CW,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
out := aroundBound(tc.box, tc.input, tc.expected)
if !reflect.DeepEqual(out, tc.output) {
t.Errorf("does not match")
t.Logf("%v", out)
t.Logf("%v", tc.output)
}
})
}
}