fixed bug 519
[org.ibex.core.git] / src / org / ibex / Box.java
index 8c0d3ef..3c3b868 100644 (file)
@@ -294,57 +294,60 @@ public final class Box extends JSScope implements Scheduler.Task {
     }
 
     private static float[] coeff = null;
+    private static LinearProgramming.Simplex lp_h = new LinearProgramming.Simplex(50, 50, 300);
+    private static LinearProgramming.Simplex lp_v = new LinearProgramming.Simplex(50, 50, 300);
+
     void place_children() {
-        LinearProgramming.Problem lpr_h;
-        LinearProgramming.Problem lpr_v;
         int numkids = 0; for(Box c = firstPackedChild(); c != null; c = c.nextPackedSibling()) numkids++;
         //#repeat col/row colspan/rowspan contentwidth/contentheight width/height \
-        //        maxwidth/maxheight cols/rows minwidth/minheight lp_h/lp_v lpr_h/lpr_v
+        //        maxwidth/maxheight cols/rows minwidth/minheight lp_h/lp_v lp_h/lp_v
         do {
             int nc = numkids * 2 + cols * 3 + 1 + 2;
             if (coeff == null || nc+1>coeff.length) coeff = new float[nc+1];
-            LinearProgramming.Simplex lp_h = new LinearProgramming.Simplex();
-            lpr_h = new LinearProgramming.Problem(nc, nc);
+            lp_h.init(nc);
 
             // objective function
             for(int i=0; i<coeff.length; i++) coeff[i] = (float)0.0;
-            coeff[cols*2+numkids] = (float)-100000.0;                             // priority 1: sum of columns equals parent
-            for(int i=cols*2; i<cols*2+numkids; i++) coeff[i] = (float)-1000.0;    // priority 2: honor maxwidths
-            for(int i=cols; i<cols*2; i++) coeff[i] = (float)(-1.0);              // priority 3: equalize columns
-            lp_h.set_obj_fn(lpr_h, coeff);
-            lp_h.set_maxim(lpr_h);
+            coeff[cols*2+numkids] = (float)10000.0;                              // priority 1: sum of columns equals parent
+            for(int i=cols*2; i<cols*2+numkids; i++) coeff[i] = (float)100.0;    // priority 2: honor maxwidths
+            for(int i=cols; i<cols*2; i++) coeff[i] = (float)(1.0);              // priority 3: equalize columns
+            lp_h.setObjective(coeff, false);
 
             // priority 1: sum of columns at least as big as parent
             for(int i=0; i<coeff.length; i++) coeff[i] = (i<cols) ? (float)1.0 : (float)0.0;
-            lp_h.add_constraint(lpr_h, coeff, LinearProgramming.GE, (float)width);
+            lp_h.add_constraint(coeff, LinearProgramming.GE, (float)width);
             for(int i=0; i<coeff.length; i++) coeff[i] = (i<cols) ? (float)1.0 : (float)0.0;
             coeff[cols*2+numkids] = (float)1.0;
-            lp_h.add_constraint(lpr_h, coeff, LinearProgramming.EQ, (float)width);
+            lp_h.add_constraint(coeff, LinearProgramming.EQ, (float)width);
 
             // priority 2: honor maxwidths
             int childnum = 0;
             for(Box child = firstPackedChild(); child != null; child = child.nextPackedSibling()) {
                 for(int i=0; i<coeff.length; i++)
                     coeff[i] = (i>=child.col && i<min(child.colspan+child.col, cols)) ? (float)1.0 : (float)0.0;
-                lp_h.add_constraint(lpr_h, coeff, LinearProgramming.GE, (float)child.contentwidth);
+                lp_h.add_constraint(coeff, LinearProgramming.GE, (float)child.contentwidth);
                 if (child.maxwidth < Integer.MAX_VALUE) {
                     for(int i=0; i<coeff.length; i++)
                         coeff[i] = (i>=child.col && i<min(child.colspan+child.col, cols)) ? (float)1.0 : (float)0.0;
                     coeff[cols*2+childnum] = (float)-1.0;
-                    lp_h.add_constraint(lpr_h, coeff, LinearProgramming.EQ, (float)child.maxwidth);
+                    lp_h.add_constraint(coeff, LinearProgramming.EQ, (float)child.maxwidth);
                 }
                 for(int j=0; j<coeff.length; j++) coeff[j] = (float)0.0;
                 childnum++;
             }
 
             // priority 3: equalize columns
+            for(int i=0 ; i<cols; i++) lp_h.set_lowbo(i+1, (float)0.0);
             for(int i=0 ; i<cols; i++) {
-                lp_h.set_lowbo(lpr_h, i+1, (float)0.0);
-                lp_h.bound_difference(lpr_h, i, cols+i, ((float)width)/((float)cols), LinearProgramming.LE, coeff);
-                lp_h.bound_sum(       lpr_h, i, cols+i, ((float)width)/((float)cols), LinearProgramming.GE, coeff);
+                for(int j=0 ; j<i; j++) {
+                    for(int k=0; k<coeff.length; k++) coeff[k] = (float)(k==i?1.0:k==j?-1.0:k==(cols+1)?-1.0:0.0);
+                    lp_h.add_constraint(coeff, LinearProgramming.LE, 0);
+                    for(int k=0; k<coeff.length; k++) coeff[k] = (float)(k==i?1.0:k==j?-1.0:k==(cols+1)?1.0:0.0);
+                    lp_h.add_constraint(coeff, LinearProgramming.GE, 0);
+                }
             }
-
-            lp_h.solve(lpr_h);
+            for(int i=0; i<coeff.length; i++) coeff[i] = (float)0.0;
+            lp_h.solve();
         } while(false);
         //#end
         
@@ -364,11 +367,11 @@ public final class Box extends JSScope implements Scheduler.Task {
                 int diff;
                 //#repeat col/row colspan/rowspan contentwidth/contentheight width/height colMaxWidth/rowMaxHeight \
                 //        child_x/child_y x/y HSHRINK/VSHRINK maxwidth/maxheight cols/rows minwidth/minheight x_slack/y_slack \
-                //        child_width/child_height ALIGN_RIGHT/ALIGN_BOTTOM ALIGN_LEFT/ALIGN_TOP lpr_h/lpr_v
+                //        child_width/child_height ALIGN_RIGHT/ALIGN_BOTTOM ALIGN_LEFT/ALIGN_TOP lp_h/lp_v
                 child_width = 0;
                 child_x = 0;
-                for(int i=0; i < child.col; i++) child_x += Math.round(lpr_h.solution[lpr_h.rows+i+1]);
-                for(int i = child.col; i<child.col + child.colspan; i++) child_width += Math.round(lpr_h.solution[lpr_h.rows+i+1]);
+                for(int i=0; i < child.col; i++) child_x += Math.round(lp_h.solution[lp_h.rows+i+1]);
+                for(int i = child.col; i<child.col + child.colspan; i++) child_width += Math.round(lp_h.solution[lp_h.rows+i+1]);
                 diff = (child_width - min(child_width, child.test(HSHRINK) ? child.contentwidth : child.maxwidth));
                 child_x += (child.test(ALIGN_RIGHT) ? diff : child.test(ALIGN_LEFT) ? 0 : diff / 2);
                 child_width = min(child_width, child.test(HSHRINK) ? child.contentwidth : child.maxwidth);