]> git.donarmstrong.com Git - lilypond.git/blob - lily/constrained-breaking.cc
* scm/paper-system.scm (paper-system-annotate): also annotate the
[lilypond.git] / lily / constrained-breaking.cc
1 /*
2   constrained-breaking.cc -- implement a line breaker that
3   support limits on the number of systems
4
5   source file of the GNU LilyPond music typesetter
6
7   (c) 2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
9
10 #include "constrained-breaking.hh"
11
12 #include "international.hh"
13 #include "main.hh"
14 #include "output-def.hh"
15 #include "paper-column.hh"
16 #include "paper-score.hh"
17 #include "simple-spacer.hh"
18 #include "system.hh"
19 #include "warn.hh"
20
21 /*
22   We use the following optimal substructure. Let W(A) be our weight function.
23
24   Let A_{k,n} = (a_{k,n,1}, ... a_{k,n,k}) be the optimal set of line breaks
25   for k systems and n potential breakpoints. a_{k,n,k} = n (it is the end of
26   the piece)
27
28   Then A_{k+1, m} is contructed from
29   min_ {k < j < m} ( W(A_{k,j} :: m) )
30   where by A::m we denote appending m to the list A
31
32   Indices in the code:
33
34   The above algorithm makes it easy to end at a point before the end of the
35   score (just find A_{k,m} for some m < breaks_.size () - 1). However, we must
36   add information for starting at a point after the beginning. One constructor
37   allows the specification of a list of starting columns, start_. We then have
38   start_.size () different solution arrays. state_[i] is the array for the
39   solution starting at column number start_[i].
40
41   The indicies "start" and "end" refer to the index in the start_ array of the
42   desired starting and ending columns.
43
44   each solution array looks like
45    a_{1,1,1} a_{2,1,2} a_{3,1,3} . . .
46        X     a_{2,2,2} a_{3,2,3} . . .
47        X         X     a_{3,3,3} . . .
48        .         .         .     .
49        .         .         .       .
50   where the X's mark invalid solutions (can't have more systems than
51   breakpoints). Note that each value is of the form a_{x,n,x}. This is because
52   a breakpoint of the form a_{x,n,x-1} will also be called a_{x-1,m,x-1} for
53   some m < n. Each cell in the array stores the value of its m (ie. the
54   ending breakpoint of the previous line) as "prev_".
55
56   For finding A_{sys, brk}, let "me" be the (sys_count,brk) cell in our
57   solution array (state_[start][sys * rank + brk]).
58
59   Then A_{sys, brk} = A_{sys - 1, me.prev_} :: me
60 */
61
62 /*
63   start and sys here are indexed from 0.
64   brk is indexed from starting_breakpoints_[start]
65   (for brk, starting_breakpoints_[start] is the beginning
66   of the piece; the smallest value we should ever see here is
67   starting_breakpoints_[start] + 1) */
68 bool
69 Constrained_breaking::calc_subproblem (vsize start, vsize sys, vsize brk)
70 {
71   assert (sys < systems_);
72   assert (start < start_.size ());
73   assert (brk < breaks_.size ());
74
75   bool found_something = false;
76   vsize start_col = starting_breakpoints_[start];
77   vector<Constrained_break_node> &st = state_[start];
78   vsize rank = breaks_.size () - start_col;
79   vsize max_index = brk - start_col;
80   for (vsize j=sys; j < max_index; j++)
81     {
82       if (0 == sys && j > 0)
83         break; /* the first line cannot have its first break after the beginning */
84
85       Line_details const &cur = lines_[(j + start_col)*lines_rank_ + brk];
86       Real prev_f = 0;
87       Real prev_dem = 0;
88
89       if (sys > 0)
90         {
91           prev_f = st[(sys-1) * rank + j].details_.force_;
92           prev_dem = st[(sys-1) * rank + j].demerits_;
93         }
94       if (isinf (prev_dem))
95         break;
96
97       Real dem = combine_demerits (cur.force_, prev_f) + prev_dem + cur.break_penalty_;
98       if (isinf (dem))
99         continue;
100
101       int k = sys*rank + max_index;
102       if (isinf (st[k].demerits_) || dem < st[k].demerits_)
103         {
104           found_something = true;
105           st[k].demerits_ = dem;
106           st[k].details_ = cur;
107           st[k].prev_ = j;
108         }
109     }
110   return found_something;
111 }
112
113 vector<Column_x_positions>
114 Constrained_breaking::solve ()
115 {
116   if (!systems_)
117     {
118       programming_error (_f ("no system number set in constrained-breaking"));
119       systems_ = breaks_.size () / 4;
120     }
121
122   resize (systems_);
123   return get_solution(0, VPOS, systems_);
124 }
125
126 Column_x_positions
127 Constrained_breaking::space_line (vsize i, vsize j)
128 {
129   bool ragged_right = to_boolean (pscore_->layout ()->c_variable ("ragged-right"));
130   bool ragged_last = to_boolean (pscore_->layout ()->c_variable ("ragged-last"));
131   Column_x_positions col;
132
133   vector<Grob*> line (all_.begin () + breaks_[i],
134                       all_.begin() + breaks_[j] + 1);
135   Interval line_dims = line_dimensions_int (pscore_->layout (), i);
136   bool last = j == breaks_.size () - 1;
137   bool ragged = ragged_right || (last && ragged_last);
138
139   return get_line_configuration (line, line_dims[RIGHT] - line_dims[LEFT], line_dims[LEFT], ragged);
140 }
141
142 void
143 Constrained_breaking::resize (vsize systems)
144 {
145   systems_ = systems;
146
147   if (!breaks_.size () && pscore_)
148     {
149       Output_def *l = pscore_->layout ();
150       System *sys = pscore_->root_system ();
151       Real padding = robust_scm2double (l->c_variable ("between-system-padding"), 0);
152       Real space = robust_scm2double (l->c_variable ("ideal-system-space"), 0);
153       bool ragged_right = to_boolean (pscore_->layout ()->c_variable ("ragged-right"));
154       bool ragged_last = to_boolean (pscore_->layout ()->c_variable ("ragged-last"));
155
156       Interval first_line = line_dimensions_int (pscore_->layout (), 0);
157       Interval other_lines = line_dimensions_int (pscore_->layout (), 1);
158       /* do all the rod/spring problems */
159       breaks_ = pscore_->find_break_indices ();
160       lines_rank_ = breaks_.size ();
161       all_ = pscore_->root_system ()->columns ();
162       lines_.resize (breaks_.size () * breaks_.size ());
163       vector<Real> forces = get_line_forces (all_,
164                                              breaks_,
165                                              other_lines.length (),
166                                              other_lines.length () - first_line.length (),
167                                              ragged_right);
168       for (vsize i = 0; i < breaks_.size () - 1; i++)
169         {
170           Real max_ext = 0;
171           for (vsize j = i + 1; j < breaks_.size (); j++)
172             {
173               int start = Paper_column::get_rank (all_[breaks_[i]]);
174               int end = Paper_column::get_rank (all_[breaks_[j]]);
175               Interval extent = sys->pure_height (sys, start, end);
176               bool last = j == breaks_.size () - 1;
177               bool ragged = ragged_right || (last && ragged_last);
178               int k = i*lines_rank_ + j;
179               SCM pen = all_[breaks_[j]]->get_property ("line-break-penalty");
180               if (scm_is_number (pen))
181                 lines_[k].break_penalty_ = scm_to_double (pen);
182
183               max_ext = max (max_ext, extent.length ());
184               lines_[k].force_ = forces[k];
185               lines_[k].extent_ = extent.length ();
186               lines_[k].padding_ = padding;
187               lines_[k].space_ = space;
188               lines_[k].inverse_hooke_ = 1;
189               if (ragged && lines_[k].force_ < 0)
190                 lines_[k].force_ = infinity_f;
191               if (isinf (lines_[k].force_))
192                 break;
193             }
194         }
195
196       /* work out all the starting indices */
197       for (vsize i = 0; i < start_.size (); i++)
198         {
199           vsize j;
200           for (j = 0; j < breaks_.size () - 1 && breaks_[j] < start_[i]; j++)
201             ;
202           starting_breakpoints_.push_back (j);
203           start_[i] = breaks_[j];
204         }
205       state_.resize (start_.size ());
206     }
207
208   if (pscore_ && systems_ > valid_systems_)
209     {
210       for (vsize i = 0; i < state_.size (); i++)
211         state_[i].resize((breaks_.size () - starting_breakpoints_[i]) * systems_);
212
213       /* fill out the matrices */
214       for (vsize i = 0; i < state_.size (); i++)
215         for (vsize j = valid_systems_; j < systems_; j++)
216           for (vsize k = starting_breakpoints_[i] + j + 1; k < breaks_.size (); k++)
217             if (!calc_subproblem (i, j, k))
218               break; /* if we couldn't break this, it is too cramped already */
219       valid_systems_ = systems_;
220     }
221 }
222
223 vector<Column_x_positions>
224 Constrained_breaking::get_solution (vsize start, vsize end, vsize sys_count)
225 {
226   vsize rank;
227   vsize end_brk;
228   vsize start_brk = starting_breakpoints_[start];
229   prepare_solution (start, end, sys_count, &rank, &end_brk);
230
231   vector<Constrained_break_node> const &st = state_[start];
232   vector<Column_x_positions> ret;
233
234   /* find the first solution that satisfies constraints */
235   for (vsize sys = sys_count-1; sys != VPOS; sys--)
236     {
237       for (vsize brk = end_brk; brk != VPOS; brk--)
238         {
239           if (!isinf (st[sys*rank + brk].details_.force_))
240             {
241               if (brk != end_brk)
242                 {
243                   warning ( _("couldn't find line breaking that satisfies constraints" ));
244                   ret.push_back (space_line (brk, end_brk));
245                 }
246               /* build up the good solution */
247               for (vsize cur_sys = sys; cur_sys != VPOS; cur_sys--)
248                 {
249                   vsize prev_brk = st[cur_sys*rank + brk].prev_;
250                   assert (brk != VPOS);
251                   ret.push_back (space_line (prev_brk + start_brk, brk + start_brk));
252                   brk = prev_brk;
253                 }
254               reverse (ret);
255               return ret;
256             }
257         }
258     }
259   /* if we get to here, just put everything on one line */
260   warning ( _("couldn't find line breaking that satisfies constraints" ));
261   ret.push_back (space_line (0, end_brk));
262   return ret;
263 }
264
265 std::vector<Line_details>
266 Constrained_breaking::get_details (vsize start, vsize end, vsize sys_count)
267 {
268   vsize rank;
269   vsize brk;
270   prepare_solution (start, end, sys_count, &rank, &brk);
271   vector<Constrained_break_node> const &st = state_[start];
272   vector<Line_details> ret;
273
274   for (int sys = sys_count-1; sys >= 0 && brk != VPOS; sys--)
275     {
276       ret.push_back (st[sys*rank + brk].details_);
277       brk = st[sys*rank + brk].prev_;
278     }
279   return ret;
280 }
281
282 int
283 Constrained_breaking::get_min_systems (vsize start, vsize end)
284 {
285   vsize rank;
286   vsize brk;
287   vsize sys_count;
288
289   prepare_solution (start, end, 1, &rank, &brk);
290   vector<Constrained_break_node> const &st = state_[start];
291
292   /* sys_count < rank : rank is the # of breakpoints, we can't have more systems */
293   for (sys_count = 0; sys_count < rank; sys_count++)
294     {
295       if (sys_count >= valid_systems_)
296         {
297           resize (sys_count + 3);
298         }
299       if (!isinf (st[sys_count*rank + brk].details_.force_))
300         return sys_count + 1;
301     }
302   /* no possible breaks satisfy constraints */
303   return 1;
304 }
305
306 int
307 Constrained_breaking::get_max_systems (vsize start, vsize end)
308 {
309   vsize brk = (end >= start_.size ()) ? breaks_.size () - 1 : starting_breakpoints_[end];
310   return brk - starting_breakpoints_[start];
311 }
312
313 void
314 Constrained_breaking::prepare_solution (vsize start, vsize end, vsize sys_count, vsize *rank, vsize *brk)
315 {
316   assert (start < start_.size () && (end == VPOS || end <= start_.size ()));
317   assert (start < end);
318
319   resize (sys_count);
320   if (end == start_.size ())
321     end = VPOS;
322
323   *rank = breaks_.size () - starting_breakpoints_[start];
324   *brk = end == VPOS ? breaks_.size () - 1 : starting_breakpoints_[end];
325   *brk -= starting_breakpoints_[start];
326 }
327
328 Constrained_breaking::Constrained_breaking ()
329 {
330   valid_systems_ = systems_ = 0;
331   start_.push_back (0);
332 }
333
334 Constrained_breaking::Constrained_breaking (vector<vsize> const &start)
335   : start_ (start)
336 {
337   valid_systems_ = systems_ = 0;
338 }
339
340 Real
341 Constrained_breaking::combine_demerits (Real force, Real prev_force)
342 {
343   return force * force + (prev_force - force) * (prev_force - force);
344 }
345