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