]> git.donarmstrong.com Git - lilypond.git/blobdiff - linespace.cc
release: 0.0.3
[lilypond.git] / linespace.cc
index 7892cefa3da1ee1599a34284bb71781e3d231299..402bd4074ebe16afb1d75f6ee5945366b2fc4804 100644 (file)
@@ -27,6 +27,7 @@ Spacing_problem::col_id(const PCol *w)const
 void
 Spacing_problem::OK() const
 {
+#ifndef NDEBUG
     Union_find connected(cols.sz());
 
     for (int i=0; i < ideals.sz(); i++) {
@@ -39,6 +40,7 @@ Spacing_problem::OK() const
     for (int i = 0; i < cols.sz(); i++) {
        assert( connected.equiv(0,i));
     }
+#endif    
 }
 
 bool
@@ -118,7 +120,7 @@ Spacing_problem::find_initial_solution() const
 }
 // generate the matrices
 void
-Spacing_problem::make_matrices(Matrix &quad, Vector &lin) const
+Spacing_problem::make_matrices(Matrix &quad, Vector &lin, Real &c) const
 {
     quad.fill(0);
     lin.fill(0);
@@ -134,6 +136,8 @@ Spacing_problem::make_matrices(Matrix &quad, Vector &lin) const
 
        lin(r) -= i->space*i->hooke;
        lin(l) += i->space*i->hooke;
+
+       c += sqr(i->space);
     }
 }
 
@@ -141,29 +145,20 @@ Spacing_problem::make_matrices(Matrix &quad, Vector &lin) const
 void
 Spacing_problem::make_constraints(Optimisation_problem& lp) const
 {    
-    for (int j=0; j < cols.sz(); j++) {
+    int dim=cols.sz();
+    for (int j=0; j < dim; j++) {
        Colinfo *c=&(cols[j]);
-       int dim=cols.sz();
-               
        if (c->fixed) {
-           lp.add_fixed_var(j,c->fixpos);
-           continue;
-       }else {
-
+           lp.add_fixed_var(j,c->fixpos);          
+       }
+       if (j > 0){
            Vector c1(dim);
-           Vector c2(dim);
            
               
            c1(j)=1.0 ;
            c1(j-1)=-1.0 ;
            lp.add_inequality_cons(c1, cols[j-1].minright() +
                                   cols[j].minleft());
-
-           c2(j)=-1.0 ;
-           c2(j+1)=1.0;
-           lp.add_inequality_cons(c2,
-                                  cols[j+1].minleft() +
-                                  cols[j].minright());
        }
     }
 }
@@ -171,18 +166,19 @@ Spacing_problem::make_constraints(Optimisation_problem& lp) const
 svec<Real>
 Spacing_problem::solve() const
 {
+    print();
     OK();
     assert(check_feasible());
-    //    print();
+
     
     /* optimalisatiefunctie */        
     Optimisation_problem lp(cols.sz());
-    make_matrices(lp.quad,lp.lin);
+    make_matrices(lp.quad,lp.lin, lp.const_term);
     make_constraints(lp);    
     Vector start=find_initial_solution();    
     Vector sol(lp.solve(start));
     if (!check_constraints(sol)) {
-       error( "solution doesn't solve. Sorry");        
+       WARN << "solution doesn't satisfy constraints.\n" ;
     }
        
 
@@ -219,16 +215,19 @@ Spacing_problem::add_ideal(const Idealspacing *i)
 void
 Spacing_problem::print_ideal(const Idealspacing*id)const
 {
+#ifndef NPRINT
     int l = col_id(id->left);
     int r = col_id(id->right);
 
     mtor << "idealspacing { between " << l <<","<<r<<'\n';
     mtor << "distance "<<id->space<< " strength " << id->hooke << "}\n";
+#endif
 }
 
 void
 Spacing_problem::print() const
 {
+    #ifndef NPRINT
     for (int i=0; i < cols.sz(); i++) {
        mtor << "col " << i<<' ';
        cols[i].print();
@@ -236,14 +235,20 @@ Spacing_problem::print() const
     for (int i=0; i < ideals.sz(); i++) {
        print_ideal(ideals[i]);
     }
+    #endif
+    
 }
 
 void
 Colinfo::print() const
 {
+#ifndef NPRINT
     mtor << "column { ";
     if (fixed)
        mtor << "fixed at " << fixpos<<", ";
     mtor << "[" << minleft() << ", " << minright() << "]";
     mtor <<"}\n";
+#endif
 }
+
+