From db9c3ea2e34312c59b68f7a374d8376992b5d6b6 Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Thu, 1 Oct 2026 14:26:50 -0400 Subject: [PATCH 01/39] Added algorithm folder and document for summary/requirements. Marigold 10-1 --- algorithm/algo_doc.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 algorithm/algo_doc.md diff --git a/algorithm/algo_doc.md b/algorithm/algo_doc.md new file mode 100644 index 0000000..feac140 --- /dev/null +++ b/algorithm/algo_doc.md @@ -0,0 +1,4 @@ +# 2D Irregular Shape Packing Algorithm +## Summary + +## Requirements From 640e3df1fc0698a53e4ab70dd95dfbee13226bc1 Mon Sep 17 00:00:00 2001 From: Marigold <111760235+LiterallyAFlower@users.noreply.github.com> Date: Thu, 8 Oct 2026 10:56:23 -0400 Subject: [PATCH 02/39] added algorithm process document to algorithm folder. --- algorithm/algo_doc.md | 112 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 109 insertions(+), 3 deletions(-) diff --git a/algorithm/algo_doc.md b/algorithm/algo_doc.md index feac140..554e97f 100644 --- a/algorithm/algo_doc.md +++ b/algorithm/algo_doc.md @@ -1,4 +1,110 @@ -# 2D Irregular Shape Packing Algorithm -## Summary +I’m using a bottom-left greedy polygon-packing algorithm with a compactness score. Basically, place the biggest/weirdest/most difficult sticker first, then place the next one in the lowest leftmost position that produces the most compact sheet. +It’s greedy because I’m not planning on moving stickers again after placing them, as that would be very expensive computationally and time-wise. +It’s not optimal! But it is relatively fast, deterministic, human-readable, and with polygon geometry it can be very good. +For my code and for your visualization, I’m using tetris shapes. + +Steps: +Get info from stickers. +For a quick example, 2 5 square pieces. +A +O O +O +O +O +(0,0)(1,0) +(0,1) +(0,2) +(0,3) + +And +B +O O +O O +O +(0,0)(1,0) +(0,1)(1,1) +(0,2) +(this is just convenient picks to show the program) + +2: Sort the stickers +We need to place the largest sticker first, so we sort with a couple parameters. +Area, bounding-box area (ill get to this later), width, height, complexity. +(easiest version: +stickers.sort( + key=lambda sticker: sticker.area, + reverse=True +) +) + +For example, A would have an area of 5, and so would B. Their bounding box would be 2*4 for A, but only 2*3 for B, so you could put A first because it’s more awkward. Pick weird shapes first. + +3: +The sheet can be whatever size you want, so we can arbitrarily choose what our demo sheet size is (obviously usually it’s 8x4 or whatever the size of a half sheet is) + +4: +Sticker by Sticker +First, nothing on the sheet, so the candidate is (0,0) +And you place A there, taking up +(0,0)(1,0) +(0,1) +(0,2) +(0,3) +Then, B: +The laziest implementation would look at every coordinate, but because we’re not just working in whole numbers, that’s not helpful. +We would want to look for each edge of the current shape, including +Right side, bottom line, corners, and internal vertices. +For this example we get (2,0)(1,1)(1,2)(1,3) and (0,4) as options. + +5: Where to put it +We go through all of the candidates! +(2,0) +If we place at 2,0 it looks like +O O O O +O X O O +O X O X +O X X X +So the placement would be (2,0) + +(1,1) +It looks like +O O X X +O O O X +O O O X +O O X X + +Continue for all of those position, then consider rotations of the shape. + +A simple algorithm prefers lowest x, then lowest y, cram it all in the corner. However, I also want to consider going for the most square space. +O O X X feels more helpful than O O X X +O O X X O O O X +O O O O O O O X +O O O O O O O X + +Because it can fit a bigger sticker in that space. (Want to figure this out, what im working on currently.) + +So, I used a score: height of the shape after placement + wasted space + fragmentation. A lower score is better. + +For option (2,0) our choice was +O O O O +O X O O +O X O X +O X X X +With a bounding box of 4*4 +Where +O O X X +O O O X +O O O X +O O X X +Only has 3*4. +So it chooses that one. + +That pattern continues until we get the smallest total area and score. +Obviously normal stickers aren’t just blocks but this is proof of concept that should work. + + +I’m including a 1-step lookahead, to avoid true recursion and too much burden, but it does help fit bigger stickers. + +I’m going to include a variable for this that we can actually change to get deeper, but for now it’s staying at 0 for demos, then 1 for normal stuff, but if we want to go insane for efficiency over time we can deepen it. +Basically it’ll try one sticker, check other answers, go back a step and check again in case one of those is a more accurate fit. + -## Requirements From e6391446dbbacd4c04c8a6e9fbd10900184cb6b9 Mon Sep 17 00:00:00 2001 From: Marigold <111760235+LiterallyAFlower@users.noreply.github.com> Date: Sat, 17 Oct 2026 16:34:24 -0400 Subject: [PATCH 03/39] added sticker png samples, file structure for algorithm, restarting. Test based code. --- algorithm/algo_doc.md | 6 +++--- algorithm/app/models/__init__.py | 0 algorithm/app/models/sticker.py | 0 algorithm/app/svg/__init__.py | 0 algorithm/app/svg/loader.py | 0 algorithm/tests/fixtures/tetrispieceI.svg | 7 +++++++ algorithm/tests/fixtures/tetrispiecesL.svg | 6 ++++++ algorithm/tests/fixtures/tetrispiecesS.svg | 6 ++++++ algorithm/tests/fixtures/tetrispiecesT.svg | 6 ++++++ algorithm/tests/fixtures/tetrispiecesZ.svg | 6 ++++++ algorithm/tests/fixtures/tetrispiecesquare.svg | 6 ++++++ algorithm/tests/fixtures/tetrispiecesrevL.svg | 6 ++++++ algorithm/tests/test_svg_input.py | 0 images.png | Bin 0 -> 2676 bytes tetrispieceI.png | Bin 0 -> 744 bytes tetrispiecesL.png | Bin 0 -> 1001 bytes tetrispiecesS.png | Bin 0 -> 833 bytes tetrispiecesT.png | Bin 0 -> 1111 bytes tetrispiecesZ.png | Bin 0 -> 934 bytes tetrispiecesquare.png | Bin 0 -> 743 bytes tetrispiecesrevL.png | Bin 0 -> 890 bytes 21 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 algorithm/app/models/__init__.py create mode 100644 algorithm/app/models/sticker.py create mode 100644 algorithm/app/svg/__init__.py create mode 100644 algorithm/app/svg/loader.py create mode 100644 algorithm/tests/fixtures/tetrispieceI.svg create mode 100644 algorithm/tests/fixtures/tetrispiecesL.svg create mode 100644 algorithm/tests/fixtures/tetrispiecesS.svg create mode 100644 algorithm/tests/fixtures/tetrispiecesT.svg create mode 100644 algorithm/tests/fixtures/tetrispiecesZ.svg create mode 100644 algorithm/tests/fixtures/tetrispiecesquare.svg create mode 100644 algorithm/tests/fixtures/tetrispiecesrevL.svg create mode 100644 algorithm/tests/test_svg_input.py create mode 100644 images.png create mode 100644 tetrispieceI.png create mode 100644 tetrispiecesL.png create mode 100644 tetrispiecesS.png create mode 100644 tetrispiecesT.png create mode 100644 tetrispiecesZ.png create mode 100644 tetrispiecesquare.png create mode 100644 tetrispiecesrevL.png diff --git a/algorithm/algo_doc.md b/algorithm/algo_doc.md index 554e97f..4fc866d 100644 --- a/algorithm/algo_doc.md +++ b/algorithm/algo_doc.md @@ -76,9 +76,9 @@ Continue for all of those position, then consider rotations of the shape. A simple algorithm prefers lowest x, then lowest y, cram it all in the corner. However, I also want to consider going for the most square space. O O X X feels more helpful than O O X X -O O X X O O O X -O O O O O O O X -O O O O O O O X +O O X X O O O X +O O O O O O O X +O O O O O O O X Because it can fit a bigger sticker in that space. (Want to figure this out, what im working on currently.) diff --git a/algorithm/app/models/__init__.py b/algorithm/app/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/algorithm/app/models/sticker.py b/algorithm/app/models/sticker.py new file mode 100644 index 0000000..e69de29 diff --git a/algorithm/app/svg/__init__.py b/algorithm/app/svg/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/algorithm/app/svg/loader.py b/algorithm/app/svg/loader.py new file mode 100644 index 0000000..e69de29 diff --git a/algorithm/tests/fixtures/tetrispieceI.svg b/algorithm/tests/fixtures/tetrispieceI.svg new file mode 100644 index 0000000..7b199aa --- /dev/null +++ b/algorithm/tests/fixtures/tetrispieceI.svg @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/algorithm/tests/fixtures/tetrispiecesL.svg b/algorithm/tests/fixtures/tetrispiecesL.svg new file mode 100644 index 0000000..64be517 --- /dev/null +++ b/algorithm/tests/fixtures/tetrispiecesL.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/algorithm/tests/fixtures/tetrispiecesS.svg b/algorithm/tests/fixtures/tetrispiecesS.svg new file mode 100644 index 0000000..69058a4 --- /dev/null +++ b/algorithm/tests/fixtures/tetrispiecesS.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/algorithm/tests/fixtures/tetrispiecesT.svg b/algorithm/tests/fixtures/tetrispiecesT.svg new file mode 100644 index 0000000..cb7248e --- /dev/null +++ b/algorithm/tests/fixtures/tetrispiecesT.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/algorithm/tests/fixtures/tetrispiecesZ.svg b/algorithm/tests/fixtures/tetrispiecesZ.svg new file mode 100644 index 0000000..c7d71d8 --- /dev/null +++ b/algorithm/tests/fixtures/tetrispiecesZ.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/algorithm/tests/fixtures/tetrispiecesquare.svg b/algorithm/tests/fixtures/tetrispiecesquare.svg new file mode 100644 index 0000000..779fa68 --- /dev/null +++ b/algorithm/tests/fixtures/tetrispiecesquare.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/algorithm/tests/fixtures/tetrispiecesrevL.svg b/algorithm/tests/fixtures/tetrispiecesrevL.svg new file mode 100644 index 0000000..418bc3c --- /dev/null +++ b/algorithm/tests/fixtures/tetrispiecesrevL.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/algorithm/tests/test_svg_input.py b/algorithm/tests/test_svg_input.py new file mode 100644 index 0000000..e69de29 diff --git a/images.png b/images.png new file mode 100644 index 0000000000000000000000000000000000000000..1d4aad2376dabb31f83a6455a59903db6f450e47 GIT binary patch literal 2676 zcmd6pX;_oj5`a%>6)39)i6RBrLTRI)EV8M9>@guMP1sDoSQHfj*?KhugkBIRhOH<> zNDBfDJ4DeaO9Dy@2mz5zcBovi>I0{xY8;8WpYWSI7o(Baj><|6YhrBJuJ%+2)j;E%BInJIN&? zs_E(JoSYmkmuqEZg~ei7EJ;vMP*qixpP!%d5u?>r;5NCs-|n<+ZF@sQ0|0dZd|<1vnfK5%Khbj!tbY2nYZc4R+XcV*r+o_UswO;~9)Og4My!&Q8-B8vx7!kk<;t z0N{@Br5-)1ANY?;WIeA{i4lXD6bwPRW&j)lV2l-G>gX7WLLHJ(J)o9n>_2u`4NW-M#Iggx*4kJV*EtD5=kDE?Kl`kp$lM%*0W=!W*SAYf23A(! z)1{+A0AvBq{uGJ!^U`?aH!5B#Heo5FyAtK&<#u}&e-|q2PI>Wzc9;t#lv;=*HE&7e z1Ae^!E}j&b_xxYv2x56*R{6q$M$EB35^nj)Hi2Pw?8OSE1a+i>E8~i@JkNv7-eoDI z{vj$YzX9z$3DxbN6YVH%dQ>xF3`Px-QZ{E2n`#PfAIFt$)~sz~0`-!ZL<;NdVTe2? zxTW*hO|FGrMr_v3YfdgX2)8OO54+N%4y7}bS6;g(#W=qSpWcA`lgmkvH9VeBueO@v zLg9(?(f#5e*gF7DGiwz%EY4W6hS=z3_E5JDe|ARD$AF#bJ#sjRt~KWX>GEe2ens;$ zgoWr#SZLe(|2s6OV2+laOkE3Cp$GIf%GTzeAAB$(xKugD`%R4jPp-t?Lkp6;yQw50 zemISJ&F67AuZfVR)HmY(ZJYxy4#l|);TUzk*JIo3t8On8MoGzyb6(D%$?P$lE&b`f zIX$n1e5Y%QHMzn_;cKyM|2P3AiaY5028=gfd03}BY+#r)S%38=^6_T4tkq4}HFCnm zXT1XbTCY#UfW1G)h`mpt%A&vZ*9<1C0m*UF3yqfa%Vx`YBZ2`e?5NYKc+c~VxTqz- zHwsgIGT-Jf)S}F*zDf%5hIHuL7ZNeU{sSwJt7}c39~*u$tr??Q%e+$J6iC*g6*Je`JMyU$ zYkx9F1pLT;&B3YesM5ch_U2R5;?pKLQ&ueV#vl0dgsI)q(tpYGD>rJQV@Xjt7nL|$ z)%)J=xGyERES_xUt5A+WTWV8!$fM*(hkdM}uM3Hb44bqs69r+0evi&Y*QSSD&+mdw zGw*d5|B?HUdiW_mTPUT}&P`9J&rR1=661L(tsPy*D-C(+oZ~lGxp`A|@o(xA*C~Uc z@w`gwrm^L%_3C5Awp&?e3g%jz>4uqb%CBgh4-(nDdcJ42BdXd3ni=R?Kli<1R%kHY zfHNYPv3OY|V{TZ&EG9)oUX$mpJ5sD3p(2zOo>V`*DzCFXN~GTI~39mxL^Q_*RnXa-kjtCvI2|^HSPmf-q9uDWo0g2Sd$X z)Bs@AW5 zrYXxNnFs`B`B(35{*opa_DDnjUyYnKAg{Cn#MK_Y$uoTCQ|%+a zB|FgrGML!81ee?M{RTZ@9mSzeSrYnKu9nMl2HM| zCW}B45Zd#$2oVZF%lQx!(@mEbc0!M8rmH0S-C%2ospR_Gn9T8f@tv;j?QKo*4%-KI zESeIfPoV21msB|zy158&)wVN|Qr9vUq<^WQ8JO^&lw)CE6Q(ufk9L6lGI@gI$#{G%scl*+MW5{2qSD;muE!YHPq#j6Dw&~Hpn3XqZ4xC7+;9q zBVZPP6w~p2uYW(TdmnAZJzAJ!Z-&QcR%}Y?KUY~qD)?0?k$RS&z7^_MUS=_~^_Z)OA5J@TsGq-Puci`*W<$3I}kkw9$r2*d;1#i8j);vkARfb*F z^!>$iJN@3t+jxKC+gW5}w@0&N_uVab>y~WY+I^~5#WTor=Paj5Dw&Hf2JGCm>T~(K zRW<9R%93m9e!H>nww~n{dhPY9{S_;{-Y!#noLEu!uJ_xwtv}yv+?TT_*svyeQrAVH zJij@%#b17=XB8a^6zN-3Akw;6L&o)?2A6xFMHc~~jS_)Np9&RqUA(kq;f0qam(n(` zEGiA1{?7Ko$`~H)(UYAY{*E=JlAX#(g%E5Pj zcGrEm3uTtC4fFo>d5K^A`qp@cLoa@Be>wS7PTIU(Rk}9)1#eszJY=0c<)iBY7ZUyd zP^0T;2iJ8AP2*d)?YmAqPD^{{`P5PG{lc?rX0N-mZ|PzA_J==rnp?h83P=jj5T43T zj`>=;(+^F57WZT3^3{c3YWL5dzwPVSzsKJHpX)zKW%D}E`AH(Ki6(K1zL&S$f8V}- b@n5Ec&ky|+?(dEVrcMS=S3j3^P6Nn{1`ISV`@iy0XB4ude`@%$Aj3=GV5o-U3d6>)Fx?9YxZm1+N2zV;dW*L&N% zOs`xpX%W!u5Z%bvWYwZ)#d^t8yim|@8DCL*$tV5*9;Gk#8}8_)6c{H=3tAzjUKk+U z*|lK%*C%Rf&XIi=aj;V3$@2?2xzrsK1NyzJs zzb5kCybw^bH*SAj%k>ihz9||U&W;jXhXMrJnhZo*7Ao+tCQ@67hu74qXir_srM&*D z{U6w_9oYNj&P|;?b<6D9KmIM7>7QRS+axD`zO;d2TU@PG72CRf*W+rm^7j_nsmrJ9 zh5vbXy!vI$pU_JiR_7~RnR@#9rOM|{ep}sMZRX&=x+=)ZJmJ*^8v6ihyaY$+H~vq* z@BV%BMowYAjm~PX+iy4SF}S_%eBzYq{%6nT&Yz^x-Q7KN=FFA-4}ARn&8tH*gXb;M zSoQz!qA2wl(gyGU?VPWl_ji)Y%-LC~WnIRtKtk>r!=H}7OqeonRqM!uBXD$mtk>*IG% z`8i$vm9oV9`V#B;wf~%f2L8Ezon@WY)rE#Mic{nOmy%r7DQ(c+rnFw`@8f%Qe@ofY zH2iP=KmQ9DHFAHKYjnN3ufIy?H2c?*B~wy=CTx6k>sFMRx%q2j4{K}doA>UWJ95P3 k*zx19?H~L2`90IGU!C(tir4K6Fncn1y85}Sb4q9e0HV#O-T(jq literal 0 HcmV?d00001 diff --git a/tetrispiecesS.png b/tetrispiecesS.png new file mode 100644 index 0000000000000000000000000000000000000000..50689344ad0e7fe4709b8cecd76121b22ebead6f GIT binary patch literal 833 zcmeAS@N?(olHy`uVBq!ia0vp^bAULDgAGV}7F&1%DaPU;cPEB*=VV?2IV|apzK#qG z8~eHcB(eheoCO|{#S9F5he4R}c>anM1_q{Uo-U3d6>)Fx8hS(rO0+#ZA2)#~ZAYX` ziFbFCHwWLNSNEDq3ST^0ki6jBrK?9LIa;L%S(WBEx^HhvcGJI;b9Mrco}h^09KE$S z7R&xUv2zdqoTvZ(3ss!-U1luq#KGhfca*+fWnea*|%yyt!F`@?{}2OZYGF0GA<4oN;FTcoXla?Gkdeo~%&hr=VhPn@LgC4H?0ZjG`p00i_ I>zopr0AU+t^#A|> literal 0 HcmV?d00001 diff --git a/tetrispiecesT.png b/tetrispiecesT.png new file mode 100644 index 0000000000000000000000000000000000000000..84daa076b0bcebf4a4d9322632bf0d892d55c12c GIT binary patch literal 1111 zcmeAS@N?(olHy`uVBq!ia0vp^%YZnCgAGVN_Vmfjq< z{`HR^7BOu9!dHfP{r&s*Y~^i(^0G1$d;9fKYsKCK>8&&pfQ_nE_4xUE=*h;prNugYVFO~ z+tS6^+1|y)#ah!(?<#y8&dc-Ukn+WfUtbn#P0f8;l)S6%@ACp{DgF4cFE1|2Pr71w z-*aKWg-e%$J|FwjX24(a{oS&4vA+#%gnEB}fB)j-Wc#V-&u1h(n)mBh9Y?=QV*dM$ zVPSb*Ss7Wc^n3gM3CVRH`~6;p=X1>dQ~v&srRNLJ>rAY-D|JiokX*$sN8nb12`&}OY-M!IxrXw%}y_P?&(f#uIdHw33o70xO*gyH~+54eu z`dB`eJH{Zu?> zo=MmtJ2mU8!=x!^`C6kdT3T6MxpHN0f8LpGiPv72zJ33`{q#l8<(H#kVy+xJ)>gFB z@b0d!(G@CE>*M~eX6Ij~>bZLLYAJd7@WmH7`dw2K5*)(U#blnl-FDM6Ej@kfzJ2Rz z>fD5dzhAj7{`~)p>gxWjn~H*ioR202PM$pZ;lvkne}`A9TiEvImvpb=6W+%mJZ%8c zE~_*1<`uB&0qN`O`@ek4jaeLB@-%xPB*Y(xTNn{1`ISV`@iy0XB4ude`@%$Aj3=GWHo-U3d6>)FxZuGvAD0BSd`QWzenf5{6 z-p(CdB8wO5Wn^XcGO4$#i7WCgS-Vv?$|`_;?q6mlCU(w4fm||Ni`t}(K6I#j*>R~S zzB|m$q<+WqarL%8q`1VRuX`-ps2+Z`a+mSGNiB2rt)upS_L@}k^5KkgcWlMuJrBhd`%9m< zTWRp-FUF=MuKzB z21T<)=-b{`v0RyUvG2baizz%gVwc!!CVTys={LlmS_AWl)xq~JsoR0C=*8K6C6tXrfbZywx9J9ySCs&4O)hF-% z^JP|5-kh%YD#^7Q*ZsP~UfP?o%c%J8e*K@z9^6mgoxOF|vgOMkYkP7jihRDAw?%z` zLJS&7mW%$jCzeYct(5q7{d@dJj@$?5g(s+M*S?ZF`KZ=t*8A;W-hK_w{QvgKl(qWT zmNMBfuKat$BDW%IYgC-bBoQ@r_0MnLzO^{=#6*gB?b@|j6+afaxw}6uousCwW@2Y2 z*FRari6gPN*tlO@6R4-&Zu;q`&bvjnM(IvHo%;35mm{lHS_KZ}GerDV?cT0p4$NB& Mp00i_>zopr02Dl(?EnA( literal 0 HcmV?d00001 diff --git a/tetrispiecesquare.png b/tetrispiecesquare.png new file mode 100644 index 0000000000000000000000000000000000000000..63c079f592254ddab1ba2fd31dd1e2292f9f39b2 GIT binary patch literal 743 zcmeAS@N?(olHy`uVBq!ia0vp^nLwPy!3HGvi`7p7QjEnx?oJHr&dIz4a#+$GeH|GX zHuiJ>Nn{1`ISV`@iy0XB4ude`@%$Aj3=B*)o-U3d6>)Fx7-CP(Kvo^EBU<#g;8coGu*LrI0>oQk7~Nk^Y)OL^yG?hc2$^Q>h8YHFCD zSO2Vi!2kS*4ExWjYxRmP0%@CP>de=g+QoJFp@E4M@7wq9k3ZKq{nY95%llVt|9?Ea^z#YP=?v&`uN>@YyD=mx1TkC=^k_O)A5N6GMoqKNN_bD zRM--wJJrjz)avyAr2Da3wPItBE|-b^{qEP_f7iA-`PUu${jO+Lh*sX6_1CKZ{rehR zZ*||w;)ccX2%ROjqhCH%jecKQDQSQ3;~$;7cT%p$y`OpcW0k*xJ5zxQ1HFibt6E#D zVt4JE#>fBI-QwK2B`Ondz5S|XwCK3|%Re@tfR|kvqV?)U#>%^Qe`w42`(HZlzU|)U zCn|PNE2>X_`=)uX{Q1e}OERUn9)Gm>Qe`{!^ivCsQ#E$iGfXZO?YwZ#&vk2*ZXHXy k-Tc+J-yV~Xmix<`b5hzS?dqjzz{JVm>FVdQ&MBb@0J6O+Jpcdz literal 0 HcmV?d00001 diff --git a/tetrispiecesrevL.png b/tetrispiecesrevL.png new file mode 100644 index 0000000000000000000000000000000000000000..156c876869c4b80f33947f7ac4e1edbd90552483 GIT binary patch literal 890 zcmeAS@N?(olHy`uVBq!ia0vp^SwK9CgAGVJZoba~60+7BevL9R^{>9s*GZ$uo~u-D_ry+bbDTe%I_MY9Ozb=|P&`a19A z&x6;`+|iwO_|B)-Q-Zvr9)0>`^!oK{OACvPeNMM;-0<+~=4n5?Y0H+7Nn5hmnjK$G z5vvUp7Z;C=jz0bDS=ywXD(h#tJDi)u6BN3AGz53?8dw!cHJn{X^ zaUyY&|H@VOZ{GTKXC3bhb#?XBy1IV{**t%JsoMMKD*MB$<{P7GzVZV7x@S*Kd1{Gw z(1FieRrI|>&rUg*@ZhRDjophdel2%tZSCEfD_nDfye#eP&Ye5w_gVH_h0VEt|EABG zSLbgu{YmMh)z1t&HmfwNJpS_g?~*A&uXfdKkh?r{&daY=OM|^MckkXE856Tdd*=lW z@1m(sq9+xu%2Hc=@w1BYS!V(J$vu-*{?`4!f3@8HO6H`S)%Fj6OneeM>9CLg{Pz_NMzsUV3r#v{9*v0_ z9qvwn=G|MPv#%dey{qzmHoHULq%}5M+0M;eQkld%N9| Date: Sun, 18 Oct 2026 09:06:13 -0400 Subject: [PATCH 04/39] added toml for dependancies for testing/algo --- algorithm/pyproject.toml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 algorithm/pyproject.toml diff --git a/algorithm/pyproject.toml b/algorithm/pyproject.toml new file mode 100644 index 0000000..e93ae3c --- /dev/null +++ b/algorithm/pyproject.toml @@ -0,0 +1,26 @@ +[build-system] +requires = ["setuptools>=75"] +build-backed = "setuptools.build_meta" + +[project] +name = "sticker-packer" +version = "0.1.0" +description = "Test-driven geometry and HPGL tools for packing sticker outlines" +requires-python = ">=3.11" +dependencies = [ + "shapely>=2.0, <3.0", + "svgpathtools>=1.6,<2.0", +] + +[project.optional-dependencies] +test = [ + "pytest>=8.0,<9.0", +] + +[tool.pytest.ini_options] +testpaths = ["test"] +pythonpath = ["."] +addopts = "-ra" + +[tool.setuptools.packages.find] +include = ["app*"] \ No newline at end of file From ab81375bc2728047689f62e56c14c49e34b1f3c7 Mon Sep 17 00:00:00 2001 From: Marigold <111760235+LiterallyAFlower@users.noreply.github.com> Date: Sun, 18 Oct 2026 09:07:30 -0400 Subject: [PATCH 05/39] added .gitignore, just for settings/os files and generated output, we dont want that added to repo --- algorithm/.gitignore | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 algorithm/.gitignore diff --git a/algorithm/.gitignore b/algorithm/.gitignore new file mode 100644 index 0000000..f6d9080 --- /dev/null +++ b/algorithm/.gitignore @@ -0,0 +1,31 @@ + +# Python bytecode and caches +__pycache__/ +*.py[cod] +.pytest_cache/ +.coverage +htmlcov/ + +# Virtual environments +.venv/ +venv/ +env/ + +# Packaging/build artifacts +build/ +dist/ +*.egg-info/ + +# IDE/editor settings +.vscode/ +.idea/ +*.swp +*.swo + +# OS files +.DS_Store +Thumbs.db + +# Generated output +output/* +!output/.gitkeep From 1835f5c84b2a6c006cfed5c576eb1c2168c0e88f Mon Sep 17 00:00:00 2001 From: Marigold <111760235+LiterallyAFlower@users.noreply.github.com> Date: Sun, 18 Oct 2026 09:42:51 -0400 Subject: [PATCH 06/39] test driven development, test fails, writing now. --- algorithm/tests/test_svg_input.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/algorithm/tests/test_svg_input.py b/algorithm/tests/test_svg_input.py index e69de29..099180a 100644 --- a/algorithm/tests/test_svg_input.py +++ b/algorithm/tests/test_svg_input.py @@ -0,0 +1,27 @@ +from pathlib import Path +import pytest +from app.svg.loader import load_sticker_svg + +FIXTURES = Path(__file__).parent / "fixtures" + +def test_svg_loads_as_one_nonempty_sticker(): + sticker = load_sticker_svg( + FIXTURES / "tetrispieceI.svg" + ) + + assert sticker is not None + assert not sticker.geometry.is_empty + assert sticker.geometry.is_valid + assert sticker.geometry.area > 0 + assert sticker.width > 0 + assert sticker.height > 0 + +def test_empty_svg_is_rejected(): + with pytest.raises( + ValueError, + match = "no usable sticker geometry", + ): + load_sticker_svg( + FIXTURES / "tetrispiecesL.svg" + ) + \ No newline at end of file From abce7c8134939acfcaf1f3e57992174671c6d81b Mon Sep 17 00:00:00 2001 From: Marigold <111760235+LiterallyAFlower@users.noreply.github.com> Date: Sun, 18 Oct 2026 09:54:23 -0400 Subject: [PATCH 07/39] additional test for creating smallest sticker model needed --- algorithm/tests/test_sticker_model.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 algorithm/tests/test_sticker_model.py diff --git a/algorithm/tests/test_sticker_model.py b/algorithm/tests/test_sticker_model.py new file mode 100644 index 0000000..cec79ad --- /dev/null +++ b/algorithm/tests/test_sticker_model.py @@ -0,0 +1,22 @@ +from shapely.geometry import Polygon +from app.models.sticker import Sticker + +def test_sticker_stores_geometry_and_dimensions(): + geometry = Polygon([ + (0,0), + (4,0), + (4,2), + (0,2), + ]) + + sticker = Sticker( + sticker_id = "test-rectangle", + geometry=geometry, + ) + + assert sticker.sticker_id == "test-rectangle" + assert sticker.geometry.equals(geometry) + assert sticker.width == 4 + assert sticker.height == 2 + assert sticker.area == 8 + \ No newline at end of file From 41b16f68db385b8ddd553a646f827a0d9b7a9c98 Mon Sep 17 00:00:00 2001 From: Marigold <111760235+LiterallyAFlower@users.noreply.github.com> Date: Sun, 18 Oct 2026 09:59:14 -0400 Subject: [PATCH 08/39] added sticker class, additional files. --- algorithm/app/models/sticker.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/algorithm/app/models/sticker.py b/algorithm/app/models/sticker.py index e69de29..0b778a6 100644 --- a/algorithm/app/models/sticker.py +++ b/algorithm/app/models/sticker.py @@ -0,0 +1,6 @@ +from dataclasses import dataclass +from shapely.geometry import Polygon + +@dataclass +class Sticker: + \ No newline at end of file From 528eed12d721b566365a6be352bc24ec3b12691e Mon Sep 17 00:00:00 2001 From: Marigold <111760235+LiterallyAFlower@users.noreply.github.com> Date: Sun, 18 Oct 2026 10:11:14 -0400 Subject: [PATCH 09/39] added sticker tests for sticker implementation with new model requirements --- algorithm/tests/fixtures/tetrispieceI.hpgl | 78 ++++++++++++++++++++++ algorithm/tests/test_sticker_model.py | 61 ++++++++++++----- 2 files changed, 123 insertions(+), 16 deletions(-) create mode 100644 algorithm/tests/fixtures/tetrispieceI.hpgl diff --git a/algorithm/tests/fixtures/tetrispieceI.hpgl b/algorithm/tests/fixtures/tetrispieceI.hpgl new file mode 100644 index 0000000..44edcdc --- /dev/null +++ b/algorithm/tests/fixtures/tetrispieceI.hpgl @@ -0,0 +1,78 @@ +%-12345X@PJL SET RESOLUTION = 300 +@PJL SET ORIENTATION = LANDSCAPE +@PJL ENTER LANGUAGE = HPGL2 +%1B +BP5,1;IN;SP1;IW;PS9600,7100;TR0;IP0,0,7100,9600;SC; +PC1,82,229,250; +PU51928,206583; +PM0; +PU51928,206583; +PD51928,204548,51928,202513,51928,200479,51928,198444,51928,196410,51928,194375,51928,192341,51928,190306,51928,188272,51928,186237,51928,184203,51928,182168,51928,180134,51928,178099,51928,176065,51928,174030,51928,171995,51928,169961,51928,167926,51928,165892,51928,163857,51928,161823,51928,159788,51928,157754,49889,157754,47851,157754,45813,157754,43775,157754,41737,157754,39699,157754,37661,157754,35624,157754,33586,157754,31548,157754,29510,157754,27473,157754,25435,157754,23397,157754,21359,157754,19322,157754,17284,157754,15246,157754,13208,157754,11170,157754,9132,157754,7094,157754,5056,157754,3018,157754,3018,159788,3018,161823,3018,163857,3018,165892,3018,167926,3018,169961,3018,171995,3018,174030,3018,176065,3018,178099,3018,180134,3018,182168,3018,184203,3018,186237,3018,188272,3018,190306,3018,192341,3018,194375,3018,196410,3018,198444,3018,200479,3018,202513,3018,204548,3018,206583,5056,206583,7094,206583,9132,206583,11170,206583,13208,206583,15246,206583,17284,206583,19322,206583,21359,206583,23397,206583,25435,206583,27473,206583,29510,206583,31548,206583,33586,206583,35624,206583,37661,206583,39699,206583,41737,206583,43775,206583,45813,206583,47851,206583,49889,206583,51928,206583; +PU;PM1; +PU51928,155224; +PD51928,153191,51928,151157,51928,149124,51928,147091,51928,145058,51928,143024,51928,140991,51928,138958,51928,136925,51928,134891,51928,132858,51928,130825,51928,128791,51928,126758,51928,124725,51928,122692,51928,120658,51928,118625,51928,116592,51928,114559,51928,112525,51928,110492,51928,108459,51928,106426,49889,106426,47851,106426,45813,106426,43774,106426,41736,106426,39698,106426,37659,106426,35621,106426,33583,106426,31544,106426,29506,106426,27468,106426,25429,106426,23391,106426,21352,106426,19314,106426,17276,106426,15237,106426,13199,106426,11161,106426,9122,106426,7084,106426,5046,106426,3007,106426,3007,108459,3007,110492,3007,112525,3007,114559,3007,116592,3007,118625,3007,120658,3007,122692,3007,124725,3007,126758,3007,128791,3007,130825,3007,132858,3007,134891,3007,136925,3007,138958,3007,140991,3007,143024,3007,145058,3007,147091,3007,149124,3007,151157,3007,153191,3007,155224,5046,155224,7084,155224,9122,155224,11161,155224,13199,155224,15237,155224,17276,155224,19314,155224,21352,155224,23391,155224,25429,155224,27468,155224,29506,155224,31544,155224,33583,155224,35621,155224,37659,155224,39698,155224,41736,155224,43774,155224,45813,155224,47851,155224,49889,155224,51928,155224; +PU;PM1; +PU51938,103378; +PD51938,101355,51938,99331,51938,97308,51938,95285,51938,93262,51938,91239,51938,89216,51938,87193,51938,85170,51938,83147,51938,81123,51938,79100,51938,77077,51938,75054,51938,73031,51938,71008,51938,68985,51938,66962,51938,64939,51938,62916,51938,60892,51938,58869,51938,56846,51938,54823,49898,54823,47859,54823,45819,54823,43780,54823,41741,54823,39702,54823,37663,54823,35624,54823,33585,54823,31546,54823,29507,54823,27469,54823,25430,54823,23392,54823,21353,54823,19315,54823,17276,54823,15238,54823,13199,54823,11161,54823,9122,54823,7084,54823,5046,54823,3007,54823,3007,56832,3007,58841,3007,60851,3007,62860,3007,64869,3007,66878,3007,68887,3007,70896,3007,72905,3007,74914,3007,76924,3007,78933,3007,80942,3007,82951,3007,84960,3007,86969,3007,88978,3007,90988,3007,92997,3007,95006,3007,97015,3007,99024,3007,101033,3007,103042,3322,103280,3679,103485,4073,103660,4502,103807,4961,103927,5448,104023,5959,104097,6491,104151,7040,104186,7602,104205,8175,104209,8755,104201,9339,104182,9922,104154,10503,104120,11076,104081,11639,104039,12188,103997,12721,103955,13233,103917,13721,103884,14181,103858,14611,103841,15006,103835,16051,103835,17097,103835,18141,103835,19186,103835,20231,103835,21276,103835,22320,103835,23365,103835,24409,103835,25454,103835,26498,103835,27542,103835,28587,103835,29631,103835,30675,103835,31719,103835,32763,103835,33807,103835,34851,103835,35895,103835,36939,103835,37983,103835,39027,103835,40071,103835,40533,103839,41005,103850,41487,103867,41978,103888,42476,103913,42981,103941,43491,103969,44006,103996,44523,104022,45043,104046,45564,104065,46084,104079,46604,104086,47122,104085,47636,104076,48146,104056,48650,104025,49148,103981,49638,103922,50120,103849,50592,103759,51053,103651,51502,103525,51938,103378; +PU;PM1; +PU51928,52395; +PD51928,50350,51928,48306,51928,46261,51928,44217,51928,42173,51928,40128,51928,38084,51928,36040,51928,33996,51928,31952,51928,29908,51928,27864,51928,25820,51928,23776,51928,21731,51928,19687,51928,17643,51928,15599,51928,13555,51928,11510,51928,9466,51928,7422,51928,5377,51928,3332,49889,3332,47851,3332,45813,3332,43775,3332,41737,3332,39699,3332,37661,3332,35624,3332,33586,3332,31548,3332,29510,3332,27473,3332,25435,3332,23397,3332,21359,3332,19322,3332,17284,3332,15246,3332,13208,3332,11170,3332,9132,3332,7094,3332,5056,3332,3018,3332,3018,5377,3018,7422,3018,9466,3018,11510,3018,13555,3018,15599,3018,17643,3018,19687,3018,21731,3018,23776,3018,25820,3018,27864,3018,29908,3018,31952,3018,33996,3018,36040,3018,38084,3018,40128,3018,42173,3018,44217,3018,46261,3018,48306,3018,50350,3018,52395,5056,52395,7094,52395,9132,52395,11170,52395,13208,52395,15246,52395,17284,52395,19322,52395,21359,52395,23397,52395,25435,52395,27473,52395,29510,52395,31548,52395,33586,52395,35624,52395,37661,52395,39699,52395,41737,52395,43775,52395,45813,52395,47851,52395,49889,52395,51928,52395; +PU;PM1; +PM2;FP; +IW; +PU51928,206583; +PD51928,204548,51928,202513,51928,200479,51928,198444,51928,196410,51928,194375,51928,192341,51928,190306,51928,188272,51928,186237,51928,184203,51928,182168,51928,180134,51928,178099,51928,176065,51928,174030,51928,171995,51928,169961,51928,167926,51928,165892,51928,163857,51928,161823,51928,159788,51928,157754,49889,157754,47851,157754,45813,157754,43775,157754,41737,157754,39699,157754,37661,157754,35624,157754,33586,157754,31548,157754,29510,157754,27473,157754,25435,157754,23397,157754,21359,157754,19322,157754,17284,157754,15246,157754,13208,157754,11170,157754,9132,157754,7094,157754,5056,157754,3018,157754,3018,159788,3018,161823,3018,163857,3018,165892,3018,167926,3018,169961,3018,171995,3018,174030,3018,176065,3018,178099,3018,180134,3018,182168,3018,184203,3018,186237,3018,188272,3018,190306,3018,192341,3018,194375,3018,196410,3018,198444,3018,200479,3018,202513,3018,204548,3018,206583,5056,206583,7094,206583,9132,206583,11170,206583,13208,206583,15246,206583,17284,206583,19322,206583,21359,206583,23397,206583,25435,206583,27473,206583,29510,206583,31548,206583,33586,206583,35624,206583,37661,206583,39699,206583,41737,206583,43775,206583,45813,206583,47851,206583,49889,206583,51928,206583,51928,206583; +PU51928,155224; +PD51928,153191,51928,151157,51928,149124,51928,147091,51928,145058,51928,143024,51928,140991,51928,138958,51928,136925,51928,134891,51928,132858,51928,130825,51928,128791,51928,126758,51928,124725,51928,122692,51928,120658,51928,118625,51928,116592,51928,114559,51928,112525,51928,110492,51928,108459,51928,106426,49889,106426,47851,106426,45813,106426,43774,106426,41736,106426,39698,106426,37659,106426,35621,106426,33583,106426,31544,106426,29506,106426,27468,106426,25429,106426,23391,106426,21352,106426,19314,106426,17276,106426,15237,106426,13199,106426,11161,106426,9122,106426,7084,106426,5046,106426,3007,106426,3007,108459,3007,110492,3007,112525,3007,114559,3007,116592,3007,118625,3007,120658,3007,122692,3007,124725,3007,126758,3007,128791,3007,130825,3007,132858,3007,134891,3007,136925,3007,138958,3007,140991,3007,143024,3007,145058,3007,147091,3007,149124,3007,151157,3007,153191,3007,155224,5046,155224,7084,155224,9122,155224,11161,155224,13199,155224,15237,155224,17276,155224,19314,155224,21352,155224,23391,155224,25429,155224,27468,155224,29506,155224,31544,155224,33583,155224,35621,155224,37659,155224,39698,155224,41736,155224,43774,155224,45813,155224,47851,155224,49889,155224,51928,155224,51928,155224; +PU51938,103378; +PD51938,101355,51938,99331,51938,97308,51938,95285,51938,93262,51938,91239,51938,89216,51938,87193,51938,85170,51938,83147,51938,81123,51938,79100,51938,77077,51938,75054,51938,73031,51938,71008,51938,68985,51938,66962,51938,64939,51938,62916,51938,60892,51938,58869,51938,56846,51938,54823,49898,54823,47859,54823,45819,54823,43780,54823,41741,54823,39702,54823,37663,54823,35624,54823,33585,54823,31546,54823,29507,54823,27469,54823,25430,54823,23392,54823,21353,54823,19315,54823,17276,54823,15238,54823,13199,54823,11161,54823,9122,54823,7084,54823,5046,54823,3007,54823,3007,56832,3007,58841,3007,60851,3007,62860,3007,64869,3007,66878,3007,68887,3007,70896,3007,72905,3007,74914,3007,76924,3007,78933,3007,80942,3007,82951,3007,84960,3007,86969,3007,88978,3007,90988,3007,92997,3007,95006,3007,97015,3007,99024,3007,101033,3007,103042,3322,103280,3679,103485,4073,103660,4502,103807,4961,103927,5448,104023,5959,104097,6491,104151,7040,104186,7602,104205,8175,104209,8755,104201,9339,104182,9922,104154,10503,104120,11076,104081,11639,104039,12188,103997,12721,103955,13233,103917,13721,103884,14181,103858,14611,103841,15006,103835,16051,103835,17097,103835,18141,103835,19186,103835,20231,103835,21276,103835,22320,103835,23365,103835,24409,103835,25454,103835,26498,103835,27542,103835,28587,103835,29631,103835,30675,103835,31719,103835,32763,103835,33807,103835,34851,103835,35895,103835,36939,103835,37983,103835,39027,103835,40071,103835,40533,103839,41005,103850,41487,103867,41978,103888,42476,103913,42981,103941,43491,103969,44006,103996,44523,104022,45043,104046,45564,104065,46084,104079,46604,104086,47122,104085,47636,104076,48146,104056,48650,104025,49148,103981,49638,103922,50120,103849,50592,103759,51053,103651,51502,103525,51938,103378,51938,103378; +PU51928,52395; +PD51928,50350,51928,48306,51928,46261,51928,44217,51928,42173,51928,40128,51928,38084,51928,36040,51928,33996,51928,31952,51928,29908,51928,27864,51928,25820,51928,23776,51928,21731,51928,19687,51928,17643,51928,15599,51928,13555,51928,11510,51928,9466,51928,7422,51928,5377,51928,3332,49889,3332,47851,3332,45813,3332,43775,3332,41737,3332,39699,3332,37661,3332,35624,3332,33586,3332,31548,3332,29510,3332,27473,3332,25435,3332,23397,3332,21359,3332,19322,3332,17284,3332,15246,3332,13208,3332,11170,3332,9132,3332,7094,3332,5056,3332,3018,3332,3018,5377,3018,7422,3018,9466,3018,11510,3018,13555,3018,15599,3018,17643,3018,19687,3018,21731,3018,23776,3018,25820,3018,27864,3018,29908,3018,31952,3018,33996,3018,36040,3018,38084,3018,40128,3018,42173,3018,44217,3018,46261,3018,48306,3018,50350,3018,52395,5056,52395,7094,52395,9132,52395,11170,52395,13208,52395,15246,52395,17284,52395,19322,52395,21359,52395,23397,52395,25435,52395,27473,52395,29510,52395,31548,52395,33586,52395,35624,52395,37661,52395,39699,52395,41737,52395,43775,52395,45813,52395,47851,52395,49889,52395,51928,52395,51928,52395; +PC1,2,5,6; +PU996,209153; +PM0; +PU996,209153; +PD3193,209153,5390,209153,7587,209153,9783,209152,11980,209152,14177,209151,16373,209151,18569,209150,20766,209149,22962,209148,25158,209148,27354,209147,29551,209146,31747,209145,33943,209144,36139,209142,38335,209141,40531,209140,42726,209139,44922,209138,47118,209136,49314,209135,51510,209134,53706,209133,53707,200459,53708,191785,53709,183111,53711,174437,53712,165764,53713,157090,53714,148417,53715,139743,53717,131070,53718,122397,53719,113723,53720,105050,53721,96377,53721,87704,53722,79031,53723,70358,53724,61685,53724,53012,53725,44339,53725,35667,53726,26994,53726,18321,53726,9648,53726,975,51535,988,49344,1001,47153,1013,44962,1026,42771,1039,40580,1052,38388,1064,36197,1077,34006,1090,31814,1102,29622,1115,27431,1128,25239,1140,23047,1153,20855,1166,18664,1179,16472,1191,14280,1204,12088,1217,9896,1229,7704,1242,5512,1255,3320,1267,1128,1280,1123,9942,1118,18603,1112,27264,1107,35926,1102,44587,1097,53248,1092,61910,1087,70571,1082,79232,1076,87894,1071,96555,1066,105217,1060,113878,1055,122539,1049,131201,1043,139862,1038,148523,1032,157185,1026,165846,1020,174508,1014,183169,1008,191830,1002,200492,996,209153; +PU;PM1; +PU51928,206583; +PD49889,206583,47851,206583,45813,206583,43775,206583,41737,206583,39699,206583,37661,206583,35624,206583,33586,206583,31548,206583,29510,206583,27473,206583,25435,206583,23397,206583,21359,206583,19322,206583,17284,206583,15246,206583,13208,206583,11170,206583,9132,206583,7094,206583,5056,206583,3018,206583,3018,204548,3018,202513,3018,200479,3018,198444,3018,196410,3018,194375,3018,192341,3018,190306,3018,188272,3018,186237,3018,184203,3018,182168,3018,180134,3018,178099,3018,176065,3018,174030,3018,171995,3018,169961,3018,167926,3018,165892,3018,163857,3018,161823,3018,159788,3018,157754,5056,157754,7094,157754,9132,157754,11170,157754,13208,157754,15246,157754,17284,157754,19322,157754,21359,157754,23397,157754,25435,157754,27473,157754,29510,157754,31548,157754,33586,157754,35624,157754,37661,157754,39699,157754,41737,157754,43775,157754,45813,157754,47851,157754,49889,157754,51928,157754,51928,159788,51928,161823,51928,163857,51928,165892,51928,167926,51928,169961,51928,171995,51928,174030,51928,176065,51928,178099,51928,180134,51928,182168,51928,184203,51928,186237,51928,188272,51928,190306,51928,192341,51928,194375,51928,196410,51928,198444,51928,200479,51928,202513,51928,204548,51928,206583; +PU;PM1; +PU51928,155224; +PD49889,155224,47851,155224,45813,155224,43774,155224,41736,155224,39698,155224,37659,155224,35621,155224,33583,155224,31544,155224,29506,155224,27468,155224,25429,155224,23391,155224,21352,155224,19314,155224,17276,155224,15237,155224,13199,155224,11161,155224,9122,155224,7084,155224,5046,155224,3007,155224,3007,153191,3007,151157,3007,149124,3007,147091,3007,145058,3007,143024,3007,140991,3007,138958,3007,136925,3007,134891,3007,132858,3007,130825,3007,128791,3007,126758,3007,124725,3007,122692,3007,120658,3007,118625,3007,116592,3007,114559,3007,112525,3007,110492,3007,108459,3007,106426,5046,106426,7084,106426,9122,106426,11161,106426,13199,106426,15237,106426,17276,106426,19314,106426,21352,106426,23391,106426,25429,106426,27468,106426,29506,106426,31544,106426,33583,106426,35621,106426,37659,106426,39698,106426,41736,106426,43774,106426,45813,106426,47851,106426,49889,106426,51928,106426,51928,108459,51928,110492,51928,112525,51928,114559,51928,116592,51928,118625,51928,120658,51928,122692,51928,124725,51928,126758,51928,128791,51928,130825,51928,132858,51928,134891,51928,136925,51928,138958,51928,140991,51928,143024,51928,145058,51928,147091,51928,149124,51928,151157,51928,153191,51928,155224; +PU;PM1; +PU51938,103378; +PD51502,103525,51053,103651,50592,103759,50120,103849,49638,103922,49148,103981,48650,104025,48146,104056,47636,104076,47122,104085,46604,104086,46084,104079,45564,104065,45043,104046,44523,104022,44006,103996,43491,103969,42981,103941,42476,103913,41978,103888,41487,103867,41005,103850,40533,103839,40071,103835,39027,103835,37983,103835,36939,103835,35895,103835,34851,103835,33807,103835,32763,103835,31719,103835,30675,103835,29631,103835,28587,103835,27542,103835,26498,103835,25454,103835,24409,103835,23365,103835,22320,103835,21276,103835,20231,103835,19186,103835,18141,103835,17097,103835,16051,103835,15006,103835,14611,103841,14181,103858,13721,103884,13233,103917,12721,103955,12188,103997,11639,104039,11076,104081,10503,104120,9922,104154,9339,104182,8755,104201,8175,104209,7602,104205,7040,104186,6491,104151,5959,104097,5448,104023,4961,103927,4502,103807,4073,103660,3679,103485,3322,103280,3007,103042,3007,101033,3007,99024,3007,97015,3007,95006,3007,92997,3007,90988,3007,88978,3007,86969,3007,84960,3007,82951,3007,80942,3007,78933,3007,76924,3007,74914,3007,72905,3007,70896,3007,68887,3007,66878,3007,64869,3007,62860,3007,60851,3007,58841,3007,56832,3007,54823,5046,54823,7084,54823,9122,54823,11161,54823,13199,54823,15238,54823,17276,54823,19315,54823,21353,54823,23392,54823,25430,54823,27469,54823,29507,54823,31546,54823,33585,54823,35624,54823,37663,54823,39702,54823,41741,54823,43780,54823,45819,54823,47859,54823,49898,54823,51938,54823,51938,56846,51938,58869,51938,60892,51938,62916,51938,64939,51938,66962,51938,68985,51938,71008,51938,73031,51938,75054,51938,77077,51938,79100,51938,81123,51938,83147,51938,85170,51938,87193,51938,89216,51938,91239,51938,93262,51938,95285,51938,97308,51938,99331,51938,101355,51938,103378; +PU;PM1; +PU51928,52395; +PD49889,52395,47851,52395,45813,52395,43775,52395,41737,52395,39699,52395,37661,52395,35624,52395,33586,52395,31548,52395,29510,52395,27473,52395,25435,52395,23397,52395,21359,52395,19322,52395,17284,52395,15246,52395,13208,52395,11170,52395,9132,52395,7094,52395,5056,52395,3018,52395,3018,50350,3018,48306,3018,46261,3018,44217,3018,42173,3018,40128,3018,38084,3018,36040,3018,33996,3018,31952,3018,29908,3018,27864,3018,25820,3018,23776,3018,21731,3018,19687,3018,17643,3018,15599,3018,13555,3018,11510,3018,9466,3018,7422,3018,5377,3018,3332,5056,3332,7094,3332,9132,3332,11170,3332,13208,3332,15246,3332,17284,3332,19322,3332,21359,3332,23397,3332,25435,3332,27473,3332,29510,3332,31548,3332,33586,3332,35624,3332,37661,3332,39699,3332,41737,3332,43775,3332,45813,3332,47851,3332,49889,3332,51928,3332,51928,5377,51928,7422,51928,9466,51928,11510,51928,13555,51928,15599,51928,17643,51928,19687,51928,21731,51928,23776,51928,25820,51928,27864,51928,29908,51928,31952,51928,33996,51928,36040,51928,38084,51928,40128,51928,42173,51928,44217,51928,46261,51928,48306,51928,50350,51928,52395; +PU;PM1; +PM2;FP; +IW; +PU996,209153; +PD3193,209153,5390,209153,7587,209153,9783,209152,11980,209152,14177,209151,16373,209151,18569,209150,20766,209149,22962,209148,25158,209148,27354,209147,29551,209146,31747,209145,33943,209144,36139,209142,38335,209141,40531,209140,42726,209139,44922,209138,47118,209136,49314,209135,51510,209134,53706,209133,53707,200459,53708,191785,53709,183111,53711,174437,53712,165764,53713,157090,53714,148417,53715,139743,53717,131070,53718,122397,53719,113723,53720,105050,53721,96377,53721,87704,53722,79031,53723,70358,53724,61685,53724,53012,53725,44339,53725,35667,53726,26994,53726,18321,53726,9648,53726,975,51535,988,49344,1001,47153,1013,44962,1026,42771,1039,40580,1052,38388,1064,36197,1077,34006,1090,31814,1102,29622,1115,27431,1128,25239,1140,23047,1153,20855,1166,18664,1179,16472,1191,14280,1204,12088,1217,9896,1229,7704,1242,5512,1255,3320,1267,1128,1280,1123,9942,1118,18603,1112,27264,1107,35926,1102,44587,1097,53248,1092,61910,1087,70571,1082,79232,1076,87894,1071,96555,1066,105217,1060,113878,1055,122539,1049,131201,1043,139862,1038,148523,1032,157185,1026,165846,1020,174508,1014,183169,1008,191830,1002,200492,996,209153,996,209153; +PU51928,206583; +PD49889,206583,47851,206583,45813,206583,43775,206583,41737,206583,39699,206583,37661,206583,35624,206583,33586,206583,31548,206583,29510,206583,27473,206583,25435,206583,23397,206583,21359,206583,19322,206583,17284,206583,15246,206583,13208,206583,11170,206583,9132,206583,7094,206583,5056,206583,3018,206583,3018,204548,3018,202513,3018,200479,3018,198444,3018,196410,3018,194375,3018,192341,3018,190306,3018,188272,3018,186237,3018,184203,3018,182168,3018,180134,3018,178099,3018,176065,3018,174030,3018,171995,3018,169961,3018,167926,3018,165892,3018,163857,3018,161823,3018,159788,3018,157754,5056,157754,7094,157754,9132,157754,11170,157754,13208,157754,15246,157754,17284,157754,19322,157754,21359,157754,23397,157754,25435,157754,27473,157754,29510,157754,31548,157754,33586,157754,35624,157754,37661,157754,39699,157754,41737,157754,43775,157754,45813,157754,47851,157754,49889,157754,51928,157754,51928,159788,51928,161823,51928,163857,51928,165892,51928,167926,51928,169961,51928,171995,51928,174030,51928,176065,51928,178099,51928,180134,51928,182168,51928,184203,51928,186237,51928,188272,51928,190306,51928,192341,51928,194375,51928,196410,51928,198444,51928,200479,51928,202513,51928,204548,51928,206583,51928,206583; +PU51928,155224; +PD49889,155224,47851,155224,45813,155224,43774,155224,41736,155224,39698,155224,37659,155224,35621,155224,33583,155224,31544,155224,29506,155224,27468,155224,25429,155224,23391,155224,21352,155224,19314,155224,17276,155224,15237,155224,13199,155224,11161,155224,9122,155224,7084,155224,5046,155224,3007,155224,3007,153191,3007,151157,3007,149124,3007,147091,3007,145058,3007,143024,3007,140991,3007,138958,3007,136925,3007,134891,3007,132858,3007,130825,3007,128791,3007,126758,3007,124725,3007,122692,3007,120658,3007,118625,3007,116592,3007,114559,3007,112525,3007,110492,3007,108459,3007,106426,5046,106426,7084,106426,9122,106426,11161,106426,13199,106426,15237,106426,17276,106426,19314,106426,21352,106426,23391,106426,25429,106426,27468,106426,29506,106426,31544,106426,33583,106426,35621,106426,37659,106426,39698,106426,41736,106426,43774,106426,45813,106426,47851,106426,49889,106426,51928,106426,51928,108459,51928,110492,51928,112525,51928,114559,51928,116592,51928,118625,51928,120658,51928,122692,51928,124725,51928,126758,51928,128791,51928,130825,51928,132858,51928,134891,51928,136925,51928,138958,51928,140991,51928,143024,51928,145058,51928,147091,51928,149124,51928,151157,51928,153191,51928,155224,51928,155224; +PU51938,103378; +PD51502,103525,51053,103651,50592,103759,50120,103849,49638,103922,49148,103981,48650,104025,48146,104056,47636,104076,47122,104085,46604,104086,46084,104079,45564,104065,45043,104046,44523,104022,44006,103996,43491,103969,42981,103941,42476,103913,41978,103888,41487,103867,41005,103850,40533,103839,40071,103835,39027,103835,37983,103835,36939,103835,35895,103835,34851,103835,33807,103835,32763,103835,31719,103835,30675,103835,29631,103835,28587,103835,27542,103835,26498,103835,25454,103835,24409,103835,23365,103835,22320,103835,21276,103835,20231,103835,19186,103835,18141,103835,17097,103835,16051,103835,15006,103835,14611,103841,14181,103858,13721,103884,13233,103917,12721,103955,12188,103997,11639,104039,11076,104081,10503,104120,9922,104154,9339,104182,8755,104201,8175,104209,7602,104205,7040,104186,6491,104151,5959,104097,5448,104023,4961,103927,4502,103807,4073,103660,3679,103485,3322,103280,3007,103042,3007,101033,3007,99024,3007,97015,3007,95006,3007,92997,3007,90988,3007,88978,3007,86969,3007,84960,3007,82951,3007,80942,3007,78933,3007,76924,3007,74914,3007,72905,3007,70896,3007,68887,3007,66878,3007,64869,3007,62860,3007,60851,3007,58841,3007,56832,3007,54823,5046,54823,7084,54823,9122,54823,11161,54823,13199,54823,15238,54823,17276,54823,19315,54823,21353,54823,23392,54823,25430,54823,27469,54823,29507,54823,31546,54823,33585,54823,35624,54823,37663,54823,39702,54823,41741,54823,43780,54823,45819,54823,47859,54823,49898,54823,51938,54823,51938,56846,51938,58869,51938,60892,51938,62916,51938,64939,51938,66962,51938,68985,51938,71008,51938,73031,51938,75054,51938,77077,51938,79100,51938,81123,51938,83147,51938,85170,51938,87193,51938,89216,51938,91239,51938,93262,51938,95285,51938,97308,51938,99331,51938,101355,51938,103378,51938,103378; +PU51928,52395; +PD49889,52395,47851,52395,45813,52395,43775,52395,41737,52395,39699,52395,37661,52395,35624,52395,33586,52395,31548,52395,29510,52395,27473,52395,25435,52395,23397,52395,21359,52395,19322,52395,17284,52395,15246,52395,13208,52395,11170,52395,9132,52395,7094,52395,5056,52395,3018,52395,3018,50350,3018,48306,3018,46261,3018,44217,3018,42173,3018,40128,3018,38084,3018,36040,3018,33996,3018,31952,3018,29908,3018,27864,3018,25820,3018,23776,3018,21731,3018,19687,3018,17643,3018,15599,3018,13555,3018,11510,3018,9466,3018,7422,3018,5377,3018,3332,5056,3332,7094,3332,9132,3332,11170,3332,13208,3332,15246,3332,17284,3332,19322,3332,21359,3332,23397,3332,25435,3332,27473,3332,29510,3332,31548,3332,33586,3332,35624,3332,37661,3332,39699,3332,41737,3332,43775,3332,45813,3332,47851,3332,49889,3332,51928,3332,51928,5377,51928,7422,51928,9466,51928,11510,51928,13555,51928,15599,51928,17643,51928,19687,51928,21731,51928,23776,51928,25820,51928,27864,51928,29908,51928,31952,51928,33996,51928,36040,51928,38084,51928,40128,51928,42173,51928,44217,51928,46261,51928,48306,51928,50350,51928,52395,51928,52395; +PC1,178,178,178; +PU20,209133; +PM0; +PU20,209133; +PD61,209134,102,209135,142,209136,183,209138,224,209139,264,209140,305,209141,345,209142,386,209144,427,209145,467,209146,508,209147,549,209148,589,209148,630,209149,671,209150,711,209151,752,209151,792,209152,833,209152,874,209153,914,209153,955,209153,996,209153,1002,200492,1008,191830,1014,183169,1020,174508,1026,165846,1032,157185,1038,148523,1043,139862,1049,131201,1055,122539,1060,113878,1066,105217,1071,96555,1076,87894,1082,79232,1087,70571,1092,61910,1097,53248,1102,44587,1107,35926,1112,27264,1118,18603,1123,9942,1128,1280,3320,1267,5512,1255,7704,1242,9896,1229,12088,1217,14280,1204,16472,1191,18664,1179,20855,1166,23047,1153,25239,1140,27431,1128,29622,1115,31814,1102,34006,1090,36197,1077,38388,1064,40580,1052,42771,1039,44962,1026,47153,1013,49344,1001,51535,988,53726,975,53726,959,53726,942,53726,926,53726,909,53726,893,53726,876,53726,860,53726,843,53726,827,53726,810,53726,794,53726,777,53726,761,53726,744,53726,728,53726,711,53726,695,53726,678,53726,662,53726,645,53726,629,53726,612,53726,596,53726,579,52202,531,50676,491,49150,459,47623,433,46096,414,44567,400,43039,392,41510,389,39980,390,38450,394,36920,402,35390,413,33859,425,32329,440,30799,455,29268,472,27738,488,26208,503,24679,518,23150,531,21621,543,20093,551,18565,557,17038,559,16384,553,15711,536,15021,509,14316,476,13597,437,12867,395,12128,351,11381,308,10629,267,9874,231,9116,200,8359,178,7604,165,6854,165,6109,178,5372,207,4646,253,3931,319,3230,406,2545,516,1877,652,1229,815,603,1007,0,1229,0,9892,0,18555,0,27217,1,35880,1,44543,2,53205,2,61868,3,70530,4,79193,5,87856,5,96518,6,105181,7,113844,8,122506,9,131169,11,139832,12,148494,13,157157,14,165819,15,174482,17,183145,18,191807,19,200470,20,209133; +PM1;PM2;FP; +IW; +PU20,209133; +PD61,209134,102,209135,142,209136,183,209138,224,209139,264,209140,305,209141,345,209142,386,209144,427,209145,467,209146,508,209147,549,209148,589,209148,630,209149,671,209150,711,209151,752,209151,792,209152,833,209152,874,209153,914,209153,955,209153,996,209153,1002,200492,1008,191830,1014,183169,1020,174508,1026,165846,1032,157185,1038,148523,1043,139862,1049,131201,1055,122539,1060,113878,1066,105217,1071,96555,1076,87894,1082,79232,1087,70571,1092,61910,1097,53248,1102,44587,1107,35926,1112,27264,1118,18603,1123,9942,1128,1280,3320,1267,5512,1255,7704,1242,9896,1229,12088,1217,14280,1204,16472,1191,18664,1179,20855,1166,23047,1153,25239,1140,27431,1128,29622,1115,31814,1102,34006,1090,36197,1077,38388,1064,40580,1052,42771,1039,44962,1026,47153,1013,49344,1001,51535,988,53726,975,53726,959,53726,942,53726,926,53726,909,53726,893,53726,876,53726,860,53726,843,53726,827,53726,810,53726,794,53726,777,53726,761,53726,744,53726,728,53726,711,53726,695,53726,678,53726,662,53726,645,53726,629,53726,612,53726,596,53726,579,52202,531,50676,491,49150,459,47623,433,46096,414,44567,400,43039,392,41510,389,39980,390,38450,394,36920,402,35390,413,33859,425,32329,440,30799,455,29268,472,27738,488,26208,503,24679,518,23150,531,21621,543,20093,551,18565,557,17038,559,16384,553,15711,536,15021,509,14316,476,13597,437,12867,395,12128,351,11381,308,10629,267,9874,231,9116,200,8359,178,7604,165,6854,165,6109,178,5372,207,4646,253,3931,319,3230,406,2545,516,1877,652,1229,815,603,1007,0,1229,0,9892,0,18555,0,27217,1,35880,1,44543,2,53205,2,61868,3,70530,4,79193,5,87856,5,96518,6,105181,7,113844,8,122506,9,131169,11,139832,12,148494,13,157157,14,165819,15,174482,17,183145,18,191807,19,200470,20,209133,20,209133; +PC1,238,238,238; +PU0,1229; +PM0; +PU0,1229; +PD603,1007,1229,815,1877,652,2545,516,3230,406,3931,319,4646,253,5372,207,6109,178,6854,165,7604,165,8359,178,9116,200,9874,231,10629,267,11381,308,12128,351,12867,395,13597,437,14316,476,15021,509,15711,536,16384,553,17038,559,18565,557,20093,551,21621,543,23150,531,24679,518,26208,503,27738,488,29268,472,30799,455,32329,440,33859,425,35390,413,36920,402,38450,394,39980,390,41510,389,43039,392,44567,400,46096,414,47623,433,49150,459,50676,491,52202,531,53726,579,53726,555,53726,531,53726,507,53725,483,53725,458,53724,434,53724,410,53723,386,53722,362,53721,338,53721,314,53720,290,53719,265,53718,241,53717,217,53715,193,53714,169,53713,145,53712,121,53711,97,53709,72,53708,48,53707,24,53706,0,51469,0,49233,0,46996,0,44759,0,42522,0,40285,0,38048,0,35811,0,33574,0,31337,0,29100,0,26863,0,24626,0,22389,0,20152,0,17915,0,15678,0,13441,0,11204,0,8967,0,6730,0,4493,0,2257,0,20,0,19,51,18,102,17,153,15,204,14,255,13,306,12,358,11,409,9,460,8,512,7,563,6,615,5,666,5,718,4,769,3,820,2,872,2,923,1,974,1,1025,0,1077,0,1128,0,1178,0,1229; +PM1;PM2;FP; +IW; +PU0,1229; +PD603,1007,1229,815,1877,652,2545,516,3230,406,3931,319,4646,253,5372,207,6109,178,6854,165,7604,165,8359,178,9116,200,9874,231,10629,267,11381,308,12128,351,12867,395,13597,437,14316,476,15021,509,15711,536,16384,553,17038,559,18565,557,20093,551,21621,543,23150,531,24679,518,26208,503,27738,488,29268,472,30799,455,32329,440,33859,425,35390,413,36920,402,38450,394,39980,390,41510,389,43039,392,44567,400,46096,414,47623,433,49150,459,50676,491,52202,531,53726,579,53726,555,53726,531,53726,507,53725,483,53725,458,53724,434,53724,410,53723,386,53722,362,53721,338,53721,314,53720,290,53719,265,53718,241,53717,217,53715,193,53714,169,53713,145,53712,121,53711,97,53709,72,53708,48,53707,24,53706,0,51469,0,49233,0,46996,0,44759,0,42522,0,40285,0,38048,0,35811,0,33574,0,31337,0,29100,0,26863,0,24626,0,22389,0,20152,0,17915,0,15678,0,13441,0,11204,0,8967,0,6730,0,4493,0,2257,0,20,0,19,51,18,102,17,153,15,204,14,255,13,306,12,358,11,409,9,460,8,512,7,563,6,615,5,666,5,718,4,769,3,820,2,872,2,923,1,974,1,1025,0,1077,0,1128,0,1178,0,1229,0,1229; diff --git a/algorithm/tests/test_sticker_model.py b/algorithm/tests/test_sticker_model.py index cec79ad..b24103e 100644 --- a/algorithm/tests/test_sticker_model.py +++ b/algorithm/tests/test_sticker_model.py @@ -1,22 +1,51 @@ from shapely.geometry import Polygon from app.models.sticker import Sticker -def test_sticker_stores_geometry_and_dimensions(): - geometry = Polygon([ - (0,0), - (4,0), - (4,2), - (0,2), - ]) - +def test_sticker_stores_catalog_properties(): sticker = Sticker( - sticker_id = "test-rectangle", - geometry=geometry, + id="tetris-i", + name="Tetris I Block", + size=100.0, + description="A straight Tetris-style sticker.", + tags=["tetris","block","geometric"], + category = "Tetris Blocks", + hpgl_path="stickers/tetrispiecesI.hpgl", + svg_path="stickers/tetrispiecesI.svg", + ) + + assert sticker.id == "tetris-i" + assert sticker.name == "Tetris I Block" + assert sticker.size==100.0 + assert sticker.description=="A straight Tetris-style sticker." + assert sticker.tags==["tetris","block","geometric"] + assert sticker.category == "Tetris Blocks" + assert sticker.hpgl_path=="stickers/tetrispiecesI.hpgl" + assert sticker.svg_path=="stickers/tetrispiecesI.svg" + +def test_stickers_can_have_independent_tag_lists(): + first = Sticker( + id="sticker-a", + name="Sticker A", + size = 10.0 + description = "First sticker.", + tags = ["test"], + category = "Tests", + hpgl_path="a.hpgl", + svg_path="a.svg", ) - assert sticker.sticker_id == "test-rectangle" - assert sticker.geometry.equals(geometry) - assert sticker.width == 4 - assert sticker.height == 2 - assert sticker.area == 8 - \ No newline at end of file + second = Sticker( + id="sticker-b", + name="Sticker B", + size = 20.0 + description = "Second sticker.", + tags = ["example"], + category = "Examples", + hpgl_path="b.hpgl", + svg_path="b.svg", + ) + + first.tags.append("extra") + + assert first.tags == ["test","extra"] + assert second.tags == ["example"] \ No newline at end of file From 5cde7d4d9edd67c8d9c652db65f1517ea42127fd Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Tue, 20 Oct 2026 15:10:18 -0400 Subject: [PATCH 10/39] added tests, in red, testing with fixtures --- algorithm/app/models/sticker.py | 14 +++++++++++--- algorithm/tests/test_sticker_model.py | 4 ++-- algorithm/tests/test_svg_input.py | 25 ------------------------- 3 files changed, 13 insertions(+), 30 deletions(-) diff --git a/algorithm/app/models/sticker.py b/algorithm/app/models/sticker.py index 0b778a6..cf8ad70 100644 --- a/algorithm/app/models/sticker.py +++ b/algorithm/app/models/sticker.py @@ -1,6 +1,14 @@ -from dataclasses import dataclass -from shapely.geometry import Polygon +from dataclasses import dataclass,field +# from shapely.geometry import Polygon @dataclass class Sticker: - \ No newline at end of file + """Catalog metadata and file refs for one sticker.""" + id:str + name:str + size:float + description:str + tags:list[str] = field(default_factory=list) + category:str="" + hpgl_path:str="" + svg_path:str="" \ No newline at end of file diff --git a/algorithm/tests/test_sticker_model.py b/algorithm/tests/test_sticker_model.py index b24103e..d6608ae 100644 --- a/algorithm/tests/test_sticker_model.py +++ b/algorithm/tests/test_sticker_model.py @@ -26,7 +26,7 @@ def test_stickers_can_have_independent_tag_lists(): first = Sticker( id="sticker-a", name="Sticker A", - size = 10.0 + size = 10.0, description = "First sticker.", tags = ["test"], category = "Tests", @@ -37,7 +37,7 @@ def test_stickers_can_have_independent_tag_lists(): second = Sticker( id="sticker-b", name="Sticker B", - size = 20.0 + size = 20.0, description = "Second sticker.", tags = ["example"], category = "Examples", diff --git a/algorithm/tests/test_svg_input.py b/algorithm/tests/test_svg_input.py index 099180a..e06148d 100644 --- a/algorithm/tests/test_svg_input.py +++ b/algorithm/tests/test_svg_input.py @@ -1,27 +1,2 @@ -from pathlib import Path -import pytest from app.svg.loader import load_sticker_svg -FIXTURES = Path(__file__).parent / "fixtures" - -def test_svg_loads_as_one_nonempty_sticker(): - sticker = load_sticker_svg( - FIXTURES / "tetrispieceI.svg" - ) - - assert sticker is not None - assert not sticker.geometry.is_empty - assert sticker.geometry.is_valid - assert sticker.geometry.area > 0 - assert sticker.width > 0 - assert sticker.height > 0 - -def test_empty_svg_is_rejected(): - with pytest.raises( - ValueError, - match = "no usable sticker geometry", - ): - load_sticker_svg( - FIXTURES / "tetrispiecesL.svg" - ) - \ No newline at end of file From f975c568fcb5507005ed890c806f5e5fcbaad429 Mon Sep 17 00:00:00 2001 From: Marigold <111760235+LiterallyAFlower@users.noreply.github.com> Date: Thu, 22 Oct 2026 18:03:36 -0400 Subject: [PATCH 11/39] added tests for svg input: test it loads stickers, test nonsense svg is rejected, test missing file is rejected --- algorithm/tests/test_svg_input.py | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/algorithm/tests/test_svg_input.py b/algorithm/tests/test_svg_input.py index e06148d..5d8e78c 100644 --- a/algorithm/tests/test_svg_input.py +++ b/algorithm/tests/test_svg_input.py @@ -1,2 +1,35 @@ from app.svg.loader import load_sticker_svg +from pathlib import Path +import pytest + +FIXTURES = Path(__file__).parent / "fixtures" + +def test_svg_loads_as_one_nonempty_sticker(): + geometry = load_sticker_svg( + FIXTURES / "tetrispiecesI.svg" + ) + + assert not geometry.is_empty + assert geometry.is_valid + assert geometry.area > 0 + + min_x, min_y, max_x, max_y = geometry.bounds + assert max_x > min_x + assert max_y > min_y + +def test_svg_without_usable_geometry_is_rejected(tmp_path): + empty_svg = tmp_path / "empty.svg" + empty_svg.write_text( + '', + encoding="utf-8", + ) + + with pytest.raises(ValueError, match="no usable sticker geometry"): + load_sticker_svg(empty_svg) + +def test_missing_svg_file_is_rejected(tmp_path): + missing_svg = tmp_path / "missing.svg" + + with pytest.raises(FileNotFoundError): + load_sticker_svg(missing_svg) \ No newline at end of file From 0e6f50dbca70de742370c5b21d519f23ef986d7c Mon Sep 17 00:00:00 2001 From: Marigold <111760235+LiterallyAFlower@users.noreply.github.com> Date: Thu, 22 Oct 2026 18:21:00 -0400 Subject: [PATCH 12/39] added path/point logic to loader.py to handle svg format. --- algorithm/app/svg/loader.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/algorithm/app/svg/loader.py b/algorithm/app/svg/loader.py index e69de29..7584583 100644 --- a/algorithm/app/svg/loader.py +++ b/algorithm/app/svg/loader.py @@ -0,0 +1,37 @@ +from pathlib import Path +import xml.etree.ElementTree as ET + +from shapely.geometry import Polygon +from shapely.ops import unary_union +from svgpathtools import parse_path + +def _local_tag(element): + """Return an SVG element's tag without the XML namespace, for testing/parsing.""" + return element.tag.rsplit("}",1)[-1] + +def _simple_path(path_data,samples_per_curve=12): + """Convert an SVG path into sampled coordinate pairs.""" + path = parse_path(path_data) + + if not path.isclosed(): + return None + + points = [] + + for segment in path: + start = segment.start + if not points: + points.append((start.real, start.imag)) + + # sampling handles lines and curves with the same interface + for i in range(1, samples_per_curve+1): + point = segment.point(i/samples_per_curve) + points.append((point.real, point.imag)) + + if len(points) < 4: + return None + + return points + +def _parse_points(points_text): + """""" \ No newline at end of file From 4d06f30b137213bcf220f3b6eba4a33c7ac36530 Mon Sep 17 00:00:00 2001 From: Marigold <111760235+LiterallyAFlower@users.noreply.github.com> Date: Thu, 22 Oct 2026 18:40:29 -0400 Subject: [PATCH 13/39] added load_sticker, so hopefully that works with these tests. tests should pass. --- algorithm/app/svg/loader.py | 78 ++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/algorithm/app/svg/loader.py b/algorithm/app/svg/loader.py index 7584583..2b09e89 100644 --- a/algorithm/app/svg/loader.py +++ b/algorithm/app/svg/loader.py @@ -9,7 +9,7 @@ def _local_tag(element): """Return an SVG element's tag without the XML namespace, for testing/parsing.""" return element.tag.rsplit("}",1)[-1] -def _simple_path(path_data,samples_per_curve=12): +def _sample_path(path_data,samples_per_curve=12): """Convert an SVG path into sampled coordinate pairs.""" path = parse_path(path_data) @@ -34,4 +34,78 @@ def _simple_path(path_data,samples_per_curve=12): return points def _parse_points(points_text): - """""" \ No newline at end of file + """Parse the points attribute of an SVG polygon or line.""" + import re + values = re.findall( + r"[-+]?(?:\d*\.\d+|\d+\.?)(?:[eE][]?\d+)?", + points_text, + ) + + if len(values) < 6 or len(values) % 2: + return [] + + numbers = [float(value) for value in values] + return list(zip(numbers[::2], numbers[1::2])) + +def load_sticker_svg(path:str | Path): + """Load one svg file and return its combined sticker outline. + + Returns: + A Shapely polygon or multipolygon (like a polygon but multi) + + Raises: + FileNotFoundError: If the SVG file doesn't exist. + ValueError: If the SVG contains no usable geometry. + """ + + path = Path(path) + + if not path.is_file(): + raise FileNotFoundError(path) + + try: + root = ET.parse(path).getroot() + except ET.ParseError as exc: + raise ValueError(f"Invalid SVG file: {path}") from exc + + polygons = [] + + for element in root.iter(): + tag = _local_tag(element) + + if tag == "path": + path_data = element.get("d", "").strip() + if not path_data: + continue + + try: + points = _sample_path(path_data) + except (ValueError, TypeError, ZeroDivisionError): + continue + + if points: + polygon = Polygon(points) + if polygon.is_valid and polygon.area > 0: + polygons.append(polygon) + + elif tag in {"polygon", "polyline"}: + points_text = element.get("points","") + points = _parse_points(points_text) + + if tag == "polyline" and points: + if points[0] != points[-1]: + continue + + polygon = Polygon(points) + if polygon.is_valid and polygon.area > 0: + polygons.append(polygon) + + if not polygons: + raise ValueError("No usable sticker geometry") + + geometry = unary_union(polygons) + + if geometry.is_empty or not geometry.is_valid or geometry.area <= 0: + raise ValueError("no usable sticker geometry") + + return geometry From 28187828e256f4862c6f3a9df0931f2f12613c02 Mon Sep 17 00:00:00 2001 From: Marigold <111760235+LiterallyAFlower@users.noreply.github.com> Date: Thu, 22 Oct 2026 18:48:32 -0400 Subject: [PATCH 14/39] tracking whether something is or isnt continuous is messing up. continuing to improve loader.py. --- algorithm/tests/test_svg_input.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/algorithm/tests/test_svg_input.py b/algorithm/tests/test_svg_input.py index 5d8e78c..8c2da92 100644 --- a/algorithm/tests/test_svg_input.py +++ b/algorithm/tests/test_svg_input.py @@ -7,7 +7,7 @@ def test_svg_loads_as_one_nonempty_sticker(): geometry = load_sticker_svg( - FIXTURES / "tetrispiecesI.svg" + FIXTURES / "tetrispieceI.svg" ) assert not geometry.is_empty @@ -25,7 +25,7 @@ def test_svg_without_usable_geometry_is_rejected(tmp_path): encoding="utf-8", ) - with pytest.raises(ValueError, match="no usable sticker geometry"): + with pytest.raises(ValueError, match="No usable sticker geometry"): load_sticker_svg(empty_svg) def test_missing_svg_file_is_rejected(tmp_path): From 115981ea215779189652f60a9af9560827653d07 Mon Sep 17 00:00:00 2001 From: Marigold <111760235+LiterallyAFlower@users.noreply.github.com> Date: Thu, 22 Oct 2026 19:05:44 -0400 Subject: [PATCH 15/39] altered loader.py to check for continous stickers. should work with ajustments on test. --- algorithm/app/svg/loader.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/algorithm/app/svg/loader.py b/algorithm/app/svg/loader.py index 2b09e89..838ee62 100644 --- a/algorithm/app/svg/loader.py +++ b/algorithm/app/svg/loader.py @@ -10,18 +10,34 @@ def _local_tag(element): return element.tag.rsplit("}",1)[-1] def _sample_path(path_data,samples_per_curve=12): - """Convert an SVG path into sampled coordinate pairs.""" + """Convert an SVG path into sampled coordinate pairs. + + Raises: + ValueError: If the path is empty, discontinuous, or open. + """ path = parse_path(path_data) + if len(path) == 0: + raise ValueError("Sticker SVG contains an empty path") + + # Check continuity before isclosed(), which asserts continuity + if not path.iscontinuous(): + raise ValueError( + "Sticker SVG path contains disconnecte segments; " + "each sticker must have one continuous outline" + ) + if not path.isclosed(): - return None + raise ValueError( + "Sticker SVG path is open; each sticker must have " + "one closed outline" + ) points = [] for segment in path: - start = segment.start if not points: - points.append((start.real, start.imag)) + points.append((segment.start.real, segment.start.imag)) # sampling handles lines and curves with the same interface for i in range(1, samples_per_curve+1): @@ -29,7 +45,7 @@ def _sample_path(path_data,samples_per_curve=12): points.append((point.real, point.imag)) if len(points) < 4: - return None + raise ValueError("Sticker SVG path has too few points") return points From 8626bef0fe95a04714d9f89d87ad9818b312dcfd Mon Sep 17 00:00:00 2001 From: Marigold <111760235+LiterallyAFlower@users.noreply.github.com> Date: Thu, 22 Oct 2026 19:33:39 -0400 Subject: [PATCH 16/39] red phase, added lots of tests to check continuity. made a choice to interpret all stickers as continuous, connecting around the outline. --- algorithm/app/svg/loader.py | 36 ++++++++++++++++++---- algorithm/tests/test_svg_input.py | 51 ++++++++++++++++++++++++------- 2 files changed, 70 insertions(+), 17 deletions(-) diff --git a/algorithm/app/svg/loader.py b/algorithm/app/svg/loader.py index 838ee62..e5d30bd 100644 --- a/algorithm/app/svg/loader.py +++ b/algorithm/app/svg/loader.py @@ -16,6 +16,24 @@ def _sample_path(path_data,samples_per_curve=12): ValueError: If the path is empty, discontinuous, or open. """ path = parse_path(path_data) + print(f"Number of segments: {len(path)}") + for i, segment in enumerate(path): + print( + f"Segement {i}: " + f"Start = {segment.start}, " + f"End={segment.end}" + ) + + for i in range(len(path) - 1): + current_end = path[i].end + next_start = path[i+1].start + + if abs(current_end - next_start) > 1e-7: + print( + f"DISCONTINUITY between segments {i} and {i+1}: " + f"{current_end} != {next_start}" + ) + if len(path) == 0: raise ValueError("Sticker SVG contains an empty path") @@ -96,13 +114,19 @@ def load_sticker_svg(path:str | Path): try: points = _sample_path(path_data) - except (ValueError, TypeError, ZeroDivisionError): - continue + except (ValueError, AssertionError) as exc: + raise ValueError( + f"Invalid sticker outline in {path}: {exc}" + ) from exc + + polygon = Polygon(points) + + if not polygon.is_valid or polygon.area <= 0: + raise ValueError( + f"Invalid polygon geometry in SVG: {path}" + ) - if points: - polygon = Polygon(points) - if polygon.is_valid and polygon.area > 0: - polygons.append(polygon) + polygons.append(polygon) elif tag in {"polygon", "polyline"}: points_text = element.get("points","") diff --git a/algorithm/tests/test_svg_input.py b/algorithm/tests/test_svg_input.py index 8c2da92..da8166f 100644 --- a/algorithm/tests/test_svg_input.py +++ b/algorithm/tests/test_svg_input.py @@ -1,5 +1,5 @@ from app.svg.loader import load_sticker_svg - +from shapely.geometry import Polygon from pathlib import Path import pytest @@ -7,26 +7,55 @@ def test_svg_loads_as_one_nonempty_sticker(): geometry = load_sticker_svg( - FIXTURES / "tetrispieceI.svg" + FIXTURES/"tetrispieceI.svg" ) assert not geometry.is_empty assert geometry.is_valid + assert geometry.geom_type == "Polygon" assert geometry.area > 0 + assert len(geometry.interiors) == 0 + +def test_multiple_touching_blocks_form_one_outline(tmp_path): + svg = tmp_path / "two_blocks.svg" + svg.write_text( + """ + + + + """, + encoding="utf-8", + ) + + geometry = load_sticker_svg(svg) - min_x, min_y, max_x, max_y = geometry.bounds - assert max_x > min_x - assert max_y > min_y + assert geometry.geom_type == "Polygon" + assert geometry.area == pytest.approx(200) + assert geometry.bounds == (0,0,20,10) + assert len(geometry.interiors) == 0 -def test_svg_without_usable_geometry_is_rejected(tmp_path): - empty_svg = tmp_path / "empty.svg" - empty_svg.write_text( - '', +def test_disconnected_blocks_can_be_merged_with_tolerance(tmp_path): + svg = tmp_path / "gapped_blocks.svg" + svg.write_text( + """ + + + + """, encoding="utf-8", ) - with pytest.raises(ValueError, match="No usable sticker geometry"): - load_sticker_svg(empty_svg) + geometry = load_sticker_svg(svg, merge_tolerance=1.1) + + assert geometry.geom_type == "Polygon" + assert geometry.bounds == (0, 0, 22, 10) + assert len(geometry.interiors) == 0 def test_missing_svg_file_is_rejected(tmp_path): missing_svg = tmp_path / "missing.svg" From c097c5b30ea6c4571056c6a6e77d7a31eb4c5446 Mon Sep 17 00:00:00 2001 From: Marigold <111760235+LiterallyAFlower@users.noreply.github.com> Date: Thu, 22 Oct 2026 20:25:57 -0400 Subject: [PATCH 17/39] changed load sticker, need to rewrite some tests, stoppign for tonight. --- algorithm/app/svg/loader.py | 198 ++++++++++++++++++++---------------- 1 file changed, 111 insertions(+), 87 deletions(-) diff --git a/algorithm/app/svg/loader.py b/algorithm/app/svg/loader.py index e5d30bd..66ae44d 100644 --- a/algorithm/app/svg/loader.py +++ b/algorithm/app/svg/loader.py @@ -1,4 +1,5 @@ from pathlib import Path +import re import xml.etree.ElementTree as ET from shapely.geometry import Polygon @@ -9,63 +10,60 @@ def _local_tag(element): """Return an SVG element's tag without the XML namespace, for testing/parsing.""" return element.tag.rsplit("}",1)[-1] -def _sample_path(path_data,samples_per_curve=12): - """Convert an SVG path into sampled coordinate pairs. - - Raises: - ValueError: If the path is empty, discontinuous, or open. - """ - path = parse_path(path_data) - print(f"Number of segments: {len(path)}") - for i, segment in enumerate(path): - print( - f"Segement {i}: " - f"Start = {segment.start}, " - f"End={segment.end}" - ) +def _split_subpaths(path_data): + """Split SVG path data at moveto commands into seperate subpaths.""" + # An uppercase or lowercase M starts a subpath + # exponents can't be mistaken as commands + starts = [ + match.start() + for match in re.finditer(r"[Mm]", path_data) + ] - for i in range(len(path) - 1): - current_end = path[i].end - next_start = path[i+1].start + if not starts: + return [] - if abs(current_end - next_start) > 1e-7: - print( - f"DISCONTINUITY between segments {i} and {i+1}: " - f"{current_end} != {next_start}" - ) + starts.append(len(path_data)) + return [ + path_data[starts[i]:starts[i+1]].strip() + for i in range(len(starts) - 1) + if path_data[starts[i]:starts[i+1]].strip() + ] + +def _sample_closed_path(path_data, samples_per_curve=12): + """Convert one closed psubpath into polygon coordinates""" + path = parse_path(path_data) if len(path) == 0: - raise ValueError("Sticker SVG contains an empty path") + return None - # Check continuity before isclosed(), which asserts continuity - if not path.iscontinuous(): - raise ValueError( - "Sticker SVG path contains disconnecte segments; " - "each sticker must have one continuous outline" - ) + #check continuity + if not path.iscontinuous() or not path.isclosed(): + return None - if not path.isclosed(): - raise ValueError( - "Sticker SVG path is open; each sticker must have " - "one closed outline" - ) - points = [] for segment in path: if not points: - points.append((segment.start.real, segment.start.imag)) + points.append( + (segment.start.real, segment.start.imag) + ) - # sampling handles lines and curves with the same interface for i in range(1, samples_per_curve+1): point = segment.point(i/samples_per_curve) points.append((point.real, point.imag)) - if len(points) < 4: - raise ValueError("Sticker SVG path has too few points") + if len(points < 4): + return None + + polygon = Polygon(points) + + if polygon.is_empty or not polygon.is_valid or polygon.area <= 0: + return None + + return polygon + - return points def _parse_points(points_text): """Parse the points attribute of an SVG polygon or line.""" @@ -81,71 +79,97 @@ def _parse_points(points_text): numbers = [float(value) for value in values] return list(zip(numbers[::2], numbers[1::2])) -def load_sticker_svg(path:str | Path): - """Load one svg file and return its combined sticker outline. - +def _read_polygons(root): + """Extract closed polygon geometry from supported SVG elements.""" + polygons = [] + for element in root.iter(): + tag = _local_tag(element) + + if tag == "path": + path_data = element.get("d","").strip() + + for subpath_data in _split_subpaths(path_data): + try: + polygon = _sample_closed_path(subpath_data) + except (ValueError, AssertionError, ZeroDivisionError): + polygon = None + + if polygon is not None: + polygons.append(polygon) + + elif tag in {"polygon", "polyline"}: + points = _parse_points(element.get("points", "")) + + if tag == "polyline" and points and points[0] != points[-1]: + continue + + if len(points) >= 3: + polygon = Polygon(points) + + if polygon.is_valid and polygon.area > 0: + polygons.append(polygon) + + return polygons + +def load_sticker_svg( + path: str | Path, + *, + merge_tolerance: float = 0.0, +): + """Load one sticker SVG and return its exterior silhouette. + + Args: + path: SVG file path. + merge_tolerance: Maximum geometric closing distance, in SVG units. + Zero merges touching shapes but does not bridge gaps. + Returns: - A Shapely polygon or multipolygon (like a polygon but multi) - + A Shapely Polygon representing the sticker's outside outline. + Raises: - FileNotFoundError: If the SVG file doesn't exist. - ValueError: If the SVG contains no usable geometry. + FileNotFoundError: If the file does not exist. + ValueError: If no usable single sticker outline can be extracted. """ - path = Path(path) if not path.is_file(): raise FileNotFoundError(path) + if merge_tolerance < 0: + raise ValueError("merge_tolerance cannot be negative") + try: root = ET.parse(path).getroot() except ET.ParseError as exc: raise ValueError(f"Invalid SVG file: {path}") from exc - polygons = [] - - for element in root.iter(): - tag = _local_tag(element) - - if tag == "path": - path_data = element.get("d", "").strip() - if not path_data: - continue + polygons = _read_polygons(root) - try: - points = _sample_path(path_data) - except (ValueError, AssertionError) as exc: - raise ValueError( - f"Invalid sticker outline in {path}: {exc}" - ) from exc - - polygon = Polygon(points) - - if not polygon.is_valid or polygon.area <= 0: - raise ValueError( - f"Invalid polygon geometry in SVG: {path}" - ) - - polygons.append(polygon) - - elif tag in {"polygon", "polyline"}: - points_text = element.get("points","") - points = _parse_points(points_text) + if not polygons: + raise ValueError("no usable sticker geometry") - if tag == "polyline" and points: - if points[0] != points[-1]: - continue + geometry = unary_union(polygons) - polygon = Polygon(points) - if polygon.is_valid and polygon.area > 0: - polygons.append(polygon) + # Close small gaps, then restore the original overall scale as much + # as possible. This is a geometric closing operation. + if merge_tolerance > 0: + geometry = ( + geometry.buffer(merge_tolerance) + .buffer(-merge_tolerance) + ) - if not polygons: - raise ValueError("No usable sticker geometry") + # The cutter needs one continuous exterior outline. + if geometry.geom_type == "MultiPolygon": + raise ValueError( + "Sticker geometry contains disconnected components. " + "Increase merge_tolerance only if the gaps are unintended." + ) - geometry = unary_union(polygons) + if geometry.geom_type != "Polygon" or geometry.is_empty: + raise ValueError("no usable sticker geometry") - if geometry.is_empty or not geometry.is_valid or geometry.area <= 0: + if not geometry.is_valid or geometry.area <= 0: raise ValueError("no usable sticker geometry") - return geometry + # Return only the exterior silhouette, discarding interior rings. + return Polygon(geometry.exterior) From 66b9456db8bf71dda7f92cec4ad51da2f1191298 Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Mon, 26 Oct 2026 09:21:33 -0400 Subject: [PATCH 18/39] red stage to test overlap and incorperate geometry, trying to get a graphical or easy representation for him tues. --- algorithm/app/svg/loader.py | 2 +- algorithm/tests/test_placement.py | 39 +++++++++++++++++++++++++++++++ algorithm/tests/test_svg_input.py | 16 ++++++++++++- 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 algorithm/tests/test_placement.py diff --git a/algorithm/app/svg/loader.py b/algorithm/app/svg/loader.py index 66ae44d..8c99ce7 100644 --- a/algorithm/app/svg/loader.py +++ b/algorithm/app/svg/loader.py @@ -53,7 +53,7 @@ def _sample_closed_path(path_data, samples_per_curve=12): point = segment.point(i/samples_per_curve) points.append((point.real, point.imag)) - if len(points < 4): + if len(points) < 4: return None polygon = Polygon(points) diff --git a/algorithm/tests/test_placement.py b/algorithm/tests/test_placement.py new file mode 100644 index 0000000..dc0c368 --- /dev/null +++ b/algorithm/tests/test_placement.py @@ -0,0 +1,39 @@ +import pytest +from shapely.geometry import box +from app.geometry.placement import place_sticker, stickers_overlap + +def test_placement_moves_sticker_to_requested_position(): + sticker = box(0,0,10,5) + placed = place_sticker(sticker, x=20, y=30) + assert placed.bounds == pytest.approx((20,30,30,35)) + assert placed.area == pytest.approx(sticker.area) + +def test_rotation_preserves_area_and_swaps_bounds_at_90_degrees(): + sticker = box(0,0,10,5) + placed = place_sticker( + sticker, + x=0, + y=0, + rotation_degreese=90, + ) + + min_x,min_y,max_x,max_y = placed.bounds + + assert max_x - min_x == pytest.approx(5) + assert max_y - min_y == pytest.approx(10) + assert placed.area == pytest.approx(sticker.area) + +def test_overlapping_stickers_are_detected(): + first = box(0,0,10,10) + second = box(5,5,15,15) + assert stickers_overlap(first, second) + +def test_stickers_that_only_touch_edges_do_not_overlap(): + first = box(0,0,10,10) + second = box(10,0,20,10) + assert not stickers_overlap(first, second) + +def test_seperate_stickers_do_not_overlap(): + first = box(0,0,10,10) + second = box(20,20,30,30) + assert not stickers_overlap(first,second) \ No newline at end of file diff --git a/algorithm/tests/test_svg_input.py b/algorithm/tests/test_svg_input.py index da8166f..b5f0196 100644 --- a/algorithm/tests/test_svg_input.py +++ b/algorithm/tests/test_svg_input.py @@ -61,4 +61,18 @@ def test_missing_svg_file_is_rejected(tmp_path): missing_svg = tmp_path / "missing.svg" with pytest.raises(FileNotFoundError): - load_sticker_svg(missing_svg) \ No newline at end of file + load_sticker_svg(missing_svg) + + +def test_svg_loads_as_one_nonempty_sticker(): + geometry = load_sticker_svg(FIXTURES / "tetrispieceI.svg") + + assert not geometry.is_empty + assert geometry.is_valid + assert geometry.geom_type == "Polygon" + assert geometry.area > 0 + + # sticker should just have exterior + assert len(geometry.interiors) == 0 + assert geometry.exterior.is_ring + assert geometry.is_simple \ No newline at end of file From bff57ee47da0bd790fac191cb4fb9615385d1ed8 Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Mon, 26 Oct 2026 09:35:48 -0400 Subject: [PATCH 19/39] red flag, coding page logic. this should be transmissable to whatever size we need --- algorithm/app/geometry/__init__.py | 0 algorithm/app/geometry/placement.py | 21 +++++++++++++++++++++ algorithm/tests/test_placement.py | 23 +++++++++++++++++++++-- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 algorithm/app/geometry/__init__.py create mode 100644 algorithm/app/geometry/placement.py diff --git a/algorithm/app/geometry/__init__.py b/algorithm/app/geometry/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/algorithm/app/geometry/placement.py b/algorithm/app/geometry/placement.py new file mode 100644 index 0000000..7080144 --- /dev/null +++ b/algorithm/app/geometry/placement.py @@ -0,0 +1,21 @@ +from shapely.affinity import rotate, translate + +def place_sticker(geometry, x, y, rotation_degrees=0): + """Rotate a sticker and place its bounding box at (x,y)""" + placed = rotate( + geometry, + rotation_degrees, + origin="centroid", + ) + + min_x, min_y, _, _ = placed.bounds + + return translate( + placed, + xoff=x - min_x, + yoff = y- min_y, + ) + +def stickers_overlap(first,second): + """Return whether two stickers overlap in their interiors""" + return first.intersection(second).area >0 \ No newline at end of file diff --git a/algorithm/tests/test_placement.py b/algorithm/tests/test_placement.py index dc0c368..331e14f 100644 --- a/algorithm/tests/test_placement.py +++ b/algorithm/tests/test_placement.py @@ -1,6 +1,7 @@ import pytest from shapely.geometry import box from app.geometry.placement import place_sticker, stickers_overlap +from app.geometry.placement import fits_on_page def test_placement_moves_sticker_to_requested_position(): sticker = box(0,0,10,5) @@ -14,7 +15,7 @@ def test_rotation_preserves_area_and_swaps_bounds_at_90_degrees(): sticker, x=0, y=0, - rotation_degreese=90, + rotation_degrees=90, ) min_x,min_y,max_x,max_y = placed.bounds @@ -36,4 +37,22 @@ def test_stickers_that_only_touch_edges_do_not_overlap(): def test_seperate_stickers_do_not_overlap(): first = box(0,0,10,10) second = box(20,20,30,30) - assert not stickers_overlap(first,second) \ No newline at end of file + assert not stickers_overlap(first,second) + + +# extra geometry: page boundaries +def test_sticker_inside_page_fits(): + sticker = box(10,10,30,20) + page = box(0,0,100,100) + + assert fits_on_page(sticker, page) + +def test_sticker_outside_page_does_not_fit(): + sticker = box(90,90,110,110) + page = box(0,0,100,100) + assert not fits_on_page(sticker,page) + +def test_sticker_touching_page_edge_fits(): + sticker = box(0,0,100,20) + page = box(0,0,100,100) + assert fits_on_page(sticker, page) \ No newline at end of file From a8ddba1f6cd2eae899e3dedddd808c26f16ec619 Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Mon, 26 Oct 2026 09:42:15 -0400 Subject: [PATCH 20/39] page logic works???? first try??????? somethings wrong ill look at it but that would be cool. moving to proper algo rewrite. --- algorithm/app/geometry/placement.py | 8 +++++++- algorithm/app/packing/__init__.py | 0 algorithm/tests/test_packing.py | 0 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 algorithm/app/packing/__init__.py create mode 100644 algorithm/tests/test_packing.py diff --git a/algorithm/app/geometry/placement.py b/algorithm/app/geometry/placement.py index 7080144..70bdf5f 100644 --- a/algorithm/app/geometry/placement.py +++ b/algorithm/app/geometry/placement.py @@ -18,4 +18,10 @@ def place_sticker(geometry, x, y, rotation_degrees=0): def stickers_overlap(first,second): """Return whether two stickers overlap in their interiors""" - return first.intersection(second).area >0 \ No newline at end of file + return first.intersection(second).area >0 + +# page logic +def fits_on_page(sticker,page): + """Return whether the entire sticker lies inside the page""" + return page.covers(sticker) + diff --git a/algorithm/app/packing/__init__.py b/algorithm/app/packing/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/algorithm/tests/test_packing.py b/algorithm/tests/test_packing.py new file mode 100644 index 0000000..e69de29 From 32c63de150d1ae1cb23427138aaa9c3fbdfc61f6 Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Mon, 26 Oct 2026 09:49:44 -0400 Subject: [PATCH 21/39] added tests for algorithm handling --- algorithm/tests/test_packing.py | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/algorithm/tests/test_packing.py b/algorithm/tests/test_packing.py index e69de29..ded4b31 100644 --- a/algorithm/tests/test_packing.py +++ b/algorithm/tests/test_packing.py @@ -0,0 +1,46 @@ +import pytest +from shapely.geometry import box +from app.packing.greedy import pack_stickers + +def test_packs_two_stickers_without_overlap(): + stickers = [ + box(0,0,10,10), + box(0,0,10,10), + ] + page = box(0,0,20,10) + + placements = pack_stickers(stickers,page) + + assert len(placements) == 2 + + first = placements[0][1] + second = placements[1][1] + + assert page.covers(first) + assert page.covers(second) + assert first.intersection(second).area == pytest.approx(0) + +def test_skips_sticker_that_cannot_fit(): + stickers = [ + box(0,0,20,20), + ] + page = box(0,0,10,10) + placements = pack_stickers(stickers,page) + assert placements == [] + +def test_returns_original_and_placed_geometry(): + sticker = box(0,0,5,5) + page = box(0,0,10,10) + placements = pack_stickers([sticker],page) + + assert len(placements) == 1 + + original, placed = placements[0] + + assert original is sticker + assert placed.area == pytest.approx(sticker.area) + assert page.covers(placed) + +def test_empty_input_returns_no_placements(): + page = box(0,0,100,100) + assert pack_stickers([],page) == [] \ No newline at end of file From 84ad607ca0e9c6115b5159abd47d1414c8bbfca8 Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Mon, 26 Oct 2026 10:03:59 -0400 Subject: [PATCH 22/39] greedy algorithm works!!!!! done for now, going to add more tests later!!!!!!! --- algorithm/app/packing/greedy.py | 50 +++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 algorithm/app/packing/greedy.py diff --git a/algorithm/app/packing/greedy.py b/algorithm/app/packing/greedy.py new file mode 100644 index 0000000..0b1ccd6 --- /dev/null +++ b/algorithm/app/packing/greedy.py @@ -0,0 +1,50 @@ +from app.geometry.placement import ( + fits_on_page, + place_sticker, + stickers_overlap, +) + +def pack_stickers(stickers,page): + """Place stickers at the first valid position (super greedy)""" + placements = [] + min_x,min_y,max_x,max_y = page.bounds + + # use smallest sticker dimension + dimensions = [ + min(sticker.bounds[2] - sticker.bounds[0], + sticker.bounds[3] - sticker.bounds[1]) + for sticker in stickers + if not sticker.is_empty + ] + + if not dimensions: + return placements + + step = min(dimensions) + if step <= 0: + return placements + + y = min_y + while y < max_y: + x = min_x + while x < max_x: + for sticker in stickers: + if any(original is sticker for original, _ in placements): + continue + + placed = place_sticker(sticker, x, y) + + if not fits_on_page(placed,page): + continue + + if any( + stickers_overlap(placed,existing) + for _, existing in placements + ): + continue + + placements.append((sticker, placed)) + + x+= step + y += step + return placements \ No newline at end of file From 5c848fa8013f336aa791cd32776610b389c3bda6 Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Tue, 27 Oct 2026 14:13:51 -0400 Subject: [PATCH 23/39] developing less greedy algorithm to show off today with a visual element --- algorithm/tests/test_packing.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/algorithm/tests/test_packing.py b/algorithm/tests/test_packing.py index ded4b31..7737fd9 100644 --- a/algorithm/tests/test_packing.py +++ b/algorithm/tests/test_packing.py @@ -43,4 +43,26 @@ def test_returns_original_and_placed_geometry(): def test_empty_input_returns_no_placements(): page = box(0,0,100,100) - assert pack_stickers([],page) == [] \ No newline at end of file + assert pack_stickers([],page) == [] + + +#post-greedy development tests +def test_rotates_sticker_when_needed(): + sticker = box(0,0,10,5) + page = box(0,0,5,10) + + placements = pack_stickers([sticker],page) + + assert len(placements)==1 + assert page.covers(placements[0][1]) + +def test_places_large_sticker_before_small_sticker(): + small = box(0,0,2,2) + large = box(0,0,8,8) + page = box(0,0,10,10) + + placements = pack_stickers([small,large], page) + + #both should fit, but the large sticker should be placed first at [0][0] + assert len(placements) == 2 + assert placements[0][0] is large From b09a44d76946940497ae963aab8ee632acf55776 Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Tue, 27 Oct 2026 14:21:46 -0400 Subject: [PATCH 24/39] created semi-greedy algo, currently just uses greedy to pass tests, will do more in a moment --- algorithm/app/packing/semi_greedy.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 algorithm/app/packing/semi_greedy.py diff --git a/algorithm/app/packing/semi_greedy.py b/algorithm/app/packing/semi_greedy.py new file mode 100644 index 0000000..316c172 --- /dev/null +++ b/algorithm/app/packing/semi_greedy.py @@ -0,0 +1,7 @@ +from app.packing.greedy import pack_stickers + +def pack_stickers_semi_greedy(stickers, page): + """Semi-greedy packing entry + + currently uses greedy baseline.""" + return pack_stickers(stickers, page) \ No newline at end of file From d7bf96c3b5a006ff5035af3e7da1c88eef13758f Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Tue, 27 Oct 2026 14:48:27 -0400 Subject: [PATCH 25/39] two reds in a row, ik, but im on a streak. fixing tests for semi-greedy now. --- algorithm/app/packing/semi_greedy.py | 67 +++++++++++++++++++++++++--- algorithm/tests/test_packing.py | 16 +++++++ 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/algorithm/app/packing/semi_greedy.py b/algorithm/app/packing/semi_greedy.py index 316c172..da4fa78 100644 --- a/algorithm/app/packing/semi_greedy.py +++ b/algorithm/app/packing/semi_greedy.py @@ -1,7 +1,64 @@ from app.packing.greedy import pack_stickers +import random -def pack_stickers_semi_greedy(stickers, page): - """Semi-greedy packing entry - - currently uses greedy baseline.""" - return pack_stickers(stickers, page) \ No newline at end of file +from app.geometry.placement import ( + fits_on_page, + place_sticker, + stickers_overlap, +) + + +def pack_stickers_semi_greedy(stickers, page, seed=None): + """Pack stickers by randomly choosing among the best candidates, including rotation""" + rng = random.Random(seed) + + remaining = sorted( + stickers, + key=lambda s: ( + (s.bounds[2] - s.bounds[0]) + * (s.bounds[3] - s.bounds[1]) + ), + reverse=True, + ) + + min_x, min_y, _,_ = page.bounds + placements = [] + + for sticker in remaining: + positions = {(min_x,min_y)} + + for _, placed in placements: + left, bottom, right, top = placed.bounds + positions.add((right, bottom)) + positions.add((left,top)) + + candidates = [] + + for x, y in positions: + for angle in (0,90,180,270): + candidate = place_sticker( + sticker, + x, + y, + rotation_degrees=angle, + ) + + if not fits_on_page(candidate,page): + continue + if any( + stickers_overlap(candidate, existing) + for _, existing in placements + ): + continue + + _, bottom, right, top = candidate.bounds + score = (top,right,bottom, x,y, angle) + candidates.append((score, candidate)) + if candidates: + candidates.sort(key=lambda item: item[0]) + + #choose randomly from 3 (variable) best valid candidates + shortlist = candidates[:3] + _, chosen = rng.choice(shortlist) + placements.append((sticker,chosen)) + return placements \ No newline at end of file diff --git a/algorithm/tests/test_packing.py b/algorithm/tests/test_packing.py index 7737fd9..00bbee4 100644 --- a/algorithm/tests/test_packing.py +++ b/algorithm/tests/test_packing.py @@ -2,6 +2,8 @@ from shapely.geometry import box from app.packing.greedy import pack_stickers +from app.packing.semi_greedy import pack_stickers_semi_greedy + def test_packs_two_stickers_without_overlap(): stickers = [ box(0,0,10,10), @@ -66,3 +68,17 @@ def test_places_large_sticker_before_small_sticker(): #both should fit, but the large sticker should be placed first at [0][0] assert len(placements) == 2 assert placements[0][0] is large + +#purely semi greedy tests + +def test_semi_greedy_packs_a_sticker(): + sticker = box(0,0,5,5) + page = box(0,0,10,10) + + placements = pack_stickers_semi_greedy( + [sticker], page, seed=42 + ) + + assert len(placements) == 1 + assert page.covers(placements[0][1]) + From deaf34c0478845ae789b28c3b9063dc54836b557 Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Tue, 27 Oct 2026 15:04:53 -0400 Subject: [PATCH 26/39] main tests now use semi-greedy, which factors in rotation and ordering. --- algorithm/app/packing/greedy.py | 9 +++++++++ algorithm/tests/test_packing.py | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/algorithm/app/packing/greedy.py b/algorithm/app/packing/greedy.py index 0b1ccd6..561730c 100644 --- a/algorithm/app/packing/greedy.py +++ b/algorithm/app/packing/greedy.py @@ -24,6 +24,15 @@ def pack_stickers(stickers,page): if step <= 0: return placements + remaining = sorted( + stickers, + key=lambda s: ( + (s.bounds[2]-s.bounds[0]) + * (s.bounds[3] - s.bounds[1]) + ), + reverse= True + ) + y = min_y while y < max_y: x = min_x diff --git a/algorithm/tests/test_packing.py b/algorithm/tests/test_packing.py index 00bbee4..913e1af 100644 --- a/algorithm/tests/test_packing.py +++ b/algorithm/tests/test_packing.py @@ -53,7 +53,7 @@ def test_rotates_sticker_when_needed(): sticker = box(0,0,10,5) page = box(0,0,5,10) - placements = pack_stickers([sticker],page) + placements = pack_stickers_semi_greedy([sticker],page) assert len(placements)==1 assert page.covers(placements[0][1]) @@ -63,7 +63,7 @@ def test_places_large_sticker_before_small_sticker(): large = box(0,0,8,8) page = box(0,0,10,10) - placements = pack_stickers([small,large], page) + placements = pack_stickers_semi_greedy([small,large], page) #both should fit, but the large sticker should be placed first at [0][0] assert len(placements) == 2 From bafeab81ccc0369161f01deedc93c3691645d747 Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Tue, 27 Oct 2026 15:09:31 -0400 Subject: [PATCH 27/39] added visualization tests and directory. implementing visualization now. --- algorithm/app/visualization/__init__.py | 0 algorithm/tests/test_preview.py | 30 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 algorithm/app/visualization/__init__.py create mode 100644 algorithm/tests/test_preview.py diff --git a/algorithm/app/visualization/__init__.py b/algorithm/app/visualization/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/algorithm/tests/test_preview.py b/algorithm/tests/test_preview.py new file mode 100644 index 0000000..9967186 --- /dev/null +++ b/algorithm/tests/test_preview.py @@ -0,0 +1,30 @@ +from shapely.geometry import box + +from app.visualization.preview import save_packing_preview + +def test_preview_creates_svg_file(tmp_path): + page = box(0,0,100,100) + sticker = box(10,20,30,40) + output = tmp_path/"preview.svg" + + save_packing_preview( + page, + [(sticker,sticker)], + output, + ) + + assert output.exists() + + svg = output.read_text() + assert " Date: Tue, 27 Oct 2026 15:57:00 -0400 Subject: [PATCH 28/39] semi-greedy both works and exports everything correctly, outtput needs refined through me getting better svg models honestly --- algorithm/app/visualization/preview.py | 87 ++++++++++++++++++++++ algorithm/testrun.py | 19 +++++ algorithm/tests/fixtures/tetrispiecesZ.svg | 6 -- algorithm/teststuff.py | 48 ++++++++++++ 4 files changed, 154 insertions(+), 6 deletions(-) create mode 100644 algorithm/app/visualization/preview.py create mode 100644 algorithm/testrun.py delete mode 100644 algorithm/tests/fixtures/tetrispiecesZ.svg create mode 100644 algorithm/teststuff.py diff --git a/algorithm/app/visualization/preview.py b/algorithm/app/visualization/preview.py new file mode 100644 index 0000000..a873dad --- /dev/null +++ b/algorithm/app/visualization/preview.py @@ -0,0 +1,87 @@ +from pathlib import Path +from shapely.geometry import Polygon, MultiPolygon, GeometryCollection + +def _polygons(geometry): + """polygon componenets from shapely geometry""" + + if isinstance(geometry,Polygon): + yield geometry + elif isinstance(geometry, (MultiPolygon,GeometryCollection)): + for part in geometry.geoms: + yield from _polygons(part) + + +def save_packing_preview(page,placements,output): + """Save an svg preview of a page and its placed sticker outlines.""" + + output = Path(output) + + min_x, min_y, max_x, max_y = page.bounds + page_width = max_x - min_x + page_height = max_y - min_y + + if page_width <= 0 or page_height <= 0: + raise ValueError("Page must have positive width and height") + + padding= 20 + max_size = 800 + scale = min( + max_size / page_width, + max_size/page_height, + ) + + width = page_width * scale + 2* padding + height = page_height * scale + 2 * padding + + def point(x,y): + screen_x = padding + (x- min_x) * scale + screen_y = padding + (max_y -y) * scale + return screen_x, screen_y + + def ring_path(ring): + coords = list(ring.coords) + if not coords: + return "" + + x,y = point(*coords[0]) + commands = [f"M {x:.3f} {y:.3f}"] + + for coord in coords[1:]: + x, y = point(*coord) + commands.append(f"L {x:.3f} {y:.3f}") + + commands.append("Z") + return " ".join(commands) + + output_lines = [ + f'', + '', + ( + f'' + ), + ] + + for _, geometry in placements: + for polygon in _polygons(geometry): + paths = [ring_path(polygon.exterior)] + paths.extend(ring_path(ring) for ring in polygon.interiors) + path_data = " ".join(paths) + + output_lines.append( + f'' + ) + + output_lines.append("") + + output.parent.mkdir(parents=True, exist_ok=True) + output.write_text( + "\n".join(output_lines) + ) \ No newline at end of file diff --git a/algorithm/testrun.py b/algorithm/testrun.py new file mode 100644 index 0000000..06c559a --- /dev/null +++ b/algorithm/testrun.py @@ -0,0 +1,19 @@ +from shapely.geometry import box +from app.packing.semi_greedy import pack_stickers_semi_greedy +from app.visualization.preview import save_packing_preview + +page = box(0, 0, 100, 100) + +stickers = [ + box(0, 0, 20, 10), + box(0, 0, 15, 15), + box(0, 0, 10, 25), +] + +placements = pack_stickers_semi_greedy(stickers, page, seed=42) + +save_packing_preview( + page, + placements, + "output/packing_preview.svg", +) \ No newline at end of file diff --git a/algorithm/tests/fixtures/tetrispiecesZ.svg b/algorithm/tests/fixtures/tetrispiecesZ.svg deleted file mode 100644 index c7d71d8..0000000 --- a/algorithm/tests/fixtures/tetrispiecesZ.svg +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/algorithm/teststuff.py b/algorithm/teststuff.py new file mode 100644 index 0000000..07e2335 --- /dev/null +++ b/algorithm/teststuff.py @@ -0,0 +1,48 @@ +from pathlib import Path +from shapely.geometry import box + +from app.svg.loader import load_sticker_svg +from app.packing.semi_greedy import pack_stickers_semi_greedy +from app.visualization.preview import save_packing_preview + +fixtures = Path("tests/fixtures") +page = box(0, 0, 250, 180) + + +stickers = [] +# stickers = [ +# load_sticker_svg(path) +# for path in sorted(fixtures.glob("*.svg")) +# ] + +for path in sorted(fixtures.glob("*.svg")): + print(f"Loading {path.name}...") + + #each svg is ONE sticker, try to merge paths to make one silhouette + geometry = load_sticker_svg(path,merge_tolerance=8) + + if geometry.geom_type != "Polygon": + raise ValueError( + f"{path.name} did not produce one polygon:" + f"{geometry.geom_type}" + ) + + if len(geometry.interiors) > 0: + raise ValueError( + f"{path.name} contains interior outlines." + ) + + stickers.append(geometry) + +placements = pack_stickers_semi_greedy(stickers, page, seed=42) + +save_packing_preview( + page, + placements, + "output/packing_preview.svg", +) + +print(f"\nLoaded {len(stickers)} stickers.") +print(f"Placed {len(placements)} stickers.") +print(f"Skipped {len(stickers) - len(placements)} stickers.") +print(f"Preview saved to output/packing_preview.svg") \ No newline at end of file From 95c54d8f9a63cd332be25327a97d7c4df5414ccb Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Mon, 2 Nov 2026 11:06:45 -0500 Subject: [PATCH 29/39] adding hpgl creation! fingers crossed, red stage. --- algorithm/testrun.py | 13 +++++- algorithm/tests/test_hpgl_output.py | 69 +++++++++++++++++++++++++++++ algorithm/teststuff.py | 2 +- 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 algorithm/tests/test_hpgl_output.py diff --git a/algorithm/testrun.py b/algorithm/testrun.py index 06c559a..7eecb3c 100644 --- a/algorithm/testrun.py +++ b/algorithm/testrun.py @@ -1,8 +1,9 @@ from shapely.geometry import box +from app.packing.greedy import pack_stickers from app.packing.semi_greedy import pack_stickers_semi_greedy from app.visualization.preview import save_packing_preview -page = box(0, 0, 100, 100) +page = box(0, 0, 300, 300) stickers = [ box(0, 0, 20, 10), @@ -15,5 +16,13 @@ save_packing_preview( page, placements, - "output/packing_preview.svg", + "output/packing_preview_semi_greedy.svg", +) + +placements = pack_stickers(stickers, page) + +save_packing_preview( + page, + placements, + "output/packing_preview_greedy.svg", ) \ No newline at end of file diff --git a/algorithm/tests/test_hpgl_output.py b/algorithm/tests/test_hpgl_output.py new file mode 100644 index 0000000..18188a7 --- /dev/null +++ b/algorithm/tests/test_hpgl_output.py @@ -0,0 +1,69 @@ +from shapely.geometry import box +from app.hptgl.output import save_hpgl + +def test_writes_a_closed_sticker_outline(tmp_path): + sticker = box(0,0,10,5) + output = tmp_path / "test.plt" + + save_hpgl( + [(sticker,sticker)], + output, + units_per_unit=1, + ) + + content = output.read_text() + + assert "IN;" in content + assert "PA;" in content + assert "SP1;" in content + assert "PU10,0;" in content + assert "PD10,5,0,5,0,0;" in content + assert "PU;" in content + assert "SP0;" in content + +def test_scales_coordinates(tmp_path): + sticker = box(0,0,10,5) + output = tmp_path / "scaled.plt" + + save_hpgl( + [(sticker,sticker)], + output, + unites_per_unit = 10, + ) + + content = output.read_text() + + assert "PU100,0;" in content + assert "PD100,50,0,50,0,0;" in content + +def test_applies_coordinate_offset(tmp_path): + sticker = box(0,0,10,5) + output = tmp_path / "offset.plt" + + save_hpgl( + [(sticker,sticker)], + output, + units_per_unit = 1 + offset = (20,30), + ) + + content = output.read_text() + + assert "PU30.30;" in content + assert "PD30,35,20,35,20,30;" in content + +def test_lifts_pen_between_stickers(tmp_path): + first = box(0, 0, 10, 10) + second = box(20, 0, 30, 10) + output = tmp_path / "multiple.plt" + + save_hpgl( + [(first, first), (second, second)], + output, + units_per_unit=1, + ) + + content = output.read_text() + + assert content.count("PU;") == 2 + assert content.count("PD") == 2 \ No newline at end of file diff --git a/algorithm/teststuff.py b/algorithm/teststuff.py index 07e2335..72b3102 100644 --- a/algorithm/teststuff.py +++ b/algorithm/teststuff.py @@ -6,7 +6,7 @@ from app.visualization.preview import save_packing_preview fixtures = Path("tests/fixtures") -page = box(0, 0, 250, 180) +page = box(0, 0, 500, 500) stickers = [] From 5484b6855c60e90771505e7c40fb32aa9cc3ed8c Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Mon, 2 Nov 2026 11:37:14 -0500 Subject: [PATCH 30/39] file properly generates svg ouput! --- algorithm/app/hpgl/__init__.py | 0 algorithm/app/hpgl/output.py | 54 +++++++++++++++++++++++++++++ algorithm/tests/test_hpgl_output.py | 8 ++--- 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 algorithm/app/hpgl/__init__.py create mode 100644 algorithm/app/hpgl/output.py diff --git a/algorithm/app/hpgl/__init__.py b/algorithm/app/hpgl/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/algorithm/app/hpgl/output.py b/algorithm/app/hpgl/output.py new file mode 100644 index 0000000..aaf040a --- /dev/null +++ b/algorithm/app/hpgl/output.py @@ -0,0 +1,54 @@ +from pathlib import Path + +def save_hpgl( + placements, + output, + units_per_unit=1, + offset=(0,0), +): + """Export sticker polygons as HPGL paths""" + output = Path(output) + + offset_x, offset_y = offset + commands = [ + "IN;", + "PA;", + "SP1;", + ] + + def plot_point(x,y): + plot_x = round((x + offset_x) * units_per_unit) + plot_y = round((y+offset_y) * units_per_unit) + return plot_x, plot_y + + for _, geometry in placements: + if geometry.geom_type != "Polygon": + raise ValueError( + "Each sticker must be a single Polygon" + ) + + coordinates = list(geometry.exterior.coords) + if len(coordinates) < 4: + raise ValueError( + "Sticker outline must contain at least three points" + ) + + points = [plot_point(x,y) for x,y in coordinates] + + # go to starting point with pen raised + start_x, start_y = points[0] + commands.append(f"PU{start_x},{start_y};") + + #lower pen + remaining_points = points[1:-1] + coordinate_data = ",".join( + f"{x},{y}" for x,y in remaining_points + ) + + commands.append(f"PD{coordinate_data};") + commands.append("PU;") + + commands.append("SP0;") + + output.parent.mkdir(parents=True,exist_ok=True) + output.write_text("\n".join(commands), encoding="ascii") diff --git a/algorithm/tests/test_hpgl_output.py b/algorithm/tests/test_hpgl_output.py index 18188a7..8a4b335 100644 --- a/algorithm/tests/test_hpgl_output.py +++ b/algorithm/tests/test_hpgl_output.py @@ -1,5 +1,5 @@ from shapely.geometry import box -from app.hptgl.output import save_hpgl +from app.hpgl.output import save_hpgl def test_writes_a_closed_sticker_outline(tmp_path): sticker = box(0,0,10,5) @@ -28,7 +28,7 @@ def test_scales_coordinates(tmp_path): save_hpgl( [(sticker,sticker)], output, - unites_per_unit = 10, + units_per_unit = 10, ) content = output.read_text() @@ -43,13 +43,13 @@ def test_applies_coordinate_offset(tmp_path): save_hpgl( [(sticker,sticker)], output, - units_per_unit = 1 + units_per_unit = 1, offset = (20,30), ) content = output.read_text() - assert "PU30.30;" in content + assert "PU30,30;" in content assert "PD30,35,20,35,20,30;" in content def test_lifts_pen_between_stickers(tmp_path): From 8717dcfa9d4c0260aa5980eccbdac182b5f985fc Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Thu, 5 Nov 2026 11:58:18 -0500 Subject: [PATCH 31/39] merging into github --- algorithm/tests/test_hpgl_output.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/algorithm/tests/test_hpgl_output.py b/algorithm/tests/test_hpgl_output.py index 8a4b335..8f37b26 100644 --- a/algorithm/tests/test_hpgl_output.py +++ b/algorithm/tests/test_hpgl_output.py @@ -3,7 +3,7 @@ def test_writes_a_closed_sticker_outline(tmp_path): sticker = box(0,0,10,5) - output = tmp_path / "test.plt" + output = "../output/test.plt" save_hpgl( [(sticker,sticker)], From e9318fe304d5bd923b9b6c2d6b20110d17ed76b1 Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Thu, 5 Nov 2026 12:55:43 -0500 Subject: [PATCH 32/39] created pdf export files and test files. --- sticker-business/packing-algorithm/.gitignore | 31 ++++ .../packing-algorithm/algo_doc.md | 110 +++++++++++ .../app/geometry/__init__.py | 0 .../app/geometry/placement.py | 27 +++ .../packing-algorithm/app/hpgl/__init__.py | 0 .../packing-algorithm/app/hpgl/output.py | 54 ++++++ .../packing-algorithm/app/models/__init__.py | 0 .../packing-algorithm/app/models/sticker.py | 14 ++ .../packing-algorithm/app/packing/__init__.py | 0 .../packing-algorithm/app/packing/greedy.py | 59 ++++++ .../app/packing/semi_greedy.py | 64 +++++++ .../packing-algorithm/app/pdf/__init__.py | 0 .../packing-algorithm/app/pdf/output.py | 0 .../packing-algorithm/app/svg/__init__.py | 0 .../packing-algorithm/app/svg/loader.py | 175 ++++++++++++++++++ .../app/visualization/__init__.py | 0 .../app/visualization/preview.py | 87 +++++++++ .../packing-algorithm/pyproject.toml | 26 +++ sticker-business/packing-algorithm/testrun.py | 28 +++ .../tests/fixtures/tetrispieceI.hpgl | 78 ++++++++ .../tests/fixtures/tetrispieceI.svg | 7 + .../tests/fixtures/tetrispiecesL.svg | 6 + .../tests/fixtures/tetrispiecesS.svg | 6 + .../tests/fixtures/tetrispiecesT.svg | 6 + .../tests/fixtures/tetrispiecesquare.svg | 6 + .../tests/fixtures/tetrispiecesrevL.svg | 6 + .../tests/test_hpgl_output.py | 69 +++++++ .../packing-algorithm/tests/test_packing.py | 84 +++++++++ .../tests/test_pdf_output.py | 0 .../packing-algorithm/tests/test_placement.py | 58 ++++++ .../packing-algorithm/tests/test_preview.py | 30 +++ .../tests/test_sticker_model.py | 51 +++++ .../packing-algorithm/tests/test_svg_input.py | 78 ++++++++ .../packing-algorithm/teststuff.py | 48 +++++ 34 files changed, 1208 insertions(+) create mode 100644 sticker-business/packing-algorithm/.gitignore create mode 100644 sticker-business/packing-algorithm/algo_doc.md create mode 100644 sticker-business/packing-algorithm/app/geometry/__init__.py create mode 100644 sticker-business/packing-algorithm/app/geometry/placement.py create mode 100644 sticker-business/packing-algorithm/app/hpgl/__init__.py create mode 100644 sticker-business/packing-algorithm/app/hpgl/output.py create mode 100644 sticker-business/packing-algorithm/app/models/__init__.py create mode 100644 sticker-business/packing-algorithm/app/models/sticker.py create mode 100644 sticker-business/packing-algorithm/app/packing/__init__.py create mode 100644 sticker-business/packing-algorithm/app/packing/greedy.py create mode 100644 sticker-business/packing-algorithm/app/packing/semi_greedy.py create mode 100644 sticker-business/packing-algorithm/app/pdf/__init__.py create mode 100644 sticker-business/packing-algorithm/app/pdf/output.py create mode 100644 sticker-business/packing-algorithm/app/svg/__init__.py create mode 100644 sticker-business/packing-algorithm/app/svg/loader.py create mode 100644 sticker-business/packing-algorithm/app/visualization/__init__.py create mode 100644 sticker-business/packing-algorithm/app/visualization/preview.py create mode 100644 sticker-business/packing-algorithm/pyproject.toml create mode 100644 sticker-business/packing-algorithm/testrun.py create mode 100644 sticker-business/packing-algorithm/tests/fixtures/tetrispieceI.hpgl create mode 100644 sticker-business/packing-algorithm/tests/fixtures/tetrispieceI.svg create mode 100644 sticker-business/packing-algorithm/tests/fixtures/tetrispiecesL.svg create mode 100644 sticker-business/packing-algorithm/tests/fixtures/tetrispiecesS.svg create mode 100644 sticker-business/packing-algorithm/tests/fixtures/tetrispiecesT.svg create mode 100644 sticker-business/packing-algorithm/tests/fixtures/tetrispiecesquare.svg create mode 100644 sticker-business/packing-algorithm/tests/fixtures/tetrispiecesrevL.svg create mode 100644 sticker-business/packing-algorithm/tests/test_hpgl_output.py create mode 100644 sticker-business/packing-algorithm/tests/test_packing.py create mode 100644 sticker-business/packing-algorithm/tests/test_pdf_output.py create mode 100644 sticker-business/packing-algorithm/tests/test_placement.py create mode 100644 sticker-business/packing-algorithm/tests/test_preview.py create mode 100644 sticker-business/packing-algorithm/tests/test_sticker_model.py create mode 100644 sticker-business/packing-algorithm/tests/test_svg_input.py create mode 100644 sticker-business/packing-algorithm/teststuff.py diff --git a/sticker-business/packing-algorithm/.gitignore b/sticker-business/packing-algorithm/.gitignore new file mode 100644 index 0000000..f6d9080 --- /dev/null +++ b/sticker-business/packing-algorithm/.gitignore @@ -0,0 +1,31 @@ + +# Python bytecode and caches +__pycache__/ +*.py[cod] +.pytest_cache/ +.coverage +htmlcov/ + +# Virtual environments +.venv/ +venv/ +env/ + +# Packaging/build artifacts +build/ +dist/ +*.egg-info/ + +# IDE/editor settings +.vscode/ +.idea/ +*.swp +*.swo + +# OS files +.DS_Store +Thumbs.db + +# Generated output +output/* +!output/.gitkeep diff --git a/sticker-business/packing-algorithm/algo_doc.md b/sticker-business/packing-algorithm/algo_doc.md new file mode 100644 index 0000000..4fc866d --- /dev/null +++ b/sticker-business/packing-algorithm/algo_doc.md @@ -0,0 +1,110 @@ +I’m using a bottom-left greedy polygon-packing algorithm with a compactness score. Basically, place the biggest/weirdest/most difficult sticker first, then place the next one in the lowest leftmost position that produces the most compact sheet. +It’s greedy because I’m not planning on moving stickers again after placing them, as that would be very expensive computationally and time-wise. +It’s not optimal! But it is relatively fast, deterministic, human-readable, and with polygon geometry it can be very good. +For my code and for your visualization, I’m using tetris shapes. + +Steps: +Get info from stickers. +For a quick example, 2 5 square pieces. +A +O O +O +O +O +(0,0)(1,0) +(0,1) +(0,2) +(0,3) + +And +B +O O +O O +O +(0,0)(1,0) +(0,1)(1,1) +(0,2) +(this is just convenient picks to show the program) + +2: Sort the stickers +We need to place the largest sticker first, so we sort with a couple parameters. +Area, bounding-box area (ill get to this later), width, height, complexity. +(easiest version: +stickers.sort( + key=lambda sticker: sticker.area, + reverse=True +) +) + +For example, A would have an area of 5, and so would B. Their bounding box would be 2*4 for A, but only 2*3 for B, so you could put A first because it’s more awkward. Pick weird shapes first. + +3: +The sheet can be whatever size you want, so we can arbitrarily choose what our demo sheet size is (obviously usually it’s 8x4 or whatever the size of a half sheet is) + +4: +Sticker by Sticker +First, nothing on the sheet, so the candidate is (0,0) +And you place A there, taking up +(0,0)(1,0) +(0,1) +(0,2) +(0,3) +Then, B: +The laziest implementation would look at every coordinate, but because we’re not just working in whole numbers, that’s not helpful. +We would want to look for each edge of the current shape, including +Right side, bottom line, corners, and internal vertices. +For this example we get (2,0)(1,1)(1,2)(1,3) and (0,4) as options. + +5: Where to put it +We go through all of the candidates! +(2,0) +If we place at 2,0 it looks like +O O O O +O X O O +O X O X +O X X X +So the placement would be (2,0) + +(1,1) +It looks like +O O X X +O O O X +O O O X +O O X X + +Continue for all of those position, then consider rotations of the shape. + +A simple algorithm prefers lowest x, then lowest y, cram it all in the corner. However, I also want to consider going for the most square space. +O O X X feels more helpful than O O X X +O O X X O O O X +O O O O O O O X +O O O O O O O X + +Because it can fit a bigger sticker in that space. (Want to figure this out, what im working on currently.) + +So, I used a score: height of the shape after placement + wasted space + fragmentation. A lower score is better. + +For option (2,0) our choice was +O O O O +O X O O +O X O X +O X X X +With a bounding box of 4*4 +Where +O O X X +O O O X +O O O X +O O X X +Only has 3*4. +So it chooses that one. + +That pattern continues until we get the smallest total area and score. +Obviously normal stickers aren’t just blocks but this is proof of concept that should work. + + +I’m including a 1-step lookahead, to avoid true recursion and too much burden, but it does help fit bigger stickers. + +I’m going to include a variable for this that we can actually change to get deeper, but for now it’s staying at 0 for demos, then 1 for normal stuff, but if we want to go insane for efficiency over time we can deepen it. +Basically it’ll try one sticker, check other answers, go back a step and check again in case one of those is a more accurate fit. + + diff --git a/sticker-business/packing-algorithm/app/geometry/__init__.py b/sticker-business/packing-algorithm/app/geometry/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sticker-business/packing-algorithm/app/geometry/placement.py b/sticker-business/packing-algorithm/app/geometry/placement.py new file mode 100644 index 0000000..70bdf5f --- /dev/null +++ b/sticker-business/packing-algorithm/app/geometry/placement.py @@ -0,0 +1,27 @@ +from shapely.affinity import rotate, translate + +def place_sticker(geometry, x, y, rotation_degrees=0): + """Rotate a sticker and place its bounding box at (x,y)""" + placed = rotate( + geometry, + rotation_degrees, + origin="centroid", + ) + + min_x, min_y, _, _ = placed.bounds + + return translate( + placed, + xoff=x - min_x, + yoff = y- min_y, + ) + +def stickers_overlap(first,second): + """Return whether two stickers overlap in their interiors""" + return first.intersection(second).area >0 + +# page logic +def fits_on_page(sticker,page): + """Return whether the entire sticker lies inside the page""" + return page.covers(sticker) + diff --git a/sticker-business/packing-algorithm/app/hpgl/__init__.py b/sticker-business/packing-algorithm/app/hpgl/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sticker-business/packing-algorithm/app/hpgl/output.py b/sticker-business/packing-algorithm/app/hpgl/output.py new file mode 100644 index 0000000..aaf040a --- /dev/null +++ b/sticker-business/packing-algorithm/app/hpgl/output.py @@ -0,0 +1,54 @@ +from pathlib import Path + +def save_hpgl( + placements, + output, + units_per_unit=1, + offset=(0,0), +): + """Export sticker polygons as HPGL paths""" + output = Path(output) + + offset_x, offset_y = offset + commands = [ + "IN;", + "PA;", + "SP1;", + ] + + def plot_point(x,y): + plot_x = round((x + offset_x) * units_per_unit) + plot_y = round((y+offset_y) * units_per_unit) + return plot_x, plot_y + + for _, geometry in placements: + if geometry.geom_type != "Polygon": + raise ValueError( + "Each sticker must be a single Polygon" + ) + + coordinates = list(geometry.exterior.coords) + if len(coordinates) < 4: + raise ValueError( + "Sticker outline must contain at least three points" + ) + + points = [plot_point(x,y) for x,y in coordinates] + + # go to starting point with pen raised + start_x, start_y = points[0] + commands.append(f"PU{start_x},{start_y};") + + #lower pen + remaining_points = points[1:-1] + coordinate_data = ",".join( + f"{x},{y}" for x,y in remaining_points + ) + + commands.append(f"PD{coordinate_data};") + commands.append("PU;") + + commands.append("SP0;") + + output.parent.mkdir(parents=True,exist_ok=True) + output.write_text("\n".join(commands), encoding="ascii") diff --git a/sticker-business/packing-algorithm/app/models/__init__.py b/sticker-business/packing-algorithm/app/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sticker-business/packing-algorithm/app/models/sticker.py b/sticker-business/packing-algorithm/app/models/sticker.py new file mode 100644 index 0000000..cf8ad70 --- /dev/null +++ b/sticker-business/packing-algorithm/app/models/sticker.py @@ -0,0 +1,14 @@ +from dataclasses import dataclass,field +# from shapely.geometry import Polygon + +@dataclass +class Sticker: + """Catalog metadata and file refs for one sticker.""" + id:str + name:str + size:float + description:str + tags:list[str] = field(default_factory=list) + category:str="" + hpgl_path:str="" + svg_path:str="" \ No newline at end of file diff --git a/sticker-business/packing-algorithm/app/packing/__init__.py b/sticker-business/packing-algorithm/app/packing/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sticker-business/packing-algorithm/app/packing/greedy.py b/sticker-business/packing-algorithm/app/packing/greedy.py new file mode 100644 index 0000000..561730c --- /dev/null +++ b/sticker-business/packing-algorithm/app/packing/greedy.py @@ -0,0 +1,59 @@ +from app.geometry.placement import ( + fits_on_page, + place_sticker, + stickers_overlap, +) + +def pack_stickers(stickers,page): + """Place stickers at the first valid position (super greedy)""" + placements = [] + min_x,min_y,max_x,max_y = page.bounds + + # use smallest sticker dimension + dimensions = [ + min(sticker.bounds[2] - sticker.bounds[0], + sticker.bounds[3] - sticker.bounds[1]) + for sticker in stickers + if not sticker.is_empty + ] + + if not dimensions: + return placements + + step = min(dimensions) + if step <= 0: + return placements + + remaining = sorted( + stickers, + key=lambda s: ( + (s.bounds[2]-s.bounds[0]) + * (s.bounds[3] - s.bounds[1]) + ), + reverse= True + ) + + y = min_y + while y < max_y: + x = min_x + while x < max_x: + for sticker in stickers: + if any(original is sticker for original, _ in placements): + continue + + placed = place_sticker(sticker, x, y) + + if not fits_on_page(placed,page): + continue + + if any( + stickers_overlap(placed,existing) + for _, existing in placements + ): + continue + + placements.append((sticker, placed)) + + x+= step + y += step + return placements \ No newline at end of file diff --git a/sticker-business/packing-algorithm/app/packing/semi_greedy.py b/sticker-business/packing-algorithm/app/packing/semi_greedy.py new file mode 100644 index 0000000..da4fa78 --- /dev/null +++ b/sticker-business/packing-algorithm/app/packing/semi_greedy.py @@ -0,0 +1,64 @@ +from app.packing.greedy import pack_stickers +import random + +from app.geometry.placement import ( + fits_on_page, + place_sticker, + stickers_overlap, +) + + +def pack_stickers_semi_greedy(stickers, page, seed=None): + """Pack stickers by randomly choosing among the best candidates, including rotation""" + rng = random.Random(seed) + + remaining = sorted( + stickers, + key=lambda s: ( + (s.bounds[2] - s.bounds[0]) + * (s.bounds[3] - s.bounds[1]) + ), + reverse=True, + ) + + min_x, min_y, _,_ = page.bounds + placements = [] + + for sticker in remaining: + positions = {(min_x,min_y)} + + for _, placed in placements: + left, bottom, right, top = placed.bounds + positions.add((right, bottom)) + positions.add((left,top)) + + candidates = [] + + for x, y in positions: + for angle in (0,90,180,270): + candidate = place_sticker( + sticker, + x, + y, + rotation_degrees=angle, + ) + + if not fits_on_page(candidate,page): + continue + if any( + stickers_overlap(candidate, existing) + for _, existing in placements + ): + continue + + _, bottom, right, top = candidate.bounds + score = (top,right,bottom, x,y, angle) + candidates.append((score, candidate)) + if candidates: + candidates.sort(key=lambda item: item[0]) + + #choose randomly from 3 (variable) best valid candidates + shortlist = candidates[:3] + _, chosen = rng.choice(shortlist) + placements.append((sticker,chosen)) + return placements \ No newline at end of file diff --git a/sticker-business/packing-algorithm/app/pdf/__init__.py b/sticker-business/packing-algorithm/app/pdf/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sticker-business/packing-algorithm/app/pdf/output.py b/sticker-business/packing-algorithm/app/pdf/output.py new file mode 100644 index 0000000..e69de29 diff --git a/sticker-business/packing-algorithm/app/svg/__init__.py b/sticker-business/packing-algorithm/app/svg/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sticker-business/packing-algorithm/app/svg/loader.py b/sticker-business/packing-algorithm/app/svg/loader.py new file mode 100644 index 0000000..8c99ce7 --- /dev/null +++ b/sticker-business/packing-algorithm/app/svg/loader.py @@ -0,0 +1,175 @@ +from pathlib import Path +import re +import xml.etree.ElementTree as ET + +from shapely.geometry import Polygon +from shapely.ops import unary_union +from svgpathtools import parse_path + +def _local_tag(element): + """Return an SVG element's tag without the XML namespace, for testing/parsing.""" + return element.tag.rsplit("}",1)[-1] + +def _split_subpaths(path_data): + """Split SVG path data at moveto commands into seperate subpaths.""" + # An uppercase or lowercase M starts a subpath + # exponents can't be mistaken as commands + starts = [ + match.start() + for match in re.finditer(r"[Mm]", path_data) + ] + + if not starts: + return [] + + starts.append(len(path_data)) + + return [ + path_data[starts[i]:starts[i+1]].strip() + for i in range(len(starts) - 1) + if path_data[starts[i]:starts[i+1]].strip() + ] + +def _sample_closed_path(path_data, samples_per_curve=12): + """Convert one closed psubpath into polygon coordinates""" + path = parse_path(path_data) + + if len(path) == 0: + return None + + #check continuity + if not path.iscontinuous() or not path.isclosed(): + return None + + points = [] + + for segment in path: + if not points: + points.append( + (segment.start.real, segment.start.imag) + ) + + for i in range(1, samples_per_curve+1): + point = segment.point(i/samples_per_curve) + points.append((point.real, point.imag)) + + if len(points) < 4: + return None + + polygon = Polygon(points) + + if polygon.is_empty or not polygon.is_valid or polygon.area <= 0: + return None + + return polygon + + + +def _parse_points(points_text): + """Parse the points attribute of an SVG polygon or line.""" + import re + values = re.findall( + r"[-+]?(?:\d*\.\d+|\d+\.?)(?:[eE][]?\d+)?", + points_text, + ) + + if len(values) < 6 or len(values) % 2: + return [] + + numbers = [float(value) for value in values] + return list(zip(numbers[::2], numbers[1::2])) + +def _read_polygons(root): + """Extract closed polygon geometry from supported SVG elements.""" + polygons = [] + for element in root.iter(): + tag = _local_tag(element) + + if tag == "path": + path_data = element.get("d","").strip() + + for subpath_data in _split_subpaths(path_data): + try: + polygon = _sample_closed_path(subpath_data) + except (ValueError, AssertionError, ZeroDivisionError): + polygon = None + + if polygon is not None: + polygons.append(polygon) + + elif tag in {"polygon", "polyline"}: + points = _parse_points(element.get("points", "")) + + if tag == "polyline" and points and points[0] != points[-1]: + continue + + if len(points) >= 3: + polygon = Polygon(points) + + if polygon.is_valid and polygon.area > 0: + polygons.append(polygon) + + return polygons + +def load_sticker_svg( + path: str | Path, + *, + merge_tolerance: float = 0.0, +): + """Load one sticker SVG and return its exterior silhouette. + + Args: + path: SVG file path. + merge_tolerance: Maximum geometric closing distance, in SVG units. + Zero merges touching shapes but does not bridge gaps. + + Returns: + A Shapely Polygon representing the sticker's outside outline. + + Raises: + FileNotFoundError: If the file does not exist. + ValueError: If no usable single sticker outline can be extracted. + """ + path = Path(path) + + if not path.is_file(): + raise FileNotFoundError(path) + + if merge_tolerance < 0: + raise ValueError("merge_tolerance cannot be negative") + + try: + root = ET.parse(path).getroot() + except ET.ParseError as exc: + raise ValueError(f"Invalid SVG file: {path}") from exc + + polygons = _read_polygons(root) + + if not polygons: + raise ValueError("no usable sticker geometry") + + geometry = unary_union(polygons) + + # Close small gaps, then restore the original overall scale as much + # as possible. This is a geometric closing operation. + if merge_tolerance > 0: + geometry = ( + geometry.buffer(merge_tolerance) + .buffer(-merge_tolerance) + ) + + # The cutter needs one continuous exterior outline. + if geometry.geom_type == "MultiPolygon": + raise ValueError( + "Sticker geometry contains disconnected components. " + "Increase merge_tolerance only if the gaps are unintended." + ) + + if geometry.geom_type != "Polygon" or geometry.is_empty: + raise ValueError("no usable sticker geometry") + + if not geometry.is_valid or geometry.area <= 0: + raise ValueError("no usable sticker geometry") + + # Return only the exterior silhouette, discarding interior rings. + return Polygon(geometry.exterior) diff --git a/sticker-business/packing-algorithm/app/visualization/__init__.py b/sticker-business/packing-algorithm/app/visualization/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sticker-business/packing-algorithm/app/visualization/preview.py b/sticker-business/packing-algorithm/app/visualization/preview.py new file mode 100644 index 0000000..a873dad --- /dev/null +++ b/sticker-business/packing-algorithm/app/visualization/preview.py @@ -0,0 +1,87 @@ +from pathlib import Path +from shapely.geometry import Polygon, MultiPolygon, GeometryCollection + +def _polygons(geometry): + """polygon componenets from shapely geometry""" + + if isinstance(geometry,Polygon): + yield geometry + elif isinstance(geometry, (MultiPolygon,GeometryCollection)): + for part in geometry.geoms: + yield from _polygons(part) + + +def save_packing_preview(page,placements,output): + """Save an svg preview of a page and its placed sticker outlines.""" + + output = Path(output) + + min_x, min_y, max_x, max_y = page.bounds + page_width = max_x - min_x + page_height = max_y - min_y + + if page_width <= 0 or page_height <= 0: + raise ValueError("Page must have positive width and height") + + padding= 20 + max_size = 800 + scale = min( + max_size / page_width, + max_size/page_height, + ) + + width = page_width * scale + 2* padding + height = page_height * scale + 2 * padding + + def point(x,y): + screen_x = padding + (x- min_x) * scale + screen_y = padding + (max_y -y) * scale + return screen_x, screen_y + + def ring_path(ring): + coords = list(ring.coords) + if not coords: + return "" + + x,y = point(*coords[0]) + commands = [f"M {x:.3f} {y:.3f}"] + + for coord in coords[1:]: + x, y = point(*coord) + commands.append(f"L {x:.3f} {y:.3f}") + + commands.append("Z") + return " ".join(commands) + + output_lines = [ + f'', + '', + ( + f'' + ), + ] + + for _, geometry in placements: + for polygon in _polygons(geometry): + paths = [ring_path(polygon.exterior)] + paths.extend(ring_path(ring) for ring in polygon.interiors) + path_data = " ".join(paths) + + output_lines.append( + f'' + ) + + output_lines.append("") + + output.parent.mkdir(parents=True, exist_ok=True) + output.write_text( + "\n".join(output_lines) + ) \ No newline at end of file diff --git a/sticker-business/packing-algorithm/pyproject.toml b/sticker-business/packing-algorithm/pyproject.toml new file mode 100644 index 0000000..e93ae3c --- /dev/null +++ b/sticker-business/packing-algorithm/pyproject.toml @@ -0,0 +1,26 @@ +[build-system] +requires = ["setuptools>=75"] +build-backed = "setuptools.build_meta" + +[project] +name = "sticker-packer" +version = "0.1.0" +description = "Test-driven geometry and HPGL tools for packing sticker outlines" +requires-python = ">=3.11" +dependencies = [ + "shapely>=2.0, <3.0", + "svgpathtools>=1.6,<2.0", +] + +[project.optional-dependencies] +test = [ + "pytest>=8.0,<9.0", +] + +[tool.pytest.ini_options] +testpaths = ["test"] +pythonpath = ["."] +addopts = "-ra" + +[tool.setuptools.packages.find] +include = ["app*"] \ No newline at end of file diff --git a/sticker-business/packing-algorithm/testrun.py b/sticker-business/packing-algorithm/testrun.py new file mode 100644 index 0000000..7eecb3c --- /dev/null +++ b/sticker-business/packing-algorithm/testrun.py @@ -0,0 +1,28 @@ +from shapely.geometry import box +from app.packing.greedy import pack_stickers +from app.packing.semi_greedy import pack_stickers_semi_greedy +from app.visualization.preview import save_packing_preview + +page = box(0, 0, 300, 300) + +stickers = [ + box(0, 0, 20, 10), + box(0, 0, 15, 15), + box(0, 0, 10, 25), +] + +placements = pack_stickers_semi_greedy(stickers, page, seed=42) + +save_packing_preview( + page, + placements, + "output/packing_preview_semi_greedy.svg", +) + +placements = pack_stickers(stickers, page) + +save_packing_preview( + page, + placements, + "output/packing_preview_greedy.svg", +) \ No newline at end of file diff --git a/sticker-business/packing-algorithm/tests/fixtures/tetrispieceI.hpgl b/sticker-business/packing-algorithm/tests/fixtures/tetrispieceI.hpgl new file mode 100644 index 0000000..44edcdc --- /dev/null +++ b/sticker-business/packing-algorithm/tests/fixtures/tetrispieceI.hpgl @@ -0,0 +1,78 @@ +%-12345X@PJL SET RESOLUTION = 300 +@PJL SET ORIENTATION = LANDSCAPE +@PJL ENTER LANGUAGE = HPGL2 +%1B +BP5,1;IN;SP1;IW;PS9600,7100;TR0;IP0,0,7100,9600;SC; +PC1,82,229,250; +PU51928,206583; +PM0; +PU51928,206583; +PD51928,204548,51928,202513,51928,200479,51928,198444,51928,196410,51928,194375,51928,192341,51928,190306,51928,188272,51928,186237,51928,184203,51928,182168,51928,180134,51928,178099,51928,176065,51928,174030,51928,171995,51928,169961,51928,167926,51928,165892,51928,163857,51928,161823,51928,159788,51928,157754,49889,157754,47851,157754,45813,157754,43775,157754,41737,157754,39699,157754,37661,157754,35624,157754,33586,157754,31548,157754,29510,157754,27473,157754,25435,157754,23397,157754,21359,157754,19322,157754,17284,157754,15246,157754,13208,157754,11170,157754,9132,157754,7094,157754,5056,157754,3018,157754,3018,159788,3018,161823,3018,163857,3018,165892,3018,167926,3018,169961,3018,171995,3018,174030,3018,176065,3018,178099,3018,180134,3018,182168,3018,184203,3018,186237,3018,188272,3018,190306,3018,192341,3018,194375,3018,196410,3018,198444,3018,200479,3018,202513,3018,204548,3018,206583,5056,206583,7094,206583,9132,206583,11170,206583,13208,206583,15246,206583,17284,206583,19322,206583,21359,206583,23397,206583,25435,206583,27473,206583,29510,206583,31548,206583,33586,206583,35624,206583,37661,206583,39699,206583,41737,206583,43775,206583,45813,206583,47851,206583,49889,206583,51928,206583; +PU;PM1; +PU51928,155224; +PD51928,153191,51928,151157,51928,149124,51928,147091,51928,145058,51928,143024,51928,140991,51928,138958,51928,136925,51928,134891,51928,132858,51928,130825,51928,128791,51928,126758,51928,124725,51928,122692,51928,120658,51928,118625,51928,116592,51928,114559,51928,112525,51928,110492,51928,108459,51928,106426,49889,106426,47851,106426,45813,106426,43774,106426,41736,106426,39698,106426,37659,106426,35621,106426,33583,106426,31544,106426,29506,106426,27468,106426,25429,106426,23391,106426,21352,106426,19314,106426,17276,106426,15237,106426,13199,106426,11161,106426,9122,106426,7084,106426,5046,106426,3007,106426,3007,108459,3007,110492,3007,112525,3007,114559,3007,116592,3007,118625,3007,120658,3007,122692,3007,124725,3007,126758,3007,128791,3007,130825,3007,132858,3007,134891,3007,136925,3007,138958,3007,140991,3007,143024,3007,145058,3007,147091,3007,149124,3007,151157,3007,153191,3007,155224,5046,155224,7084,155224,9122,155224,11161,155224,13199,155224,15237,155224,17276,155224,19314,155224,21352,155224,23391,155224,25429,155224,27468,155224,29506,155224,31544,155224,33583,155224,35621,155224,37659,155224,39698,155224,41736,155224,43774,155224,45813,155224,47851,155224,49889,155224,51928,155224; +PU;PM1; +PU51938,103378; +PD51938,101355,51938,99331,51938,97308,51938,95285,51938,93262,51938,91239,51938,89216,51938,87193,51938,85170,51938,83147,51938,81123,51938,79100,51938,77077,51938,75054,51938,73031,51938,71008,51938,68985,51938,66962,51938,64939,51938,62916,51938,60892,51938,58869,51938,56846,51938,54823,49898,54823,47859,54823,45819,54823,43780,54823,41741,54823,39702,54823,37663,54823,35624,54823,33585,54823,31546,54823,29507,54823,27469,54823,25430,54823,23392,54823,21353,54823,19315,54823,17276,54823,15238,54823,13199,54823,11161,54823,9122,54823,7084,54823,5046,54823,3007,54823,3007,56832,3007,58841,3007,60851,3007,62860,3007,64869,3007,66878,3007,68887,3007,70896,3007,72905,3007,74914,3007,76924,3007,78933,3007,80942,3007,82951,3007,84960,3007,86969,3007,88978,3007,90988,3007,92997,3007,95006,3007,97015,3007,99024,3007,101033,3007,103042,3322,103280,3679,103485,4073,103660,4502,103807,4961,103927,5448,104023,5959,104097,6491,104151,7040,104186,7602,104205,8175,104209,8755,104201,9339,104182,9922,104154,10503,104120,11076,104081,11639,104039,12188,103997,12721,103955,13233,103917,13721,103884,14181,103858,14611,103841,15006,103835,16051,103835,17097,103835,18141,103835,19186,103835,20231,103835,21276,103835,22320,103835,23365,103835,24409,103835,25454,103835,26498,103835,27542,103835,28587,103835,29631,103835,30675,103835,31719,103835,32763,103835,33807,103835,34851,103835,35895,103835,36939,103835,37983,103835,39027,103835,40071,103835,40533,103839,41005,103850,41487,103867,41978,103888,42476,103913,42981,103941,43491,103969,44006,103996,44523,104022,45043,104046,45564,104065,46084,104079,46604,104086,47122,104085,47636,104076,48146,104056,48650,104025,49148,103981,49638,103922,50120,103849,50592,103759,51053,103651,51502,103525,51938,103378; +PU;PM1; +PU51928,52395; +PD51928,50350,51928,48306,51928,46261,51928,44217,51928,42173,51928,40128,51928,38084,51928,36040,51928,33996,51928,31952,51928,29908,51928,27864,51928,25820,51928,23776,51928,21731,51928,19687,51928,17643,51928,15599,51928,13555,51928,11510,51928,9466,51928,7422,51928,5377,51928,3332,49889,3332,47851,3332,45813,3332,43775,3332,41737,3332,39699,3332,37661,3332,35624,3332,33586,3332,31548,3332,29510,3332,27473,3332,25435,3332,23397,3332,21359,3332,19322,3332,17284,3332,15246,3332,13208,3332,11170,3332,9132,3332,7094,3332,5056,3332,3018,3332,3018,5377,3018,7422,3018,9466,3018,11510,3018,13555,3018,15599,3018,17643,3018,19687,3018,21731,3018,23776,3018,25820,3018,27864,3018,29908,3018,31952,3018,33996,3018,36040,3018,38084,3018,40128,3018,42173,3018,44217,3018,46261,3018,48306,3018,50350,3018,52395,5056,52395,7094,52395,9132,52395,11170,52395,13208,52395,15246,52395,17284,52395,19322,52395,21359,52395,23397,52395,25435,52395,27473,52395,29510,52395,31548,52395,33586,52395,35624,52395,37661,52395,39699,52395,41737,52395,43775,52395,45813,52395,47851,52395,49889,52395,51928,52395; +PU;PM1; +PM2;FP; +IW; +PU51928,206583; +PD51928,204548,51928,202513,51928,200479,51928,198444,51928,196410,51928,194375,51928,192341,51928,190306,51928,188272,51928,186237,51928,184203,51928,182168,51928,180134,51928,178099,51928,176065,51928,174030,51928,171995,51928,169961,51928,167926,51928,165892,51928,163857,51928,161823,51928,159788,51928,157754,49889,157754,47851,157754,45813,157754,43775,157754,41737,157754,39699,157754,37661,157754,35624,157754,33586,157754,31548,157754,29510,157754,27473,157754,25435,157754,23397,157754,21359,157754,19322,157754,17284,157754,15246,157754,13208,157754,11170,157754,9132,157754,7094,157754,5056,157754,3018,157754,3018,159788,3018,161823,3018,163857,3018,165892,3018,167926,3018,169961,3018,171995,3018,174030,3018,176065,3018,178099,3018,180134,3018,182168,3018,184203,3018,186237,3018,188272,3018,190306,3018,192341,3018,194375,3018,196410,3018,198444,3018,200479,3018,202513,3018,204548,3018,206583,5056,206583,7094,206583,9132,206583,11170,206583,13208,206583,15246,206583,17284,206583,19322,206583,21359,206583,23397,206583,25435,206583,27473,206583,29510,206583,31548,206583,33586,206583,35624,206583,37661,206583,39699,206583,41737,206583,43775,206583,45813,206583,47851,206583,49889,206583,51928,206583,51928,206583; +PU51928,155224; +PD51928,153191,51928,151157,51928,149124,51928,147091,51928,145058,51928,143024,51928,140991,51928,138958,51928,136925,51928,134891,51928,132858,51928,130825,51928,128791,51928,126758,51928,124725,51928,122692,51928,120658,51928,118625,51928,116592,51928,114559,51928,112525,51928,110492,51928,108459,51928,106426,49889,106426,47851,106426,45813,106426,43774,106426,41736,106426,39698,106426,37659,106426,35621,106426,33583,106426,31544,106426,29506,106426,27468,106426,25429,106426,23391,106426,21352,106426,19314,106426,17276,106426,15237,106426,13199,106426,11161,106426,9122,106426,7084,106426,5046,106426,3007,106426,3007,108459,3007,110492,3007,112525,3007,114559,3007,116592,3007,118625,3007,120658,3007,122692,3007,124725,3007,126758,3007,128791,3007,130825,3007,132858,3007,134891,3007,136925,3007,138958,3007,140991,3007,143024,3007,145058,3007,147091,3007,149124,3007,151157,3007,153191,3007,155224,5046,155224,7084,155224,9122,155224,11161,155224,13199,155224,15237,155224,17276,155224,19314,155224,21352,155224,23391,155224,25429,155224,27468,155224,29506,155224,31544,155224,33583,155224,35621,155224,37659,155224,39698,155224,41736,155224,43774,155224,45813,155224,47851,155224,49889,155224,51928,155224,51928,155224; +PU51938,103378; +PD51938,101355,51938,99331,51938,97308,51938,95285,51938,93262,51938,91239,51938,89216,51938,87193,51938,85170,51938,83147,51938,81123,51938,79100,51938,77077,51938,75054,51938,73031,51938,71008,51938,68985,51938,66962,51938,64939,51938,62916,51938,60892,51938,58869,51938,56846,51938,54823,49898,54823,47859,54823,45819,54823,43780,54823,41741,54823,39702,54823,37663,54823,35624,54823,33585,54823,31546,54823,29507,54823,27469,54823,25430,54823,23392,54823,21353,54823,19315,54823,17276,54823,15238,54823,13199,54823,11161,54823,9122,54823,7084,54823,5046,54823,3007,54823,3007,56832,3007,58841,3007,60851,3007,62860,3007,64869,3007,66878,3007,68887,3007,70896,3007,72905,3007,74914,3007,76924,3007,78933,3007,80942,3007,82951,3007,84960,3007,86969,3007,88978,3007,90988,3007,92997,3007,95006,3007,97015,3007,99024,3007,101033,3007,103042,3322,103280,3679,103485,4073,103660,4502,103807,4961,103927,5448,104023,5959,104097,6491,104151,7040,104186,7602,104205,8175,104209,8755,104201,9339,104182,9922,104154,10503,104120,11076,104081,11639,104039,12188,103997,12721,103955,13233,103917,13721,103884,14181,103858,14611,103841,15006,103835,16051,103835,17097,103835,18141,103835,19186,103835,20231,103835,21276,103835,22320,103835,23365,103835,24409,103835,25454,103835,26498,103835,27542,103835,28587,103835,29631,103835,30675,103835,31719,103835,32763,103835,33807,103835,34851,103835,35895,103835,36939,103835,37983,103835,39027,103835,40071,103835,40533,103839,41005,103850,41487,103867,41978,103888,42476,103913,42981,103941,43491,103969,44006,103996,44523,104022,45043,104046,45564,104065,46084,104079,46604,104086,47122,104085,47636,104076,48146,104056,48650,104025,49148,103981,49638,103922,50120,103849,50592,103759,51053,103651,51502,103525,51938,103378,51938,103378; +PU51928,52395; +PD51928,50350,51928,48306,51928,46261,51928,44217,51928,42173,51928,40128,51928,38084,51928,36040,51928,33996,51928,31952,51928,29908,51928,27864,51928,25820,51928,23776,51928,21731,51928,19687,51928,17643,51928,15599,51928,13555,51928,11510,51928,9466,51928,7422,51928,5377,51928,3332,49889,3332,47851,3332,45813,3332,43775,3332,41737,3332,39699,3332,37661,3332,35624,3332,33586,3332,31548,3332,29510,3332,27473,3332,25435,3332,23397,3332,21359,3332,19322,3332,17284,3332,15246,3332,13208,3332,11170,3332,9132,3332,7094,3332,5056,3332,3018,3332,3018,5377,3018,7422,3018,9466,3018,11510,3018,13555,3018,15599,3018,17643,3018,19687,3018,21731,3018,23776,3018,25820,3018,27864,3018,29908,3018,31952,3018,33996,3018,36040,3018,38084,3018,40128,3018,42173,3018,44217,3018,46261,3018,48306,3018,50350,3018,52395,5056,52395,7094,52395,9132,52395,11170,52395,13208,52395,15246,52395,17284,52395,19322,52395,21359,52395,23397,52395,25435,52395,27473,52395,29510,52395,31548,52395,33586,52395,35624,52395,37661,52395,39699,52395,41737,52395,43775,52395,45813,52395,47851,52395,49889,52395,51928,52395,51928,52395; +PC1,2,5,6; +PU996,209153; +PM0; +PU996,209153; +PD3193,209153,5390,209153,7587,209153,9783,209152,11980,209152,14177,209151,16373,209151,18569,209150,20766,209149,22962,209148,25158,209148,27354,209147,29551,209146,31747,209145,33943,209144,36139,209142,38335,209141,40531,209140,42726,209139,44922,209138,47118,209136,49314,209135,51510,209134,53706,209133,53707,200459,53708,191785,53709,183111,53711,174437,53712,165764,53713,157090,53714,148417,53715,139743,53717,131070,53718,122397,53719,113723,53720,105050,53721,96377,53721,87704,53722,79031,53723,70358,53724,61685,53724,53012,53725,44339,53725,35667,53726,26994,53726,18321,53726,9648,53726,975,51535,988,49344,1001,47153,1013,44962,1026,42771,1039,40580,1052,38388,1064,36197,1077,34006,1090,31814,1102,29622,1115,27431,1128,25239,1140,23047,1153,20855,1166,18664,1179,16472,1191,14280,1204,12088,1217,9896,1229,7704,1242,5512,1255,3320,1267,1128,1280,1123,9942,1118,18603,1112,27264,1107,35926,1102,44587,1097,53248,1092,61910,1087,70571,1082,79232,1076,87894,1071,96555,1066,105217,1060,113878,1055,122539,1049,131201,1043,139862,1038,148523,1032,157185,1026,165846,1020,174508,1014,183169,1008,191830,1002,200492,996,209153; +PU;PM1; +PU51928,206583; +PD49889,206583,47851,206583,45813,206583,43775,206583,41737,206583,39699,206583,37661,206583,35624,206583,33586,206583,31548,206583,29510,206583,27473,206583,25435,206583,23397,206583,21359,206583,19322,206583,17284,206583,15246,206583,13208,206583,11170,206583,9132,206583,7094,206583,5056,206583,3018,206583,3018,204548,3018,202513,3018,200479,3018,198444,3018,196410,3018,194375,3018,192341,3018,190306,3018,188272,3018,186237,3018,184203,3018,182168,3018,180134,3018,178099,3018,176065,3018,174030,3018,171995,3018,169961,3018,167926,3018,165892,3018,163857,3018,161823,3018,159788,3018,157754,5056,157754,7094,157754,9132,157754,11170,157754,13208,157754,15246,157754,17284,157754,19322,157754,21359,157754,23397,157754,25435,157754,27473,157754,29510,157754,31548,157754,33586,157754,35624,157754,37661,157754,39699,157754,41737,157754,43775,157754,45813,157754,47851,157754,49889,157754,51928,157754,51928,159788,51928,161823,51928,163857,51928,165892,51928,167926,51928,169961,51928,171995,51928,174030,51928,176065,51928,178099,51928,180134,51928,182168,51928,184203,51928,186237,51928,188272,51928,190306,51928,192341,51928,194375,51928,196410,51928,198444,51928,200479,51928,202513,51928,204548,51928,206583; +PU;PM1; +PU51928,155224; +PD49889,155224,47851,155224,45813,155224,43774,155224,41736,155224,39698,155224,37659,155224,35621,155224,33583,155224,31544,155224,29506,155224,27468,155224,25429,155224,23391,155224,21352,155224,19314,155224,17276,155224,15237,155224,13199,155224,11161,155224,9122,155224,7084,155224,5046,155224,3007,155224,3007,153191,3007,151157,3007,149124,3007,147091,3007,145058,3007,143024,3007,140991,3007,138958,3007,136925,3007,134891,3007,132858,3007,130825,3007,128791,3007,126758,3007,124725,3007,122692,3007,120658,3007,118625,3007,116592,3007,114559,3007,112525,3007,110492,3007,108459,3007,106426,5046,106426,7084,106426,9122,106426,11161,106426,13199,106426,15237,106426,17276,106426,19314,106426,21352,106426,23391,106426,25429,106426,27468,106426,29506,106426,31544,106426,33583,106426,35621,106426,37659,106426,39698,106426,41736,106426,43774,106426,45813,106426,47851,106426,49889,106426,51928,106426,51928,108459,51928,110492,51928,112525,51928,114559,51928,116592,51928,118625,51928,120658,51928,122692,51928,124725,51928,126758,51928,128791,51928,130825,51928,132858,51928,134891,51928,136925,51928,138958,51928,140991,51928,143024,51928,145058,51928,147091,51928,149124,51928,151157,51928,153191,51928,155224; +PU;PM1; +PU51938,103378; +PD51502,103525,51053,103651,50592,103759,50120,103849,49638,103922,49148,103981,48650,104025,48146,104056,47636,104076,47122,104085,46604,104086,46084,104079,45564,104065,45043,104046,44523,104022,44006,103996,43491,103969,42981,103941,42476,103913,41978,103888,41487,103867,41005,103850,40533,103839,40071,103835,39027,103835,37983,103835,36939,103835,35895,103835,34851,103835,33807,103835,32763,103835,31719,103835,30675,103835,29631,103835,28587,103835,27542,103835,26498,103835,25454,103835,24409,103835,23365,103835,22320,103835,21276,103835,20231,103835,19186,103835,18141,103835,17097,103835,16051,103835,15006,103835,14611,103841,14181,103858,13721,103884,13233,103917,12721,103955,12188,103997,11639,104039,11076,104081,10503,104120,9922,104154,9339,104182,8755,104201,8175,104209,7602,104205,7040,104186,6491,104151,5959,104097,5448,104023,4961,103927,4502,103807,4073,103660,3679,103485,3322,103280,3007,103042,3007,101033,3007,99024,3007,97015,3007,95006,3007,92997,3007,90988,3007,88978,3007,86969,3007,84960,3007,82951,3007,80942,3007,78933,3007,76924,3007,74914,3007,72905,3007,70896,3007,68887,3007,66878,3007,64869,3007,62860,3007,60851,3007,58841,3007,56832,3007,54823,5046,54823,7084,54823,9122,54823,11161,54823,13199,54823,15238,54823,17276,54823,19315,54823,21353,54823,23392,54823,25430,54823,27469,54823,29507,54823,31546,54823,33585,54823,35624,54823,37663,54823,39702,54823,41741,54823,43780,54823,45819,54823,47859,54823,49898,54823,51938,54823,51938,56846,51938,58869,51938,60892,51938,62916,51938,64939,51938,66962,51938,68985,51938,71008,51938,73031,51938,75054,51938,77077,51938,79100,51938,81123,51938,83147,51938,85170,51938,87193,51938,89216,51938,91239,51938,93262,51938,95285,51938,97308,51938,99331,51938,101355,51938,103378; +PU;PM1; +PU51928,52395; +PD49889,52395,47851,52395,45813,52395,43775,52395,41737,52395,39699,52395,37661,52395,35624,52395,33586,52395,31548,52395,29510,52395,27473,52395,25435,52395,23397,52395,21359,52395,19322,52395,17284,52395,15246,52395,13208,52395,11170,52395,9132,52395,7094,52395,5056,52395,3018,52395,3018,50350,3018,48306,3018,46261,3018,44217,3018,42173,3018,40128,3018,38084,3018,36040,3018,33996,3018,31952,3018,29908,3018,27864,3018,25820,3018,23776,3018,21731,3018,19687,3018,17643,3018,15599,3018,13555,3018,11510,3018,9466,3018,7422,3018,5377,3018,3332,5056,3332,7094,3332,9132,3332,11170,3332,13208,3332,15246,3332,17284,3332,19322,3332,21359,3332,23397,3332,25435,3332,27473,3332,29510,3332,31548,3332,33586,3332,35624,3332,37661,3332,39699,3332,41737,3332,43775,3332,45813,3332,47851,3332,49889,3332,51928,3332,51928,5377,51928,7422,51928,9466,51928,11510,51928,13555,51928,15599,51928,17643,51928,19687,51928,21731,51928,23776,51928,25820,51928,27864,51928,29908,51928,31952,51928,33996,51928,36040,51928,38084,51928,40128,51928,42173,51928,44217,51928,46261,51928,48306,51928,50350,51928,52395; +PU;PM1; +PM2;FP; +IW; +PU996,209153; +PD3193,209153,5390,209153,7587,209153,9783,209152,11980,209152,14177,209151,16373,209151,18569,209150,20766,209149,22962,209148,25158,209148,27354,209147,29551,209146,31747,209145,33943,209144,36139,209142,38335,209141,40531,209140,42726,209139,44922,209138,47118,209136,49314,209135,51510,209134,53706,209133,53707,200459,53708,191785,53709,183111,53711,174437,53712,165764,53713,157090,53714,148417,53715,139743,53717,131070,53718,122397,53719,113723,53720,105050,53721,96377,53721,87704,53722,79031,53723,70358,53724,61685,53724,53012,53725,44339,53725,35667,53726,26994,53726,18321,53726,9648,53726,975,51535,988,49344,1001,47153,1013,44962,1026,42771,1039,40580,1052,38388,1064,36197,1077,34006,1090,31814,1102,29622,1115,27431,1128,25239,1140,23047,1153,20855,1166,18664,1179,16472,1191,14280,1204,12088,1217,9896,1229,7704,1242,5512,1255,3320,1267,1128,1280,1123,9942,1118,18603,1112,27264,1107,35926,1102,44587,1097,53248,1092,61910,1087,70571,1082,79232,1076,87894,1071,96555,1066,105217,1060,113878,1055,122539,1049,131201,1043,139862,1038,148523,1032,157185,1026,165846,1020,174508,1014,183169,1008,191830,1002,200492,996,209153,996,209153; +PU51928,206583; +PD49889,206583,47851,206583,45813,206583,43775,206583,41737,206583,39699,206583,37661,206583,35624,206583,33586,206583,31548,206583,29510,206583,27473,206583,25435,206583,23397,206583,21359,206583,19322,206583,17284,206583,15246,206583,13208,206583,11170,206583,9132,206583,7094,206583,5056,206583,3018,206583,3018,204548,3018,202513,3018,200479,3018,198444,3018,196410,3018,194375,3018,192341,3018,190306,3018,188272,3018,186237,3018,184203,3018,182168,3018,180134,3018,178099,3018,176065,3018,174030,3018,171995,3018,169961,3018,167926,3018,165892,3018,163857,3018,161823,3018,159788,3018,157754,5056,157754,7094,157754,9132,157754,11170,157754,13208,157754,15246,157754,17284,157754,19322,157754,21359,157754,23397,157754,25435,157754,27473,157754,29510,157754,31548,157754,33586,157754,35624,157754,37661,157754,39699,157754,41737,157754,43775,157754,45813,157754,47851,157754,49889,157754,51928,157754,51928,159788,51928,161823,51928,163857,51928,165892,51928,167926,51928,169961,51928,171995,51928,174030,51928,176065,51928,178099,51928,180134,51928,182168,51928,184203,51928,186237,51928,188272,51928,190306,51928,192341,51928,194375,51928,196410,51928,198444,51928,200479,51928,202513,51928,204548,51928,206583,51928,206583; +PU51928,155224; +PD49889,155224,47851,155224,45813,155224,43774,155224,41736,155224,39698,155224,37659,155224,35621,155224,33583,155224,31544,155224,29506,155224,27468,155224,25429,155224,23391,155224,21352,155224,19314,155224,17276,155224,15237,155224,13199,155224,11161,155224,9122,155224,7084,155224,5046,155224,3007,155224,3007,153191,3007,151157,3007,149124,3007,147091,3007,145058,3007,143024,3007,140991,3007,138958,3007,136925,3007,134891,3007,132858,3007,130825,3007,128791,3007,126758,3007,124725,3007,122692,3007,120658,3007,118625,3007,116592,3007,114559,3007,112525,3007,110492,3007,108459,3007,106426,5046,106426,7084,106426,9122,106426,11161,106426,13199,106426,15237,106426,17276,106426,19314,106426,21352,106426,23391,106426,25429,106426,27468,106426,29506,106426,31544,106426,33583,106426,35621,106426,37659,106426,39698,106426,41736,106426,43774,106426,45813,106426,47851,106426,49889,106426,51928,106426,51928,108459,51928,110492,51928,112525,51928,114559,51928,116592,51928,118625,51928,120658,51928,122692,51928,124725,51928,126758,51928,128791,51928,130825,51928,132858,51928,134891,51928,136925,51928,138958,51928,140991,51928,143024,51928,145058,51928,147091,51928,149124,51928,151157,51928,153191,51928,155224,51928,155224; +PU51938,103378; +PD51502,103525,51053,103651,50592,103759,50120,103849,49638,103922,49148,103981,48650,104025,48146,104056,47636,104076,47122,104085,46604,104086,46084,104079,45564,104065,45043,104046,44523,104022,44006,103996,43491,103969,42981,103941,42476,103913,41978,103888,41487,103867,41005,103850,40533,103839,40071,103835,39027,103835,37983,103835,36939,103835,35895,103835,34851,103835,33807,103835,32763,103835,31719,103835,30675,103835,29631,103835,28587,103835,27542,103835,26498,103835,25454,103835,24409,103835,23365,103835,22320,103835,21276,103835,20231,103835,19186,103835,18141,103835,17097,103835,16051,103835,15006,103835,14611,103841,14181,103858,13721,103884,13233,103917,12721,103955,12188,103997,11639,104039,11076,104081,10503,104120,9922,104154,9339,104182,8755,104201,8175,104209,7602,104205,7040,104186,6491,104151,5959,104097,5448,104023,4961,103927,4502,103807,4073,103660,3679,103485,3322,103280,3007,103042,3007,101033,3007,99024,3007,97015,3007,95006,3007,92997,3007,90988,3007,88978,3007,86969,3007,84960,3007,82951,3007,80942,3007,78933,3007,76924,3007,74914,3007,72905,3007,70896,3007,68887,3007,66878,3007,64869,3007,62860,3007,60851,3007,58841,3007,56832,3007,54823,5046,54823,7084,54823,9122,54823,11161,54823,13199,54823,15238,54823,17276,54823,19315,54823,21353,54823,23392,54823,25430,54823,27469,54823,29507,54823,31546,54823,33585,54823,35624,54823,37663,54823,39702,54823,41741,54823,43780,54823,45819,54823,47859,54823,49898,54823,51938,54823,51938,56846,51938,58869,51938,60892,51938,62916,51938,64939,51938,66962,51938,68985,51938,71008,51938,73031,51938,75054,51938,77077,51938,79100,51938,81123,51938,83147,51938,85170,51938,87193,51938,89216,51938,91239,51938,93262,51938,95285,51938,97308,51938,99331,51938,101355,51938,103378,51938,103378; +PU51928,52395; +PD49889,52395,47851,52395,45813,52395,43775,52395,41737,52395,39699,52395,37661,52395,35624,52395,33586,52395,31548,52395,29510,52395,27473,52395,25435,52395,23397,52395,21359,52395,19322,52395,17284,52395,15246,52395,13208,52395,11170,52395,9132,52395,7094,52395,5056,52395,3018,52395,3018,50350,3018,48306,3018,46261,3018,44217,3018,42173,3018,40128,3018,38084,3018,36040,3018,33996,3018,31952,3018,29908,3018,27864,3018,25820,3018,23776,3018,21731,3018,19687,3018,17643,3018,15599,3018,13555,3018,11510,3018,9466,3018,7422,3018,5377,3018,3332,5056,3332,7094,3332,9132,3332,11170,3332,13208,3332,15246,3332,17284,3332,19322,3332,21359,3332,23397,3332,25435,3332,27473,3332,29510,3332,31548,3332,33586,3332,35624,3332,37661,3332,39699,3332,41737,3332,43775,3332,45813,3332,47851,3332,49889,3332,51928,3332,51928,5377,51928,7422,51928,9466,51928,11510,51928,13555,51928,15599,51928,17643,51928,19687,51928,21731,51928,23776,51928,25820,51928,27864,51928,29908,51928,31952,51928,33996,51928,36040,51928,38084,51928,40128,51928,42173,51928,44217,51928,46261,51928,48306,51928,50350,51928,52395,51928,52395; +PC1,178,178,178; +PU20,209133; +PM0; +PU20,209133; +PD61,209134,102,209135,142,209136,183,209138,224,209139,264,209140,305,209141,345,209142,386,209144,427,209145,467,209146,508,209147,549,209148,589,209148,630,209149,671,209150,711,209151,752,209151,792,209152,833,209152,874,209153,914,209153,955,209153,996,209153,1002,200492,1008,191830,1014,183169,1020,174508,1026,165846,1032,157185,1038,148523,1043,139862,1049,131201,1055,122539,1060,113878,1066,105217,1071,96555,1076,87894,1082,79232,1087,70571,1092,61910,1097,53248,1102,44587,1107,35926,1112,27264,1118,18603,1123,9942,1128,1280,3320,1267,5512,1255,7704,1242,9896,1229,12088,1217,14280,1204,16472,1191,18664,1179,20855,1166,23047,1153,25239,1140,27431,1128,29622,1115,31814,1102,34006,1090,36197,1077,38388,1064,40580,1052,42771,1039,44962,1026,47153,1013,49344,1001,51535,988,53726,975,53726,959,53726,942,53726,926,53726,909,53726,893,53726,876,53726,860,53726,843,53726,827,53726,810,53726,794,53726,777,53726,761,53726,744,53726,728,53726,711,53726,695,53726,678,53726,662,53726,645,53726,629,53726,612,53726,596,53726,579,52202,531,50676,491,49150,459,47623,433,46096,414,44567,400,43039,392,41510,389,39980,390,38450,394,36920,402,35390,413,33859,425,32329,440,30799,455,29268,472,27738,488,26208,503,24679,518,23150,531,21621,543,20093,551,18565,557,17038,559,16384,553,15711,536,15021,509,14316,476,13597,437,12867,395,12128,351,11381,308,10629,267,9874,231,9116,200,8359,178,7604,165,6854,165,6109,178,5372,207,4646,253,3931,319,3230,406,2545,516,1877,652,1229,815,603,1007,0,1229,0,9892,0,18555,0,27217,1,35880,1,44543,2,53205,2,61868,3,70530,4,79193,5,87856,5,96518,6,105181,7,113844,8,122506,9,131169,11,139832,12,148494,13,157157,14,165819,15,174482,17,183145,18,191807,19,200470,20,209133; +PM1;PM2;FP; +IW; +PU20,209133; +PD61,209134,102,209135,142,209136,183,209138,224,209139,264,209140,305,209141,345,209142,386,209144,427,209145,467,209146,508,209147,549,209148,589,209148,630,209149,671,209150,711,209151,752,209151,792,209152,833,209152,874,209153,914,209153,955,209153,996,209153,1002,200492,1008,191830,1014,183169,1020,174508,1026,165846,1032,157185,1038,148523,1043,139862,1049,131201,1055,122539,1060,113878,1066,105217,1071,96555,1076,87894,1082,79232,1087,70571,1092,61910,1097,53248,1102,44587,1107,35926,1112,27264,1118,18603,1123,9942,1128,1280,3320,1267,5512,1255,7704,1242,9896,1229,12088,1217,14280,1204,16472,1191,18664,1179,20855,1166,23047,1153,25239,1140,27431,1128,29622,1115,31814,1102,34006,1090,36197,1077,38388,1064,40580,1052,42771,1039,44962,1026,47153,1013,49344,1001,51535,988,53726,975,53726,959,53726,942,53726,926,53726,909,53726,893,53726,876,53726,860,53726,843,53726,827,53726,810,53726,794,53726,777,53726,761,53726,744,53726,728,53726,711,53726,695,53726,678,53726,662,53726,645,53726,629,53726,612,53726,596,53726,579,52202,531,50676,491,49150,459,47623,433,46096,414,44567,400,43039,392,41510,389,39980,390,38450,394,36920,402,35390,413,33859,425,32329,440,30799,455,29268,472,27738,488,26208,503,24679,518,23150,531,21621,543,20093,551,18565,557,17038,559,16384,553,15711,536,15021,509,14316,476,13597,437,12867,395,12128,351,11381,308,10629,267,9874,231,9116,200,8359,178,7604,165,6854,165,6109,178,5372,207,4646,253,3931,319,3230,406,2545,516,1877,652,1229,815,603,1007,0,1229,0,9892,0,18555,0,27217,1,35880,1,44543,2,53205,2,61868,3,70530,4,79193,5,87856,5,96518,6,105181,7,113844,8,122506,9,131169,11,139832,12,148494,13,157157,14,165819,15,174482,17,183145,18,191807,19,200470,20,209133,20,209133; +PC1,238,238,238; +PU0,1229; +PM0; +PU0,1229; +PD603,1007,1229,815,1877,652,2545,516,3230,406,3931,319,4646,253,5372,207,6109,178,6854,165,7604,165,8359,178,9116,200,9874,231,10629,267,11381,308,12128,351,12867,395,13597,437,14316,476,15021,509,15711,536,16384,553,17038,559,18565,557,20093,551,21621,543,23150,531,24679,518,26208,503,27738,488,29268,472,30799,455,32329,440,33859,425,35390,413,36920,402,38450,394,39980,390,41510,389,43039,392,44567,400,46096,414,47623,433,49150,459,50676,491,52202,531,53726,579,53726,555,53726,531,53726,507,53725,483,53725,458,53724,434,53724,410,53723,386,53722,362,53721,338,53721,314,53720,290,53719,265,53718,241,53717,217,53715,193,53714,169,53713,145,53712,121,53711,97,53709,72,53708,48,53707,24,53706,0,51469,0,49233,0,46996,0,44759,0,42522,0,40285,0,38048,0,35811,0,33574,0,31337,0,29100,0,26863,0,24626,0,22389,0,20152,0,17915,0,15678,0,13441,0,11204,0,8967,0,6730,0,4493,0,2257,0,20,0,19,51,18,102,17,153,15,204,14,255,13,306,12,358,11,409,9,460,8,512,7,563,6,615,5,666,5,718,4,769,3,820,2,872,2,923,1,974,1,1025,0,1077,0,1128,0,1178,0,1229; +PM1;PM2;FP; +IW; +PU0,1229; +PD603,1007,1229,815,1877,652,2545,516,3230,406,3931,319,4646,253,5372,207,6109,178,6854,165,7604,165,8359,178,9116,200,9874,231,10629,267,11381,308,12128,351,12867,395,13597,437,14316,476,15021,509,15711,536,16384,553,17038,559,18565,557,20093,551,21621,543,23150,531,24679,518,26208,503,27738,488,29268,472,30799,455,32329,440,33859,425,35390,413,36920,402,38450,394,39980,390,41510,389,43039,392,44567,400,46096,414,47623,433,49150,459,50676,491,52202,531,53726,579,53726,555,53726,531,53726,507,53725,483,53725,458,53724,434,53724,410,53723,386,53722,362,53721,338,53721,314,53720,290,53719,265,53718,241,53717,217,53715,193,53714,169,53713,145,53712,121,53711,97,53709,72,53708,48,53707,24,53706,0,51469,0,49233,0,46996,0,44759,0,42522,0,40285,0,38048,0,35811,0,33574,0,31337,0,29100,0,26863,0,24626,0,22389,0,20152,0,17915,0,15678,0,13441,0,11204,0,8967,0,6730,0,4493,0,2257,0,20,0,19,51,18,102,17,153,15,204,14,255,13,306,12,358,11,409,9,460,8,512,7,563,6,615,5,666,5,718,4,769,3,820,2,872,2,923,1,974,1,1025,0,1077,0,1128,0,1178,0,1229,0,1229; diff --git a/sticker-business/packing-algorithm/tests/fixtures/tetrispieceI.svg b/sticker-business/packing-algorithm/tests/fixtures/tetrispieceI.svg new file mode 100644 index 0000000..7b199aa --- /dev/null +++ b/sticker-business/packing-algorithm/tests/fixtures/tetrispieceI.svg @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/sticker-business/packing-algorithm/tests/fixtures/tetrispiecesL.svg b/sticker-business/packing-algorithm/tests/fixtures/tetrispiecesL.svg new file mode 100644 index 0000000..64be517 --- /dev/null +++ b/sticker-business/packing-algorithm/tests/fixtures/tetrispiecesL.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/sticker-business/packing-algorithm/tests/fixtures/tetrispiecesS.svg b/sticker-business/packing-algorithm/tests/fixtures/tetrispiecesS.svg new file mode 100644 index 0000000..69058a4 --- /dev/null +++ b/sticker-business/packing-algorithm/tests/fixtures/tetrispiecesS.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/sticker-business/packing-algorithm/tests/fixtures/tetrispiecesT.svg b/sticker-business/packing-algorithm/tests/fixtures/tetrispiecesT.svg new file mode 100644 index 0000000..cb7248e --- /dev/null +++ b/sticker-business/packing-algorithm/tests/fixtures/tetrispiecesT.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/sticker-business/packing-algorithm/tests/fixtures/tetrispiecesquare.svg b/sticker-business/packing-algorithm/tests/fixtures/tetrispiecesquare.svg new file mode 100644 index 0000000..779fa68 --- /dev/null +++ b/sticker-business/packing-algorithm/tests/fixtures/tetrispiecesquare.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/sticker-business/packing-algorithm/tests/fixtures/tetrispiecesrevL.svg b/sticker-business/packing-algorithm/tests/fixtures/tetrispiecesrevL.svg new file mode 100644 index 0000000..418bc3c --- /dev/null +++ b/sticker-business/packing-algorithm/tests/fixtures/tetrispiecesrevL.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/sticker-business/packing-algorithm/tests/test_hpgl_output.py b/sticker-business/packing-algorithm/tests/test_hpgl_output.py new file mode 100644 index 0000000..db599ec --- /dev/null +++ b/sticker-business/packing-algorithm/tests/test_hpgl_output.py @@ -0,0 +1,69 @@ +from shapely.geometry import box +from app.hpgl.output import save_hpgl + +def test_writes_a_closed_sticker_outline(tmp_path): + sticker = box(0,0,10,5) + output = tmp_path/"test.plt" + + save_hpgl( + [(sticker,sticker)], + output, + units_per_unit=1, + ) + + content = output.read_text() + + assert "IN;" in content + assert "PA;" in content + assert "SP1;" in content + assert "PU10,0;" in content + assert "PD10,5,0,5,0,0;" in content + assert "PU;" in content + assert "SP0;" in content + +def test_scales_coordinates(tmp_path): + sticker = box(0,0,10,5) + output = tmp_path / "scaled.plt" + + save_hpgl( + [(sticker,sticker)], + output, + units_per_unit = 10, + ) + + content = output.read_text() + + assert "PU100,0;" in content + assert "PD100,50,0,50,0,0;" in content + +def test_applies_coordinate_offset(tmp_path): + sticker = box(0,0,10,5) + output = tmp_path / "offset.plt" + + save_hpgl( + [(sticker,sticker)], + output, + units_per_unit = 1, + offset = (20,30), + ) + + content = output.read_text() + + assert "PU30,30;" in content + assert "PD30,35,20,35,20,30;" in content + +def test_lifts_pen_between_stickers(tmp_path): + first = box(0, 0, 10, 10) + second = box(20, 0, 30, 10) + output = tmp_path / "multiple.plt" + + save_hpgl( + [(first, first), (second, second)], + output, + units_per_unit=1, + ) + + content = output.read_text() + + assert content.count("PU;") == 2 + assert content.count("PD") == 2 \ No newline at end of file diff --git a/sticker-business/packing-algorithm/tests/test_packing.py b/sticker-business/packing-algorithm/tests/test_packing.py new file mode 100644 index 0000000..913e1af --- /dev/null +++ b/sticker-business/packing-algorithm/tests/test_packing.py @@ -0,0 +1,84 @@ +import pytest +from shapely.geometry import box +from app.packing.greedy import pack_stickers + +from app.packing.semi_greedy import pack_stickers_semi_greedy + +def test_packs_two_stickers_without_overlap(): + stickers = [ + box(0,0,10,10), + box(0,0,10,10), + ] + page = box(0,0,20,10) + + placements = pack_stickers(stickers,page) + + assert len(placements) == 2 + + first = placements[0][1] + second = placements[1][1] + + assert page.covers(first) + assert page.covers(second) + assert first.intersection(second).area == pytest.approx(0) + +def test_skips_sticker_that_cannot_fit(): + stickers = [ + box(0,0,20,20), + ] + page = box(0,0,10,10) + placements = pack_stickers(stickers,page) + assert placements == [] + +def test_returns_original_and_placed_geometry(): + sticker = box(0,0,5,5) + page = box(0,0,10,10) + placements = pack_stickers([sticker],page) + + assert len(placements) == 1 + + original, placed = placements[0] + + assert original is sticker + assert placed.area == pytest.approx(sticker.area) + assert page.covers(placed) + +def test_empty_input_returns_no_placements(): + page = box(0,0,100,100) + assert pack_stickers([],page) == [] + + +#post-greedy development tests +def test_rotates_sticker_when_needed(): + sticker = box(0,0,10,5) + page = box(0,0,5,10) + + placements = pack_stickers_semi_greedy([sticker],page) + + assert len(placements)==1 + assert page.covers(placements[0][1]) + +def test_places_large_sticker_before_small_sticker(): + small = box(0,0,2,2) + large = box(0,0,8,8) + page = box(0,0,10,10) + + placements = pack_stickers_semi_greedy([small,large], page) + + #both should fit, but the large sticker should be placed first at [0][0] + assert len(placements) == 2 + assert placements[0][0] is large + +#purely semi greedy tests + +def test_semi_greedy_packs_a_sticker(): + sticker = box(0,0,5,5) + page = box(0,0,10,10) + + placements = pack_stickers_semi_greedy( + [sticker], page, seed=42 + ) + + assert len(placements) == 1 + assert page.covers(placements[0][1]) + diff --git a/sticker-business/packing-algorithm/tests/test_pdf_output.py b/sticker-business/packing-algorithm/tests/test_pdf_output.py new file mode 100644 index 0000000..e69de29 diff --git a/sticker-business/packing-algorithm/tests/test_placement.py b/sticker-business/packing-algorithm/tests/test_placement.py new file mode 100644 index 0000000..331e14f --- /dev/null +++ b/sticker-business/packing-algorithm/tests/test_placement.py @@ -0,0 +1,58 @@ +import pytest +from shapely.geometry import box +from app.geometry.placement import place_sticker, stickers_overlap +from app.geometry.placement import fits_on_page + +def test_placement_moves_sticker_to_requested_position(): + sticker = box(0,0,10,5) + placed = place_sticker(sticker, x=20, y=30) + assert placed.bounds == pytest.approx((20,30,30,35)) + assert placed.area == pytest.approx(sticker.area) + +def test_rotation_preserves_area_and_swaps_bounds_at_90_degrees(): + sticker = box(0,0,10,5) + placed = place_sticker( + sticker, + x=0, + y=0, + rotation_degrees=90, + ) + + min_x,min_y,max_x,max_y = placed.bounds + + assert max_x - min_x == pytest.approx(5) + assert max_y - min_y == pytest.approx(10) + assert placed.area == pytest.approx(sticker.area) + +def test_overlapping_stickers_are_detected(): + first = box(0,0,10,10) + second = box(5,5,15,15) + assert stickers_overlap(first, second) + +def test_stickers_that_only_touch_edges_do_not_overlap(): + first = box(0,0,10,10) + second = box(10,0,20,10) + assert not stickers_overlap(first, second) + +def test_seperate_stickers_do_not_overlap(): + first = box(0,0,10,10) + second = box(20,20,30,30) + assert not stickers_overlap(first,second) + + +# extra geometry: page boundaries +def test_sticker_inside_page_fits(): + sticker = box(10,10,30,20) + page = box(0,0,100,100) + + assert fits_on_page(sticker, page) + +def test_sticker_outside_page_does_not_fit(): + sticker = box(90,90,110,110) + page = box(0,0,100,100) + assert not fits_on_page(sticker,page) + +def test_sticker_touching_page_edge_fits(): + sticker = box(0,0,100,20) + page = box(0,0,100,100) + assert fits_on_page(sticker, page) \ No newline at end of file diff --git a/sticker-business/packing-algorithm/tests/test_preview.py b/sticker-business/packing-algorithm/tests/test_preview.py new file mode 100644 index 0000000..9967186 --- /dev/null +++ b/sticker-business/packing-algorithm/tests/test_preview.py @@ -0,0 +1,30 @@ +from shapely.geometry import box + +from app.visualization.preview import save_packing_preview + +def test_preview_creates_svg_file(tmp_path): + page = box(0,0,100,100) + sticker = box(10,20,30,40) + output = tmp_path/"preview.svg" + + save_packing_preview( + page, + [(sticker,sticker)], + output, + ) + + assert output.exists() + + svg = output.read_text() + assert " 0 + assert len(geometry.interiors) == 0 + +def test_multiple_touching_blocks_form_one_outline(tmp_path): + svg = tmp_path / "two_blocks.svg" + svg.write_text( + """ + + + + """, + encoding="utf-8", + ) + + geometry = load_sticker_svg(svg) + + assert geometry.geom_type == "Polygon" + assert geometry.area == pytest.approx(200) + assert geometry.bounds == (0,0,20,10) + assert len(geometry.interiors) == 0 + +def test_disconnected_blocks_can_be_merged_with_tolerance(tmp_path): + svg = tmp_path / "gapped_blocks.svg" + svg.write_text( + """ + + + + """, + encoding="utf-8", + ) + + geometry = load_sticker_svg(svg, merge_tolerance=1.1) + + assert geometry.geom_type == "Polygon" + assert geometry.bounds == (0, 0, 22, 10) + assert len(geometry.interiors) == 0 + +def test_missing_svg_file_is_rejected(tmp_path): + missing_svg = tmp_path / "missing.svg" + + with pytest.raises(FileNotFoundError): + load_sticker_svg(missing_svg) + + +def test_svg_loads_as_one_nonempty_sticker(): + geometry = load_sticker_svg(FIXTURES / "tetrispieceI.svg") + + assert not geometry.is_empty + assert geometry.is_valid + assert geometry.geom_type == "Polygon" + assert geometry.area > 0 + + # sticker should just have exterior + assert len(geometry.interiors) == 0 + assert geometry.exterior.is_ring + assert geometry.is_simple \ No newline at end of file diff --git a/sticker-business/packing-algorithm/teststuff.py b/sticker-business/packing-algorithm/teststuff.py new file mode 100644 index 0000000..72b3102 --- /dev/null +++ b/sticker-business/packing-algorithm/teststuff.py @@ -0,0 +1,48 @@ +from pathlib import Path +from shapely.geometry import box + +from app.svg.loader import load_sticker_svg +from app.packing.semi_greedy import pack_stickers_semi_greedy +from app.visualization.preview import save_packing_preview + +fixtures = Path("tests/fixtures") +page = box(0, 0, 500, 500) + + +stickers = [] +# stickers = [ +# load_sticker_svg(path) +# for path in sorted(fixtures.glob("*.svg")) +# ] + +for path in sorted(fixtures.glob("*.svg")): + print(f"Loading {path.name}...") + + #each svg is ONE sticker, try to merge paths to make one silhouette + geometry = load_sticker_svg(path,merge_tolerance=8) + + if geometry.geom_type != "Polygon": + raise ValueError( + f"{path.name} did not produce one polygon:" + f"{geometry.geom_type}" + ) + + if len(geometry.interiors) > 0: + raise ValueError( + f"{path.name} contains interior outlines." + ) + + stickers.append(geometry) + +placements = pack_stickers_semi_greedy(stickers, page, seed=42) + +save_packing_preview( + page, + placements, + "output/packing_preview.svg", +) + +print(f"\nLoaded {len(stickers)} stickers.") +print(f"Placed {len(placements)} stickers.") +print(f"Skipped {len(stickers) - len(placements)} stickers.") +print(f"Preview saved to output/packing_preview.svg") \ No newline at end of file From c16e24f0aae12e0697709de4e57a180e43f74c62 Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Thu, 5 Nov 2026 13:16:28 -0500 Subject: [PATCH 33/39] added failing tests for pdf creation. --- .../packing-algorithm/pyproject.toml | 2 + .../tests/test_pdf_output.py | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/sticker-business/packing-algorithm/pyproject.toml b/sticker-business/packing-algorithm/pyproject.toml index e93ae3c..cdaf902 100644 --- a/sticker-business/packing-algorithm/pyproject.toml +++ b/sticker-business/packing-algorithm/pyproject.toml @@ -10,6 +10,8 @@ requires-python = ">=3.11" dependencies = [ "shapely>=2.0, <3.0", "svgpathtools>=1.6,<2.0", + "reportlab>=4.0", + "svglib>=1.5", ] [project.optional-dependencies] diff --git a/sticker-business/packing-algorithm/tests/test_pdf_output.py b/sticker-business/packing-algorithm/tests/test_pdf_output.py index e69de29..8e13335 100644 --- a/sticker-business/packing-algorithm/tests/test_pdf_output.py +++ b/sticker-business/packing-algorithm/tests/test_pdf_output.py @@ -0,0 +1,39 @@ +from pathlib import Path + +import pytest +from pypdf import PdfReader +from shapely.geometry import box + +def test_pdf_file_is_created(tmp_path): + sticker = box(0,0,20,10) + placed = box(10,10,30,20) + placements = [(sticker,placed)] + + sticker_paths = { + id(sticker): "tests/fixtures/tetrispieceI.svg" + } + + output = tmp_path / "stickers.pdf" + + save_pdf(placements, sticker_paths, output) + + assert output.exists() + assert output.stat().st_size > 0 + +def test_pdf_uses_half_letter_page_size(tmp_path): + sticker = box(0,0,20,10) + placed = box(10,10,30,20) + + placements = [(sticker,placed)] + sticker_paths = { + id(sticker): "tests/fixtures/tetrispieceI.svg" + } + + output = tmp_path / "stickers.pdf" + + save_pdf(placements, sticker_paths, output) + reader = PdfReader(str(output)) + page = reader.pages[0] + + assert float(page.mediabox.width) == pytest.approx(396) + assert float(page.mediabox.height) == pytest.approx(612) \ No newline at end of file From 082023af8bd323ff060f350344742e272f69c228 Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Thu, 5 Nov 2026 14:09:36 -0500 Subject: [PATCH 34/39] pdf exports are now working!! tests check for rotation, proper colors, and if the stickers are actually there or not --- .../packing-algorithm/app/pdf/output.py | 114 ++++++++++++ .../tests/test_pdf_output.py | 168 +++++++++++++++++- 2 files changed, 280 insertions(+), 2 deletions(-) diff --git a/sticker-business/packing-algorithm/app/pdf/output.py b/sticker-business/packing-algorithm/app/pdf/output.py index e69de29..7be9873 100644 --- a/sticker-business/packing-algorithm/app/pdf/output.py +++ b/sticker-business/packing-algorithm/app/pdf/output.py @@ -0,0 +1,114 @@ +from pathlib import Path +from reportlab.lib.units import inch +from reportlab.pdfgen import canvas +from reportlab.graphics import renderPDF +from svglib.svglib import svg2rlg +from shapely.affinity import rotate, translate + +PAGE_SIZE = (5.5*inch, 8.5*inch) + +def _get_rotation(original,placed): + """Determine how far the placed is rotated""" + + for angle in (0,90,180,270): + rotated = rotate( + original, + angle, + origin='centroid', + ) + + min_x,min_y, _, _ = rotated.bounds + placed_min_x, placed_min_y,_,_ = placed.bounds + + candidate = translate( + rotated, + xoff = placed_min_x - min_x, + yoff = placed_min_y - min_y, + ) + + if candidate.equals(placed): + return angle + + raise ValueError( + "Could not determine sticker rotation from placement geometry" + ) + + +def save_pdf(placements, sticker_paths, output): + """Export packed stickers as a full-color half-letter PDF. + + Args: + placements: List of (original_geometry, placed_geometry) pairs. + sticker_paths: Dictionary mapping id(original_geometry) to SVG paths. + output: Destination PDF file path.""" + + output = Path(output) + output.parent.mkdir(parents=True, exist_ok=True) + + pdf = canvas.Canvas(str(output), pagesize = PAGE_SIZE) + + for original, placed in placements: + svg_path = sticker_paths.get(id(original)) + + if not svg_path: + raise ValueError( + "No SVG artwork path found for a packed sticker." + ) + + svg_path = Path(svg_path) + if not svg_path.is_file(): + raise FileNotFoundError(svg_path) + + drawing = svg2rlg(str(svg_path)) + + if drawing is None or not drawing.width or not drawing.height: + raise ValueError( + f"Could not load SVG artwork: {svg_path}" + ) + + min_x,min_y,max_x,max_y = original.bounds + placed_min_x,placed_min_y,_,_ = placed.bounds + + geometry_width = max_x - min_x + geometry_height = max_y - min_y + + #preserving aspect ratio + + scale = min( + geometry_width / drawing.width, + geometry_height / drawing.height + ) + + artwork_width = drawing.width * scale + artwork_height = drawing.height * scale + + # Center the artwork inside its original geometry bounds. + artwork_x = min_x + (geometry_width - artwork_width) / 2 + artwork_y = min_y + (geometry_height - artwork_height) / 2 + + angle = _get_rotation(original, placed) + + # Align the rotated geometry with its packed position. + rotated = rotate(original, angle, origin="centroid") + rotated_min_x, rotated_min_y, _, _ = rotated.bounds + + offset_x = placed_min_x - rotated_min_x + offset_y = placed_min_y - rotated_min_y + + pdf.saveState() + # Apply the same rotation and translation as the packing algorithm. + centroid_x, centroid_y = original.centroid.coords[0] + + pdf.translate(offset_x, offset_y) + pdf.translate(centroid_x, centroid_y) + pdf.rotate(angle) + pdf.translate(-centroid_x, -centroid_y) + + pdf.translate(artwork_x, artwork_y) + pdf.scale(scale, scale) + + renderPDF.draw(drawing, pdf, 0, 0) + + pdf.restoreState() + + pdf.save() \ No newline at end of file diff --git a/sticker-business/packing-algorithm/tests/test_pdf_output.py b/sticker-business/packing-algorithm/tests/test_pdf_output.py index 8e13335..cf044c3 100644 --- a/sticker-business/packing-algorithm/tests/test_pdf_output.py +++ b/sticker-business/packing-algorithm/tests/test_pdf_output.py @@ -1,8 +1,9 @@ from pathlib import Path - +from app.pdf.output import save_pdf import pytest from pypdf import PdfReader from shapely.geometry import box +import fitz def test_pdf_file_is_created(tmp_path): sticker = box(0,0,20,10) @@ -36,4 +37,167 @@ def test_pdf_uses_half_letter_page_size(tmp_path): page = reader.pages[0] assert float(page.mediabox.width) == pytest.approx(396) - assert float(page.mediabox.height) == pytest.approx(612) \ No newline at end of file + assert float(page.mediabox.height) == pytest.approx(612) + +def make_svg(path, color="red",width=20,height=10): + """Create a simple svg fixture""" + + path.write_text( + f""" + + """, + encoding="utf-8", + ) + return path + +def render_pdf(output): + """Render the first pdf as a pixmap""" + document = fitz.open(output) + page = document[0] + pixmap = page.get_pixmap() + return pixmap, page + +def pixel_at(pixmap,x,y): + """return rgb at a pdf point coordinate""" + x=round(x * pixmap.width / 396) + y = round((612-y) * pixmap.height / 612) + offset = (y*pixmap.width + x) * pixmap.n + return tuple(pixmap.samples[offset:offset+3]) + + +def test_artwork_appears_at_packed_position(tmp_path): + sticker = box(0,0,20,10) + placed = box(100,100,120,110) + artwork = make_svg(tmp_path/"sticker.svg") + output = tmp_path/"stickers.pdf" + + save_pdf( + [(sticker,placed)], + {id(sticker): artwork}, + output, + ) + + pixmap, _ = render_pdf(output) + + # PDF coordinates are measured from the bottom-left. + # Convert the intended point to top-left raster coordinates. + red = pixel_at(pixmap, 110, 105) + + assert red[0] > 200 + assert red[1] < 100 + assert red[2] < 100 + + +def test_artwork_is_not_drawn_at_original_position(tmp_path): + sticker = box(0, 0, 20, 10) + placed = box(100, 100, 120, 110) + artwork = make_svg(tmp_path / "sticker.svg") + output = tmp_path / "stickers.pdf" + + save_pdf( + [(sticker, placed)], + {id(sticker): artwork}, + output, + ) + + pixmap, _ = render_pdf(output) + + # This point is inside the page but outside the placed sticker. + red = pixel_at(pixmap, 10, 10) + + assert not (red[0] > 200 and red[1] < 100 and red[2] < 100) + + +def test_rotated_sticker_artwork_matches_placement(tmp_path): + sticker = box(0, 0, 20, 10) + placed = box(100, 100, 110, 120) + + artwork = tmp_path/"sticker.svg" + + artwork.write_text( + """ + + + """, + encoding="utf-8", + ) + + + + output = tmp_path / "stickers.pdf" + + save_pdf( + [(sticker, placed)], + {id(sticker): artwork}, + output, + ) + + pixmap, _ = render_pdf(output) + + # A point inside the rotated sticker should contain blue artwork. + blue = pixel_at(pixmap, 105, 115) + + assert blue[2] > 200 + assert blue[0] < 100 + assert blue[1] < 100 + + red = pixel_at(pixmap, 105, 105) + + assert red[0] > 200 + assert red[1] < 100 + assert red[2] < 100 + + +def test_original_svg_color_is_preserved(tmp_path): + sticker = box(0, 0, 20, 10) + artwork = make_svg( + tmp_path / "sticker.svg", + color="#00cc00", + ) + + output = tmp_path / "stickers.pdf" + + save_pdf( + [(sticker, sticker)], + {id(sticker): artwork}, + output, + ) + + pixmap, _ = render_pdf(output) + + green = pixel_at(pixmap, 10, 5) + + assert green[1] > 150 + assert green[0] < 100 + assert green[2] < 100 + + +def test_missing_artwork_path_raises_error(tmp_path): + sticker = box(0, 0, 20, 10) + placed = box(10, 10, 30, 20) + + output = tmp_path / "stickers.pdf" + + with pytest.raises(ValueError, match="No SVG artwork"): + save_pdf( + [(sticker, placed)], + {}, + output, + ) + + +def test_nonexistent_svg_file_raises_error(tmp_path): + sticker = box(0, 0, 20, 10) + placed = box(10, 10, 30, 20) + + output = tmp_path / "stickers.pdf" + + with pytest.raises(FileNotFoundError): + save_pdf( + [(sticker, placed)], + {id(sticker): tmp_path / "missing.svg"}, + output, + ) \ No newline at end of file From 52015afebdee561a436489d350a37a1ba2b6fd51 Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Thu, 12 Nov 2026 15:40:42 -0500 Subject: [PATCH 35/39] added new pdf tests, refactored save pdf to take database arguments --- .../packing-algorithm/app/pdf/output.py | 15 ++++++++--- .../tests/test_pdf_output.py | 27 ++++++++++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/sticker-business/packing-algorithm/app/pdf/output.py b/sticker-business/packing-algorithm/app/pdf/output.py index 7be9873..8df31a1 100644 --- a/sticker-business/packing-algorithm/app/pdf/output.py +++ b/sticker-business/packing-algorithm/app/pdf/output.py @@ -4,6 +4,7 @@ from reportlab.graphics import renderPDF from svglib.svglib import svg2rlg from shapely.affinity import rotate, translate +from app.models.sticker import Sticker PAGE_SIZE = (5.5*inch, 8.5*inch) @@ -34,12 +35,12 @@ def _get_rotation(original,placed): ) -def save_pdf(placements, sticker_paths, output): +def save_pdf(placements, sticker_lookup, output): """Export packed stickers as a full-color half-letter PDF. Args: placements: List of (original_geometry, placed_geometry) pairs. - sticker_paths: Dictionary mapping id(original_geometry) to SVG paths. + stickerlookup: Dictionary mapping id(original_geometry) to SVG paths. output: Destination PDF file path.""" output = Path(output) @@ -48,7 +49,15 @@ def save_pdf(placements, sticker_paths, output): pdf = canvas.Canvas(str(output), pagesize = PAGE_SIZE) for original, placed in placements: - svg_path = sticker_paths.get(id(original)) + sticker = sticker_lookup.get(id(original)) + + sticker_or_path = sticker_lookup.get(id(original)) + if isinstance(sticker_or_path, Sticker): + svg_path = sticker_or_path.svg_path + else: + svg_path = sticker_or_path + + if not svg_path: raise ValueError( diff --git a/sticker-business/packing-algorithm/tests/test_pdf_output.py b/sticker-business/packing-algorithm/tests/test_pdf_output.py index cf044c3..1feddc7 100644 --- a/sticker-business/packing-algorithm/tests/test_pdf_output.py +++ b/sticker-business/packing-algorithm/tests/test_pdf_output.py @@ -4,6 +4,7 @@ from pypdf import PdfReader from shapely.geometry import box import fitz +from app.models.sticker import Sticker def test_pdf_file_is_created(tmp_path): sticker = box(0,0,20,10) @@ -200,4 +201,28 @@ def test_nonexistent_svg_file_raises_error(tmp_path): [(sticker, placed)], {id(sticker): tmp_path / "missing.svg"}, output, - ) \ No newline at end of file + ) + +def test_pdf_uses_sticker_model_svg_path(tmp_path): + geometry = box(0,0,20,10) + placed = box(100,100,120,110) + + artwork = make_svg(tmp_path / "sticker.svg") + + sticker = Sticker( + id="test-001", + name="test Sticker", + size = 200, + description = "A test sticker", + svg_path = str(artwork), + ) + + output = tmp_path / "stickers.pdf" + + save_pdf( + [(geometry, placed)], + {id(geometry): sticker}, + output, + ) + assert output.exists() + assert output.stat().st_size >0 \ No newline at end of file From 0b0e14451bef8ce622a7627cbe485977e86a520f Mon Sep 17 00:00:00 2001 From: Oscar Rojas-Perez Date: Sat, 14 Nov 2026 15:43:04 -0500 Subject: [PATCH 36/39] feat(test): adding failing test to point out svg y-axis flip bug --- .../packing-algorithm/tests/test_svg_input.py | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/sticker-business/packing-algorithm/tests/test_svg_input.py b/sticker-business/packing-algorithm/tests/test_svg_input.py index b5f0196..a6dbd0a 100644 --- a/sticker-business/packing-algorithm/tests/test_svg_input.py +++ b/sticker-business/packing-algorithm/tests/test_svg_input.py @@ -1,5 +1,5 @@ from app.svg.loader import load_sticker_svg -from shapely.geometry import Polygon +from shapely.geometry import Point, Polygon from pathlib import Path import pytest @@ -75,4 +75,34 @@ def test_svg_loads_as_one_nonempty_sticker(): # sticker should just have exterior assert len(geometry.interiors) == 0 assert geometry.exterior.is_ring - assert geometry.is_simple \ No newline at end of file + assert geometry.is_simple + +def test_svg_y_axis_is_flipped_to_point_up(tmp_path): + """SVG y grows downward; the rest of the app treats y as growing upward. + + The L below has its foot at the BOTTOM of the drawing. After loading, + the foot must be at the bottom (low y) of the geometry. Otherwise the + HPGL cut line is a mirror image of the art printed in the PDF. + + This test case was written by Claude Code. + """ + svg = tmp_path / "l_shape.svg" + svg.write_text( + """ + + + + + """, + encoding="utf-8", + ) + + geometry = load_sticker_svg(svg) + + # The bottom-right of the drawing is filled (the foot)... + assert geometry.contains(Point(15, 5)) + # ...and the top-right of the drawing is empty. + assert not geometry.contains(Point(15, 15)) From 5f3bef53a536f7c57db5114efd8e43bada8d88e3 Mon Sep 17 00:00:00 2001 From: Marigold <111760235+LiterallyAFlower@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:19:56 -0500 Subject: [PATCH 37/39] Added and fixed several problems, focused on pr criteria from oscar --- .../packing-algorithm/app/models/sticker.py | 3 ++- .../packing-algorithm/app/svg/loader.py | 9 ++++++-- .../packing-algorithm/tests/test_svg_input.py | 21 ++++++++++--------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/sticker-business/packing-algorithm/app/models/sticker.py b/sticker-business/packing-algorithm/app/models/sticker.py index cf8ad70..0997178 100644 --- a/sticker-business/packing-algorithm/app/models/sticker.py +++ b/sticker-business/packing-algorithm/app/models/sticker.py @@ -6,7 +6,8 @@ class Sticker: """Catalog metadata and file refs for one sticker.""" id:str name:str - size:float + width_cm = float + height_cm: float description:str tags:list[str] = field(default_factory=list) category:str="" diff --git a/sticker-business/packing-algorithm/app/svg/loader.py b/sticker-business/packing-algorithm/app/svg/loader.py index 8c99ce7..529731c 100644 --- a/sticker-business/packing-algorithm/app/svg/loader.py +++ b/sticker-business/packing-algorithm/app/svg/loader.py @@ -1,7 +1,7 @@ from pathlib import Path import re import xml.etree.ElementTree as ET - +from shapely.affinity import scale from shapely.geometry import Polygon from shapely.ops import unary_union from svgpathtools import parse_path @@ -172,4 +172,9 @@ def load_sticker_svg( raise ValueError("no usable sticker geometry") # Return only the exterior silhouette, discarding interior rings. - return Polygon(geometry.exterior) + return scale( + Polygon(geometry.exterior), + xfact=1, + yfact=-1, + origin="center", + ) diff --git a/sticker-business/packing-algorithm/tests/test_svg_input.py b/sticker-business/packing-algorithm/tests/test_svg_input.py index a6dbd0a..ea4b6f0 100644 --- a/sticker-business/packing-algorithm/tests/test_svg_input.py +++ b/sticker-business/packing-algorithm/tests/test_svg_input.py @@ -5,16 +5,17 @@ FIXTURES = Path(__file__).parent / "fixtures" -def test_svg_loads_as_one_nonempty_sticker(): - geometry = load_sticker_svg( - FIXTURES/"tetrispieceI.svg" - ) - - assert not geometry.is_empty - assert geometry.is_valid - assert geometry.geom_type == "Polygon" - assert geometry.area > 0 - assert len(geometry.interiors) == 0 +# Duplicated test, oops +# def test_svg_loads_as_one_nonempty_sticker(): +# geometry = load_sticker_svg( +# FIXTURES/"tetrispieceI.svg" +# ) + +# assert not geometry.is_empty +# assert geometry.is_valid +# assert geometry.geom_type == "Polygon" +# assert geometry.area > 0 +# assert len(geometry.interiors) == 0 def test_multiple_touching_blocks_form_one_outline(tmp_path): svg = tmp_path / "two_blocks.svg" From 0c96da603b827a0823dd59c7bf379a5e28ec9a31 Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Tue, 17 Nov 2026 14:16:08 -0500 Subject: [PATCH 38/39] tests pass, moving on to revisiting the algorithm to fit it well in frontend. --- .../packing-algorithm/app/models/sticker.py | 2 +- .../packing-algorithm/app/svg/loader.py | 63 ++++++++++- .../tests/test_pdf_output.py | 3 +- .../tests/test_sticker_model.py | 12 +- .../packing-algorithm/tests/test_svg_input.py | 105 ++++++++++++++++++ 5 files changed, 178 insertions(+), 7 deletions(-) diff --git a/sticker-business/packing-algorithm/app/models/sticker.py b/sticker-business/packing-algorithm/app/models/sticker.py index 0997178..c7e057a 100644 --- a/sticker-business/packing-algorithm/app/models/sticker.py +++ b/sticker-business/packing-algorithm/app/models/sticker.py @@ -6,7 +6,7 @@ class Sticker: """Catalog metadata and file refs for one sticker.""" id:str name:str - width_cm = float + width_cm: float height_cm: float description:str tags:list[str] = field(default_factory=list) diff --git a/sticker-business/packing-algorithm/app/svg/loader.py b/sticker-business/packing-algorithm/app/svg/loader.py index 529731c..0ff9693 100644 --- a/sticker-business/packing-algorithm/app/svg/loader.py +++ b/sticker-business/packing-algorithm/app/svg/loader.py @@ -5,6 +5,7 @@ from shapely.geometry import Polygon from shapely.ops import unary_union from svgpathtools import parse_path +import math def _local_tag(element): """Return an SVG element's tag without the XML namespace, for testing/parsing.""" @@ -115,6 +116,8 @@ def load_sticker_svg( path: str | Path, *, merge_tolerance: float = 0.0, + width_cm: float | None = None, + height_cm: float | None = None, ): """Load one sticker SVG and return its exterior silhouette. @@ -172,9 +175,67 @@ def load_sticker_svg( raise ValueError("no usable sticker geometry") # Return only the exterior silhouette, discarding interior rings. - return scale( + geometry = scale( Polygon(geometry.exterior), xfact=1, yfact=-1, origin="center", ) + # physical sizing + if(width_cm is None) != (height_cm is None): + raise ValueError( + "Both width_cm and height_cm must be provided" + ) + if(width_cm is not None): + if ( + not math.isfinite(width_cm) + or not math.isfinite(height_cm) + or width_cm <= 0 + or height_cm <= 0 + ): + raise ValueError( + "Sticker dimensions must be positive and finite" + ) + + min_x, min_y, max_x, max_y = geometry.bounds + original_width = max_x - min_x + original_height = max_y - min_y + + if original_width <= 0 or original_height <= 0: + raise ValueError("Sticker geometry has invalid dimensions.") + + scale_factor = width_cm / original_width + calculated_height = original_height * scale_factor + + if not math.isclose( + calculated_height, + height_cm, + rel_tol=1e-6, + abs_tol=1e-9, + ): + raise ValueError( + "Requested dimensions do not match the SVG aspect ratio." + ) + + geometry = scale( + geometry, + xfact=scale_factor, + yfact=scale_factor, + origin="center", + ) + + return geometry + + +def load_sticker_geometry(sticker, *, merge_tolerance=0.0): + """Load a sticker's SVG using its database dimensions in cm""" + if not sticker.svg_path: + raise ValueError("Sticker does not have an SVG path.") + + + return load_sticker_svg( + sticker.svg_path, + merge_tolerance=merge_tolerance, + width_cm = sticker.width_cm, + height_cm = sticker.height_cm + ) \ No newline at end of file diff --git a/sticker-business/packing-algorithm/tests/test_pdf_output.py b/sticker-business/packing-algorithm/tests/test_pdf_output.py index 1feddc7..31e274d 100644 --- a/sticker-business/packing-algorithm/tests/test_pdf_output.py +++ b/sticker-business/packing-algorithm/tests/test_pdf_output.py @@ -212,7 +212,8 @@ def test_pdf_uses_sticker_model_svg_path(tmp_path): sticker = Sticker( id="test-001", name="test Sticker", - size = 200, + width_cm=2.0, + height_cm=1.0, description = "A test sticker", svg_path = str(artwork), ) diff --git a/sticker-business/packing-algorithm/tests/test_sticker_model.py b/sticker-business/packing-algorithm/tests/test_sticker_model.py index d6608ae..4f45987 100644 --- a/sticker-business/packing-algorithm/tests/test_sticker_model.py +++ b/sticker-business/packing-algorithm/tests/test_sticker_model.py @@ -5,7 +5,8 @@ def test_sticker_stores_catalog_properties(): sticker = Sticker( id="tetris-i", name="Tetris I Block", - size=100.0, + width_cm=100, + height_cm=100, description="A straight Tetris-style sticker.", tags=["tetris","block","geometric"], category = "Tetris Blocks", @@ -15,7 +16,8 @@ def test_sticker_stores_catalog_properties(): assert sticker.id == "tetris-i" assert sticker.name == "Tetris I Block" - assert sticker.size==100.0 + assert sticker.width_cm==100 + assert sticker.height_cm==100 assert sticker.description=="A straight Tetris-style sticker." assert sticker.tags==["tetris","block","geometric"] assert sticker.category == "Tetris Blocks" @@ -26,7 +28,8 @@ def test_stickers_can_have_independent_tag_lists(): first = Sticker( id="sticker-a", name="Sticker A", - size = 10.0, + width_cm=5, + height_cm=2, description = "First sticker.", tags = ["test"], category = "Tests", @@ -37,7 +40,8 @@ def test_stickers_can_have_independent_tag_lists(): second = Sticker( id="sticker-b", name="Sticker B", - size = 20.0, + height_cm=10, + width_cm=20, description = "Second sticker.", tags = ["example"], category = "Examples", diff --git a/sticker-business/packing-algorithm/tests/test_svg_input.py b/sticker-business/packing-algorithm/tests/test_svg_input.py index ea4b6f0..28c0539 100644 --- a/sticker-business/packing-algorithm/tests/test_svg_input.py +++ b/sticker-business/packing-algorithm/tests/test_svg_input.py @@ -1,4 +1,6 @@ from app.svg.loader import load_sticker_svg +from app.svg.loader import load_sticker_geometry +from app.models.sticker import Sticker from shapely.geometry import Point, Polygon from pathlib import Path import pytest @@ -107,3 +109,106 @@ def test_svg_y_axis_is_flipped_to_point_up(tmp_path): assert geometry.contains(Point(15, 5)) # ...and the top-right of the drawing is empty. assert not geometry.contains(Point(15, 15)) + +def test_svg_scales_to_database_dimensions_in_cm(tmp_path): + svg = tmp_path / "sized_sticker.svg" + svg.write_text( + """ + + + + """, + encoding="utf-8", + ) + + geometry = load_sticker_svg( + svg, + width_cm=4.0, + height_cm=2.0, + ) + min_x, min_y, max_x, max_y = geometry.bounds + + assert max_x - min_x == pytest.approx(4.0) + assert max_y - min_y == pytest.approx(2.0) + assert geometry.area == pytest.approx(8.0) + + +def test_svg_scaling_preserves_aspect_ratio(tmp_path): + svg = tmp_path / "sized_sticker.svg" + svg.write_text( + """ + + + + """, + encoding="utf-8", + ) + + geometry = load_sticker_svg( + svg, + width_cm=6.0, + height_cm=3.0, + ) + + min_x, min_y, max_x, max_y = geometry.bounds + + assert (max_x - min_x) / (max_y - min_y) == pytest.approx(2.0) + + +def test_svg_rejects_dimensions_that_would_distort_aspect_ratio(tmp_path): + svg = tmp_path / "sized_sticker.svg" + svg.write_text( + """ + + + + """, + encoding="utf-8", + ) + + with pytest.raises(ValueError, match="aspect ratio"): + load_sticker_svg( + svg, + width_cm=4.0, + height_cm=3.0, + ) + +def test_load_sticker_geometry_uses_database_dimensions(tmp_path): + svg = tmp_path / "sticker.svg" + svg.write_text( + """ + + + + """, + encoding="utf-8", + ) + + sticker = Sticker( + id="test-001", + name="Test Sticker", + width_cm=4.0, + height_cm=2.0, + description="Test sticker", + svg_path=str(svg), + ) + + geometry = load_sticker_geometry(sticker) + + min_x, min_y, max_x, max_y = geometry.bounds + + assert max_x - min_x == pytest.approx(4.0) + assert max_y - min_y == pytest.approx(2.0) + + +def test_load_sticker_geometry_rejects_missing_svg_path(): + sticker = Sticker( + id="test-002", + name="Missing SVG", + width_cm=4.0, + height_cm=2.0, + description="Test sticker", + ) + + with pytest.raises(ValueError, match="SVG path"): + load_sticker_geometry(sticker) \ No newline at end of file From 6daf6451d56badeed9a1c2ff80c96e5baf74967a Mon Sep 17 00:00:00 2001 From: LiterallyAFlower Date: Tue, 17 Nov 2026 14:53:15 -0500 Subject: [PATCH 39/39] Added a results class for the algorithm: can show if stickers are not placed. --- .../packing-algorithm/app/packing/results.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 sticker-business/packing-algorithm/app/packing/results.py diff --git a/sticker-business/packing-algorithm/app/packing/results.py b/sticker-business/packing-algorithm/app/packing/results.py new file mode 100644 index 0000000..9306e19 --- /dev/null +++ b/sticker-business/packing-algorithm/app/packing/results.py @@ -0,0 +1,24 @@ +from dataclasses import dataclass, field + +@dataclass +class UnplacedSticker: + sticker: object + reason: str + message: str + +@dataclass +class PackingResult: + placements: list = field(default_factory=list) + unplaced: list[UnplacedSticker] = field(default_factory=list) + + @property + def requested(self): + return len(self.placements) + len(self.unplaced) + + @property + def placed(self): + return len(self.placements) + + @property + def success(self): + return len(self.unplaced) == 0 \ No newline at end of file