]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/constrained-breaking.hh
* lily/include/constrained-breaking.hh (class
[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 Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
9
10 #ifndef CONSTRAINED_BREAKING_HH
11 #define CONSTRAINED_BREAKING_HH
12
13 #include "break-algorithm.hh"
14
15 /**
16    Helper to trace back an optimal path
17 */
18 struct Constrained_break_node
19 {
20   /** the number of bars in all the systems before this one
21   */
22   int prev_;
23
24   /** unlike the Gourlay breaker, this is the sum of all demerits up to,
25    * and including, this line */
26   Real demerits_;
27   Real force_;
28   Real penalty_;
29   Column_x_positions line_config_;
30
31   Constrained_break_node ()
32   {
33     prev_ = -1;
34     demerits_ = infinity_f;
35     force_ = infinity_f;
36     penalty_ = 0;
37     line_config_.satisfies_constraints_ = false;
38   }
39
40   void print () const
41   {
42     printf ("prev break %d, demerits %f\n",
43             prev_, demerits_);
44   }
45 };
46
47 /**
48    A dynamic programming solution to breaking scores into lines
49 */
50 class Constrained_breaking : public Break_algorithm
51 {
52   public:
53     std::vector<Column_x_positions> do_solve ();
54     Constrained_breaking ();
55     Constrained_breaking (std::vector<int> const &start_col_posns);
56
57     std::vector<Column_x_positions> get_solution(int start, int end, int sys_count);
58     Real get_demerits (int start, int end, int sys_count);
59     Real get_force (int start, int end, int sys_count);
60     Real get_penalty (int start, int end, int sys_count);
61     int get_max_systems (int start, int end);
62     int get_min_systems (int start, int end);
63
64     /* get the page penalty of system number sys with the given breaking */
65     Real get_page_penalty (int start, int end, int sys_count, int sys);
66
67   private:
68     int valid_systems_;
69     int systems_;
70
71     /* the (i,j)th entry is the column configuration for breaking between
72      * columns i and j */
73     std::vector<Column_x_positions> cols_;
74     int cols_rank_;
75
76     /* the [i](j,k)th entry is the score for fitting the first k bars onto the
77      * first j systems, starting at the i'th allowed starting column */
78     std::vector<std::vector<Constrained_break_node> > state_;
79
80     vector<int> start_;         /* the columns at which we might be asked to start breaking */
81     vector<int> starting_breakpoints_; /* the corresponding index in breaks_ */
82
83     vector<Grob*> all_;
84     std::vector<int> breaks_;
85
86     void prepare_solution (int start, int end, int sys_count, int *rank, int *brk);
87
88     void combine_demerits (Column_x_positions const &, Column_x_positions const &,
89                            Real *force, Real *pen, Real *dem) const;
90
91     bool calc_subproblem(int start, int systems, int max_break_index);
92     void resize ();
93 };
94 #endif /* CONSTRAINED_BREAKING_HH */