-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdp.go
118 lines (101 loc) · 2.67 KB
/
dp.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
package dp
import (
"github.com/TopoSimplify/common"
"github.com/TopoSimplify/decompose"
"github.com/TopoSimplify/lnr"
"github.com/TopoSimplify/node"
"github.com/TopoSimplify/offset"
"github.com/TopoSimplify/opts"
"github.com/TopoSimplify/pln"
"github.com/TopoSimplify/state"
"github.com/intdxdt/cmp"
"github.com/intdxdt/geom"
"github.com/intdxdt/iter"
"github.com/intdxdt/sset"
)
//Type DP
type DouglasPeucker struct {
id int
Hulls []node.Node
Pln pln.Polyline
Meta map[string]interface{}
Opts *opts.Opts
Score lnr.ScoreFn
SquareScore lnr.ScoreFn
SimpleSet *sset.SSet
state state.State
}
//Creates a new constrained DP Simplification instance
func New(
id int,
coordinates geom.Coords,
options *opts.Opts,
offsetScore lnr.ScoreFn,
squareOffsetScore ...lnr.ScoreFn,
) *DouglasPeucker {
var sqrScore lnr.ScoreFn
if len(squareOffsetScore) > 0 {
sqrScore = squareOffsetScore[0]
}
var instance = DouglasPeucker{
id: id,
Opts: options,
Meta: make(map[string]interface{}, 0),
SimpleSet: sset.NewSSet(cmp.Int),
Score: offsetScore,
SquareScore: sqrScore,
}
if coordinates.Len() > 1 {
instance.Pln = pln.CreatePolyline(coordinates)
}
return &instance
}
func (self *DouglasPeucker) ScoreRelation(val float64) bool {
return val <= self.Opts.Threshold
}
func (self *DouglasPeucker) SquareScoreRelation(val float64) bool {
return val <= (self.Opts.Threshold * self.Opts.Threshold)
}
func (self *DouglasPeucker) Decompose(id *iter.Igen) []node.Node {
var score = self.Score
var relation = self.ScoreRelation
if self.SquareScore != nil {
score = self.SquareScore
relation = self.SquareScoreRelation
}
var decomp = offset.EpsilonDecomposition{ScoreFn: score, Relation: relation}
return decompose.DouglasPeucker(
id, self.Polyline(), decomp, common.Geometry, self,
)
}
func (self *DouglasPeucker) Simplify(id *iter.Igen) *DouglasPeucker {
self.Hulls = self.Decompose(id)
return self
}
func (self *DouglasPeucker) Simple() []int {
self.SimpleSet.Empty()
for i := range self.Hulls {
self.SimpleSet.Extend(self.Hulls[i].Range.I, self.Hulls[i].Range.J)
}
var indices = make([]int, self.SimpleSet.Size())
self.SimpleSet.ForEach(func(v interface{}, i int) bool {
indices[i] = v.(int)
return true
})
return indices
}
func (self *DouglasPeucker) Id() int {
return self.id
}
func (self *DouglasPeucker) State() *state.State {
return &self.state
}
func (self *DouglasPeucker) Options() *opts.Opts {
return self.Opts
}
func (self *DouglasPeucker) Coordinates() geom.Coords {
return self.Pln.Coordinates
}
func (self *DouglasPeucker) Polyline() pln.Polyline {
return self.Pln
}