]> git.donarmstrong.com Git - lilypond.git/blob - lily/bezier.cc
release: 1.3.29
[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--2000 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include <math.h>
10 #include "bezier.hh"
11 #include "polynomial.hh"
12
13 Real
14 binomial_coefficient (Real over , int under)
15 {
16   Real x = 1.0;
17
18   while (under)
19     {
20       x *= over / Real (under);
21
22       over  -= 1.0;
23       under --;
24     }
25   return x;
26 }
27
28 void
29 flip (Array<Offset>* arr_p, Axis a)
30 {
31   // huh?
32   //  for (int i = c.size (); i--;)
33   for (int i = 0; i < arr_p->size (); i++)
34     (*arr_p)[i][a] = - (*arr_p)[i][a];
35 }
36
37 void
38 rotate (Array<Offset>* arr_p, Real phi)
39 {
40   Offset rot (complex_exp (Offset (0, phi)));
41   for (int i = 0; i < arr_p->size (); i++)
42     (*arr_p)[i] = complex_multiply (rot, (*arr_p)[i]);
43 }
44
45 void
46 translate (Array<Offset>* arr_p, Offset o)
47 {
48   for (int i = 0; i < arr_p->size (); i++)
49     (*arr_p)[i] += o;
50 }
51
52 /*
53
54   Formula of the bezier 3-spline
55
56   sum_{j=0}^3  (3 over j) z_j (1-t)^(3-j)  t^j
57  */
58
59 Bezier::Bezier ()
60   : control_ (CONTROL_COUNT)
61 {
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   Offset c = curve_point (ts[0]);
71   assert (fabs (c[a] - x) < 1e-8);
72   
73   return c[other];
74 }
75
76
77 Offset
78 Bezier::curve_point (Real t)const
79 {
80   Real tj = 1;
81   Real one_min_tj =  (1-t)*(1-t)*(1-t);
82
83   Offset o;
84   for (int j=0 ; j < 4; j++)
85     {
86       o += control_[j] * binomial_coefficient (3, j)
87         * pow (t,j) * pow (1-t, 3-j);
88
89       tj *= t;
90       if (1-t)
91         one_min_tj /= (1-t);
92     }
93
94   assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t))< 1e-8);
95   assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t))< 1e-8);
96
97   
98   return o;
99 }
100
101
102 Polynomial
103 Bezier::polynomial (Axis a)const
104 {
105   Polynomial p (0.0);
106   for (int j=0; j <= 3; j++)
107     {
108       p += control_[j][a]
109         * Polynomial::power (j , Polynomial (0,1))*
110         Polynomial::power (3 - j, Polynomial (1,-1))*
111         binomial_coefficient(3, j);
112     }
113
114   return p;
115 }
116
117 /**
118    Remove all numbers outside [0,1] from SOL
119  */
120 Array<Real>
121 filter_solutions (Array<Real> sol)
122 {
123   for (int i = sol.size (); i--;)
124     if (sol[i] < 0 || sol[i] >1)
125       sol.del (i);
126   return sol;
127 }
128
129 /**
130    find t such that derivative is proportional to DERIV
131  */
132 Array<Real>
133 Bezier::solve_derivative (Offset deriv)const
134 {
135   Polynomial xp=polynomial (X_AXIS);
136   Polynomial yp=polynomial (Y_AXIS);
137   xp.differentiate ();
138   yp.differentiate ();
139   
140   Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
141
142   return filter_solutions (combine.solve ());
143 }
144   
145
146 /*
147   Find t such that curve_point (t)[AX] == COORDINATE
148 */
149 Array<Real> 
150 Bezier::solve_point (Axis ax, Real coordinate) const
151 {
152   Polynomial p(polynomial (ax));
153   p.coefs_[0] -= coordinate;
154   
155   Array<Real> sol (p.solve ());
156   return filter_solutions (sol);
157 }
158
159 Interval
160 Bezier::extent (Axis a)const
161 {
162   int o = (a+1)%NO_AXES;
163   Offset d;
164   d[Axis (o)] =1.0;
165   Interval iv;
166   Array<Real> sols (solve_derivative (d));
167   sols.push (1.0);
168   sols.push (0.0);  
169   for (int i= sols.size (); i--;)
170     {
171       Offset o (curve_point (sols[i]));
172       iv.unite (Interval (o[a],o[a]));
173     }
174   return iv;
175 }
176
177 void
178 Bezier::flip (Axis a)
179 {
180   ::flip (&control_, a);
181 }
182
183 void
184 Bezier::rotate (Real phi)
185 {
186   ::rotate (&control_, phi);
187 }
188
189 void
190 Bezier::translate (Offset o)
191 {
192   ::translate (&control_, o);
193 }
194
195 void
196 Bezier::assert_sanity () const
197 {
198   for (int i=0; i < CONTROL_COUNT; i++)
199     assert (!isnan (control_[i].length ())
200             && !isinf (control_[i].length ()));
201 }
202
203 void
204 Bezier::reverse ()
205 {
206   Bezier b2;
207   for (int i =0; i < CONTROL_COUNT; i++)
208     b2.control_[CONTROL_COUNT-i-1] = control_[i];
209   *this = b2;
210 }