]> git.donarmstrong.com Git - lilypond.git/blob - lily/gourlay-breaking.cc
release: 1.1.58
[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 "colhpos.hh"
11 #include "spring-spacer.hh"
12 #include "debug.hh"
13 #include "paper-column.hh"
14 #include "paper-score.hh"
15 #include "paper-def.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   int line_i_;
32   Real energy_f_;
33   Column_x_positions line_config_;
34   Break_node () 
35   {
36     prev_break_i_ = -1;
37     line_i_ = 0;
38   }
39 };
40
41 /**
42   This algorithms is adapted from the OSU Tech report on breaking lines.
43   
44  */
45
46 Array<Column_x_positions>
47 Gourlay_breaking::do_solve () const
48 {
49   Array<Break_node> optimal_paths;
50   Line_of_cols all = pscore_l_->col_l_arr_ ;
51   Array<int> breaks = find_break_indices ();
52   
53   optimal_paths.set_size (breaks.size ());
54
55   Break_node first_node ;
56   first_node.prev_break_i_ = -1;
57   first_node.line_config_.energy_f_ = 0;
58   first_node.line_i_ = 0;
59   
60   optimal_paths[0] = first_node; 
61   int break_idx=1;
62
63   
64   for (; break_idx< breaks.size (); break_idx++) 
65     {
66       Array<int> candidates;
67       Array<Column_x_positions> candidate_lines;
68       Cons_list<Line_spacer> spacer_p_list;
69         
70       /*
71         start with a short line, add measures. At some point 
72         the line becomes infeasible. Then we don't try to add more 
73         */
74       for (int start_idx = break_idx; start_idx--;)
75         {
76           if  (break_idx - start_idx > max_measures_i_) 
77             break;
78
79           if (optimal_paths[start_idx].prev_break_i_ < 0
80               && optimal_paths[start_idx].line_config_.energy_f_)
81                 
82             continue;
83             
84           Line_of_cols line = all.slice (breaks[start_idx], breaks[break_idx]+1);
85   
86           line[0] = dynamic_cast<Paper_column*>(line[0]->find_prebroken_piece (RIGHT));
87           line.top () =  dynamic_cast<Paper_column*>(line.top ()->find_prebroken_piece (LEFT));
88             
89           if (!feasible (line))
90             break;
91             
92           Column_x_positions approx;
93           approx.cols = line;
94             
95           approx.spacer_l_ = generate_spacing_problem (line, 
96             pscore_l_->paper_l_->line_dimensions_int (optimal_paths[start_idx].line_i_));
97           spacer_p_list.append (new Killing_cons<Line_spacer> (approx.spacer_l_,0));
98
99           ( (Break_algorithm*)this)->approx_stats_.add (approx.cols);
100           approx.approximate_solve_line ();
101             
102           if  (approx.energy_f_  > energy_bound_f_)
103             {
104               continue;
105             }
106
107             
108           // this is a likely candidate. Store it.
109           candidate_lines.push (approx);
110           candidates.push (start_idx);
111         }
112
113             
114       int minimal_j = -1;
115       Real minimal_energy = infinity_f;
116       for (int j=0; j < candidates.size (); j++) 
117         {
118           int start = candidates[j];
119           if (optimal_paths[start].line_config_.energy_f_
120                + candidate_lines[j].energy_f_  > minimal_energy)
121                 
122             continue;
123
124           if (!candidate_lines[j].satisfies_constraints_b_) 
125             {
126               candidate_lines[j].solve_line ();
127               ( (Break_algorithm*)this)->exact_stats_.add (candidate_lines[j].cols);
128             }
129             
130           Real this_energy 
131             = optimal_paths[start].line_config_.energy_f_ 
132             + candidate_lines[j].energy_f_ ;
133             
134           if (this_energy < minimal_energy) 
135             {
136               minimal_j = j;
137               minimal_energy = this_energy;
138             }
139         }
140
141       if (minimal_j < 0) 
142         {
143           optimal_paths[break_idx].prev_break_i_ = -1;
144           optimal_paths[break_idx].line_config_.energy_f_ = infinity_f;
145         }
146       else 
147         {
148           optimal_paths[break_idx].prev_break_i_ = candidates[minimal_j];
149           optimal_paths[break_idx].line_config_ = candidate_lines[minimal_j];
150           optimal_paths[break_idx].line_i_ = 
151             optimal_paths[optimal_paths[break_idx].prev_break_i_].line_i_ + 1;
152         }
153
154       if (! (break_idx % HAPPY_DOTS_I))
155         *mlog << "[" << break_idx << "]" << flush;
156
157       spacer_p_list.junk ();
158     }
159
160   if  (break_idx % HAPPY_DOTS_I) 
161     *mlog << "[" << break_idx << "]" << flush;
162
163   Array<int> final_breaks;
164
165   Array<Column_x_positions> lines;
166
167   /* skip 0-th element, since it is a "dummy" elt*/
168   for (int i = optimal_paths.size ()-1; i> 0;) 
169     {
170       final_breaks.push (i);
171       assert (i > optimal_paths[i].prev_break_i_);
172
173       // there was no "feasible path"
174       if (!optimal_paths[i].line_config_.config.size ()) {
175         final_breaks.set_size (0);
176         break;
177       }
178       i = optimal_paths[i].prev_break_i_;
179     }
180
181
182   for (int i= final_breaks.size (); i--;) 
183     lines.push (optimal_paths[final_breaks[i]].line_config_);
184   
185   return lines;
186 }
187
188
189 Gourlay_breaking::Gourlay_breaking ()
190 {
191   get_line_spacer = Spring_spacer::constructor;
192   energy_bound_f_ = infinity_f;
193   max_measures_i_ = INT_MAX;
194 }
195
196 void
197 Gourlay_breaking::do_set_pscore ()
198 {
199   energy_bound_f_ = pscore_l_->paper_l_->get_var ("gourlay_energybound");
200   max_measures_i_ =int (rint (pscore_l_->paper_l_->get_var ("gourlay_maxmeasures")));
201 }
202