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