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