2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 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"
44 #include "grob-array.hh"
46 #if DEBUG_BEAM_SCORING
47 #include "text-interface.hh" // debug output.
48 #include "font-interface.hh" // debug output.
54 Beam_stem_segment::Beam_stem_segment ()
56 max_connect_ = 1000; // infinity
65 Beam_segment::Beam_segment ()
71 Beam::add_stem (Grob *me, Grob *s)
73 if (Stem::get_beam (s))
75 programming_error ("Stem already has beam");
79 Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s);
80 s->set_object ("beam", me->self_scm ());
81 add_bound_item (dynamic_cast<Spanner *> (me), dynamic_cast<Item *> (s));
85 Beam::get_thickness (Grob *me)
87 return robust_scm2double (me->get_property ("thickness"), 0)
88 * Staff_symbol_referencer::staff_space (me);
91 /* Return the translation between 2 adjoining beams. */
93 Beam::get_beam_translation (Grob *me)
95 int beam_count = get_beam_count (me);
96 Real staff_space = Staff_symbol_referencer::staff_space (me);
97 Real line = Staff_symbol_referencer::line_thickness (me);
98 Real thickness = get_thickness (me);
99 Real fract = robust_scm2double (me->get_property ("length-fraction"), 1.0);
101 Real beam_translation = beam_count < 4
102 ? (2 * staff_space + line - thickness) / 2.0
103 : (3 * staff_space + line - thickness) / 3.0;
105 return fract * beam_translation;
108 /* Maximum beam_count. */
110 Beam::get_beam_count (Grob *me)
114 extract_grob_set (me, "stems", stems);
115 for (vsize i = 0; i < stems.size (); i++)
117 Grob *stem = stems[i];
118 m = max (m, (Stem::beam_multiplicity (stem).length () + 1));
123 MAKE_SCHEME_CALLBACK (Beam, calc_normal_stems, 1);
125 Beam::calc_normal_stems (SCM smob)
127 Grob *me = unsmob_grob (smob);
129 extract_grob_set (me, "stems", stems);
130 SCM val = Grob_array::make_array ();
131 Grob_array *ga = unsmob_grob_array (val);
132 for (vsize i = 0; i < stems.size (); i++)
133 if (Stem::is_normal_stem (stems[i]))
139 MAKE_SCHEME_CALLBACK (Beam, calc_direction, 1);
141 Beam::calc_direction (SCM smob)
143 Grob *me = unsmob_grob (smob);
145 /* Beams with less than 2 two stems don't make much sense, but could happen
152 Direction dir = CENTER;
154 int count = normal_stem_count (me);
157 extract_grob_set (me, "stems", stems);
158 if (stems.size () == 0)
160 me->warning (_ ("removing beam with no stems"));
163 return SCM_UNSPECIFIED;
167 Grob *stem = first_normal_stem (me);
170 ugh: stems[0] case happens for chord tremolo.
172 dir = to_dir ((stem ? stem : stems[0])->get_property ("default-direction"));
179 dir = get_default_dir (me);
181 consider_auto_knees (me);
186 set_stem_directions (me, dir);
189 return scm_from_int (dir);
194 /* We want a maximal number of shared beams, but if there is choice, we
195 * take the one that is closest to the end of the stem. This is for
207 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
211 Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
215 for (int i = lslice[-left_dir];
216 (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
219 for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
221 int k = -right_dir * scm_to_int (scm_car (s)) + i;
222 if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F)
226 if (count >= best_count)
236 MAKE_SCHEME_CALLBACK (Beam, calc_beaming, 1)
238 Beam::calc_beaming (SCM smob)
240 Grob *me = unsmob_grob (smob);
242 extract_grob_set (me, "stems", stems);
245 last_int.set_empty ();
247 SCM last_beaming = scm_cons (SCM_EOL, scm_list_1 (scm_from_int (0)));
248 Direction last_dir = CENTER;
249 for (vsize i = 0; i < stems.size (); i++)
251 Grob *this_stem = stems[i];
252 SCM this_beaming = this_stem->get_property ("beaming");
254 Direction this_dir = get_grob_direction (this_stem);
255 if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming))
257 int start_point = position_with_maximal_common_beams
258 (last_beaming, this_beaming,
259 last_dir ? last_dir : this_dir,
266 new_slice.set_empty ();
267 SCM s = index_get_cell (this_beaming, d);
268 for (; scm_is_pair (s); s = scm_cdr (s))
271 = start_point - this_dir * scm_to_int (scm_car (s));
273 new_slice.add_point (new_beam_pos);
274 scm_set_car_x (s, scm_from_int (new_beam_pos));
277 while (flip (&d) != LEFT);
279 if (!new_slice.is_empty ())
280 last_int = new_slice;
285 FIXME: what's this for?
287 SCM s = scm_cdr (this_beaming);
288 for (; scm_is_pair (s); s = scm_cdr (s))
290 int np = -this_dir * scm_to_int (scm_car (s));
291 scm_set_car_x (s, scm_from_int (np));
292 last_int.add_point (np);
296 if (scm_ilength (scm_cdr (this_beaming)) > 0)
298 last_beaming = this_beaming;
307 operator <(Beam_stem_segment const &a,
308 Beam_stem_segment const &b)
310 return a.rank_ < b.rank_;
313 typedef map<int, vector<Beam_stem_segment> > Position_stem_segments_map;
316 Beam::get_beam_segments (Grob *me_grob, Grob **common)
318 /* ugh, this has a side-effect that we need to ensure that
319 Stem #'beaming is correct */
320 (void) me_grob->get_property ("beaming");
322 Spanner *me = dynamic_cast<Spanner*> (me_grob);
324 extract_grob_set (me, "stems", stems);
325 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
327 commonx = me->get_bound (LEFT)->common_refpoint (commonx, X_AXIS);
328 commonx = me->get_bound (RIGHT)->common_refpoint (commonx, X_AXIS);
332 int gap_count = robust_scm2int (me->get_property ("gap-count"), 0);
333 Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
335 Position_stem_segments_map stem_segments;
336 Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
340 for (vsize i = 0; i < stems.size (); i++)
342 Grob *stem = stems[i];
343 Real stem_width = robust_scm2double (stem->get_property ("thickness"), 1.0) * lt;
344 Real stem_x = stem->relative_coordinate (commonx, X_AXIS);
345 SCM beaming = stem->get_property ("beaming");
349 for (SCM s = index_get_cell (beaming, d);
350 scm_is_pair (s); s = scm_cdr (s))
352 if (!scm_is_integer (scm_car (s)))
355 int beam_rank = scm_to_int (scm_car (s));
356 ranks.add_point (beam_rank);
359 for (SCM s = index_get_cell (beaming, d);
360 scm_is_pair (s); s = scm_cdr (s))
362 if (!scm_is_integer (scm_car (s)))
365 int beam_rank = scm_to_int (scm_car (s));
366 Beam_stem_segment seg;
368 seg.stem_x_ = stem_x;
369 seg.rank_ = 2 * i + (d+1)/2;
370 seg.width_ = stem_width;
373 seg.max_connect_ = robust_scm2int (stem->get_property ("max-beam-connect"), 1000);
375 Direction stem_dir = get_grob_direction (stem);
378 = (stem_dir * beam_rank < (stem_dir * ranks[-stem_dir] + gap_count));
379 stem_segments[beam_rank].push_back (seg);
382 while (flip (&d) != LEFT);
385 Drul_array<Real> break_overshoot
386 = robust_scm2drul (me->get_property ("break-overshoot"),
387 Drul_array<Real> (-0.5, 0.0));
389 vector<Beam_segment> segments;
390 for (Position_stem_segments_map::const_iterator i (stem_segments.begin ());
391 i != stem_segments.end (); i++)
393 vector<Beam_stem_segment> segs = (*i).second;
394 vector_sort (segs, less<Beam_stem_segment> ());
396 Beam_segment current;
398 int vertical_count = (*i).first;
399 for (vsize j = 0; j < segs.size (); j++)
402 event_dir == LEFT: left edge of a beamsegment.
404 Direction event_dir = LEFT;
407 bool on_bound = (event_dir == LEFT) ? j == 0 :
408 j == segs.size () - 1;
410 bool inside_stem = (event_dir == LEFT)
411 ? segs[j].stem_index_ > 0
412 : segs[j].stem_index_ + 1 < stems.size () ;
414 bool event = on_bound
415 || abs (segs[j].rank_ - segs[j+event_dir].rank_) > 1
416 || (abs (vertical_count) >= segs[j].max_connect_
417 || abs (vertical_count) >= segs[j + event_dir].max_connect_);
422 current.vertical_count_ = vertical_count;
423 current.horizontal_[event_dir] = segs[j].stem_x_;
424 if (segs[j].dir_ == event_dir)
427 && me->get_bound (event_dir)->break_status_dir ())
429 current.horizontal_[event_dir]
430 = (robust_relative_extent (me->get_bound (event_dir),
431 commonx, X_AXIS)[RIGHT]
432 + event_dir * break_overshoot[event_dir]);
436 Real notehead_width =
437 Stem::duration_log (segs[j].stem_) == 1
444 Grob *neighbor_stem = stems[segs[j].stem_index_ + event_dir];
446 = neighbor_stem->relative_coordinate (commonx, X_AXIS);
448 notehead_width = min (notehead_width,
449 fabs (neighbor_stem_x - segs[j].stem_x_)/2);
451 current.horizontal_[event_dir] += event_dir * notehead_width;
456 current.horizontal_[event_dir] += event_dir * segs[j].width_/2;
459 current.horizontal_[event_dir] -= event_dir * gap_length;
461 if (Stem::is_invisible (segs[j].stem_))
464 Need to do this in case of whole notes. We don't want the
465 heads to collide with the beams.
467 extract_grob_set (segs[j].stem_, "note-heads", heads);
469 for (vsize k = 0; k < heads.size (); k ++)
470 current.horizontal_[event_dir]
471 = event_dir * min (event_dir * current.horizontal_[event_dir],
474 * heads[k]->extent (commonx,
475 X_AXIS)[-event_dir]);
480 if (event_dir == RIGHT)
482 segments.push_back (current);
483 current = Beam_segment ();
486 while (flip (&event_dir) != LEFT);
494 MAKE_SCHEME_CALLBACK (Beam, print, 1);
496 Beam::print (SCM grob)
498 Spanner *me = unsmob_spanner (grob);
500 vector<Beam_segment> segments = get_beam_segments (me, &commonx);
503 if (normal_stem_count (me))
505 span[LEFT] = first_normal_stem (me)->relative_coordinate (commonx, X_AXIS);
506 span[RIGHT] = last_normal_stem (me)->relative_coordinate (commonx, X_AXIS);
510 extract_grob_set (me, "stems", stems);
511 span[LEFT] = stems[0]->relative_coordinate (commonx, X_AXIS);
512 span[RIGHT] = stems.back ()->relative_coordinate (commonx, X_AXIS);
515 Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
517 SCM posns = me->get_property ("quantized-positions");
519 if (!is_number_pair (posns))
521 programming_error ("no beam positions?");
522 pos = Interval (0, 0);
525 pos = ly_scm2realdrul (posns);
527 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
529 Real dy = pos[RIGHT] - pos[LEFT];
530 Real slope = (dy && span.length ()) ? dy / span.length () : 0;
532 Real thick = get_thickness (me);
533 Real beam_dy = get_beam_translation (me);
535 Direction feather_dir = to_dir (me->get_property ("grow-direction"));
538 for (vsize i = 0; i < segments.size (); i ++)
540 Real local_slope = slope;
543 local_slope += feather_dir * segments[i].vertical_count_ * beam_dy / span.length ();
546 Stencil b = Lookup::beam (local_slope, segments[i].horizontal_.length (), thick, blot);
548 b.translate_axis (segments[i].horizontal_[LEFT], X_AXIS);
550 b.translate_axis (local_slope
551 * (segments[i].horizontal_[LEFT] - span.linear_combination (feather_dir))
552 + pos.linear_combination (feather_dir)
553 + beam_dy * segments[i].vertical_count_, Y_AXIS);
554 the_beam.add_stencil (b);
557 #if (DEBUG_BEAM_SCORING)
558 SCM quant_score = me->get_property ("quant-score");
559 SCM debug = me->layout ()->lookup_variable (ly_symbol2scm ("debug-beam-scoring"));
560 if (to_boolean (debug) && scm_is_string (quant_score))
562 extract_grob_set (me, "stems", stems);
565 This code prints the demerits for each beam. Perhaps this
566 should be switchable for those who want to twiddle with the
570 SCM properties = Font_interface::text_font_alist_chain (me);
572 Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP;
574 Stencil score = *unsmob_stencil (Text_interface::interpret_markup
575 (me->layout ()->self_scm (), properties, quant_score));
577 if (!score.is_empty ())
578 the_beam.add_at_edge (Y_AXIS, stem_dir, score, 1.0);
582 the_beam.translate_axis (-me->relative_coordinate (commonx, X_AXIS), X_AXIS);
583 return the_beam.smobbed_copy ();
587 Beam::get_default_dir (Grob *me)
589 extract_grob_set (me, "stems", stems);
591 Drul_array<Real> extremes (0.0, 0.0);
592 for (iterof (s, stems); s != stems.end (); s++)
594 Interval positions = Stem::head_positions (*s);
598 if (sign (positions[d]) == d)
599 extremes[d] = d * max (d * positions[d], d * extremes[d]);
601 while (flip (&d) != DOWN);
604 Drul_array<int> total (0, 0);
605 Drul_array<int> count (0, 0);
607 bool force_dir = false;
608 for (vsize i = 0; i < stems.size (); i++)
611 Direction stem_dir = CENTER;
612 SCM stem_dir_scm = s->get_property_data ("direction");
613 if (is_direction (stem_dir_scm))
615 stem_dir = to_dir (stem_dir_scm);
619 stem_dir = to_dir (s->get_property ("default-direction"));
622 stem_dir = to_dir (s->get_property ("neutral-direction"));
627 total[stem_dir] += max (int (- stem_dir * Stem::head_positions (s) [-stem_dir]), 0);
634 if (abs (extremes[UP]) > -extremes[DOWN])
636 else if (extremes[UP] < -extremes[DOWN])
640 Direction dir = CENTER;
641 Direction d = CENTER;
642 if ((d = (Direction) sign (count[UP] - count[DOWN])))
646 && (d = (Direction) sign (total[UP] / count[UP] - total[DOWN]/count[DOWN])))
648 else if ((d = (Direction) sign (total[UP] - total[DOWN])))
651 dir = to_dir (me->get_property ("neutral-direction"));
656 /* Set all stems with non-forced direction to beam direction.
657 Urg: non-forced should become `without/with unforced' direction,
658 once stem gets cleaned-up. */
660 Beam::set_stem_directions (Grob *me, Direction d)
662 extract_grob_set (me, "stems", stems);
664 for (vsize i = 0; i < stems.size (); i++)
668 SCM forcedir = s->get_property_data ("direction");
669 if (!to_dir (forcedir))
670 set_grob_direction (s, d);
675 Only try horizontal beams for knees. No reliable detection of
676 anything else is possible here, since we don't know funky-beaming
677 settings, or X-distances (slopes!) People that want sloped
678 knee-beams, should set the directions manually.
683 this routine should take into account the stemlength scoring
684 of a possible knee/nonknee beam.
687 Beam::consider_auto_knees (Grob *me)
689 SCM scm = me->get_property ("auto-knee-gap");
690 if (!scm_is_number (scm))
697 extract_grob_set (me, "normal-stems", stems);
699 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
700 Real staff_space = Staff_symbol_referencer::staff_space (me);
702 vector<Interval> head_extents_array;
703 for (vsize i = 0; i < stems.size (); i++)
705 Grob *stem = stems[i];
707 Interval head_extents = Stem::head_positions (stem);
708 if (!head_extents.is_empty ())
710 head_extents[LEFT] += -1;
711 head_extents[RIGHT] += 1;
712 head_extents *= staff_space * 0.5;
715 We could subtract beam Y position, but this routine only
716 sets stem directions, a constant shift does not have an
719 head_extents += stem->pure_relative_y_coordinate (common, 0, INT_MAX);
721 if (to_dir (stem->get_property_data ("direction")))
723 Direction stemdir = to_dir (stem->get_property ("direction"));
724 head_extents[-stemdir] = -stemdir * infinity_f;
727 head_extents_array.push_back (head_extents);
729 gaps.remove_interval (head_extents);
733 Real max_gap_len = 0.0;
735 for (vsize i = gaps.allowed_regions_.size () -1; i != VPOS ;i--)
737 Interval gap = gaps.allowed_regions_[i];
740 the outer gaps are not knees.
742 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
745 if (gap.length () >= max_gap_len)
747 max_gap_len = gap.length ();
752 Real beam_translation = get_beam_translation (me);
753 Real beam_thickness = Beam::get_thickness (me);
754 int beam_count = Beam::get_beam_count (me);
755 Real height_of_beams = beam_thickness / 2
756 + (beam_count - 1) * beam_translation;
757 Real threshold = scm_to_double (scm) + height_of_beams;
759 if (max_gap_len > threshold)
762 for (vsize i = 0; i < stems.size (); i++)
764 Grob *stem = stems[i];
765 Interval head_extents = head_extents_array[j++];
767 Direction d = (head_extents.center () < max_gap.center ())
770 stem->set_property ("direction", scm_from_int (d));
772 head_extents.intersect (max_gap);
773 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
778 /* Set stem's shorten property if unset.
781 take some y-position (chord/beam/nearest?) into account
782 scmify forced-fraction
784 This is done in beam because the shorten has to be uniform over the
791 set_minimum_dy (Grob *me, Real *dy)
796 If dy is smaller than the smallest quant, we
797 get absurd direction-sign penalties.
800 Real ss = Staff_symbol_referencer::staff_space (me);
801 Real thickness = Beam::get_thickness (me) / ss;
802 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
803 Real sit = (thickness - slt) / 2;
805 Real hang = 1.0 - (thickness - slt) / 2;
807 *dy = sign (*dy) * max (fabs (*dy),
808 min (min (sit, inter), hang));
814 MAKE_SCHEME_CALLBACK (Beam, calc_stem_shorten, 1)
816 Beam::calc_stem_shorten (SCM smob)
818 Grob *me = unsmob_grob (smob);
821 shortening looks silly for x staff beams
824 return scm_from_int (0);
826 Real forced_fraction = 1.0 * forced_stem_count (me)
827 / normal_stem_count (me);
829 int beam_count = get_beam_count (me);
831 SCM shorten_list = me->get_property ("beamed-stem-shorten");
832 if (shorten_list == SCM_EOL)
833 return scm_from_int (0);
835 Real staff_space = Staff_symbol_referencer::staff_space (me);
838 = robust_list_ref (beam_count -1, shorten_list);
839 Real shorten = scm_to_double (shorten_elt) * staff_space;
841 shorten *= forced_fraction;
845 return scm_from_double (shorten);
847 return scm_from_double (0.0);
853 Compute a first approximation to the beam slope.
855 MAKE_SCHEME_CALLBACK (Beam, calc_least_squares_positions, 2);
857 Beam::calc_least_squares_positions (SCM smob, SCM posns)
861 Grob *me = unsmob_grob (smob);
863 int count = normal_stem_count (me);
866 return ly_interval2scm (pos);
868 vector<Real> x_posns;
869 extract_grob_set (me, "normal-stems", stems);
870 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
871 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
873 Real my_y = me->relative_coordinate (commony, Y_AXIS);
875 Grob *fvs = first_normal_stem (me);
876 Grob *lvs = last_normal_stem (me);
878 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
879 + fvs->relative_coordinate (commony, Y_AXIS) - my_y,
880 Stem::get_stem_info (lvs).ideal_y_
881 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
883 Real x0 = first_normal_stem (me)->relative_coordinate (commonx, X_AXIS);
884 for (vsize i = 0; i < stems.size (); i++)
888 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
889 x_posns.push_back (x);
891 Real dx = last_normal_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
899 Interval chord (Stem::chord_start_y (stems[0]),
900 Stem::chord_start_y (stems.back ()));
902 /* Simple beams (2 stems) on middle line should be allowed to be
905 However, if both stems reach middle line,
906 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
908 For that case, we apply artificial slope */
909 if (!ideal[LEFT] && chord.delta () && count == 2)
912 Direction d = (Direction) (sign (chord.delta ()) * UP);
913 pos[d] = get_thickness (me) / 2;
920 For broken beams this doesn't work well. In this case, the
921 slope esp. of the first part of a broken beam should predict
922 where the second part goes.
924 ldy = pos[RIGHT] - pos[LEFT];
928 vector<Offset> ideals;
929 for (vsize i = 0; i < stems.size (); i++)
932 ideals.push_back (Offset (x_posns[i],
933 Stem::get_stem_info (s).ideal_y_
934 + s->relative_coordinate (commony, Y_AXIS)
938 minimise_least_squares (&slope, &y, ideals);
942 set_minimum_dy (me, &dy);
945 pos = Interval (y, (y + dy));
949 "position" is relative to the staff.
951 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
953 me->set_property ("least-squares-dy", scm_from_double (ldy));
954 return ly_interval2scm (pos);
958 We can't combine with previous function, since check concave and
959 slope damping comes first.
961 TODO: we should use the concaveness to control the amount of damping
964 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 2);
966 Beam::shift_region_to_valid (SCM grob, SCM posns)
968 Grob *me = unsmob_grob (grob);
972 vector<Real> x_posns;
973 extract_grob_set (me, "stems", stems);
974 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
975 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
977 Grob *fvs = first_normal_stem (me);
982 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
983 for (vsize i = 0; i < stems.size (); i++)
987 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
988 x_posns.push_back (x);
991 Grob *lvs = last_normal_stem (me);
995 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
997 Drul_array<Real> pos = ly_scm2interval (posns);
999 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1001 Real dy = pos[RIGHT] - pos[LEFT];
1003 Real slope = dx ? (dy / dx) : 0.0;
1006 Shift the positions so that we have a chance of finding good
1007 quants (i.e. no short stem failures.)
1009 Interval feasible_left_point;
1010 feasible_left_point.set_full ();
1011 for (vsize i = 0; i < stems.size (); i++)
1014 if (Stem::is_invisible (s))
1017 Direction d = get_grob_direction (s);
1020 = Stem::get_stem_info (s).shortest_y_
1021 - slope * x_posns [i];
1024 left_y is now relative to the stem S. We want relative to
1025 ourselves, so translate:
1028 += + s->relative_coordinate (commony, Y_AXIS)
1029 - me->relative_coordinate (commony, Y_AXIS);
1035 feasible_left_point.intersect (flp);
1038 if (feasible_left_point.is_empty ())
1039 warning (_ ("no viable initial configuration found: may not find good beam slope"));
1040 else if (!feasible_left_point.contains (y))
1042 const int REGION_SIZE = 2; // UGH UGH
1043 if (isinf (feasible_left_point[DOWN]))
1044 y = feasible_left_point[UP] - REGION_SIZE;
1045 else if (isinf (feasible_left_point[UP]))
1046 y = feasible_left_point[DOWN]+ REGION_SIZE;
1048 y = feasible_left_point.center ();
1051 pos = Drul_array<Real> (y, (y + dy));
1052 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1054 return ly_interval2scm (pos);
1057 /* This neat trick is by Werner Lemberg,
1058 damped = tanh (slope)
1059 corresponds with some tables in [Wanske] CHECKME */
1060 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 2);
1062 Beam::slope_damping (SCM smob, SCM posns)
1064 Grob *me = unsmob_grob (smob);
1065 Drul_array<Real> pos = ly_scm2interval (posns);
1067 if (normal_stem_count (me) <= 1)
1071 SCM s = me->get_property ("damping");
1072 Real damping = scm_to_double (s);
1073 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
1074 if (concaveness >= 10000)
1076 pos[LEFT] = pos[RIGHT];
1077 me->set_property ("least-squares-dy", scm_from_double (0));
1083 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1085 Real dy = pos[RIGHT] - pos[LEFT];
1087 Grob *fvs = first_normal_stem (me);
1088 Grob *lvs = last_normal_stem (me);
1090 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1092 Real dx = last_normal_stem (me)->relative_coordinate (commonx, X_AXIS)
1093 - first_normal_stem (me)->relative_coordinate (commonx, X_AXIS);
1095 Real slope = dy && dx ? dy / dx : 0;
1097 slope = 0.6 * tanh (slope) / (damping + concaveness);
1099 Real damped_dy = slope * dx;
1101 set_minimum_dy (me, &damped_dy);
1103 pos[LEFT] += (dy - damped_dy) / 2;
1104 pos[RIGHT] -= (dy - damped_dy) / 2;
1106 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1109 return ly_interval2scm (pos);
1113 Report slice containing the numbers that are both in (car BEAMING)
1117 where_are_the_whole_beams (SCM beaming)
1121 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1123 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1125 l.add_point (scm_to_int (scm_car (s)));
1131 /* Return the Y position of the stem-end, given the Y-left, Y-right
1132 in POS for stem S. This Y position is relative to S. */
1134 Beam::calc_stem_y (Grob *me, Grob *stem, Grob **common,
1136 Drul_array<Real> pos, bool french)
1138 Real beam_translation = get_beam_translation (me);
1140 Real r = stem->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1141 Real dy = pos[RIGHT] - pos[LEFT];
1143 Real stem_y_beam0 = (dy && dx
1148 Direction my_dir = get_grob_direction (stem);
1149 SCM beaming = stem->get_property ("beaming");
1151 Real stem_y = stem_y_beam0;
1154 Slice bm = where_are_the_whole_beams (beaming);
1155 if (!bm.is_empty ())
1156 stem_y += beam_translation * bm[-my_dir];
1160 Slice bm = Stem::beam_multiplicity (stem);
1161 if (!bm.is_empty ())
1162 stem_y += bm[my_dir] * beam_translation;
1165 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1166 - stem->relative_coordinate (common[Y_AXIS], Y_AXIS);
1172 Hmm. At this time, beam position and slope are determined. Maybe,
1173 stem directions and length should set to relative to the chord's
1174 position of the beam. */
1175 MAKE_SCHEME_CALLBACK (Beam, set_stem_lengths, 1);
1177 Beam::set_stem_lengths (SCM smob)
1179 Grob *me = unsmob_grob (smob);
1181 /* trigger callbacks. */
1182 (void) me->get_property ("direction");
1183 (void) me->get_property ("beaming");
1185 SCM posns = me->get_property ("positions");
1187 extract_grob_set (me, "stems", stems);
1192 for (int a = 2; a--;)
1193 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1195 Drul_array<Real> pos = ly_scm2realdrul (posns);
1196 Real staff_space = Staff_symbol_referencer::staff_space (me);
1197 scale_drul (&pos, staff_space);
1201 if (robust_scm2int (me->get_property ("gap-count"), 0))
1204 thick = get_thickness (me);
1207 Grob *fvs = first_normal_stem (me);
1208 Grob *lvs = last_normal_stem (me);
1210 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1211 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1213 for (vsize i = 0; i < stems.size (); i++)
1217 bool french = to_boolean (s->get_property ("french-beaming"));
1218 Real stem_y = calc_stem_y (me, s, common,
1220 pos, french && s != lvs && s!= fvs);
1223 Make the stems go up to the end of the beam. This doesn't matter
1224 for normal beams, but for tremolo beams it looks silly otherwise.
1227 && !Stem::is_invisible (s))
1228 stem_y += thick * 0.5 * get_grob_direction (s);
1231 Do set_stemend for invisible stems too, so tuplet brackets
1232 have a reference point for sloping
1234 Stem::set_stemend (s, 2 * stem_y / staff_space);
1241 Beam::set_beaming (Grob *me, Beaming_pattern const *beaming)
1243 extract_grob_set (me, "stems", stems);
1246 for (vsize i = 0; i < stems.size (); i++)
1249 Don't overwrite user settings.
1253 Grob *stem = stems[i];
1254 SCM beaming_prop = stem->get_property ("beaming");
1255 if (beaming_prop == SCM_EOL
1256 || index_get_cell (beaming_prop, d) == SCM_EOL)
1258 int count = beaming->beamlet_count (i, d);
1260 && i + 1 < stems.size ()
1261 && Stem::is_invisible (stem))
1262 count = min (count, beaming->beamlet_count (i,-d));
1264 if ( ((i == 0 && d == LEFT)
1265 || (i == stems.size ()-1 && d == RIGHT))
1266 && stems.size () > 1
1267 && to_boolean (me->get_property ("clip-edges")))
1270 Stem::set_beaming (stem, count, d);
1273 while (flip (&d) != LEFT);
1278 Beam::forced_stem_count (Grob *me)
1280 extract_grob_set (me, "normal-stems", stems);
1283 for (vsize i = 0; i < stems.size (); i++)
1287 /* I can imagine counting those boundaries as a half forced stem,
1288 but let's count them full for now. */
1289 Direction defdir = to_dir (s->get_property ("default-direction"));
1291 if (abs (Stem::chord_start_y (s)) > 0.1
1293 && get_grob_direction (s) != defdir)
1300 Beam::normal_stem_count (Grob *me)
1302 extract_grob_set (me, "normal-stems", stems);
1303 return stems.size ();
1307 Beam::first_normal_stem (Grob *me)
1309 extract_grob_set (me, "normal-stems", stems);
1310 return stems.size () ? stems[0] : 0;
1314 Beam::last_normal_stem (Grob *me)
1316 extract_grob_set (me, "normal-stems", stems);
1317 return stems.size () ? stems.back () : 0;
1323 handle rest under beam (do_post: beams are calculated now)
1324 what about combination of collisions and rest under beam.
1328 rest -> stem -> beam -> interpolate_y_position ()
1330 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Beam, rest_collision_callback, 2, 1, "");
1332 Beam::rest_collision_callback (SCM smob, SCM prev_offset)
1334 Grob *rest = unsmob_grob (smob);
1335 if (scm_is_number (rest->get_property ("staff-position")))
1336 return scm_from_int (0);
1338 Real offset = robust_scm2double (prev_offset, 0.0);
1340 Grob *st = unsmob_grob (rest->get_object ("stem"));
1343 return scm_from_double (0.0);
1344 Grob *beam = unsmob_grob (stem->get_object ("beam"));
1346 || !Beam::has_interface (beam)
1347 || !Beam::normal_stem_count (beam))
1348 return scm_from_double (0.0);
1350 Drul_array<Real> pos (robust_scm2drul (beam->get_property ("positions"),
1351 Drul_array<Real> (0,0)));
1353 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1355 scale_drul (&pos, staff_space);
1357 Real dy = pos[RIGHT] - pos[LEFT];
1359 Drul_array<Grob*> visible_stems (first_normal_stem (beam),
1360 last_normal_stem (beam));
1361 extract_grob_set (beam, "stems", stems);
1363 Grob *common = common_refpoint_of_array (stems, beam, X_AXIS);
1365 Real x0 = visible_stems[LEFT]->relative_coordinate (common, X_AXIS);
1366 Real dx = visible_stems[RIGHT]->relative_coordinate (common, X_AXIS) - x0;
1367 Real slope = dy && dx ? dy / dx : 0;
1369 Direction d = get_grob_direction (stem);
1370 Real stem_y = pos[LEFT]
1371 + (stem->relative_coordinate (common, X_AXIS) - x0) * slope;
1373 Real beam_translation = get_beam_translation (beam);
1374 Real beam_thickness = Beam::get_thickness (beam);
1377 TODO: this is not strictly correct for 16th knee beams.
1380 = Stem::beam_multiplicity (stem).length () + 1;
1382 Real height_of_my_beams = beam_thickness / 2
1383 + (beam_count - 1) * beam_translation;
1384 Real beam_y = stem_y - d * height_of_my_beams;
1386 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1389 TODO: this is dubious, because this call needs the info we're
1390 computing right now.
1392 Interval rest_extent = rest->extent (common_y, Y_AXIS);
1393 rest_extent.translate (offset);
1395 Real rest_dim = rest_extent[d];
1396 Real minimum_distance
1397 = staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1398 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1400 Real shift = d * min (d * (beam_y - d * minimum_distance - rest_dim), 0.0);
1402 shift /= staff_space;
1403 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1405 /* Always move discretely by half spaces */
1406 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1408 /* Inside staff, move by whole spaces*/
1409 if ((rest_extent[d] + staff_space * shift) * d
1411 || (rest_extent[-d] + staff_space * shift) * -d
1413 shift = ceil (fabs (shift)) * sign (shift);
1415 return scm_from_double (offset + staff_space * shift);
1419 Beam::is_knee (Grob *me)
1421 SCM k = me->get_property ("knee");
1422 if (scm_is_bool (k))
1423 return ly_scm2bool (k);
1427 extract_grob_set (me, "stems", stems);
1428 for (vsize i = stems.size (); i--;)
1430 Direction dir = get_grob_direction (stems[i]);
1439 me->set_property ("knee", ly_bool2scm (knee));
1445 Beam::is_cross_staff (Grob *me)
1447 extract_grob_set (me, "stems", stems);
1448 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
1449 for (vsize i = 0; i < stems.size (); i++)
1450 if (Staff_symbol_referencer::get_staff_symbol (stems[i]) != staff_symbol)
1455 MAKE_SCHEME_CALLBACK (Beam, calc_cross_staff, 1)
1457 Beam::calc_cross_staff (SCM smob)
1459 return scm_from_bool (is_cross_staff (unsmob_grob (smob)));
1463 Beam::get_direction_beam_count (Grob *me, Direction d)
1465 extract_grob_set (me, "stems", stems);
1468 for (vsize i = stems.size (); i--;)
1471 Should we take invisible stems into account?
1473 if (get_grob_direction (stems[i]) == d)
1474 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1480 ADD_INTERFACE (Beam,
1483 "The @code{thickness} property is the weight of beams, "
1484 "measured in staffspace. The @code{direction} "
1485 "property is not user-serviceable. Use "
1486 "the @code{direction} property of @code{Stem} instead. "
1492 "beamed-stem-shorten "
1507 "neutral-direction "
1511 "quantized-positions "