-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
143 lines (126 loc) · 3.69 KB
/
query.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
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
package healpix
// Given a desired coordinate on a healpix map, return the pixel index of
// of the desired neighbor pixel of in the selected HEALPix numbering scheme.
func Neighbor(hp Healpix, scheme HealpixScheme, where Where, xo int, yo int) int {
fp := where.ToFacePixel(hp)
maxXY := hp.FaceSidePixels() - 1
x := fp.x + xo
y := fp.y + yo
// these track whether the neighbor is in a different face, and which x/y direction the face is in
fxdir := 0
fydir := 0
if x < 0 {
fxdir = -1
x += hp.FaceSidePixels()
} else if x >= hp.FaceSidePixels() {
fxdir = 1
x -= hp.FaceSidePixels()
}
if y < 0 {
fydir = -1
y += hp.FaceSidePixels()
} else if y >= hp.FaceSidePixels() {
fydir = 1
y -= hp.FaceSidePixels()
}
if fxdir == 1 && fydir != -1 && fp.face < 4 {
x = y
y = maxXY
}
if fydir == 1 && fxdir != -1 && fp.face < 4 {
y = x
x = maxXY
}
if fydir == -1 && fxdir != 1 && fp.face > 7 {
y = x
x = 0
}
if fxdir == -1 && fydir != 1 && fp.face > 7 {
x = y
y = 0
}
face := NewFace(fp.face).Neighbor(fxdir, fydir)
return FacePixel{x, y, face}.PixelId(hp, scheme)
}
// Given a desired coordinate on a healpix map, return the pixel indices
// of each neighbor pixel of the selected coordinate in the HEALPix index
// scheme desired.
func Neighbors(hp Healpix, where Where, scheme HealpixScheme) []int {
fp := where.ToFacePixel(hp)
maxXY := hp.FaceSidePixels() - 1
var result []int
if fp.x > 0 && fp.x < maxXY && fp.y > 0 && fp.y < maxXY {
result = make([]int, 8)
// pixel not on face boundary
// highest probability branch in higher resolutions
// TODO: nested can be even faster here as a special case, doesn't have to go through FacePixel first
result[0] = FacePixel{fp.x - 1, fp.y - 1, fp.face}.PixelId(hp, scheme)
result[1] = FacePixel{fp.x, fp.y - 1, fp.face}.PixelId(hp, scheme)
result[2] = FacePixel{fp.x + 1, fp.y - 1, fp.face}.PixelId(hp, scheme)
result[3] = FacePixel{fp.x - 1, fp.y, fp.face}.PixelId(hp, scheme)
result[4] = FacePixel{fp.x + 1, fp.y, fp.face}.PixelId(hp, scheme)
result[5] = FacePixel{fp.x - 1, fp.y + 1, fp.face}.PixelId(hp, scheme)
result[6] = FacePixel{fp.x, fp.y + 1, fp.face}.PixelId(hp, scheme)
result[7] = FacePixel{fp.x + 1, fp.y + 1, fp.face}.PixelId(hp, scheme)
} else {
// pixel is on an edge boundary, we need to be cognizant of
// special corner pixels and edge index swapping
result = []int{}
iterLen := 9
// account for special pixels that only have 7 neighbors
if fp.face < 8 && fp.x == maxXY && fp.y == maxXY {
iterLen = 8
}
iterStart := 0
if fp.face > 3 && fp.x == 0 && fp.y == 0 {
iterStart = 1
}
for i := iterStart; i < iterLen; i++ {
xo := i%3 - 1
yo := (i / 3) - 1
// we iterate over the pixel itself but don't include it in the result set
if xo == 0 && yo == 0 {
continue
}
x := fp.x + xo
y := fp.y + yo
// these track whether the neighbor is in a different face, and which x/y direction the face is in
fxdir := 0
fydir := 0
if x < 0 {
fxdir = -1
x += hp.FaceSidePixels()
} else if x >= hp.FaceSidePixels() {
fxdir = 1
x -= hp.FaceSidePixels()
}
if y < 0 {
fydir = -1
y += hp.FaceSidePixels()
} else if y >= hp.FaceSidePixels() {
fydir = 1
y -= hp.FaceSidePixels()
}
if fxdir == 1 && fydir != -1 && fp.face < 4 {
x = y
y = maxXY
}
if fydir == 1 && fxdir != -1 && fp.face < 4 {
y = x
x = maxXY
}
if fydir == -1 && fxdir != 1 && fp.face > 7 {
y = x
x = 0
}
if fxdir == -1 && fydir != 1 && fp.face > 7 {
x = y
y = 0
}
face := NewFace(fp.face).Neighbor(fxdir, fydir)
neigh := FacePixel{x, y, face}.PixelId(hp, scheme)
result = append(result, neigh)
}
}
return result
}