]> git.donarmstrong.com Git - lilypond.git/blob - lily/bezier.cc
* lily/system.cc (do_derived_mark): don't mark from object_alist_
[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   Offset c = curve_point (ts[0]);
79
80 #ifdef PARANOID
81   if (fabs (c[a] - x) > 1e-8)
82     programming_error ("bezier intersection not correct?");
83 #endif
84   
85   return c[other];
86 }
87
88 Offset
89 Bezier::curve_point (Real t) 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   Offset o;
100   for (int j = 0; j < 4; j++)
101     {
102       o += control_[j] * binomial_coefficient_3[j]
103         * tj * one_min_tj[3-j];
104
105       tj *= t;
106     }
107
108 #ifdef PARANOID
109   assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t)) < 1e-8);
110   assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t)) < 1e-8);
111 #endif
112
113   return o;
114 }
115
116 /*
117   Cache binom(3,j) t^j (1-t)^{3-j}
118 */
119 static struct Polynomial bezier_term_cache[4];
120 static bool  done_cache_init;
121
122 void
123 init_polynomial_cache ()
124 {
125   for (int j = 0; j <= 3;  j++)
126     bezier_term_cache[j] =
127       binomial_coefficient_3[j]
128       * Polynomial::power (j, Polynomial (0, 1))
129       * Polynomial::power (3 - j, Polynomial (1, -1));
130   done_cache_init = true;
131 }
132
133 Polynomial
134 Bezier::polynomial (Axis a) const
135 {
136   if (!done_cache_init)
137     init_polynomial_cache ();
138   
139   Polynomial p (0.0);
140   Polynomial q ;
141   for (int j = 0; j <= 3; j++)
142     {
143       q = bezier_term_cache[j];
144       q *= control_[j][a];
145       p += q;
146     }
147
148   return p;
149 }
150
151 /**
152    Remove all numbers outside [0, 1] from SOL
153 */
154 Array<Real>
155 filter_solutions (Array<Real> sol)
156 {
157   for (int i = sol.size (); i--;)
158     if (sol[i] < 0 || sol[i] > 1)
159       sol.del (i);
160   return sol;
161 }
162
163 /**
164    find t such that derivative is proportional to DERIV
165 */
166 Array<Real>
167 Bezier::solve_derivative (Offset deriv) const
168 {
169   Polynomial xp = polynomial (X_AXIS);
170   Polynomial yp = polynomial (Y_AXIS);
171   xp.differentiate ();
172   yp.differentiate ();
173
174   Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
175
176   return filter_solutions (combine.solve ());
177 }
178
179 /*
180   Find t such that curve_point (t)[AX] == COORDINATE
181 */
182 Array<Real>
183 Bezier::solve_point (Axis ax, Real coordinate) const
184 {
185   Polynomial p (polynomial (ax));
186   p.coefs_[0] -= coordinate;
187
188   Array<Real> sol (p.solve ());
189   return filter_solutions (sol);
190 }
191
192 /**
193    Compute the bounding box dimensions in direction of A.
194 */
195 Interval
196 Bezier::extent (Axis a) const
197 {
198   int o = (a + 1)%NO_AXES;
199   Offset d;
200   d[Axis (o)] = 1.0;
201   Interval iv;
202   Array<Real> sols (solve_derivative (d));
203   sols.push (1.0);
204   sols.push (0.0);
205   for (int i = sols.size (); i--;)
206     {
207       Offset o (curve_point (sols[i]));
208       iv.unite (Interval (o[a], o[a]));
209     }
210   return iv;
211 }
212
213 /**
214    Flip around axis A
215 */
216 void
217 Bezier::scale (Real x, Real y)
218 {
219   for (int i = CONTROL_COUNT; i--;)
220     {
221       control_[i][X_AXIS] = x * control_[i][X_AXIS];
222       control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
223     }
224 }
225
226 void
227 Bezier::rotate (Real phi)
228 {
229   Offset rot (complex_exp (Offset (0, phi)));
230   for (int i = 0; i < CONTROL_COUNT; i++)
231     control_[i] = complex_multiply (rot, control_[i]);
232 }
233
234 void
235 Bezier::translate (Offset o)
236 {
237   for (int i = 0; i < CONTROL_COUNT; i++)
238     control_[i] += o;
239 }
240
241 void
242 Bezier::assert_sanity () const
243 {
244   for (int i = 0; i < CONTROL_COUNT; i++)
245     assert (!isnan (control_[i].length ())
246             && !isinf (control_[i].length ()));
247 }
248
249 void
250 Bezier::reverse ()
251 {
252   Bezier b2;
253   for (int i = 0; i < CONTROL_COUNT; i++)
254     b2.control_[CONTROL_COUNT - i - 1] = control_[i];
255   *this = b2;
256 }