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