-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
path_tiling.go
351 lines (330 loc) · 10.8 KB
/
path_tiling.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
package canvas
import (
"math"
"github.com/ByteArena/poly2tri-go"
)
// PrimitiveCell is a (primitive) cell used for tiling.
func PrimitiveCell(a, b Point) Matrix {
A := a.Length()
B := a.PerpDot(b) / A
s := a.Dot(b) / A / B
return Identity.Rotate(a.Angle()*180.0/math.Pi).Shear(s, 0.0).Scale(A, B)
}
// SquareCell is a square cell with sides of length a used for tiling.
func SquareCell(a float64) Matrix {
return Identity.Scale(a, a)
}
// RectangleCell is a rectangular cell with width a and height b used for tiling.
func RectangleCell(a, b float64) Matrix {
return Identity.Scale(a, b)
}
// RhombusCell is a rhombus cell with sides of length a at an angle of 120 degrees used for tiling.
func RhombusCell(a float64) Matrix {
return PrimitiveCell(Point{a, 0.0}, Point{a, 0.0}.Rot(120.0, Origin))
}
// ParallelogramCell is a paralellogram cell with sides of length a and b at an angle of rot degrees used for tiling.
func ParallelogramCell(a, b, rot float64) Matrix {
return PrimitiveCell(Point{a, 0.0}, Point{b, 0.0}.Rot(rot, Origin))
}
// TileRectangle tiles the given cell (determines the axes along which cells are repeated) onto the rectangle dst (bounds of clipping path), where cells are filled by rectangle src (bounds of object to be tiled).
func TileRectangle(cell Matrix, dst, src Rect) []Matrix {
// find extremes along cell axes
invCell := cell.Inv()
points := []Point{
invCell.Dot(Point{dst.X0, dst.Y0}),
invCell.Dot(Point{dst.X1, dst.Y0}),
invCell.Dot(Point{dst.X1, dst.Y1}),
invCell.Dot(Point{dst.X0, dst.Y1}),
}
x0, x1 := points[0].X, points[0].X
y0, y1 := points[0].Y, points[0].Y
for _, point := range points[1:] {
x0 = math.Min(x0, point.X)
x1 = math.Max(x1, point.X)
y0 = math.Min(y0, point.Y)
y1 = math.Max(y1, point.Y)
}
// add/subtract when overflowing/underflowing cell
cellBounds := src.Transform(invCell)
x0 -= cellBounds.X1 - 1.0
y0 -= cellBounds.Y1 - 1.0
x1 -= cellBounds.X0
y1 -= cellBounds.Y0
// collect all positions
cells := []Matrix{}
for y := math.Floor(y0); y < y1; y += 1.0 {
for x := math.Floor(x0); x < x1; x += 1.0 {
p := cell.Dot(Point{x, y})
if src.Translate(p.X, p.Y).Overlaps(dst) {
cells = append(cells, cell.Translate(x, y))
}
}
}
return cells
}
//// P1 wallpaper group
//var P1 = []Matrix{Identity}
//
//// Pm wallpaper group
//var Pm = []Matrix{
// Identity,
// Identity.ReflectYAbout(0.5),
//}
//
//// Pg wallpaper group
//var Pg = []Matrix{
// Identity,
// Identity.Translate(0.5, 0.0).ReflectYAbout(0.5),
//}
//func Cm(x, y float64) Tiler {
// return Tiler{
// Point{x, 0.0},
// Point{0.0, 2.0 * y},
// []Matrix{
// Identity,
// Identity.Translate(x/2.0, 0.0).ReflectYAbout(y / 2.0),
// Identity.Translate(x/2.0, y),
// Identity.ReflectYAbout(y),
// },
// }
//}
//func Pm(x, y float64) Tiler {
// a := Point{x, 0.0}
// b := Point{0.0, y}
// return func(i, j int) []Matrix {
// pos := a.Mul(float64(i)).Add(b.Mul(float64(j)))
// return []Matrix{
// Identity.Translate(pos.X, pos.Y),
// Identity.Translate(pos.X+x, pos.Y).ReflectX(),
// }
// }
//}
//
//func Cm(x, y float64) Tiler {
// a := Point{x, 0.0}
// b := Point{0.0, y}
// return func(i, j int) []Matrix {
// pos := a.Mul(float64(i)).Add(b.Mul(float64(j)))
// return []Matrix{
// Identity.Translate(pos.X, pos.Y),
// Identity.Translate(pos.X+x/2.0, pos.Y).ReflectX(),
// Identity.Translate(pos.X+x/2.0, pos.Y+y/2.0),
// Identity.Translate(pos.X+x, pos.Y+y/2.0).ReflectX(),
// }
// }
//}
//
//func P2(x, y, rot float64) Tiler {
// a := Point{x, 0.0}
// b := Point{y, 0.0}.Rot(rot*math.Pi/180.0, Point{0.0, 0.0})
// return func(i, j int) []Matrix {
// pos := a.Mul(float64(i)).Add(b.Mul(float64(j)))
// return []Matrix{
// Identity.Translate(pos.X, pos.Y),
// Identity.Translate(pos.X+x, pos.Y+y).Rotate(180),
// }
// }
//}
//
//func Pgg(x, y float64) Tiler {
// a := Point{x, 0.0}
// b := Point{0.0, y}
// return func(i, j int) []Matrix {
// pos := a.Mul(float64(i)).Add(b.Mul(float64(j)))
// return []Matrix{
// Identity.Translate(pos.X, pos.Y),
// Identity.Translate(pos.X+x/2.0, pos.Y+y/2.0).Rotate(180),
// Identity.Translate(pos.X+x/2.0, pos.Y+y).ReflectY(),
// Identity.Translate(pos.X+x, pos.Y+y/2.0).ReflectX(),
// }
// }
//}
//
//func Pmm(x, y float64) Tiler {
// a := Point{x, 0.0}
// b := Point{0.0, y}
// return func(i, j int) []Matrix {
// pos := a.Mul(float64(i)).Add(b.Mul(float64(j)))
// return []Matrix{
// Identity.Translate(pos.X, pos.Y),
// Identity.Translate(pos.X+x, pos.Y).ReflectX(),
// Identity.Translate(pos.X, pos.Y+y).ReflectY(),
// Identity.Translate(pos.X+x, pos.Y+y).Rotate(180),
// }
// }
//}
//
//func Cmm(x, y float64) Tiler {
// a := Point{x, 0.0}
// b := Point{0.0, y}
// return func(i, j int) []Matrix {
// pos := a.Mul(float64(i)).Add(b.Mul(float64(j)))
// return []Matrix{
// Identity.Translate(pos.X, pos.Y),
// Identity.Translate(pos.X+x, pos.Y).ReflectX(),
// Identity.Translate(pos.X, pos.Y+y).ReflectY(),
// Identity.Translate(pos.X+x, pos.Y+y).Rotate(180),
// Identity.Translate(pos.X+x/2.0, pos.Y+y/2.0).Rotate(180),
// Identity.Translate(pos.X+x/2.0, pos.Y+y/2.0).ReflectY(),
// Identity.Translate(pos.X+x/2.0, pos.Y+y/2.0).ReflectX(),
// Identity.Translate(pos.X+x/2.0, pos.Y+y/2.0),
// }
// }
//}
//
//func Pmg(x, y float64) Tiler {
// a := Point{x, 0.0}
// b := Point{0.0, y}
// return func(i, j int) []Matrix {
// pos := a.Mul(float64(i)).Add(b.Mul(float64(j)))
// return []Matrix{
// Identity.Translate(pos.X, pos.Y),
// Identity.Translate(pos.X+x/2.0, pos.Y).ReflectX(),
// Identity.Translate(pos.X+x/2.0, pos.Y+y).ReflectY(),
// Identity.Translate(pos.X+x, pos.Y+y).Rotate(180),
// }
// }
//}
//
//func P4(x float64) Tiler {
// a := Point{x, 0.0}
// b := Point{0.0, x}
// d := x / 2.0
// return func(i, j int) []Matrix {
// pos := a.Mul(float64(i)).Add(b.Mul(float64(j)))
// return []Matrix{
// Identity.Translate(pos.X, pos.Y),
// Identity.Translate(pos.X, pos.Y).RotateAbout(90, d, d),
// Identity.Translate(pos.X, pos.Y).RotateAbout(180, d, d),
// Identity.Translate(pos.X, pos.Y).RotateAbout(270, d, d),
// }
// }
//}
//
//func P4m(x float64) Tiler {
// a := Point{x, 0.0}
// b := Point{0.0, x}
// d := x / 2.0
// return func(i, j int) []Matrix {
// pos := a.Mul(float64(i)).Add(b.Mul(float64(j)))
// return []Matrix{
// Identity.Translate(pos.X, pos.Y),
// Identity.Translate(pos.X, pos.Y).RotateAbout(90, d, d),
// Identity.Translate(pos.X, pos.Y).RotateAbout(180, d, d),
// Identity.Translate(pos.X, pos.Y).RotateAbout(270, d, d),
// Identity.Translate(pos.X, pos.Y).ReflectXAbout(d).RotateAbout(90, d, d),
// Identity.Translate(pos.X, pos.Y).ReflectXAbout(d),
// Identity.Translate(pos.X, pos.Y).ReflectYAbout(d),
// Identity.Translate(pos.X, pos.Y).ReflectYAbout(d).RotateAbout(90, d, d),
// }
// }
//}
//
//func P4g(x float64) Tiler {
// a := Point{x, 0.0}
// b := Point{0.0, x}
// d := x / 2.0
// return func(i, j int) []Matrix {
// pos := a.Mul(float64(i)).Add(b.Mul(float64(j)))
// return []Matrix{
// Identity.Translate(pos.X, pos.Y),
// Identity.Translate(pos.X, pos.Y).ReflectXAbout(x / 4.0),
// Identity.Translate(pos.X, pos.Y).ReflectYAbout(x / 4.0),
// Identity.Translate(pos.X, pos.Y).RotateAbout(180, x/4.0, x/4.0),
// Identity.Translate(pos.X, pos.Y).RotateAbout(90, d, d),
// Identity.Translate(pos.X, pos.Y).RotateAbout(90, d, d).ReflectXAbout(x / 4.0),
// Identity.Translate(pos.X, pos.Y).RotateAbout(90, d, d).ReflectYAbout(x / 4.0),
// Identity.Translate(pos.X, pos.Y).RotateAbout(90, d, d).RotateAbout(180, x/4.0, x/4.0),
// Identity.Translate(pos.X, pos.Y).RotateAbout(180, d, d),
// Identity.Translate(pos.X, pos.Y).RotateAbout(180, d, d).ReflectXAbout(x / 4.0),
// Identity.Translate(pos.X, pos.Y).RotateAbout(180, d, d).ReflectYAbout(x / 4.0),
// Identity.Translate(pos.X, pos.Y).RotateAbout(180, d, d).RotateAbout(180, x/4.0, x/4.0),
// Identity.Translate(pos.X, pos.Y).RotateAbout(270, d, d),
// Identity.Translate(pos.X, pos.Y).RotateAbout(270, d, d).ReflectXAbout(x / 4.0),
// Identity.Translate(pos.X, pos.Y).RotateAbout(270, d, d).ReflectYAbout(x / 4.0),
// Identity.Translate(pos.X, pos.Y).RotateAbout(270, d, d).RotateAbout(180, x/4.0, x/4.0),
// }
// }
//}
//func P3(d float64) Tiler {
// a := Point{d, 0.0}
// b := Point{d, 0.0}.Rot(60.0*math.Pi/180.0, Point{0.0, 0.0})
// return func() (Point, Point, []Matrix) {
// return a, b, []Matrix{
// Identity,
// Identity.Rotate(120.0),
// Identity.Rotate(240.0),
// }
// }
//}
// WallpaperGroup applies the symmetry transformations for the given plane symmetry (or wallpaper) group within the given primitive cell.
//func (p *Path) WallpaperGroup(group []Matrix, cell Matrix) *Path {
// // apply isometries
// r := &Path{}
// invCell := cell.Inv()
// for _, isometry := range group {
// rev := (isometry[0][0] < 0.0) != (isometry[1][1] < 0.0)
// isometry = Identity.Mul(cell).Mul(isometry).Mul(invCell)
// if p.Closed() && rev {
// r = r.Append(p.Copy().Transform(isometry).Reverse())
// } else {
// r = r.Append(p.Copy().Transform(isometry))
// }
// }
// return r
//}
// Tile tiles a path within a clipping path using the given primitive cell.
func (p *Path) Tile(clip *Path, cell Matrix) *Path {
// get path overflow out of cell
bounds := p.FastBounds()
clipBounds := clip.FastBounds()
cells := TileRectangle(cell, clipBounds, bounds)
// append all tiles
r := &Path{}
for _, cell := range cells {
pos := cell.Dot(Origin)
r = r.Append(p.Copy().Translate(pos.X, pos.Y))
}
return r.And(clip)
}
// Triangulate tessellates the path with triangles that fill the path. WIP
func (p *Path) Triangulate() ([][3]Point, [][5]Point) {
p = p.ReplaceArcs()
beziers := [][5]Point{}
contour := []*poly2tri.Point{}
var start, end Point
for i := 0; i < len(p.d); {
cmd := p.d[i]
switch cmd {
case MoveToCmd, LineToCmd:
end = Point{p.d[i+1], p.d[i+2]}
contour = append(contour, poly2tri.NewPoint(end.X, end.Y))
case QuadToCmd:
cp := Point{p.d[i+1], p.d[i+2]}
end = Point{p.d[i+3], p.d[i+4]}
cp1, cp2 := quadraticToCubicBezier(start, cp, end)
contour = append(contour, poly2tri.NewPoint(end.X, end.Y))
beziers = append(beziers, [5]Point{start, cp1, cp2, end, {1.0, 1.0}})
case CubeToCmd:
cp1 := Point{p.d[i+1], p.d[i+2]}
cp2 := Point{p.d[i+3], p.d[i+4]}
end = Point{p.d[i+5], p.d[i+6]}
contour = append(contour, poly2tri.NewPoint(end.X, end.Y))
beziers = append(beziers, [5]Point{start, cp1, cp2, end, {1.0, 1.0}})
case ArcToCmd:
panic("arcs should have been replaced")
}
i += cmdLen(cmd)
start = end
}
swctx := poly2tri.NewSweepContext(contour, false)
swctx.Triangulate()
triangles := [][3]Point{}
for _, tr := range swctx.GetTriangles() {
p0 := Point{tr.Points[0].X, tr.Points[0].Y}
p1 := Point{tr.Points[1].X, tr.Points[1].Y}
p2 := Point{tr.Points[2].X, tr.Points[2].Y}
triangles = append(triangles, [3]Point{p0, p1, p2})
}
return triangles, beziers
}