]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/constrained-breaking.hh
* scm/define-grobs.scm (all-grob-descriptions): make NonMusicalPaperColumn
[lilypond.git] / lily / include / constrained-breaking.hh
1 /*
2   constrained-breaking.hh -- declare a line breaker that
3   supports limits on the number of systems
4
5   source file of the GNU LilyPond music typesetter
6
7   (c) 2006 Joe Neeman <joeneeman@gmail.com>
8 */
9
10 #ifndef CONSTRAINED_BREAKING_HH
11 #define CONSTRAINED_BREAKING_HH
12
13 #include "break-algorithm.hh"
14 #include "lily-guile.hh"
15 #include "matrix.hh"
16
17 struct Line_details {
18   Real force_;
19   Interval extent_;   /* Y-extent of the system */
20   Real padding_;  /* compulsory space after this system (if we're not last on a page) */
21   Real bottom_padding_;
22   Real space_;    /* spring length */
23   Real inverse_hooke_;
24
25   SCM break_permission_;
26   SCM page_permission_;
27   SCM turn_permission_;
28   Real break_penalty_;
29   Real page_penalty_;
30   Real turn_penalty_;
31
32   Line_details ()
33   {
34     force_ = infinity_f;
35     padding_ = 0;
36     bottom_padding_ = 0;
37     space_ = 0;
38     inverse_hooke_ = 1;
39     break_permission_ = ly_symbol2scm ("allow");
40     page_permission_ = ly_symbol2scm ("allow");
41     turn_permission_ = ly_symbol2scm ("allow");
42     break_penalty_ = 0;
43     page_penalty_ = 0;
44     turn_penalty_ = 0;
45   }
46 };
47
48 /*
49    Helper to trace back an optimal path
50 */
51 struct Constrained_break_node
52 {
53   /* the number of bars in all the systems before this one
54   */
55   int prev_;
56
57   /* unlike the Gourlay breaker, this is the sum of all demerits up to,
58    * and including, this line */
59   Real demerits_;
60   struct Line_details details_;
61
62   Constrained_break_node ()
63   {
64     prev_ = -1;
65     demerits_ = infinity_f;
66   }
67
68   void print () const
69   {
70     printf ("prev break %d, demerits %f\n", prev_, demerits_);
71   }
72 };
73
74 /*
75    A dynamic programming solution to breaking scores into lines
76 */
77 class Constrained_breaking : public Break_algorithm
78 {
79 public:
80   vector<Column_x_positions> solve ();
81   Constrained_breaking ();
82   Constrained_breaking (vector<vsize> const &start_col_posns);
83
84   vector<Column_x_positions> get_solution (vsize start, vsize end, vsize sys_count);
85   vector<Column_x_positions> get_best_solution (vsize start, vsize end);
86   vector<Line_details> get_details (vsize start, vsize end, vsize sys_count);
87   int get_max_systems (vsize start, vsize end);
88   int get_min_systems (vsize start, vsize end);
89
90   void resize (vsize systems);
91
92 private:
93   vsize valid_systems_;
94   vsize systems_;
95
96   /* the (i,j)th entry is the configuration for breaking between
97     columns i and j */
98   Matrix<Line_details> lines_;
99
100   /* the [i](j,k)th entry is the score for fitting the first k bars onto the
101     first j systems, starting at the i'th allowed starting column */
102   vector<Matrix<Constrained_break_node> > state_;
103
104   vector<vsize> start_;         /* the columns at which we might be asked to start breaking */
105   vector<vsize> starting_breakpoints_; /* the corresponding index in breaks_ */
106
107   vector<Grob*> all_;
108   vector<vsize> breaks_;
109
110   Column_x_positions space_line (vsize start_col, vsize end_col);
111   vsize prepare_solution (vsize start, vsize end, vsize sys_count);
112
113   Real combine_demerits (Real force, Real prev_force);
114
115   bool calc_subproblem(vsize start, vsize systems, vsize max_break_index);
116 };
117 #endif /* CONSTRAINED_BREAKING_HH */