hlfw.ca

binpack

Download patch

ref: b294da37854705e27144afc0653030ff31d72525
parent: b6af1e9a2161df2ca656471983274ac632539095
author: Halfwit <michaelmisch1985@gmail.com>
date: Mon Nov 7 14:10:16 PST 2016

Dropping to seperate function for scrubbing out bins

--- a/binpack.c
+++ b/binpack.c
@@ -60,19 +60,6 @@
 
 	for (unsigned i = 1; i < *bin_count; i++) {
 		for (unsigned j = 0; j < *bin_count - i; j++) {
-			/* If two bins can be logically made in to one bin, do so */
-			if (b[j + 1].x == b[j].x + b[j].w && b[j + 1].y == b[j].y && b[j].h == b[j].h) {
-				b[j].w += b[j + 1].w;
-				b[j + 1].h = 0;
-				b[j + 1].w = 0;
-				continue;
-			}
-			if (b[j + 1].y == b[j].y + b[j].h && b[j + 1].x == b[j].x && b[j + 1].w == b[j].w) {
-				b[j].h += b[j + 1].h;
-				b[j + 1].h = 0;
-				b[j + 1].w = 0;
-				continue;
-			}
 			if ((b[j + 1].w * b[j + 1].h) > (b[j].w * b[j].h)) {
 				temp = b[j];
 				b[j] = b[j + 1];
@@ -82,6 +69,27 @@
 	}
 }
 
+void
+scrub_bins(struct bins b[], size_t *bin_count) {
+    
+    for (unsigned i = 1; i < *bin_count; i++) {
+		if(b[i].w == 0 || b[i].h == 0)
+				continue;
+		for (unsigned j = 0; j < *bin_count -i; j++) {
+
+				if (b[j].w == 0 || b[j].h == 0 || i == j)
+						continue;
+				
+				if (b[j].y == b[i].y && b[j].h == b[i].h) {
+						b[i].h += b[j].h;
+						b[j].h = 0;
+						b[j].w = 0;
+						continue;
+				}
+		}
+    }
+}
+
 /* arrange rectangles largest to smallest */
 void
 sort_input(struct input r[], const size_t length)
@@ -117,7 +125,7 @@
 		bin[j].h -= (out[i].h - mb.gaps);
 		*bin_count += 1;
 	}
-
+	
 	/* rect same height */
 	else if (out[i].h + mb.gaps < h) {
 		bin[j].y += (out[i].h + mb.gaps);
@@ -169,6 +177,7 @@
 				save_rect(bin, out, r, i, j, mb);
 				create_bins(bin, out, i, j, &bin_count, mb);
 				sort_bins(bin, &bin_count);
+				scrub_bins(bin, &bin_count);
 				break;
 			}
 		}