]> git.donarmstrong.com Git - lilypond.git/blob - lily/bezier.cc
Web-ja: update introduction
[lilypond.git] / lily / bezier.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2015 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 {
27   1, 3, 3, 1
28 };
29
30 void
31 scale (vector<Offset> *array, Real x, Real y)
32 {
33   for (vsize i = 0; i < array->size (); i++)
34     {
35       (*array)[i][X_AXIS] = x * (*array)[i][X_AXIS];
36       (*array)[i][Y_AXIS] = y * (*array)[i][Y_AXIS];
37     }
38 }
39
40 void
41 rotate (vector<Offset> *array, Real deg)
42 {
43   Offset rot (offset_directed (deg));
44   for (vsize i = 0; i < array->size (); i++)
45     (*array)[i] = complex_multiply (rot, (*array)[i]);
46 }
47
48 void
49 translate (vector<Offset> *array, Offset o)
50 {
51   for (vsize i = 0; i < array->size (); i++)
52     (*array)[i] += o;
53 }
54
55 /*
56   Formula of the bezier 3-spline
57
58   sum_{j = 0}^3 (3 over j) z_j (1-t)^ (3-j)  t^j
59
60
61   A is the axis of X coordinate.
62 */
63
64 Real
65 Bezier::get_other_coordinate (Axis a, Real x) const
66 {
67   Axis other = Axis ((a + 1) % NO_AXES);
68   vector<Real> ts = solve_point (a, x);
69
70   if (ts.size () == 0)
71     {
72       programming_error ("no solution found for Bezier intersection");
73       return 0.0;
74     }
75
76 #ifdef PARANOID
77   Offset c = curve_point (ts[0]);
78   if (fabs (c[a] - x) > 1e-8)
79     programming_error ("bezier intersection not correct?");
80 #endif
81
82   return curve_coordinate (ts[0], other);
83 }
84
85 vector<Real>
86 Bezier::get_other_coordinates (Axis a, Real x) const
87 {
88   Axis other = other_axis (a);
89   vector<Real> ts = solve_point (a, x);
90   vector<Real> sols;
91   for (vsize i = 0; i < ts.size (); i++)
92     sols.push_back (curve_coordinate (ts[i], other));
93   return sols;
94 }
95
96 Real
97 Bezier::curve_coordinate (Real t, Axis a) const
98 {
99   Real tj = 1;
100   Real one_min_tj[4];
101   one_min_tj[0] = 1;
102   for (int i = 1; i < 4; i++)
103     one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
104
105   Real r = 0.0;
106   for (int j = 0; j < 4; j++)
107     {
108       r += control_[j][a] * binomial_coefficient_3[j]
109            * tj * one_min_tj[3 - j];
110
111       tj *= t;
112     }
113
114   return r;
115 }
116
117 Offset
118 Bezier::curve_point (Real t) const
119 {
120   Real tj = 1;
121   Real one_min_tj[4];
122   one_min_tj[0] = 1;
123   for (int i = 1; i < 4; i++)
124     one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
125
126   Offset o;
127   for (int j = 0; j < 4; j++)
128     {
129       o += control_[j] * binomial_coefficient_3[j]
130            * tj * one_min_tj[3 - j];
131
132       tj *= t;
133     }
134
135 #ifdef PARANOID
136   assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t)) < 1e-8);
137   assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t)) < 1e-8);
138 #endif
139
140   return o;
141 }
142
143 // The return value is normalized unless zero or indefinite.
144 Offset
145 Bezier::dir_at_point (Real t) const
146 {
147   Offset second_order[3];
148   Offset third_order[2];
149
150   for (vsize i = 0; i < 3; i++)
151     second_order[i] = ((control_[i + 1] - control_[i]) * t) + control_[i];
152
153   for (vsize i = 0; i < 2; i++)
154     third_order[i] = ((second_order[i + 1] - second_order[i]) * t) + second_order[i];
155
156   return (third_order[1] - third_order[0]).direction ();
157 }
158
159 /*
160   Cache binom (3, j) t^j (1-t)^{3-j}
161 */
162 struct Polynomial_cache
163 {
164   Polynomial terms_[4];
165   Polynomial_cache ()
166   {
167     for (int j = 0; j <= 3; j++)
168       terms_[j]
169         = binomial_coefficient_3[j]
170           * Polynomial::power (j, Polynomial (0, 1))
171           * Polynomial::power (3 - j, Polynomial (1, -1));
172   }
173 };
174
175 static Polynomial_cache poly_cache;
176
177 Polynomial
178 Bezier::polynomial (Axis a) const
179 {
180   Polynomial p (0.0);
181   Polynomial q;
182   for (int j = 0; j <= 3; j++)
183     {
184       q = poly_cache.terms_[j];
185       q *= control_[j][a];
186       p += q;
187     }
188
189   return p;
190 }
191
192 /**
193    Remove all numbers outside [0, 1] from SOL
194 */
195 vector<Real>
196 filter_solutions (vector<Real> sol)
197 {
198   for (vsize i = sol.size (); i--;)
199     if (sol[i] < 0 || sol[i] > 1)
200       sol.erase (sol.begin () + i);
201   return sol;
202 }
203
204 /**
205    find t such that derivative is proportional to DERIV
206 */
207 vector<Real>
208 Bezier::solve_derivative (Offset deriv) const
209 {
210   Polynomial xp = polynomial (X_AXIS);
211   Polynomial yp = polynomial (Y_AXIS);
212   xp.differentiate ();
213   yp.differentiate ();
214
215   Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
216
217   return filter_solutions (combine.solve ());
218 }
219
220 /*
221   Find t such that curve_point (t)[AX] == COORDINATE
222 */
223 vector<Real>
224 Bezier::solve_point (Axis ax, Real coordinate) const
225 {
226   Polynomial p (polynomial (ax));
227   p.coefs_[0] -= coordinate;
228
229   vector<Real> sol (p.solve ());
230   return filter_solutions (sol);
231 }
232
233 /**
234    For the portion of the curve between L and R along axis AX,
235    return the bounding box limit in direction D along the cross axis to AX.
236    If there is no portion between L and R, return 0.0 and report error.
237 */
238 Real
239 Bezier::minmax (Axis ax, Real l, Real r, Direction d) const
240 {
241   Axis bx = other_axis (ax);
242
243   // The curve could hit its bounding box limit along BX at:
244   //  points where the curve is parallel to AX,
245   Offset vec (0.0, 0.0);
246   vec[ax] = 1.0;
247   vector<Real> sols (solve_derivative (vec));
248   //  or endpoints of the curve,
249   sols.push_back (0.999);
250   sols.push_back (0.001);
251   // (using points just inside the ends, so that an endpoint is evaulated
252   //  if it falls within rounding error of L or R and the curve lies inside)
253
254   Interval iv;
255   for (vsize i = sols.size (); i--;)
256     {
257       Offset p (curve_point (sols[i]));
258       if (p[ax] >= l && p[ax] <= r)
259         iv.add_point (p[bx]);
260     }
261
262   //  or intersections of the curve with the bounding lines at L and R.
263   Interval lr (l, r);
264   for (LEFT_and_RIGHT (dir))
265     {
266       vector<Real> v = get_other_coordinates (ax, lr[dir]);
267       for (vsize i = v.size (); i--;)
268         iv.add_point (v[i]);
269     }
270
271   if (iv.is_empty ())
272     {
273       programming_error ("Bezier curve does not cross region of concern");
274       return 0.0;
275     }
276
277   return iv.at (d);
278 }
279
280 /**
281    Compute the bounding box dimensions in direction of A.
282 */
283 Interval
284 Bezier::extent (Axis a) const
285 {
286   int o = (a + 1) % NO_AXES;
287   Offset d;
288   d[Axis (o)] = 1.0;
289   Interval iv;
290   vector<Real> sols (solve_derivative (d));
291   sols.push_back (1.0);
292   sols.push_back (0.0);
293   for (vsize i = sols.size (); i--;)
294     {
295       Offset o (curve_point (sols[i]));
296       iv.unite (Interval (o[a], o[a]));
297     }
298   return iv;
299 }
300
301 Interval
302 Bezier::control_point_extent (Axis a) const
303 {
304   Interval ext;
305   for (int i = CONTROL_COUNT; i--;)
306     ext.add_point (control_[i][a]);
307
308   return ext;
309 }
310
311 /**
312    Flip around axis A
313 */
314 void
315 Bezier::scale (Real x, Real y)
316 {
317   for (int i = CONTROL_COUNT; i--;)
318     {
319       control_[i][X_AXIS] = x * control_[i][X_AXIS];
320       control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
321     }
322 }
323
324 void
325 Bezier::rotate (Real deg)
326 {
327   Offset rot (offset_directed (deg));
328   for (int i = 0; i < CONTROL_COUNT; i++)
329     control_[i] = complex_multiply (rot, control_[i]);
330 }
331
332 void
333 Bezier::translate (Offset o)
334 {
335   for (int i = 0; i < CONTROL_COUNT; i++)
336     control_[i] += o;
337 }
338
339 void
340 Bezier::assert_sanity () const
341 {
342   for (int i = 0; i < CONTROL_COUNT; i++)
343     assert (!isnan (control_[i].length ())
344             && !isinf (control_[i].length ()));
345 }
346
347 void
348 Bezier::reverse ()
349 {
350   Bezier b2;
351   for (int i = 0; i < CONTROL_COUNT; i++)
352     b2.control_[CONTROL_COUNT - i - 1] = control_[i];
353   *this = b2;
354 }
355
356 /*
357   Subdivide a bezier at T into LEFT_PART and RIGHT_PART
358   using deCasteljau's algorithm.
359 */
360 void
361 Bezier::subdivide (Real t, Bezier *left_part, Bezier *right_part) const
362 {
363   Offset p[CONTROL_COUNT][CONTROL_COUNT];
364
365   for (int i = 0; i < CONTROL_COUNT; i++)
366     p[i][CONTROL_COUNT - 1 ] = control_[i];
367   for (int j = CONTROL_COUNT - 2; j >= 0; j--)
368     for (int i = 0; i < CONTROL_COUNT - 1; i++)
369       p[i][j] = p[i][j + 1] + t * (p[i + 1][j + 1] - p[i][j + 1]);
370   for (int i = 0; i < CONTROL_COUNT; i++)
371     {
372       left_part->control_[i] = p[0][CONTROL_COUNT - 1 - i];
373       right_part->control_[i] = p[i][i];
374     }
375 }
376
377 /*
378   Extract a portion of a bezier from T_MIN to T_MAX
379 */
380
381 Bezier
382 Bezier::extract (Real t_min, Real t_max) const
383 {
384   if ((t_min < 0) || (t_max) > 1)
385     programming_error
386     ("bezier extract arguments outside of limits: curve may have bad shape");
387   if (t_min >= t_max)
388     programming_error
389     ("lower bezier extract value not less than upper value: curve may have bad shape");
390   Bezier bez1, bez2, bez3, bez4;
391   if (t_min == 0.0)
392     bez2 = *this;
393   else
394     subdivide (t_min, &bez1, &bez2);
395   if (t_max == 1.0)
396     return bez2;
397   else
398     {
399       bez2.subdivide ((t_max - t_min) / (1 - t_min), &bez3, &bez4);
400       return bez3;
401     }
402 }