-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
polygon.cpp
1714 lines (1604 loc) · 63.1 KB
/
polygon.cpp
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2020 Lucas Heitzmann Gabrielli.
This file is part of gdstk, distributed under the terms of the
Boost Software License - Version 1.0. See the accompanying
LICENSE file or <http://www.boost.org/LICENSE_1_0.txt>
*/
#include "polygon.h"
#include <float.h>
#include <inttypes.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "allocator.h"
#include "clipper_tools.h"
#include "font.h"
#include "repetition.h"
#include "sort.h"
#include "utils.h"
#include "vec.h"
namespace gdstk {
void Polygon::print(bool all) const {
printf("Polygon <%p>, count %" PRIu64 ", layer %" PRIu32 ", datatype %" PRIu32
", properties <%p>, owner <%p>\n",
this, point_array.count, get_layer(tag), get_type(tag), properties, owner);
if (all) {
printf("Points: ");
point_array.print(true);
}
properties_print(properties);
repetition.print();
}
void Polygon::clear() {
point_array.clear();
repetition.clear();
properties_clear(properties);
}
void Polygon::copy_from(const Polygon& polygon) {
tag = polygon.tag;
point_array.copy_from(polygon.point_array);
repetition.copy_from(polygon.repetition);
properties = properties_copy(polygon.properties);
}
double Polygon::area() const {
if (point_array.count < 3) return 0;
double result = 0;
Vec2* p = point_array.items;
Vec2 v0 = *p++;
Vec2 v1 = *p++ - v0;
for (uint64_t num = point_array.count - 2; num > 0; num--) {
Vec2 v2 = *p++ - v0;
result += v1.cross(v2);
v1 = v2;
}
if (repetition.type != RepetitionType::None) result *= repetition.get_count();
return 0.5 * fabs(result);
}
double Polygon::signed_area() const {
if (point_array.count < 3) return 0;
double result = 0;
Vec2* p = point_array.items;
Vec2 v0 = *p++;
Vec2 v1 = *p++ - v0;
for (uint64_t num = point_array.count - 2; num > 0; num--) {
Vec2 v2 = *p++ - v0;
result += v1.cross(v2);
v1 = v2;
}
return 0.5 * result;
}
// Based on algorithm 7 from: Kai Hormann, Alexander Agathos, “The point in
// polygon problem for arbitrary polygons,” Computational Geometry, Volume 20,
// Issue 3, 2001, Pages 131-144, ISSN 0925-7721.
// https://doi.org/10.1016/S0925-7721(01)00012-8
bool Polygon::contain(const Vec2 point) const {
if (point_array.count == 0) {
return false;
}
Vec2 p0 = point_array[point_array.count - 1];
if (point == p0) {
return true;
}
int64_t winding = 0;
Vec2* v = point_array.items;
for (uint64_t i = point_array.count; i > 0; i--, v++) {
Vec2 p1 = *v;
if (p1.y == point.y &&
(p1.x == point.x || (p0.y == point.y && (p1.x > point.x) == (p0.x < point.x)))) {
return true;
}
if ((p0.y < point.y) != (p1.y < point.y)) {
if (p0.x >= point.x) {
if (p1.x > point.x) {
winding += p1.y > p0.y ? 1 : -1;
} else {
double det = (p0 - point).cross(p1 - point);
if (det == 0) {
return true;
}
if ((det > 0) == (p1.y > p0.y)) {
winding += p1.y > p0.y ? 1 : -1;
}
}
} else if (p1.x > point.x) {
double det = (p0 - point).cross(p1 - point);
if (det == 0) {
return true;
}
if ((det > 0) == (p1.y > p0.y)) {
winding += p1.y > p0.y ? 1 : -1;
}
}
}
p0 = p1;
}
return winding != 0;
}
bool Polygon::contain_all(const Array<Vec2>& points) const {
Vec2 min, max;
bounding_box(min, max);
for (uint64_t i = 0; i < points.count; i++) {
Vec2 point = points[i];
if (point.x < min.x || point.x > max.x || point.y < min.y || point.x > max.x) return false;
}
for (uint64_t i = 0; i < points.count; i++) {
if (!contain(points[i])) return false;
}
return true;
}
bool Polygon::contain_any(const Array<Vec2>& points) const {
Vec2 min, max;
bounding_box(min, max);
for (uint64_t i = 0; i < points.count; i++) {
Vec2 point = points[i];
if (point.x >= min.x && point.x <= max.x && point.y >= min.y && point.x <= max.x &&
contain(point))
return true;
}
return false;
}
void Polygon::bounding_box(Vec2& min, Vec2& max) const {
min.x = min.y = DBL_MAX;
max.x = max.y = -DBL_MAX;
Vec2* p = point_array.items;
for (uint64_t num = point_array.count; num > 0; num--, p++) {
if (p->x < min.x) min.x = p->x;
if (p->x > max.x) max.x = p->x;
if (p->y < min.y) min.y = p->y;
if (p->y > max.y) max.y = p->y;
}
if (repetition.type != RepetitionType::None) {
Array<Vec2> offsets = {};
repetition.get_extrema(offsets);
Vec2* off = offsets.items;
Vec2 min0 = min;
Vec2 max0 = max;
for (uint64_t i = offsets.count; i > 0; i--, off++) {
if (min0.x + off->x < min.x) min.x = min0.x + off->x;
if (max0.x + off->x > max.x) max.x = max0.x + off->x;
if (min0.y + off->y < min.y) min.y = min0.y + off->y;
if (max0.y + off->y > max.y) max.y = max0.y + off->y;
}
offsets.clear();
}
}
void Polygon::translate(const Vec2 v) {
Vec2* p = point_array.items;
for (uint64_t num = point_array.count; num > 0; num--) *p++ += v;
}
void Polygon::scale(const Vec2 scale_factor, const Vec2 center) {
Vec2* p = point_array.items;
for (uint64_t num = point_array.count; num > 0; num--, p++)
*p = (*p - center) * scale_factor + center;
}
void Polygon::mirror(const Vec2 p0, const Vec2 p1) {
Vec2 v = p1 - p0;
double tmp = v.length_sq();
if (tmp == 0) return;
Vec2 r = v * (2 / tmp);
Vec2 p2 = p0 * 2;
Vec2* p = point_array.items;
for (uint64_t num = point_array.count; num > 0; num--, p++)
*p = v * (*p - p0).inner(r) - *p + p2;
}
void Polygon::rotate(double angle, const Vec2 center) {
double ca = cos(angle);
double sa = sin(angle);
Vec2* p = point_array.items;
for (uint64_t num = point_array.count; num > 0; num--, p++) {
Vec2 q = *p - center;
p->x = q.x * ca - q.y * sa + center.x;
p->y = q.x * sa + q.y * ca + center.y;
}
}
void Polygon::transform(double magnification, bool x_reflection, double rotation,
const Vec2 origin) {
double ca = cos(rotation);
double sa = sin(rotation);
Vec2* p = point_array.items;
for (uint64_t num = point_array.count; num > 0; num--, p++) {
Vec2 q = *p * magnification;
if (x_reflection) q.y = -q.y;
p->x = q.x * ca - q.y * sa + origin.x;
p->y = q.x * sa + q.y * ca + origin.y;
}
}
void Polygon::fillet(const Array<double> radii, double tolerance) {
if (point_array.count < 3) return;
Array<Vec2> old_pts;
old_pts.copy_from(point_array);
point_array.count = 0;
const uint64_t old_size = old_pts.count;
uint64_t j = 0;
if (old_pts[old_size - 1] == old_pts[0]) {
j = old_size - 1;
while (old_pts[j - 1] == old_pts[j]) j -= 1;
}
const uint64_t last = j;
uint64_t i = j == 0 ? old_size - 1 : j - 1;
Vec2 p0 = old_pts[i];
Vec2 p1 = old_pts[j];
Vec2 v0 = p1 - p0;
double len0 = v0.normalize();
uint64_t k = last + 1;
while (k != last) {
k = j == old_size - 1 ? 0 : j + 1;
while (old_pts[k] == old_pts[j]) k += 1;
const Vec2 p2 = old_pts[k];
Vec2 v1 = p2 - p1;
const double len1 = v1.normalize();
const double theta = acos(v0.inner(v1));
if (theta > 1e-12) {
const double tant = tan(0.5 * theta);
const double cost = cos(0.5 * theta);
Vec2 dv = v1 - v0;
const double fac = 1 / (cost * dv.length());
dv *= fac;
double radius = radii[j % radii.count];
double max_len = radius * tant;
if (max_len > 0.5 * (len0 - tolerance)) {
max_len = 0.5 * (len0 - tolerance);
radius = max_len / tant;
}
if (max_len > 0.5 * (len1 - tolerance)) {
max_len = 0.5 * (len1 - tolerance);
radius = max_len / tant;
}
double a0 = (v0 * -tant - dv).angle();
double a1 = (v1 * tant - dv).angle();
if (a1 - a0 > M_PI)
a1 -= 2 * M_PI;
else if (a1 - a0 < -M_PI)
a1 += 2 * M_PI;
uint64_t n = 1;
if (radius > 0) {
n = arc_num_points(fabs(a1 - a0), radius, tolerance);
if (n == 0) n = 1;
}
point_array.ensure_slots(n);
if (n == 1) {
point_array.append_unsafe(p1);
} else {
for (uint64_t l = 0; l < n; l++) {
const double a = a0 + l * (a1 - a0) / (n - 1.0);
Vec2 cosi = {cos(a), sin(a)};
point_array.append_unsafe(p1 + (dv + cosi) * radius);
}
}
} else {
point_array.append(p1);
}
// i = j;
j = k;
p0 = p1;
p1 = p2;
v0 = v1;
len0 = len1;
}
old_pts.clear();
}
void Polygon::fracture(uint64_t max_points, double precision, Array<Polygon*>& result) const {
if (max_points <= 4) return;
Polygon* poly = (Polygon*)allocate_clear(sizeof(Polygon));
poly->point_array.copy_from(point_array);
result.append(poly);
double scaling = 1.0 / precision;
for (uint64_t i = 0; i < result.count;) {
Polygon* subj = result[i];
uint64_t num_points = subj->point_array.count;
if (num_points <= max_points) {
i++;
continue;
}
Vec2 min;
Vec2 max;
subj->bounding_box(min, max);
const uint64_t num_cuts = num_points / max_points;
const double frac = num_points / (num_cuts + 1.0);
Array<double> cuts = {};
cuts.ensure_slots(num_cuts);
cuts.count = num_cuts;
bool x_axis;
double* coords = (double*)allocate(sizeof(double) * num_points);
if (max.x - min.x > max.y - min.y) {
double* x = coords;
double* px = x;
Vec2* pt = subj->point_array.items;
for (uint64_t j = 0; j < num_points; j++) (*px++) = (pt++)->x;
sort(x, num_points);
x_axis = true;
px = cuts.items;
for (uint64_t j = 0; j < num_cuts; j++) (*px++) = x[(uint64_t)((j + 1.0) * frac + 0.5)];
} else {
double* y = coords;
double* py = y;
Vec2* pt = subj->point_array.items;
for (uint64_t j = 0; j < num_points; j++) (*py++) = (pt++)->y;
sort(y, num_points);
x_axis = false;
py = cuts.items;
for (uint64_t j = 0; j < num_cuts; j++) (*py++) = y[(uint64_t)((j + 1.0) * frac + 0.5)];
}
free_allocation(coords);
Array<Polygon*>* chopped =
(Array<Polygon*>*)allocate_clear((cuts.count + 1) * sizeof(Array<Polygon*>));
slice(*subj, cuts, x_axis, scaling, chopped);
cuts.clear();
subj->point_array.clear();
result.remove_unordered(i);
free_allocation(subj);
uint64_t total = 0;
for (uint64_t j = 0; j <= num_cuts; j++) total += chopped[j].count;
result.ensure_slots(total);
for (uint64_t j = 0; j <= num_cuts; j++) {
result.extend(chopped[j]);
chopped[j].clear();
}
free_allocation(chopped);
}
for (uint64_t i = 0; i < result.count; i++) {
poly = result[i];
poly->tag = tag;
poly->repetition.copy_from(repetition);
poly->properties = properties_copy(properties);
}
}
void Polygon::apply_repetition(Array<Polygon*>& result) {
if (repetition.type == RepetitionType::None) return;
Array<Vec2> offsets = {};
repetition.get_offsets(offsets);
repetition.clear();
// Skip first offset (0, 0)
Vec2* offset_p = offsets.items + 1;
result.ensure_slots(offsets.count - 1);
for (uint64_t offset_count = offsets.count - 1; offset_count > 0; offset_count--) {
Polygon* poly = (Polygon*)allocate_clear(sizeof(Polygon));
poly->copy_from(*this);
poly->translate(*offset_p++);
result.append_unsafe(poly);
}
offsets.clear();
return;
}
ErrorCode Polygon::to_gds(FILE* out, double scaling) const {
ErrorCode error_code = ErrorCode::NoError;
if (point_array.count < 3) return error_code;
uint16_t buffer_start[] = {
4, 0x0800, 6, 0x0D02, (uint16_t)get_layer(tag), 6, 0x0E02, (uint16_t)get_type(tag)};
uint16_t buffer_end[] = {4, 0x1100};
big_endian_swap16(buffer_start, COUNT(buffer_start));
big_endian_swap16(buffer_end, COUNT(buffer_end));
uint64_t total = point_array.count + 1;
if (total > 8190) {
fputs(
"[GDSTK] Polygons with more than 8190 are not supported by the official GDSII specification. This GDSII file might not be compatible with all readers.\n",
stderr);
error_code = ErrorCode::UnofficialSpecification;
}
Array<int32_t> coords = {};
coords.ensure_slots(2 * total);
coords.count = 2 * total;
Vec2 zero = {0, 0};
Array<Vec2> offsets = {};
if (repetition.type != RepetitionType::None) {
repetition.get_offsets(offsets);
} else {
offsets.count = 1;
offsets.items = &zero;
}
double* offset_p = (double*)offsets.items;
for (uint64_t offset_count = offsets.count; offset_count > 0; offset_count--) {
fwrite(buffer_start, sizeof(uint16_t), COUNT(buffer_start), out);
double offset_x = *offset_p++;
double offset_y = *offset_p++;
int32_t* c = coords.items;
Vec2* p = point_array.items;
for (uint64_t j = point_array.count; j > 0; j--) {
*c++ = (int32_t)lround((offset_x + p->x) * scaling);
*c++ = (int32_t)lround((offset_y + p->y) * scaling);
p++;
}
*c++ = coords[0];
*c++ = coords[1];
big_endian_swap32((uint32_t*)coords.items, coords.count);
uint64_t i0 = 0;
while (i0 < total) {
uint64_t i1 = total < i0 + 8190 ? total : i0 + 8190;
uint16_t buffer_pts[] = {(uint16_t)(4 + 8 * (i1 - i0)), 0x1003};
big_endian_swap16(buffer_pts, COUNT(buffer_pts));
fwrite(buffer_pts, sizeof(uint16_t), COUNT(buffer_pts), out);
fwrite(coords.items + 2 * i0, sizeof(int32_t), 2 * (i1 - i0), out);
i0 = i1;
}
ErrorCode err = properties_to_gds(properties, out);
if (err != ErrorCode::NoError) error_code = err;
fwrite(buffer_end, sizeof(uint16_t), COUNT(buffer_end), out);
}
if (repetition.type != RepetitionType::None) offsets.clear();
coords.clear();
return error_code;
}
static bool is_rectangle(const Array<IntVec2> points, IntVec2& corner, IntVec2& size) {
if (points.count == 4 && ((points[0].x == points[1].x && points[1].y == points[2].y &&
points[2].x == points[3].x && points[3].y == points[0].y) ||
(points[0].y == points[1].y && points[1].x == points[2].x &&
points[2].y == points[3].y && points[3].x == points[0].x))) {
for (uint64_t i = 0; i < 2; i++) {
int64_t e0 = points[0].e[i];
int64_t e2 = points[2].e[i];
if (e0 < e2) {
size.e[i] = e2 - e0;
corner.e[i] = e0;
} else {
size.e[i] = e0 - e2;
corner.e[i] = e2;
}
}
return true;
}
return false;
}
// 0 <= type < 26 matches the CTRAPEZOID types, 26 is for horizontal TRAPEZOID, 27 for vertical
static bool is_trapezoid(const Array<IntVec2> points, uint8_t& type, IntVec2& corner, IntVec2& size,
int64_t& delta_a, int64_t& delta_b) {
if (points.count == 4) {
IntVec2 p, q, r, s;
if (points[0].x == points[1].x && points[2].x == points[3].x) {
if (points[0].x < points[2].x) {
if (points[0].y < points[1].y || points[3].y < points[2].y) {
p = points[0];
q = points[1];
r = points[3];
s = points[2];
} else {
p = points[1];
q = points[0];
r = points[2];
s = points[3];
}
} else {
if (points[0].y < points[1].y || points[3].y < points[2].y) {
p = points[3];
q = points[2];
r = points[0];
s = points[1];
} else {
p = points[2];
q = points[3];
r = points[1];
s = points[0];
}
}
type = 27;
} else if (points[3].x == points[0].x && points[1].x == points[2].x) {
if (points[3].x < points[1].x) {
if (points[3].y < points[0].y || points[2].y < points[1].y) {
p = points[3];
q = points[0];
r = points[2];
s = points[1];
} else {
p = points[0];
q = points[3];
r = points[1];
s = points[2];
}
} else {
if (points[3].y < points[0].y || points[2].y < points[1].y) {
p = points[2];
q = points[1];
r = points[3];
s = points[0];
} else {
p = points[1];
q = points[2];
r = points[0];
s = points[3];
}
}
type = 27;
} else if (points[0].y == points[1].y && points[2].y == points[3].y) {
if (points[0].y < points[2].y) {
if (points[0].x < points[1].x || points[3].x < points[2].x) {
p = points[3];
q = points[2];
r = points[0];
s = points[1];
} else {
p = points[2];
q = points[3];
r = points[1];
s = points[0];
}
} else {
if (points[0].x < points[1].x || points[3].x < points[2].x) {
p = points[0];
q = points[1];
r = points[3];
s = points[2];
} else {
p = points[1];
q = points[0];
r = points[2];
s = points[3];
}
}
type = 26;
} else if (points[3].y == points[0].y && points[1].y == points[2].y) {
if (points[3].y < points[1].y) {
if (points[3].x < points[0].x || points[2].x < points[1].x) {
p = points[2];
q = points[1];
r = points[3];
s = points[0];
} else {
p = points[1];
q = points[2];
r = points[0];
s = points[3];
}
} else {
if (points[3].x < points[0].x || points[2].x < points[1].x) {
p = points[3];
q = points[0];
r = points[2];
s = points[1];
} else {
p = points[0];
q = points[3];
r = points[1];
s = points[2];
}
}
type = 26;
} else {
return false;
}
if (type == 26) {
corner.x = p.x < r.x ? p.x : r.x;
size.x = (s.x > q.x ? s.x : q.x) - corner.x;
delta_a = p.x - r.x;
delta_b = q.x - s.x;
corner.y = r.y;
size.y = p.y - r.y;
if (delta_a == 0) {
if (delta_b == 0) {
type = size.x == size.y ? 25 : 24;
} else if (delta_b == -size.y) {
type = 0;
} else if (delta_b == size.y) {
type = 1;
}
} else if (delta_a == -size.y) {
if (delta_b == 0) {
type = 3;
} else if (delta_b == -size.y) {
type = 7;
} else if (delta_b == size.y) {
type = 5;
}
} else if (delta_a == size.y) {
if (delta_b == 0) {
type = 2;
} else if (delta_b == -size.y) {
type = 4;
} else if (delta_b == size.y) {
type = 6;
}
}
} else {
corner.y = p.y < r.y ? p.y : r.y;
size.y = (s.y > q.y ? s.y : q.y) - corner.y;
delta_a = p.y - r.y;
delta_b = q.y - s.y;
corner.x = p.x;
size.x = r.x - p.x;
if (delta_a == 0) {
if (delta_b == 0) {
type = size.x == size.y ? 25 : 24;
} else if (delta_b == -size.x) {
type = 9;
} else if (delta_b == size.x) {
type = 8;
}
} else if (delta_a == -size.x) {
if (delta_b == 0) {
type = 10;
} else if (delta_b == -size.x) {
type = 14;
} else if (delta_b == size.x) {
type = 12;
}
} else if (delta_a == size.x) {
if (delta_b == 0) {
type = 11;
} else if (delta_b == -size.x) {
type = 13;
} else if (delta_b == size.x) {
type = 15;
}
}
}
return true;
} else if (points.count == 3) {
IntVec2 p, q, r;
// Sort p < q < r
if (points[0] < points[1]) {
if (points[0] < points[2]) {
p = points[0];
if (points[1] < points[2]) {
q = points[1];
r = points[2];
} else {
q = points[2];
r = points[1];
}
} else {
p = points[2];
q = points[0];
r = points[1];
}
} else {
if (points[1] < points[2]) {
p = points[1];
if (points[0] < points[2]) {
q = points[0];
r = points[2];
} else {
q = points[2];
r = points[0];
}
} else {
p = points[2];
q = points[1];
r = points[0];
}
}
corner.x = p.x;
size.x = r.x - p.x;
corner.y = p.y < q.y ? (p.y < r.y ? p.y : r.y) : (q.y < r.y ? q.y : r.y);
size.y = (p.y > q.y ? (p.y > r.y ? p.y : r.y) : (q.y > r.y ? q.y : r.y)) - corner.y;
if (size.x == size.y) {
if (q.x == p.x) {
if (r.y == p.y) {
type = 16;
return true;
} else if (r.y == q.y) {
type = 17;
return true;
}
return false;
} else if (r.x == q.x) {
if (p.y == q.y) {
type = 18;
return true;
} else if (p.y == r.y) {
type = 19;
return true;
}
return false;
}
return false;
} else if (size.x == 2 * size.y && p.y == r.y && q.x == corner.x + size.y) {
type = q.y > p.y ? 20 : 21;
return true;
} else if (size.y == 2 * size.x) {
if (p.x == q.x && r.y == corner.y + size.x) {
type = 22;
return true;
} else if (q.x == r.x && p.y == corner.y + size.x) {
type = 23;
return true;
}
return false;
}
return false;
}
return false;
}
#define CIRCLE_DETECTION_LSQ_COEFFICIENTS 4
static bool is_circle(const Array<Vec2> point_array, double tolerance, Vec2& center,
double& radius) {
if (point_array.count <= CIRCLE_DETECTION_LSQ_COEFFICIENTS) return false;
double coef_a = 0;
double coef_b = 0;
double coef_m = 0;
double res_a = 0;
double res_b = 0;
Vec2 ref = point_array[0];
double ref_length_sq = ref.length_sq();
for (uint64_t i = 1; i <= CIRCLE_DETECTION_LSQ_COEFFICIENTS; i++) {
uint64_t j = i * (point_array.count - 1) / CIRCLE_DETECTION_LSQ_COEFFICIENTS;
Vec2 ab = 2 * (point_array[j] - ref);
double r = point_array[j].length_sq() - ref_length_sq;
coef_a += ab.x * ab.x;
coef_b += ab.y * ab.y;
coef_m += ab.x * ab.y;
res_a += ab.x * r;
res_b += ab.y * r;
}
double den = coef_a * coef_b - coef_m * coef_m;
if (fabs(den) < GDSTK_PARALLEL_EPS) return false;
center.x = (coef_b * res_a - coef_m * res_b) / den;
center.y = (coef_a * res_b - coef_m * res_a) / den;
// printf("Center: (%lf, %lf)\n", center.x, center.y);
radius = 0;
for (uint64_t i = 0; i <= CIRCLE_DETECTION_LSQ_COEFFICIENTS; i++) {
uint64_t j = i * (point_array.count - 1) / CIRCLE_DETECTION_LSQ_COEFFICIENTS;
radius += (point_array[j] - center).length();
}
radius /= 1 + CIRCLE_DETECTION_LSQ_COEFFICIENTS;
// printf("Radius: %lf\n", radius);
if (point_array.count < arc_num_points(2 * M_PI, radius, tolerance)) return false;
double radius_sq = radius * radius;
double neighbor_distance_sq = tolerance + 2 * sqrt(2 * tolerance * (radius - tolerance));
neighbor_distance_sq *= neighbor_distance_sq;
Vec2* pt = point_array.items;
Vec2* last = point_array.items + point_array.count - 1;
for (uint64_t i = point_array.count; i > 0; i--) {
if (fabs((*pt - center).length_sq() - radius_sq) >= tolerance ||
(*pt - *last).length_sq() >= neighbor_distance_sq) {
return false;
}
last = pt++;
}
return true;
}
ErrorCode Polygon::to_oas(OasisStream& out, OasisState& state) const {
ErrorCode error_code = ErrorCode::NoError;
Vec2 center;
double radius;
IntVec2 corner, size;
int64_t delta_a, delta_b;
uint8_t type;
bool has_repetition = repetition.get_count() > 1;
Array<IntVec2> points = {};
scale_and_round_array(point_array, state.scaling, points);
if ((state.config_flags & OASIS_CONFIG_DETECT_RECTANGLES) &&
is_rectangle(points, corner, size)) {
bool is_square = size.x == size.y;
uint8_t info;
if (is_square) {
info = 0xDB;
} else {
info = 0x7B;
}
if (has_repetition) info |= 0x04;
oasis_putc((int)OasisRecord::RECTANGLE, out);
oasis_putc(info, out);
oasis_write_unsigned_integer(out, get_layer(tag));
oasis_write_unsigned_integer(out, get_type(tag));
oasis_write_unsigned_integer(out, size.x);
if (!is_square) oasis_write_unsigned_integer(out, size.y);
oasis_write_integer(out, corner.x);
oasis_write_integer(out, corner.y);
// if (is_square)
// printf("SQUARE @ (%ld, %ld) w %ld\n", corner.x, corner.y, size.x);
// else
// printf("RECTANGLE @ (%ld, %ld) w %ld, h %ld\n", corner.x, corner.y, size.x,
// size.y);
} else if ((state.config_flags & OASIS_CONFIG_DETECT_TRAPEZOIDS) &&
is_trapezoid(points, type, corner, size, delta_a, delta_b)) {
if (type > 25) {
uint8_t info = type == 26 ? 0x7B : 0xFB;
if (has_repetition) info |= 0x04;
if (delta_a == 0) {
oasis_putc((int)OasisRecord::TRAPEZOID_B, out);
} else if (delta_b == 0) {
oasis_putc((int)OasisRecord::TRAPEZOID_A, out);
} else {
oasis_putc((int)OasisRecord::TRAPEZOID_AB, out);
}
oasis_putc(info, out);
oasis_write_unsigned_integer(out, get_layer(tag));
oasis_write_unsigned_integer(out, get_type(tag));
oasis_write_unsigned_integer(out, size.x);
oasis_write_unsigned_integer(out, size.y);
if (delta_a == 0) {
oasis_write_1delta(out, delta_b);
// printf("TRAPEZOID_B %s @ (%ld, %ld) w %ld, h %ld, db %ld\n",
// type == 26 ? "hor" : "ver", corner.x, corner.y, size.x, size.y,
// delta_b);
} else if (delta_b == 0) {
oasis_write_1delta(out, delta_a);
// printf("TRAPEZOID_A %s @ (%ld, %ld) w %ld, h %ld, da %ld\n",
// type == 26 ? "hor" : "ver", corner.x, corner.y, size.x, size.y,
// delta_a);
} else {
oasis_write_1delta(out, delta_a);
oasis_write_1delta(out, delta_b);
// printf("TRAPEZOID_AB %s @ (%ld, %ld) w %ld, h %ld, da %ld, db %ld\n",
// type == 26 ? "hor" : "ver", corner.x, corner.y, size.x, size.y,
// delta_a, delta_b);
}
} else {
uint8_t info = 0x9B;
bool use_h = type < 16 || type == 20 || type == 21 || type == 24;
bool use_w = type != 20 && type != 21;
if (use_h) info |= 0x20;
if (use_w) info |= 0x40;
if (has_repetition) info |= 0x04;
oasis_putc((int)OasisRecord::CTRAPEZOID, out);
oasis_putc(info, out);
oasis_write_unsigned_integer(out, get_layer(tag));
oasis_write_unsigned_integer(out, get_type(tag));
oasis_putc(type, out);
if (use_w) oasis_write_unsigned_integer(out, size.x);
if (use_h) oasis_write_unsigned_integer(out, size.y);
// if (use_w && use_h)
// printf("CTRAPEZOID %hu @ (%ld, %ld) w %ld, h %ld\n", type, corner.x,
// corner.y,
// size.x, size.y);
// else if (use_w)
// printf("CTRAPEZOID %hu @ (%ld, %ld) w %ld\n", type, corner.x, corner.y,
// size.x);
// else
// printf("CTRAPEZOID %hu @ (%ld, %ld) h %ld\n", type, corner.x, corner.y,
// size.y);
}
oasis_write_integer(out, corner.x);
oasis_write_integer(out, corner.y);
} else if (state.circle_tolerance > 0 &&
is_circle(point_array, state.circle_tolerance, center, radius)) {
uint8_t info = 0x3B;
if (has_repetition) info |= 0x04;
oasis_putc((int)OasisRecord::CIRCLE, out);
oasis_putc(info, out);
oasis_write_unsigned_integer(out, get_layer(tag));
oasis_write_unsigned_integer(out, get_type(tag));
oasis_write_unsigned_integer(out, (uint64_t)llround(radius * state.scaling));
oasis_write_integer(out, (int64_t)llround(center.x * state.scaling));
oasis_write_integer(out, (int64_t)llround(center.y * state.scaling));
// printf("CIRCLE @ (%lf, %lf) r %lf\n", center.x, center.y, radius);
} else {
uint8_t info = 0x3B;
if (has_repetition) info |= 0x04;
oasis_putc((int)OasisRecord::POLYGON, out);
oasis_putc(info, out);
oasis_write_unsigned_integer(out, get_layer(tag));
oasis_write_unsigned_integer(out, get_type(tag));
oasis_write_point_list(out, points, true);
oasis_write_integer(out, points[0].x);
oasis_write_integer(out, points[0].y);
// printf("POLYGON @ (%ld, %ld)\n", points[0].x, points[0].y);
}
if (has_repetition) oasis_write_repetition(out, repetition, state.scaling);
ErrorCode err = properties_to_oas(properties, out, state);
if (err != ErrorCode::NoError) error_code = err;
points.clear();
return error_code;
}
ErrorCode Polygon::to_svg(FILE* out, double scaling, uint32_t precision) const {
if (point_array.count < 3) return ErrorCode::NoError;
char double_buffer[GDSTK_DOUBLE_BUFFER_COUNT];
fprintf(out, "<polygon id=\"%p\" class=\"l%" PRIu32 "d%" PRIu32 "\" points=\"", this,
get_layer(tag), get_type(tag));
Vec2* p = point_array.items;
for (uint64_t j = 0; j < point_array.count - 1; j++) {
fputs(double_print(p->x * scaling, precision, double_buffer, COUNT(double_buffer)), out);
fputc(',', out);
fputs(double_print(p->y * scaling, precision, double_buffer, COUNT(double_buffer)), out);
fputc(' ', out);
p++;
}
fputs(double_print(p->x * scaling, precision, double_buffer, COUNT(double_buffer)), out);
fputc(',', out);
fputs(double_print(p->y * scaling, precision, double_buffer, COUNT(double_buffer)), out);
fputs("\"/>\n", out);
if (repetition.type != RepetitionType::None) {
Array<Vec2> offsets = {};
repetition.get_offsets(offsets);
double* offset_p = (double*)(offsets.items + 1);
for (uint64_t offset_count = offsets.count - 1; offset_count > 0; offset_count--) {
double offset_x = *offset_p++;
double offset_y = *offset_p++;
fprintf(out, "<use href=\"#%p\" x=\"", this);
fputs(double_print(offset_x * scaling, precision, double_buffer, COUNT(double_buffer)),
out);
fputs("\" y=\"", out);
fputs(double_print(offset_y * scaling, precision, double_buffer, COUNT(double_buffer)),
out);
fputs("\"/>\n", out);
}
offsets.clear();
}
return ErrorCode::NoError;
}
Polygon rectangle(const Vec2 corner1, const Vec2 corner2, Tag tag) {
Polygon result = {};
result.tag = tag;
result.point_array.ensure_slots(4);
result.point_array.count = 4;
result.point_array[0] = corner1;
result.point_array[1] = Vec2{corner2.x, corner1.y};
result.point_array[2] = corner2;
result.point_array[3] = Vec2{corner1.x, corner2.y};
return result;
};
Polygon cross(const Vec2 center, double full_size, double arm_width, Tag tag) {
const double len = full_size / 2;
const double half_width = arm_width / 2;
Polygon result = {};
result.tag = tag;
result.point_array.ensure_slots(12);
result.point_array.count = 12;
result.point_array[0] = center + Vec2{len, half_width};
result.point_array[1] = center + Vec2{half_width, half_width};
result.point_array[2] = center + Vec2{half_width, len};