Repository navigation
Expand file tree
/
Copy pathclipperd.go
More file actions
166 lines (126 loc) · 4.29 KB
/
Copy pathclipperd.go
File metadata and controls
166 lines (126 loc) · 4.29 KB
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
package go_clipper2
import (
"math"
)
type clipperD struct {
*clipperBase
scale float64
invScale float64
}
func NewClipperD(decimalPrecision int) *clipperD {
if decimalPrecision == 0 {
decimalPrecision = 2
}
if decimalPrecision < -8 || decimalPrecision > 8 {
panic(ErrPrecisionRange)
}
scale := math.Pow(10, float64(decimalPrecision))
return &clipperD{
clipperBase: newClipperBase(),
scale: scale,
invScale: 1 / scale,
}
}
func (c *clipperD) AddPaths(paths PathsD, polytype PathType, isOpen bool) {
c.addPaths(ScalePathsDToPaths64(paths, c.scale), polytype, isOpen)
}
func (c *clipperD) AddPathsWithScaleFunc(paths PathsD, polytype PathType, isOpen bool, scaleFn func(paths PathsD, scale float64) Paths64) {
c.addPaths(scaleFn(paths, c.scale), polytype, isOpen)
}
func (c *clipperD) Execute(clipType ClipType, fillRule FillRule, solution *PathsD) bool {
solOpen := make(PathsD, 0)
return c.ExecuteOC(clipType, fillRule, solution, &solOpen)
}
func (c *clipperD) ExecutePolyTreeD(clipType ClipType, fillRule FillRule, polytree *PolyTreeD, openPaths *PathsD) bool {
c.usingPolyTree = true
polytree.Clear()
*openPaths = (*openPaths)[:0]
polytree.SetScale(c.scale)
oPaths := make(Paths64, 0)
c.clipperBase.executeInternal(clipType, fillRule)
c.buildTree(polytree.PolyPathBase, &oPaths)
c.clearSolutionOnly()
if !c.succeeded {
return false
}
for _, oPath := range oPaths {
*openPaths = append(*openPaths, ScalePath64ToPathD(oPath, c.invScale))
}
return true
}
func (c *clipperD) ExecuteOC(clipType ClipType, fillRule FillRule, solutionClosed, solutionOpen *PathsD) bool {
solClosed64 := make(Paths64, 0)
solOpen64 := make(Paths64, 0)
success := c.clipperBase.execute(clipType, fillRule, &solClosed64, &solOpen64)
c.clearSolutionOnly()
if !success {
return false
}
for _, path := range solClosed64 {
*solutionClosed = append(*solutionClosed, ScalePath64ToPathD(path, c.invScale))
}
for _, path := range solOpen64 {
*solutionOpen = append(*solutionOpen, ScalePath64ToPathD(path, c.invScale))
}
return true
}
func (c *clipperD) ExecuteWithScaleFunc(clipType ClipType, fillRule FillRule, solutionClosed, solutionOpen *PathsD, scaleFn func(path Path64, scale float64) PathD) bool {
solClosed64 := make(Paths64, 0)
solOpen64 := make(Paths64, 0)
success := c.clipperBase.execute(clipType, fillRule, &solClosed64, &solOpen64)
c.clearSolutionOnly()
if !success {
return false
}
for _, path := range solClosed64 {
*solutionClosed = append(*solutionClosed, scaleFn(path, c.invScale))
}
for _, path := range solOpen64 {
*solutionOpen = append(*solutionOpen, scaleFn(path, c.invScale))
}
return true
}
func UnionPathsD(subject PathsD, fillRule FillRule, precision ...int) PathsD {
return BooleanOpPathsD(Union, subject, nil, fillRule, precision...)
}
func UnionWithClipPathsD(subject, clip PathsD, fillRule FillRule, precision ...int) PathsD {
return BooleanOpPathsD(Union, subject, clip, fillRule, precision...)
}
func IntersectWithClipPathsD(subject, clip PathsD, fillRule FillRule, precision ...int) PathsD {
return BooleanOpPathsD(Intersection, subject, clip, fillRule, precision...)
}
func DifferenceWithClipPathsD(subject, clip PathsD, fillRule FillRule, precision ...int) PathsD {
return BooleanOpPathsD(Difference, subject, clip, fillRule, precision...)
}
func XorWithClipPathsD(subject, clip PathsD, fillRule FillRule, precision ...int) PathsD {
return BooleanOpPathsD(Xor, subject, clip, fillRule, precision...)
}
func BooleanOpPathsD(clipType ClipType, subject PathsD, clip PathsD, fillRule FillRule, precisionV ...int) PathsD {
precision := 2
if len(precisionV) > 0 {
precision = precisionV[0]
}
solution := make(PathsD, 0)
c := NewClipperD(precision)
c.AddPaths(subject, Subject, false)
if clip != nil {
c.AddPaths(clip, Clip, false)
}
c.Execute(clipType, fillRule, &solution)
return solution
}
func BooleanOpPolyTreeD(clipType ClipType, subject PathsD, clip PathsD, fillRule FillRule, precisionV ...int) *PolyTreeD {
precision := 2
if len(precisionV) > 0 {
precision = precisionV[0]
}
polytree := NewPolyTreeD()
c := NewClipperD(precision)
c.AddPaths(subject, Subject, false)
if clip != nil {
c.AddPaths(clip, Clip, false)
}
openPath := make(PathsD, 0)
c.ExecutePolyTreeD(clipType, fillRule, polytree, &openPath)
return polytree
}