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.
29 #include "beaming-pattern.hh"
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 ()
55 max_connect_ = 1000; // infinity
64 Beam_segment::Beam_segment ()
70 Beam::add_stem (Grob *me, Grob *s)
72 if (Stem::get_beam (s))
74 programming_error ("Stem already has beam");
78 Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s);
79 s->set_object ("beam", me->self_scm ());
80 add_bound_item (dynamic_cast<Spanner *> (me), dynamic_cast<Item *> (s));
84 Beam::get_thickness (Grob *me)
86 return robust_scm2double (me->get_property ("thickness"), 0)
87 * Staff_symbol_referencer::staff_space (me);
90 /* Return the translation between 2 adjoining beams. */
92 Beam::get_beam_translation (Grob *me)
94 int beam_count = get_beam_count (me);
95 Real staff_space = Staff_symbol_referencer::staff_space (me);
96 Real line = Staff_symbol_referencer::line_thickness (me);
97 Real thickness = get_thickness (me);
98 Real fract = robust_scm2double (me->get_property ("length-fraction"), 1.0);
100 Real beam_translation = beam_count < 4
101 ? (2 * staff_space + line - thickness) / 2.0
102 : (3 * staff_space + line - thickness) / 3.0;
104 return fract * beam_translation;
107 /* Maximum beam_count. */
109 Beam::get_beam_count (Grob *me)
113 extract_grob_set (me, "stems", stems);
114 for (vsize i = 0; i < stems.size (); i++)
116 Grob *stem = stems[i];
117 m = max (m, (Stem::beam_multiplicity (stem).length () + 1));
123 MAKE_SCHEME_CALLBACK (Beam, calc_direction, 1);
125 Beam::calc_direction (SCM smob)
127 Grob *me = unsmob_grob (smob);
129 /* Beams with less than 2 two stems don't make much sense, but could happen
136 Direction dir = CENTER;
138 int count = visible_stem_count (me);
141 extract_grob_set (me, "stems", stems);
142 if (stems.size () == 0)
144 me->warning (_ ("removing beam with no stems"));
147 return SCM_UNSPECIFIED;
151 Grob *stem = first_visible_stem (me);
154 ugh: stems[0] case happens for chord tremolo.
156 dir = to_dir ((stem ? stem : stems[0])->get_property ("default-direction"));
163 dir = get_default_dir (me);
165 consider_auto_knees (me);
170 set_stem_directions (me, dir);
173 return scm_from_int (dir);
178 /* We want a maximal number of shared beams, but if there is choice, we
179 * take the one that is closest to the end of the stem. This is for
191 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
195 Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
199 for (int i = lslice[-left_dir];
200 (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
203 for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
205 int k = -right_dir * scm_to_int (scm_car (s)) + i;
206 if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F)
210 if (count >= best_count)
220 MAKE_SCHEME_CALLBACK(Beam, calc_beaming, 1)
222 Beam::calc_beaming (SCM smob)
224 Grob *me = unsmob_grob (smob);
226 extract_grob_set (me, "stems", stems);
229 last_int.set_empty ();
231 SCM last_beaming = scm_cons (SCM_EOL, scm_list_1 (scm_from_int (0)));
232 Direction last_dir = CENTER;
233 for (vsize i = 0; i < stems.size (); i++)
235 Grob *this_stem = stems[i];
236 SCM this_beaming = this_stem->get_property ("beaming");
238 Direction this_dir = get_grob_direction (this_stem);
239 if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming))
241 int start_point = position_with_maximal_common_beams
242 (last_beaming, this_beaming,
243 last_dir ? last_dir : this_dir,
250 new_slice.set_empty ();
251 SCM s = index_get_cell (this_beaming, d);
252 for (; scm_is_pair (s); s = scm_cdr (s))
255 = start_point - this_dir * scm_to_int (scm_car (s));
257 new_slice.add_point (new_beam_pos);
258 scm_set_car_x (s, scm_from_int (new_beam_pos));
261 while (flip (&d) != LEFT);
263 if (!new_slice.is_empty ())
264 last_int = new_slice;
268 SCM s = scm_cdr (this_beaming);
269 for (; scm_is_pair (s); s = scm_cdr (s))
271 int np = -this_dir * scm_to_int (scm_car (s));
272 scm_set_car_x (s, scm_from_int (np));
273 last_int.add_point (np);
277 if (scm_ilength (scm_cdr (this_beaming)) > 0)
279 last_beaming = this_beaming;
288 operator <(Beam_stem_segment const &a,
289 Beam_stem_segment const &b)
291 return a.rank_ < b.rank_;
294 typedef map<int, vector<Beam_stem_segment> > Position_stem_segments_map;
297 Beam::get_beam_segments (Grob *me_grob, Grob **common)
299 /* ugh, this has a side-effect that we need to ensure that
300 Stem #'beaming is correct */
301 (void) me_grob->get_property ("quantized-positions");
303 Spanner *me = dynamic_cast<Spanner*> (me_grob);
305 extract_grob_set (me, "stems", stems);
306 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
308 commonx = me->get_bound (LEFT)->common_refpoint (commonx, X_AXIS);
309 commonx = me->get_bound (RIGHT)->common_refpoint (commonx, X_AXIS);
313 int gap_count = robust_scm2int (me->get_property ("gap-count"), 0);
314 Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
316 Position_stem_segments_map stem_segments;
317 Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
321 for (vsize i = 0; i < stems.size (); i++)
323 Grob *stem = stems[i];
324 Real stem_width = robust_scm2double (stem->get_property ("thickness"), 1.0) * lt;
325 Real stem_x = stem->relative_coordinate (commonx, X_AXIS);
326 SCM beaming = stem->get_property ("beaming");
330 for (SCM s = index_get_cell (beaming, d);
331 scm_is_pair (s); s = scm_cdr (s))
333 if (!scm_is_integer (scm_car (s)))
336 int beam_rank = scm_to_int (scm_car (s));
337 ranks.add_point (beam_rank);
340 for (SCM s = index_get_cell (beaming, d);
341 scm_is_pair (s); s = scm_cdr (s))
343 if (!scm_is_integer (scm_car (s)))
346 int beam_rank = scm_to_int (scm_car (s));
347 Beam_stem_segment seg;
349 seg.stem_x_ = stem_x;
350 seg.rank_ = 2 * i + (d+1)/2;
351 seg.width_ = stem_width;
354 seg.max_connect_ = robust_scm2int (stem->get_property ("max-beam-connect"), 1000);
356 Direction stem_dir = get_grob_direction (stem);
359 = (stem_dir * beam_rank < (stem_dir * ranks[-stem_dir] + gap_count));
360 stem_segments[beam_rank].push_back (seg);
363 while (flip (&d) != LEFT);
366 Drul_array<Real> break_overshoot
367 = robust_scm2drul (me->get_property ("break-overshoot"),
368 Drul_array<Real> (-0.5, 0.0));
370 vector<Beam_segment> segments;
371 for (Position_stem_segments_map::const_iterator i (stem_segments.begin ());
372 i != stem_segments.end (); i++)
374 vector<Beam_stem_segment> segs = (*i).second;
375 vector_sort (segs, less<Beam_stem_segment> ());
377 Beam_segment current;
379 int 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 bool on_bound = (event_dir == LEFT) ? j == 0 :
389 j == segs.size() - 1;
391 bool inside_stem = (event_dir == LEFT)
392 ? segs[j].stem_index_ > 0
393 : segs[j].stem_index_ < stems.size () - 1;
395 bool event = on_bound
396 || abs (segs[j].rank_ - segs[j+event_dir].rank_) > 1
397 || (abs (vertical_count) >= segs[j].max_connect_
398 || abs (vertical_count) >= segs[j + event_dir].max_connect_);
403 current.vertical_count_ = vertical_count;
404 current.horizontal_[event_dir] = segs[j].stem_x_;
405 if (segs[j].dir_ == event_dir)
408 && me->get_bound (event_dir)->break_status_dir ())
410 current.horizontal_[event_dir]
411 = (me->get_bound (event_dir)->extent (commonx, X_AXIS)[RIGHT]
412 + event_dir * break_overshoot[event_dir]);
416 Real notehead_width =
417 Stem::duration_log (segs[j].stem_) == 1
424 Grob *neighbor_stem = stems[segs[j].stem_index_ + event_dir];
425 Real neighbor_stem_x = neighbor_stem->relative_coordinate (commonx, X_AXIS);
427 notehead_width = min (notehead_width,
428 fabs (neighbor_stem_x - segs[j].stem_x_)/2);
430 current.horizontal_[event_dir] += event_dir * notehead_width;
435 current.horizontal_[event_dir] += event_dir * segs[j].width_/2;
437 current.horizontal_[event_dir] -= event_dir * gap_length;
440 if (event_dir == RIGHT)
442 segments.push_back (current);
443 current = Beam_segment();
446 while (flip (&event_dir) != LEFT);
454 MAKE_SCHEME_CALLBACK(Beam, print, 1);
456 Beam::print (SCM grob)
458 Spanner *me = unsmob_spanner (grob);
460 vector<Beam_segment> segments = get_beam_segments (me, &commonx);
463 if (visible_stem_count (me))
465 span[LEFT] = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
466 span[RIGHT] = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
470 extract_grob_set (me, "stems", stems);
471 span[LEFT] = stems[0]->relative_coordinate (commonx, X_AXIS);
472 span[RIGHT] = stems.back ()->relative_coordinate (commonx, X_AXIS);
475 Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
477 SCM posns = me->get_property ("quantized-positions");
479 if (!is_number_pair (posns))
481 programming_error ("no beam positions?");
482 pos = Interval (0, 0);
485 pos = ly_scm2realdrul (posns);
487 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
489 Real dy = pos[RIGHT] - pos[LEFT];
490 Real slope = (dy && span.length ()) ? dy / span.length () : 0;
492 Real thick = get_thickness (me);
493 Real beam_dy = get_beam_translation (me);
495 Direction feather_dir = to_dir (me->get_property ("grow-direction"));
498 for (vsize i = 0; i < segments.size (); i ++)
500 Real local_slope = slope;
503 local_slope += feather_dir * segments[i].vertical_count_ * beam_dy / span.length ();
506 Stencil b = Lookup::beam (local_slope, segments[i].horizontal_.length (), thick, blot);
508 b.translate_axis (segments[i].horizontal_[LEFT], X_AXIS);
510 b.translate_axis (local_slope
511 * (segments[i].horizontal_[LEFT] - span.linear_combination (feather_dir))
512 + pos.linear_combination (feather_dir)
513 + beam_dy * segments[i].vertical_count_, Y_AXIS);
514 the_beam.add_stencil (b);
517 #if (DEBUG_BEAM_SCORING)
518 SCM quant_score = me->get_property ("quant-score");
519 SCM debug = me->layout ()->lookup_variable (ly_symbol2scm ("debug-beam-scoring"));
520 if (to_boolean (debug) && scm_is_string (quant_score))
522 extract_grob_set (me, "stems", stems);
525 This code prints the demerits for each beam. Perhaps this
526 should be switchable for those who want to twiddle with the
530 SCM properties = Font_interface::text_font_alist_chain (me);
532 Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP;
534 Stencil score = *unsmob_stencil (Text_interface::interpret_markup
535 (me->layout ()->self_scm (), properties, quant_score));
537 if (!score.is_empty ())
538 the_beam.add_at_edge (Y_AXIS, stem_dir, score, 1.0, 0);
542 the_beam.translate_axis (-me->relative_coordinate (commonx, X_AXIS), X_AXIS);
543 return the_beam.smobbed_copy ();
547 Beam::get_default_dir (Grob *me)
549 extract_grob_set (me, "stems", stems);
551 Drul_array<Real> extremes (0.0, 0.0);
552 for (iterof (s, stems); s != stems.end (); s++)
554 Interval positions = Stem::head_positions (*s);
558 if (sign (positions[d]) == d)
559 extremes[d] = d * max (d * positions[d], d * extremes[d]);
561 while (flip (&d) != DOWN);
564 Drul_array<int> total (0, 0);
565 Drul_array<int> count (0, 0);
567 bool force_dir = false;
568 for (vsize i = 0; i < stems.size (); i++)
571 Direction stem_dir = CENTER;
572 SCM stem_dir_scm = s->get_property_data ("direction");
573 if (is_direction (stem_dir_scm))
575 stem_dir = to_dir (stem_dir_scm);
579 stem_dir = to_dir (s->get_property ("default-direction"));
582 stem_dir = to_dir (s->get_property ("neutral-direction"));
587 total[stem_dir] += max (int (- stem_dir * Stem::head_positions (s) [-stem_dir]), 0);
594 if (abs (extremes[UP]) > -extremes[DOWN])
596 else if (extremes[UP] < -extremes[DOWN])
600 Direction dir = CENTER;
601 Direction d = CENTER;
602 if ((d = (Direction) sign (count[UP] - count[DOWN])))
606 && (d = (Direction) sign (total[UP] / count[UP] - total[DOWN]/count[DOWN])))
608 else if ((d = (Direction) sign (total[UP] - total[DOWN])))
611 dir = to_dir (me->get_property ("neutral-direction"));
616 /* Set all stems with non-forced direction to beam direction.
617 Urg: non-forced should become `without/with unforced' direction,
618 once stem gets cleaned-up. */
620 Beam::set_stem_directions (Grob *me, Direction d)
622 extract_grob_set (me, "stems", stems);
624 for (vsize i = 0; i < stems.size (); i++)
628 SCM forcedir = s->get_property_data ("direction");
629 if (!to_dir (forcedir))
630 set_grob_direction (s, d);
635 Only try horizontal beams for knees. No reliable detection of
636 anything else is possible here, since we don't know funky-beaming
637 settings, or X-distances (slopes!) People that want sloped
638 knee-beams, should set the directions manually.
643 this routine should take into account the stemlength scoring
644 of a possible knee/nonknee beam.
647 Beam::consider_auto_knees (Grob *me)
649 SCM scm = me->get_property ("auto-knee-gap");
650 if (!scm_is_number (scm))
657 extract_grob_set (me, "stems", stems);
659 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
660 Real staff_space = Staff_symbol_referencer::staff_space (me);
662 vector<Interval> head_extents_array;
663 for (vsize i = 0; i < stems.size (); i++)
665 Grob *stem = stems[i];
666 if (Stem::is_invisible (stem))
669 Interval head_extents = Stem::head_positions (stem);
670 if (!head_extents.is_empty ())
672 head_extents[LEFT] += -1;
673 head_extents[RIGHT] += 1;
674 head_extents *= staff_space * 0.5;
677 We could subtract beam Y position, but this routine only
678 sets stem directions, a constant shift does not have an
681 head_extents += stem->relative_coordinate (common, Y_AXIS);
683 if (to_dir (stem->get_property_data ("direction")))
685 Direction stemdir = to_dir (stem->get_property ("direction"));
686 head_extents[-stemdir] = -stemdir * infinity_f;
689 head_extents_array.push_back (head_extents);
691 gaps.remove_interval (head_extents);
695 Real max_gap_len = 0.0;
697 for (vsize i = gaps.allowed_regions_.size () -1; i != VPOS ;i--)
699 Interval gap = gaps.allowed_regions_[i];
702 the outer gaps are not knees.
704 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
707 if (gap.length () >= max_gap_len)
709 max_gap_len = gap.length ();
714 Real beam_translation = get_beam_translation (me);
715 Real beam_thickness = Beam::get_thickness (me);
716 int beam_count = Beam::get_beam_count (me);
717 Real height_of_beams = beam_thickness / 2
718 + (beam_count - 1) * beam_translation;
719 Real threshold = scm_to_double (scm) + height_of_beams;
721 if (max_gap_len > threshold)
724 for (vsize i = 0; i < stems.size (); i++)
726 Grob *stem = stems[i];
727 if (Stem::is_invisible (stem))
730 Interval head_extents = head_extents_array[j++];
732 Direction d = (head_extents.center () < max_gap.center ())
735 stem->set_property ("direction", scm_from_int (d));
737 head_extents.intersect (max_gap);
738 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
743 /* Set stem's shorten property if unset.
746 take some y-position (chord/beam/nearest?) into account
747 scmify forced-fraction
749 This is done in beam because the shorten has to be uniform over the
756 set_minimum_dy (Grob *me, Real *dy)
761 If dy is smaller than the smallest quant, we
762 get absurd direction-sign penalties.
765 Real ss = Staff_symbol_referencer::staff_space (me);
766 Real thickness = Beam::get_thickness (me) / ss;
767 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
768 Real sit = (thickness - slt) / 2;
770 Real hang = 1.0 - (thickness - slt) / 2;
772 *dy = sign (*dy) * max (fabs (*dy),
773 min (min (sit, inter), hang));
779 MAKE_SCHEME_CALLBACK(Beam, calc_stem_shorten, 1)
781 Beam::calc_stem_shorten (SCM smob)
783 Grob *me = unsmob_grob (smob);
786 shortening looks silly for x staff beams
789 return scm_from_int (0);
791 Real forced_fraction = 1.0 * forced_stem_count (me)
792 / visible_stem_count (me);
794 int beam_count = get_beam_count (me);
796 SCM shorten_list = me->get_property ("beamed-stem-shorten");
797 if (shorten_list == SCM_EOL)
798 return scm_from_int (0);
800 Real staff_space = Staff_symbol_referencer::staff_space (me);
803 = robust_list_ref (beam_count -1, shorten_list);
804 Real shorten = scm_to_double (shorten_elt) * staff_space;
806 shorten *= forced_fraction;
810 return scm_from_double (shorten);
812 return scm_from_double (0.0);
818 Compute a first approximation to the beam slope.
820 MAKE_SCHEME_CALLBACK (Beam, calc_least_squares_positions, 2);
822 Beam::calc_least_squares_positions (SCM smob, SCM posns)
826 Grob *me = unsmob_grob (smob);
828 int count = visible_stem_count (me);
831 return ly_interval2scm (pos);
833 vector<Real> x_posns;
834 extract_grob_set (me, "stems", stems);
835 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
836 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
838 Real my_y = me->relative_coordinate (commony, Y_AXIS);
840 Grob *fvs = first_visible_stem (me);
841 Grob *lvs = last_visible_stem (me);
843 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
844 + fvs->relative_coordinate (commony, Y_AXIS) - my_y,
845 Stem::get_stem_info (lvs).ideal_y_
846 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
848 Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
849 for (vsize i = 0; i < stems.size (); i++)
853 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
854 x_posns.push_back (x);
856 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
864 Interval chord (Stem::chord_start_y (first_visible_stem (me)),
865 Stem::chord_start_y (last_visible_stem (me)));
867 /* Simple beams (2 stems) on middle line should be allowed to be
870 However, if both stems reach middle line,
871 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
873 For that case, we apply artificial slope */
874 if (!ideal[LEFT] && chord.delta () && count == 2)
877 Direction d = (Direction) (sign (chord.delta ()) * UP);
878 pos[d] = get_thickness (me) / 2;
885 For broken beams this doesn't work well. In this case, the
886 slope esp. of the first part of a broken beam should predict
887 where the second part goes.
889 ldy = pos[RIGHT] - pos[LEFT];
893 vector<Offset> ideals;
894 for (vsize i = 0; i < stems.size (); i++)
897 if (!Stem::is_normal_stem (s))
900 ideals.push_back (Offset (x_posns[i],
901 Stem::get_stem_info (s).ideal_y_
902 + s->relative_coordinate (commony, Y_AXIS)
906 minimise_least_squares (&slope, &y, ideals);
910 set_minimum_dy (me, &dy);
913 pos = Interval (y, (y + dy));
917 "position" is relative to the staff.
919 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
921 me->set_property ("least-squares-dy", scm_from_double (ldy));
922 return ly_interval2scm (pos);
926 We can't combine with previous function, since check concave and
927 slope damping comes first.
929 TODO: we should use the concaveness to control the amount of damping
932 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 2);
934 Beam::shift_region_to_valid (SCM grob, SCM posns)
936 Grob *me = unsmob_grob (grob);
940 vector<Real> x_posns;
941 extract_grob_set (me, "stems", stems);
942 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
943 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
945 Grob *fvs = first_visible_stem (me);
950 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
951 for (vsize i = 0; i < stems.size (); i++)
955 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
956 x_posns.push_back (x);
959 Grob *lvs = last_visible_stem (me);
963 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
965 Drul_array<Real> pos = ly_scm2interval (posns);
968 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
970 Real dy = pos[RIGHT] - pos[LEFT];
972 Real slope = dx ? (dy / dx) : 0.0;
975 Shift the positions so that we have a chance of finding good
976 quants (i.e. no short stem failures.)
978 Interval feasible_left_point;
979 feasible_left_point.set_full ();
980 for (vsize i = 0; i < stems.size (); i++)
983 if (Stem::is_invisible (s))
986 Direction d = get_grob_direction (s);
989 = Stem::get_stem_info (s).shortest_y_
990 - slope * x_posns [i];
993 left_y is now relative to the stem S. We want relative to
994 ourselves, so translate:
997 += + s->relative_coordinate (commony, Y_AXIS)
998 - me->relative_coordinate (commony, Y_AXIS);
1004 feasible_left_point.intersect (flp);
1007 if (feasible_left_point.is_empty ())
1008 warning (_ ("no viable initial configuration found: may not find good beam slope"));
1009 else if (!feasible_left_point.contains (y))
1011 const int REGION_SIZE = 2; // UGH UGH
1012 if (isinf (feasible_left_point[DOWN]))
1013 y = feasible_left_point[UP] - REGION_SIZE;
1014 else if (isinf (feasible_left_point[UP]))
1015 y = feasible_left_point[DOWN]+ REGION_SIZE;
1017 y = feasible_left_point.center ();
1020 pos = Drul_array<Real> (y, (y + dy));
1021 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1023 return ly_interval2scm (pos);
1026 /* This neat trick is by Werner Lemberg,
1027 damped = tanh (slope)
1028 corresponds with some tables in [Wanske] CHECKME */
1029 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 2);
1031 Beam::slope_damping (SCM smob, SCM posns)
1033 Grob *me = unsmob_grob (smob);
1034 Drul_array<Real> pos = ly_scm2interval (posns);
1036 if (visible_stem_count (me) <= 1)
1040 SCM s = me->get_property ("damping");
1041 Real damping = scm_to_double (s);
1042 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
1043 if (concaveness >= 10000)
1045 pos[LEFT] = pos[RIGHT];
1046 me->set_property ("least-squares-dy", scm_from_double (0));
1052 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1054 Real dy = pos[RIGHT] - pos[LEFT];
1056 Grob *fvs = first_visible_stem (me);
1057 Grob *lvs = last_visible_stem (me);
1059 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1061 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
1062 - first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
1064 Real slope = dy && dx ? dy / dx : 0;
1066 slope = 0.6 * tanh (slope) / (damping + concaveness);
1068 Real damped_dy = slope * dx;
1070 set_minimum_dy (me, &damped_dy);
1072 pos[LEFT] += (dy - damped_dy) / 2;
1073 pos[RIGHT] -= (dy - damped_dy) / 2;
1075 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1078 return ly_interval2scm (pos);
1082 Report slice containing the numbers that are both in (car BEAMING)
1086 where_are_the_whole_beams (SCM beaming)
1090 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1092 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1094 l.add_point (scm_to_int (scm_car (s)));
1100 /* Return the Y position of the stem-end, given the Y-left, Y-right
1101 in POS for stem S. This Y position is relative to S. */
1103 Beam::calc_stem_y (Grob *me, Grob *stem, Grob **common,
1105 Drul_array<Real> pos, bool french)
1107 Real beam_translation = get_beam_translation (me);
1109 Real r = stem->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1110 Real dy = pos[RIGHT] - pos[LEFT];
1112 Real stem_y_beam0 = (dy && dx
1117 Direction my_dir = get_grob_direction (stem);
1118 SCM beaming = stem->get_property ("beaming");
1120 Real stem_y = stem_y_beam0;
1123 Slice bm = where_are_the_whole_beams (beaming);
1124 if (!bm.is_empty ())
1125 stem_y += beam_translation * bm[-my_dir];
1129 Slice bm = Stem::beam_multiplicity (stem);
1130 if (!bm.is_empty ())
1131 stem_y += bm[my_dir] * beam_translation;
1134 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1135 - stem->relative_coordinate (common[Y_AXIS], Y_AXIS);
1141 Hmm. At this time, beam position and slope are determined. Maybe,
1142 stem directions and length should set to relative to the chord's
1143 position of the beam. */
1144 MAKE_SCHEME_CALLBACK(Beam, set_stem_lengths, 1);
1146 Beam::set_stem_lengths (SCM smob)
1148 Grob *me = unsmob_grob (smob);
1150 /* trigger callbacks. */
1151 (void) me->get_property ("direction");
1152 (void) me->get_property ("beaming");
1154 SCM posns = me->get_property ("positions");
1156 extract_grob_set (me, "stems", stems);
1161 for (int a = 2; a--;)
1162 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1164 Drul_array<Real> pos = ly_scm2realdrul (posns);
1165 Real staff_space = Staff_symbol_referencer::staff_space (me);
1166 scale_drul (&pos, staff_space);
1170 if (robust_scm2int (me->get_property ("gap-count"), 0))
1173 thick = get_thickness (me);
1176 Grob *fvs = first_visible_stem (me);
1177 Grob *lvs = last_visible_stem (me);
1179 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1180 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1182 for (vsize i = 0; i < stems.size (); i++)
1186 bool french = to_boolean (s->get_property ("french-beaming"));
1187 Real stem_y = calc_stem_y (me, s, common,
1189 pos, french && s != lvs && s!= fvs);
1192 Make the stems go up to the end of the beam. This doesn't matter
1193 for normal beams, but for tremolo beams it looks silly otherwise.
1196 && !Stem::is_invisible (s))
1197 stem_y += thick * 0.5 * get_grob_direction (s);
1200 Do set_stemend for invisible stems too, so tuplet brackets
1201 have a reference point for sloping
1203 Stem::set_stemend (s, 2 * stem_y / staff_space);
1210 Beam::set_beaming (Grob *me, Beaming_pattern const *beaming)
1212 extract_grob_set (me, "stems", stems);
1215 for (vsize i = 0; i < stems.size (); i++)
1218 Don't overwrite user settings.
1222 Grob *stem = stems[i];
1223 SCM beaming_prop = stem->get_property ("beaming");
1224 if (beaming_prop == SCM_EOL
1225 || index_get_cell (beaming_prop, d) == SCM_EOL)
1227 int count = beaming->beamlet_count (i, d);
1229 && i < stems.size () -1
1230 && Stem::is_invisible (stem))
1231 count = min (count, beaming->beamlet_count (i,-d));
1233 if ( ((i == 0 && d == LEFT)
1234 || (i == stems.size ()-1 && d == RIGHT))
1235 && stems.size () > 1
1236 && to_boolean (me->get_property ("clip-edges")))
1239 Stem::set_beaming (stem, count, d);
1242 while (flip (&d) != LEFT);
1247 Beam::forced_stem_count (Grob *me)
1249 extract_grob_set (me, "stems", stems);
1252 for (vsize i = 0; i < stems.size (); i++)
1256 if (Stem::is_invisible (s))
1259 /* I can imagine counting those boundaries as a half forced stem,
1260 but let's count them full for now. */
1261 Direction defdir = to_dir (s->get_property ("default-direction"));
1263 if (abs (Stem::chord_start_y (s)) > 0.1
1265 && get_grob_direction (s) != defdir)
1272 Beam::visible_stem_count (Grob *me)
1274 extract_grob_set (me, "stems", stems);
1276 for (vsize i = stems.size (); i--;)
1278 if (!Stem::is_invisible (stems[i]))
1285 Beam::first_visible_stem (Grob *me)
1287 extract_grob_set (me, "stems", stems);
1289 for (vsize i = 0; i < stems.size (); i++)
1291 if (Stem::is_normal_stem (stems[i]))
1298 Beam::last_visible_stem (Grob *me)
1300 extract_grob_set (me, "stems", stems);
1302 for (vsize i = stems.size (); i--;)
1304 if (Stem::is_normal_stem (stems[i]))
1313 handle rest under beam (do_post: beams are calculated now)
1314 what about combination of collisions and rest under beam.
1318 rest -> stem -> beam -> interpolate_y_position ()
1320 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Beam, rest_collision_callback, 2, 1);
1322 Beam::rest_collision_callback (SCM smob, SCM prev_offset)
1324 Grob *rest = unsmob_grob (smob);
1325 if (scm_is_number (rest->get_property ("staff-position")))
1326 return scm_from_int (0);
1328 Real offset = robust_scm2double (prev_offset, 0.0);
1330 Grob *st = unsmob_grob (rest->get_object ("stem"));
1333 return scm_from_double (0.0);
1334 Grob *beam = unsmob_grob (stem->get_object ("beam"));
1336 || !Beam::has_interface (beam)
1337 || !Beam::visible_stem_count (beam))
1338 return scm_from_double (0.0);
1340 Drul_array<Real> pos (robust_scm2drul (beam->get_property ("positions"),
1341 Drul_array<Real> (0,0)));
1343 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1345 scale_drul (&pos, staff_space);
1347 Real dy = pos[RIGHT] - pos[LEFT];
1349 Drul_array<Grob*> visible_stems (first_visible_stem (beam),
1350 last_visible_stem (beam));
1351 extract_grob_set (beam, "stems", stems);
1353 Grob *common = common_refpoint_of_array (stems, beam, X_AXIS);
1355 Real x0 = visible_stems[LEFT]->relative_coordinate (common, X_AXIS);
1356 Real dx = visible_stems[RIGHT]->relative_coordinate (common, X_AXIS) - x0;
1357 Real slope = dy && dx ? dy / dx : 0;
1359 Direction d = get_grob_direction (stem);
1360 Real stem_y = pos[LEFT]
1361 + (stem->relative_coordinate (common, X_AXIS) - x0) * slope;
1363 Real beam_translation = get_beam_translation (beam);
1364 Real beam_thickness = Beam::get_thickness (beam);
1367 TODO: this is not strictly correct for 16th knee beams.
1370 = Stem::beam_multiplicity (stem).length () + 1;
1372 Real height_of_my_beams = beam_thickness / 2
1373 + (beam_count - 1) * beam_translation;
1374 Real beam_y = stem_y - d * height_of_my_beams;
1376 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1379 TODO: this is dubious, because this call needs the info we're
1380 computing right now.
1382 Interval rest_extent = rest->extent (common_y, Y_AXIS);
1383 rest_extent.translate (offset);
1385 Real rest_dim = rest_extent[d];
1386 Real minimum_distance
1387 = staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1388 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1390 Real shift = d * min (d * (beam_y - d * minimum_distance - rest_dim), 0.0);
1392 shift /= staff_space;
1393 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1395 /* Always move discretely by half spaces */
1396 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1398 /* Inside staff, move by whole spaces*/
1399 if ((rest_extent[d] + staff_space * shift) * d
1401 || (rest_extent[-d] + staff_space * shift) * -d
1403 shift = ceil (fabs (shift)) * sign (shift);
1405 return scm_from_double (offset + staff_space * shift);
1409 Beam::is_knee (Grob *me)
1411 SCM k = me->get_property ("knee");
1412 if (scm_is_bool (k))
1413 return ly_scm2bool (k);
1417 extract_grob_set (me, "stems", stems);
1418 for (vsize i = stems.size (); i--;)
1420 Direction dir = get_grob_direction (stems[i]);
1429 me->set_property ("knee", ly_bool2scm (knee));
1435 Beam::get_direction_beam_count (Grob *me, Direction d)
1437 extract_grob_set (me, "stems", stems);
1440 for (vsize i = stems.size (); i--;)
1443 Should we take invisible stems into account?
1445 if (get_grob_direction (stems[i]) == d)
1446 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1452 ADD_INTERFACE (Beam,
1455 "The @code{thickness} property is the weight of beams, "
1456 "measured in staffspace. The @code{direction} "
1457 "property is not user-serviceable. Use "
1458 "the @code{direction} property of @code{Stem} instead. "
1464 "beamed-stem-shorten "
1479 "neutral-direction "
1482 "quantized-positions "