-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernels_test.go
More file actions
222 lines (203 loc) · 6.39 KB
/
kernels_test.go
File metadata and controls
222 lines (203 loc) · 6.39 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package kernels
import (
"testing"
"github.com/Post-Math/Ncode-SDK-for-Linux/pattern"
)
// dotsAt returns every (x, y) where bm has pixel value 0 (dot).
func dotsAt(bm pattern.Bitmap) [][2]int {
var out [][2]int
for y := 0; y < bm.Height; y++ {
for x := 0; x < bm.Width; x++ {
if bm.PixelAt(x, y) == 0 {
out = append(out, [2]int{x, y})
}
}
}
return out
}
// makeBitmapWithDots returns a w×h blank bitmap with the given (x, y)
// positions set to dot.
func makeBitmapWithDots(w, h int, dots [][2]int) pattern.Bitmap {
bm := pattern.NewBlankBitmap(w, h, 600)
for _, p := range dots {
bm.SetPixel(p[0], p[1], 0)
}
return bm
}
func sameSet(a, b [][2]int) bool {
if len(a) != len(b) {
return false
}
seen := map[[2]int]bool{}
for _, p := range a {
seen[p] = true
}
for _, p := range b {
if !seen[p] {
return false
}
}
return true
}
func TestApplyKernelTri3Up(t *testing.T) {
src := makeBitmapWithDots(20, 20, [][2]int{{10, 10}})
got := dotsAt(ApplyKernel(src, Tri3Up))
want := [][2]int{{10, 10}, {9, 11}, {11, 11}}
if !sameSet(got, want) {
t.Errorf("Tri3Up dots = %v, want %v", got, want)
}
}
func TestApplyKernelTri3Down(t *testing.T) {
src := makeBitmapWithDots(20, 20, [][2]int{{10, 10}})
got := dotsAt(ApplyKernel(src, Tri3Down))
want := [][2]int{{10, 10}, {9, 9}, {11, 9}}
if !sameSet(got, want) {
t.Errorf("Tri3Down dots = %v, want %v", got, want)
}
}
func TestApplyKernelDiamond4(t *testing.T) {
src := makeBitmapWithDots(20, 20, [][2]int{{10, 10}})
got := dotsAt(ApplyKernel(src, Diamond4))
want := [][2]int{{10, 9}, {9, 10}, {11, 10}, {10, 11}}
if !sameSet(got, want) {
t.Errorf("Diamond4 dots = %v, want %v", got, want)
}
}
// Every production kernel must keep its footprint within ±1 pixel of
// the original SDK nobold pixel, so the dilated cluster stays inside
// the same Anoto cell (~9 px pitch at 600 DPI). Without this property
// the pen-IR camera could read the cluster as belonging to the
// neighbouring Ncode coordinate.
func TestKernelFootprintWithinOneCell(t *testing.T) {
all := map[string]Kernel{
"Tri3Up": Tri3Up,
"Tri3Down": Tri3Down,
"Tri4Up": Tri4Up,
"Tri4Down": Tri4Down,
"Diamond4": Diamond4,
}
for name, k := range all {
for _, off := range k {
if off[0] < -1 || off[0] > 1 || off[1] < -1 || off[1] > 1 {
t.Errorf("%s offset %v outside ±1 box", name, off)
}
}
}
}
// The Tri3Up/Tri3Down alternating pair and Diamond4 (alone) have the
// stronger centroid-on-origin property: the average pixel position
// equals the original SDK pixel exactly. Tri4Up/Tri4Down do NOT — their
// pair centroid is offset by (0, 0.5) — so they are not asserted here.
func TestExactCentroidInvariance(t *testing.T) {
type pair struct {
name string
k Kernel
m Kernel // mirror; nil for kernels asserted alone
}
cases := []pair{
{name: "Tri3 alternating", k: Tri3Up, m: Tri3Down},
{name: "Diamond4", k: Diamond4},
}
for _, c := range cases {
var sx, sy float64
all := append([][2]int{}, c.k...)
all = append(all, c.m...)
for _, off := range all {
sx += float64(off[0])
sy += float64(off[1])
}
n := float64(len(all))
if sx/n != 0 || sy/n != 0 {
t.Errorf("%s centroid = (%g, %g), want (0, 0)", c.name, sx/n, sy/n)
}
}
}
func TestApplyAlternatingFlipsByOrder(t *testing.T) {
// Two dots in row-major order: first uses Tri3Up, second uses Tri3Down.
src := makeBitmapWithDots(40, 40, [][2]int{{10, 10}, {20, 20}})
out := ApplyAlternating(src, Tri3Up, Tri3Down)
got := dotsAt(out)
want := [][2]int{
{10, 10}, {9, 11}, {11, 11}, // Tri3Up at (10,10)
{20, 20}, {19, 19}, {21, 19}, // Tri3Down at (20,20)
}
if !sameSet(got, want) {
t.Errorf("alternating dots = %v, want %v", got, want)
}
}
func TestApplyAlternatingEmptyKernelFallsBack(t *testing.T) {
src := makeBitmapWithDots(20, 20, [][2]int{{5, 5}})
out := ApplyAlternating(src, Tri3Up, nil)
got := dotsAt(out)
want := [][2]int{{5, 5}, {4, 6}, {6, 6}}
if !sameSet(got, want) {
t.Errorf("fallback to ApplyKernel(Tri3Up): got %v, want %v", got, want)
}
}
func TestApplyKernelEmptyIsCopy(t *testing.T) {
src := makeBitmapWithDots(8, 8, [][2]int{{3, 3}, {5, 5}})
out := ApplyKernel(src, nil)
if !sameSet(dotsAt(out), [][2]int{{3, 3}, {5, 5}}) {
t.Error("ApplyKernel(nil) must be an identity copy")
}
// Mutate src; out must not change.
src.SetPixel(7, 7, 0)
if out.PixelAt(7, 7) == 0 {
t.Error("ApplyKernel(nil) returned shared backing storage")
}
}
func TestApplySquareReproducesIsBold(t *testing.T) {
// SDK isBold=true emits a 2×2 cluster anchored at the original pixel.
// ApplySquare(2) must reproduce that shape.
src := makeBitmapWithDots(10, 10, [][2]int{{4, 4}})
got := dotsAt(ApplySquare(src, 2))
want := [][2]int{{4, 4}, {5, 4}, {4, 5}, {5, 5}}
if !sameSet(got, want) {
t.Errorf("ApplySquare(2) = %v, want %v", got, want)
}
}
func TestApplySquareNoOp(t *testing.T) {
src := makeBitmapWithDots(10, 10, [][2]int{{4, 4}})
out := ApplySquare(src, 1)
if !sameSet(dotsAt(out), [][2]int{{4, 4}}) {
t.Error("ApplySquare(1) must be a no-op copy")
}
}
func TestApplyDiscR1(t *testing.T) {
src := makeBitmapWithDots(10, 10, [][2]int{{5, 5}})
got := dotsAt(ApplyDisc(src, 1))
// r=1 is a 5-pixel plus sign: center + 4 cardinals.
want := [][2]int{{5, 5}, {4, 5}, {6, 5}, {5, 4}, {5, 6}}
if !sameSet(got, want) {
t.Errorf("ApplyDisc(1) = %v, want %v", got, want)
}
}
func TestApplyKernelDropsOutOfBounds(t *testing.T) {
src := makeBitmapWithDots(5, 5, [][2]int{{0, 0}})
out := ApplyKernel(src, Diamond4)
got := dotsAt(out)
// (0,-1) and (-1,0) are out of bounds and must be dropped.
want := [][2]int{{1, 0}, {0, 1}}
if !sameSet(got, want) {
t.Errorf("OOB-drop result = %v, want %v", got, want)
}
}
func TestReduceClusterTopLeftsBoldCluster(t *testing.T) {
// SDK isBold=true: a 2×2 cluster at (10,10).
dots := [][2]int{{10, 10}, {11, 10}, {10, 11}, {11, 11}}
src := makeBitmapWithDots(20, 20, dots)
out := ReduceClusterTopLefts(src)
got := dotsAt(out)
want := [][2]int{{10, 10}}
if !sameSet(got, want) {
t.Errorf("ReduceClusterTopLefts on 2×2 cluster = %v, want %v", got, want)
}
}
func TestReduceClusterTopLeftsNoboldIsIdentity(t *testing.T) {
// Singleton (nobold) dots, well-separated. All survive.
src := makeBitmapWithDots(40, 40, [][2]int{{5, 5}, {15, 5}, {25, 25}})
out := ReduceClusterTopLefts(src)
if !sameSet(dotsAt(out), [][2]int{{5, 5}, {15, 5}, {25, 25}}) {
t.Error("ReduceClusterTopLefts on isolated singletons must be identity")
}
}