]> git.donarmstrong.com Git - lilypond.git/blob - lily/gourlay-breaking.cc
release: 1.2.14
[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--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "gourlay-breaking.hh"
10 #include "column-x-positions.hh"
11 #include "debug.hh"
12 #include "paper-column.hh"
13 #include "paper-score.hh"
14 #include "paper-def.hh"
15 #include "simple-spacer.hh"
16
17 #include "killing-cons.tcc"
18
19 /// How often to print operator pacification marks?
20 const int HAPPY_DOTS_I = 3;
21
22 /**
23   Helper to trace back an optimal path 
24  */
25 struct Break_node {
26   /** this was the previous. If negative, this break should not be
27     considered: this path has infinite energy
28     
29     */
30   int prev_break_i_;
31   /**
32      Which system number so far?
33    */
34   int line_i_;
35
36   Real demerits_f_;
37   Column_x_positions line_config_;
38   
39   Break_node () 
40   {
41     prev_break_i_ = -1;
42     line_i_ = 0;
43   }
44 };
45
46 /**
47   This algorithms is adapted from the OSU Tech report on breaking lines.
48  */
49 Array<Column_x_positions>
50 Gourlay_breaking::do_solve () const
51 {
52   Array<Break_node> optimal_paths;
53   Line_of_cols all = pscore_l_->col_l_arr_ ;
54   Array<int> breaks = find_break_indices ();
55   
56   optimal_paths.set_size (breaks.size ());
57
58   Break_node first_node ;
59   first_node.prev_break_i_ = -1;
60   first_node.line_config_.force_f_ = 0;
61   first_node.line_config_.energy_f_ = 0;  
62   first_node.line_i_ = 0;
63   
64   optimal_paths[0] = first_node; 
65   int break_idx=1;
66
67   for (; break_idx< breaks.size (); break_idx++) 
68     {
69       /*
70         start with a short line, add measures. At some point 
71         the line becomes infeasible. Then we don't try to add more 
72         */
73       int minimal_start_idx = -1;
74       Column_x_positions minimal_sol;
75       Real minimal_demerits = infinity_f;
76
77       for (int start_idx = break_idx; start_idx--;)
78         {
79 #if 0
80           if  (break_idx - start_idx > max_measures_i_) 
81             break;
82 #endif
83           
84           if (optimal_paths[start_idx].prev_break_i_ < 0
85               && optimal_paths[start_idx].line_config_.energy_f_)
86             continue;
87
88
89           
90           Line_of_cols line = all.slice (breaks[start_idx], breaks[break_idx]+1);
91   
92           line[0] = dynamic_cast<Paper_column*>(line[0]->find_broken_piece (RIGHT));
93           line.top () =  dynamic_cast<Paper_column*>(line.top ()->find_broken_piece (LEFT));
94             
95           if (!feasible (line))
96             break;
97             
98           Column_x_positions cp;
99           cp.cols_ = line;
100
101           Interval line_dims
102             = pscore_l_->paper_l_->line_dimensions_int (optimal_paths[start_idx].line_i_);
103           Simple_spacer * sp = generate_spacing_problem (line, line_dims);
104           sp->solve (&cp);
105           delete sp;
106           
107           if (!cp.satisfies_constraints_b_)
108             break;
109
110           Real this_demerits 
111             = combine_demerits (optimal_paths[start_idx].line_config_, cp)
112             + optimal_paths[start_idx].demerits_f_;
113
114           if (this_demerits < minimal_demerits) 
115             {
116               minimal_start_idx = start_idx;
117               minimal_sol = cp;
118               minimal_demerits = this_demerits;
119             }
120         }
121
122       if (minimal_start_idx < 0) 
123         {
124           optimal_paths[break_idx].prev_break_i_ = -1;
125           optimal_paths[break_idx].line_config_.energy_f_ = infinity_f;
126         }
127       else 
128         {
129           optimal_paths[break_idx].prev_break_i_ = minimal_start_idx;
130           optimal_paths[break_idx].line_config_ = minimal_sol;
131           optimal_paths[break_idx].demerits_f_ = minimal_demerits;
132           optimal_paths[break_idx].line_i_ =
133             optimal_paths[minimal_start_idx].line_i_ + 1;
134         }
135
136       if (! (break_idx % HAPPY_DOTS_I))
137         *mlog << "[" << break_idx << "]" << flush;
138     }
139
140   /* do the last one */
141   if  (break_idx % HAPPY_DOTS_I) 
142     *mlog << "[" << break_idx << "]";
143
144   *mlog << endl;
145
146   Array<int> final_breaks;
147   Array<Column_x_positions> lines;
148
149   /* skip 0-th element, since it is a "dummy" elt*/
150   for (int i = optimal_paths.size ()-1; i> 0;) 
151     {
152       final_breaks.push (i);
153       assert (i > optimal_paths[i].prev_break_i_);
154
155       // there was no "feasible path"
156       if (!optimal_paths[i].line_config_.config_.size ()) {
157         final_breaks.set_size (0);
158         break;
159       }
160       i = optimal_paths[i].prev_break_i_;
161     }
162
163
164   for (int i= final_breaks.size (); i--;) 
165     lines.push (optimal_paths[final_breaks[i]].line_config_);
166   
167   return lines;
168 }
169
170
171 Gourlay_breaking::Gourlay_breaking ()
172 {
173   energy_bound_f_ = infinity_f;
174   max_measures_i_ = INT_MAX;
175 }
176
177 void
178 Gourlay_breaking::do_set_pscore ()
179 {
180   max_measures_i_ =int (rint (pscore_l_->paper_l_->get_var ("gourlay_maxmeasures")));
181 }
182
183
184 /*
185   TODO: uniformity parameter to control rel. importance of spacing differences.
186  */
187 Real
188 Gourlay_breaking::combine_demerits (Column_x_positions const &prev,
189                                     Column_x_positions const &this_one) const
190 {
191   return abs (this_one.force_f_) + abs (prev.force_f_ - this_one.force_f_);
192 }