-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbox_dist_test.go
More file actions
152 lines (135 loc) · 4 KB
/
box_dist_test.go
File metadata and controls
152 lines (135 loc) · 4 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
package flatrtree
import (
"math"
"testing"
"github.com/stretchr/testify/require"
)
var (
inside = [2]float64{-73.649597, 45.51982}
north = [2]float64{-73.627625, 45.815401}
northEast = [2]float64{-72.951965, 45.823057}
east = [2]float64{-72.927246, 45.512121}
southEast = [2]float64{-72.946472, 45.154927}
south = [2]float64{-73.624878, 45.13168}
southWest = [2]float64{-74.382935, 45.182037}
west = [2]float64{-74.374695, 45.494796}
northWest = [2]float64{-74.344482, 45.811572}
bboxMin = [2]float64{-74.19342, 45.265222}
bboxMax = [2]float64{-73.157959, 45.704261}
epsilon = 1e-5
)
func haversine(a, b [2]float64) float64 {
return earthRadiusMeters * distRad(
a[1]*math.Pi/180, a[0]*math.Pi/180,
b[1]*math.Pi/180, b[0]*math.Pi/180,
)
}
func TestGeodeticBoxDistInside(t *testing.T) {
expected := 0.0
actual := GeodeticBoxDist(
inside[0], inside[1],
bboxMin[0], bboxMin[1],
bboxMax[0], bboxMax[1],
)
require.Equal(t, expected, actual)
}
func TestGeodeticBoxDistNorth(t *testing.T) {
expected := haversine(north, [2]float64{north[0], bboxMax[1]})
actual := GeodeticBoxDist(
north[0], north[1],
bboxMin[0], bboxMin[1],
bboxMax[0], bboxMax[1],
)
require.InEpsilon(t, expected, actual, epsilon)
}
func TestGeodeticBoxDistNorthEast(t *testing.T) {
expected := haversine(northEast, bboxMax)
actual := GeodeticBoxDist(
northEast[0], northEast[1],
bboxMin[0], bboxMin[1],
bboxMax[0], bboxMax[1],
)
require.InEpsilon(t, expected, actual, epsilon)
}
func TestGeodeticBoxDistEast(t *testing.T) {
expected := haversine(east, [2]float64{bboxMax[0], east[1]})
actual := GeodeticBoxDist(
east[0], east[1],
bboxMin[0], bboxMin[1],
bboxMax[0], bboxMax[1],
)
require.InEpsilon(t, expected, actual, epsilon)
}
func TestGeodeticBoxDistSouthEast(t *testing.T) {
expected := haversine(southEast, [2]float64{bboxMax[0], bboxMin[1]})
actual := GeodeticBoxDist(
southEast[0], southEast[1],
bboxMin[0], bboxMin[1],
bboxMax[0], bboxMax[1],
)
require.InEpsilon(t, expected, actual, epsilon)
}
func TestGeodeticBoxDistSouth(t *testing.T) {
expected := haversine(south, [2]float64{south[0], bboxMin[1]})
actual := GeodeticBoxDist(
south[0], south[1],
bboxMin[0], bboxMin[1],
bboxMax[0], bboxMax[1],
)
require.InEpsilon(t, expected, actual, epsilon)
}
func TestGeodeticBoxDistSouthWest(t *testing.T) {
expected := haversine(southWest, bboxMin)
actual := GeodeticBoxDist(
southWest[0], southWest[1],
bboxMin[0], bboxMin[1],
bboxMax[0], bboxMax[1],
)
require.InEpsilon(t, expected, actual, epsilon)
}
func TestGeodeticBoxDistWest(t *testing.T) {
expected := haversine(west, [2]float64{bboxMin[0], west[1]})
actual := GeodeticBoxDist(
west[0], west[1],
bboxMin[0], bboxMin[1],
bboxMax[0], bboxMax[1],
)
require.InEpsilon(t, expected, actual, epsilon)
}
func TestGeodeticBoxDistNorthWest(t *testing.T) {
expected := haversine(northWest, [2]float64{bboxMin[0], bboxMax[1]})
actual := GeodeticBoxDist(
northWest[0], northWest[1],
bboxMin[0], bboxMin[1],
bboxMax[0], bboxMax[1],
)
require.InEpsilon(t, expected, actual, epsilon)
}
func TestPlanarBoxDist(t *testing.T) {
var (
minX = 0.0
minY = 0.0
maxX = 2.0
maxY = 2.0
midY = (maxX - minX) / 2
midy = (maxY - minY) / 2
)
// inside
require.Equal(t, 0.0, PlanarBoxDist(midY, midy, minX, minY, maxX, maxY))
// top
require.Equal(t, 1.0, PlanarBoxDist(midY, maxY+1, minX, minY, maxX, maxY))
// top right
require.Equal(t, 2.0, PlanarBoxDist(maxX+1, maxY+1, minX, minY, maxX, maxY))
// right
require.Equal(t, 1.0, PlanarBoxDist(maxX+1, midy, minX, minY, maxX, maxY))
// bottom right
require.Equal(t, 2.0, PlanarBoxDist(maxX+1, minY-1, minX, minY, maxX, maxY))
// bottom
require.Equal(t, 1.0, PlanarBoxDist(midY, minY-1, minX, minY, maxX, maxY))
// bottom left
require.Equal(t, 2.0, PlanarBoxDist(minX-1, minY-1, minX, minY, maxX, maxY))
// left
require.Equal(t, 1.0, PlanarBoxDist(minX-1, midy, minX, minY, maxX, maxY))
// top right
require.Equal(t, 2.0, PlanarBoxDist(minX-1, maxY+1, minX, minY, maxX, maxY))
}