]> git.donarmstrong.com Git - lilypond.git/blob - lily/gourlay-breaking.cc
``slikken kreng''
[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--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include <math.h>               // rint
9 #include <stdio.h>
10
11 #include "gourlay-breaking.hh"
12 #include "column-x-positions.hh"
13 #include "warn.hh"
14 #include "main.hh"
15 #include "paper-column.hh"
16 #include "paper-score.hh"
17 #include "paper-def.hh"
18 #include "simple-spacer.hh"
19 #include "system.hh"
20
21 /// How often to print operator pacification marks?
22 const int HAPPY_DOTS_I = 3;
23
24 /**
25   Helper to trace back an optimal path 
26  */
27 struct Break_node {
28   /** this was the previous. If negative, this break should not be
29     considered: this path has infinite energy
30     
31     */
32   int prev_break_;
33   /**
34      Which system number so far?
35    */
36   int line_;
37
38   Real demerits_;
39   Column_x_positions line_config_;
40   
41   Break_node () 
42   {
43     prev_break_ = -1;
44     line_ = 0;
45     demerits_ = 0;
46   }
47 };
48
49 /**
50   This algorithms is adapted from the OSU Tech report on breaking lines.
51
52   this function is longish, but not very complicated.
53   
54  */
55 Array<Column_x_positions>
56 Gourlay_breaking::do_solve () const
57 {
58   Array<Break_node> optimal_paths;
59   Link_array<Grob> all =
60     pscore_->system_->columns ();
61   
62   Array<int> breaks = find_break_indices ();
63   
64   Break_node first_node ;
65   optimal_paths.push (first_node);
66
67   Real worst_force = 0.0;
68   
69   for (int break_idx=1; break_idx< breaks.size (); break_idx++) 
70     {
71       /*
72         start with a short line, add measures. At some point 
73         the line becomes infeasible. Then we don't try to add more 
74         */
75       int minimal_start_idx = -1;
76       Column_x_positions minimal_sol;
77       Column_x_positions backup_sol;
78       
79       Real minimal_demerits = infinity_f;
80
81       bool ragged = to_boolean (pscore_->paper_->get_scmvar ("raggedright"));
82
83       for (int start_idx = break_idx; start_idx--;)
84         {
85           Link_array<Grob> line = all.slice (breaks[start_idx], breaks[break_idx]+1);
86   
87           line[0]     = dynamic_cast<Item*> (line[0])    ->find_prebroken_piece (RIGHT);
88           line.top () = dynamic_cast<Item*> (line.top ())->find_prebroken_piece (LEFT);
89             
90           Column_x_positions cp;
91           cp.cols_ = line;
92
93           Interval line_dims
94             = pscore_->paper_->line_dimensions_int (optimal_paths[start_idx].line_);
95           Simple_spacer * sp = generate_spacing_problem (line, line_dims);
96           sp->solve (&cp, ragged);
97           delete sp;
98
99           if (fabs (cp.force_) > worst_force)
100             worst_force = fabs (cp.force_);
101
102           /*
103             We remember this solution as a "should always work
104             solution", in case everything fucks up.  */
105           if (start_idx == break_idx - 1)
106             backup_sol = cp;
107                   
108           Real this_demerits;
109
110           if (optimal_paths[start_idx].demerits_ >= infinity_f)
111             this_demerits = infinity_f;
112           else
113             this_demerits = combine_demerits (optimal_paths[start_idx].line_config_, cp)
114               + optimal_paths[start_idx].demerits_;
115
116           if (this_demerits < minimal_demerits) 
117             {
118               minimal_start_idx = start_idx;
119               minimal_sol = cp;
120               minimal_demerits = this_demerits;
121             }
122
123           /*
124             we couldn't satisfy the constraints, this won't get better
125             if we add more columns, so we get on with the next one
126           */
127           if (!cp.satisfies_constraints_b_)
128             break ; 
129         }
130
131
132       Break_node bnod;
133       if (minimal_start_idx < 0) 
134         {
135           bnod.demerits_ = infinity_f;
136           bnod.line_config_ = backup_sol;
137           bnod.prev_break_ = break_idx - 1;       
138         }
139       else 
140         {
141           bnod.prev_break_ = minimal_start_idx;
142           bnod.demerits_ = minimal_demerits;
143           bnod.line_config_ = minimal_sol;
144         }
145       bnod.line_ = optimal_paths[bnod.prev_break_].line_ + 1;
146       optimal_paths.push (bnod);
147       
148       if (! (break_idx % HAPPY_DOTS_I))
149         progress_indication (String ("[") + to_string (break_idx) + "]");
150     }
151
152   /* do the last one */
153   if (breaks.size () % HAPPY_DOTS_I)
154     progress_indication (String ("[") + to_string (breaks.size()) + "]");    
155
156
157   progress_indication ("\n");
158
159   Array<int> final_breaks;
160   Array<Column_x_positions> lines;
161
162   /* skip 0-th element, since it is a "dummy" elt*/
163   for (int i = optimal_paths.size ()-1; i> 0;) 
164     {
165       final_breaks.push (i);
166       int prev = optimal_paths[i].prev_break_;
167       assert (i > prev);
168       i = prev;
169     }
170
171   if (verbose_global_b)
172     printf ("Optimal demerits: %f\n", optimal_paths.top ().demerits_); 
173   
174   if (optimal_paths.top ().demerits_ >= infinity_f)
175     warning (_ ("No feasible line breaking found"));
176   
177   for (int i= final_breaks.size (); i--;)
178     {
179       Column_x_positions cp (optimal_paths[final_breaks[i]].line_config_);
180       
181       lines.push (cp);
182       if(!cp.satisfies_constraints_b_)
183         warning ("Could not find line breaking that satisfies constraints.");
184     }
185   return lines;
186 }
187
188
189 Gourlay_breaking::Gourlay_breaking ()
190 {
191 }
192
193
194
195 /*
196   TODO: uniformity parameter to control rel. importance of spacing differences.
197
198   TODO:
199
200   mixing break penalties and constraint-failing solutions is confusing.
201  */
202 Real
203 Gourlay_breaking::combine_demerits (Column_x_positions const &prev,
204                                     Column_x_positions const &this_one) const
205 {
206   Real break_penalties = 0.0;
207   Grob * pc = this_one.cols_.top ();
208   if (pc->original_)
209     {
210       SCM pen = pc->get_grob_property ("penalty");
211       if (gh_number_p (pen) && fabs (gh_scm2double (pen)) < 10000)
212         {
213           break_penalties += gh_scm2double (pen);
214         }
215     }
216
217 #if 1
218   /*
219     Q: do want globally non-cramped lines, or locally equally cramped lines. 
220    */
221   Real demerit = abs (this_one.force_) + 0.1 *abs (prev.force_ - this_one.force_)
222     + break_penalties;
223 #else
224   Real demerit = abs (this_one.force_) + break_penalties;
225 #endif
226
227    if (!this_one.satisfies_constraints_b_)
228      {
229        /*
230          If it doesn't satisfy constraints, we make this one
231          really unattractive.
232
233          add 20000 to the demerits, so that a break penalty
234          of -10000 won't change the result */
235        demerit = (demerit + 20000) >? 2000;
236        
237        demerit *= 10;
238      }
239
240    return demerit;
241 }
242