]> git.donarmstrong.com Git - lilypond.git/blob - lily/bezier.cc
release: 0.1.57
[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 Jan Nieuwenhuizen <jan@digicash.com>
7 */
8
9 #include <math.h>
10 #include "bezier.hh"
11 #include "direction.hh"
12
13 #ifndef STANDALONE
14 #include "direction.hh"
15 #include "dimen.hh"
16 #include "paper-def.hh"
17 #include "debug.hh"
18 #include "main.hh"
19 #define SLUR_DOUT if (check_debug && !monitor->silent_b ("Slur")) cout
20 #else
21 #define SLUR_DOUT cerr
22 #endif
23
24 void
25 Curve::flipy ()
26 {
27   // ugh, Offset should have mirror funcs
28   for (int i = 0; i < size (); i++)
29     (*this)[i].mirror (Y_AXIS);
30 }
31
32 int
33 Curve::largest_disturbing ()
34 {
35   Real alpha = 0;
36   int j = 0;
37   for (int i = 1; i < size (); i++)
38     {
39       if ((*this)[i].y () > 0)
40         {
41           Real phi = (*this)[i].y () / (*this)[i].x ();
42           if (phi > alpha)
43             {
44               alpha = phi;
45               j = i;
46             }
47         }
48     }
49   return j;
50 }
51
52 void
53 Curve::rotate (Real phi)
54 {
55   Offset rot (complex_exp (Offset (0,phi)));
56
57   for (int i = 1; i < size (); i++)
58     (*this)[i] = complex_multiply (rot, (*this)[i]);
59 }
60
61 void
62 Curve::translate (Offset o)
63 {
64   for (int i = 1; i < size (); i++)
65     (*this)[i] += o;
66 }
67
68 Bezier::Bezier (int steps)
69 {
70   control_.set_size (4);
71   curve_.set_size (steps);
72 }
73
74 //from GNU gs3.33: ega.c
75 void
76 Bezier::calc ()
77 {       
78   Real dt = 1.0 / curve_.size ();
79   Offset c = 3.0 * (control_[1] - control_[0]);
80   Offset b = 3.0 * (control_[2] - control_[1]) - c;
81   Offset a = control_[3] - (control_[0] + c + b);
82   Real t = 0.0;
83   for (int i = 0; i < curve_.size (); i++ )
84     {    
85       curve_[i] = ((a * t + b) * t + c) * t + control_[0];
86       t += dt;
87     }
88 }
89
90 void
91 Bezier::set (Array<Offset> points)
92 {       
93   assert (points.size () == 4);
94   control_ = points;
95 }
96
97 Real
98 Bezier::y (Real x)
99 {
100   if (x <= curve_[0].x ())
101     return curve_[0].y ();
102   for (int i = 1; i < curve_.size (); i++ )
103     {
104       if (x < curve_[i].x ())
105         //           ^ ? see below   
106         {
107           Real lin = (x - curve_[i-1].x ()) / (curve_[i] - curve_[i-1]).x ();
108           //                     ^ ?
109           return (curve_[i-1] + lin * (curve_[i] - curve_[i-1])).y ();
110         }
111     }
112   return curve_[curve_.size ()-1].y ();
113 }
114
115
116 Bezier_bow::Bezier_bow (Paper_def* paper_l)
117   : Bezier(10)
118 {
119   paper_l_ = paper_l;
120   return_.set_size (4);
121 }
122
123 /* 
124   from feta-sleur.ly
125
126         slurheightlimit#:=staffsize#/2;
127         sluralpha:=slurheightlimit#*pi/2;
128         % slurratio:=1/3;
129         slurratio:=0.3333;
130         slurbeta:=3/4*pi*slurratio/sluralpha;
131
132         b#:=length(dx#,dy#);
133         % ugh: huh? 2/5
134         indent#:=2/5*sluralpha*atan(slurbeta*b#);
135         height:=(indent+h)*d;
136         z1=(0,0);
137         z2=(b,0);
138         z3=(indent,height);
139         z4=(b-indent,height);
140
141         boogje:=boogje rotated angle(dxs,dys);
142 */
143
144 void
145 Bezier_bow::blow_fit ()
146 {
147   Real dy1 = check_fit_f ();
148   if (!dy1)
149     return;
150
151   // be careful not to take too big step
152   Real f = 0.75;
153   Real h1 = dy1 * f;
154   control_[1].y () += h1; 
155   control_[2].y () += h1; 
156   return_[1].y () += h1; 
157   return_[2].y () += h1; 
158
159   Real dy2 = check_fit_f ();
160   if (!dy2)
161     return;
162
163 #ifndef STANDALONE
164   Real epsilon = paper_l_->rule_thickness ();
165 #else
166   Real epsilon = 1.5 * 0.4 PT;
167 #endif
168   if (abs (dy2 - dy1) < epsilon)
169     return;
170   
171   /*
172     Assume 
173       dy = B (h) 
174     with 
175       B (h) = a * h + b;
176
177     Then we get for h : B (h) = 0
178
179      B(0)  = dy1 = a * 0 + b   =>   b = dy1
180      B(h1) = dy2 = a * h1 + b  =>   a * f * dy1 + b = dy2
181
182          =>
183
184      a * dy1 / 2 + dy1 = dy2  =>  a = (dy2 - dy1) / (f * dy1)
185    */
186
187   Real a = (dy2 - dy1) / (f * dy1);
188   Real b = dy1;
189   Real h = -b / a;
190
191   control_[1].y () += -h1 +h; 
192   control_[2].y () = -h1 +h; 
193   return_[1].y () = -h1 +h;
194   return_[2].y () = -h1 +h; 
195 }
196
197 Real
198 Bezier_bow::calc_f (Real height)
199 {
200   transform ();
201   calc_default (height);
202   Bezier::calc ();
203   
204   Real dy = check_fit_f ();
205   calc_return (0, 0);
206
207   transform_controls_back ();
208   return dy;
209 }
210
211 void
212 Bezier_bow::calc ()
213 {
214   transform ();
215   calc_default (0);
216   Bezier::calc ();
217   
218   if (check_fit_bo ())
219     calc_return (0, 0);
220   else
221     {
222       calc_controls ();
223       blow_fit ();
224     }
225
226   transform_controls_back ();
227 }
228
229 void
230 Bezier_bow::calc_return (Real begin_alpha, Real end_alpha)
231 {
232 #ifndef STANDALONE
233   Real thick = 1.8 * paper_l_->rule_thickness ();
234 #else
235   Real thick = 10.0 * 1.8 * 0.4 PT;
236 #endif
237   return_[0] = control_[3];
238
239   return_[1] = control_[2] - thick * complex_exp (Offset (0, 90 + end_alpha));
240   return_[2] = control_[1] - thick * complex_exp (Offset (0, 90 - begin_alpha));  
241   
242   /*
243   return_[1].x () = control_[2].x () - thick * cos (90 + end_alpha);
244   return_[1].y () = control_[2].y () - thick * sin (90 + end_alpha);
245   return_[2].x () = control_[1].x () - thick * cos (90 - begin_alpha);
246   return_[2].y () = control_[1].y () - thick * sin (90 - begin_alpha);
247   */
248   return_[3] = control_[0];
249 }
250
251 void
252 Bezier_bow::calc_controls ()
253 {
254   // ugh: tooo steep
255 //  Real default_rc = atan (control_[1].y () / control_[1].x ());
256   
257   Offset ijk_p (control_[3].x () / 2, control_[1].y ());
258   SLUR_DOUT << "ijk: " << ijk_p.x () << ", " << ijk_p.y () << endl;
259
260   Real default_rc = ijk_p.y () / ijk_p.x ();
261
262   int begin_disturb = encompass_.largest_disturbing ();
263   Offset begin_p = begin_disturb ? Offset (encompass_[begin_disturb].x (), 
264     encompass_[begin_disturb].y ()) : ijk_p;
265   Real begin_rc = begin_p.y () / begin_p.x ();
266   if (default_rc > begin_rc)
267     {
268       begin_p = ijk_p;
269       begin_rc = default_rc;
270     }
271
272   // ugh
273   Curve reversed;
274   reversed.set_size (encompass_.size ());
275   Real b = control_[3].x ();
276   for (int i = 0; i < encompass_.size (); i++ )
277     {
278       reversed[i] = Offset (b,0) - encompass_[encompass_.size () - i -1];
279       /*
280       reversed[i].x () = b - encompass_[encompass_.size () - i - 1].x ();
281       reversed[i].y () = encompass_[encompass_.size () - i - 1].y ();
282       */
283     }
284
285   int end_disturb = reversed.largest_disturbing ();
286   end_disturb = end_disturb ? encompass_.size () - end_disturb - 1 : 0;
287   Offset end_p = end_disturb ? Offset (encompass_[end_disturb].x (), 
288     encompass_[end_disturb].y ()) : ijk_p;
289   Real end_rc = end_p.y () / (control_[3].x () - end_p.x ());
290   if (default_rc > end_rc)
291     {
292       end_p = ijk_p;
293       end_rc = default_rc;
294     }
295   SLUR_DOUT << "begin " << begin_p.x () << ", " << begin_p.y () << endl;
296   SLUR_DOUT << "end " << end_p.x () << ", " << end_p.y () << endl;
297
298   Real height =control_[1].y (); 
299   for (int i = 0; i < encompass_.size (); i++ )
300     height = height >? encompass_[i].y ();
301
302   // emperic computer science:
303   //   * tangents somewhat steeper than minimal line
304   Real rc_correct = 2.4;
305
306   begin_rc *= rc_correct;
307   end_rc *= rc_correct;
308   Real rc1 = begin_rc;
309   Real rc2 = -end_rc;
310   
311   Real begin_alpha = atan (begin_rc);
312   Real end_alpha = atan (-end_rc);
313   Real theta = (begin_alpha - end_alpha) / 2;
314
315   // if we have two disturbing points, have height line through those...
316   /*
317     UGH UGH UGH! NEVER compare floats with == 
318    */
319   if (!((begin_p.x () == end_p.x ()) && (begin_p.y () == end_p.y ())))
320       theta = atan (end_p.y () - begin_p.y ()) / (end_p.x () - begin_p.x ());
321
322   Real rc3 = tan (theta);
323   // ugh: be less steep
324   rc3 /= 2*rc_correct;
325
326   Real c2 = -rc2 * control_[3].x ();
327   Real c3 = begin_p.y () > end_p.y () ? begin_p.y () 
328     - rc3 * begin_p.x () : end_p.y () - rc3 * end_p.x ();
329
330   SLUR_DOUT << "y1 = " << rc1 << " x + 0" << endl;
331   SLUR_DOUT << "y2 = " << rc2 << " x + " << c2 << endl;
332   SLUR_DOUT << "y3 = " << rc3 << " x + " << c3 << endl;
333   control_[1].x () = c3 / (rc1 - rc3);
334   control_[1].y () = rc1 * control_[1].x ();
335   control_[2].x () = (c3 - c2) / (rc2 - rc3);
336   SLUR_DOUT << "c2.x () = " << control_[2].x () << endl;
337   SLUR_DOUT << "(c3 - c2) = " << (c3 - c2) << endl;
338   SLUR_DOUT << "(rc2 - rc3) = " << (rc2 - rc3) << endl;
339   control_[2].y () = rc2 * control_[2].x () + c2;
340   SLUR_DOUT << "c2.y ()" << control_[2].y () << endl;
341
342   calc_return (begin_alpha, end_alpha);
343 }
344
345 bool
346 Bezier_bow::check_fit_bo ()
347 {
348   for (int i = 1; i < encompass_.size () - 1; i++)
349     if (encompass_[i].y () > y (encompass_[i].x ()))
350       return false;
351   return true;
352 }
353
354 Real
355 Bezier_bow::check_fit_f ()
356 {
357   Real dy = 0;
358   for (int i = 1; i < encompass_.size () - 1; i++)
359     dy = dy >? (encompass_[i].y () - y (encompass_[i].x ()));
360   return dy;
361 }
362
363 void
364 Bezier_bow::set (Array<Offset> points, int dir)
365 {
366   dir_ = dir;
367   encompass_ = points;
368 }
369
370 void
371 Bezier_bow::transform ()
372 {
373   origin_ = encompass_[0];
374   encompass_.translate (-origin_);
375
376   Offset delta = encompass_[encompass_.size () - 1] - encompass_[0];
377   /*
378   Real dx = encompass_[encompass_.size () - 1].x () - encompass_[0].x (); 
379   Real dy = encompass_[encompass_.size () - 1].y () - encompass_[0].y ();
380   */
381
382   alpha_ = delta.arg ();
383   encompass_.rotate (-alpha_);
384
385   if (dir_ == DOWN)
386     encompass_.flipy ();
387 }
388
389 void
390 Bezier_bow::transform_controls_back ()
391 {
392   // silly name; let's transform encompass back too
393   // to allow recalculation without re-set()ting encompass array
394   if (dir_ == DOWN)
395     {
396       control_.flipy ();
397       return_.flipy ();
398       encompass_.flipy ();
399     }
400
401   control_.rotate (alpha_);
402   control_.translate (origin_);
403
404   return_.rotate (alpha_);
405   return_.translate (origin_);
406
407   encompass_.rotate (alpha_);
408   encompass_.translate (origin_);
409 }
410
411 void
412 Bezier_bow::calc_default (Real h)
413 {
414   Real pi = M_PI;
415 #ifndef STANDALONE
416   Real staffsize_f = paper_l_->get_var ("barsize");
417 #else
418   Real staffsize_f = 16 PT;
419 #endif
420
421   Real height_limit = staffsize_f;
422   Real alpha = height_limit * pi / 2.0;
423   Real ratio = 1.0/3.0;
424   Real beta = 3.0/4.0 * pi * ratio/alpha;
425
426
427   Offset delta (encompass_[encompass_.size () - 1].x () - encompass_[0].x (), 0);
428
429   Real d = 1;
430
431   Real b = delta.length ();
432   Real indent = 2.0/5.0 * alpha * atan (beta * b);
433   // ugh, ugly height hack, see lily-ps-defs.tex
434   Real height = (indent + h) * d;
435  
436 //  Offset control[4] = {0, 0, indent, height, b - indent, height, b, 0 };
437   Array<Offset> control;
438   control.push (Offset (0, 0));
439   control.push (Offset (indent, height));
440   control.push (Offset (b - indent, height));
441   control.push (Offset (b, 0));
442   Bezier::set (control);
443  
444 //  Real phi = dx ? atan (dy/dx) : sign (dy) * pi / 2.0;
445 //  control.rotate (phi);
446 }
447