]> git.donarmstrong.com Git - lilypond.git/blob - lily/gourlay-breaking.cc
release: 1.3.0
[lilypond.git] / lily / gourlay-breaking.cc
1 /*
2   gourlay-breaking.cc -- implement Gourlay_breaking
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "gourlay-breaking.hh"
10 #include "column-x-positions.hh"
11 #include "debug.hh"
12 #include "paper-column.hh"
13 #include "paper-score.hh"
14 #include "paper-def.hh"
15 #include "simple-spacer.hh"
16
17 #include "killing-cons.tcc"
18
19 /// How often to print operator pacification marks?
20 const int HAPPY_DOTS_I = 3;
21
22 /**
23   Helper to trace back an optimal path 
24  */
25 struct Break_node {
26   /** this was the previous. If negative, this break should not be
27     considered: this path has infinite energy
28     
29     */
30   int prev_break_i_;
31   /**
32      Which system number so far?
33    */
34   int line_i_;
35
36   Real demerits_f_;
37   Column_x_positions line_config_;
38   
39   Break_node () 
40   {
41     prev_break_i_ = -1;
42     line_i_ = 0;
43     demerits_f_ = 0;
44   }
45 };
46
47 /**
48   This algorithms is adapted from the OSU Tech report on breaking lines.
49  */
50 Array<Column_x_positions>
51 Gourlay_breaking::do_solve () const
52 {
53   Array<Break_node> optimal_paths;
54   Line_of_cols all = pscore_l_->col_l_arr_ ;
55   Array<int> breaks = find_break_indices ();
56   
57   optimal_paths.set_size (breaks.size ());
58
59   Break_node first_node ;
60   first_node.line_config_.energy_f_ = 0;  
61   
62   optimal_paths[0] = first_node; 
63   int break_idx=1;
64
65   for (; break_idx< breaks.size (); break_idx++) 
66     {
67       /*
68         start with a short line, add measures. At some point 
69         the line becomes infeasible. Then we don't try to add more 
70         */
71       int minimal_start_idx = -1;
72       Column_x_positions minimal_sol;
73       Column_x_positions backup_sol;
74       
75       Real minimal_demerits = infinity_f;
76
77       for (int start_idx = break_idx; start_idx--;)
78         {
79           Line_of_cols line = all.slice (breaks[start_idx], breaks[break_idx]+1);
80   
81           line[0] = dynamic_cast<Paper_column*>(line[0]->find_broken_piece (RIGHT));
82           line.top () =  dynamic_cast<Paper_column*>(line.top ()->find_broken_piece (LEFT));
83             
84           Column_x_positions cp;
85           cp.cols_ = line;
86
87           Interval line_dims
88             = pscore_l_->paper_l_->line_dimensions_int (optimal_paths[start_idx].line_i_);
89           Simple_spacer * sp = generate_spacing_problem (line, line_dims);
90           sp->solve (&cp);
91           delete sp;
92
93           if (start_idx == break_idx - 1)
94             backup_sol = cp;    // in case everything fucks up
95           if (!cp.satisfies_constraints_b_)
96             break;
97
98           
99           Real this_demerits;
100           if (optimal_paths[start_idx].demerits_f_ >= infinity_f)
101             this_demerits = infinity_f;
102           else
103             this_demerits = combine_demerits (optimal_paths[start_idx].line_config_, cp)
104               + optimal_paths[start_idx].demerits_f_;
105
106           if (this_demerits < minimal_demerits) 
107             {
108               minimal_start_idx = start_idx;
109               minimal_sol = cp;
110               minimal_demerits = this_demerits;
111             }
112         }
113
114       int prev =break_idx - 1;
115       if (minimal_start_idx < 0) 
116         {
117           optimal_paths[break_idx].demerits_f_ = infinity_f;
118           optimal_paths[break_idx].line_config_ = backup_sol;     
119         }
120       else 
121         {
122           prev = minimal_start_idx;
123           optimal_paths[break_idx].line_config_ = minimal_sol;
124           optimal_paths[break_idx].demerits_f_ = minimal_demerits;
125         }
126       optimal_paths[break_idx].prev_break_i_ = prev;
127       optimal_paths[break_idx].line_i_ = optimal_paths[prev].line_i_ + 1;
128
129       if (! (break_idx % HAPPY_DOTS_I))
130         *mlog << "[" << break_idx << "]" << flush;
131     }
132
133   /* do the last one */
134   if  (break_idx % HAPPY_DOTS_I) 
135     *mlog << "[" << break_idx << "]";
136
137   *mlog << endl;
138
139   Array<int> final_breaks;
140   Array<Column_x_positions> lines;
141
142   /* skip 0-th element, since it is a "dummy" elt*/
143   for (int i = optimal_paths.size ()-1; i> 0;) 
144     {
145       final_breaks.push (i);
146       int prev = optimal_paths[i].prev_break_i_;
147       assert (i > prev);
148       i = prev;
149     }
150
151   if (optimal_paths.top ().demerits_f_ >= infinity_f)
152     warning (_ ("No feasible line breaking found"));
153   
154   for (int i= final_breaks.size (); i--;) 
155     lines.push (optimal_paths[final_breaks[i]].line_config_);
156   
157   return lines;
158 }
159
160
161 Gourlay_breaking::Gourlay_breaking ()
162 {
163   energy_bound_f_ = infinity_f;
164   max_measures_i_ = INT_MAX;
165 }
166
167 void
168 Gourlay_breaking::do_set_pscore ()
169 {
170   max_measures_i_ =int (rint (pscore_l_->paper_l_->get_var ("gourlay_maxmeasures")));
171 }
172
173
174 /*
175   TODO: uniformity parameter to control rel. importance of spacing differences.
176  */
177 Real
178 Gourlay_breaking::combine_demerits (Column_x_positions const &prev,
179                                     Column_x_positions const &this_one) const
180 {
181   Real break_penalties = 0.0;
182   Paper_column * pc = this_one.cols_.top ();
183   if (pc->original_l_)
184     {
185       SCM pen = pc->get_elt_property (penalty_scm_sym);
186       if (pen != SCM_BOOL_F)
187         {
188           break_penalties += gh_scm2double (SCM_CDR(pen));
189         }
190     }
191
192   return abs (this_one.force_f_) + abs (prev.force_f_ - this_one.force_f_)
193     + break_penalties;
194 }