]> git.donarmstrong.com Git - lilypond.git/blob - lily/gourlay-breaking.cc
* lily/stem.cc (get_default_stem_end_position): use beam_count - 1
[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   void print () const
49   {
50     printf ("prev break %d, line %d, demerits %f\n",
51             prev_break_, line_, demerits_);
52   }
53 };
54
55 void
56 print_break_nodes (Array<Break_node> const & arr)
57 {
58   for (int i =0; i < arr.size (); i++)
59     {
60       printf ( "node %d: ", i); 
61       arr[i].print ();
62     }      
63 }
64
65 /**
66   This algorithms is adapted from the OSU Tech report on breaking lines.
67
68   this function is longish, but not very complicated.
69   
70  */
71 Array<Column_x_positions>
72 Gourlay_breaking::do_solve () const
73 {
74   Array<Break_node> optimal_paths;
75   Link_array<Grob> all =
76     pscore_->system_->columns ();
77   
78   Array<int> breaks = find_break_indices ();
79   
80   Break_node first_node ;
81   optimal_paths.push (first_node);
82
83   Real worst_force = 0.0;
84   
85   for (int break_idx=1; break_idx< breaks.size (); break_idx++) 
86     {
87       /*
88         start with a short line, add measures. At some point 
89         the line becomes infeasible. Then we don't try to add more 
90         */
91       int minimal_start_idx = -1;
92       Column_x_positions minimal_sol;
93       Column_x_positions backup_sol;
94       
95       Real minimal_demerits = infinity_f;
96
97       bool ragged = to_boolean (pscore_->paper_->get_scmvar ("raggedright"));
98
99       for (int start_idx = break_idx; start_idx--;)
100         {
101           Link_array<Grob> line = all.slice (breaks[start_idx], breaks[break_idx]+1);
102   
103           line[0]     = dynamic_cast<Item*> (line[0])    ->find_prebroken_piece (RIGHT);
104           line.top () = dynamic_cast<Item*> (line.top ())->find_prebroken_piece (LEFT);
105             
106           Column_x_positions cp;
107           cp.cols_ = line;
108
109           Interval line_dims
110             = pscore_->paper_->line_dimensions_int (optimal_paths[start_idx].line_);
111           Simple_spacer * sp = generate_spacing_problem (line, line_dims);
112           sp->solve (&cp, ragged);
113           delete sp;
114
115           if (fabs (cp.force_) > worst_force)
116             worst_force = fabs (cp.force_);
117
118           /*
119             We remember this solution as a "should always work
120             solution", in case everything fucks up.  */
121           if (start_idx == break_idx - 1)
122             backup_sol = cp;
123                   
124           Real this_demerits;
125
126           if (optimal_paths[start_idx].demerits_ >= infinity_f)
127             this_demerits = infinity_f;
128           else
129             this_demerits = combine_demerits (optimal_paths[start_idx].line_config_, cp)
130               + optimal_paths[start_idx].demerits_;
131
132           if (this_demerits < minimal_demerits) 
133             {
134               minimal_start_idx = start_idx;
135               minimal_sol = cp;
136               minimal_demerits = this_demerits;
137             }
138
139           /*
140             we couldn't satisfy the constraints, this won't get better
141             if we add more columns, so we get on with the next one
142           */
143           if (!cp.satisfies_constraints_b_)
144             break ; 
145         }
146
147
148       Break_node bnod;
149       if (minimal_start_idx < 0) 
150         {
151           bnod.demerits_ = infinity_f;
152           bnod.line_config_ = backup_sol;
153           bnod.prev_break_ = break_idx - 1;       
154         }
155       else 
156         {
157           bnod.prev_break_ = minimal_start_idx;
158           bnod.demerits_ = minimal_demerits;
159           bnod.line_config_ = minimal_sol;
160         }
161       bnod.line_ = optimal_paths[bnod.prev_break_].line_ + 1;
162       optimal_paths.push (bnod);
163       
164       if (! (break_idx % HAPPY_DOTS_I))
165         progress_indication (String ("[") + to_string (break_idx) + "]");
166     }
167
168   /* do the last one */
169   if (breaks.size () % HAPPY_DOTS_I)
170     progress_indication (String ("[") + to_string (breaks.size()) + "]");    
171
172   progress_indication ("\n");
173
174   Array<int> final_breaks;
175   Array<Column_x_positions> lines;
176
177   /* skip 0-th element, since it is a "dummy" elt*/
178   for (int i = optimal_paths.size ()-1; i> 0;) 
179     {
180       final_breaks.push (i);
181       int prev = optimal_paths[i].prev_break_;
182       assert (i > prev);
183       i = prev;
184     }
185
186   if (verbose_global_b)
187     printf ("Optimal demerits: %f\n", optimal_paths.top ().demerits_); 
188   
189   if (optimal_paths.top ().demerits_ >= infinity_f)
190     warning (_ ("No feasible line breaking found"));
191   
192   for (int i= final_breaks.size (); i--;)
193     {
194       Column_x_positions cp (optimal_paths[final_breaks[i]].line_config_);
195       
196       lines.push (cp);
197       if(!cp.satisfies_constraints_b_)
198         warning ("Could not find line breaking that satisfies constraints.");
199     }
200   return lines;
201 }
202
203
204 Gourlay_breaking::Gourlay_breaking ()
205 {
206 }
207
208
209
210 /*
211   TODO: uniformity parameter to control rel. importance of spacing differences.
212
213   TODO:
214
215   mixing break penalties and constraint-failing solutions is confusing.
216  */
217 Real
218 Gourlay_breaking::combine_demerits (Column_x_positions const &prev,
219                                     Column_x_positions const &this_one) const
220 {
221   Real break_penalties = 0.0;
222   Grob * pc = this_one.cols_.top ();
223   if (pc->original_)
224     {
225       SCM pen = pc->get_grob_property ("penalty");
226       if (gh_number_p (pen) && fabs (gh_scm2double (pen)) < 10000)
227         {
228           break_penalties += gh_scm2double (pen);
229         }
230     }
231   /*
232     Q: do want globally non-cramped lines, or locally equally cramped lines. 
233    */
234   Real demerit = abs (this_one.force_) +  abs (prev.force_ - this_one.force_)
235     + break_penalties;
236
237   if (!this_one.satisfies_constraints_b_)
238      {
239        /*
240          If it doesn't satisfy constraints, we make this one
241          really unattractive.
242
243          add 20000 to the demerits, so that a break penalty
244          of -10000 won't change the result */
245        demerit = (demerit + 20000) >? 2000;
246        
247        demerit *= 10;
248      }
249
250    return demerit;
251 }
252