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