-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
path_decimate.go
283 lines (268 loc) · 9.09 KB
/
path_decimate.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
package canvas
import (
"math"
)
// Gridsnap snaps all vertices to a grid with the given spacing. This will significantly reduce numerical issues e.g. for path boolean operations. This operation is in-place.
func (p *Path) Gridsnap(spacing float64) *Path {
for i := 0; i < len(p.d); {
cmd := p.d[i]
switch cmd {
case MoveToCmd:
p.d[i+1] = snap(p.d[i+1], spacing)
p.d[i+2] = snap(p.d[i+2], spacing)
case LineToCmd:
p.d[i+1] = snap(p.d[i+1], spacing)
p.d[i+2] = snap(p.d[i+2], spacing)
case QuadToCmd:
p.d[i+1] = snap(p.d[i+1], spacing)
p.d[i+2] = snap(p.d[i+2], spacing)
p.d[i+3] = snap(p.d[i+3], spacing)
p.d[i+4] = snap(p.d[i+4], spacing)
case CubeToCmd:
p.d[i+1] = snap(p.d[i+1], spacing)
p.d[i+2] = snap(p.d[i+2], spacing)
p.d[i+3] = snap(p.d[i+3], spacing)
p.d[i+4] = snap(p.d[i+4], spacing)
p.d[i+5] = snap(p.d[i+5], spacing)
p.d[i+6] = snap(p.d[i+6], spacing)
case ArcToCmd:
p.d[i+1] = snap(p.d[i+1], spacing)
p.d[i+2] = snap(p.d[i+2], spacing)
p.d[i+5] = snap(p.d[i+5], spacing)
p.d[i+6] = snap(p.d[i+6], spacing)
case CloseCmd:
p.d[i+1] = snap(p.d[i+1], spacing)
p.d[i+2] = snap(p.d[i+2], spacing)
}
i += cmdLen(cmd)
}
return p
}
// Decimate decimates the path using the Visvalingam-Whyatt algorithm. Assuming path is flat and has no subpaths.
func (p *Path) Decimate(tolerance float64) *Path {
q := &Path{}
Loop:
for _, pi := range p.Split() {
// indices are always one past the current point with -1 the command and [-3,-2] the endpoint
var is []int // stack of coordinate indices
closed := pi.d[len(pi.d)-1] == CloseCmd
if closed {
// put before-close command first
is = append(is, len(pi.d)-cmdLen(CloseCmd))
}
i := 0
for len(is) < 3 {
if len(pi.d) <= i {
q = q.Append(pi)
continue Loop
}
i += cmdLen(pi.d[i])
is = append(is, i)
}
// find indices of triangles with an area superior or equal to tolerance
for {
iPrev, iCur, iNext := is[len(is)-3], is[len(is)-2], is[len(is)-1]
prev := Point{pi.d[iPrev-3], pi.d[iPrev-2]}
cur := Point{pi.d[iCur-3], pi.d[iCur-2]}
next := Point{pi.d[iNext-3], pi.d[iNext-2]}
area := 0.5 * math.Abs(prev.X*cur.Y+cur.X*next.Y+next.X*prev.Y-prev.X*next.Y-cur.X*prev.Y-next.X*cur.Y)
if area < tolerance {
// remove point
is[len(is)-2] = is[len(is)-1] // cur = next
is = is[:len(is)-1]
}
if tolerance <= area || len(is) < 3 {
// advance to next triangle
if len(pi.d) <= i {
// end of path
break
} else if closed && i == is[0] {
if iNext < iCur || len(is) < 3 {
// past the end, no point is removed, so we're done
break
}
// end of closed path, move first index to the end
is = append(is, is[0])
is = is[1:]
i = is[0]
} else {
i += cmdLen(pi.d[i])
is = append(is, i)
}
}
}
// build the new path
if len(is) < 2 || closed && len(is) < 3 {
continue
}
if closed {
q.MoveTo(pi.d[is[len(is)-1]-3], pi.d[is[len(is)-1]-2])
is = is[:len(is)-1]
} else {
q.MoveTo(pi.d[is[0]-3], pi.d[is[0]-2])
is = is[1:]
}
for _, i := range is {
q.d = append(q.d, pi.d[i-cmdLen(pi.d[i-1]):i]...)
}
if closed {
q.Close()
}
}
return q
}
// Clip removes all segments that are completely outside the given clipping rectangle. To ensure that the removal doesn't cause a segment to cross the rectangle from the outside, it keeps points that cross at least two lines to infinity along the rectangle's edges. This is much quicker (along O(n)) than using p.And(canvas.Rectangle(x1-x0, y1-y0).Translate(x0, y0)) (which is O(n log n)).
func (p *Path) Clip(x0, y0, x1, y1 float64) *Path {
if x1 < x0 {
x0, x1 = x1, x0
}
if y1 < y0 {
y0, y1 = y1, y0
}
rect := Rect{x0, y0, x1, y1}
startIn := false
pendingMoveTo := true
first, start := Point{}, Point{}
var moveToIndex, firstInIndex int
q := &Path{} //d: p.d[:0]} // q is always smaller or equal to p
for i := 0; i < len(p.d); {
cmd := p.d[i]
i += cmdLen(cmd)
end := Point{p.d[i-3], p.d[i-2]}
endIn := rect.TouchesPoint(end)
crossesXY := false
if 0 < len(q.d) && !startIn && !endIn {
// check if between out last position on Q and the end of the current segment we cross
// lines along X0, X1, Y0, or Y1. If we cross both an X and Y boundary, the path may
// cross into the clipping rectangle, so we must include some points on the exterior
// of the clipping rectangle to prevent that.
prev := Point{q.d[len(q.d)-3], q.d[len(q.d)-2]}
crossesX0 := prev.X < rect.X0 && rect.X0 < end.X || rect.X0 < prev.X && end.X < rect.X0
crossesX1 := prev.X < rect.X1 && rect.X1 < end.X || rect.X1 < prev.X && end.X < rect.X1
crossesY0 := prev.Y < rect.Y0 && rect.Y0 < end.Y || rect.Y0 < prev.Y && end.Y < rect.Y0
crossesY1 := prev.Y < rect.Y1 && rect.Y1 < end.Y || rect.Y1 < prev.Y && end.Y < rect.Y1
crossesXY = (crossesX0 || crossesX1) && (crossesY0 || crossesY1)
}
if cmd == MoveToCmd {
if endIn {
q.d = append(q.d, MoveToCmd, end.X, end.Y, MoveToCmd)
pendingMoveTo = false
firstInIndex = i
first = end
} else {
pendingMoveTo = true
}
moveToIndex = i - cmdLen(MoveToCmd)
} else {
if crossesXY || !startIn && endIn {
if pendingMoveTo {
q.d = append(q.d, MoveToCmd, start.X, start.Y, MoveToCmd)
pendingMoveTo = false
firstInIndex = i
first = start
} else {
q.d = append(q.d, LineToCmd, start.X, start.Y, LineToCmd)
}
}
if cmd == LineToCmd && (startIn || endIn) {
if pendingMoveTo {
q.d = append(q.d, MoveToCmd, start.X, start.Y, MoveToCmd)
pendingMoveTo = false
firstInIndex = i
first = start
}
q.d = append(q.d, p.d[i-4:i]...)
} else if cmd == QuadToCmd {
cp := Point{p.d[i-5], p.d[i-4]}
if startIn || endIn || rect.TouchesPoint(cp) {
if pendingMoveTo {
q.d = append(q.d, MoveToCmd, start.X, start.Y, MoveToCmd)
pendingMoveTo = false
firstInIndex = i
first = start
}
q.d = append(q.d, p.d[i-6:i]...)
}
} else if cmd == CubeToCmd {
cp0 := Point{p.d[i-7], p.d[i-6]}
cp1 := Point{p.d[i-5], p.d[i-4]}
if startIn || endIn || rect.TouchesPoint(cp0) || rect.TouchesPoint(cp1) {
if pendingMoveTo {
q.d = append(q.d, MoveToCmd, start.X, start.Y, MoveToCmd)
pendingMoveTo = false
firstInIndex = i
first = start
}
q.d = append(q.d, p.d[i-8:i]...)
}
} else if cmd == ArcToCmd {
touches := startIn || endIn
rx, ry, phi := p.d[i-7], p.d[i-6], p.d[i-5]
large, sweep := toArcFlags(p.d[i-4])
if !touches {
cx, cy, theta0, theta1 := ellipseToCenter(start.X, start.Y, rx, ry, phi, large, sweep, end.X, end.Y)
// find the four extremes (top, bottom, left, right) and apply those who are between theta1 and theta2
// x(theta) = cx + rx*cos(theta)*cos(phi) - ry*sin(theta)*sin(phi)
// y(theta) = cy + rx*cos(theta)*sin(phi) + ry*sin(theta)*cos(phi)
// be aware that positive rotation appears clockwise in SVGs (non-Cartesian coordinate system)
// we can now find the angles of the extremes
sinphi, cosphi := math.Sincos(phi)
thetaRight := math.Atan2(-ry*sinphi, rx*cosphi)
thetaTop := math.Atan2(rx*cosphi, ry*sinphi)
thetaLeft := thetaRight + math.Pi
thetaBottom := thetaTop + math.Pi
dx := math.Sqrt(rx*rx*cosphi*cosphi + ry*ry*sinphi*sinphi)
dy := math.Sqrt(rx*rx*sinphi*sinphi + ry*ry*cosphi*cosphi)
if angleBetween(thetaLeft, theta0, theta1) {
touches = touches || rect.TouchesPoint(Point{cx - dx, cy})
}
if angleBetween(thetaRight, theta0, theta1) {
touches = touches || rect.TouchesPoint(Point{cx + dx, cy})
}
if angleBetween(thetaBottom, theta0, theta1) {
touches = touches || rect.TouchesPoint(Point{cx, cy - dy})
}
if angleBetween(thetaTop, theta0, theta1) {
touches = touches || rect.TouchesPoint(Point{cx, cy + dy})
}
}
if touches {
if pendingMoveTo {
q.d = append(q.d, MoveToCmd, start.X, start.Y, MoveToCmd)
pendingMoveTo = false
firstInIndex = i
first = start
}
q.d = append(q.d, p.d[i-8:i]...)
}
} else if cmd == CloseCmd {
if !pendingMoveTo {
// handle first part of the path which may cross boundaries
start = Point{p.d[moveToIndex+1], p.d[moveToIndex+2]}
for i := moveToIndex; ; {
cmd := p.d[i]
i += cmdLen(cmd)
if firstInIndex <= i {
break
}
end := Point{p.d[i-3], p.d[i-2]}
prev := Point{q.d[len(q.d)-3], q.d[len(q.d)-2]}
crossesX0 := prev.X < rect.X0 && rect.X0 < end.X || rect.X0 < prev.X && end.X < rect.X0
crossesX1 := prev.X < rect.X1 && rect.X1 < end.X || rect.X1 < prev.X && end.X < rect.X1
crossesY0 := prev.Y < rect.Y0 && rect.Y0 < end.Y || rect.Y0 < prev.Y && end.Y < rect.Y0
crossesY1 := prev.Y < rect.Y1 && rect.Y1 < end.Y || rect.Y1 < prev.Y && end.Y < rect.Y1
if (crossesX0 || crossesX1) && (crossesY0 || crossesY1) {
q.d = append(q.d, LineToCmd, start.X, start.Y, LineToCmd)
}
start = end
}
q.d = append(q.d, CloseCmd, first.X, first.Y, CloseCmd)
}
pendingMoveTo = true
}
}
start = end
startIn = endIn
}
return q
}