]> git.donarmstrong.com Git - lilypond.git/blob - lily/gourlay-breaking.cc
0425c8a118cdc97c3d56b0c11a9f79a53775932a
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include <math.h>               // rint
9
10 #include "gourlay-breaking.hh"
11 #include "column-x-positions.hh"
12 #include "debug.hh"
13 #include "paper-column.hh"
14 #include "paper-score.hh"
15 #include "paper-def.hh"
16 #include "simple-spacer.hh"
17 #include "line-of-score.hh"
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   this function is longish, but not very complicated.
51   
52  */
53 Array<Column_x_positions>
54 Gourlay_breaking::do_solve () const
55 {
56   Array<Break_node> optimal_paths;
57   Link_array<Paper_column> all =
58     pscore_l_->line_l_->column_l_arr ();
59   
60   Array<int> breaks = find_break_indices ();
61   
62   optimal_paths.set_size (breaks.size ());
63
64   Break_node first_node ;
65   first_node.line_config_.energy_f_ = 0;  
66   
67   optimal_paths[0] = first_node; 
68   int break_idx=1;
69
70   for (; break_idx< breaks.size (); break_idx++) 
71     {
72       /*
73         start with a short line, add measures. At some point 
74         the line becomes infeasible. Then we don't try to add more 
75         */
76       int minimal_start_idx = -1;
77       Column_x_positions minimal_sol;
78       Column_x_positions backup_sol;
79       
80       Real minimal_demerits = infinity_f;
81
82       for (int start_idx = break_idx; start_idx--;)
83         {
84           Link_array<Paper_column> line = all.slice (breaks[start_idx], breaks[break_idx]+1);
85   
86           line[0] = dynamic_cast<Paper_column*>(line[0]->find_broken_piece (RIGHT));
87           line.top () =  dynamic_cast<Paper_column*>(line.top ()->find_broken_piece (LEFT));
88             
89           Column_x_positions cp;
90           cp.cols_ = line;
91
92           Interval line_dims
93             = pscore_l_->paper_l_->line_dimensions_int (optimal_paths[start_idx].line_i_);
94           Simple_spacer * sp = generate_spacing_problem (line, line_dims);
95           sp->solve (&cp);
96           delete sp;
97
98           if (start_idx == break_idx - 1)
99             backup_sol = cp;    // in case everything fucks up
100           if (!cp.satisfies_constraints_b_)
101             break;
102
103           
104           Real this_demerits;
105           if (optimal_paths[start_idx].demerits_f_ >= infinity_f)
106             this_demerits = infinity_f;
107           else
108             this_demerits = combine_demerits (optimal_paths[start_idx].line_config_, cp)
109               + optimal_paths[start_idx].demerits_f_;
110
111           if (this_demerits < minimal_demerits) 
112             {
113               minimal_start_idx = start_idx;
114               minimal_sol = cp;
115               minimal_demerits = this_demerits;
116             }
117         }
118
119       int prev =break_idx - 1;
120       if (minimal_start_idx < 0) 
121         {
122           optimal_paths[break_idx].demerits_f_ = infinity_f;
123           optimal_paths[break_idx].line_config_ = backup_sol;     
124         }
125       else 
126         {
127           prev = minimal_start_idx;
128           optimal_paths[break_idx].line_config_ = minimal_sol;
129           optimal_paths[break_idx].demerits_f_ = minimal_demerits;
130         }
131       optimal_paths[break_idx].prev_break_i_ = prev;
132       optimal_paths[break_idx].line_i_ = optimal_paths[prev].line_i_ + 1;
133
134       if (! (break_idx % HAPPY_DOTS_I))
135         progress_indication (String ("[") + to_str (break_idx) + "]");
136     }
137
138   /* do the last one */
139   if  (break_idx % HAPPY_DOTS_I)
140         progress_indication (String ("[") + to_str (break_idx) + "]");    
141
142
143   progress_indication ("\n");
144
145   Array<int> final_breaks;
146   Array<Column_x_positions> lines;
147
148   /* skip 0-th element, since it is a "dummy" elt*/
149   for (int i = optimal_paths.size ()-1; i> 0;) 
150     {
151       final_breaks.push (i);
152       int prev = optimal_paths[i].prev_break_i_;
153       assert (i > prev);
154       i = prev;
155     }
156
157   if (optimal_paths.top ().demerits_f_ >= infinity_f)
158     warning (_ ("No feasible line breaking found"));
159   
160   for (int i= final_breaks.size (); i--;) 
161     lines.push (optimal_paths[final_breaks[i]].line_config_);
162   
163   return lines;
164 }
165
166
167 Gourlay_breaking::Gourlay_breaking ()
168 {
169   energy_bound_f_ = infinity_f;
170 }
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");
186       if (gh_number_p (pen))
187         {
188           break_penalties += gh_scm2double (pen);
189         }
190     }
191
192   return abs (this_one.force_f_) + abs (prev.force_f_ - this_one.force_f_)
193     + break_penalties;
194 }
195