]> git.donarmstrong.com Git - lilypond.git/blob - lily/bezier.cc
Update dashed slurs to have variable thickness.
[lilypond.git] / lily / bezier.cc
1 /*
2   bezier.cc -- implement Bezier and Bezier_bow
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2009 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "bezier.hh"
10 #include "warn.hh"
11 #include "libc-extension.hh"
12
13 Real binomial_coefficient_3[] = {
14   1, 3, 3, 1
15 };
16
17 void
18 scale (vector<Offset> *array, Real x, Real y)
19 {
20   for (vsize i = 0; i < array->size (); i++)
21     {
22       (*array)[i][X_AXIS] = x * (*array)[i][X_AXIS];
23       (*array)[i][Y_AXIS] = y * (*array)[i][Y_AXIS];
24     }
25 }
26
27 void
28 rotate (vector<Offset> *array, Real phi)
29 {
30   Offset rot (complex_exp (Offset (0, phi)));
31   for (vsize i = 0; i < array->size (); i++)
32     (*array)[i] = complex_multiply (rot, (*array)[i]);
33 }
34
35 void
36 translate (vector<Offset> *array, Offset o)
37 {
38   for (vsize i = 0; i < array->size (); i++)
39     (*array)[i] += o;
40 }
41
42 /*
43   Formula of the bezier 3-spline
44
45   sum_{j = 0}^3 (3 over j) z_j (1-t)^ (3-j)  t^j
46
47
48   A is the axis of X coordinate.
49 */
50
51 Real
52 Bezier::get_other_coordinate (Axis a, Real x) const
53 {
54   Axis other = Axis ((a +1) % NO_AXES);
55   vector<Real> ts = solve_point (a, x);
56
57   if (ts.size () == 0)
58     {
59       programming_error ("no solution found for Bezier intersection");
60       return 0.0;
61     }
62
63 #ifdef PARANOID
64   Offset c = curve_point (ts[0]);
65   if (fabs (c[a] - x) > 1e-8)
66     programming_error ("bezier intersection not correct?");
67 #endif
68
69   return curve_coordinate (ts[0], other);
70 }
71
72 Real
73 Bezier::curve_coordinate (Real t, Axis a) const
74 {
75   Real tj = 1;
76   Real one_min_tj[4];
77   one_min_tj[0] = 1;
78   for (int i = 1; i < 4; i++)
79     one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
80
81   Real r = 0.0;
82   for (int j = 0; j < 4; j++)
83     {
84       r += control_[j][a] * binomial_coefficient_3[j]
85         * tj * one_min_tj[3 - j];
86
87       tj *= t;
88     }
89
90   return r;
91 }
92
93 Offset
94 Bezier::curve_point (Real t) const
95 {
96   Real tj = 1;
97   Real one_min_tj[4];
98   one_min_tj[0] = 1;
99   for (int i = 1; i < 4; i++)
100     one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
101
102   Offset o;
103   for (int j = 0; j < 4; j++)
104     {
105       o += control_[j] * binomial_coefficient_3[j]
106         * tj * one_min_tj[3 - j];
107
108       tj *= t;
109     }
110
111 #ifdef PARANOID
112   assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t)) < 1e-8);
113   assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t)) < 1e-8);
114 #endif
115
116   return o;
117 }
118
119 /*
120   Cache binom (3, j) t^j (1-t)^{3-j}
121 */
122 struct Polynomial_cache {
123   Polynomial terms_[4];
124   Polynomial_cache ()
125   {
126     for (int j = 0; j <= 3; j++)
127       terms_[j]
128         = binomial_coefficient_3[j]
129         * Polynomial::power (j, Polynomial (0, 1))
130         * Polynomial::power (3 - j, Polynomial (1, -1));
131   }
132 };
133
134 static Polynomial_cache poly_cache;
135
136 Polynomial
137 Bezier::polynomial (Axis a) const
138 {
139   Polynomial p (0.0);
140   Polynomial q;
141   for (int j = 0; j <= 3; j++)
142     {
143       q = poly_cache.terms_[j];
144       q *= control_[j][a];
145       p += q;
146     }
147
148   return p;
149 }
150
151 /**
152    Remove all numbers outside [0, 1] from SOL
153 */
154 vector<Real>
155 filter_solutions (vector<Real> sol)
156 {
157   for (vsize i = sol.size (); i--;)
158     if (sol[i] < 0 || sol[i] > 1)
159       sol.erase (sol.begin () + i);
160   return sol;
161 }
162
163 /**
164    find t such that derivative is proportional to DERIV
165 */
166 vector<Real>
167 Bezier::solve_derivative (Offset deriv) const
168 {
169   Polynomial xp = polynomial (X_AXIS);
170   Polynomial yp = polynomial (Y_AXIS);
171   xp.differentiate ();
172   yp.differentiate ();
173
174   Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
175
176   return filter_solutions (combine.solve ());
177 }
178
179 /*
180   Find t such that curve_point (t)[AX] == COORDINATE
181 */
182 vector<Real>
183 Bezier::solve_point (Axis ax, Real coordinate) const
184 {
185   Polynomial p (polynomial (ax));
186   p.coefs_[0] -= coordinate;
187
188   vector<Real> sol (p.solve ());
189   return filter_solutions (sol);
190 }
191
192 /**
193    Compute the bounding box dimensions in direction of A.
194 */
195 Interval
196 Bezier::extent (Axis a) const
197 {
198   int o = (a + 1)%NO_AXES;
199   Offset d;
200   d[Axis (o)] = 1.0;
201   Interval iv;
202   vector<Real> sols (solve_derivative (d));
203   sols.push_back (1.0);
204   sols.push_back (0.0);
205   for (vsize i = sols.size (); i--;)
206     {
207       Offset o (curve_point (sols[i]));
208       iv.unite (Interval (o[a], o[a]));
209     }
210   return iv;
211 }
212
213 Interval
214 Bezier::control_point_extent (Axis a) const
215 {
216   Interval ext;
217   for (int i = CONTROL_COUNT; i--;)
218     ext.add_point (control_[i][a]);
219
220   return ext;      
221 }
222
223
224 /**
225    Flip around axis A
226 */
227 void
228 Bezier::scale (Real x, Real y)
229 {
230   for (int i = CONTROL_COUNT; i--;)
231     {
232       control_[i][X_AXIS] = x * control_[i][X_AXIS];
233       control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
234     }
235 }
236
237 void
238 Bezier::rotate (Real phi)
239 {
240   Offset rot (complex_exp (Offset (0, phi)));
241   for (int i = 0; i < CONTROL_COUNT; i++)
242     control_[i] = complex_multiply (rot, control_[i]);
243 }
244
245 void
246 Bezier::translate (Offset o)
247 {
248   for (int i = 0; i < CONTROL_COUNT; i++)
249     control_[i] += o;
250 }
251
252 void
253 Bezier::assert_sanity () const
254 {
255   for (int i = 0; i < CONTROL_COUNT; i++)
256     assert (!isnan (control_[i].length ())
257             && !isinf (control_[i].length ()));
258 }
259
260 void
261 Bezier::reverse ()
262 {
263   Bezier b2;
264   for (int i = 0; i < CONTROL_COUNT; i++)
265     b2.control_[CONTROL_COUNT - i - 1] = control_[i];
266   *this = b2;
267 }
268
269
270 /*
271   Subdivide a bezier at T into LEFT_PART and RIGHT_PART
272 */
273 void
274 Bezier::subdivide (Real t, Bezier &left_part, Bezier &right_part)
275 {
276   Offset b2[3];
277   Offset b1[2];
278   Offset b0;
279   for (int i = 0; i < 3; i++)
280     b2[i] = control_[i] + t * (control_[i+1] - control_[i]);
281   for (int i = 0; i < 2; i++)
282     b1[i] = b2[i] + t * (b2[i+1] - b2[i]);
283   b0 = b1[0] + t * (b1[1] - b1[0]);
284   left_part.control_[0] = control_[0];
285   left_part.control_[1] = b2[0];
286   left_part.control_[2] = b1[0];
287   left_part.control_[3] = b0;
288   right_part.control_[0] = b0;
289   right_part.control_[1] = b1[1];
290   right_part.control_[2] = b2[2];
291   right_part.control_[3] = control_[3];
292 }
293
294 /*
295   Extract a portion of a bezier from T_MIN to T_MAX
296 */
297
298 Bezier
299 Bezier::extract (Real t_min, Real t_max)
300 {
301   Bezier bez1, bez2, bez3, bez4;
302   if (t_min == 0.0)
303     {
304       for (int i = 0; i < CONTROL_COUNT; i++)
305         bez2.control_[i] = control_[i];
306     }
307   else
308     {
309       subdivide (t_min, bez1, bez2);
310     }
311   if (t_max == 1.0)
312     {
313       return bez2;
314     }
315   else
316    {
317      bez2.subdivide ((t_max-t_min)/(1-t_min), bez3, bez4);
318      return bez3;
319   }
320 }