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