]> git.donarmstrong.com Git - lilypond.git/blob - lily/bezier.cc
* configure.in (--enable-std-vector): New option.
[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 (std::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 (std::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 (std::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   std::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 static struct Polynomial bezier_term_cache[4];
138 static bool done_cache_init;
139
140 void
141 init_polynomial_cache ()
142 {
143   for (int j = 0; j <= 3; j++)
144     bezier_term_cache[j]
145       = binomial_coefficient_3[j]
146       * Polynomial::power (j, Polynomial (0, 1))
147       * Polynomial::power (3 - j, Polynomial (1, -1));
148   done_cache_init = true;
149 }
150
151 Polynomial
152 Bezier::polynomial (Axis a) const
153 {
154   if (!done_cache_init)
155     init_polynomial_cache ();
156
157   Polynomial p (0.0);
158   Polynomial q;
159   for (int j = 0; j <= 3; j++)
160     {
161       q = bezier_term_cache[j];
162       q *= control_[j][a];
163       p += q;
164     }
165
166   return p;
167 }
168
169 /**
170    Remove all numbers outside [0, 1] from SOL
171 */
172 std::vector<Real>
173 filter_solutions (std::vector<Real> sol)
174 {
175   for (vsize i = sol.size (); i--;)
176     if (sol[i] < 0 || sol[i] > 1)
177       sol.del (i);
178   return sol;
179 }
180
181 /**
182    find t such that derivative is proportional to DERIV
183 */
184 std::vector<Real>
185 Bezier::solve_derivative (Offset deriv) const
186 {
187   Polynomial xp = polynomial (X_AXIS);
188   Polynomial yp = polynomial (Y_AXIS);
189   xp.differentiate ();
190   yp.differentiate ();
191
192   Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
193
194   return filter_solutions (combine.solve ());
195 }
196
197 /*
198   Find t such that curve_point (t)[AX] == COORDINATE
199 */
200 std::vector<Real>
201 Bezier::solve_point (Axis ax, Real coordinate) const
202 {
203   Polynomial p (polynomial (ax));
204   p.coefs_[0] -= coordinate;
205
206   std::vector<Real> sol (p.solve ());
207   return filter_solutions (sol);
208 }
209
210 /**
211    Compute the bounding box dimensions in direction of A.
212 */
213 Interval
214 Bezier::extent (Axis a) const
215 {
216   int o = (a + 1)%NO_AXES;
217   Offset d;
218   d[Axis (o)] = 1.0;
219   Interval iv;
220   std::vector<Real> sols (solve_derivative (d));
221   sols.push_back (1.0);
222   sols.push_back (0.0);
223   for (vsize i = sols.size (); i--;)
224     {
225       Offset o (curve_point (sols[i]));
226       iv.unite (Interval (o[a], o[a]));
227     }
228   return iv;
229 }
230
231 Interval
232 Bezier::control_point_extent (Axis a) const
233 {
234   Interval ext;
235   for (int i = CONTROL_COUNT; i--;)
236     ext.add_point (control_[i][a]);
237
238   return ext;      
239 }
240
241
242 /**
243    Flip around axis A
244 */
245 void
246 Bezier::scale (Real x, Real y)
247 {
248   for (int i = CONTROL_COUNT; i--;)
249     {
250       control_[i][X_AXIS] = x * control_[i][X_AXIS];
251       control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
252     }
253 }
254
255 void
256 Bezier::rotate (Real phi)
257 {
258   Offset rot (complex_exp (Offset (0, phi)));
259   for (int i = 0; i < CONTROL_COUNT; i++)
260     control_[i] = complex_multiply (rot, control_[i]);
261 }
262
263 void
264 Bezier::translate (Offset o)
265 {
266   for (int i = 0; i < CONTROL_COUNT; i++)
267     control_[i] += o;
268 }
269
270 void
271 Bezier::assert_sanity () const
272 {
273   for (int i = 0; i < CONTROL_COUNT; i++)
274     assert (!isnan (control_[i].length ())
275             && !isinf (control_[i].length ()));
276 }
277
278 void
279 Bezier::reverse ()
280 {
281   Bezier b2;
282   for (int i = 0; i < CONTROL_COUNT; i++)
283     b2.control_[CONTROL_COUNT - i - 1] = control_[i];
284   *this = b2;
285 }