]> git.donarmstrong.com Git - lilypond.git/blob - lily/break.cc
f6c374dce5c88f3cdb745131398adb0190422ba3
[lilypond.git] / lily / break.cc
1 /*
2   break.cc -- implement Break_algorithm
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996,  1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "score-column.hh"
10 #include "break.hh"
11 #include "paper-def.hh"
12 #include "spring-spacer.hh"
13 #include "debug.hh"
14 #include "line-of-score.hh"
15 #include "p-score.hh"
16 #include "p-col.hh"
17 #include "cpu-timer.hh"
18 #include "command-request.hh"
19
20 String
21 Col_stats::str () const
22 {
23   String s;
24   if (!count_i_)
25     s = _ ("0 lines");
26   else if (count_i_ == 1)
27     s = _f ("1 line (of %.0f columns)", (Real)cols_i_/count_i_);
28   else
29     s = _f ("%d lines (with an average of %.1f columns)", 
30       count_i_, (Real)cols_i_/count_i_);
31   return s;
32 }
33
34 void
35 Col_stats::add (Line_of_cols const& line)
36 {
37   count_i_++;
38   cols_i_ += line.size ();
39 }
40
41
42 Col_stats::Col_stats ()
43 {
44   count_i_ =0;
45   cols_i_ =0;
46 }
47
48 /* **************************************************************** */
49
50 Line_of_cols
51 Break_algorithm::all_cols () const
52 {
53   Line_of_cols retval;
54   for (PCursor<Paper_column*> c (pscore_l_->col_p_list_.top ());
55        c.ok (); c++)
56     {
57
58       retval.push (c);
59     }
60   return retval;
61 }
62
63 Array<int>
64 Break_algorithm::find_break_indices () const
65 {
66   Line_of_cols all (all_cols ());
67   Array<int> retval;
68
69   for (int i=0; i < all.size (); i++)
70     if (all[i]->breakable_b_)
71       retval.push (i);
72
73   if (linelength <=0)
74     while (retval.size () >2)
75       retval.del (1);
76
77   return retval;
78 }
79
80 ///  return all breakable columns
81 Line_of_cols
82 Break_algorithm::find_breaks () const
83 {
84   Line_of_cols all (all_cols ());
85   Line_of_cols retval;
86
87   for (int i=0; i < all.size (); i++)
88     if (all[i]->breakable_b_)
89       retval.push (all[i]);
90
91
92   if (linelength <=0)
93     while (retval.size () >2)
94       retval.del (1);
95
96   return retval;
97 }
98
99
100
101
102
103 Line_spacer*
104 Break_algorithm::generate_spacing_problem (Line_of_cols curline, Interval line) const
105 {
106   Spring_spacer * sp= (Spring_spacer*) (*get_line_spacer) (); // ugh
107
108   sp->paper_l_ = pscore_l_->paper_l_;
109   sp->add_column (curline[0], true, line[LEFT]);
110   for (int i=1; i< curline.size ()-1; i++)
111     sp->add_column (curline[i]);
112
113   if (line.length () > 0)
114     {
115       sp->add_column (curline.top (), true, line[RIGHT]);
116       sp->energy_normalisation_f_  = sqr (line.length ());
117     }
118   else
119     sp->add_column (curline.top ());
120
121   sp->prepare ();
122   return sp;
123 }
124
125 Break_algorithm::Break_algorithm ()
126 {
127   pscore_l_ = 0;
128   get_line_spacer =0;
129   linelength = 0;
130 }
131
132 void
133 Break_algorithm::set_pscore (Paper_score*s)
134 {
135   pscore_l_ = s;
136   linelength = s->paper_l_->linewidth_f ();
137   do_set_pscore ();
138 }
139
140 bool
141 Break_algorithm::feasible (Line_of_cols curline) const
142 {
143   if (linelength <=  0)
144     return true;
145
146   for (int i=0; i < curline.size (); i++)
147     {
148       if (i && i < curline.size () -1
149           && (((Score_column*)curline[i])->break_penalty_i () >= Break_req::FORCE))
150         return false;
151     }
152   return true;
153 }
154
155 void
156 Break_algorithm::problem_OK () const
157 {
158   if (!pscore_l_->col_p_list_.size ())
159     error (_("score does not have any columns"));
160   OK ();
161 }
162
163 void
164 Break_algorithm::OK () const
165 {
166 #ifndef NDEBUG
167   iter_top (pscore_l_->col_p_list_,start);
168   PCursor<Paper_column *> end (pscore_l_->col_p_list_.bottom ());
169
170   assert (start->breakable_b_);
171   assert (end->breakable_b_);
172 #endif
173 }
174
175 Array<Column_x_positions>
176 Break_algorithm::solve () const
177 {
178   Cpu_timer timer;
179
180   Array<Column_x_positions> h= do_solve ();
181
182   if (approx_stats_.count_i_)
183     *mlog << '\n' << _f ("approximated: %s", approx_stats_.str ()) << endl;
184   if (exact_stats_.count_i_)
185     *mlog << _f ("calculated exactly: %s", exact_stats_.str ()) << endl;
186   *mlog << _f ("time: %.2f seconds",  timer.read ()) << endl;
187
188   return h;
189 }
190
191 void
192 Break_algorithm::do_set_pscore ()
193 {
194
195 }