2 slur-bezier-bow.cc -- implement Slur_bezier_bow
4 source file of the GNU LilyPond music typesetter
6 (c) 2000 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "paper-def.hh"
11 #include "slur-bezier-bow.hh"
14 Slur_bezier_bow::Slur_bezier_bow (Array<Offset> encompass, Direction dir,
21 encompass_ = encompass;
24 Real w = encompass_.top ()[X_AXIS] - encompass_[0][X_AXIS];
25 curve_ = slur_shape (w, h_inf, r_0);
29 Slur_bezier_bow::get_bezier () const
38 rv.translate (origin_);
44 Slur_bezier_bow::to_canonical_form ()
46 origin_ = encompass_[0];
47 translate (&encompass_, -origin_);
49 Offset delta = encompass_.top () - encompass_[0];
50 alpha_ = delta.arg ();
52 rotate (&encompass_, -alpha_);
55 scale (&encompass_, 1, -1);
58 while (encompass_.size () > 1 && encompass_[1][X_AXIS] <= 0.0)
60 programming_error ("Degenerate bow: infinite steepness reqd");
64 Real l = encompass_.top ()[X_AXIS];
65 while (encompass_.size () > 1 && encompass_.top (1)[X_AXIS] >= l)
67 programming_error ("Degenerate bow: infinite steepness reqd");
68 encompass_.del (encompass_.size ()-2);
75 Slur_bezier_bow::blow_fit ()
77 Real len = curve_.control_[3][X_AXIS];
78 Real h = curve_.control_[1][Y_AXIS] * fit_factor () / len;
79 curve_.control_[1][Y_AXIS] = h * len;
80 curve_.control_[2][Y_AXIS] = h * len;
81 curve_.assert_sanity ();
86 Slur_bezier_bow::enclosed_area_f () const
89 for (int i=0; i < encompass_.size (); i++)
95 x = Interval (0, encompass_[1][X_AXIS] / 2);
97 curve_.get_other_coordinate (X_AXIS,
101 else if (i == encompass_.size () - 1)
103 x = Interval ((encompass_[i-1][X_AXIS] + encompass_[i][X_AXIS])/2,
104 encompass_[i][X_AXIS]);
106 (curve_.get_other_coordinate (X_AXIS,
107 (x[MIN] + x[MAX]) / 2)));
111 x = Interval ((encompass_[i-1][X_AXIS] + encompass_[i][X_AXIS]) / 2,
112 (encompass_[i][X_AXIS] + encompass_[i+1][X_AXIS]) / 2);
113 y = Interval (encompass_[i][Y_AXIS],
114 (curve_.get_other_coordinate (X_AXIS, x[MIN])
115 + curve_.get_other_coordinate (X_AXIS,
116 (x[MIN] + x[MAX]) / 2)
117 + curve_.get_other_coordinate (X_AXIS, x[MAX])) / 3);
120 Real da = x.length () * y.length ();
127 Slur_bezier_bow::area_x_gradients_array (Real area)
129 Real len = curve_.control_[3][X_AXIS];
130 Real grow = len / 10.0;
132 for (int i=0; i < 2; i++)
134 Real r = curve_.control_[i+1][X_AXIS];
135 curve_.control_[i+1][X_AXIS] += grow;
136 da[i] = (enclosed_area_f () - area) / grow;
137 curve_.control_[i+1][X_AXIS] = r;
143 ugh, should have another look, and use a regular optimization
144 algorithm, instead of this homebrew.
147 Slur_bezier_bow::minimise_enclosed_area (Paper_def* paper_l, Real beauty)
149 Real length = curve_.control_[3][X_AXIS];
150 Real beautiful = beauty * length * slur_height (length, h_inf_, r_0_);
152 DEBUG_OUT << to_str ("Beautiful: %f\n", beautiful);
153 DEBUG_OUT << to_str ("Length: %f\n", length);
154 // DEBUG_OUT << to_str ("D-height: %f\n", default_height);
155 DEBUG_OUT << to_str ("FitFac: %f\n", fit_factor ());
157 if (fit_factor () > 1.0)
160 Real pct_c0 = paper_l->get_var ("bezier_pct_c0");
161 Real pct_c3 = paper_l->get_var ("bezier_pct_c3");
162 Real pct_in_max = paper_l->get_var ("bezier_pct_in_max");
163 Real pct_out_max = paper_l->get_var ("bezier_pct_out_max");
164 Real steps = paper_l->get_var ("bezier_area_steps");
166 for (int i=0; i < steps; i++)
168 Real area = enclosed_area_f ();
170 DEBUG_OUT << to_str ("Init area: %f\n", area);
172 if (area <= beautiful)
175 Array<Real> da = area_x_gradients_array (area);
178 Real pct = pct_c0 + pct_c3 * length * length * length;
179 pct *= (steps - i) / steps;
180 if (da[0] > 0 || da[1] < 0)
181 pct = pct <? pct_out_max;
183 pct = pct <? pct_in_max;
185 Real u = (abs (curve_.control_[1][X_AXIS] / da[0])
186 <? abs ((curve_.control_[3][X_AXIS]
187 - curve_.control_[2][X_AXIS]) / da[1]));
189 DEBUG_OUT << to_str ("pct: %f\n", pct);
190 DEBUG_OUT << to_str ("u: %f\n", u);
192 DEBUG_OUT << to_str ("da: (%f, %f)\n", da[0], da[1]);
193 DEBUG_OUT << to_str ("da*u: (%f, %f)\n", da[0]*u*pct, da[1]*u*pct);
194 DEBUG_OUT << to_str ("cx: (%f, %f)\n", curve_.control_[1][X_AXIS],
195 curve_.control_[2][X_AXIS]);
197 curve_.control_[1][X_AXIS] -= da[0] * u * pct;
198 curve_.control_[2][X_AXIS] -= da[1] * u * pct;
201 Real area = enclosed_area_f ();
202 DEBUG_OUT << to_str ("Exarea: %f\n", area);
208 max ( encompass.y / curve.y )
212 Slur_bezier_bow::fit_factor () const
214 Real x1 = encompass_[0][X_AXIS];
215 Real x2 = encompass_.top ()[X_AXIS];
218 for (int i=1; i < encompass_.size ()-1; i++)
220 if (encompass_[i][X_AXIS] > x1 && encompass_[i][X_AXIS] < x2)
222 Real y = curve_.get_other_coordinate (X_AXIS, encompass_[i][X_AXIS]);
225 Real f = encompass_[i][Y_AXIS] / y;
226 factor = factor >? f;