]> git.donarmstrong.com Git - lilypond.git/blob - lily/bezier.cc
Admin: run yearly grand-replace.
[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   1, 3, 3, 1
26 };
27
28 void
29 scale (vector<Offset> *array, Real x, Real y)
30 {
31   for (vsize i = 0; i < array->size (); i++)
32     {
33       (*array)[i][X_AXIS] = x * (*array)[i][X_AXIS];
34       (*array)[i][Y_AXIS] = y * (*array)[i][Y_AXIS];
35     }
36 }
37
38 void
39 rotate (vector<Offset> *array, Real phi)
40 {
41   Offset rot (complex_exp (Offset (0, phi)));
42   for (vsize i = 0; i < array->size (); i++)
43     (*array)[i] = complex_multiply (rot, (*array)[i]);
44 }
45
46 void
47 translate (vector<Offset> *array, Offset o)
48 {
49   for (vsize i = 0; i < array->size (); i++)
50     (*array)[i] += o;
51 }
52
53 /*
54   Formula of the bezier 3-spline
55
56   sum_{j = 0}^3 (3 over j) z_j (1-t)^ (3-j)  t^j
57
58
59   A is the axis of X coordinate.
60 */
61
62 Real
63 Bezier::get_other_coordinate (Axis a, Real x) const
64 {
65   Axis other = Axis ((a +1) % NO_AXES);
66   vector<Real> ts = solve_point (a, x);
67
68   if (ts.size () == 0)
69     {
70       programming_error ("no solution found for Bezier intersection");
71       return 0.0;
72     }
73
74 #ifdef PARANOID
75   Offset c = curve_point (ts[0]);
76   if (fabs (c[a] - x) > 1e-8)
77     programming_error ("bezier intersection not correct?");
78 #endif
79
80   return curve_coordinate (ts[0], other);
81 }
82
83 Real
84 Bezier::curve_coordinate (Real t, Axis a) const
85 {
86   Real tj = 1;
87   Real one_min_tj[4];
88   one_min_tj[0] = 1;
89   for (int i = 1; i < 4; i++)
90     one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
91
92   Real r = 0.0;
93   for (int j = 0; j < 4; j++)
94     {
95       r += control_[j][a] * binomial_coefficient_3[j]
96         * tj * one_min_tj[3 - j];
97
98       tj *= t;
99     }
100
101   return r;
102 }
103
104 Offset
105 Bezier::curve_point (Real t) const
106 {
107   Real tj = 1;
108   Real one_min_tj[4];
109   one_min_tj[0] = 1;
110   for (int i = 1; i < 4; i++)
111     one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
112
113   Offset o;
114   for (int j = 0; j < 4; j++)
115     {
116       o += control_[j] * binomial_coefficient_3[j]
117         * tj * one_min_tj[3 - j];
118
119       tj *= t;
120     }
121
122 #ifdef PARANOID
123   assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t)) < 1e-8);
124   assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t)) < 1e-8);
125 #endif
126
127   return o;
128 }
129
130 /*
131   Cache binom (3, j) t^j (1-t)^{3-j}
132 */
133 struct Polynomial_cache {
134   Polynomial terms_[4];
135   Polynomial_cache ()
136   {
137     for (int j = 0; j <= 3; j++)
138       terms_[j]
139         = binomial_coefficient_3[j]
140         * Polynomial::power (j, Polynomial (0, 1))
141         * Polynomial::power (3 - j, Polynomial (1, -1));
142   }
143 };
144
145 static Polynomial_cache poly_cache;
146
147 Polynomial
148 Bezier::polynomial (Axis a) const
149 {
150   Polynomial p (0.0);
151   Polynomial q;
152   for (int j = 0; j <= 3; j++)
153     {
154       q = poly_cache.terms_[j];
155       q *= control_[j][a];
156       p += q;
157     }
158
159   return p;
160 }
161
162 /**
163    Remove all numbers outside [0, 1] from SOL
164 */
165 vector<Real>
166 filter_solutions (vector<Real> sol)
167 {
168   for (vsize i = sol.size (); i--;)
169     if (sol[i] < 0 || sol[i] > 1)
170       sol.erase (sol.begin () + i);
171   return sol;
172 }
173
174 /**
175    find t such that derivative is proportional to DERIV
176 */
177 vector<Real>
178 Bezier::solve_derivative (Offset deriv) const
179 {
180   Polynomial xp = polynomial (X_AXIS);
181   Polynomial yp = polynomial (Y_AXIS);
182   xp.differentiate ();
183   yp.differentiate ();
184
185   Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
186
187   return filter_solutions (combine.solve ());
188 }
189
190 /*
191   Find t such that curve_point (t)[AX] == COORDINATE
192 */
193 vector<Real>
194 Bezier::solve_point (Axis ax, Real coordinate) const
195 {
196   Polynomial p (polynomial (ax));
197   p.coefs_[0] -= coordinate;
198
199   vector<Real> sol (p.solve ());
200   return filter_solutions (sol);
201 }
202
203 /**
204    Compute the bounding box dimensions in direction of A.
205 */
206 Interval
207 Bezier::extent (Axis a) const
208 {
209   int o = (a + 1)%NO_AXES;
210   Offset d;
211   d[Axis (o)] = 1.0;
212   Interval iv;
213   vector<Real> sols (solve_derivative (d));
214   sols.push_back (1.0);
215   sols.push_back (0.0);
216   for (vsize i = sols.size (); i--;)
217     {
218       Offset o (curve_point (sols[i]));
219       iv.unite (Interval (o[a], o[a]));
220     }
221   return iv;
222 }
223
224 Interval
225 Bezier::control_point_extent (Axis a) const
226 {
227   Interval ext;
228   for (int i = CONTROL_COUNT; i--;)
229     ext.add_point (control_[i][a]);
230
231   return ext;      
232 }
233
234
235 /**
236    Flip around axis A
237 */
238 void
239 Bezier::scale (Real x, Real y)
240 {
241   for (int i = CONTROL_COUNT; i--;)
242     {
243       control_[i][X_AXIS] = x * control_[i][X_AXIS];
244       control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
245     }
246 }
247
248 void
249 Bezier::rotate (Real phi)
250 {
251   Offset rot (complex_exp (Offset (0, phi)));
252   for (int i = 0; i < CONTROL_COUNT; i++)
253     control_[i] = complex_multiply (rot, control_[i]);
254 }
255
256 void
257 Bezier::translate (Offset o)
258 {
259   for (int i = 0; i < CONTROL_COUNT; i++)
260     control_[i] += o;
261 }
262
263 void
264 Bezier::assert_sanity () const
265 {
266   for (int i = 0; i < CONTROL_COUNT; i++)
267     assert (!isnan (control_[i].length ())
268             && !isinf (control_[i].length ()));
269 }
270
271 void
272 Bezier::reverse ()
273 {
274   Bezier b2;
275   for (int i = 0; i < CONTROL_COUNT; i++)
276     b2.control_[CONTROL_COUNT - i - 1] = control_[i];
277   *this = b2;
278 }
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 }