]> git.donarmstrong.com Git - lilypond.git/blob - lily/bezier.cc
release: 1.3.72
[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 "config.h"
11
12 #include "libc-extension.hh"
13 #include "bezier.hh"
14 #include "polynomial.hh"
15
16 Real
17 binomial_coefficient (Real over , int under)
18 {
19   Real x = 1.0;
20
21   while (under)
22     {
23       x *= over / Real (under);
24
25       over  -= 1.0;
26       under --;
27     }
28   return x;
29 }
30
31 void
32 scale (Array<Offset>* arr_p, Real x , Real y)
33 {
34   for (int i = 0; i < arr_p->size (); i++)
35     {
36       (*arr_p)[i][X_AXIS] = x* (*arr_p)[i][X_AXIS];
37       (*arr_p)[i][Y_AXIS] = y* (*arr_p)[i][Y_AXIS];
38     }
39 }
40
41 void
42 rotate (Array<Offset>* arr_p, Real phi)
43 {
44   Offset rot (complex_exp (Offset (0, phi)));
45   for (int i = 0; i < arr_p->size (); i++)
46     (*arr_p)[i] = complex_multiply (rot, (*arr_p)[i]);
47 }
48
49 void
50 translate (Array<Offset>* arr_p, Offset o)
51 {
52   for (int i = 0; i < arr_p->size (); i++)
53     (*arr_p)[i] += o;
54 }
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 Real
64 Bezier::get_other_coordinate (Axis a,  Real x) const
65 {
66   Axis other = Axis ((a +1)%NO_AXES);
67   Array<Real> ts = solve_point (a, x);
68   
69   Offset c = curve_point (ts[0]);
70   assert (fabs (c[a] - x) < 1e-8);
71   
72   return c[other];
73 }
74
75
76 Offset
77 Bezier::curve_point (Real t)const
78 {
79   Real tj = 1;
80   Real one_min_tj =  (1-t)*(1-t)*(1-t);
81
82   Offset o;
83   for (int j=0 ; j < 4; j++)
84     {
85       o += control_[j] * binomial_coefficient (3, j)
86         * pow (t,j) * pow (1-t, 3-j);
87
88       tj *= t;
89       if (1-t)
90         one_min_tj /= (1-t);
91     }
92
93 #ifdef PARANOID
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 #endif
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 /**
160    Compute the bounding box dimensions in direction of A.
161  */
162 Interval
163 Bezier::extent (Axis a)const
164 {
165   int o = (a+1)%NO_AXES;
166   Offset d;
167   d[Axis (o)] =1.0;
168   Interval iv;
169   Array<Real> sols (solve_derivative (d));
170   sols.push (1.0);
171   sols.push (0.0);  
172   for (int i= sols.size (); i--;)
173     {
174       Offset o (curve_point (sols[i]));
175       iv.unite (Interval (o[a],o[a]));
176     }
177   return iv;
178 }
179
180 /**
181    Flip around axis A
182  */
183
184 void
185 Bezier::scale (Real x, Real y)
186 {
187   for (int i = CONTROL_COUNT; i--;)
188     {
189       control_[i][X_AXIS] = x * control_[i][X_AXIS];
190       control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
191     }
192 }
193
194 void
195 Bezier::rotate (Real phi)
196 {
197   Offset rot (complex_exp (Offset (0, phi)));
198   for (int i = 0; i < CONTROL_COUNT; i++)
199     control_[i] = complex_multiply (rot, control_[i]);
200 }
201
202 void
203 Bezier::translate (Offset o)
204 {
205   for (int i = 0; i < CONTROL_COUNT; i++)
206     control_[i] += o;
207 }
208
209 void
210 Bezier::assert_sanity () const
211 {
212   for (int i=0; i < CONTROL_COUNT; i++)
213     assert (!isnan (control_[i].length ())
214             && !isinf (control_[i].length ()));
215 }
216
217 void
218 Bezier::reverse ()
219 {
220   Bezier b2;
221   for (int i =0; i < CONTROL_COUNT; i++)
222     b2.control_[CONTROL_COUNT-i-1] = control_[i];
223   *this = b2;
224 }