2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
14 -* shorter! (now +- 1000 lines)
18 -* Remove #'direction from beam. The beam has no direction per se.
19 It may only set directions for stems.
23 #include <math.h> // tanh.
25 #include "molecule.hh"
26 #include "directional-element-interface.hh"
30 #include "least-squares.hh"
32 #include "paper-def.hh"
34 #include "group-interface.hh"
35 #include "staff-symbol-referencer.hh"
41 Beam::add_stem (Grob*me, Grob*s)
43 Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s);
45 s->add_dependency (me);
47 assert (!Stem::beam_l (s));
48 s->set_grob_property ("beam", me->self_scm ());
50 add_bound_item (dynamic_cast<Spanner*> (me), dynamic_cast<Item*> (s));
54 Beam::get_multiplicity (Grob*me)
57 for (SCM s = me->get_grob_property ("stems"); gh_pair_p (s); s = ly_cdr (s))
59 Grob * sc = unsmob_grob (ly_car (s));
61 if (Stem::has_interface (sc))
62 m = m >? Stem::beam_count (sc,LEFT) >? Stem::beam_count (sc,RIGHT);
68 After pre-processing all directions should be set.
69 Several post-processing routines (stem, slur, script) need stem/beam
71 Currenly, this means that beam has set all stem's directions.
72 [Alternatively, stems could set its own directions, according to
73 their beam, during 'final-pre-processing'.]
75 MAKE_SCHEME_CALLBACK (Beam,before_line_breaking,1);
77 Beam::before_line_breaking (SCM smob)
79 Grob * me = unsmob_grob (smob);
82 Beams with less than 2 two stems don't make much sense, but could happen
87 For a beam that only has one stem, we try to do some disappearance magic:
88 we revert the flag, and move on to The Eternal Engraving Fields.*/
91 if (visible_stem_count (me) < 2)
93 me->warning (_ ("beam has less than two visible stems"));
95 SCM stems = me->get_grob_property ("stems");
96 if (scm_ilength (stems) == 1)
98 me->warning (_("Beam has less than two stems. Removing beam."));
100 unsmob_grob (gh_car (stems))->remove_grob_property ("beam");
103 return SCM_UNSPECIFIED;
105 else if (scm_ilength (stems) == 0)
108 return SCM_UNSPECIFIED;
111 if (visible_stem_count (me) >= 1)
113 if (!Directional_element_interface::get (me))
114 Directional_element_interface::set (me, get_default_dir (me));
116 consider_auto_knees (me);
117 set_stem_directions (me);
118 set_stem_shorten (me);
124 Beam::get_default_dir (Grob*me)
126 Drul_array<int> total;
127 total[UP] = total[DOWN] = 0;
128 Drul_array<int> count;
129 count[UP] = count[DOWN] = 0;
132 Link_array<Item> stems=
133 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
135 for (int i=0; i <stems.size (); i++)
138 Direction sd = Directional_element_interface::get (s);
139 int current = sd ? (1 + d * sd)/2
140 : Stem::get_center_distance (s, (Direction)-d);
148 } while (flip (&d) != DOWN);
150 SCM func = me->get_grob_property ("dir-function");
151 SCM s = gh_call2 (func,
152 gh_cons (gh_int2scm (count[UP]),
153 gh_int2scm (count[DOWN])),
154 gh_cons (gh_int2scm (total[UP]),
155 gh_int2scm (total[DOWN])));
157 if (gh_number_p (s) && gh_scm2int (s))
161 If dir is not determined: get default
163 return to_dir (me->get_grob_property ("neutral-direction"));
168 Set all stems with non-forced direction to beam direction.
169 Urg: non-forced should become `without/with unforced' direction,
170 once stem gets cleaned-up.
173 Beam::set_stem_directions (Grob*me)
175 Link_array<Item> stems
176 =Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
177 Direction d = Directional_element_interface::get (me);
179 for (int i=0; i <stems.size (); i++)
182 SCM force = s->remove_grob_property ("dir-forced");
183 if (!gh_boolean_p (force) || !gh_scm2bool (force))
184 Directional_element_interface ::set (s,d);
189 Simplistic auto-knees; only consider vertical gap between two
192 `Forced' stem directions are ignored. If you don't want auto-knees,
193 don't set, or unset auto-knee-gap.
196 Beam::consider_auto_knees (Grob *me)
198 SCM scm = me->get_grob_property ("auto-knee-gap");
200 if (gh_number_p (scm))
204 Real staff_space = Staff_symbol_referencer::staff_space (me);
205 Real gap = gh_scm2double (scm) / staff_space;
207 Direction d = Directional_element_interface::get (me);
208 Link_array<Item> stems=
209 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
211 Grob *common = me->common_refpoint (stems[0], Y_AXIS);
212 for (int i=1; i < stems.size (); i++)
213 if (!Stem::invisible_b (stems[i]))
214 common = common->common_refpoint (stems[i], Y_AXIS);
217 for (int i=1; i < stems.size (); i++)
219 if (!Stem::invisible_b (stems[i-1]))
221 if (Stem::invisible_b (stems[l]))
223 if (Stem::invisible_b (stems[i]))
226 Real left = Stem::extremal_heads (stems[l])[d]
227 ->relative_coordinate (common, Y_AXIS);
228 Real right = Stem::extremal_heads (stems[i])[-d]
229 ->relative_coordinate (common, Y_AXIS);
231 Real dy = right - left;
235 knee_y = (right + left) / 2;
243 for (int i=0; i < stems.size (); i++)
245 if (Stem::invisible_b (stems[i]))
248 Real y = Stem::extremal_heads (stems[i])[d]
249 ->relative_coordinate (common, Y_AXIS);
251 Directional_element_interface::set (s, y < knee_y ? UP : DOWN);
252 s->set_grob_property ("dir-forced", SCM_BOOL_T);
259 Set stem's shorten property if unset.
261 take some y-position (chord/beam/nearest?) into account
262 scmify forced-fraction
265 Beam::set_stem_shorten (Grob*m)
267 Spanner*me = dynamic_cast<Spanner*> (m);
269 Real forced_fraction = forced_stem_count (me) / visible_stem_count (me);
271 int multiplicity = get_multiplicity (me);
273 SCM shorten = me->get_grob_property ("beamed-stem-shorten");
274 if (shorten == SCM_EOL)
277 int sz = scm_ilength (shorten);
279 Real staff_space = Staff_symbol_referencer::staff_space (me);
280 SCM shorten_elt = scm_list_ref (shorten, gh_int2scm (multiplicity <? (sz - 1)));
281 Real shorten_f = gh_scm2double (shorten_elt) * staff_space;
283 /* your similar cute comment here */
284 shorten_f *= forced_fraction;
286 me->set_grob_property ("shorten", gh_double2scm (shorten_f));
290 Call list of y-dy-callbacks, that handle setting of
291 grob-properties y, dy.
293 User may set grob-properties: y-position-hs and height-hs
294 (to be fixed) that override the calculated y and dy.
296 Because y and dy cannot be calculated and quanted separately, we
297 always calculate both, then check for user override.
299 MAKE_SCHEME_CALLBACK (Beam, after_line_breaking, 1);
301 Beam::after_line_breaking (SCM smob)
303 Grob * me = unsmob_grob (smob);
305 me->set_grob_property ("y", gh_double2scm (0));
306 me->set_grob_property ("dy", gh_double2scm (0));
308 /* Hmm, callbacks should be called by, a eh, callback mechanism
309 somewhere (?), I guess, not by looping here. */
311 SCM list = me->get_grob_property ("y-dy-callbacks");
312 for (SCM i = list; gh_pair_p (i); i = ly_cdr (i))
313 gh_call1 (ly_car (i), smob);
315 // UGH. Y is not in staff position unit?
316 // Ik dacht datwe daar juist van weg wilden?
318 // Hmm, nu hebben we 3 dimensies, want inmiddels zijn we daar
319 // weer terug, maar dan / 2
320 // (staff-space iso staff-position)
322 set_stem_lengths (me);
324 return SCM_UNSPECIFIED;
328 MAKE_SCHEME_CALLBACK (Beam, least_squares, 1);
330 Beam::least_squares (SCM smob)
332 Grob *me = unsmob_grob (smob);
334 if (visible_stem_count (me) <= 1)
335 return SCM_UNSPECIFIED;
340 /* Stem_info, and thus y,dy in this function are corrected for beam-dir */
341 Real first_ideal = Stem::calc_stem_info (first_visible_stem (me)).idealy_f_;
342 if (first_ideal == Stem::calc_stem_info (last_visible_stem (me)).idealy_f_)
349 Array<Offset> ideals;
351 // ugh -> use commonx
352 Real x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
353 Link_array<Item> stems=
354 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
356 for (int i=0; i < stems.size (); i++)
359 if (Stem::invisible_b (s))
361 ideals.push (Offset (s->relative_coordinate (0, X_AXIS) - x0,
362 Stem::calc_stem_info (s).idealy_f_));
365 minimise_least_squares (&dydx, &y, ideals);
367 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
371 /* Store true, not dir-corrected values */
372 Direction dir = Directional_element_interface::get (me);
373 me->set_grob_property ("y", gh_double2scm (y * dir));
374 me->set_grob_property ("dy", gh_double2scm (dy * dir));
375 return SCM_UNSPECIFIED;
379 MAKE_SCHEME_CALLBACK (Beam, check_concave, 1);
381 Beam::check_concave (SCM smob)
383 Grob *me = unsmob_grob (smob);
385 Link_array<Item> stems =
386 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
388 for (int i = 0; i < stems.size ();)
390 if (Stem::invisible_b (stems[i]))
396 if (stems.size () < 3)
397 return SCM_UNSPECIFIED;
399 /* TODO: find-out what makes beam concave (#1, #2, #3, #4 or
401 SCM s = me->get_grob_property ("concaveness-no-slope");
406 /* Concaveness try #1: Sum distances of inner noteheads to line
407 between two outer noteheads. */
409 s = me->get_grob_property ("concave-if-bigger-than-two");
411 Real dy = Stem::chord_start_f (stems.top ())
412 - Stem::chord_start_f (stems[0]);
413 Real slope = dy / (stems.size () - 1);
415 Real y0 = Stem::chord_start_f (stems[0]);
416 for (int i = 1; i < stems.size () - 1; i++)
418 Real c = (Stem::chord_start_f (stems[i]) - y0) - i * slope;
420 /* try #4: (Han-Wen): neem maximum afstand lijn - tot
421 extreme notehead (in geval van akkoorden). Als die
422 afstand >= 2.0 ss was, dan moest hij recht (of blijkbaar:
423 vrijwel recht, zie m 17, 18). Dat was nl. wat stolba zei:
424 als afstand lijn-noot >= 2.0 dan recht. */
426 if (to_boolean (s) && c >= 2.0)
428 concave = 1000 * Directional_element_interface::get (me);
438 /* Concaveness try #2: Sum distances of inner noteheads that
439 fall outside the interval of the two outer noteheads */
441 Interval iv = Interval (Stem::chord_start_f (stems[0]),
442 Stem::chord_start_f (stems.top ()));
444 if (iv[MAX] < iv[MIN])
446 iv = Interval (iv[MAX], iv[MIN]);
448 for (int i = 1; i < stems.size () - 1; i++)
451 Real f = Stem::chord_start_f (stems[i]);
452 if ((c = f - iv[MAX]) > 0)
454 else if ((c = f - iv[MIN]) < 0)
459 concave *= Directional_element_interface::get (me);
461 Real concaveness = concave / (stems.size () - 2);
463 /* ugh: this is the a kludge to get input/regression/beam-concave.ly
464 to behave as baerenreiter.
466 try #3 (add-on to #2): */
467 s = me->get_grob_property ("concaveness-square");
469 concaveness /= (stems.size () - 2);
471 s = me->get_grob_property ("concaveness");
472 Real r = gh_scm2double (s);
474 /* TODO: some sort of damping iso -> plain horizontal */
477 Direction dir = Directional_element_interface::get (me);
478 Real y = gh_scm2double (me->get_grob_property ("y")) * dir;
479 Real dy = gh_scm2double (me->get_grob_property ("dy")) * dir;
481 Real adjusted_y = y + dy / 2;
482 /* Store true, not dir-corrected values */
483 me->set_grob_property ("y", gh_double2scm (adjusted_y * dir));
484 me->set_grob_property ("dy", gh_double2scm (0));
487 s = me->get_grob_property ("debug-concave");
491 Item *text = new Item (me->get_property ("TextScript"));
492 text->set_grob_property ("text",
493 ly_str02scm (to_str (concaveness).ch_C ())),
494 Side_position_interface::add_support (text, stem[0]);
496 printf ("concaveness: %.2f\n", concaveness);
500 return SCM_UNSPECIFIED;
504 This neat trick is by Werner Lemberg,
505 damped = tanh (slope)
506 corresponds with some tables in [Wanske]
508 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 1);
510 Beam::slope_damping (SCM smob)
512 Grob *me = unsmob_grob (smob);
514 if (visible_stem_count (me) <= 1)
515 return SCM_UNSPECIFIED;
517 SCM s = me->get_grob_property ("damping");
518 int damping = gh_scm2int (s);
522 /* y,dy in this function are corrected for beam-dir */
523 Direction dir = Directional_element_interface::get (me);
524 Real y = gh_scm2double (me->get_grob_property ("y")) * dir;
525 Real dy = gh_scm2double (me->get_grob_property ("dy")) * dir;
527 // ugh -> use commonx
528 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS)
529 - first_visible_stem (me)->relative_coordinate (0, X_AXIS);
530 Real dydx = dy && dx ? dy/dx : 0;
531 dydx = 0.6 * tanh (dydx) / damping;
533 Real damped_dy = dydx * dx;
534 Real adjusted_y = y + (dy - damped_dy) / 2;
535 /* Store true, not dir-corrected values */
536 me->set_grob_property ("y", gh_double2scm (adjusted_y * dir));
537 me->set_grob_property ("dy", gh_double2scm (damped_dy * dir));
539 return SCM_UNSPECIFIED;
543 Quantise dy (height) of beam.
544 Generalisation of [Ross].
546 MAKE_SCHEME_CALLBACK (Beam, quantise_dy, 1);
548 Beam::quantise_dy (SCM smob)
550 Grob *me = unsmob_grob (smob);
552 if (visible_stem_count (me) <= 1)
553 return SCM_UNSPECIFIED;
556 SCM proc = me->get_grob_property ("height-quants");
557 SCM quants = gh_call2 (proc, me->self_scm (),
558 gh_double2scm (me->paper_l ()->get_var ("stafflinethickness")
561 for (SCM s = quants; gh_pair_p (s); s = ly_cdr (s))
562 a.push (gh_scm2double (ly_car (s)));
566 /* y,dy in this function are corrected for beam-dir */
567 Direction dir = Directional_element_interface::get (me);
568 Real y = gh_scm2double (me->get_grob_property ("y")) * dir;
569 Real dy = gh_scm2double (me->get_grob_property ("dy")) * dir;
571 Real staff_space = Staff_symbol_referencer::staff_space (me);
573 Interval iv = quantise_iv (a, abs (dy)/staff_space) * staff_space;
574 Real q = (abs (dy) - iv[SMALLER] <= iv[BIGGER] - abs (dy))
578 if (to_boolean (me->get_grob_property ("quantise-dy-never-steeper"))
582 Real quantised_dy = q * sign (dy);
583 Real adjusted_y = y + (dy - quantised_dy) * 0.5;
584 /* Store true, not dir-corrected values */
585 me->set_grob_property ("y", gh_double2scm (adjusted_y * dir));
586 me->set_grob_property ("dy", gh_double2scm (quantised_dy * dir));
588 return SCM_UNSPECIFIED;
591 /* It's tricky to have the user override y,dy directly, so we use this
592 translation func. Also, if our staff_space != 1 (smaller staff, eg),
593 user will expect staff-position to be discrete values. */
594 MAKE_SCHEME_CALLBACK (Beam, user_override, 1);
596 Beam::user_override (SCM smob)
598 Grob *me = unsmob_grob (smob);
599 Real staff_space = Staff_symbol_referencer::staff_space (me);
601 SCM s = me->get_grob_property ("staff-position");
604 Real y = gh_scm2double (s) * staff_space * 0.5;
605 me->set_grob_property ("y", gh_double2scm (y));
608 /* Name suggestions? Tilt, slope, vertical-* ? */
609 s = me->get_grob_property ("height");
612 Real dy = gh_scm2double (s) * staff_space * 0.5;
613 me->set_grob_property ("dy", gh_double2scm (dy));
616 return SCM_UNSPECIFIED;
620 Ugh, this must be last, after user_override
621 Assumes directionised y/dy.
623 MAKE_SCHEME_CALLBACK (Beam, do_quantise_y, 1);
625 Beam::do_quantise_y (SCM smob)
627 Grob *me = unsmob_grob (smob);
630 If the user set y-position, we shouldn't do quanting.
632 if (gh_number_p (me->get_grob_property ("y-position-hs")))
633 return SCM_UNSPECIFIED;
635 Real y = gh_scm2double (me->get_grob_property ("y"));
636 Real dy = gh_scm2double (me->get_grob_property ("dy"));
638 /* we can modify y, so we should quantise y */
639 Real half_space = Staff_symbol_referencer::staff_space (me) / 2;
640 Real y_shift = check_stem_length_f (me, y, dy);
642 y = quantise_y_f (me, y, dy, 0);
645 Hmm, this is a bit keyhole operation: we're passing `this' as a
646 parameter, and member vars as SCM properties. We should decide on
647 SCM/C/C++ boundary */
648 me->set_grob_property ("y", gh_double2scm (y));
649 set_stem_lengths (me);
650 y = gh_scm2double (me->get_grob_property ("y"));
652 y_shift = check_stem_length_f (me, y, dy);
654 if (y_shift > half_space / 4)
659 for significantly lengthened or shortened stems,
660 request quanting the other way.
663 if (abs (y_shift) > half_space / 2)
664 quant_dir = sign (y_shift) * Directional_element_interface::get (me);
665 y = quantise_y_f (me, y, dy, quant_dir);
668 me->set_grob_property ("y", gh_double2scm (y));
669 // me->set_grob_property ("dy", gh_double2scm (dy));
670 return SCM_UNSPECIFIED;
675 Beam::calc_stem_y_f (Grob*me,Item* s, Real y, Real dy)
677 int beam_multiplicity = get_multiplicity (me);
678 int stem_multiplicity = (Stem::flag_i (s) - 2) >? 0;
680 SCM space_proc = me->get_grob_property ("space-function");
681 SCM space = gh_call1 (space_proc, gh_int2scm (beam_multiplicity));
683 Real thick = gh_scm2double (me->get_grob_property ("thickness")) ;
684 Real interbeam_f = gh_scm2double (space) ;
686 // ugh -> use commonx
687 Real x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
688 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
689 Real stem_y = (dy && dx ? (s->relative_coordinate (0, X_AXIS) - x0) / dx * dy : 0) + y;
692 Direction dir = Directional_element_interface::get (me);
693 Direction sdir = Directional_element_interface::get (s);
699 * (thick / 2 + (beam_multiplicity - 1) * interbeam_f);
703 // huh, why not for first visible?
704 if (Staff_symbol_referencer::staff_symbol_l (s)
705 != Staff_symbol_referencer::staff_symbol_l (last_visible_stem (me)))
706 stem_y += Directional_element_interface::get (me)
707 * (beam_multiplicity - stem_multiplicity) * interbeam_f;
713 /* Make very sure that we don't have stems that are too short.
714 Try our best not to have stems that are too long (think: knees).
716 Optionally (testing): try to lengthen more, to reach more ideal
719 Beam::check_stem_length_f (Grob*me,Real y, Real dy)
723 Direction dir = Directional_element_interface::get (me);
725 Link_array<Item> stems=
726 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
728 int ideal_lengthen_count = 0;
729 Real ideal_lengthen = 0;
731 for (int i=0; i < stems.size (); i++)
734 if (Stem::invisible_b (s))
737 Real stem_y = calc_stem_y_f (me, s, y, dy);
740 Stem_info info = Stem::calc_stem_info (s);
742 // if (0 > info.maxy_f_ - stem_y)
743 shorten = shorten <? info.maxy_f_ - stem_y;
744 // if (0 < info.miny_f_ - stem_y)
745 lengthen = lengthen >? info.miny_f_ - stem_y;
747 if (info.idealy_f_ - stem_y > 0)
749 ideal_lengthen += (info.idealy_f_ - stem_y);
750 ideal_lengthen_count++;
752 // too long is not so bad as too short
753 else if (0) //info.idealy_f_ - stem_y < 0)
755 ideal_lengthen += info.idealy_f_ - stem_y;
756 ideal_lengthen_count++;
760 if (lengthen && shorten)
761 me->warning (_ ("weird beam vertical offset"));
763 if (to_boolean (me->get_grob_property ("ideal-lengthen"))
764 && ideal_lengthen_count)
765 lengthen = (ideal_lengthen / ideal_lengthen_count) >? lengthen;
767 /* when all stems are too short, normal stems win */
768 return dir * ((shorten) ? shorten : lengthen);
772 Hmm. At this time, beam position and slope are determined. Maybe,
773 stem directions and length should set to relative to the chord's
774 position of the beam. */
776 Beam::set_stem_lengths (Grob *me)
778 if (visible_stem_count (me) <= 1)
781 Real y = gh_scm2double (me->get_grob_property ("y"));
782 Real dy = gh_scm2double (me->get_grob_property ("dy"));
784 Real half_space = Staff_symbol_referencer::staff_space (me)/2;
785 Link_array<Item> stems=
786 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
788 Grob *common = me->common_refpoint (stems[0], Y_AXIS);
789 for (int i=1; i < stems.size (); i++)
790 if (!Stem::invisible_b (stems[i]))
791 common = common->common_refpoint (stems[i], Y_AXIS);
793 for (int i=0; i < stems.size (); i++)
796 if (Stem::invisible_b (s))
799 Real stem_y = calc_stem_y_f (me, s, y, dy);
801 // doesn't play well with dvips
802 if (scm_definedp (ly_symbol2scm ("ps-testing"), SCM_UNDEFINED)
804 if (Stem::get_direction (s) == Directional_element_interface::get (me))
805 stem_y += Stem::get_direction (s)
806 * gh_scm2double (me->get_grob_property ("thickness")) / 2;
808 /* caution: stem measures in staff-positions */
809 Real id = me->relative_coordinate (common, Y_AXIS)
810 - stems[i]->relative_coordinate (common, Y_AXIS);
811 Stem::set_stemend (s, (stem_y + id) / half_space);
816 Prevent interference from stafflines and beams.
818 We only need to quantise the (left) y of the beam,
819 since dy is quantised too.
820 if extend_b then stems must *not* get shorter
823 Beam::quantise_y_f (Grob*me,Real y, Real dy, int quant_dir)
825 int multiplicity = get_multiplicity (me);
827 Real staff_space = Staff_symbol_referencer::staff_space (me);
828 Real thick = me->paper_l ()->get_var ("stafflinethickness");
831 SCM proc = me->get_grob_property ("vertical-position-quant-function");
832 SCM quants = scm_apply (proc,
834 scm_list_n (gh_int2scm (multiplicity),
835 gh_double2scm (dy/staff_space),
836 gh_double2scm (thick/staff_space),
837 SCM_EOL, SCM_UNDEFINED));
841 for (; gh_pair_p (quants); quants = ly_cdr (quants))
842 a.push (gh_scm2double (ly_car (quants)));
847 Real up_y = Directional_element_interface::get (me) * y;
848 Interval iv = quantise_iv (a, up_y/staff_space) * staff_space;
850 Real q = up_y - iv[SMALLER] <= iv[BIGGER] - up_y
851 ? iv[SMALLER] : iv[BIGGER];
853 q = iv[ (Direction)quant_dir];
855 return q * Directional_element_interface::get (me);
859 Beam::set_beaming (Grob*me,Beaming_info_list *beaming)
861 Link_array<Grob> stems=
862 Pointer_group_interface__extract_grobs (me, (Grob*)0, "stems");
865 for (int i=0; i < stems.size (); i++)
869 /* Don't overwrite user override (?) */
870 if (Stem::beam_count (stems[i], d) == -1
871 /* Don't set beaming for outside of outer stems */
872 && ! (d == LEFT && i == 0)
873 && ! (d == RIGHT && i == stems.size () -1))
875 int b = beaming->infos_.elem (i).beams_i_drul_[d];
876 Stem::set_beaming (stems[i], b, d);
879 while (flip (&d) != LEFT);
886 beams to go with one stem.
891 Beam::stem_beams (Grob*me,Item *here, Item *next, Item *prev,
892 Real /* dy */ , Real dydx
895 // ugh -> use commonx
896 if ((next && ! (next->relative_coordinate (0, X_AXIS) > here->relative_coordinate (0, X_AXIS))) ||
897 (prev && ! (prev->relative_coordinate (0, X_AXIS) < here->relative_coordinate (0, X_AXIS))))
898 programming_error ("Beams are not left-to-right");
900 int multiplicity = get_multiplicity (me);
902 SCM space_proc = me->get_grob_property ("space-function");
903 SCM space = gh_call1 (space_proc, gh_int2scm (multiplicity));
905 Real thick = gh_scm2double (me->get_grob_property ("thickness")) ;
906 Real interbeam_f = gh_scm2double (space) ;
908 Real bdy = interbeam_f;
911 // ugh -> use commonx
912 Real dx = visible_stem_count (me) ?
913 last_visible_stem (me)->relative_coordinate (0, X_AXIS) - first_visible_stem (me)->relative_coordinate (0, X_AXIS)
921 if (!Stem::first_head (here))
924 int t = Stem::type_i (here);
926 SCM proc = me->get_grob_property ("flag-width-function");
927 SCM result = gh_call1 (proc, gh_int2scm (t));
928 nw_f = gh_scm2double (result);
932 Direction dir = Directional_element_interface::get (me);
934 /* [Tremolo] beams on whole notes may not have direction set? */
936 dir = Directional_element_interface::get (here);
939 /* half beams extending to the left. */
942 int lhalfs= lhalfs = Stem::beam_count (here,LEFT) - Stem::beam_count (prev,RIGHT);
943 int lwholebeams= Stem::beam_count (here,LEFT) <? Stem::beam_count (prev,RIGHT) ;
945 Half beam should be one note-width,
946 but let's make sure two half-beams never touch
949 // FIXME: TODO (check) stem width / sloped beams
950 Real w = here->relative_coordinate (0, X_AXIS)
951 - prev->relative_coordinate (0, X_AXIS);
952 Real stem_w = gh_scm2double (prev->get_grob_property ("thickness"))
954 * me->paper_l ()->get_var ("stafflinethickness");
958 if (lhalfs) // generates warnings if not
959 a = Lookup::beam (dydx, w + stem_w, thick);
960 a.translate (Offset (-w, -w * dydx));
961 a.translate_axis (-stem_w/2, X_AXIS);
962 for (int j = 0; j < lhalfs; j++)
965 b.translate_axis (-dir * bdy * (lwholebeams+j), Y_AXIS);
966 leftbeams.add_molecule (b);
972 int rhalfs = Stem::beam_count (here,RIGHT)
973 - Stem::beam_count (next,LEFT);
974 int rwholebeams= Stem::beam_count (here,RIGHT)
975 <? Stem::beam_count (next,LEFT) ;
977 Real w = next->relative_coordinate (0, X_AXIS)
978 - here->relative_coordinate (0, X_AXIS);
980 Real stem_w = gh_scm2double (next->get_grob_property ("thickness"))
982 * me->paper_l ()->get_var ("stafflinethickness");
984 Molecule a = Lookup::beam (dydx, w + stem_w, thick);
985 a.translate_axis (- stem_w/2, X_AXIS);
989 SCM gap = me->get_grob_property ("gap");
990 if (gh_number_p (gap))
992 int gap_i = gh_scm2int ((gap));
993 int nogap = rwholebeams - gap_i;
995 for (; j < nogap; j++)
998 b.translate_axis (-dir * bdy * j, Y_AXIS);
999 rightbeams.add_molecule (b);
1001 if (Stem::invisible_b (here))
1006 a = Lookup::beam (dydx, w + stem_w, thick);
1009 for (; j < rwholebeams; j++)
1013 if (Stem::invisible_b (here))
1014 // ugh, see chord-tremolo.ly
1015 tx = (-dir + 1) / 2 * nw_f * 1.5 + gap_f/4;
1018 b.translate (Offset (tx, -dir * bdy * j));
1019 rightbeams.add_molecule (b);
1024 a = Lookup::beam (dydx, w, thick);
1026 for (; j < rwholebeams + rhalfs; j++)
1029 b.translate_axis (- dir * bdy * j, Y_AXIS);
1030 rightbeams.add_molecule (b);
1034 leftbeams.add_molecule (rightbeams);
1037 Does beam quanting think of the asymetry of beams?
1038 Refpoint is on bottom of symbol. (FIXTHAT) --hwn.
1043 MAKE_SCHEME_CALLBACK (Beam,brew_molecule,1);
1045 Beam::brew_molecule (SCM smob)
1047 Grob * me =unsmob_grob (smob);
1050 if (!gh_pair_p (me->get_grob_property ("stems")))
1053 Link_array<Item>stems =
1054 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
1055 if (visible_stem_count (me))
1057 // ugh -> use commonx
1058 x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
1059 dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
1063 x0 = stems[0]->relative_coordinate (0, X_AXIS);
1064 dx = stems.top ()->relative_coordinate (0, X_AXIS) - x0;
1070 TODO: the naming of the grob properties sucks.
1072 SCM dy_s = me->get_grob_property ("dy");
1073 SCM y_s = me->get_grob_property ("y");
1076 Real dy = gh_number_p (dy_s) ? gh_scm2double (dy_s) : 0.0;
1077 Real dydx = dy && dx ? dy/dx : 0;
1078 Real y = gh_number_p (y_s) ? gh_scm2double (y_s) : 0.0;
1081 for (int j=0; j <stems.size (); j++)
1084 Item * prev = (j > 0)? stems[j-1] : 0;
1085 Item * next = (j < stems.size ()-1) ? stems[j+1] :0;
1087 Molecule sb = stem_beams (me, i, next, prev, dy, dydx);
1088 Real x = i->relative_coordinate (0, X_AXIS)-x0;
1089 sb.translate (Offset (x, x * dydx + y));
1090 mol.add_molecule (sb);
1092 mol.translate_axis (x0
1093 - dynamic_cast<Spanner*> (me)->get_bound (LEFT)->relative_coordinate (0, X_AXIS), X_AXIS);
1095 return mol.smobbed_copy ();
1099 Beam::forced_stem_count (Grob*me)
1101 Link_array<Item>stems =
1102 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
1104 for (int i=0; i < stems.size (); i++)
1108 if (Stem::invisible_b (s))
1111 if (( (int)Stem::chord_start_f (s))
1112 && (Stem::get_direction (s) != Stem::get_default_dir (s)))
1122 use filter and standard list functions.
1125 Beam::visible_stem_count (Grob*me)
1127 Link_array<Item>stems =
1128 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
1130 for (int i = stems.size (); i--;)
1132 if (!Stem::invisible_b (stems[i]))
1139 Beam::first_visible_stem (Grob*me)
1141 Link_array<Item>stems =
1142 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
1144 for (int i = 0; i < stems.size (); i++)
1146 if (!Stem::invisible_b (stems[i]))
1153 Beam::last_visible_stem (Grob*me)
1155 Link_array<Item>stems =
1156 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
1157 for (int i = stems.size (); i--;)
1159 if (!Stem::invisible_b (stems[i]))
1168 handle rest under beam (do_post: beams are calculated now)
1169 what about combination of collisions and rest under beam.
1173 rest -> stem -> beam -> interpolate_y_position ()
1175 MAKE_SCHEME_CALLBACK (Beam,rest_collision_callback,2);
1177 Beam::rest_collision_callback (SCM element_smob, SCM axis)
1179 Grob *rest = unsmob_grob (element_smob);
1180 Axis a = (Axis) gh_scm2int (axis);
1182 assert (a == Y_AXIS);
1184 Grob * st = unsmob_grob (rest->get_grob_property ("stem"));
1187 return gh_double2scm (0.0);
1188 Grob * beam = unsmob_grob (stem->get_grob_property ("beam"));
1189 if (!beam || !Beam::has_interface (beam) || !Beam::visible_stem_count (beam))
1190 return gh_double2scm (0.0);
1192 // make callback for rest from this.
1197 // todo: make sure this calced already.
1198 SCM s = beam->get_grob_property ("dy");
1199 if (gh_number_p (s))
1200 beam_dy = gh_scm2double (s);
1202 s = beam->get_grob_property ("y");
1203 if (gh_number_p (s))
1204 beam_y = gh_scm2double (s);
1206 // ugh -> use commonx
1207 Real x0 = first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
1208 Real dx = last_visible_stem (beam)->relative_coordinate (0, X_AXIS) - x0;
1209 Real dydx = beam_dy && dx ? beam_dy/dx : 0;
1211 Direction d = Stem::get_direction (stem);
1212 Real beamy = (stem->relative_coordinate (0, X_AXIS) - x0) * dydx + beam_y;
1214 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1217 Real rest_dim = rest->extent (rest, Y_AXIS)[d]*2.0 / staff_space ; // refp??
1220 = gh_scm2double (rest->get_grob_property ("minimum-beam-collision-distance"));
1222 minimum_dist + -d * (beamy - rest_dim) >? 0;
1224 int stafflines = Staff_symbol_referencer::line_count (rest);
1226 // move discretely by half spaces.
1227 int discrete_dist = int (ceil (dist));
1229 // move by whole spaces inside the staff.
1230 if (discrete_dist < stafflines+1)
1231 discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
1233 return gh_double2scm (-d * discrete_dist);
1238 Beam::has_interface (Grob*me)
1240 return me->has_interface (ly_symbol2scm ("beam-interface"));