-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathpolygon.go
441 lines (410 loc) · 14 KB
/
polygon.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package kad
import (
"log"
"math"
"strings"
"github.com/Knetic/govaluate"
clipper "github.com/swill/go.clipper"
)
const (
CLIP_DIST = 0.7 // In order to just clean duplicate points: .7*.7 = 0.49 < 0.5
)
type Point struct { // point, saving space as it is used a LOT
X, Y float64
}
type Path []Point
type CustomPolygon struct {
Diameter float64 `json:"diameter"`
Height float64 `json:"height"`
Layers []string `json:"layers"`
Op string `json:"op"`
Points string `json:"points"`
Polygon string `json:"polygon"`
Radius float64 `json:"radius"`
RelTo string `json:"rel_to"`
RelAbs string `json:"-"`
Width float64 `json:"width"`
}
// Sets the path to the absolute positioning of the Path relative to an origin Point 'r'.
func (ps Path) Rel(r Point) {
for i := range ps {
ps[i].X += r.X
ps[i].Y += r.Y
}
}
// Copies the Path
func (ps Path) Copy() Path {
dup := Path{}
for i := range ps {
dup = append(dup, Point{ps[i].X, ps[i].Y})
}
return dup
}
// Rotates each point in a set and rotates them 'r' degrees around 'a'.
func (ps Path) RotatePath(r float64, a Point) {
for i := range ps {
px := ps[i].X
py := ps[i].Y
ps[i].X = math.Cos(radians(r))*(px-a.X) - math.Sin(radians(r))*(py-a.Y) + a.X
ps[i].Y = math.Sin(radians(r))*(px-a.X) + math.Cos(radians(r))*(py-a.Y) + a.Y
}
}
// SplitOnAxis path to be drawn by SVGo
func (ps Path) SplitOnAxis() ([]float64, []float64) {
xs := make([]float64, 0)
ys := make([]float64, 0)
for i := range ps {
xs = append(xs, ps[i].X)
ys = append(ys, ps[i].Y)
}
return xs, ys
}
func (ps Path) ToClipperPath() clipper.Path {
p := make(clipper.Path, 0)
for i := range ps {
p = append(p, &clipper.IntPoint{clipper.CInt(ps[i].X * PRECISION), clipper.CInt(ps[i].Y * PRECISION)})
}
c := clipper.NewClipper(clipper.IoNone)
p = c.CleanPolygon(p, CLIP_DIST) // clean duplicates
return p
}
func FromClipperPath(cp clipper.Path) Path {
c := clipper.NewClipper(clipper.IoNone)
cp = c.CleanPolygon(cp, CLIP_DIST) // clean duplicates
p := make(Path, 0)
for i := range cp {
p = append(p, Point{float64(cp[i].X) / PRECISION, float64(cp[i].Y) / PRECISION})
}
return p
}
// Finalize the polygons before they go for file processing
func (k *KAD) FinalizePolygons() {
has_err := false
corner_segments := 20
if k.Fillet == 0 {
corner_segments = 0 // square corner
}
for _, layer := range k.Result.Plates {
// handle layer specific details
switch {
case layer == OPENLAYER:
usb_shift := k.Case.UsbLocation
if usb_shift < -(k.Width/2 - k.Case.EdgeWidth - k.Case.UsbWidth/2) {
usb_shift = -(k.Width/2 - k.Case.EdgeWidth - k.Case.UsbWidth/2)
}
if usb_shift > (k.Width/2 - k.Case.EdgeWidth - k.Case.UsbWidth/2) {
usb_shift = k.Width/2 - k.Case.EdgeWidth - k.Case.UsbWidth/2
}
usb_width := k.Case.UsbWidth
if usb_width > (k.Width - 2*k.Case.EdgeWidth - 2*k.Kerf) {
usb_width = k.Width - 2*k.Case.EdgeWidth - 2*k.Kerf
usb_shift = 0
}
c := Point{k.LayoutCenter.X + usb_shift, k.DMZ + k.TopPad/2 + k.Kerf}
usb_pts := Path{
{-usb_width/2 + k.Kerf, -k.TopPad/2 - k.Kerf}, {usb_width/2 - k.Kerf, -k.TopPad/2 - k.Kerf},
{usb_width/2 - k.Kerf, k.TopPad/2 + k.Kerf}, {-usb_width/2 + k.Kerf, k.TopPad/2 + k.Kerf}}
usb_pts.Rel(c)
// overlap top side so it is completely removed...
usb_pts[0].Y -= 1
usb_pts[1].Y -= 1
k.Layers[layer].CutPolys = append(k.Layers[layer].CutPolys, usb_pts)
mid_pts := Path{
{-k.Width/2 + 2*k.Kerf + k.Case.LeftWidth, -k.Height/2 + 2*k.Kerf + k.Case.TopWidth},
{k.Width/2 - 2*k.Kerf - k.Case.RightWidth, -k.Height/2 + 2*k.Kerf + k.Case.TopWidth},
{k.Width/2 - 2*k.Kerf - k.Case.RightWidth, k.Height/2 - 2*k.Kerf - k.Case.BottomWidth},
{-k.Width/2 + 2*k.Kerf + k.Case.LeftWidth, k.Height/2 - 2*k.Kerf - k.Case.BottomWidth}}
mid_pts.Rel(k.CaseCenter)
k.Layers[layer].CutPolys = append(k.Layers[layer].CutPolys, mid_pts)
case layer == CLOSEDLAYER:
mid_pts := Path{
{-k.Width/2 + 2*k.Kerf + k.Case.LeftWidth, -k.Height/2 + 2*k.Kerf + k.Case.TopWidth},
{k.Width/2 - 2*k.Kerf - k.Case.RightWidth, -k.Height/2 + 2*k.Kerf + k.Case.TopWidth},
{k.Width/2 - 2*k.Kerf - k.Case.RightWidth, k.Height/2 - 2*k.Kerf - k.Case.BottomWidth},
{-k.Width/2 + 2*k.Kerf + k.Case.LeftWidth, k.Height/2 - 2*k.Kerf - k.Case.BottomWidth}}
mid_pts.Rel(k.CaseCenter)
k.Layers[layer].CutPolys = append(k.Layers[layer].CutPolys, mid_pts)
}
// starting point for the 'keep' polygon
keep_poly := RoundRectanglePolygon(k.DMZ+(k.Width/2), k.DMZ+(k.Height/2),
k.Width, k.Height, k.Fillet, corner_segments)
k.Layers[layer].KeepPolys = []Path{keep_poly}
// handle custom polygons added to this drawing
for _, cp := range k.CustomPolygons {
if in_strings(layer, cp.Layers) { // apply this custom polygon to this layer
paths := k.ParsePoints(cp.Points, cp.RelTo, true)
paths = append(paths, k.ParsePoints(cp.Points, cp.RelAbs, false)...)
polygons := make([]Path, 0)
if len(paths) > 0 && len(paths[0]) > 0 {
switch cp.Polygon {
case "custom-circle":
for _, path := range paths {
for _, pt := range path {
polygons = append(polygons, CirclePolygon(pt.X, pt.Y, cp.Diameter/2, 20))
}
}
break
case "custom-superellipse":
for _, path := range paths {
for _, pt := range path {
polygons = append(polygons, SuperellipsePolygon(pt.X, pt.Y, cp.Radius, 20))
}
}
break
case "custom-rectangle":
for _, path := range paths {
for _, pt := range path {
polygons = append(polygons, RoundRectanglePolygon(pt.X, pt.Y, cp.Width, cp.Height, 0, 0))
}
}
break
case "custom-rounded-rectangle":
for _, path := range paths {
for _, pt := range path {
polygons = append(polygons, RoundRectanglePolygon(pt.X, pt.Y, cp.Width, cp.Height, cp.Radius, 20))
}
}
break
case "custom-path":
for _, path := range paths {
if len(path) > 2 {
polygons = append(polygons, path)
}
}
break
}
}
// apply the polygons to the kad
if len(polygons) > 0 {
switch cp.Op {
case "add":
k.Layers[layer].KeepPolys = append(k.Layers[layer].KeepPolys, polygons...)
break
case "cut":
k.Layers[layer].CutPolys = append(k.Layers[layer].CutPolys, polygons...)
break
}
}
}
}
// union all of the cut polygons to make sure we don't have any crossing cut paths
if len(k.Layers[layer].CutPolys) > 0 { // union all inside
c := clipper.NewClipper(clipper.IoNone)
c.AddPath(Path{}.ToClipperPath(), clipper.PtSubject, true)
for _, poly := range k.Layers[layer].CutPolys {
c.AddPath(poly.ToClipperPath(), clipper.PtClip, true)
}
solution, ok := c.Execute1(clipper.CtUnion, clipper.PftNonZero, clipper.PftNonZero)
if !ok {
log.Printf("ERROR drawing layout: %s, %s", k.Hash, layer)
log.Printf("ERROR drawing inner union...\nCutPolys: %#v", k.Layers[layer].CutPolys)
has_err = true
} else {
cut_union := make([]Path, 0)
for _, cpath := range solution {
cut_union = append(cut_union, FromClipperPath(cpath))
}
k.Layers[layer].CutPolys = cut_union
}
}
// union all of the keep polygons to make sure we don't have any crossing keep paths
if len(k.Layers[layer].KeepPolys) > 0 { // union all inside
c := clipper.NewClipper(clipper.IoNone)
c.AddPath(Path{}.ToClipperPath(), clipper.PtSubject, true)
for _, poly := range k.Layers[layer].KeepPolys {
c.AddPath(poly.ToClipperPath(), clipper.PtClip, true)
}
solution, ok := c.Execute1(clipper.CtUnion, clipper.PftNonZero, clipper.PftNonZero)
if !ok {
log.Printf("ERROR drawing layout: %s, %s", k.Hash, layer)
log.Printf("ERROR drawing inner union...\nKeepPolys: %#v", k.Layers[layer].KeepPolys)
has_err = true
} else {
keep_union := make([]Path, 0)
for _, cpath := range solution {
keep_union = append(keep_union, FromClipperPath(cpath))
}
k.Layers[layer].KeepPolys = keep_union
}
}
// at this point we have everything we need to evaluate if any cut polygons cross the exterior keep boundary
// get the surface areas before we 'cut' from the 'keep' paths
k.Result.Details[layer].Area = SurfaceArea(k.Layers[layer].KeepPolys) - SurfaceArea(k.Layers[layer].CutPolys)
// get the difference when we do the cut from keep
if len(k.Layers[layer].CutPolys) > 0 { // difference with cuts
c := clipper.NewClipper(clipper.IoNone)
for _, poly := range k.Layers[layer].KeepPolys {
c.AddPath(poly.ToClipperPath(), clipper.PtSubject, true)
}
for _, poly := range k.Layers[layer].CutPolys {
c.AddPath(poly.ToClipperPath(), clipper.PtClip, true)
}
solution, ok := c.Execute1(clipper.CtDifference, clipper.PftNonZero, clipper.PftNonZero)
if !ok {
log.Printf("ERROR drawing layout: %s, %s", k.Hash, layer)
log.Printf("ERROR drawing outer / inner difference...\nKeepPolys: %#v\nCutPolys: %#v",
k.Layers[layer].KeepPolys, k.Layers[layer].CutPolys)
has_err = true
} else {
keep_polys := make([]Path, 0)
for _, cpath := range solution {
keep_polys = append(keep_polys, FromClipperPath(cpath))
}
k.Layers[layer].KeepPolys = keep_polys
}
}
}
if has_err {
log.Printf("ERROR Context (raw layout):\n%#v", k.RawLayout)
}
}
// Parse the points passed in for custom polygons
func (k *KAD) ParsePoints(points_str, rel_to_str string, rel_center bool) []Path {
get_points := func(point_str string) Path {
points := make(Path, 0)
point_str = strings.ToLower(strings.Replace(point_str, " ", "", -1)) // remove spaces and make lower case
point_ary := strings.Split(point_str, ";")
for _, pt := range point_ary {
pt = strings.Replace(pt, "[", "", -1)
pt = strings.Replace(pt, "]", "", -1)
pts := strings.Split(pt, ",")
point_error := false
if len(pts) == 2 {
params := make(map[string]interface{}, 8)
params["x"] = k.Width / 2
params["y"] = k.Height / 2
var err error
var x_exp, y_exp *govaluate.EvaluableExpression
var x_val, y_val interface{}
x_exp, err = govaluate.NewEvaluableExpression(pts[0])
if err != nil {
log.Printf("ERROR Govaluating expression: %s", pts[0])
point_error = true
}
if !point_error {
x_val, err = x_exp.Evaluate(params)
if err != nil {
log.Printf("ERROR Govaluating '%s' w/ params: %#v", pts[0], params)
point_error = true
}
}
if !point_error {
y_exp, err = govaluate.NewEvaluableExpression(pts[1])
if err != nil {
log.Printf("ERROR Govaluating expression: %s", pts[1])
point_error = true
}
}
if !point_error {
y_val, err = y_exp.Evaluate(params)
if err != nil {
log.Printf("ERROR Govaluating '%s' w/ params: %#v", pts[1], params)
point_error = true
}
}
if !point_error {
points = append(points, Point{x_val.(float64), y_val.(float64)})
}
}
}
return points
}
paths := make([]Path, 0)
points := get_points(points_str)
rel_to := get_points(rel_to_str)
if rel_center {
rel_to.Rel(k.CaseCenter)
}
if len(rel_to) > 0 {
for p := range rel_to {
rel_points := points.Copy()
rel_points.Rel(rel_to[p])
paths = append(paths, rel_points)
}
}
return paths
}
// create a rectangle as a polygon with optional rounded corners.
// set 'r' and 's' to zero to have a non-rounded corner.
func RoundRectanglePolygon(cx, cy, w, h, r float64, s int) Path {
// make a rounded corner
corner := func(x, y, r, a float64, s int, ps *Path) {
n := float64(s)
p := &Point{x, y}
*ps = append(*ps, *p)
la := radians(90 - (90.0 / (2 * n))) // angle to determine the segment length
for j := 1.0; j < n+1; j++ {
sa := radians(90 - ((90.0 / (2 * n)) * (2*j - 1)) + a) // angle of the vector of each successive segment
p.X += 2 * r * math.Cos(la) * math.Sin(sa)
p.Y += 2 * r * math.Cos(la) * math.Cos(sa)
*ps = append(*ps, *p)
}
}
// draw the rounded rectangle
pts := make(Path, 0)
corner((cx + w/2 - r), (cy - h/2), r, 0, s, &pts)
corner((cx + w/2), (cy + h/2 - r), r, -90, s, &pts)
corner((cx - w/2 + r), (cy + h/2), r, 180, s, &pts)
corner((cx - w/2), (cy - h/2 + r), r, 90, s, &pts)
return pts
}
// create a circle as a polygon with each quarter made up of 's' segments.
func CirclePolygon(cx, cy, r float64, s int) Path {
// make a circle
circle := func(x, y, r float64, s int, ps *Path) {
n := float64(s)
p := &Point{x, y}
*ps = append(*ps, *p)
la := radians(90 - (90.0 / (2 * n))) // angle to determine the segment length
for j := 1.0; j < 4*n; j++ {
sa := radians(90 - ((90.0 / (2 * n)) * (2*j - 1))) // angle of the vector of each successive segment
p.X += 2 * r * math.Cos(la) * math.Sin(sa)
p.Y += 2 * r * math.Cos(la) * math.Cos(sa)
*ps = append(*ps, *p)
}
}
// draw the circle
pts := make(Path, 0)
circle(cx, cy-r, r, s, &pts)
return pts
}
// create a superellipse as a polygon with each quarter made up of 's' segments.
func SuperellipsePolygon(cx, cy, r float64, s int) Path {
// make a concave quadrant
quadrant := func(x, y, r, a float64, s int, ps *Path) {
n := float64(s)
p := &Point{x, y}
*ps = append(*ps, *p)
la := radians(90 + (90.0 / (2 * n))) // angle to determine the segment length
for j := 1.0; j < n+1; j++ {
sa := radians(90 + ((90.0 / (2 * n)) * (2*j - 1)) + a) // angle of the vector of each successive segment
p.X += 2 * r * math.Cos(la) * math.Sin(sa)
p.Y += 2 * r * math.Cos(la) * math.Cos(sa)
*ps = append(*ps, *p)
}
}
// draw the superellipse
pts := make(Path, 0)
quadrant((cx), (cy - r), r, 90, s, &pts)
quadrant((cx + r), (cy), r, 0, s, &pts)
quadrant((cx), (cy + r), r, -90, s, &pts)
quadrant((cx - r), (cy), r, 180, s, &pts)
return pts
}
func SurfaceArea(paths []Path) float64 {
sa := 0.0
for _, path := range paths {
area := 0.0
i := len(path) - 1
for j := 0; j < len(path); j++ {
area += (path[i].X + path[j].X) * (path[i].Y - path[j].Y)
i = j //set previous to current for next pass
}
sa += math.Abs(area / 2)
}
return sa
}
// convert degrees to radians
func radians(deg float64) float64 {
return (deg * math.Pi) / 180
}