-
Notifications
You must be signed in to change notification settings - Fork 0
/
decompose.go
51 lines (44 loc) · 1.09 KB
/
decompose.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
package decompose
import (
"github.com/TopoSimplify/lnr"
"github.com/TopoSimplify/node"
"github.com/TopoSimplify/offset"
"github.com/TopoSimplify/pln"
"github.com/TopoSimplify/rng"
"github.com/intdxdt/geom"
"github.com/intdxdt/iter"
)
//Douglas-Peucker decomposition at a given threshold
func DouglasPeucker(
id *iter.Igen,
pln pln.Polyline,
decomp offset.EpsilonDecomposition,
geomFn func(geom.Coords) geom.Geometry,
instance lnr.Linegen,
) []node.Node {
var k, n int
var val float64
var coordinates geom.Coords
var hque []node.Node
if pln.LineString == nil {
return hque
}
var r = pln.Range()
var stack = make([]rng.Rng, 0, (r.J-r.I)+1)
stack = append(stack, r)
for !(len(stack) == 0) {
n = len(stack) - 1
r = stack[n]
stack = stack[:n]
coordinates = pln.SubCoordinates(r)
k, val = decomp.ScoreFn(coordinates)
k = r.I + k //offset
if decomp.Relation(val) {
hque = append(hque, node.CreateNode(id, coordinates, r, geomFn, instance))
} else {
stack = append(stack, rng.Range(k, r.J)) // right
stack = append(stack, rng.Range(r.I, k)) // left
}
}
return hque
}