]> git.donarmstrong.com Git - lilypond.git/blob - lily/bezier.cc
2003 -> 2004
[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.h"
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 Real
65 Bezier::get_other_coordinate (Axis a,  Real x) const
66 {
67   Axis other = Axis ((a +1)%NO_AXES);
68   Array<Real> ts = solve_point (a, x);
69
70   if (ts.size () == 0)
71     {
72       programming_error ("No solution found for Bezier intersection.");
73       return 0.0;
74     }
75   
76   Offset c = curve_point (ts[0]);
77
78   if (fabs (c[a] - x) > 1e-8)
79     programming_error ("Bezier intersection not correct?");
80   
81   return c[other];
82 }
83
84
85 Offset
86 Bezier::curve_point (Real t)const
87 {
88   Real tj = 1;
89   Real one_min_tj = (1-t)* (1-t)* (1-t);
90
91   Offset o;
92   for (int j=0 ; j < 4; j++)
93     {
94       o += control_[j] * binomial_coefficient (3, j)
95         * pow (t,j) * pow (1-t, 3-j);
96
97       tj *= t;
98       if (1-t)
99         one_min_tj /= (1-t);
100     }
101
102 #ifdef PARANOID
103   assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t))< 1e-8);
104   assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t))< 1e-8);
105 #endif
106   
107   return o;
108 }
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 += (control_[j][a] *    binomial_coefficient (3, j))
118         * Polynomial::power (j , Polynomial (0,1))*
119         Polynomial::power (3 - j, Polynomial (1,-1));
120     }
121
122   return p;
123 }
124
125 /**
126    Remove all numbers outside [0,1] from SOL
127  */
128 Array<Real>
129 filter_solutions (Array<Real> sol)
130 {
131   for (int i = sol.size (); i--;)
132     if (sol[i] < 0 || sol[i] >1)
133       sol.del (i);
134   return sol;
135 }
136
137 /**
138    find t such that derivative is proportional to DERIV
139  */
140 Array<Real>
141 Bezier::solve_derivative (Offset deriv)const
142 {
143   Polynomial xp=polynomial (X_AXIS);
144   Polynomial yp=polynomial (Y_AXIS);
145   xp.differentiate ();
146   yp.differentiate ();
147   
148   Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
149
150   return filter_solutions (combine.solve ());
151 }
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
192 void
193 Bezier::scale (Real x, Real y)
194 {
195   for (int i = CONTROL_COUNT; i--;)
196     {
197       control_[i][X_AXIS] = x * control_[i][X_AXIS];
198       control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
199     }
200 }
201
202 void
203 Bezier::rotate (Real phi)
204 {
205   Offset rot (complex_exp (Offset (0, phi)));
206   for (int i = 0; i < CONTROL_COUNT; i++)
207     control_[i] = complex_multiply (rot, control_[i]);
208 }
209
210 void
211 Bezier::translate (Offset o)
212 {
213   for (int i = 0; i < CONTROL_COUNT; i++)
214     control_[i] += o;
215 }
216
217 void
218 Bezier::assert_sanity () const
219 {
220   for (int i=0; i < CONTROL_COUNT; i++)
221     assert (!isnan (control_[i].length ())
222             && !isinf (control_[i].length ()));
223 }
224
225 void
226 Bezier::reverse ()
227 {
228   Bezier b2;
229   for (int i =0; i < CONTROL_COUNT; i++)
230     b2.control_[CONTROL_COUNT-i-1] = control_[i];
231   *this = b2;
232 }