-
Notifications
You must be signed in to change notification settings - Fork 1
/
packer.cpp
1671 lines (1386 loc) · 54.3 KB
/
packer.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
// + ----------------------------------- +
// | |
// | Author: Romain BESSON |
// | |
// | Start Date: 16 / 12 / 2021 |
// | |
// | Publish Date: 22 / 12 / 2021 |
// | |
// + ----------------------------------- +
#include <algorithm>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "packer.h"
#include <cassert>
#include <cstdlib>
#include <conio.h>
#include <string>
#include <vector>
#include <math.h>
#include <chrono>
#include <thread>
#include <map>
using namespace std;
using namespace std::chrono;
// ------------------------------------------------------------------------------------
// GLOBAL DATA
// ------------------------------------------------------------------------------------
#define MAX_NB_RECTANGLES 100000
#define INT_INFINITY 100000000
GLOBAL Manager;
EZ Draw;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
HINSTANCE* hInstance = new HINSTANCE();
// ------------------------------------------------------------------------------------
// ~~ FUNCTIONS
// ------------------------------------------------------------------------------------
// adjustString : Fit string inside n of blanks
string adjustString(string s, int fit)
{
string n = s;
fit = (fit < s.length()) ? s.length() : fit;
for (int i = 0; i < fit-s.length(); i++) {n += " ";}
return n;
}
char* toCharStar(string s)
{
return ((char *) s.c_str());
}
// ------------------------------------------------------------------------------------
// GLOBAL CLASS FUNCTIONS
// ------------------------------------------------------------------------------------
// newRectID : Returns a new ID for new Rectangle
int GLOBAL::newRectID()
{
return this->Rect_ID_Counter++;
}
// newHoleID : Returns a new ID for new Hole
int GLOBAL::newHoleID()
{
return this->Hole_ID_Counter++;
}
// resetRectCounter
void GLOBAL::resetRectCounter()
{
this->Rect_ID_Counter = 0;
}
// resetHoleCounter
void GLOBAL::resetHoleCounter()
{
this->Hole_ID_Counter = 0;
}
// newHole : Add a new Hole to Holes;
void GLOBAL::newHole(SHAPE* Hole)
{
Hole->id = this->newHoleID();
(this->Holes)[Hole->id] = Hole;
}
// newRectangle : Add a new Rectangle to Rectangles
void GLOBAL::newRectangle(SHAPE* Rectangle)
{
(this->Rectangles)[this->newRectID()] = Rectangle;
}
// clearShapes : clears rectangles and holes
void GLOBAL::clearShapes()
{
(this->Rectangles).clear();
(this->Holes).clear();
}
// Copy Shapes //
SHAPES GLOBAL::copyRectangles()
{
SHAPES Copied_Shapes;
SHAPES::iterator it;
for (it = this->Rectangles.begin(); it != this->Rectangles.end(); it++)
{
SHAPE* Rectangle = it->second;
SHAPE* Copied = new SHAPE;
Copied->x1 = Rectangle->x1;
Copied->y1 = Rectangle->y1;
Copied->x2 = Rectangle->x2;
Copied->y2 = Rectangle->y2;
Copied->id = Rectangle->id;
Copied->origin = Rectangle->origin;
Copied->appendTo(&Copied_Shapes);
}
return Copied_Shapes;
}
SHAPES GLOBAL::copyHoles()
{
SHAPES Copied_Shapes;
SHAPES::iterator it;
for (it = this->Holes.begin(); it != this->Holes.end(); it++)
{
SHAPE* Hole = it->second;
SHAPE* Copied = new SHAPE;
Copied->x1 = Hole->x1;
Copied->y1 = Hole->y1;
Copied->x2 = Hole->x2;
Copied->y2 = Hole->y2;
Copied->id = Hole->id;
Copied->origin = Hole->origin;
Copied->appendTo(&Copied_Shapes);
}
return Copied_Shapes;
}
// ----------- //
// Get Run Result
RESULT* GLOBAL::getResult()
{
RESULT* CurrentRun = new RESULT();
CurrentRun->Width = this->Width;
CurrentRun->Theoretical_Height = this->Theoretical_Height;
CurrentRun->Rect_Area = this->Rect_Area;
CurrentRun->Rotate = this->Rotate;
CurrentRun->Sort_Strategy = this->Sort_Strategy;
CurrentRun->Total_Time = this->Total_Time;
CurrentRun->Holes_Created = this->Hole_ID_Counter;
CurrentRun->Sort_Strategy = this->Sort_Strategy;
CurrentRun->Rectangles = this->copyRectangles();
for (SHAPES::iterator it = (CurrentRun->Rectangles).begin(); it != (CurrentRun->Rectangles).end(); it++)
{
SHAPE* Rectangle = it->second;
// End Height
if (Rectangle->y2 > CurrentRun->Height) CurrentRun->Height = Rectangle->y2;
}
return CurrentRun;
}
// ------------------------------------------------------------------------------------
// CLASS DEFINED FUNCTIONS
// ------------------------------------------------------------------------------------
void SHAPE::Print(string Type, int maxID, int maxDims, int maxCoords)
{
cout << Type << " ID: " << adjustString(to_string(this->id)+",", maxID);
cout << " Width: " << adjustString(to_string(this->x2 - this->x1)+",", maxDims);
cout << " Height: " << adjustString(to_string(this->y2 - this->y1)+",", maxDims);
cout << " [ X1: " << adjustString(to_string(this->x1)+",", maxCoords);
cout << "Y1: " << adjustString(to_string(this->y1)+",", maxCoords);
cout << "X2: " << adjustString(to_string(this->x2)+",", maxCoords);
cout << "Y2: " << adjustString(to_string(this->y2), maxCoords-2) << " ], ";
cout << "Origin: " << adjustString(to_string(this->origin), 4) << endl;
}
// Rotate : Flips Shape1 (this) 90° : Shape1->Rotate();
void SHAPE::Rotate()
{
int oldX2 = this->x2;
this->x2 = this->y2;
this->y2 = oldX2;
this->isRotated = !(this->isRotated);
}
// sameAs : Shape1 (this) same properties as Shape2 (Shape) : Shape1->sameAs(Shape2);
bool SHAPE::sameAs(SHAPE* Shape)
{
if (
this->x1 == Shape->x1 &&
this->x2 == Shape->x2 &&
this->y1 == Shape->y1 &&
this->y2 == Shape->y2 &&
this->id == Shape->id &&
this->origin == Shape->origin)
return true;
return false;
}
// smallerThan : Shape1 (this) smaller than Shape2 (Shape) : Shape1->smallerThan(Shape2);
bool SHAPE::smallerThan(SHAPE* Shape)
{
// Check if this shape fits in shape2
// ONLY by comparing their Widths and Heights AND
// Not their actual 2D positions
if (
(this->x2 - this->x1) < (Shape->x2 - Shape->x1) && // Width 1 smaller than Width 2
(this->y2 - this->y1) < (Shape->y2 - Shape->y1) // Height 1 smaller than Height 2
)
return true;
return false;
}
// fitsIn : Shape1 (this) fits inside Shape2 (Shape) : Shape1->fitsIn(Shape2);
bool SHAPE::fitsIn(SHAPE* Shape)
{
// Check if this shape fits in the other
// By comparing all of the corners of this shape
if (
this->x1 >= Shape->x1 && // Top Left Corner of this shape is inside the other
this->y1 >= Shape->y1 && //
this->x2 <= Shape->x2 && // Bottom Right of this shape is inside the other
this->y2 <= Shape->y2 // Therefore, a boundary is formed for this shape only using 2 corners of the other
)
return true;
return false;
}
// Overlaps : Checks if Shape1 (this) overlaps with Shape 2 (Shape) : Shape1->Overlaps(Shape2);
bool SHAPE::Overlaps(SHAPE* Shape)
{
// Check corners //
// Check top left corner
if (
(this->x1 > Shape->x1) &&
(this->x1 < Shape->x2) &&
(this->y1 > Shape->y1) &&
(this->y1 < Shape->y2)
) return true;
// Check top right corner
if (
(this->x2 > Shape->x1) &&
(this->x2 < Shape->x2) &&
(this->y1 > Shape->y1) &&
(this->y1 < Shape->y2)
) return true;
// Check bottom left corner
if (
(this->x1 > Shape->x1) &&
(this->x1 < Shape->x2) &&
(this->y2 > Shape->y1) &&
(this->y2 < Shape->y2)
) return true;
// Check bottom right corner
if (
(this->x2 > Shape->x1) &&
(this->x2 < Shape->x2) &&
(this->y2 > Shape->y1) &&
(this->y2 < Shape->y2)
) return true;
// ------------- //
// Check Edges don't overlap //
// Check if the top horizontal edge of the rectangle passes through the hole
if (
(this->x1 < Shape->x2) &&
(this->x2 > Shape->x1) &&
(this->y1 >= Shape->y1) &&
(this->y1 < Shape->y2)
) return true;
// Check if the bottom horizontal edge of the rectangle passes through the hole
if (
(this->x1 < Shape->x2) &&
(this->x2 > Shape->x1) &&
(this->y2 <= Shape->y2) &&
(this->y2 > Shape->y1)
) return true;
// Check if the left vertical edge of the rectangle passes through the hole
if (
(this->y1 < Shape->y2) &&
(this->y2 > Shape->y1) &&
(this->x1 < Shape->x2) &&
(this->x1 >= Shape->x1)
) return true;
// Check if the right vertical edge of the rectangle passes through the hole
if (
(this->y1 < Shape->y2) &&
(this->y2 > Shape->y1) &&
(this->x2 <= Shape->x2) &&
(this->x2 > Shape->x1)
) return true;
// ------------------------- //
return false; // No overlap detected
}
// isCovered : Shape1 (this) covered by some Shape in Shapes : Shape1->isCovered(Shapes, ExceptionShape);
bool SHAPE::isCovered(SHAPES* Shapes)
{
// Iterates through all the shapes and checks if it is covered by a Shape AND
// We also pass an argument 'ExceptionShape' if we are for example: Breaking a Shape into new Shapes
SHAPES::iterator it;
for (it = (*Shapes).begin(); it != (*Shapes).end(); it++)
{
SHAPE* shape = it->second;
if (this->fitsIn(shape)) return true;
}
return false;
}
// removeFrom : Removes Shape1 (this) from Shapes : Shape1->Rotate(Shapes);
void SHAPE::removeFrom(SHAPES* Shapes)
{
(*Shapes).erase(this->id);
}
// appendTo : Adds Shape1 to Shapes : Shape1->appendTo(Shapes);
void SHAPE::appendTo(SHAPES* Shapes)
{
(*Shapes)[this->id] = this;
}
// ------------------------------------------------------------------------------------
// FUNCTIONS
// ------------------------------------------------------------------------------------
// clearConsole : Clears the Windows console
void clearConsole()
{
system("CLS");
}
// setColor : Set color for Cout -> Windows Console
void setColor(int Color)
{
SetConsoleTextAttribute(hConsole, Color);
}
// resetColor : Set color to default for Cout -> Windows Console
void resetColor()
{
SetConsoleTextAttribute(hConsole, 7);
}
// checkSolve : Check Solving Possibility
int checkSolve()
{
for (vector<SHAPE*>::iterator it = Manager.Stored_Rectangles.begin(); it != Manager.Stored_Rectangles.end(); it++)
{
SHAPE* Rectangle = *it;
if ( // (1) Check if Rectangle is placable without rotations
!Manager.Rotate &&
(Rectangle->x2 - Rectangle->x1) > Manager.Width
) return Rectangle->id;
if ( // (2) Check if Rectangle is placable even with rotations
(Rectangle->x2 - Rectangle->x1) > Manager.Width &&
(Rectangle->y2 - Rectangle->y1) > Manager.Width
) return Rectangle->id;
}
return 0;
}
// printUsage : Prints Usage Info to user
void printUsage()
{
cout << " ./packer <rectangle_list> <width> <can_rotate> <sort_strategy>\n\n";
cout << "Examples:\n";
cout << " ./packer list 100 --> width => 100\n";
cout << " ./packer list ??? 0 --> non-rotatable solution\n";
cout << " ./packer list ??? 1 --> rotatable solution\n";
cout << " ./packer list ??? ? 1 --> Sort Strategy => Area\n";
cout << " ./packer list ??? ? 2 --> Sort Strategy => Height\n";
cout << " ./packer list ??? ? 3 --> Sort Strategy => Automatic\n";
}
// printHelp : Prints Usage Info to user
void printHelp()
{
cout << "\nUsage:\n";
printUsage();
}
// printError : Prints an Error to user
void printError(string message, int type, SHAPES* FaultyShapes)
{
setColor(12);
cout << "\nFailed:\n------------------------------------------------------------\n";
resetColor();
switch (type) {
case 1:
cout << "Error Message: " << message;
cout << "\n\nCorrect Usage:\n";
printUsage();
break;
case 2:
cout << "Error Message: " << message;
cout << "\n\nFile Format:\n";
cout << " Rectangle <width, height>\n\n";
cout << "Example (example.txt):\n";
cout << " 10 20 --> New Rectangle with 10 Width, 20 Height\n";
cout << " 20 30 --> New Rectangle with 20 Width, 30 Height\n";
cout << " 30 40 --> New Rectangle with 30 Width, 40 Height\n";
break;
case 3:
cout << "Error Message: " << message << endl;
break;
case 4:
SHAPES::iterator it;
int bigID = 0;
int bigDim = 0;
int bigCoord = 0;
for (it = FaultyShapes->begin(); it != FaultyShapes->end(); it++)
{
int key = it->first;
SHAPE* Shape = it->second;
// assert(key == Shape->id); // The key is normally the Hole's id
//
if (to_string(Shape->id).length() > bigID) bigID = to_string(Shape->id).length();
int Dimensions = max((Shape->x2-Shape->x1), (Shape->y2-Shape->y1));
if (to_string(Dimensions).length() > bigDim) bigDim = to_string(Dimensions).length();
int Coords = max(Shape->x1,max(Shape->x2,max(Shape->y1,Shape->y2)));
if (to_string(Coords).length() > bigCoord) bigCoord = to_string(Coords).length();
//
}
for (it = FaultyShapes->begin(); it != FaultyShapes->end(); it++)
{
int key = it->first;
SHAPE* Shape = it->second;
// assert(key == Shape->id); // The key is normally the Shape's id
Shape->Print((Shape->id > Manager.Rect_ID_Counter) ? "Hole: " : "Rectangle: ", bigID+3, bigDim+3, bigCoord+3);
}
}
setColor(12);
cout << "------------------------------------------------------------\n";
resetColor();
}
// awaitInput //
void ExitStartInput()
{
int key = getch();
switch (key) {
case 115: return;
case 113: exit(0);
}
ExitStartInput();
}
void awaitInput(string Message)
{
cout << "\n" << Message << "...\n";
ExitStartInput();
}
// ---------- //
// printRunInf : Print current run info
void printRunInf()
{
cout << "\nYou are about to start packing:\n";
cout << "\n--> Rectangles: " + to_string(Manager.Stored_Rectangles.size());
cout << "\n--> Width: " + to_string(Manager.Width);
cout << "\n--> Rotations: " << ((Manager.Rotate) ? "Yes\n" : "No\n");
cout << "--> Sort Strategy: ";
switch (Manager.Sort_Strategy)
{
case 1: cout << "By Area\n"; break;
case 2: cout << "By Height\n"; break;
default: cout << " By Area\n"; break;
}
awaitInput("Press 's' to start packing, or 'q' to abort");
}
// printEndInf : Prints the Ending Info
void printEndInf(RESULT* EndResult)
{
resetColor();
cout << "Theoretical Min. Height: "; setColor(10); cout << EndResult->Theoretical_Height << endl; resetColor();
cout << "Height Calculated: "; setColor(10); cout << EndResult->Height << endl; resetColor();
cout << "Holes Created: "; setColor(10); cout << EndResult->Holes_Created << endl; resetColor();
cout << "Total Area: "; setColor(10); cout << EndResult->Rect_Area << endl; resetColor();
cout << "Time Taken: "; setColor(10);
if (duration <double, milli> (EndResult->Total_Time).count() < 10)
{
cout << duration <double, micro> (EndResult->Total_Time).count() << " micro s" << endl << endl; resetColor();
} else cout << duration <double, milli> (EndResult->Total_Time).count() << " ms" << endl << endl; resetColor();
float num = (EndResult->Height - EndResult->Theoretical_Height) * EndResult->Width * 100;
float denum = EndResult->Height * EndResult->Width;
float lost = (num / denum);
float used = 100 - lost;
cout << "Used % (green): " ; setColor(10); cout << used << " %" << endl; resetColor();
cout << "Lost % (green): "; setColor(10); cout << lost << " %" << endl;; resetColor();
cout << "Sort Chosen: "; setColor(10); cout << ((EndResult->Sort_Strategy == 1) ? "By Area" : "By Height") << endl;; resetColor();
}
// byArea : Comparison function for Area
bool byArea(SHAPE* a, SHAPE* b)
{
return (a->x2 * a->y2) > (b->x2 * b->y2);
}
// byHeight : Comparison function for height
bool byHeight(SHAPE* a, SHAPE* b)
{
int heightA = a->y2 - a->y1;
int heightRotatedA = a->x2 - a->x1;
int heightB = b->y2 - b->y1;
int heightRotatedB = b->x2 - b->x1;
bool result = heightA < heightB;
bool resultRotated = min(heightA, heightRotatedA) < min(heightB, heightRotatedB);
return (Manager.Rotate) ? resultRotated : result;
}
// readRectangles : reads specified file path and sets Manager.Rectangles
bool readRectangles(char* fileName)
{
FILE *at;
if ((at = fopen(fileName, "r")) == NULL) return false;
Manager.clearShapes();
int w = 0;
int h = 0;
while (fscanf(at, "%d %d", &w, &h) != EOF)
{
RECTANGLE* NewRectangle = new RECTANGLE();
NewRectangle->x2 = w;
NewRectangle->y2 = h;
NewRectangle->id = Manager.newRectID();
Manager.Stored_Rectangles.push_back(NewRectangle);
}
return true;
}
// init_G_ : Initiate Global Variables
bool init_G_(int argc, char* argv[])
{
if (((string) argv[1]) == "-help")
{
printHelp();
return false;
}
// Check that we have right arguments //
if ((argc < 4) || (argc > 5))
{
printHelp();
return false; // Failed
}
// ---------------------------------- //
// SET Global Variables //
Manager.Width = atoi((char*) argv[2]);
if (Manager.Width <= 0)
{
printError("Width cannot be 0", 1, NULL);
return false; // Failed
}
Manager.Rotate = (argc > 3) ? atoi((char*) argv[3]) : false;
Manager.Sort_Strategy = (atoi((char*) argv[4]) == 0) ? 1 : atoi((char*) argv[4]);
Manager.Sort_Strategy = (Manager.Sort_Strategy > 3) ? 3 : ((Manager.Sort_Strategy < 1) ? 1 : Manager.Sort_Strategy);
// -------------------- //
// Specified File is Un-Readable or Doesn't Exist //
if (!readRectangles(argv[1]))
{
printError("Failed to read file: " + (string) argv[1], 2, NULL);
return false; // Failed
}
// ---------------------------------------------- //
// Cap Input Rectangles Amount for Security (Changable limit) //
if (Manager.Stored_Rectangles.size() > MAX_NB_RECTANGLES)
{
printError("Too many rectangles, MAX_NB_RECTANGLES => " + to_string(MAX_NB_RECTANGLES), 2, NULL);
return false; // Failed
}
// ---------------------------------------------------------- //
// Check Solving Possibility //
int ID_Failed_RECT = checkSolve(); // Check that rectangle's size's match with specified Manager.Width
if (ID_Failed_RECT) //
{
printError("Rectangle " + to_string(ID_Failed_RECT) + " is too big in relation to specified Width", 2, NULL);
return false; // Failed
}
// ------------------------- //
// Calculate Theoretical Min. Height + Total Rectangles AREA //
for (vector<SHAPE*>::iterator it = Manager.Stored_Rectangles.begin(); it != Manager.Stored_Rectangles.end(); it++)
{
SHAPE* Rectangle = *it;
// T. Min. Height
Manager.Theoretical_Height += (Rectangle->x2 - Rectangle->x1) * (Rectangle->y2 - Rectangle->y1);
// Rect Area
Manager.Rect_Area += (Rectangle->x2 - Rectangle->x1) * (Rectangle->y2 - Rectangle->y1);
}
Manager.Theoretical_Height /= Manager.Width;
// -------------------------------- //
return true; // Passed initiations
}
// printShapes //
void printRectangles()
{
cout << "Rectangles:" << endl;
SHAPES::iterator it;
int bigID = 0;
int bigDim = 0;
int bigCoord = 0;
for (it = Manager.Rectangles.begin(); it != Manager.Rectangles.end(); it++)
{
int key = it->first;
SHAPE* Rectangle = it->second;
// assert(key == Rectangle->id); // The key is normally the Hole's id
//
if (to_string(Rectangle->id).length() > bigID) bigID = to_string(Rectangle->id).length();
int Dimensions = max((Rectangle->x2-Rectangle->x1), (Rectangle->y2-Rectangle->y1));
if (to_string(Dimensions).length() > bigDim) bigDim = to_string(Dimensions).length();
int Coords = max(Rectangle->x1,max(Rectangle->x2,max(Rectangle->y1,Rectangle->y2)));
if (to_string(Coords).length() > bigCoord) bigCoord = to_string(Coords).length();
//
}
for (it = Manager.Rectangles.begin(); it != Manager.Rectangles.end(); it++)
{
(it->second)->Print("", bigID+3, bigDim+3, bigCoord+3);
}
}
void printHoles()
{
cout << "Holes:" << endl;
SHAPES::iterator it;
for (it = Manager.Holes.begin(); it != Manager.Holes.end(); it++)
{
int H_key = it->first;
SHAPE* Hole = it->second;
// assert(H_key == Hole->id); // The key is normally the Hole's id
cout << "ID: " << Hole->id << " x1: " << Hole->x1 << " x2: " << Hole->x2 << " y1: " << Hole->y1 << " y2: " << Hole->y2 << endl;
}
}
// ----------- //
// ------------------------------------------------------------------------------------
// PACKING FUNCTIONS
// ------------------------------------------------------------------------------------
// calculateHeight : Returns the height of the Hole + the Rectangle
int calculateHeight(SHAPE* Rectangle, SHAPE* Hole)
{
return Hole->y1 + (Rectangle->y2 - Rectangle->y1);
}
// getBestHole : Get the best hole to Cut for Packing using different methods
SHAPE* getBestHole(SHAPE* Rectangle, int Method)
{
SHAPE* bestHoleFound = NULL;
bool doRotation = false;
switch (Method)
{
case 1: // [METHOD 1]: Choose hole that makes canvas height the smallest
{
double lowestHeightFound = INT_INFINITY;
SHAPES::iterator it;
for (it = Manager.Holes.begin(); it != Manager.Holes.end(); it++)
{
int key = it->first;
SHAPE* Hole = it->second;
// Hole->Print("Considered HOLE ->");
// assert(key == Hole->id); // The key is normally the Hole's id
// Check Normal Rotation //
bool normalFits = Rectangle->smallerThan(Hole);
int normalH = calculateHeight(Rectangle, Hole);
if (normalFits && normalH < lowestHeightFound) { // Check that Height increase is worthy of this Hole
lowestHeightFound = normalH;
bestHoleFound = Hole;
doRotation = false;
}
// --------------------- //
// Check Rotated Solution //
if (!Manager.Rotate) continue;
Rectangle->Rotate(); // Inner
bool rotationFits = Rectangle->smallerThan(Hole);
int rotationH = calculateHeight(Rectangle, Hole);
if (rotationFits && rotationH < lowestHeightFound) { // Check that Rotated Height is worthy of the rectangle being rotated
lowestHeightFound = rotationH;
bestHoleFound = Hole;
doRotation = true;
}
Rectangle->Rotate(); // Outer
// ---------------------- //
}
break;
}
}
if (doRotation) Rectangle->Rotate();
return bestHoleFound;
}
// createNewHoles : Creates/Cuts Hole into new Holes based on a Shape
bool createNewHoles(SHAPE* Rectangle, SHAPE* Hole, SHAPES* Holes)
{
// Create new holes from a Placement / Overlapping
// ⬛ => Hole
// ⬜ => Rectangle
// So we enumerate all the 15 cases where the Rectangle can clip inside the Hole
bool caseMet = false;
SHAPE* newHole1 = NULL;
SHAPE* newHole2 = NULL;
SHAPE* newHole3 = NULL;
// ⬜⬜⬜
// ⬜⬜⬜
// ⬜⬜⬜
// [CASE 1]: Perfect fit!
if (
!caseMet &&
Rectangle->x1 == Hole->x1 && // Rectangle's Left Vertical Edge is on the hole's Left Vertical Edge
Rectangle->y1 == Hole->y1 && // Rectangle's Top Horizontal Edge is on the hole's Top Horizontal Edge
Rectangle->x2 == Hole->x2 && // Rectangle's Right Vertical Edge is on the hole's Right Vertical Edge
Rectangle->y2 == Hole->y2 // Rectangle's Bottom Horizontal Edge is on the hole's Bottom Horizontal Edge
) caseMet = true; // Only case where we add 0 Holes
//
// ⬜⬜⬜
// ⬛⬛⬛
// ⬛⬛⬛
// [CASE 2]: Top Bar Horizontal
if (
!caseMet &&
Rectangle->x1 <= Hole->x1 && // Rectangle's Left Vertical Edge is left to the hole AND
Rectangle->y1 <= Hole->y1 && // Rectangle's Top Horizontal Edge is above the hole AND
Rectangle->x2 >= Hole->x2 && // Rectangle's Right Vertical Edge is right to the hole AND
Rectangle->y2 > Hole->y1 && // Rectangle's Bottom Horizontal Edge passes through the hole
Rectangle->y2 < Hole->y2 //
) {
caseMet = true;
newHole1 = new HOLE();
newHole1->x1 = Hole->x1;
newHole1->y1 = Rectangle->y2;
newHole1->x2 = Hole->x2;
newHole1->y2 = Hole->y2;
newHole1->origin = 2;
}
//
// ⬛⬛⬜
// ⬛⬛⬜
// ⬛⬛⬜
// [CASE 3]: Right Bar Vertical
if (
!caseMet &&
Rectangle->x1 > Hole->x1 && // Rectangle's Left Vertical Edge passes through the hole AND
Rectangle->x1 < Hole->x2 && //
Rectangle->y1 <= Hole->y1 && // Rectangle's Top Horizontal Edge is above the hole AND
Rectangle->x2 >= Hole->x2 && // Rectangle's Right Vertical Edge is right to the hole AND
Rectangle->y2 >= Hole->y2 // Rectangle's Bottom Horizontal Edge is below the hole
) {
caseMet = true;
newHole1 = new HOLE();
newHole1->x1 = Hole->x1;
newHole1->y1 = Hole->y1;
newHole1->x2 = Rectangle->x1;
newHole1->y2 = Hole->y2;
newHole1->origin = 3;
}
//
// ⬛⬛⬛
// ⬛⬛⬛
// ⬜⬜⬜
// [CASE 4]: Bottom Bar Horizontal
if (
!caseMet &&
Rectangle->x1 <= Hole->x1 && // Rectangle's Left Vertical Edge is left to the hole AND
Rectangle->y1 > Hole->y1 && // Rectangle's Top Horizontal Edge passes through the hole AND
Rectangle->y1 < Hole->y2 && //
Rectangle->x2 >= Hole->x2 && // Rectangle's Right Vertical Edge is right to the hole AND
Rectangle->y2 >= Hole->y2 // Rectangle's Bottom Horizontal Edge is below the hole
) {
caseMet = true;
newHole1 = new HOLE();
newHole1->x1 = Hole->x1;
newHole1->y1 = Hole->y1;
newHole1->x2 = Hole->x2;
newHole1->y2 = Rectangle->y1;
newHole1->origin = 4;
}
//
// ⬜⬛⬛
// ⬜⬛⬛
// ⬜⬛⬛
// [CASE 5]: Left Bar Vertical
if (
!caseMet &&
Rectangle->x1 <= Hole->x1 && // Rectangle's Left Vertical Edge is left to the hole AND
Rectangle->y1 <= Hole->y1 && // Rectangle's Top Horizontal Edge is above the hole AND
Rectangle->x2 > Hole->x1 && // Rectangle's Right Vertical Edge passes through the hole AND
Rectangle->x2 < Hole->x2 && //
Rectangle->y2 >= Hole->y2 // Rectangle's Bottom Horizontal Edge is below the hole
) {
caseMet = true;
newHole1 = new HOLE();
newHole1->x1 = Rectangle->x2;
newHole1->y1 = Hole->y1;
newHole1->x2 = Hole->x2;
newHole1->y2 = Hole->y2;
newHole1->origin = 5;
}
//
// ⬜⬛⬛
// ⬛⬛⬛
// ⬛⬛⬛
// [CASE 6]: Top Left Corner
if (
!caseMet &&
Rectangle->x1 <= Hole->x1 && // Rectangle's Left Vertical Edge is left to the hole AND
Rectangle->y1 <= Hole->y1 && // Rectangle's Top Horizontal Edge is above the hole AND
Rectangle->x2 > Hole->x1 && // Rectangle's Right Vertical Edge passes through the hole AND
Rectangle->x2 < Hole->x2 && //
Rectangle->y2 > Hole->y1 && // Rectangle's Bottom Horizontal Edge passes through the hole
Rectangle->y2 < Hole->y2 //
) {
caseMet = true;
newHole1 = new HOLE();
newHole1->x1 = Hole->x1;
newHole1->y1 = Rectangle->y2;
newHole1->x2 = Hole->x2;
newHole1->y2 = Hole->y2;
newHole1->origin = 6;
newHole2 = new HOLE();
newHole2->x1 = Rectangle->x2;
newHole2->y1 = Hole->y1;
newHole2->x2 = Hole->x2;
newHole2->y2 = Hole->y2;
newHole2->origin = 6;
}
//
// ⬛⬛⬜
// ⬛⬛⬛
// ⬛⬛⬛
// [CASE 7]: Top Right Corner
if (
!caseMet &&
Rectangle->x1 > Hole->x1 && // Rectangle's Left Vertical Edge Crosses in the hole AND
Rectangle->x1 < Hole->x2 && //
Rectangle->y1 <= Hole->y1 && // Rectangle's Top Horizontal Edge is above the hole AND
Rectangle->x2 > Hole->x1 && // Rectangle's Right Vertical Edge is right to the hole AND
Rectangle->x2 >= Hole->x2 && //
Rectangle->y2 > Hole->y1 && // Rectangle's Bottom Horizontal Edge passes through the Hole
Rectangle->y2 < Hole->y2 //
) {
caseMet = true;
newHole1 = new HOLE();
newHole1->x1 = Hole->x1;
newHole1->y1 = Hole->y1;
newHole1->x2 = Rectangle->x1;
newHole1->y2 = Hole->y2;
newHole1->origin = 7;
newHole2 = new HOLE();
newHole2->x1 = Hole->x1;
newHole2->y1 = Rectangle->y2;
newHole2->x2 = Hole->x2;
newHole2->y2 = Hole->y2;
newHole2->origin = 7;
}
//
// ⬛⬛⬛
// ⬛⬛⬛
// ⬛⬛⬜
// [CASE 8]: Bottom Right Corner
if (
!caseMet &&
Rectangle->x1 < Hole->x2 && // Rectangle's Left Vertical Edge passes through the hole AND
Rectangle->x1 > Hole->x1 && //
Rectangle->y1 > Hole->y1 && // Rectangle's Top Horizontal Edge passes through the hole AND
Rectangle->y1 < Hole->y2 && //
Rectangle->x2 >= Hole->x2 && // Rectangle's Right Vertical Edge is right to the hole AND
Rectangle->y2 >= Hole->y2 // Rectangle's Bottom Horizontal Edge is below the hole
) {
caseMet = true;
newHole1 = new HOLE();
newHole1->x1 = Hole->x1;
newHole1->y1 = Hole->y1;
newHole1->x2 = Hole->x2;
newHole1->y2 = Rectangle->y1;
newHole1->origin = 8;
newHole2 = new HOLE();
newHole2->x1 = Hole->x1;
newHole2->y1 = Hole->y1;
newHole2->x2 = Rectangle->x1;
newHole2->y2 = Hole->y2;
newHole2->origin = 8;
}
// ⬛⬛⬛
// ⬛⬛⬛
// ⬜⬛⬛