-
Notifications
You must be signed in to change notification settings - Fork 10
/
fixedRadiusSearch.go
288 lines (256 loc) · 7.77 KB
/
fixedRadiusSearch.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package lidario
import (
"errors"
"math"
)
// FixedRadiusSearchResult is used to store return values from searches
type FixedRadiusSearchResult struct {
Index int
SquaredDist float64
}
type fixedRadiusSearch struct {
radius, radiusSquared float64
lf *LasFile
hm map[int64]*frsEntryList
length int
xMin, yMin, zMin float64
xMax, yMax, zMax float64
nRows, nCols, nLayers int64
nCellsPerLayer int64
threeDMode bool
}
func build(lf *LasFile, radius float64, threeDMode bool) *fixedRadiusSearch {
hm := make(map[int64]*frsEntryList)
frs := fixedRadiusSearch{radius: radius, radiusSquared: radius * radius, lf: lf, hm: hm, length: 0, threeDMode: threeDMode}
frs.xMin = lf.Header.MinX - radius
frs.yMin = lf.Header.MinY - radius
frs.zMin = lf.Header.MinZ - radius
frs.xMax = lf.Header.MaxX + radius
frs.yMax = lf.Header.MaxY + radius
frs.zMax = lf.Header.MaxZ + radius
frs.nRows = int64((frs.xMax - frs.xMin) / radius)
frs.nCols = int64((frs.yMax - frs.yMin) / radius)
frs.nLayers = int64((frs.zMax - frs.zMin) / radius)
frs.nCellsPerLayer = frs.nRows * frs.nCols
var k int64
if !frs.threeDMode {
// var c, r int
for i, p := range lf.pointData {
//c, r = frs.getBinCoordinates(p.X, p.Y)
k = frs.getCellNum2D(p.X, p.Y)
if list, ok := frs.hm[k]; ok {
list.push(i)
} else {
list = new(frsEntryList)
list.push(i)
frs.hm[k] = list
}
frs.length++
}
} else {
for i, p := range lf.pointData {
k = frs.getCellNum3D(p.X, p.Y, p.Z)
if list, ok := frs.hm[k]; ok {
list.push(i)
} else {
list = new(frsEntryList)
list.push(i)
frs.hm[k] = list
}
frs.length++
}
}
return &frs
}
func (frs *fixedRadiusSearch) getCellNum2D(x, y float64) int64 {
if x < frs.xMin || x > frs.xMax || y < frs.yMin || y > frs.yMax {
return -1
}
return int64(math.Floor((y-frs.yMin)/frs.radius))*frs.nCols + int64(math.Floor((x-frs.xMin)/frs.radius))
}
func (frs *fixedRadiusSearch) getBinCoordinates2D(x, y float64) (column, row int64) {
column = int64(math.Floor((x - frs.xMin) / frs.radius))
row = int64(math.Floor((y - frs.yMin) / frs.radius))
return
}
func (frs *fixedRadiusSearch) getCellNum3D(x, y, z float64) int64 {
if x < frs.xMin || x > frs.xMax || y < frs.yMin || y > frs.yMax || z < frs.zMin || z > frs.zMax {
return -1
}
col := int64(math.Floor((x - frs.xMin) / frs.radius))
row := int64(math.Floor((y - frs.yMin) / frs.radius))
layer := int64(math.Floor((z - frs.zMin) / frs.radius))
return int64(layer*frs.nCellsPerLayer + row*frs.nCols + col)
}
func (frs *fixedRadiusSearch) getBinCoordinates3D(x, y, z float64) (column, row, layer int64) {
column = int64(math.Floor((x - frs.xMin) / frs.radius))
row = int64(math.Floor((y - frs.yMin) / frs.radius))
layer = int64(math.Floor((z - frs.zMin) / frs.radius))
return
}
func (frs *fixedRadiusSearch) search2D(x, y float64) *FRSResultList {
ret := new(FRSResultList)
if x < frs.xMin || x > frs.xMax || y < frs.yMin || y > frs.yMax {
return ret
}
var ok bool
var l *frsEntryList
var entry *frsEntryNode
var squaredDist float64
var p PointRecord0
stCol, stRow := frs.getBinCoordinates2D(x-frs.radius, y-frs.radius)
endCol, endRow := frs.getBinCoordinates2D(x+frs.radius, y+frs.radius)
var k int64
for m := stCol; m <= endCol; m++ {
for n := stRow; n <= endRow; n++ {
k = n*frs.nCols + m
if l, ok = frs.hm[k]; ok {
for entry = l.first(); entry != nil; entry = entry.next() {
// calculate the squared distance to (x,y)
p = frs.lf.pointData[entry.index]
squaredDist = (x-p.X)*(x-p.X) + (y-p.Y)*(y-p.Y)
if squaredDist <= frs.radiusSquared {
ret.Push(FixedRadiusSearchResult{Index: entry.index, SquaredDist: squaredDist})
}
}
}
}
}
return ret
// for m := -1; m <= 1; m++ {
// for n := -1; n <= 1; n++ {
// if valContainer, ok = frs.hm[frsKey{col: c + m, row: r + n}]; ok {
// for _, entry = range valContainer.data {
// // calculate the squared distance to (x,y)
// squaredDist = (x-entry.x)*(x-entry.x) + (y-entry.y)*(y-entry.y)
// if squaredDist <= frs.radiusSquared {
// ret.Push(FixedRadiusSearchResult{entry: entry, squaredDist: squaredDist})
// // ret = append(ret, FixedRadiusSearchResult{entry: entry, squaredDist: squaredDist})
// }
// }
// }
// }
// }
}
func (frs *fixedRadiusSearch) search3D(x, y, z float64) *FRSResultList {
ret := new(FRSResultList)
if x < frs.xMin || x > frs.xMax || y < frs.yMin || y > frs.yMax || z < frs.zMin || z > frs.zMax {
return ret
}
var ok bool
var l *frsEntryList
var entry *frsEntryNode
var squaredDist float64
var p PointRecord0
stCol, stRow, stLayer := frs.getBinCoordinates3D(x-frs.radius, y-frs.radius, z-frs.radius)
endCol, endRow, endLayer := frs.getBinCoordinates3D(x+frs.radius, y+frs.radius, z+frs.radius)
var k int64
for m := stCol; m <= endCol; m++ {
for n := stRow; n <= endRow; n++ {
for s := stLayer; s <= endLayer; s++ {
k = s*frs.nCellsPerLayer + n*frs.nCols + m
if l, ok = frs.hm[k]; ok {
for entry = l.first(); entry != nil; entry = entry.next() {
// calculate the squared distance to (x,y)
p = frs.lf.pointData[entry.index]
squaredDist = (x-p.X)*(x-p.X) + (y-p.Y)*(y-p.Y) + (z-p.Z)*(z-p.Z)
if squaredDist <= frs.radiusSquared {
ret.Push(FixedRadiusSearchResult{Index: entry.index, SquaredDist: squaredDist})
}
}
}
}
}
}
return ret
}
// FRSResultNode list node
type FRSResultNode struct {
FixedRadiusSearchResult // Embedded struct
next, prev *FRSResultNode
}
// FRSResultList list return from a fixed-radius search
type FRSResultList struct {
head, tail *FRSResultNode
size int
}
// First returns the head of the list
func (l *FRSResultList) First() *FRSResultNode {
return l.head
}
// Next returns the next node to the current
func (n *FRSResultNode) Next() *FRSResultNode {
return n.next
}
// Prev returns the previous node to the current
func (n *FRSResultNode) Prev() *FRSResultNode {
return n.prev
}
// Len return the list's length
func (l *FRSResultList) Len() int {
return l.size
}
// Push Create new node with value
func (l *FRSResultList) Push(value FixedRadiusSearchResult) *FRSResultList {
n := &FRSResultNode{FixedRadiusSearchResult: value}
if l.size > 0 {
l.tail.next = n // Add after prev last node
n.prev = l.tail // Link back to prev last node
} else {
l.head = n // First node
}
l.tail = n // reset tail to newly added node
l.size++
return l
}
var errEmpty = errors.New("ERROR - List is empty")
// Pop last item from list
func (l *FRSResultList) Pop() (value FixedRadiusSearchResult, err error) {
if l.size > 0 {
value, l.tail = l.tail.FixedRadiusSearchResult, l.tail.prev
if l.tail == nil {
l.head = nil
}
l.size--
return
}
return value, errEmpty
}
type frsEntryNode struct {
index int //frsEntry // Embedded struct
nextNode, prevNode *frsEntryNode
}
// FRSResultList list return from a fixed-radius search
type frsEntryList struct {
head, tail *frsEntryNode
size int
}
// First returns the head of the list
func (l *frsEntryList) first() *frsEntryNode {
return l.head
}
// Next returns the next node to the current
func (n *frsEntryNode) next() *frsEntryNode {
return n.nextNode
}
// Prev returns the previous node to the current
func (n *frsEntryNode) prev() *frsEntryNode {
return n.prevNode
}
// Len return the list's length
func (l *frsEntryList) len() int {
return l.size
}
// Push Create new node with value
func (l *frsEntryList) push(value int) *frsEntryList {
n := &frsEntryNode{index: value}
if l.size > 0 {
l.tail.nextNode = n // Add after prev last node
n.prevNode = l.tail // Link back to prev last node
} else {
l.head = n // First node
}
l.tail = n // reset tail to newly added node
l.size++
return l
}