]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-algorithm.cc
8291f81a2372de3c28679f6860dcfad85991a8fd
[lilypond.git] / lily / break-algorithm.cc
1 /*
2   break.cc -- implement Break_algorithm
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996,  1997--1999 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 "paper-score.hh"
16 #include "paper-column.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
51 Array<int>
52 Break_algorithm::find_break_indices () const
53 {
54   Line_of_cols all (pscore_l_->col_l_arr_);
55   Array<int> retval;
56
57   for (int i=0; i < all.size (); i++)
58     if (all[i]->breakable_b ())
59       retval.push (i);
60
61   if (linelength <=0)
62     while (retval.size () >2)
63       retval.del (1);
64
65   return retval;
66 }
67
68
69 Line_of_cols
70 Break_algorithm::find_breaks () const
71 {
72   Line_of_cols all (pscore_l_->col_l_arr_);
73   Line_of_cols retval;
74
75   for (int i=0; i < all.size (); i++)
76     if (all[i]->breakable_b ())
77       retval.push (all[i]);
78
79
80   if (linelength <=0)
81     while (retval.size () >2)
82       retval.del (1);
83
84   return retval;
85 }
86
87
88
89
90
91 Line_spacer*
92 Break_algorithm::generate_spacing_problem (Line_of_cols curline, Interval line) const
93 {
94   // ugh
95   Spring_spacer * sp= dynamic_cast<Spring_spacer*> ((*get_line_spacer) ());
96
97   sp->paper_l_ = pscore_l_->paper_l_;
98   sp->add_column (curline[0], true, line[LEFT]);
99   for (int i=1; i< curline.size ()-1; i++)
100     sp->add_column (curline[i]);
101
102   if (line.length () > 0)
103     {
104       sp->add_column (curline.top (), true, line[RIGHT]);
105       sp->energy_normalisation_f_  = sqr (line.length ());
106     }
107   else
108     sp->add_column (curline.top ());
109
110   sp->prepare ();
111   return sp;
112 }
113
114 Break_algorithm::Break_algorithm ()
115 {
116   pscore_l_ = 0;
117   get_line_spacer =0;
118   linelength = 0;
119 }
120
121 void
122 Break_algorithm::set_pscore (Paper_score*s)
123 {
124   pscore_l_ = s;
125   linelength = s->paper_l_->linewidth_f ();
126   do_set_pscore ();
127 }
128
129 bool
130 Break_algorithm::feasible (Line_of_cols curline) const
131 {
132   if (linelength <=  0)
133     return true;
134
135   for (int i=0; i < curline.size (); i++)
136     {
137       if (i && i < curline.size () -1
138           && ((dynamic_cast<Score_column*>(curline[i]))->break_penalty_i () >= Break_req::FORCE))
139         return false;
140     }
141   return true;
142 }
143
144 void
145 Break_algorithm::problem_OK () const
146 {
147   if (pscore_l_->col_l_arr_.empty ())
148     error (_("score does not have any columns"));
149   OK ();
150 }
151
152 void
153 Break_algorithm::OK () const
154 {
155 }
156
157 Array<Column_x_positions>
158 Break_algorithm::solve () const
159 {
160   Cpu_timer timer;
161
162   Array<Column_x_positions> h= do_solve ();
163
164   if (approx_stats_.count_i_)
165     *mlog << '\n' << _f ("approximated %s", approx_stats_.str ()) << endl;
166   if (exact_stats_.count_i_)
167     *mlog << _f ("calculated %s exactly", exact_stats_.str ()) << endl;
168   *mlog << _f ("elapsed time %.2f seconds",  timer.read ()) << endl;
169
170   return h;
171 }
172
173 void
174 Break_algorithm::do_set_pscore ()
175 {
176
177 }