]> git.donarmstrong.com Git - lilypond.git/blob - lily/bezier.cc
release: 1.3.13
[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--1999 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[2];
110
111   xp[X_AXIS] = polynomial (X_AXIS);
112   xp[Y_AXIS] = polynomial (Y_AXIS);    
113   Polynomial combine = xp[X_AXIS] * deriv[Y_AXIS] - xp[Y_AXIS] * deriv [X_AXIS];
114   return filter_solutions (combine.solve ());
115 }
116   
117
118 /*
119   Find t such that curve_point (t)[AX] == COORDINATE
120 */
121 Array<Real> 
122 Bezier::solve_point (Axis ax, Real coordinate) const
123 {
124   Polynomial p(polynomial (ax));
125   p.coefs_[0] -= coordinate;
126   
127   Array<Real> sol (p.solve ());
128   return filter_solutions (sol);
129 }
130
131 Interval
132 Bezier::extent (Axis a)const
133 {
134   int o = (a+1)%NO_AXES;
135   Offset d;
136   d[Axis (o)] =1.0;
137   Interval iv;
138   Array<Real> sols (solve_derivative (d));
139   sols.push (1.0);
140   sols.push (0.0);  
141   for (int i= sols.size (); i--;)
142     {
143       Offset o (curve_point (sols[i]));
144       iv.unite (Interval (o[a],o[a]));
145     }
146   return iv;
147 }
148
149 void
150 Bezier::flip (Axis a)
151 {
152   for (int i = CONTROL_COUNT; i--;)
153     control_[i][a] = - control_[i][a];
154 }
155
156 void
157 Bezier::rotate (Real phi)
158 {
159   Offset rot (complex_exp (Offset (0, phi)));
160   for (int i = 0; i < CONTROL_COUNT; i++)
161     control_[i] = complex_multiply (rot, control_[i]);
162 }
163
164 void
165 Bezier::translate (Offset o)
166 {
167   for (int i = 0; i < CONTROL_COUNT; i++)
168     control_[i] += o;
169 }
170
171 void
172 Bezier::check_sanity () const
173 {
174   for (int i=0; i < CONTROL_COUNT; i++)
175     assert (!isnan (control_[i].length ())
176             && !isinf (control_[i].length ()));
177 }