2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
13 - Determine auto knees based on positions if it's set by the user.
15 - the code is littered with * and / staff_space calls for
16 #'positions. Consider moving to real-world coordinates?
18 Problematic issue is user tweaks (user tweaks are in staff-coordinates.)
22 - Stems run to the Y-center of the beam.
24 - beam_translation is the offset between Y centers of the beam.
30 #include "directional-element-interface.hh"
32 #include "international.hh"
33 #include "interval-set.hh"
35 #include "least-squares.hh"
38 #include "output-def.hh"
39 #include "pointer-group-interface.hh"
41 #include "staff-symbol-referencer.hh"
45 #if DEBUG_BEAM_SCORING
46 #include "text-interface.hh" // debug output.
47 #include "font-interface.hh" // debug output.
53 Beam_stem_segment::Beam_stem_segment ()
63 Beam_segment::Beam_segment ()
69 Beam::add_stem (Grob *me, Grob *s)
71 if (Stem::get_beam (s))
73 programming_error ("Stem already has beam");
77 Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s);
78 s->set_object ("beam", me->self_scm ());
79 add_bound_item (dynamic_cast<Spanner *> (me), dynamic_cast<Item *> (s));
83 Beam::get_thickness (Grob *me)
85 return robust_scm2double (me->get_property ("thickness"), 0)
86 * Staff_symbol_referencer::staff_space (me);
89 /* Return the translation between 2 adjoining beams. */
91 Beam::get_beam_translation (Grob *me)
93 int beam_count = get_beam_count (me);
94 Real staff_space = Staff_symbol_referencer::staff_space (me);
95 Real line = Staff_symbol_referencer::line_thickness (me);
96 Real thickness = get_thickness (me);
97 Real fract = robust_scm2double (me->get_property ("length-fraction"), 1.0);
99 Real beam_translation = beam_count < 4
100 ? (2 * staff_space + line - thickness) / 2.0
101 : (3 * staff_space + line - thickness) / 3.0;
103 return fract * beam_translation;
106 /* Maximum beam_count. */
108 Beam::get_beam_count (Grob *me)
112 extract_grob_set (me, "stems", stems);
113 for (vsize i = 0; i < stems.size (); i++)
115 Grob *stem = stems[i];
116 m = max (m, (Stem::beam_multiplicity (stem).length () + 1));
122 MAKE_SCHEME_CALLBACK (Beam, calc_direction, 1);
124 Beam::calc_direction (SCM smob)
126 Grob *me = unsmob_grob (smob);
128 /* Beams with less than 2 two stems don't make much sense, but could happen
133 For a beam that only has one stem, we try to do some disappearance magic:
134 we revert the flag, and move on to The Eternal Engraving Fields. */
136 Direction d = CENTER;
138 int count = visible_stem_count (me);
141 extract_grob_set (me, "stems", stems);
142 if (stems.size () == 1)
144 me->warning (_ ("removing beam with less than two stems"));
146 stems[0]->set_object ("beam", SCM_EOL);
149 return SCM_UNSPECIFIED;
151 else if (stems.size () == 0)
154 return SCM_UNSPECIFIED;
158 Grob *stem = first_visible_stem (me);
161 ugh: stems[0] case happens for chord tremolo.
163 d = to_dir ((stem ? stem : stems[0])->get_property ("default-direction"));
170 d = get_default_dir (me);
172 consider_auto_knees (me);
177 set_stem_directions (me, d);
180 return scm_from_int (d);
185 /* We want a maximal number of shared beams, but if there is choice, we
186 * take the one that is closest to the end of the stem. This is for
198 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
202 Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
206 for (int i = lslice[-left_dir];
207 (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
210 for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
212 int k = -right_dir * scm_to_int (scm_car (s)) + i;
213 if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F)
217 if (count >= best_count)
227 MAKE_SCHEME_CALLBACK(Beam, calc_beaming, 1)
229 Beam::calc_beaming (SCM smob)
231 Grob *me = unsmob_grob (smob);
233 extract_grob_set (me, "stems", stems);
236 last_int.set_empty ();
238 SCM last_beaming = scm_cons (SCM_EOL, scm_list_1 (scm_from_int (0)));
239 Direction last_dir = CENTER;
240 for (vsize i = 0; i < stems.size (); i++)
242 Grob *this_stem = stems[i];
243 SCM this_beaming = this_stem->get_property ("beaming");
245 Direction this_dir = get_grob_direction (this_stem);
246 if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming))
248 int start_point = position_with_maximal_common_beams
249 (last_beaming, this_beaming,
250 last_dir ? last_dir : this_dir,
257 new_slice.set_empty ();
258 SCM s = index_get_cell (this_beaming, d);
259 for (; scm_is_pair (s); s = scm_cdr (s))
262 = start_point - this_dir * scm_to_int (scm_car (s));
264 new_slice.add_point (new_beam_pos);
265 scm_set_car_x (s, scm_from_int (new_beam_pos));
268 while (flip (&d) != LEFT);
270 if (!new_slice.is_empty ())
271 last_int = new_slice;
275 SCM s = scm_cdr (this_beaming);
276 for (; scm_is_pair (s); s = scm_cdr (s))
278 int np = -this_dir * scm_to_int (scm_car (s));
279 scm_set_car_x (s, scm_from_int (np));
280 last_int.add_point (np);
284 if (scm_ilength (scm_cdr (this_beaming)) > 0)
286 last_beaming = this_beaming;
295 operator <(Beam_stem_segment const &a,
296 Beam_stem_segment const &b)
298 return a.rank_ < b.rank_;
301 typedef map<int, vector<Beam_stem_segment> > Position_stem_segments_map;
304 Beam::get_beam_segments (Grob *me_grob, Grob **common)
306 Spanner *me = dynamic_cast<Spanner*> (me_grob);
308 extract_grob_set (me, "stems", stems);
309 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
311 commonx = me->get_bound (LEFT)->common_refpoint (commonx, X_AXIS);
312 commonx = me->get_bound (RIGHT)->common_refpoint (commonx, X_AXIS);
316 int gap_count = robust_scm2int (me->get_property ("gap-count"), 0);
317 Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
319 Position_stem_segments_map stem_segments;
320 Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
324 for (vsize i = 0; i < stems.size (); i++)
326 Grob *stem = stems[i];
327 Real stem_width = robust_scm2double (stem->get_property ("thickness"), 1.0) * lt;
328 Real stem_x = stem->relative_coordinate (commonx, X_AXIS);
329 SCM beaming = stem->get_property ("beaming");
333 for (SCM s = index_get_cell (beaming, d);
334 scm_is_pair (s); s = scm_cdr (s))
336 if (!scm_is_integer (scm_car (s)))
339 int beam_rank = scm_to_int (scm_car (s));
340 ranks.add_point (beam_rank);
343 for (SCM s = index_get_cell (beaming, d);
344 scm_is_pair (s); s = scm_cdr (s))
346 if (!scm_is_integer (scm_car (s)))
349 int beam_rank = scm_to_int (scm_car (s));
350 Beam_stem_segment seg;
352 seg.stem_x_ = stem_x;
353 seg.rank_ = 2 * i + (d+1)/2;
354 seg.width_ = stem_width;
357 Direction stem_dir = get_grob_direction (stem);
360 = (stem_dir * beam_rank < (stem_dir * ranks[-stem_dir] + gap_count));
361 stem_segments[beam_rank].push_back (seg);
364 while (flip (&d) != LEFT);
367 Drul_array<Real> break_overshoot
368 = robust_scm2drul (me->get_property ("break-overshoot"),
369 Drul_array<Real> (-0.5, 0.0));
371 vector<Beam_segment> segments;
372 for (Position_stem_segments_map::const_iterator i (stem_segments.begin ());
373 i != stem_segments.end (); i++)
375 vector<Beam_stem_segment> segs = (*i).second;
376 vector_sort (segs, default_compare);
378 Beam_segment current;
379 current.vertical_count_ = (*i).first;
380 for (vsize j = 0; j < segs.size (); j++)
383 event_dir == LEFT: left edge of a beamsegment.
385 Direction event_dir = LEFT;
388 Drul_array<bool> on_bound (j == 0 && event_dir==LEFT,
389 j == segs.size() - 1 && event_dir==RIGHT);
390 Drul_array<bool> inside (j > 0, j < segs.size()-1);
391 bool event = on_bound[event_dir]
392 || abs (segs[j].rank_ - segs[j+event_dir].rank_) > 1;
397 current.vertical_count_ = (*i).first;
398 current.horizontal_[event_dir] = segs[j].stem_x_;
399 if (segs[j].dir_ == event_dir)
401 if (on_bound[event_dir] && me->get_bound (event_dir)->break_status_dir ())
403 current.horizontal_[event_dir] += event_dir * break_overshoot[event_dir];
407 Real notehead_width =
408 Stem::duration_log (segs[j].stem_) == 1
409 ? 1.98 : 1.32; // URG.
411 if (inside[event_dir])
412 notehead_width = min (notehead_width,
413 fabs (segs[j+ event_dir].stem_x_
414 - segs[j].stem_x_)/2);
416 current.horizontal_[event_dir] += event_dir * notehead_width;
421 current.horizontal_[event_dir] += event_dir * segs[j].width_/2;
423 current.horizontal_[event_dir] -= event_dir * gap_length;
426 if (event_dir == RIGHT)
428 current.vertical_count_ = (*i).first;
429 segments.push_back (current);
430 current = Beam_segment();
433 while (flip (&event_dir) != LEFT);
441 MAKE_SCHEME_CALLBACK(Beam, print, 1);
443 Beam::print (SCM grob)
445 Spanner *me = unsmob_spanner (grob);
447 vector<Beam_segment> segments = get_beam_segments (me, &commonx);
450 if (visible_stem_count (me))
452 x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
453 dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
457 extract_grob_set (me, "stems", stems);
458 x0 = stems[0]->relative_coordinate (commonx, X_AXIS);
459 dx = stems.back ()->relative_coordinate (commonx, X_AXIS) - x0;
462 Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
464 SCM posns = me->get_property ("quantized-positions");
465 Drul_array<Real> pos;
466 if (!is_number_pair (posns))
468 programming_error ("no beam positions?");
469 pos = Interval (0, 0);
472 pos = ly_scm2realdrul (posns);
474 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
476 Real dy = pos[RIGHT] - pos[LEFT];
477 Real slope = (dy && dx) ? dy / dx : 0;
479 Real thick = get_thickness (me);
480 Real beam_dy = get_beam_translation (me);
484 for (vsize i = 0; i < segments.size (); i ++)
486 Stencil b = Lookup::beam (slope, segments[i].horizontal_.length (), thick, blot);
488 b.translate_axis (segments[i].horizontal_[LEFT], X_AXIS);
489 b.translate_axis (slope * (segments[i].horizontal_[LEFT] - x0)
490 + beam_dy * segments[i].vertical_count_, Y_AXIS);
491 the_beam.add_stencil (b);
494 #if (DEBUG_BEAM_SCORING)
495 SCM quant_score = me->get_property ("quant-score");
496 SCM debug = me->layout ()->lookup_variable (ly_symbol2scm ("debug-beam-scoring"));
497 if (to_boolean (debug) && scm_is_string (quant_score))
499 extract_grob_set (me, "stems", stems);
502 This code prints the demerits for each beam. Perhaps this
503 should be switchable for those who want to twiddle with the
507 SCM properties = Font_interface::text_font_alist_chain (me);
509 Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP;
511 Stencil score = *unsmob_stencil (Text_interface::interpret_markup
512 (me->layout ()->self_scm (), properties, quant_score));
514 if (!score.is_empty ())
515 the_beam.add_at_edge (Y_AXIS, stem_dir, score, 1.0, 0);
519 the_beam.translate_axis (pos[LEFT], Y_AXIS);
520 the_beam.translate_axis (-me->relative_coordinate (commonx, X_AXIS), X_AXIS);
521 return the_beam.smobbed_copy ();
525 Beam::get_default_dir (Grob *me)
527 extract_grob_set (me, "stems", stems);
529 Drul_array<Real> extremes (0.0, 0.0);
530 for (iterof (s, stems); s != stems.end (); s++)
532 Interval positions = Stem::head_positions (*s);
536 if (sign (positions[d]) == d)
537 extremes[d] = d * max (d * positions[d], d * extremes[d]);
539 while (flip (&d) != DOWN);
542 Drul_array<int> total (0, 0);
543 Drul_array<int> count (0, 0);
545 bool force_dir = false;
546 for (vsize i = 0; i < stems.size (); i++)
549 Direction stem_dir = CENTER;
550 SCM stem_dir_scm = s->get_property_data (ly_symbol2scm ("direction"));
551 if (is_direction (stem_dir_scm))
553 stem_dir = to_dir (stem_dir_scm);
557 stem_dir = to_dir (s->get_property ("default-direction"));
560 stem_dir = to_dir (s->get_property ("neutral-direction"));
565 total[stem_dir] += max (int (- stem_dir * Stem::head_positions (s) [-stem_dir]), 0);
572 if (abs (extremes[UP]) > -extremes[DOWN])
574 else if (extremes[UP] < -extremes[DOWN])
578 Direction dir = CENTER;
579 Direction d = CENTER;
580 if ((d = (Direction) sign (count[UP] - count[DOWN])))
584 && (d = (Direction) sign (total[UP] / count[UP] - total[DOWN]/count[DOWN])))
586 else if ((d = (Direction) sign (total[UP] - total[DOWN])))
589 dir = to_dir (me->get_property ("neutral-direction"));
594 /* Set all stems with non-forced direction to beam direction.
595 Urg: non-forced should become `without/with unforced' direction,
596 once stem gets cleaned-up. */
598 Beam::set_stem_directions (Grob *me, Direction d)
600 extract_grob_set (me, "stems", stems);
602 for (vsize i = 0; i < stems.size (); i++)
606 SCM forcedir = s->get_property_data (ly_symbol2scm ("direction"));
607 if (!to_dir (forcedir))
608 set_grob_direction (s, d);
613 Only try horizontal beams for knees. No reliable detection of
614 anything else is possible here, since we don't know funky-beaming
615 settings, or X-distances (slopes!) People that want sloped
616 knee-beams, should set the directions manually.
621 this routine should take into account the stemlength scoring
622 of a possible knee/nonknee beam.
625 Beam::consider_auto_knees (Grob *me)
627 SCM scm = me->get_property ("auto-knee-gap");
628 if (!scm_is_number (scm))
635 extract_grob_set (me, "stems", stems);
637 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
638 Real staff_space = Staff_symbol_referencer::staff_space (me);
640 vector<Interval> head_extents_array;
641 for (vsize i = 0; i < stems.size (); i++)
643 Grob *stem = stems[i];
644 if (Stem::is_invisible (stem))
647 Interval head_extents = Stem::head_positions (stem);
648 if (!head_extents.is_empty ())
650 head_extents[LEFT] += -1;
651 head_extents[RIGHT] += 1;
652 head_extents *= staff_space * 0.5;
655 We could subtract beam Y position, but this routine only
656 sets stem directions, a constant shift does not have an
659 head_extents += stem->relative_coordinate (common, Y_AXIS);
661 if (to_dir (stem->get_property_data (ly_symbol2scm ("direction"))))
663 Direction stemdir = to_dir (stem->get_property ("direction"));
664 head_extents[-stemdir] = -stemdir * infinity_f;
667 head_extents_array.push_back (head_extents);
669 gaps.remove_interval (head_extents);
673 Real max_gap_len = 0.0;
675 for (vsize i = gaps.allowed_regions_.size () -1; i != VPOS ;i--)
677 Interval gap = gaps.allowed_regions_[i];
680 the outer gaps are not knees.
682 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
685 if (gap.length () >= max_gap_len)
687 max_gap_len = gap.length ();
692 Real beam_translation = get_beam_translation (me);
693 Real beam_thickness = Beam::get_thickness (me);
694 int beam_count = Beam::get_beam_count (me);
695 Real height_of_beams = beam_thickness / 2
696 + (beam_count - 1) * beam_translation;
697 Real threshold = scm_to_double (scm) + height_of_beams;
699 if (max_gap_len > threshold)
702 for (vsize i = 0; i < stems.size (); i++)
704 Grob *stem = stems[i];
705 if (Stem::is_invisible (stem))
708 Interval head_extents = head_extents_array[j++];
710 Direction d = (head_extents.center () < max_gap.center ())
713 stem->set_property ("direction", scm_from_int (d));
715 head_extents.intersect (max_gap);
716 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
721 /* Set stem's shorten property if unset.
724 take some y-position (chord/beam/nearest?) into account
725 scmify forced-fraction
727 This is done in beam because the shorten has to be uniform over the
734 set_minimum_dy (Grob *me, Real *dy)
739 If dy is smaller than the smallest quant, we
740 get absurd direction-sign penalties.
743 Real ss = Staff_symbol_referencer::staff_space (me);
744 Real thickness = Beam::get_thickness (me) / ss;
745 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
746 Real sit = (thickness - slt) / 2;
748 Real hang = 1.0 - (thickness - slt) / 2;
750 *dy = sign (*dy) * max (fabs (*dy),
751 min (min (sit, inter), hang));
757 MAKE_SCHEME_CALLBACK(Beam, calc_stem_shorten, 1)
759 Beam::calc_stem_shorten (SCM smob)
761 Grob *me = unsmob_grob (smob);
764 shortening looks silly for x staff beams
767 return scm_from_int (0);
769 Real forced_fraction = 1.0 * forced_stem_count (me)
770 / visible_stem_count (me);
772 int beam_count = get_beam_count (me);
774 SCM shorten_list = me->get_property ("beamed-stem-shorten");
775 if (shorten_list == SCM_EOL)
776 return scm_from_int (0);
778 Real staff_space = Staff_symbol_referencer::staff_space (me);
781 = robust_list_ref (beam_count -1, shorten_list);
782 Real shorten = scm_to_double (shorten_elt) * staff_space;
784 shorten *= forced_fraction;
788 return scm_from_double (shorten);
790 return scm_from_double (0.0);
796 Compute a first approximation to the beam slope.
798 MAKE_SCHEME_CALLBACK (Beam, calc_least_squares_positions, 2);
800 Beam::calc_least_squares_positions (SCM smob, SCM posns)
804 Grob *me = unsmob_grob (smob);
806 int count = visible_stem_count (me);
809 return ly_interval2scm (pos);
811 vector<Real> x_posns;
812 extract_grob_set (me, "stems", stems);
813 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
814 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
816 Real my_y = me->relative_coordinate (commony, Y_AXIS);
818 Grob *fvs = first_visible_stem (me);
819 Grob *lvs = last_visible_stem (me);
821 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
822 + fvs->relative_coordinate (commony, Y_AXIS) -my_y,
823 Stem::get_stem_info (lvs).ideal_y_
824 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
826 Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
827 for (vsize i = 0; i < stems.size (); i++)
831 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
832 x_posns.push_back (x);
834 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
842 Interval chord (Stem::chord_start_y (first_visible_stem (me)),
843 Stem::chord_start_y (last_visible_stem (me)));
845 /* Simple beams (2 stems) on middle line should be allowed to be
848 However, if both stems reach middle line,
849 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
851 For that case, we apply artificial slope */
852 if (!ideal[LEFT] && chord.delta () && count == 2)
855 Direction d = (Direction) (sign (chord.delta ()) * UP);
856 pos[d] = get_thickness (me) / 2;
863 For broken beams this doesn't work well. In this case, the
864 slope esp. of the first part of a broken beam should predict
865 where the second part goes.
867 ldy = pos[RIGHT] - pos[LEFT];
871 vector<Offset> ideals;
872 for (vsize i = 0; i < stems.size (); i++)
875 if (Stem::is_invisible (s))
877 ideals.push_back (Offset (x_posns[i],
878 Stem::get_stem_info (s).ideal_y_
879 + s->relative_coordinate (commony, Y_AXIS)
883 minimise_least_squares (&slope, &y, ideals);
887 set_minimum_dy (me, &dy);
890 pos = Interval (y, (y + dy));
894 "position" is relative to the staff.
896 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
898 me->set_property ("least-squares-dy", scm_from_double (ldy));
899 return ly_interval2scm (pos);
903 We can't combine with previous function, since check concave and
904 slope damping comes first.
906 TODO: we should use the concaveness to control the amount of damping
909 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 2);
911 Beam::shift_region_to_valid (SCM grob, SCM posns)
913 Grob *me = unsmob_grob (grob);
917 vector<Real> x_posns;
918 extract_grob_set (me, "stems", stems);
919 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
920 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
922 Grob *fvs = first_visible_stem (me);
927 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
928 for (vsize i = 0; i < stems.size (); i++)
932 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
933 x_posns.push_back (x);
936 Grob *lvs = last_visible_stem (me);
940 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
942 Drul_array<Real> pos = ly_scm2interval (posns);
945 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
947 Real dy = pos[RIGHT] - pos[LEFT];
949 Real slope = dx ? (dy / dx) : 0.0;
952 Shift the positions so that we have a chance of finding good
953 quants (i.e. no short stem failures.)
955 Interval feasible_left_point;
956 feasible_left_point.set_full ();
957 for (vsize i = 0; i < stems.size (); i++)
960 if (Stem::is_invisible (s))
963 Direction d = get_grob_direction (s);
966 = Stem::get_stem_info (s).shortest_y_
967 - slope * x_posns [i];
970 left_y is now relative to the stem S. We want relative to
971 ourselves, so translate:
974 += + s->relative_coordinate (commony, Y_AXIS)
975 - me->relative_coordinate (commony, Y_AXIS);
981 feasible_left_point.intersect (flp);
984 if (feasible_left_point.is_empty ())
985 warning (_ ("no viable initial configuration found: may not find good beam slope"));
986 else if (!feasible_left_point.contains (y))
988 const int REGION_SIZE = 2; // UGH UGH
989 if (isinf (feasible_left_point[DOWN]))
990 y = feasible_left_point[UP] - REGION_SIZE;
991 else if (isinf (feasible_left_point[UP]))
992 y = feasible_left_point[DOWN]+ REGION_SIZE;
994 y = feasible_left_point.center ();
997 pos = Drul_array<Real> (y, (y + dy));
998 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1000 return ly_interval2scm (pos);
1003 /* This neat trick is by Werner Lemberg,
1004 damped = tanh (slope)
1005 corresponds with some tables in [Wanske] CHECKME */
1006 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 2);
1008 Beam::slope_damping (SCM smob, SCM posns)
1010 Grob *me = unsmob_grob (smob);
1011 Drul_array<Real> pos = ly_scm2interval (posns);
1013 if (visible_stem_count (me) <= 1)
1017 SCM s = me->get_property ("damping");
1018 Real damping = scm_to_double (s);
1019 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
1020 if (concaveness >= 10000)
1022 pos[LEFT] = pos[RIGHT];
1023 me->set_property ("least-squares-dy", scm_from_double (0));
1029 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1031 Real dy = pos[RIGHT] - pos[LEFT];
1033 Grob *fvs = first_visible_stem (me);
1034 Grob *lvs = last_visible_stem (me);
1036 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1038 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
1039 - first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
1041 Real slope = dy && dx ? dy / dx : 0;
1043 slope = 0.6 * tanh (slope) / (damping + concaveness);
1045 Real damped_dy = slope * dx;
1047 set_minimum_dy (me, &damped_dy);
1049 pos[LEFT] += (dy - damped_dy) / 2;
1050 pos[RIGHT] -= (dy - damped_dy) / 2;
1052 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1055 return ly_interval2scm (pos);
1059 Report slice containing the numbers that are both in (car BEAMING)
1063 where_are_the_whole_beams (SCM beaming)
1067 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1069 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1071 l.add_point (scm_to_int (scm_car (s)));
1077 /* Return the Y position of the stem-end, given the Y-left, Y-right
1078 in POS for stem S. This Y position is relative to S. */
1080 Beam::calc_stem_y (Grob *me, Grob *s, Grob ** common,
1082 Drul_array<Real> pos, bool french)
1084 Real beam_translation = get_beam_translation (me);
1086 Real r = s->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1087 Real dy = pos[RIGHT] - pos[LEFT];
1089 Real stem_y_beam0 = (dy && dx
1094 Direction my_dir = get_grob_direction (s);
1095 SCM beaming = s->get_property ("beaming");
1097 Real stem_y = stem_y_beam0;
1100 Slice bm = where_are_the_whole_beams (beaming);
1101 if (!bm.is_empty ())
1102 stem_y += beam_translation * bm[-my_dir];
1106 Slice bm = Stem::beam_multiplicity (s);
1107 if (!bm.is_empty ())
1108 stem_y += bm[my_dir] * beam_translation;
1111 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1112 - s->relative_coordinate (common[Y_AXIS], Y_AXIS);
1118 Hmm. At this time, beam position and slope are determined. Maybe,
1119 stem directions and length should set to relative to the chord's
1120 position of the beam. */
1121 MAKE_SCHEME_CALLBACK(Beam, set_stem_lengths, 1);
1123 Beam::set_stem_lengths (SCM smob)
1125 Grob *me = unsmob_grob (smob);
1127 /* trigger callback. */
1128 (void) me->get_property ("direction");
1130 SCM posns = me->get_property ("positions");
1132 extract_grob_set (me, "stems", stems);
1137 for (int a = 2; a--;)
1138 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1140 Drul_array<Real> pos = ly_scm2realdrul (posns);
1141 Real staff_space = Staff_symbol_referencer::staff_space (me);
1142 scale_drul (&pos, staff_space);
1146 if (scm_is_number (me->get_property ("gap-count"))
1147 && scm_to_int (me->get_property ("gap-count")))
1150 thick = get_thickness (me);
1153 Grob *fvs = first_visible_stem (me);
1154 Grob *lvs = last_visible_stem (me);
1156 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1157 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1159 for (vsize i = 0; i < stems.size (); i++)
1162 if (Stem::is_invisible (s))
1165 bool french = to_boolean (s->get_property ("french-beaming"));
1166 Real stem_y = calc_stem_y (me, s, common,
1168 pos, french && s != lvs && s!= fvs);
1171 Make the stems go up to the end of the beam. This doesn't matter
1172 for normal beams, but for tremolo beams it looks silly otherwise.
1175 stem_y += thick * 0.5 * get_grob_direction (s);
1177 Stem::set_stemend (s, 2 * stem_y / staff_space);
1184 Beam::set_beaming (Grob *me, Beaming_info_list const *beaming)
1186 extract_grob_set (me, "stems", stems);
1189 for (vsize i = 0; i < stems.size (); i++)
1192 Don't overwrite user settings.
1196 Grob *stem = stems[i];
1197 SCM beaming_prop = stem->get_property ("beaming");
1198 if (beaming_prop == SCM_EOL
1199 || index_get_cell (beaming_prop, d) == SCM_EOL)
1201 int b = beaming->infos_.at (i).beams_i_drul_[d];
1203 && i < stems.size () -1
1204 && Stem::is_invisible (stem))
1205 b = min (b, beaming->infos_.at (i).beams_i_drul_[-d]);
1207 Stem::set_beaming (stem, b, d);
1210 while (flip (&d) != LEFT);
1215 Beam::forced_stem_count (Grob *me)
1217 extract_grob_set (me, "stems", stems);
1220 for (vsize i = 0; i < stems.size (); i++)
1224 if (Stem::is_invisible (s))
1227 /* I can imagine counting those boundaries as a half forced stem,
1228 but let's count them full for now. */
1229 Direction defdir = to_dir (s->get_property ("default-direction"));
1231 if (abs (Stem::chord_start_y (s)) > 0.1
1233 && get_grob_direction (s) != defdir)
1240 Beam::visible_stem_count (Grob *me)
1242 extract_grob_set (me, "stems", stems);
1244 for (vsize i = stems.size (); i--;)
1246 if (!Stem::is_invisible (stems[i]))
1253 Beam::first_visible_stem (Grob *me)
1255 extract_grob_set (me, "stems", stems);
1257 for (vsize i = 0; i < stems.size (); i++)
1259 if (!Stem::is_invisible (stems[i]))
1266 Beam::last_visible_stem (Grob *me)
1268 extract_grob_set (me, "stems", stems);
1270 for (vsize i = stems.size (); i--;)
1272 if (!Stem::is_invisible (stems[i]))
1281 handle rest under beam (do_post: beams are calculated now)
1282 what about combination of collisions and rest under beam.
1286 rest -> stem -> beam -> interpolate_y_position ()
1288 MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2);
1290 Beam::rest_collision_callback (SCM smob, SCM prev_offset)
1292 Grob *rest = unsmob_grob (smob);
1293 if (scm_is_number (rest->get_property ("staff-position")))
1294 return scm_from_int (0);
1296 Real offset = robust_scm2double (prev_offset, 0.0);
1298 Grob *st = unsmob_grob (rest->get_object ("stem"));
1301 return scm_from_double (0.0);
1302 Grob *beam = unsmob_grob (stem->get_object ("beam"));
1304 || !Beam::has_interface (beam)
1305 || !Beam::visible_stem_count (beam))
1306 return scm_from_double (0.0);
1308 Drul_array<Real> pos (0, 0);
1309 SCM s = beam->get_property ("positions");
1310 if (scm_is_pair (s) && scm_is_number (scm_car (s)))
1311 pos = ly_scm2interval (s);
1313 programming_error ("positions property should always be pair of numbers.");
1315 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1317 scale_drul (&pos, staff_space);
1319 Real dy = pos[RIGHT] - pos[LEFT];
1321 Drul_array<Grob*> visible_stems (first_visible_stem (beam),
1322 last_visible_stem (beam));
1323 extract_grob_set (beam, "stems", stems);
1325 Grob *common = common_refpoint_of_array (stems, beam, X_AXIS);
1327 Real x0 = visible_stems[LEFT]->relative_coordinate (common, X_AXIS);
1328 Real dx = visible_stems[RIGHT]->relative_coordinate (common, X_AXIS) - x0;
1329 Real slope = dy && dx ? dy / dx : 0;
1331 Direction d = get_grob_direction (stem);
1332 Real stem_y = pos[LEFT]
1333 + (stem->relative_coordinate (common, X_AXIS) - x0) * slope;
1335 Real beam_translation = get_beam_translation (beam);
1336 Real beam_thickness = Beam::get_thickness (beam);
1339 TODO: this is not strictly correct for 16th knee beams.
1342 = Stem::beam_multiplicity (stem).length () + 1;
1344 Real height_of_my_beams = beam_thickness / 2
1345 + (beam_count - 1) * beam_translation;
1346 Real beam_y = stem_y - d * height_of_my_beams;
1348 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1349 Interval rest_extent = rest->extent (common_y, Y_AXIS);
1350 rest_extent.translate (offset);
1352 Real rest_dim = rest_extent[d];
1353 Real minimum_distance
1354 = staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1355 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1357 Real shift = d * min (((beam_y - d * minimum_distance) - rest_dim) * d, 0.0);
1359 shift /= staff_space;
1360 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1362 /* Always move discretely by half spaces */
1363 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1365 /* Inside staff, move by whole spaces*/
1366 if ((rest_extent[d] + staff_space * shift) * d
1368 || (rest_extent[-d] + staff_space * shift) * -d
1370 shift = ceil (fabs (shift)) * sign (shift);
1372 return scm_from_double (staff_space * shift);
1376 Beam::is_knee (Grob *me)
1378 SCM k = me->get_property ("knee");
1379 if (scm_is_bool (k))
1380 return ly_scm2bool (k);
1384 extract_grob_set (me, "stems", stems);
1385 for (vsize i = stems.size (); i--;)
1387 Direction dir = get_grob_direction (stems[i]);
1396 me->set_property ("knee", ly_bool2scm (knee));
1402 Beam::get_direction_beam_count (Grob *me, Direction d)
1404 extract_grob_set (me, "stems", stems);
1407 for (vsize i = stems.size (); i--;)
1410 Should we take invisible stems into account?
1412 if (get_grob_direction (stems[i]) == d)
1413 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1419 ADD_INTERFACE (Beam,
1423 "The @code{thickness} property is the weight of beams, "
1424 "measured in staffspace. The @code{direction} "
1425 "property is not user-serviceable. Use "
1426 "the @code{direction} property of @code{Stem} instead. "
1432 "beamed-stem-shorten "
1446 "neutral-direction "
1449 "quantized-positions "