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