2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
14 * move paper vars to scm
19 #include <math.h> // tanh.
21 #include "directional-element-interface.hh"
23 #include "dimensions.hh"
27 #include "least-squares.hh"
29 #include "paper-def.hh"
31 #include "group-interface.hh"
32 #include "staff-symbol-referencer.hh"
33 #include "cross-staff.hh"
34 #include "lily-guile.icc"
38 Group_interface g (this, "stems");
43 Beam::add_stem (Stem*s)
45 Group_interface gi (this, "stems");
48 s->add_dependency (this);
50 assert (!s->beam_l ());
51 s->set_elt_property ("beam", self_scm_);
53 if (!spanned_drul_[LEFT])
60 Beam::get_multiplicity () const
63 for (SCM s = get_elt_property ("stems"); gh_pair_p (s); s = gh_cdr (s))
65 Score_element * sc = unsmob_element (gh_car (s));
67 if (Stem * st = dynamic_cast<Stem*> (sc))
68 m = m >? st->beam_count (LEFT) >? st->beam_count (RIGHT);
74 After pre-processing all directions should be set.
75 Several post-processing routines (stem, slur, script) need stem/beam
77 Currenly, this means that beam has set all stem's directions.
78 [Alternatively, stems could set its own directions, according to
79 their beam, during 'final-pre-processing'.]
82 Beam::do_pre_processing ()
85 if (visible_stem_count () < 2)
87 warning (_ ("beam has less than two stems"));
88 set_elt_property ("transparent", SCM_BOOL_T);
91 if (!directional_element (this).get ())
92 directional_element (this).set (get_default_dir ());
95 set_stem_directions ();
104 Beam::get_default_dir () const
106 Drul_array<int> total;
107 total[UP] = total[DOWN] = 0;
108 Drul_array<int> count;
109 count[UP] = count[DOWN] = 0;
112 for (int i=0; i <stem_count (); i++)
113 do { // HUH -- waar slaat dit op?
115 Direction sd = directional_element (s).get ();
116 int current = sd ? (1 + d * sd)/2
117 : s->get_center_distance ((Direction)-d);
125 } while (flip(&d) != DOWN);
128 SCM s = scm_eval (gh_list (ly_symbol2scm ("beam-dir-algorithm"),
129 ly_quote_scm (gh_cons (gh_int2scm (count[UP]),
130 gh_int2scm (count[DOWN]))),
131 ly_quote_scm (gh_cons (gh_int2scm (total[UP]),
132 gh_int2scm (total[DOWN]))),
134 if (gh_number_p (s) && gh_scm2int (s))
138 If dir is not determined: get from paper
140 return (Direction)(int)
141 paper_l ()->get_var ("stem_default_neutral_direction");
146 Set all stems with non-forced direction to beam direction.
147 Urg: non-forced should become `without/with unforced' direction,
148 once stem gets cleaned-up.
151 Beam::set_stem_directions ()
153 Direction d = directional_element (this).get ();
154 for (int i=0; i <stem_count (); i++)
157 SCM force = s->remove_elt_property ("dir-forced");
158 if (!gh_boolean_p (force) || !gh_scm2bool (force))
159 directional_element (s).set (d);
166 if (!auto_knee ("auto-interstaff-knee-gap", true))
167 auto_knee ("auto-knee-gap", false);
171 Simplistic auto-knees; only consider vertical gap between two
174 `Forced' stem directions are ignored. If you don't want auto-knees,
175 don't set, or unset autoKneeGap/autoInterstaffKneeGap.
178 Beam::auto_knee (String gap_str, bool interstaff_b)
182 SCM gap = get_elt_property (gap_str);
183 Direction d = directional_element (this).get ();
185 if (gh_number_p (gap))
187 int auto_gap_i = gh_scm2int (gap);
188 for (int i=1; i < stem_count (); i++)
190 bool is_b = (bool)(calc_interstaff_dist (stem (i), this)
191 - calc_interstaff_dist (stem (i-1), this));
192 int l_y = (int)(stem (i-1)->head_positions()[d])
193 + (int)calc_interstaff_dist (stem (i-1), this);
194 int r_y = (int)(stem (i)->head_positions()[d])
195 + (int)calc_interstaff_dist (stem (i), this);
196 int gap_i = r_y - l_y;
198 if ((abs (gap_i) >= auto_gap_i) && (!interstaff_b || is_b))
200 knee_y = (r_y + l_y) / 2;
208 for (int i=0; i < stem_count (); i++)
210 int y = (int)(stem (i)->head_positions()[d])
211 + (int)calc_interstaff_dist (stem (i), this);
212 directional_element (stem (i)).set (y < knee_y ? UP : DOWN);
213 stem (i)->set_elt_property ("dir-forced", SCM_BOOL_T);
220 Set stem's shorten property if unset.
222 take some y-position (chord/beam/nearest?) into account
223 scmify forced-fraction
226 Beam::set_stem_shorten ()
228 if (!visible_stem_count ())
231 Real forced_fraction = forced_stem_count () / visible_stem_count ();
232 if (forced_fraction < 0.5)
235 int multiplicity = get_multiplicity ();
237 SCM shorten = ly_eval_str ("beamed-stem-shorten");
240 scm_to_array (shorten, &a);
244 Staff_symbol_referencer_interface st (this);
245 Real staff_space = st.staff_space ();
246 Real shorten_f = a[multiplicity <? (a.size () - 1)] * staff_space;
248 /* cute, but who invented this -- how to customise ? */
249 if (forced_fraction < 1)
252 for (int i=0; i < stem_count (); i++)
255 if (s->invisible_b ())
257 if (gh_number_p (s->get_elt_property ("shorten")))
258 s->set_elt_property ("shorten", gh_double2scm (shorten_f));
263 Set elt properties height and y-position if not set.
264 Adjust stem lengths to reach beam.
267 Beam::do_post_processing ()
269 /* first, calculate y, dy */
271 calc_position_and_height (&y, &dy);
272 if (suspect_slope_b (y, dy))
275 Real damped_dy = calc_slope_damping_f (dy);
276 Real quantised_dy = quantise_dy_f (damped_dy);
278 y += (dy - quantised_dy) / 2;
282 until here, we used only stem_info, which acts as if dir=up
284 y *= directional_element (this).get ();
285 dy *= directional_element (this).get ();
287 /* set or read dy as necessary */
288 SCM s = get_elt_property ("height");
290 dy = gh_scm2double (s);
292 set_elt_property ("height", gh_double2scm (dy));
294 /* set or read y as necessary */
295 s = get_elt_property ("y-position");
298 y = gh_scm2double (s);
299 set_stem_length (y, dy);
303 /* we can modify y, so we should quantise y */
304 Real y_shift = check_stem_length_f (y, dy);
306 y = quantise_y_f (y, dy, 0);
307 set_stem_length (y, dy);
308 y_shift = check_stem_length_f (y, dy);
310 Staff_symbol_referencer_interface st (this);
311 Real half_space = st.staff_space () / 2;
312 if (y_shift > half_space / 4)
317 for significantly lengthened or shortened stems,
318 request quanting the other way.
321 if (abs (y_shift) > half_space / 2)
322 quant_dir = sign (y_shift) * directional_element (this).get ();
323 y = quantise_y_f (y, dy, quant_dir);
324 set_stem_length (y, dy);
327 // UGH. Y is not in staff position unit?
328 set_elt_property ("y-position", gh_double2scm (y));
333 See Documentation/tex/fonts.doc
336 Beam::calc_position_and_height (Real* y, Real* dy) const
339 if (visible_stem_count () <= 1)
342 Real first_ideal = first_visible_stem ()->calc_stem_info ().idealy_f_;
343 if (first_ideal == last_visible_stem ()->calc_stem_info ().idealy_f_)
350 Array<Offset> ideals;
351 Real x0 = first_visible_stem ()->hpos_f ();
352 for (int i=0; i < stem_count (); i++)
355 if (s->invisible_b ())
357 ideals.push (Offset (s->hpos_f () - x0,
358 s->calc_stem_info ().idealy_f_));
361 minimise_least_squares (&dydx, y, ideals); // duh, takes references
363 Real dx = last_visible_stem ()->hpos_f () - x0;
368 Beam::suspect_slope_b (Real y, Real dy) const
371 steep slope running against lengthened stem is suspect
373 Real first_ideal = first_visible_stem ()->calc_stem_info ().idealy_f_;
374 Real last_ideal = last_visible_stem ()->calc_stem_info ().idealy_f_;
375 Real lengthened = paper_l ()->get_var ("beam_lengthened");
376 Real steep = paper_l ()->get_var ("beam_steep_slope");
378 Real dx = last_visible_stem ()->hpos_f () - first_visible_stem ()->hpos_f ();
379 Real dydx = dy && dx ? dy/dx : 0;
381 if (((y - first_ideal > lengthened) && (dydx > steep))
382 || ((y + dy - last_ideal > lengthened) && (dydx < -steep)))
390 This neat trick is by Werner Lemberg,
391 damped = tanh (slope)
392 corresponds with some tables in [Wanske]
395 Beam::calc_slope_damping_f (Real dy) const
397 SCM damp = get_elt_property ("damping"); // remove?
398 int damping = 1; // ugh.
399 if (gh_number_p (damp))
400 damping = gh_scm2int (damp);
404 Real dx = last_visible_stem ()->hpos_f ()
405 - first_visible_stem ()->hpos_f ();
406 Real dydx = dy && dx ? dy/dx : 0;
407 dydx = 0.6 * tanh (dydx) / damping;
414 Beam::calc_stem_y_f (Stem* s, Real y, Real dy) const
416 Real thick = gh_scm2double (get_elt_property ("beam-thickness"));
417 int beam_multiplicity = get_multiplicity ();
418 int stem_multiplicity = (s->flag_i () - 2) >? 0;
420 Real interbeam_f = paper_l ()->interbeam_f (beam_multiplicity);
421 Real x0 = first_visible_stem ()->hpos_f ();
422 Real dx = last_visible_stem ()->hpos_f () - x0;
423 Real stem_y = (dy && dx ? (s->hpos_f () - x0) / dx * dy : 0) + y;
426 Direction dir = directional_element(this).get ();
427 Direction sdir = directional_element (s).get ();
433 * (thick / 2 + (beam_multiplicity - 1) * interbeam_f);
435 Staff_symbol_referencer_interface me (s);
436 Staff_symbol_referencer_interface last (last_visible_stem ());
438 // huh, why not for first visible?
439 if (//(s != first_visible_stem ()) &&
440 me.staff_symbol_l () != last.staff_symbol_l ())
441 stem_y += directional_element (this).get ()
442 * (beam_multiplicity - stem_multiplicity) * interbeam_f;
448 Beam::check_stem_length_f (Real y, Real dy) const
452 Direction dir = directional_element (this).get ();
454 for (int i=0; i < stem_count (); i++)
457 if (s->invisible_b ())
460 Real stem_y = calc_stem_y_f (s, y, dy);
463 Stem_info info = s->calc_stem_info ();
465 // if (0 > info.maxy_f_ - stem_y)
466 shorten = shorten <? info.maxy_f_ - stem_y;
467 // if (0 < info.miny_f_ - stem_y)
468 lengthen = lengthen >? info.miny_f_ - stem_y;
471 if (lengthen && shorten)
472 warning (_ ("weird beam vertical offset"));
474 /* when all stems are too short, normal stems win */
475 return dir * ((shorten) ? shorten : lengthen);
479 Hmm. At this time, beam position and slope are determined. Maybe,
480 stem directions and length should set to relative to the chord's
481 position of the beam. */
483 Beam::set_stem_length (Real y, Real dy)
485 Staff_symbol_referencer_interface st (this);
486 Real half_space = st.staff_space ()/2;
487 for (int i=0; i < stem_count (); i++)
490 if (s->invisible_b ())
493 Real stem_y = calc_stem_y_f (s, y, dy);
495 /* caution: stem measures in staff-positions */
496 s->set_stemend ((stem_y + calc_interstaff_dist (s, this)) / half_space);
501 [Ross] (simplification of)
502 Set dy complying with:
504 - thick / 2 + staffline_f / 2
505 - thick + staffline_f
509 Beam::quantise_dy_f (Real dy) const
511 SCM quants = ly_eval_str ("beam-height-quants");
514 scm_to_array (quants, &a);
518 Staff_symbol_referencer_interface st (this);
519 Real staff_space = st.staff_space ();
521 Interval iv = quantise_iv (a, abs (dy)/staff_space) * staff_space;
522 Real q = (abs (dy) - iv[SMALLER] <= iv[BIGGER] - abs (dy))
526 return q * sign (dy);
530 Prevent interference from stafflines and beams.
531 See Documentation/tex/fonts.doc
533 We only need to quantise the (left) y-position of the beam,
534 since dy is quantised too.
535 if extend_b then stems must *not* get shorter
538 Beam::quantise_y_f (Real y, Real dy, int quant_dir)
540 int multiplicity = get_multiplicity ();
541 Staff_symbol_referencer_interface st (this);
542 Real staff_space = st.staff_space ();
543 SCM quants = scm_eval (gh_list (
544 ly_symbol2scm ("beam-vertical-position-quants"),
545 gh_int2scm (multiplicity),
546 gh_double2scm (dy/staff_space),
549 scm_to_array (quants, &a);
553 Real up_y = directional_element (this).get () * y;
554 Interval iv = quantise_iv (a, up_y/staff_space) * staff_space;
556 Real q = up_y - iv[SMALLER] <= iv[BIGGER] - up_y
557 ? iv[SMALLER] : iv[BIGGER];
559 q = iv[(Direction)quant_dir];
561 return q * directional_element (this).get ();
565 Beam::set_beaming (Beaming_info_list *beaming)
568 for (int i=0; i < stem_count (); i++)
572 if (stem (i)->beam_count (d) == 0)
573 stem (i)->set_beaming ( beaming->infos_.elem (i).beams_i_drul_[d],d);
575 while (flip (&d) != LEFT);
582 beams to go with one stem.
588 Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
590 if ((next && !(next->hpos_f () > here->hpos_f ())) ||
591 (prev && !(prev->hpos_f () < here->hpos_f ())))
592 programming_error ("Beams are not left-to-right");
594 Real staffline_f = paper_l ()->get_var ("stafflinethickness");
595 int multiplicity = get_multiplicity ();
598 Real interbeam_f = paper_l ()->interbeam_f (multiplicity);
599 Real thick = gh_scm2double (get_elt_property ("beam-thickness"));;
601 Real bdy = interbeam_f;
602 Real stemdx = staffline_f;
604 Real dx = last_visible_stem ()->hpos_f () - first_visible_stem ()->hpos_f ();
605 Real dy = get_real ("height");
606 Real dydx = dy && dx ? dy/dx : 0;
613 if (!here->first_head ())
615 else if (here->type_i ()== 1)
616 nw_f = paper_l ()->get_var ("wholewidth");
617 else if (here->type_i () == 2)
618 nw_f = paper_l ()->get_var ("notewidth") * 0.8;
620 nw_f = paper_l ()->get_var ("quartwidth");
623 Direction dir = directional_element (this).get ();
625 /* half beams extending to the left. */
628 int lhalfs= lhalfs = here->beam_count (LEFT) - prev->beam_count (RIGHT);
629 int lwholebeams= here->beam_count (LEFT) <? prev->beam_count (RIGHT) ;
631 Half beam should be one note-width,
632 but let's make sure two half-beams never touch
634 Real w = here->hpos_f () - prev->hpos_f ();
637 if (lhalfs) // generates warnings if not
638 a = lookup_l ()->beam (dydx, w, thick);
639 a.translate (Offset (-w, -w * dydx));
640 for (int j = 0; j < lhalfs; j++)
643 b.translate_axis (-dir * bdy * (lwholebeams+j), Y_AXIS);
644 leftbeams.add_molecule (b);
650 int rhalfs = here->beam_count (RIGHT) - next->beam_count (LEFT);
651 int rwholebeams= here->beam_count (RIGHT) <? next->beam_count (LEFT) ;
653 Real w = next->hpos_f () - here->hpos_f ();
654 Molecule a = lookup_l ()->beam (dydx, w + stemdx, thick);
655 a.translate_axis( - stemdx/2, X_AXIS);
659 SCM gap = get_elt_property ("beam-gap");
660 if (gh_number_p (gap))
662 int gap_i = gh_scm2int ( (gap));
663 int nogap = rwholebeams - gap_i;
665 for (; j < nogap; j++)
668 b.translate_axis (-dir * bdy * j, Y_AXIS);
669 rightbeams.add_molecule (b);
671 // TODO: notehead widths differ for different types
674 a = lookup_l ()->beam (dydx, w + stemdx, thick);
677 for (; j < rwholebeams; j++)
680 b.translate (Offset (here->invisible_b () ? 0 : gap_f, -dir * bdy * j));
681 rightbeams.add_molecule (b);
686 a = lookup_l ()->beam (dydx, w, thick);
688 for (; j < rwholebeams + rhalfs; j++)
691 b.translate_axis (- dir * bdy * j, Y_AXIS);
692 rightbeams.add_molecule (b);
696 leftbeams.add_molecule (rightbeams);
699 Does beam quanting think of the asymetry of beams?
700 Refpoint is on bottom of symbol. (FIXTHAT) --hwn.
707 Beam::do_brew_molecule_p () const
709 Molecule *mol_p = new Molecule;
713 Real x0 = first_visible_stem ()->hpos_f ();
714 Real dx = last_visible_stem ()->hpos_f () - x0;
715 Real dy = get_real ("height");
716 Real dydx = dy && dx ? dy/dx : 0;
717 Real y = get_real ("y-position");
718 for (int j=0; j <stem_count (); j++)
721 Stem * prev = (j > 0)? stem (j-1) : 0;
722 Stem * next = (j < stem_count ()-1) ? stem (j+1) :0;
724 Molecule sb = stem_beams (i, next, prev);
725 Real x = i->hpos_f ()-x0;
726 sb.translate (Offset (x, x * dydx + y));
727 mol_p->add_molecule (sb);
729 mol_p->translate_axis (x0
730 - spanned_drul_[LEFT]->relative_coordinate (0, X_AXIS), X_AXIS);
736 Beam::forced_stem_count () const
739 for (int i=0; i < stem_count (); i++)
743 if (s->invisible_b ())
746 if (((int)s->chord_start_f ())
747 && (s->get_direction () != s->get_default_dir ()))
756 TODO: Fix this class. This is wildly inefficient.
757 And it sux. Yet another array/list 'interface'.
760 Beam::stem (int i) const
762 return Group_interface__extract_elements ((Beam*) this, (Stem*) 0, "stems")[i];
766 Beam::stem_count () const
768 Group_interface gi (this, "stems");
773 Beam::stem_top () const
775 SCM s = get_elt_property ("stems");
777 return gh_pair_p (s) ? dynamic_cast<Stem*> (unsmob_element (gh_car (s))) : 0;
779 //Group_interface__extract_elements ((Beam*) this, (Stem*) 0, "stems")[stem_count () - 1];
784 Beam::visible_stem_count () const
787 for (int i = 0; i < stem_count (); i++)
789 if (!stem (i)->invisible_b ())
796 Beam::first_visible_stem () const
798 for (int i = 0; i < stem_count (); i++)
801 if (!s->invisible_b ())
811 Beam::last_visible_stem () const
813 for (int i = stem_count (); i > 0; i--)
815 Stem* s = stem (i - 1);
816 if (!s->invisible_b ())