]> git.donarmstrong.com Git - lilypond.git/blob - lily/bezier.cc
* lily/context.cc (where_defined): also assign value in
[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 <math.h>
10
11 #include "bezier.hh"
12 #include "warn.hh"
13 #include "libc-extension.hh"
14
15 Real binomial_coefficient_3[] = {1,3 ,3, 1};
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 (Array<Offset> *array, Real x, Real y)
34 {
35   for (int 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 (Array<Offset> *array, Real phi)
44 {
45   Offset rot (complex_exp (Offset (0, phi)));
46   for (int i = 0; i < array->size (); i++)
47     (*array)[i] = complex_multiply (rot, (*array)[i]);
48 }
49
50 void
51 translate (Array<Offset> *array, Offset o)
52 {
53   for (int 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   Array<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
79 #ifdef PARANOID
80   Offset c = curve_point (ts[0]);
81   if (fabs (c[a] - x) > 1e-8)
82     programming_error ("bezier intersection not correct?");
83 #endif
84
85   return curve_coordinate (ts[0], other);
86 }
87
88 Real
89 Bezier::curve_coordinate (Real t, Axis a) const
90 {
91   Real tj = 1;
92   Real one_min_tj[4];
93   one_min_tj[0] = 1;
94   for (int i = 1; i < 4; i++)
95     {
96       one_min_tj[i] = one_min_tj[i-1] * (1-t);
97     }
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     {
119       one_min_tj[i] = one_min_tj[i-1] * (1-t);
120     }
121
122   Offset o;
123   for (int j = 0; j < 4; j++)
124     {
125       o += control_[j] * binomial_coefficient_3[j]
126         * tj * one_min_tj[3-j];
127
128       tj *= t;
129     }
130
131 #ifdef PARANOID
132   assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t)) < 1e-8);
133   assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t)) < 1e-8);
134 #endif
135
136   return o;
137 }
138
139 /*
140   Cache binom(3,j) t^j (1-t)^{3-j}
141 */
142 static struct Polynomial bezier_term_cache[4];
143 static bool  done_cache_init;
144
145 void
146 init_polynomial_cache ()
147 {
148   for (int j = 0; j <= 3;  j++)
149     bezier_term_cache[j] =
150       binomial_coefficient_3[j]
151       * Polynomial::power (j, Polynomial (0, 1))
152       * Polynomial::power (3 - j, Polynomial (1, -1));
153   done_cache_init = true;
154 }
155
156 Polynomial
157 Bezier::polynomial (Axis a) const
158 {
159   if (!done_cache_init)
160     init_polynomial_cache ();
161   
162   Polynomial p (0.0);
163   Polynomial q ;
164   for (int j = 0; j <= 3; j++)
165     {
166       q = bezier_term_cache[j];
167       q *= control_[j][a];
168       p += q;
169     }
170
171   return p;
172 }
173
174 /**
175    Remove all numbers outside [0, 1] from SOL
176 */
177 Array<Real>
178 filter_solutions (Array<Real> sol)
179 {
180   for (int i = sol.size (); i--;)
181     if (sol[i] < 0 || sol[i] > 1)
182       sol.del (i);
183   return sol;
184 }
185
186 /**
187    find t such that derivative is proportional to DERIV
188 */
189 Array<Real>
190 Bezier::solve_derivative (Offset deriv) const
191 {
192   Polynomial xp = polynomial (X_AXIS);
193   Polynomial yp = polynomial (Y_AXIS);
194   xp.differentiate ();
195   yp.differentiate ();
196
197   Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
198
199   return filter_solutions (combine.solve ());
200 }
201
202 /*
203   Find t such that curve_point (t)[AX] == COORDINATE
204 */
205 Array<Real>
206 Bezier::solve_point (Axis ax, Real coordinate) const
207 {
208   Polynomial p (polynomial (ax));
209   p.coefs_[0] -= coordinate;
210
211   Array<Real> sol (p.solve ());
212   return filter_solutions (sol);
213 }
214
215 /**
216    Compute the bounding box dimensions in direction of A.
217 */
218 Interval
219 Bezier::extent (Axis a) const
220 {
221   int o = (a + 1)%NO_AXES;
222   Offset d;
223   d[Axis (o)] = 1.0;
224   Interval iv;
225   Array<Real> sols (solve_derivative (d));
226   sols.push (1.0);
227   sols.push (0.0);
228   for (int i = sols.size (); i--;)
229     {
230       Offset o (curve_point (sols[i]));
231       iv.unite (Interval (o[a], o[a]));
232     }
233   return iv;
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 }