-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.go
591 lines (531 loc) · 12.1 KB
/
struct.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
package nest
import (
"log"
"math"
"fmt"
. "github.com/mojinfu/point"
)
func (this *SVG) logIfDebug(v ...interface{}) {
if this.config.IfDebug {
log.Println(v...)
}
}
type Polygon = []*Point
type polygonWithOffset struct {
Polygon []*Point
offsetx float64
offsety float64
}
type PolyNode struct {
OriginPolygon []*Point //原始多边形
CleanedPolygon []*Point //简化 未膨胀的多边形
polygonAfterRotaion []*Point //简化后 膨胀后的多边形
polygonBeforeRotation []*Point //简化后 膨胀后的多边形
EndPolygon []*Point //简化后 未膨胀 旋转 平移后的多边形
children []*PolyNode
parent *PolyNode
}
func (this *PolyNode)String()string{
str:=""
for index:=range this.polygonAfterRotaion{
str += fmt.Sprintf("(%v,%v)\n", this.polygonAfterRotaion[index].X,this.polygonAfterRotaion[index].Y)
}
return str
}
func CleanIntPolygon(this []*IntPoint, b float64) []*IntPoint {
// if b < 0 {
// b = 1.415 //? saya
// }
fFloat := 0.0
if b < 0 {
rectbounds := getIntPolygonBounds(this)
l:=rectbounds.height +rectbounds.width
fFloat= float64(l)/100.0
//b = 0.3 //?1.415 原始值 saya
}else{
fFloat = b * b
}
c := len(this)
if 0 == c {
return []*IntPoint{}
}
pointChain := []*OutFloatPtStruct{}
for f := 0; f < c; f++ {
pointChain = append(pointChain, &OutFloatPtStruct{})
}
for f := 0; f < c; f++ {
pointChain[f].Pt = &Point{X: float64(this[f].X), Y: float64(this[f].Y)}
pointChain[f].Next = pointChain[(f+1)%c]
pointChain[f].Next.Prev = pointChain[f]
pointChain[f].Idx = 0
}
eOutPt := pointChain[0]
for 0 == eOutPt.Idx && eOutPt.Next != eOutPt.Prev {
if PointsAreClose(eOutPt.Pt, eOutPt.Prev.Pt, fFloat) {
temp := eOutPt.Prev
ExcludeOp(eOutPt)
eOutPt = temp.Next
eOutPt.Idx = 0
c = c - 1
//log.Println("相近删除")
} else {
if PointsAreClose(eOutPt.Prev.Pt, eOutPt.Next.Pt, fFloat) {
temp := eOutPt.Prev
ExcludeOp(eOutPt.Next)
ExcludeOp(eOutPt)
eOutPt = temp
eOutPt.Idx = 0
c -= 2
// log.Println("尖角删除")
} else {
if SlopesNearCollinear(eOutPt.Prev.Pt, eOutPt.Pt, eOutPt.Next.Pt, fFloat)&&false {
// if this.isWart {
// if c > 4 {
// temp := eOutPt.Prev
// ExcludeOp(eOutPt)
// eOutPt = temp
// eOutPt.Idx = 0
// c = c - 1
// } else {
// break
// }
// } else {
temp := eOutPt.Prev
ExcludeOp(eOutPt)
eOutPt = temp
eOutPt.Idx = 0
c = c - 1
//log.Println("同线删除")
// }
} else {
eOutPt.Idx = 1
eOutPt = eOutPt.Next
}
}
}
}
if 3 > c {
c = 0
}
g := []*IntPoint{}
for f := 0; f < c; f++ {
g = append(g, &IntPoint{X: int64(eOutPt.Pt.X), Y: int64(eOutPt.Pt.Y)})
eOutPt = eOutPt.Next
}
// //多边形订正
// fmt.Println("多边形订正")
// for index := range g {
// fmt.Println(g[index].Print())
// }
IsIn := func(intPoint *IntPoint) (int, int) {
start := -1
end := -1
for index := range g {
if g[index].X == intPoint.X && g[index].Y == intPoint.Y {
if start < 0 {
start = index
continue
} else if end < 0 {
end = index
continue
}
}
}
return start, end
}
for i := range g {
start, end := IsIn(g[i])
if start >= 0 && end >= 0 {
//log.Println("clean works~")
if end-start > len(g)/2 {
return g[start:end]
} else {
cleanPol := []*IntPoint{}
for j := range g {
if j <= start || j > end {
cleanPol = append(cleanPol, g[j])
}
}
return cleanPol
}
}
}
return g
}
func (this *PolygonStruct) CleanPolygon(b float64) {
if len(this.RootPoly.polygonBeforeRotation) != 0 {
panic(" polygonBeforeRotation")
}
var cleanNearCollinear bool = true
var cleanClosePoint bool = true
var cleanSharpAngel bool = false
fFloat := 0.0
if b < 0 {
rectbounds := getPolygonBounds(this.RootPoly.OriginPolygon)
l:=rectbounds.height +rectbounds.width
fFloat= l/30
//b = 0.3 //?1.415 原始值 saya
}else{
fFloat = b * b
}
c := len(this.RootPoly.OriginPolygon)
if 0 == c {
return
}
pointChain := []*OutFloatPtStruct{}
for f := 0; f < c; f++ {
pointChain = append(pointChain, &OutFloatPtStruct{})
}
for f := 0; f < c; f++ {
pointChain[f].Pt = this.RootPoly.OriginPolygon[f]
pointChain[f].Next = pointChain[(f+1)%c]
pointChain[f].Next.Prev = pointChain[f]
pointChain[f].Idx = 0
}
//",c)
eOutPt := pointChain[0]
for 0 == eOutPt.Idx && eOutPt.Next != eOutPt.Prev {
if PointsAreClose(eOutPt.Pt, eOutPt.Prev.Pt, fFloat) && cleanClosePoint{
temp := eOutPt.Prev
ExcludeOp(eOutPt)
eOutPt = temp.Next
eOutPt.Idx = 0
c = c - 1
//log.Println("相近删除")
} else {
if PointsAreClose(eOutPt.Prev.Pt, eOutPt.Next.Pt, fFloat)&&cleanSharpAngel {
temp := eOutPt.Prev
ExcludeOp(eOutPt.Next)
ExcludeOp(eOutPt)
eOutPt = temp
eOutPt.Idx = 0
c -= 2
// log.Println("尖角删除")
} else {
if SlopesNearCollinear(eOutPt.Prev.Pt, eOutPt.Pt, eOutPt.Next.Pt, 0)&&cleanNearCollinear {
if this.isWart {
if c > 4 {
temp := eOutPt.Prev
ExcludeOp(eOutPt)
eOutPt = temp
eOutPt.Idx = 0
c = c - 1
} else {
break
}
} else {
temp := eOutPt.Prev
ExcludeOp(eOutPt)
eOutPt = temp
eOutPt.Idx = 0
c = c - 1//原先同线删除逻辑
//log.Println("同线删除")
}
} else {
eOutPt.Idx = 1
eOutPt = eOutPt.Next
}
}
}
}
if 3 > c {
c = 0
}
g := []*Point{}
for f := 0; f < c; f++ {
g = append(g, &Point{X: eOutPt.Pt.X, Y: eOutPt.Pt.Y})
eOutPt = eOutPt.Next
}
this.RootPoly.polygonBeforeRotation = g
this.RootPoly.CleanedPolygon =CopyPointList(this.RootPoly.polygonBeforeRotation)
}
type PolygonStructSlice []*PolygonStruct
func (this PolygonStructSlice) Len() int {
return len(this)
}
func (this PolygonStructSlice) Less(i, j int) bool {
if this[i].isWart && !this[j].isWart {
return true
}
if !this[i].isWart && this[j].isWart {
return false
}
return math.Abs(PolygonArea(this[i].RootPoly.polygonBeforeRotation)) > math.Abs(PolygonArea(this[j].RootPoly.polygonBeforeRotation))
}
func (this PolygonStructSlice) Swap(i, j int) {
this[i], this[j] = this[j], this[i]
}
const ClipperBase_horizontal float64 = -9007199254740992
const ClipperBase_Skip int = -2
const ClipperBase_Unassigned int = -1
const ClipperBase_tolerance float64 = 1E-20
const ClipperBase_loRange int64 = 47453132
const ClipperBase_hiRange int64 = 0xfffffffffffff
type IntPolygon = []*IntPoint
func Abs(windCnt int64) int64 {
if windCnt >= 0 {
return windCnt
} else {
return -1 * windCnt
}
}
func Max(a, b int64) int64 {
if a > b {
return a
} else {
return b
}
}
func Min(a, b int64) int64 {
if a > b {
return b
} else {
return a
}
}
type OutPtStruct struct {
Idx int
Pt *IntPoint
Prev *OutPtStruct
Next *OutPtStruct
}
type OutFloatPtStruct struct {
Idx int
Pt *Point
Prev *OutFloatPtStruct
Next *OutFloatPtStruct
}
// func NewOutPt()*OutPtStruct{
// return &OutPtStruct{
// OutPt2: &TEdgeStruct{},
// OutPt1: &TEdgeStruct{},
// OffPt:&IntPoint{
// },
// }
// }
type OutRecStruct struct {
Idx int
IsOpen bool
IsHole bool
PolyNode *IntPoint
BottomPt *OutPtStruct
Pts *OutPtStruct
FirstLeft *OutRecStruct
}
type JoinStruct struct {
OutPt2 *OutPtStruct
OutPt1 *OutPtStruct
OffPt *IntPoint
}
func NewJoin() *JoinStruct {
return &JoinStruct{
OutPt2: &OutPtStruct{},
OutPt1: &OutPtStruct{},
OffPt: &IntPoint{},
}
}
func IntPointReverse(a IntPolygon) IntPolygon {
b := IntPolygon{}
for index := range a {
Point := &IntPoint{
X: a[index].X,
Y: a[index].Y,
}
b = append(b, Point)
}
for i := 0; i <= (len(b)/2)-1; i++ {
bPoiont := &IntPoint{}
bPoiont.X = b[len(b)-1-i].X
bPoiont.Y = b[len(b)-1-i].Y
b[len(b)-1-i].X = b[i].X
b[len(b)-1-i].Y = b[i].Y
b[i].X = bPoiont.X
b[i].Y = bPoiont.Y
}
return b
}
func PolygonReverse(a Polygon) Polygon {
b := Polygon{}
for index := range a {
Point := &Point{
X: a[index].X,
Y: a[index].Y,
}
b = append(b, Point)
}
for i := 0; i <= (len(b)/2)-1; i++ {
bPoiont := &Point{}
bPoiont.X = b[len(b)-1-i].X
bPoiont.Y = b[len(b)-1-i].Y
b[len(b)-1-i].X = b[i].X
b[len(b)-1-i].Y = b[i].Y
b[i].X = bPoiont.X
b[i].Y = bPoiont.Y
}
return b
}
var ClipperOffset_def_arc_tolerance float64 = 0.25
var ClipperOffset_two_pi float64 = 6.28318530717959
type PolyType int8
const ptSubject PolyType = 0
const ptClip PolyType = 1
type ClipType int8
const ctIntersection ClipType = 0
const ctUnion ClipType = 1
const ctDifference ClipType = 2
const ctXor ClipType = 3
type PolyFillType = int64
const pftEvenOdd PolyFillType = 0
const pftNonZero PolyFillType = 1
const pftPositive PolyFillType = 2
const pftNegative PolyFillType = 3
type JoinType = int64
const jtSquare JoinType = 0
const jtRound JoinType = 1
const jtMiter JoinType = 2
type EndType = int64
const etOpenSquare EndType = 0
const etOpenRound EndType = 1
const etOpenButt EndType = 2
const etClosedLine EndType = 3
const etClosedPolygon EndType = 4
type IntersectNodeStruct struct {
Edge1 *TEdgeStruct
Edge2 *TEdgeStruct
Pt *IntPoint
}
type EdgeSide int8
const esLeft EdgeSide = 0
const esRight EdgeSide = 1
type Direction int8
const dRightToLeft Direction = 0
const dLeftToRight Direction = 1
type ConfigStruct struct {
PaperSavePath string
ClipperScale int64
CurveTolerance float64
ExploreConcave bool
MutationRate int
PopulationSize int
Rotations int
//Spacing float64
UseHoles bool
IfDebug bool
IfDraw bool
PartPartSpacing float64
BinPartSpacing float64
CanNotPutLoopMaxNum int
LoopMaxNum int
RunTimeOut int
LengthWeight float64
WidthWeight float64
MutilThread int
}
const clipperScaleTimes int64 = 10000
var PublicConfig *ConfigStruct = &ConfigStruct{
ClipperScale: clipperScaleTimes, // 扩大倍数
CurveTolerance: 0.3,//原来默认值0.3 saya
ExploreConcave: false, //searchEdges
MutationRate: 20,
PopulationSize: 2,
Rotations: 360,
// Spacing: 0,
UseHoles: false,
IfDebug: false,
PartPartSpacing: 50,
BinPartSpacing: 0,
CanNotPutLoopMaxNum :10,
}
type Path = []*Point
type PolyNodeStruct struct {
m_Parent *PolyNodeStruct
m_polygon IntPolygon
m_endtype int64
m_jointype int64
m_Index int
m_Childs []*PolyNodeStruct
IsOpen bool
}
func NewPolyNode() *PolyNodeStruct {
return &PolyNodeStruct{
m_Parent: &PolyNodeStruct{},
m_polygon: IntPolygon{},
m_endtype: 0,
m_jointype: 0,
m_Index: 0,
m_Childs: []*PolyNodeStruct{},
IsOpen: false,
}
}
func (this *PolyNodeStruct) Childs() []*PolyNodeStruct {
return this.m_Childs
}
func (this *PolyNodeStruct) AddChild(a *PolyNodeStruct) {
b := len(this.m_Childs)
this.m_Childs = append(this.m_Childs, a)
a.m_Parent = this
a.m_Index = b
}
type ClipperOffsetStruct struct {
// "undefined" == typeof a && (a = 2);
// "undefined" == typeof b && (b = d.ClipperOffset.def_arc_tolerance);
m_destPolys []IntPolygon
m_srcPoly IntPolygon
m_destPoly IntPolygon
m_normals Polygon
m_StepsPerRad float64
m_miterLim float64
m_cos float64
m_sin float64
m_sinA float64
m_delta float64
m_lowest *IntPoint
m_polyNodes *PolyNodeStruct
MiterLimit float64
ArcTolerance float64
// m_lowest.X int64
}
func NewClipperOffset(a float64, b float64) *ClipperOffsetStruct {
this := &ClipperOffsetStruct{}
this.m_destPolys = []IntPolygon{}
this.m_srcPoly = IntPolygon{}
this.m_destPoly = IntPolygon{}
//this.m_normals = [];
this.m_StepsPerRad = 0
this.m_miterLim = 0
this.m_cos = 0
this.m_sin = 0
this.m_sinA = 0
this.m_delta = 0
this.m_lowest = &IntPoint{}
this.m_polyNodes = NewPolyNode()
this.MiterLimit = a
this.ArcTolerance = b
this.m_lowest.X = -1
return this
}
type IntRectStruct struct {
left int64
top int64
right int64
bottom int64
}
func NewIntRect_4(a, b, c, d int64) *IntRectStruct {
this := &IntRectStruct{}
this.left = a
this.top = b
this.right = c
this.bottom = d
return this
}
type OneTouchStruct struct {
Type int
A int
B int
}
type vectorsStruct struct {
x float64
y float64
start *Point
end *Point
}