-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathshape.cpp
1103 lines (1005 loc) · 34.7 KB
/
shape.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
#include "packingsolver/irregular/shape.hpp"
#include <cmath>
using namespace packingsolver;
using namespace packingsolver::irregular;
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// Point /////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
std::string Point::to_string() const
{
return "(" + std::to_string(x) + ", " + std::to_string(y) + ")";
}
Point irregular::operator+(
const Point& point_1,
const Point& point_2)
{
return {point_1.x + point_2.x, point_1.y + point_2.y};
}
Point irregular::operator-(
const Point& point_1,
const Point& point_2)
{
return {point_1.x - point_2.x, point_1.y - point_2.y};
}
LengthDbl irregular::norm(
const Point& vector)
{
return std::sqrt(vector.x * vector.x + vector.y * vector.y);
}
LengthDbl irregular::squared_norm(
const Point& vector)
{
return vector.x * vector.x + vector.y * vector.y;
}
LengthDbl irregular::distance(
const Point& point_1,
const Point& point_2)
{
return norm(point_2 - point_1);
}
LengthDbl irregular::squared_distance(
const Point& point_1,
const Point& point_2)
{
return squared_norm(point_2 - point_1);
}
LengthDbl irregular::dot_product(
const Point& vector_1,
const Point& vector_2)
{
return vector_1.x * vector_2.x + vector_1.y * vector_2.y;
}
LengthDbl irregular::cross_product(
const Point& vector_1,
const Point& vector_2)
{
return vector_1.x * vector_2.y - vector_2.x * vector_1.y;
}
Point& Point::shift(
LengthDbl x,
LengthDbl y)
{
this->x += x;
this->y += y;
return *this;
}
Point Point::rotate(
Angle angle) const
{
if (equal(angle, 0.0)) {
return *this;
} else if (equal(angle, 180)) {
Point point_out;
point_out.x = -x;
point_out.y = -y;
return point_out;
} else if (equal(angle, 90)) {
Point point_out;
point_out.x = -y;
point_out.y = x;
return point_out;
} else if (equal(angle, 270)) {
Point point_out;
point_out.x = y;
point_out.y = -x;
return point_out;
} else {
Point point_out;
angle = M_PI * angle / 180;
point_out.x = std::cos(angle) * x - std::sin(angle) * y;
point_out.y = std::sin(angle) * x + std::cos(angle) * y;
return point_out;
}
}
Point Point::rotate_radians(
Angle angle) const
{
Point point_out;
point_out.x = std::cos(angle) * x - std::sin(angle) * y;
point_out.y = std::sin(angle) * x + std::cos(angle) * y;
return point_out;
}
Point Point::rotate_radians(
const Point& center,
Angle angle) const
{
Point point_out;
point_out.x = center.x
+ std::cos(angle) * (this->x - center.x)
- std::sin(angle) * (this->y - center.y);
point_out.y = center.y
+ std::sin(angle) * (this->x - center.x)
+ std::cos(angle) * (this->y - center.y);
return point_out;
}
Point Point::axial_symmetry_identity_line() const
{
Point point_out;
point_out.x = y;
point_out.y = x;
return point_out;
}
Point Point::axial_symmetry_y_axis() const
{
Point point_out;
point_out.x = -x;
point_out.y = y;
return point_out;
}
Point Point::axial_symmetry_x_axis() const
{
Point point_out;
point_out.x = x;
point_out.y = -y;
return point_out;
}
Angle irregular::angle_radian(
const Point& vector)
{
Angle a = std::atan2(vector.y, vector.x);
if (a < 0)
a += 2 * M_PI;
return a;
}
Angle irregular::angle_radian(
const Point& vector_1,
const Point& vector_2)
{
Angle a = atan2(vector_2.y, vector_2.x) - atan2(vector_1.y, vector_1.x);
if (a < 0)
a += 2 * M_PI;
return a;
}
int irregular::counter_clockwise(
const Point& point_1,
const Point& point_2,
const Point& point_3)
{
AreaDbl area = (point_2.x - point_1.x) * (point_3.y - point_1.y)
- (point_2.y - point_1.y) * (point_3.x - point_1.x);
if (strictly_greater(area, 0)) {
return -1;
} else if (strictly_lesser(area, 0)) {
return 1;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// ShapeElement /////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
LengthDbl ShapeElement::length() const
{
switch (type) {
case ShapeElementType::LineSegment:
return distance(this->start, this->end);
case ShapeElementType::CircularArc:
LengthDbl r = distance(center, start);
if (anticlockwise) {
return angle_radian(start - center, end - center) * r;
} else {
return angle_radian(end - center, start - center) * r;
}
}
return -1;
}
std::string ShapeElement::to_string() const
{
switch (type) {
case ShapeElementType::LineSegment: {
return "LineSegment start " + start.to_string()
+ " end " + end.to_string();
} case ShapeElementType::CircularArc: {
return "CircularArc start " + start.to_string()
+ " end " + end.to_string()
+ " center " + center.to_string()
+ ((anticlockwise)? " anticlockwise": " clockwise");
}
}
return "";
}
nlohmann::json ShapeElement::to_json() const
{
nlohmann::json json;
json["type"] = element2str(type);
json["start"]["x"] = start.x;
json["start"]["y"] = start.y;
json["end"]["x"] = end.x;
json["end"]["y"] = end.y;
if (type == ShapeElementType::CircularArc) {
json["center"]["x"] = center.x;
json["center"]["y"] = center.y;
json["anticlockwise"] = anticlockwise;
}
return json;
}
ShapeElement ShapeElement::rotate(
Angle angle) const
{
ShapeElement element_out = *this;
element_out.start = start.rotate(angle);
element_out.end = end.rotate(angle);
element_out.center = center.rotate(angle);
return element_out;
}
ShapeElement ShapeElement::axial_symmetry_identity_line() const
{
ShapeElement element_out = *this;
element_out.start = end.axial_symmetry_identity_line();
element_out.end = start.axial_symmetry_identity_line();
element_out.center = center.axial_symmetry_identity_line();
element_out.anticlockwise = !anticlockwise;
return element_out;
}
ShapeElement ShapeElement::axial_symmetry_x_axis() const
{
ShapeElement element_out = *this;
element_out.start = end.axial_symmetry_x_axis();
element_out.end = start.axial_symmetry_x_axis();
element_out.center = center.axial_symmetry_x_axis();
element_out.anticlockwise = !anticlockwise;
return element_out;
}
ShapeElement ShapeElement::axial_symmetry_y_axis() const
{
ShapeElement element_out = *this;
element_out.start = end.axial_symmetry_y_axis();
element_out.end = start.axial_symmetry_y_axis();
element_out.center = center.axial_symmetry_y_axis();
element_out.anticlockwise = !anticlockwise;
return element_out;
}
ShapeElementType irregular::str2element(const std::string& str)
{
if (str == "LineSegment"
|| str == "line_segment"
|| str == "L"
|| str == "l") {
return ShapeElementType::LineSegment;
} else if (str == "CircularArc"
|| str == "circular_arc"
|| str == "C"
|| str == "c") {
return ShapeElementType::CircularArc;
} else {
throw std::invalid_argument("");
return ShapeElementType::LineSegment;
}
}
std::string irregular::element2str(ShapeElementType type)
{
switch (type) {
case ShapeElementType::LineSegment: {
return "LineSegment";
} case ShapeElementType::CircularArc: {
return "CircularArc";
}
}
return "";
}
char irregular::element2char(ShapeElementType type)
{
switch (type) {
case ShapeElementType::LineSegment: {
return 'L';
} case ShapeElementType::CircularArc: {
return 'C';
}
}
return ' ';
}
std::vector<ShapeElement> irregular::approximate_circular_arc_by_line_segments(
const ShapeElement& circular_arc,
ElementPos number_of_line_segments,
bool outer)
{
if (circular_arc.type != ShapeElementType::CircularArc) {
throw std::runtime_error(
"packingsolver::irregular::circular_arc_to_line_segments: "
"input element must be of type CircularArc; "
"circular_arc.type: " + element2str(circular_arc.type) + ".");
}
if (!outer && number_of_line_segments < 1) {
throw std::runtime_error(
"packingsolver::irregular::circular_arc_to_line_segments: "
"at least 1 line segment is needed to inner approximate a circular arc; "
"outer: " + std::to_string(outer) + "; "
"number_of_line_segments: " + std::to_string(number_of_line_segments) + ".");
}
Angle angle = (circular_arc.anticlockwise)?
angle_radian(
circular_arc.start - circular_arc.center,
circular_arc.end - circular_arc.center):
angle_radian(
circular_arc.end - circular_arc.center,
circular_arc.start - circular_arc.center);
if ((outer && circular_arc.anticlockwise)
|| (!outer && !circular_arc.anticlockwise)) {
if (angle < M_PI && number_of_line_segments < 2) {
throw std::runtime_error(
"packingsolver::irregular::circular_arc_to_line_segments: "
"at least 2 line segments are needed to approximate the circular arc; "
"circular_arc: " + circular_arc.to_string() + "; "
"outer: " + std::to_string(outer) + "; "
"angle: " + std::to_string(angle) + "; "
"number_of_line_segments: " + std::to_string(number_of_line_segments) + ".");
} else if (angle >= M_PI && number_of_line_segments < 3) {
throw std::runtime_error(
"packingsolver::irregular::circular_arc_to_line_segments: "
"at least 3 line segments are needed to approximate the circular arc; "
"circular_arc: " + circular_arc.to_string() + "; "
"outer: " + std::to_string(outer) + "; "
"angle: " + std::to_string(angle) + "; "
"number_of_line_segments: " + std::to_string(number_of_line_segments) + ".");
}
}
std::vector<ShapeElement> line_segments;
LengthDbl radius = distance(circular_arc.center, circular_arc.start);
Point point_prev = circular_arc.start;
Point point_circle_prev = circular_arc.start;
for (ElementPos line_segment_id = 0;
line_segment_id < number_of_line_segments - 1;
++line_segment_id) {
Angle angle_cur = (angle * (line_segment_id + 1)) / (number_of_line_segments - 1);
if (!circular_arc.anticlockwise)
angle_cur *= -1;
Point point_circle = circular_arc.start.rotate_radians(
circular_arc.center,
angle_cur);
Point point_cur;
if ((outer && !circular_arc.anticlockwise) || (!outer && circular_arc.anticlockwise)) {
point_cur = point_circle;
} else {
// https://en.wikipedia.org/wiki/Tangent_lines_to_circles#Cartesian_equation
// https://en.wikipedia.org/wiki/Intersection_(geometry)#Two_lines
// The tangent line of the circle at (x1, y1) has Cartesian equation
// (x - x1)(x1 - xc) + (y - y1)(y1 - yc) = 0
// (x1 - xc) * x + (y1 - yc) * y - x1 * (x1 - xc) - y1 * (y1 - yc) = 0
// At (x2, y2)
// (x2 - xc) * x + (y2 - yc) * y - x2 * (x2 - xc) - y2 * (y1 - yc) = 0
LengthDbl a1 = (point_circle_prev.x - circular_arc.center.x);
LengthDbl b1 = (point_circle_prev.y - circular_arc.center.y);
LengthDbl c1 = point_circle_prev.x * (point_circle_prev.x - circular_arc.center.x)
+ point_circle_prev.y * (point_circle_prev.y - circular_arc.center.y);
LengthDbl a2 = (point_circle.x - circular_arc.center.x);
LengthDbl b2 = (point_circle.y - circular_arc.center.y);
LengthDbl c2 = point_circle.x * (point_circle.x - circular_arc.center.x)
+ point_circle.y * (point_circle.y - circular_arc.center.y);
point_cur.x = (c1 * b2 - c2 * b1) / (a1 * b2 - a2 * b1);
point_cur.y = (a1 * c2 - a2 * c1) / (a1 * b2 - a2 * b1);
}
ShapeElement line_segment;
line_segment.start = point_prev;
line_segment.end = point_cur;
line_segment.type = ShapeElementType::LineSegment;
line_segments.push_back(line_segment);
point_prev = point_cur;
point_circle_prev = point_circle;
}
ShapeElement line_segment;
line_segment.start = point_prev;
line_segment.end = circular_arc.end;
line_segment.type = ShapeElementType::LineSegment;
line_segments.push_back(line_segment);
return line_segments;
}
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// Shape /////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
std::string irregular::shape2str(ShapeType type)
{
switch (type) {
case ShapeType::Circle: {
return "C";
} case ShapeType::Square: {
return "S";
} case ShapeType::Rectangle: {
return "R";
} case ShapeType::Polygon: {
return "P";
} case ShapeType::PolygonWithHoles: {
return "PH";
} case ShapeType::MultiPolygon: {
return "MP";
} case ShapeType::MultiPolygonWithHoles: {
return "MPH";
} case ShapeType::GeneralShape: {
return "G";
}
}
return "";
}
bool Shape::is_circle() const
{
return (elements.size() == 1
&& elements.front().type == ShapeElementType::CircularArc);
}
bool Shape::is_square() const
{
if (elements.size() != 4)
return false;
auto it_prev = std::prev(elements.end());
for (auto it = elements.begin(); it != elements.end(); ++it) {
if (it->type != ShapeElementType::LineSegment)
return false;
// Check angle.
Angle theta = angle_radian(it_prev->start - it_prev->end, it->end - it->start);
if (!equal(theta, M_PI / 2))
return false;
// Check length.
if (!equal(it->length(), elements[0].length()))
return false;
it_prev = it;
}
return true;
}
bool Shape::is_rectangle() const
{
if (elements.size() != 4)
return false;
auto it_prev = std::prev(elements.end());
for (auto it = elements.begin(); it != elements.end(); ++it) {
if (it->type != ShapeElementType::LineSegment)
return false;
// Check angle.
Angle theta = angle_radian(it_prev->start - it_prev->end, it->end - it->start);
if (!equal(theta, M_PI / 2))
return false;
it_prev = it;
}
return true;
}
bool Shape::is_polygon() const
{
for (auto it = elements.begin(); it != elements.end(); ++it)
if (it->type != ShapeElementType::LineSegment)
return false;
return true;
}
AreaDbl Shape::compute_area() const
{
AreaDbl area = 0.0;
for (const ShapeElement& element: elements) {
area += cross_product(element.start, element.end);
// Handle circular arcs.
if (element.type == ShapeElementType::CircularArc) {
LengthDbl radius = distance(element.center, element.start);
Angle theta = angle_radian(element.center - element.start, element.center - element.end);
if (element.anticlockwise) {
area += radius * radius * ((!(element.start == element.end))? theta: 2.0 * M_PI);
} else {
area -= radius * radius * ((!(element.start == element.end))? 2.0 * M_PI - theta: 2.0 * M_PI);
}
}
}
return area / 2;
}
std::pair<Point, Point> Shape::compute_min_max(
Angle angle,
bool mirror) const
{
LengthDbl x_min = std::numeric_limits<LengthDbl>::infinity();
LengthDbl x_max = -std::numeric_limits<LengthDbl>::infinity();
LengthDbl y_min = std::numeric_limits<LengthDbl>::infinity();
LengthDbl y_max = -std::numeric_limits<LengthDbl>::infinity();
for (const ShapeElement& element: elements) {
Point point = (!mirror)?
element.start.rotate(angle):
element.start.axial_symmetry_y_axis().rotate(angle);
x_min = std::min(x_min, point.x);
x_max = std::max(x_max, point.x);
y_min = std::min(y_min, point.y);
y_max = std::max(y_max, point.y);
if (element.type == ShapeElementType::CircularArc) {
LengthDbl radius = distance(element.center, element.start);
Angle starting_angle = irregular::angle_radian(element.start - element.center);
Angle ending_angle = irregular::angle_radian(element.end - element.center);
if (!element.anticlockwise)
std::swap(starting_angle, ending_angle);
if (starting_angle <= ending_angle) {
if (starting_angle <= M_PI
&& M_PI <= ending_angle) {
x_min = std::min(x_min, element.center.x - radius);
}
if (starting_angle == 0)
x_max = std::max(x_max, element.center.x + radius);
if (starting_angle <= 3 * M_PI / 2
&& 3 * M_PI / 2 <= ending_angle) {
y_min = std::min(y_min, element.center.y - radius);
}
if (starting_angle <= M_PI / 2
&& M_PI / 2 <= ending_angle) {
y_max = std::max(y_max, element.center.y + radius);
}
} else { // starting_angle > ending_angle
if (starting_angle <= M_PI
|| ending_angle <= M_PI) {
x_min = std::min(x_min, element.center.x - radius);
}
x_max = std::max(x_max, element.center.x + radius);
if (starting_angle <= 3 * M_PI / 4
|| ending_angle <= 3 * M_PI / 4) {
y_min = std::min(y_min, element.center.y - radius);
}
if (starting_angle <= M_PI / 2
|| ending_angle <= M_PI / 2) {
y_max = std::max(y_max, element.center.y + radius);
}
}
}
}
return {{x_min, y_min}, {x_max, y_max}};
}
Shape& Shape::shift(
LengthDbl x,
LengthDbl y)
{
for (ShapeElement& element: elements) {
element.start.shift(x, y);
element.end.shift(x, y);
element.center.shift(x, y);
}
return *this;
}
Shape Shape::rotate(Angle angle) const
{
Shape shape;
for (const ShapeElement& element: elements) {
ShapeElement element_new = element.rotate(angle);
shape.elements.push_back(element_new);
}
return shape;
}
Shape Shape::axial_symmetry_identity_line() const
{
Shape shape;
for (auto it = elements.rbegin(); it != elements.rend(); ++it) {
ShapeElement element_new = it->axial_symmetry_identity_line();
shape.elements.push_back(element_new);
}
return shape;
}
Shape Shape::axial_symmetry_x_axis() const
{
Shape shape;
for (auto it = elements.rbegin(); it != elements.rend(); ++it) {
ShapeElement element_new = it->axial_symmetry_x_axis();
shape.elements.push_back(element_new);
}
return shape;
}
Shape Shape::axial_symmetry_y_axis() const
{
Shape shape;
for (auto it = elements.rbegin(); it != elements.rend(); ++it) {
ShapeElement element_new = it->axial_symmetry_y_axis();
shape.elements.push_back(element_new);
}
return shape;
}
Shape Shape::reverse() const
{
Shape shape;
for (auto it = elements.rbegin(); it != elements.rend(); ++it) {
const ShapeElement& element = *it;
ShapeElement element_new;
element_new.type = element.type;
element_new.start.x = element.end.x;
element_new.start.y = element.end.y;
element_new.end.x = element.start.x;
element_new.end.y = element.start.y;
element_new.center.x = element.center.x;
element_new.center.y = element.center.y;
element_new.anticlockwise = !element.anticlockwise;
shape.elements.push_back(element_new);
}
return shape;
}
std::pair<LengthDbl, LengthDbl> Shape::compute_width_and_length(
Angle angle,
bool mirror) const
{
auto points = compute_min_max(angle, mirror);
LengthDbl width = points.second.x - points.first.x;
LengthDbl height = points.second.y - points.first.y;
return {width, height};
}
bool Shape::check() const
{
// Check that the shape is not empty.
if (elements.empty())
return false;
// TODO
return true;
}
std::string Shape::to_string(
Counter indentation) const
{
std::string s = "";
std::string indent = std::string(indentation, ' ');
if (is_circle()) {
LengthDbl radius = distance(elements.front().center, elements.front().start);
s += "circle (radius: " + std::to_string(radius) + ")";
} else if (is_square()) {
s += "square (side: " + std::to_string(elements.front().length()) + ")";
} else if (is_rectangle()) {
s += "rectangle"
" (side 1: " + std::to_string(elements[0].length())
+ "; side 2: " + std::to_string(elements[1].length()) + ")";
} else if (is_polygon()) {
s += "polygon (# edges " + std::to_string(elements.size()) + ")\n";
for (Counter pos = 0; pos < (Counter)elements.size(); ++pos)
s += indent + elements[pos].to_string() + ((pos < (Counter)elements.size() - 1)? "\n": "");
} else {
s += "shape (# elements " + std::to_string(elements.size()) + ")\n";
for (Counter pos = 0; pos < (Counter)elements.size(); ++pos)
s += indent + elements[pos].to_string() + ((pos < (Counter)elements.size() - 1)? "\n": "");
}
return s;
}
nlohmann::json Shape::to_json() const
{
nlohmann::json json;
for (ElementPos element_pos = 0;
element_pos < (ElementPos)elements.size();
++element_pos) {
json[element_pos] = elements[element_pos].to_json();
}
return json;
}
std::string Shape::to_svg(double factor) const
{
std::string s = "M";
for (const ShapeElement& element: elements) {
s += std::to_string(element.start.x * factor)
+ "," + std::to_string(-(element.start.y * factor));
if (element.type == ShapeElementType::LineSegment) {
s += "L";
} else {
throw std::invalid_argument("");
}
}
s += std::to_string(elements.front().start.x * factor)
+ "," + std::to_string(-(elements.front().start.y * factor))
+ "Z";
return s;
}
void Shape::write_svg(
const std::string& file_path) const
{
if (file_path.empty())
return;
std::ofstream file{file_path};
if (!file.good()) {
throw std::runtime_error(
"Unable to open file \"" + file_path + "\".");
}
auto mm = compute_min_max(0.0);
LengthDbl width = (mm.second.x - mm.first.x);
LengthDbl height = (mm.second.y - mm.first.y);
double factor = compute_svg_factor(width);
std::string s = "<svg viewBox=\""
+ std::to_string(mm.first.x * factor)
+ " " + std::to_string(-mm.first.y * factor - height * factor)
+ " " + std::to_string(width * factor)
+ " " + std::to_string(height * factor)
+ "\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n";
file << s;
file << "<path d=\"M";
for (const ShapeElement& element: elements) {
file << (element.start.x * factor)
<< "," << -(element.start.y * factor);
if (element.type == ShapeElementType::LineSegment) {
file << "L";
} else {
throw std::invalid_argument("");
}
}
file << (elements.front().start.x * factor)
<< "," << -(elements.front().start.y * factor)
<< "Z\""
<< " stroke=\"black\""
<< " stroke-width=\"1\""
<< " fill=\"blue\""
<< " fill-opacity=\"0.2\""
<< "/>" << std::endl;
file << "</svg>" << std::endl;
}
Shape packingsolver::irregular::build_shape(
const std::vector<BuildShapeElement>& points,
bool path)
{
Shape shape;
Point point_prev = {points.back().x, points.back().y};
ShapeElementType type = ShapeElementType::LineSegment;
bool anticlockwise = false;
Point center = {0, 0};
for (ElementPos pos = 0; pos < (ElementPos)points.size(); ++pos) {
const BuildShapeElement& point = points[pos];
if (point.type == 0) {
ShapeElement element;
element.type = type;
element.start = point_prev;
element.end = {points[pos].x, points[pos].y};
element.center = center;
element.anticlockwise = anticlockwise;
if (!path || pos > 0)
shape.elements.push_back(element);
point_prev = element.end;
anticlockwise = true;
center = {0, 0};
type = ShapeElementType::LineSegment;
} else {
anticlockwise = (point.type == 1);
center = {points[pos].x, points[pos].y};
type = ShapeElementType::CircularArc;
}
}
return shape;
}
double irregular::compute_svg_factor(
double width)
{
double factor = 1;
while (width * factor > 10000)
factor /= 10;
while (width * factor < 1000)
factor *= 10;
return factor;
}
std::string irregular::to_svg(
const Shape& shape,
const std::vector<Shape>& holes,
double factor,
const std::string& fill_color)
{
std::string s = "<path d=\"" + shape.to_svg(factor);
for (const Shape& hole: holes)
s += hole.reverse().to_svg(factor);
s += "\""
" stroke=\"black\""
" stroke-width=\"1\"";
if (!fill_color.empty()) {
s += " fill=\"" + fill_color + "\""
" fill-opacity=\"0.2\"";
}
s += "/>\n";
return s;
}
void irregular::write_svg(
const Shape& shape,
const std::vector<Shape>& holes,
const std::string& file_path)
{
if (file_path.empty())
return;
std::ofstream file{file_path};
if (!file.good()) {
throw std::runtime_error(
"Unable to open file \"" + file_path + "\".");
}
auto mm = shape.compute_min_max(0.0);
LengthDbl width = (mm.second.x - mm.first.x);
LengthDbl height = (mm.second.y - mm.first.y);
double factor = compute_svg_factor(width);
std::string s = "<svg viewBox=\""
+ std::to_string(mm.first.x * factor)
+ " " + std::to_string(-mm.first.y * factor - height * factor)
+ " " + std::to_string(width * factor)
+ " " + std::to_string(height * factor)
+ "\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n";
file << s;
file << "<g>" << std::endl;
file << to_svg(shape, holes, factor);
//file << "<text x=\"" << std::to_string(x * factor)
// << "\" y=\"" << std::to_string(-y * factor)
// << "\" dominant-baseline=\"middle\" text-anchor=\"middle\">"
// << std::to_string(item_shape_pos)
// << "</text>" << std::endl;
file << "</g>" << std::endl;
file << "</svg>" << std::endl;
}
std::pair<bool, Shape> irregular::remove_redundant_vertices(
const Shape& shape)
{
bool found = false;
Shape shape_new;
for (ElementPos element_pos = 0;
element_pos < (ElementPos)shape.elements.size();
++element_pos) {
const ShapeElement& element = shape.elements[element_pos];
if (equal(element.start.x, element.end.x)
&& equal(element.start.y, element.end.y)) {
found = true;
continue;
}
shape_new.elements.push_back(element);
}
return {found, shape_new};
}
std::pair<bool, Shape> irregular::remove_aligned_vertices(
const Shape& shape)
{
if (shape.elements.size() <= 3)
return {false, shape};
bool found = false;
Shape shape_new;
ElementPos element_prev_pos = shape.elements.size() - 1;
for (ElementPos element_cur_pos = 0;
element_cur_pos < (ElementPos)shape.elements.size();
++element_cur_pos) {
ElementPos element_next_pos = element_cur_pos + 1;
const ShapeElement& element_prev = shape.elements[element_prev_pos];
const ShapeElement& element = shape.elements[element_cur_pos];
const ShapeElement& element_next = (element_next_pos < shape.elements.size())?
shape.elements[element_next_pos]:
shape_new.elements.front();
bool useless = false;
if (element.type == ShapeElementType::LineSegment
&& element_prev.type == ShapeElementType::LineSegment) {
double x1 = element_prev.start.x;
double y1 = element_prev.start.y;
double x2 = element.start.x;
double y2 = element.start.y;
double x3 = element_next.start.x;
double y3 = element_next.start.y;
double v = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2);
//std::cout << "element_prev " << element_prev_pos << " " << element_prev.to_string() << std::endl;
//std::cout << "element " << element_cur_pos << " " << element.to_string() << std::endl;
//std::cout << "element_next " << element_next_pos << " " << element_next.to_string() << std::endl;
//std::cout << "v " << v << std::endl;
if (equal(v, 0)
|| (equal(element_prev.start.y, element.start.y)
&& equal(element.start.y, element_next.start.y))
|| (equal(element_prev.start.x, element.start.x)
&& equal(element.start.x, element_next.start.x))) {
//std::cout << "useless " << element.to_string() << std::endl;
useless = true;
found = true;
}
}
if (!useless) {
if (!shape_new.elements.empty())
shape_new.elements.back().end = element.start;
shape_new.elements.push_back(element);
element_prev_pos = element_cur_pos;
}
}
shape_new.elements.back().end = shape_new.elements.front().start;
return {found, shape_new};
}
std::pair<bool, Shape> irregular::equalize_close_y(
const Shape& shape)
{
bool found = false;
Shape shape_new;
ElementPos element_prev_pos = shape.elements.size() - 1;
for (ElementPos element_pos = 0;
element_pos < (ElementPos)shape.elements.size();
++element_pos) {
const ShapeElement& element = shape.elements[element_pos];
const ShapeElement& element_prev = shape.elements[element_prev_pos];
shape_new.elements.push_back(element);
if (equal(element.start.y, element_prev.start.y)
&& element.start.y != element_prev.start.y) {
shape_new.elements.back().start.y = element_prev.start.y;
found = true;
}
element_prev_pos = element_pos;
}
return {found, shape_new};
}
Shape irregular::clean_shape(
const Shape& shape)
{
Shape shape_new = shape;
auto res = remove_redundant_vertices(shape_new);
shape_new = res.second;
for (;;) {
bool found = false;
{
auto res = remove_aligned_vertices(shape_new);
found |= res.first;
shape_new = res.second;
}
{
auto res = equalize_close_y(shape_new);
found |= res.first;
shape_new = res.second;
}
if (!found)
break;
}
return shape_new;