forked from dhconnelly/rtreego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geom.go
377 lines (334 loc) · 8.63 KB
/
geom.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2012 Daniel Connelly. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rtreego
import (
"fmt"
"math"
"strings"
)
// DimError represents a failure due to mismatched dimensions.
type DimError struct {
Expected int
Actual int
}
func (err DimError) Error() string {
return "rtreego: dimension mismatch"
}
// DistError is an improper distance measurement. It implements the error
// and is generated when a distance-related assertion fails.
type DistError float64
func (err DistError) Error() string {
return "rtreego: improper distance"
}
// Point represents a point in n-dimensional Euclidean space.
type Point []float64
// Dist computes the Euclidean distance between two points p and q.
func (p Point) dist(q Point) float64 {
if len(p) != len(q) {
panic(DimError{len(p), len(q)})
}
sum := 0.0
for i := range p {
dx := p[i] - q[i]
sum += dx * dx
}
return math.Sqrt(sum)
}
// minDist computes the square of the distance from a point to a rectangle.
// If the point is contained in the rectangle then the distance is zero.
//
// Implemented per Definition 2 of "Nearest Neighbor Queries" by
// N. Roussopoulos, S. Kelley and F. Vincent, ACM SIGMOD, pages 71-79, 1995.
func (p Point) minDist(r *Rect) float64 {
if len(p) != len(r.p) {
panic(DimError{len(p), len(r.p)})
}
sum := 0.0
for i, pi := range p {
if pi < r.p[i] {
d := pi - r.p[i]
sum += d * d
} else if pi > r.q[i] {
d := pi - r.q[i]
sum += d * d
} else {
sum += 0
}
}
return sum
}
// minMaxDist computes the minimum of the maximum distances from p to points
// on r. If r is the bounding box of some geometric objects, then there is
// at least one object contained in r within minMaxDist(p, r) of p.
//
// Implemented per Definition 4 of "Nearest Neighbor Queries" by
// N. Roussopoulos, S. Kelley and F. Vincent, ACM SIGMOD, pages 71-79, 1995.
func (p Point) minMaxDist(r *Rect) float64 {
if len(p) != len(r.p) {
panic(DimError{len(p), len(r.p)})
}
// by definition, MinMaxDist(p, r) =
// min{1<=k<=n}(|pk - rmk|^2 + sum{1<=i<=n, i != k}(|pi - rMi|^2))
// where rmk and rMk are defined as follows:
rm := func(k int) float64 {
if p[k] <= (r.p[k]+r.q[k])/2 {
return r.p[k]
}
return r.q[k]
}
rM := func(k int) float64 {
if p[k] >= (r.p[k]+r.q[k])/2 {
return r.p[k]
}
return r.q[k]
}
// This formula can be computed in linear time by precomputing
// S = sum{1<=i<=n}(|pi - rMi|^2).
S := 0.0
for i := range p {
d := p[i] - rM(i)
S += d * d
}
// Compute MinMaxDist using the precomputed S.
min := math.MaxFloat64
for k := range p {
d1 := p[k] - rM(k)
d2 := p[k] - rm(k)
d := S - d1*d1 + d2*d2
if d < min {
min = d
}
}
return min
}
// Rect represents a subset of n-dimensional Euclidean space of the form
// [a1, b1] x [a2, b2] x ... x [an, bn], where ai < bi for all 1 <= i <= n.
type Rect struct {
p, q Point // Enforced by NewRect: p[i] <= q[i] for all i.
}
// PointCoord returns the coordinate of the point of the rectangle at i
func (r *Rect) PointCoord(i int) float64 {
return r.p[i]
}
// LengthsCoord returns the coordinate of the lengths of the rectangle at i
func (r *Rect) LengthsCoord(i int) float64 {
return r.q[i] - r.p[i]
}
// Equal returns true if the two rectangles are equal
func (r *Rect) Equal(other *Rect) bool {
for i, e := range r.p {
if e != other.p[i] {
return false
}
}
for i, e := range r.q {
if e != other.q[i] {
return false
}
}
return true
}
func (r *Rect) String() string {
s := make([]string, len(r.p))
for i, a := range r.p {
b := r.q[i]
s[i] = fmt.Sprintf("[%.2f, %.2f]", a, b)
}
return strings.Join(s, "x")
}
// NewRect constructs and returns a pointer to a Rect given a corner point and
// the lengths of each dimension. The point p should be the most-negative point
// on the rectangle (in every dimension) and every length should be positive.
func NewRect(p Point, lengths []float64) (r *Rect, err error) {
r = new(Rect)
r.p = p
if len(p) != len(lengths) {
err = &DimError{len(p), len(lengths)}
return
}
r.q = make([]float64, len(p))
for i := range p {
if lengths[i] <= 0 {
err = DistError(lengths[i])
return
}
r.q[i] = p[i] + lengths[i]
}
return
}
// NewRectFromPoints constructs and returns a pointer to a Rect given a slice of points
func NewRectFromPoints(points []Point) (r *Rect) {
bb := new(Rect)
if len(points) == 1 {
panic(fmt.Errorf("Failed to create rect from single point"))
}
dim := len(points[0])
bb.p = make([]float64, dim)
bb.q = make([]float64, dim)
for i := 0; i < dim; i++ {
bb.p[i], bb.q[i] = points[0][i], points[0][i]
}
for _, point := range points {
if len(point) != dim {
panic(DimError{dim, len(points)})
}
for i := 0; i < dim; i++ {
if point[i] < bb.p[i] {
bb.p[i] = point[i]
}
if point[i] > bb.q[i] {
bb.q[i] = point[i]
}
}
}
return bb
}
// size computes the measure of a rectangle (the product of its side lengths).
func (r *Rect) size() float64 {
size := 1.0
for i, a := range r.p {
b := r.q[i]
size *= b - a
}
return size
}
// margin computes the sum of the edge lengths of a rectangle.
func (r *Rect) margin() float64 {
// The number of edges in an n-dimensional rectangle is n * 2^(n-1)
// (http://en.wikipedia.org/wiki/Hypercube_graph). Thus the number
// of edges of length (ai - bi), where the rectangle is determined
// by p = (a1, a2, ..., an) and q = (b1, b2, ..., bn), is 2^(n-1).
//
// The margin of the rectangle, then, is given by the formula
// 2^(n-1) * [(b1 - a1) + (b2 - a2) + ... + (bn - an)].
dim := len(r.p)
sum := 0.0
for i, a := range r.p {
b := r.q[i]
sum += b - a
}
return math.Pow(2, float64(dim-1)) * sum
}
// containsPoint tests whether p is located inside or on the boundary of r.
func (r *Rect) containsPoint(p Point) bool {
if len(p) != len(r.p) {
panic(DimError{len(r.p), len(p)})
}
for i, a := range p {
// p is contained in (or on) r if and only if p <= a <= q for
// every dimension.
if a < r.p[i] || a > r.q[i] {
return false
}
}
return true
}
// containsRect tests whether r2 is is located inside r1.
func (r *Rect) containsRect(r2 *Rect) bool {
if len(r.p) != len(r2.p) {
panic(DimError{len(r.p), len(r2.p)})
}
for i, a1 := range r.p {
b1, a2, b2 := r.q[i], r2.p[i], r2.q[i]
// enforced by constructor: a1 <= b1 and a2 <= b2.
// so containment holds if and only if a1 <= a2 <= b2 <= b1
// for every dimension.
if a1 > a2 || b2 > b1 {
return false
}
}
return true
}
// intersect computes the intersection of two rectangles. If no intersection
// exists, the intersection is nil.
func intersect(r1, r2 *Rect) *Rect {
dim := len(r1.p)
if len(r2.p) != dim {
panic(DimError{dim, len(r2.p)})
}
// There are four cases of overlap:
//
// 1. a1------------b1
// a2------------b2
// p--------q
//
// 2. a1------------b1
// a2------------b2
// p--------q
//
// 3. a1-----------------b1
// a2-------b2
// p--------q
//
// 4. a1-------b1
// a2-----------------b2
// p--------q
//
// Thus there are only two cases of non-overlap:
//
// 1. a1------b1
// a2------b2
//
// 2. a1------b1
// a2------b2
//
// Enforced by constructor: a1 <= b1 and a2 <= b2. So we can just
// check the endpoints.
p := make([]float64, dim)
q := make([]float64, dim)
for i := range p {
a1, b1, a2, b2 := r1.p[i], r1.q[i], r2.p[i], r2.q[i]
if b2 <= a1 || b1 <= a2 {
return nil
}
p[i] = math.Max(a1, a2)
q[i] = math.Min(b1, b2)
}
return &Rect{p, q}
}
// ToRect constructs a rectangle containing p with side lengths 2*tol.
func (p Point) ToRect(tol float64) *Rect {
dim := len(p)
a, b := make([]float64, dim), make([]float64, dim)
for i := range p {
a[i] = p[i] - tol
b[i] = p[i] + tol
}
return &Rect{a, b}
}
// boundingBox constructs the smallest rectangle containing both r1 and r2.
func boundingBox(r1, r2 *Rect) (bb *Rect) {
bb = new(Rect)
dim := len(r1.p)
bb.p = make([]float64, dim)
bb.q = make([]float64, dim)
if len(r2.p) != dim {
panic(DimError{dim, len(r2.p)})
}
for i := 0; i < dim; i++ {
if r1.p[i] <= r2.p[i] {
bb.p[i] = r1.p[i]
} else {
bb.p[i] = r2.p[i]
}
if r1.q[i] <= r2.q[i] {
bb.q[i] = r2.q[i]
} else {
bb.q[i] = r1.q[i]
}
}
return
}
// boundingBoxN constructs the smallest rectangle containing all of r...
func boundingBoxN(rects ...*Rect) (bb *Rect) {
if len(rects) == 1 {
bb = rects[0]
return
}
bb = boundingBox(rects[0], rects[1])
for _, rect := range rects[2:] {
bb = boundingBox(bb, rect)
}
return
}