]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-configuration.cc
9cccd505b9daea335b02b10d2feeaf2053f4b649
[lilypond.git] / lily / tie-configuration.cc
1 /*
2   tie-configuration.cc -- implement Tie_configuration
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #include "tie-configuration.hh"
11
12 #include "warn.hh"
13 #include "tie-formatting-problem.hh"
14 #include "bezier.hh"
15
16 int
17 Tie_configuration::compare (Tie_configuration const &a,
18                             Tie_configuration const &b)
19 {
20   if (a.position_ - b.position_)
21     return sign (a.position_ - b.position_);
22   return sign (a.dir_ - b.dir_);
23 }
24                             
25
26 Tie_configuration::Tie_configuration ()
27 {
28   dir_ = CENTER;
29   position_ = 0;
30   delta_y_ = 0.0;
31   score_ = 0.0;
32   scored_ = false;
33   column_ranks_ = Drul_array<int> (0, 0);
34 }
35
36
37 void
38 Tie_configuration::center_tie_vertically (Tie_details const &details)
39 {
40   Bezier b = get_untransformed_bezier (details);
41   Offset middle = b.curve_point (0.5);
42   Offset edge = b.curve_point (0.0);
43   Real center = (edge[Y_AXIS] + middle[Y_AXIS])/2.0;
44
45   delta_y_ = - dir_ * center;
46 }
47
48
49 Bezier
50 Tie_configuration::get_transformed_bezier (Tie_details const &details) const
51 {
52   Bezier b (get_untransformed_bezier (details));
53
54   b.scale (1, dir_);
55   b.translate (Offset (attachment_x_[LEFT],
56                        delta_y_ + details.staff_space_ * 0.5 * position_));
57
58   return b;
59 }
60
61 /*
62   Get bezier with left control at (0,0)
63  */
64 Bezier
65 Tie_configuration::get_untransformed_bezier (Tie_details const &details) const
66 {
67   Real l = attachment_x_.length ();
68   if (isinf (l) || isnan (l))
69     {
70       programming_error ("Inf or NaN encountered");
71       l = 1.0;
72     }
73   return slur_shape (l,
74                      details.height_limit_,
75                      details.ratio_);
76 }
77
78 int
79 Tie_configuration::column_span_length () const
80 {
81   return column_ranks_[RIGHT] - column_ranks_[LEFT];
82 }
83
84 Real
85 Tie_configuration::distance (Tie_configuration const &a,
86                              Tie_configuration const &b)
87 {
88
89   Real d = 3 * (a.position_ - b.position_);
90   if (d < 0)
91     return d + (2 + (b.dir_ - a.dir_));
92   else
93     return d + (2 + (a.dir_ - b.dir_));
94 }
95
96
97 void
98 Tie_configuration::add_score (Real s, string desc)
99 {
100   assert (!scored_);
101   score_ += s;
102   if (s)
103     score_card_ += to_string ("%s=%.2f ", desc.c_str (), s);
104 }
105
106 Real
107 Tie_configuration::height (Tie_details const &details) const
108 {
109   Real l = attachment_x_.length ();
110
111   return slur_shape (l,
112                      details.height_limit_,
113                      details.ratio_).curve_point (0.5)[Y_AXIS]; 
114 }
115
116 Ties_configuration::Ties_configuration ()
117 {
118   score_ = 0.0;
119   scored_ = false;
120 }
121
122 void
123 Ties_configuration::reset_score ()
124 {
125   score_ = 0.0;
126   scored_ = false;
127   score_card_ = "";
128   tie_score_cards_.clear ();
129 }
130
131 void
132 Ties_configuration::add_tie_score (Real s, int i, string desc)
133 {
134   assert (!scored_);
135   score_ += s;
136   if (s)
137     {
138       while (tie_score_cards_.size () < size ())
139         tie_score_cards_.push_back ("");
140
141       tie_score_cards_[i] += to_string ("%s=%.2f ", desc.c_str (), s);
142     }
143 }
144
145 void
146 Ties_configuration::add_score (Real s, string desc)
147 {
148   assert (!scored_);
149   score_ += s;
150   if (s)
151     score_card_ += to_string ("%s=%.2f ", desc.c_str (), s);
152 }
153
154 Real
155 Ties_configuration::score () const
156 {
157   return score_;
158 }
159
160
161 string
162 Ties_configuration::complete_tie_card (vsize i) const
163 {
164   string s;
165   s += to_string ("%d (%.2f) %c: ", (*this)[i].position_, (*this)[i].delta_y_,
166                   ((*this)[i].dir_ == UP ? 'u' : 'd'))
167     + (*this)[i].card () + (*this).tie_card (i);
168   
169   /*
170     this is a little awkward, but we must decide where to put
171     aggregrates.
172    */
173   if (i == 0)
174     s += card ();
175
176   if (i + 1 == size ())
177     s += to_string ("TOTAL=%.2f", score ());
178   
179   return s;
180 }
181
182 /* for use inside GDB */
183 string
184 Ties_configuration::complete_score_card () const
185 {
186   string s; 
187   for (vsize i = 0; i < size (); i++)
188     {
189       s += complete_tie_card (i);
190     }
191
192   return s;
193 }
194
195
196
197 string
198 Ties_configuration::card () const
199 {
200   return score_card_;
201 }
202