Use scalar instead of embedded_scm for context mod overrides.
[lilypond/mpolesky.git] / lily / bezier.cc
blobb341d73754b35ccf5828c9f2372ea65847e5878c
1 /*
2 bezier.cc -- implement Bezier and Bezier_bow
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2009 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
9 #include "bezier.hh"
10 #include "warn.hh"
11 #include "libc-extension.hh"
13 Real binomial_coefficient_3[] = {
14 1, 3, 3, 1
17 void
18 scale (vector<Offset> *array, Real x, Real y)
20 for (vsize i = 0; i < array->size (); i++)
22 (*array)[i][X_AXIS] = x * (*array)[i][X_AXIS];
23 (*array)[i][Y_AXIS] = y * (*array)[i][Y_AXIS];
27 void
28 rotate (vector<Offset> *array, Real phi)
30 Offset rot (complex_exp (Offset (0, phi)));
31 for (vsize i = 0; i < array->size (); i++)
32 (*array)[i] = complex_multiply (rot, (*array)[i]);
35 void
36 translate (vector<Offset> *array, Offset o)
38 for (vsize i = 0; i < array->size (); i++)
39 (*array)[i] += o;
43 Formula of the bezier 3-spline
45 sum_{j = 0}^3 (3 over j) z_j (1-t)^ (3-j) t^j
48 A is the axis of X coordinate.
51 Real
52 Bezier::get_other_coordinate (Axis a, Real x) const
54 Axis other = Axis ((a +1) % NO_AXES);
55 vector<Real> ts = solve_point (a, x);
57 if (ts.size () == 0)
59 programming_error ("no solution found for Bezier intersection");
60 return 0.0;
63 #ifdef PARANOID
64 Offset c = curve_point (ts[0]);
65 if (fabs (c[a] - x) > 1e-8)
66 programming_error ("bezier intersection not correct?");
67 #endif
69 return curve_coordinate (ts[0], other);
72 Real
73 Bezier::curve_coordinate (Real t, Axis a) const
75 Real tj = 1;
76 Real one_min_tj[4];
77 one_min_tj[0] = 1;
78 for (int i = 1; i < 4; i++)
79 one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
81 Real r = 0.0;
82 for (int j = 0; j < 4; j++)
84 r += control_[j][a] * binomial_coefficient_3[j]
85 * tj * one_min_tj[3 - j];
87 tj *= t;
90 return r;
93 Offset
94 Bezier::curve_point (Real t) const
96 Real tj = 1;
97 Real one_min_tj[4];
98 one_min_tj[0] = 1;
99 for (int i = 1; i < 4; i++)
100 one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
102 Offset o;
103 for (int j = 0; j < 4; j++)
105 o += control_[j] * binomial_coefficient_3[j]
106 * tj * one_min_tj[3 - j];
108 tj *= t;
111 #ifdef PARANOID
112 assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t)) < 1e-8);
113 assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t)) < 1e-8);
114 #endif
116 return o;
120 Cache binom (3, j) t^j (1-t)^{3-j}
122 struct Polynomial_cache {
123 Polynomial terms_[4];
124 Polynomial_cache ()
126 for (int j = 0; j <= 3; j++)
127 terms_[j]
128 = binomial_coefficient_3[j]
129 * Polynomial::power (j, Polynomial (0, 1))
130 * Polynomial::power (3 - j, Polynomial (1, -1));
134 static Polynomial_cache poly_cache;
136 Polynomial
137 Bezier::polynomial (Axis a) const
139 Polynomial p (0.0);
140 Polynomial q;
141 for (int j = 0; j <= 3; j++)
143 q = poly_cache.terms_[j];
144 q *= control_[j][a];
145 p += q;
148 return p;
152 Remove all numbers outside [0, 1] from SOL
154 vector<Real>
155 filter_solutions (vector<Real> sol)
157 for (vsize i = sol.size (); i--;)
158 if (sol[i] < 0 || sol[i] > 1)
159 sol.erase (sol.begin () + i);
160 return sol;
164 find t such that derivative is proportional to DERIV
166 vector<Real>
167 Bezier::solve_derivative (Offset deriv) const
169 Polynomial xp = polynomial (X_AXIS);
170 Polynomial yp = polynomial (Y_AXIS);
171 xp.differentiate ();
172 yp.differentiate ();
174 Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
176 return filter_solutions (combine.solve ());
180 Find t such that curve_point (t)[AX] == COORDINATE
182 vector<Real>
183 Bezier::solve_point (Axis ax, Real coordinate) const
185 Polynomial p (polynomial (ax));
186 p.coefs_[0] -= coordinate;
188 vector<Real> sol (p.solve ());
189 return filter_solutions (sol);
193 Compute the bounding box dimensions in direction of A.
195 Interval
196 Bezier::extent (Axis a) const
198 int o = (a + 1)%NO_AXES;
199 Offset d;
200 d[Axis (o)] = 1.0;
201 Interval iv;
202 vector<Real> sols (solve_derivative (d));
203 sols.push_back (1.0);
204 sols.push_back (0.0);
205 for (vsize i = sols.size (); i--;)
207 Offset o (curve_point (sols[i]));
208 iv.unite (Interval (o[a], o[a]));
210 return iv;
213 Interval
214 Bezier::control_point_extent (Axis a) const
216 Interval ext;
217 for (int i = CONTROL_COUNT; i--;)
218 ext.add_point (control_[i][a]);
220 return ext;
225 Flip around axis A
227 void
228 Bezier::scale (Real x, Real y)
230 for (int i = CONTROL_COUNT; i--;)
232 control_[i][X_AXIS] = x * control_[i][X_AXIS];
233 control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
237 void
238 Bezier::rotate (Real phi)
240 Offset rot (complex_exp (Offset (0, phi)));
241 for (int i = 0; i < CONTROL_COUNT; i++)
242 control_[i] = complex_multiply (rot, control_[i]);
245 void
246 Bezier::translate (Offset o)
248 for (int i = 0; i < CONTROL_COUNT; i++)
249 control_[i] += o;
252 void
253 Bezier::assert_sanity () const
255 for (int i = 0; i < CONTROL_COUNT; i++)
256 assert (!isnan (control_[i].length ())
257 && !isinf (control_[i].length ()));
260 void
261 Bezier::reverse ()
263 Bezier b2;
264 for (int i = 0; i < CONTROL_COUNT; i++)
265 b2.control_[CONTROL_COUNT - i - 1] = control_[i];
266 *this = b2;
271 Subdivide a bezier at T into LEFT_PART and RIGHT_PART
272 using deCasteljau's algorithm.
274 void
275 Bezier::subdivide (Real t, Bezier *left_part, Bezier *right_part) const
277 Offset p[CONTROL_COUNT][CONTROL_COUNT];
279 for (int i = 0; i < CONTROL_COUNT ; i++)
280 p[i][CONTROL_COUNT - 1 ] = control_[i];
281 for (int j = CONTROL_COUNT - 2; j >= 0 ; j--)
282 for (int i = 0; i < CONTROL_COUNT -1; i++)
283 p[i][j] = p[i][j+1] + t * (p[i+1][j+1] - p[i][j+1]);
284 for (int i = 0; i < CONTROL_COUNT; i++)
286 left_part->control_[i]=p[0][CONTROL_COUNT - 1 - i];
287 right_part->control_[i]=p[i][i];
292 Extract a portion of a bezier from T_MIN to T_MAX
295 Bezier
296 Bezier::extract (Real t_min, Real t_max) const
298 if ((t_min < 0) || (t_max) > 1)
299 programming_error
300 ("bezier extract arguments outside of limits: curve may have bad shape");
301 if (t_min >= t_max)
302 programming_error
303 ("lower bezier extract value not less than upper value: curve may have bad shape");
304 Bezier bez1, bez2, bez3, bez4;
305 if (t_min == 0.0)
306 bez2 = *this;
307 else
308 subdivide (t_min, &bez1, &bez2);
309 if (t_max == 1.0)
310 return bez2;
311 else
313 bez2.subdivide ((t_max-t_min)/(1-t_min), &bez3, &bez4);
314 return bez3;