2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 Jan Nieuwenhuizen <janneke@gnu.org>
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
24 - Determine auto knees based on positions if it's set by the user.
26 - the code is littered with * and / staff_space calls for
27 #'positions. Consider moving to real-world coordinates?
29 Problematic issue is user tweaks (user tweaks are in staff-coordinates.)
33 - Stems run to the Y-center of the beam.
35 - beam_translation is the offset between Y centers of the beam.
40 #include "beam-scoring-problem.hh"
41 #include "beaming-pattern.hh"
42 #include "directional-element-interface.hh"
43 #include "grob-array.hh"
44 #include "international.hh"
45 #include "interval-set.hh"
47 #include "least-squares.hh"
51 #include "output-def.hh"
52 #include "pointer-group-interface.hh"
54 #include "staff-symbol-referencer.hh"
58 #if DEBUG_BEAM_SCORING
59 #include "text-interface.hh" // debug output.
60 #include "font-interface.hh" // debug output.
66 Beam_stem_segment::Beam_stem_segment ()
68 max_connect_ = 1000; // infinity
78 beam_segment_less (Beam_segment const& a, Beam_segment const& b)
80 return a.horizontal_[LEFT] < b.horizontal_[LEFT];
83 Beam_segment::Beam_segment ()
89 Beam::add_stem (Grob *me, Grob *s)
91 if (Stem::get_beam (s))
93 programming_error ("Stem already has beam");
97 Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s);
98 s->set_object ("beam", me->self_scm ());
99 add_bound_item (dynamic_cast<Spanner *> (me), dynamic_cast<Item *> (s));
103 Beam::get_beam_thickness (Grob *me)
105 return robust_scm2double (me->get_property ("beam-thickness"), 0)
106 * Staff_symbol_referencer::staff_space (me);
109 /* Return the translation between 2 adjoining beams. */
111 Beam::get_beam_translation (Grob *me)
113 int beam_count = get_beam_count (me);
114 Real staff_space = Staff_symbol_referencer::staff_space (me);
115 Real line = Staff_symbol_referencer::line_thickness (me);
116 Real beam_thickness = get_beam_thickness (me);
117 Real fract = robust_scm2double (me->get_property ("length-fraction"), 1.0);
119 Real beam_translation = beam_count < 4
120 ? (2 * staff_space + line - beam_thickness) / 2.0
121 : (3 * staff_space + line - beam_thickness) / 3.0;
123 return fract * beam_translation;
126 /* Maximum beam_count. */
128 Beam::get_beam_count (Grob *me)
132 extract_grob_set (me, "stems", stems);
133 for (vsize i = 0; i < stems.size (); i++)
135 Grob *stem = stems[i];
136 m = max (m, (Stem::beam_multiplicity (stem).length () + 1));
141 MAKE_SCHEME_CALLBACK (Beam, calc_normal_stems, 1);
143 Beam::calc_normal_stems (SCM smob)
145 Grob *me = unsmob_grob (smob);
147 extract_grob_set (me, "stems", stems);
148 SCM val = Grob_array::make_array ();
149 Grob_array *ga = unsmob_grob_array (val);
150 for (vsize i = 0; i < stems.size (); i++)
151 if (Stem::is_normal_stem (stems[i]))
157 MAKE_SCHEME_CALLBACK (Beam, calc_direction, 1);
159 Beam::calc_direction (SCM smob)
161 Grob *me = unsmob_grob (smob);
163 /* Beams with less than 2 two stems don't make much sense, but could happen
170 Direction dir = CENTER;
172 int count = normal_stem_count (me);
175 extract_grob_set (me, "stems", stems);
176 if (stems.size () == 0)
178 me->warning (_ ("removing beam with no stems"));
181 return SCM_UNSPECIFIED;
185 Grob *stem = first_normal_stem (me);
188 This happens for chord tremolos.
193 if (is_direction (stem->get_property_data ("direction")))
194 dir = to_dir (stem->get_property_data ("direction"));
196 dir = to_dir (stem->get_property ("default-direction"));
203 dir = get_default_dir (me);
205 consider_auto_knees (me);
210 set_stem_directions (me, dir);
213 return scm_from_int (dir);
218 /* We want a maximal number of shared beams, but if there is choice, we
219 * take the one that is closest to the end of the stem. This is for
231 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
235 Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
239 for (int i = lslice[-left_dir];
240 (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
243 for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
245 int k = -right_dir * scm_to_int (scm_car (s)) + i;
246 if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F)
250 if (count >= best_count)
260 MAKE_SCHEME_CALLBACK (Beam, calc_beaming, 1)
262 Beam::calc_beaming (SCM smob)
264 Grob *me = unsmob_grob (smob);
266 extract_grob_set (me, "stems", stems);
269 last_int.set_empty ();
271 SCM last_beaming = scm_cons (SCM_EOL, scm_list_1 (scm_from_int (0)));
272 Direction last_dir = CENTER;
273 for (vsize i = 0; i < stems.size (); i++)
275 Grob *this_stem = stems[i];
276 SCM this_beaming = this_stem->get_property ("beaming");
278 Direction this_dir = get_grob_direction (this_stem);
279 if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming))
281 int start_point = position_with_maximal_common_beams
282 (last_beaming, this_beaming,
283 last_dir ? last_dir : this_dir,
290 new_slice.set_empty ();
291 SCM s = index_get_cell (this_beaming, d);
292 for (; scm_is_pair (s); s = scm_cdr (s))
295 = start_point - this_dir * scm_to_int (scm_car (s));
297 new_slice.add_point (new_beam_pos);
298 scm_set_car_x (s, scm_from_int (new_beam_pos));
301 while (flip (&d) != LEFT);
303 if (!new_slice.is_empty ())
304 last_int = new_slice;
309 FIXME: what's this for?
311 SCM s = scm_cdr (this_beaming);
312 for (; scm_is_pair (s); s = scm_cdr (s))
314 int np = -this_dir * scm_to_int (scm_car (s));
315 scm_set_car_x (s, scm_from_int (np));
316 last_int.add_point (np);
320 if (scm_ilength (scm_cdr (this_beaming)) > 0)
322 last_beaming = this_beaming;
331 operator <(Beam_stem_segment const &a,
332 Beam_stem_segment const &b)
334 return a.rank_ < b.rank_;
337 typedef map<int, vector<Beam_stem_segment> > Position_stem_segments_map;
339 // TODO - should store result in a property?
341 Beam::get_beam_segments (Grob *me_grob, Grob **common)
343 /* ugh, this has a side-effect that we need to ensure that
344 Stem #'beaming is correct */
345 (void) me_grob->get_property ("beaming");
347 Spanner *me = dynamic_cast<Spanner*> (me_grob);
349 extract_grob_set (me, "stems", stems);
350 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
352 commonx = me->get_bound (LEFT)->common_refpoint (commonx, X_AXIS);
353 commonx = me->get_bound (RIGHT)->common_refpoint (commonx, X_AXIS);
357 int gap_count = robust_scm2int (me->get_property ("gap-count"), 0);
358 Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
360 Position_stem_segments_map stem_segments;
361 Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
363 /* There are two concepts of "rank" that are used in the following code.
364 The beam_rank is the vertical position of the beam (larger numbers are
365 closer to the noteheads). Beam_stem_segment.rank_, on the other hand,
366 is the horizontal position of the segment (this is incremented by two
367 for each stem; the beam segment on the right side of the stem has
368 a higher rank (by one) than its neighbour to the left). */
370 for (vsize i = 0; i < stems.size (); i++)
372 Grob *stem = stems[i];
373 Real stem_width = robust_scm2double (stem->get_property ("thickness"), 1.0) * lt;
374 Real stem_x = stem->relative_coordinate (commonx, X_AXIS);
375 SCM beaming = stem->get_property ("beaming");
379 // Find the maximum and minimum beam ranks.
380 // Given that RANKS is never reset to empty, the interval will always be
381 // smallest for the left beamlet of the first stem, and then it might grow.
382 // Do we really want this? (It only affects the tremolo gaps) --jneem
383 for (SCM s = index_get_cell (beaming, d);
384 scm_is_pair (s); s = scm_cdr (s))
386 if (!scm_is_integer (scm_car (s)))
389 int beam_rank = scm_to_int (scm_car (s));
390 ranks.add_point (beam_rank);
393 for (SCM s = index_get_cell (beaming, d);
394 scm_is_pair (s); s = scm_cdr (s))
396 if (!scm_is_integer (scm_car (s)))
399 int beam_rank = scm_to_int (scm_car (s));
400 Beam_stem_segment seg;
402 seg.stem_x_ = stem_x;
403 seg.rank_ = 2 * i + (d+1)/2;
404 seg.width_ = stem_width;
407 seg.max_connect_ = robust_scm2int (stem->get_property ("max-beam-connect"), 1000);
409 Direction stem_dir = get_grob_direction (stem);
412 = (stem_dir * beam_rank < (stem_dir * ranks[-stem_dir] + gap_count));
413 stem_segments[beam_rank].push_back (seg);
416 while (flip (&d) != LEFT);
419 Drul_array<Real> break_overshoot
420 = robust_scm2drul (me->get_property ("break-overshoot"),
421 Drul_array<Real> (-0.5, 0.0));
423 vector<Beam_segment> segments;
424 for (Position_stem_segments_map::const_iterator i (stem_segments.begin ());
425 i != stem_segments.end (); i++)
427 vector<Beam_stem_segment> segs = (*i).second;
428 vector_sort (segs, less<Beam_stem_segment> ());
430 Beam_segment current;
432 // Iterate over all of the segments of the current beam rank,
433 // merging the adjacent Beam_stem_segments into one Beam_segment
435 int vertical_count = (*i).first;
436 for (vsize j = 0; j < segs.size (); j++)
438 // Keeping track of the different directions here is a little tricky.
439 // segs[j].dir_ is the direction of the beam segment relative to the stem
440 // (ie. segs[j].dir_ == LEFT if the beam segment sticks out to the left of
441 // its stem) whereas event_dir refers to the edge of the beam segment that
442 // we are currently looking at (ie. if segs[j].dir_ == event_dir then we
443 // are looking at that edge of the beam segment that is furthest from its
445 Direction event_dir = LEFT;
446 Beam_stem_segment const& seg = segs[j];
449 Beam_stem_segment const& neighbor_seg = segs[j + event_dir];
450 // TODO: make names clearer? --jneem
451 // on_line_bound: whether the current segment is on the boundary of the WHOLE beam
452 // on_beam_bound: whether the current segment is on the boundary of just that part
453 // of the beam with the current beam_rank
454 bool on_line_bound = (seg.dir_ == LEFT) ? seg.stem_index_ == 0
455 : seg.stem_index_ == stems.size() - 1;
456 bool on_beam_bound = (event_dir == LEFT) ? j == 0 :
457 j == segs.size () - 1;
458 bool inside_stem = (event_dir == LEFT)
459 ? seg.stem_index_ > 0
460 : seg.stem_index_ + 1 < stems.size () ;
462 bool event = on_beam_bound
463 || abs (seg.rank_ - neighbor_seg.rank_) > 1
464 || (abs (vertical_count) >= seg.max_connect_
465 || abs (vertical_count) >= neighbor_seg.max_connect_);
468 // Then this edge of the current segment is irrelevent because it will
469 // be connected with the next segment in the event_dir direction.
472 current.vertical_count_ = vertical_count;
473 current.horizontal_[event_dir] = seg.stem_x_;
474 if (seg.dir_ == event_dir)
475 // then we are examining the edge of a beam segment that is furthest
479 && me->get_bound (event_dir)->break_status_dir ())
481 current.horizontal_[event_dir]
482 = (robust_relative_extent (me->get_bound (event_dir),
483 commonx, X_AXIS)[RIGHT]
484 + event_dir * break_overshoot[event_dir]);
488 Grob *stem = stems[seg.stem_index_];
489 Drul_array<Real> beamlet_length =
490 robust_scm2interval (stem->get_property ("beamlet-default-length"), Interval (1.1, 1.1));
491 Drul_array<Real> max_proportion =
492 robust_scm2interval (stem->get_property ("beamlet-max-length-proportion"), Interval (0.75, 0.75));
493 Real length = beamlet_length[seg.dir_];
497 Grob *neighbor_stem = stems[seg.stem_index_ + event_dir];
498 Real neighbor_stem_x = neighbor_stem->relative_coordinate (commonx, X_AXIS);
500 length = min (length,
501 fabs (neighbor_stem_x - seg.stem_x_) * max_proportion[seg.dir_]);
503 current.horizontal_[event_dir] += event_dir * length;
507 // we are examining the edge of a beam segment that is closest
508 // (ie. touching, unless there is a gap) its stem.
510 current.horizontal_[event_dir] += event_dir * seg.width_/2;
513 current.horizontal_[event_dir] -= event_dir * gap_length;
515 if (Stem::is_invisible (seg.stem_))
518 Need to do this in case of whole notes. We don't want the
519 heads to collide with the beams.
521 extract_grob_set (seg.stem_, "note-heads", heads);
523 for (vsize k = 0; k < heads.size (); k ++)
524 current.horizontal_[event_dir]
525 = event_dir * min (event_dir * current.horizontal_[event_dir],
528 * heads[k]->extent (commonx,
529 X_AXIS)[-event_dir]);
534 if (event_dir == RIGHT)
536 segments.push_back (current);
537 current = Beam_segment ();
540 while (flip (&event_dir) != LEFT);
548 MAKE_SCHEME_CALLBACK (Beam, print, 1);
550 Beam::print (SCM grob)
552 Spanner *me = unsmob_spanner (grob);
554 vector<Beam_segment> segments = get_beam_segments (me, &commonx);
557 if (normal_stem_count (me))
559 span[LEFT] = first_normal_stem (me)->relative_coordinate (commonx, X_AXIS);
560 span[RIGHT] = last_normal_stem (me)->relative_coordinate (commonx, X_AXIS);
564 extract_grob_set (me, "stems", stems);
565 span[LEFT] = stems[0]->relative_coordinate (commonx, X_AXIS);
566 span[RIGHT] = stems.back ()->relative_coordinate (commonx, X_AXIS);
569 Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
571 SCM posns = me->get_property ("quantized-positions");
573 if (!is_number_pair (posns))
575 programming_error ("no beam positions?");
576 pos = Interval (0, 0);
579 pos = ly_scm2realdrul (posns);
581 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
583 Real dy = pos[RIGHT] - pos[LEFT];
584 Real slope = (dy && span.length ()) ? dy / span.length () : 0;
586 Real beam_thickness = get_beam_thickness (me);
587 Real beam_dy = get_beam_translation (me);
589 Direction feather_dir = to_dir (me->get_property ("grow-direction"));
592 for (vsize i = 0; i < segments.size (); i ++)
594 Real local_slope = slope;
597 local_slope += feather_dir * segments[i].vertical_count_ * beam_dy / span.length ();
600 Stencil b = Lookup::beam (local_slope, segments[i].horizontal_.length (), beam_thickness, blot);
602 b.translate_axis (segments[i].horizontal_[LEFT], X_AXIS);
604 b.translate_axis (local_slope
605 * (segments[i].horizontal_[LEFT] - span.linear_combination (feather_dir))
606 + pos.linear_combination (feather_dir)
607 + beam_dy * segments[i].vertical_count_, Y_AXIS);
608 the_beam.add_stencil (b);
611 #if (DEBUG_BEAM_SCORING)
612 SCM annotation = me->get_property ("annotation");
613 if (scm_is_string (annotation))
615 extract_grob_set (me, "stems", stems);
618 This code prints the demerits for each beam. Perhaps this
619 should be switchable for those who want to twiddle with the
623 SCM properties = Font_interface::text_font_alist_chain (me);
625 properties = scm_cons(scm_acons (ly_symbol2scm ("font-size"), scm_from_int (-5), SCM_EOL),
628 Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP;
630 Stencil score = *unsmob_stencil (Text_interface::interpret_markup
631 (me->layout ()->self_scm (), properties, annotation));
633 if (!score.is_empty ())
635 score.translate_axis (me->relative_coordinate(commonx, X_AXIS), X_AXIS);
636 the_beam.add_at_edge (Y_AXIS, stem_dir, score, 1.0);
641 the_beam.translate_axis (-me->relative_coordinate (commonx, X_AXIS), X_AXIS);
642 return the_beam.smobbed_copy ();
646 Beam::get_default_dir (Grob *me)
648 extract_grob_set (me, "stems", stems);
650 Drul_array<Real> extremes (0.0, 0.0);
651 for (iterof (s, stems); s != stems.end (); s++)
653 Interval positions = Stem::head_positions (*s);
657 if (sign (positions[d]) == d)
658 extremes[d] = d * max (d * positions[d], d * extremes[d]);
660 while (flip (&d) != DOWN);
663 Drul_array<int> total (0, 0);
664 Drul_array<int> count (0, 0);
666 bool force_dir = false;
667 for (vsize i = 0; i < stems.size (); i++)
670 Direction stem_dir = CENTER;
671 SCM stem_dir_scm = s->get_property_data ("direction");
672 if (is_direction (stem_dir_scm))
674 stem_dir = to_dir (stem_dir_scm);
678 stem_dir = to_dir (s->get_property ("default-direction"));
681 stem_dir = to_dir (s->get_property ("neutral-direction"));
686 total[stem_dir] += max (int (- stem_dir * Stem::head_positions (s) [-stem_dir]), 0);
693 if (abs (extremes[UP]) > -extremes[DOWN])
695 else if (extremes[UP] < -extremes[DOWN])
699 Direction dir = CENTER;
700 Direction d = CENTER;
701 if ((d = (Direction) sign (count[UP] - count[DOWN])))
705 && (d = (Direction) sign (total[UP] / count[UP] - total[DOWN]/count[DOWN])))
707 else if ((d = (Direction) sign (total[UP] - total[DOWN])))
710 dir = to_dir (me->get_property ("neutral-direction"));
715 /* Set all stems with non-forced direction to beam direction.
716 Urg: non-forced should become `without/with unforced' direction,
717 once stem gets cleaned-up. */
719 Beam::set_stem_directions (Grob *me, Direction d)
721 extract_grob_set (me, "stems", stems);
723 for (vsize i = 0; i < stems.size (); i++)
727 SCM forcedir = s->get_property_data ("direction");
728 if (!to_dir (forcedir))
729 set_grob_direction (s, d);
734 Only try horizontal beams for knees. No reliable detection of
735 anything else is possible here, since we don't know funky-beaming
736 settings, or X-distances (slopes!) People that want sloped
737 knee-beams, should set the directions manually.
742 this routine should take into account the stemlength scoring
743 of a possible knee/nonknee beam.
746 Beam::consider_auto_knees (Grob *me)
748 SCM scm = me->get_property ("auto-knee-gap");
749 if (!scm_is_number (scm))
756 extract_grob_set (me, "normal-stems", stems);
758 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
759 Real staff_space = Staff_symbol_referencer::staff_space (me);
761 vector<Interval> head_extents_array;
762 for (vsize i = 0; i < stems.size (); i++)
764 Grob *stem = stems[i];
766 Interval head_extents = Stem::head_positions (stem);
767 if (!head_extents.is_empty ())
769 head_extents[LEFT] += -1;
770 head_extents[RIGHT] += 1;
771 head_extents *= staff_space * 0.5;
774 We could subtract beam Y position, but this routine only
775 sets stem directions, a constant shift does not have an
778 head_extents += stem->pure_relative_y_coordinate (common, 0, INT_MAX);
780 if (to_dir (stem->get_property_data ("direction")))
782 Direction stemdir = to_dir (stem->get_property ("direction"));
783 head_extents[-stemdir] = -stemdir * infinity_f;
786 head_extents_array.push_back (head_extents);
788 gaps.remove_interval (head_extents);
792 Real max_gap_len = 0.0;
794 for (vsize i = gaps.allowed_regions_.size () -1; i != VPOS ;i--)
796 Interval gap = gaps.allowed_regions_[i];
799 the outer gaps are not knees.
801 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
804 if (gap.length () >= max_gap_len)
806 max_gap_len = gap.length ();
811 Real beam_translation = get_beam_translation (me);
812 Real beam_thickness = Beam::get_beam_thickness (me);
813 int beam_count = Beam::get_beam_count (me);
814 Real height_of_beams = beam_thickness / 2
815 + (beam_count - 1) * beam_translation;
816 Real threshold = scm_to_double (scm) + height_of_beams;
818 if (max_gap_len > threshold)
821 for (vsize i = 0; i < stems.size (); i++)
823 Grob *stem = stems[i];
824 Interval head_extents = head_extents_array[j++];
826 Direction d = (head_extents.center () < max_gap.center ())
829 stem->set_property ("direction", scm_from_int (d));
831 head_extents.intersect (max_gap);
832 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
837 /* Set stem's shorten property if unset.
840 take some y-position (chord/beam/nearest?) into account
841 scmify forced-fraction
843 This is done in beam because the shorten has to be uniform over the
850 set_minimum_dy (Grob *me, Real *dy)
855 If dy is smaller than the smallest quant, we
856 get absurd direction-sign penalties.
859 Real ss = Staff_symbol_referencer::staff_space (me);
860 Real beam_thickness = Beam::get_beam_thickness (me) / ss;
861 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
862 Real sit = (beam_thickness - slt) / 2;
864 Real hang = 1.0 - (beam_thickness - slt) / 2;
866 *dy = sign (*dy) * max (fabs (*dy),
867 min (min (sit, inter), hang));
873 MAKE_SCHEME_CALLBACK (Beam, calc_stem_shorten, 1)
875 Beam::calc_stem_shorten (SCM smob)
877 Grob *me = unsmob_grob (smob);
880 shortening looks silly for x staff beams
883 return scm_from_int (0);
885 Real forced_fraction = 1.0 * forced_stem_count (me)
886 / normal_stem_count (me);
888 int beam_count = get_beam_count (me);
890 SCM shorten_list = me->get_property ("beamed-stem-shorten");
891 if (shorten_list == SCM_EOL)
892 return scm_from_int (0);
894 Real staff_space = Staff_symbol_referencer::staff_space (me);
897 = robust_list_ref (beam_count -1, shorten_list);
898 Real shorten = scm_to_double (shorten_elt) * staff_space;
900 shorten *= forced_fraction;
904 return scm_from_double (shorten);
906 return scm_from_double (0.0);
911 Beam::no_visible_stem_positions (Grob *me, Interval default_value)
913 extract_grob_set (me, "stems", stems);
915 return default_value;
917 Interval head_positions;
919 for (vsize i = 0; i < stems.size(); i++)
921 head_positions.unite (Stem::head_positions (stems[i]));
922 multiplicity.unite (Stem::beam_multiplicity (stems[i]));
925 Direction dir = get_grob_direction (me);
926 Real y = head_positions[dir]
927 * 0.5 * Staff_symbol_referencer::staff_space (me)
928 + dir * get_beam_translation (me) * (multiplicity.length () + 1);
930 y /= Staff_symbol_referencer::staff_space (me);
931 return Interval (y,y);
936 Compute a first approximation to the beam slope.
938 MAKE_SCHEME_CALLBACK (Beam, calc_least_squares_positions, 2);
940 Beam::calc_least_squares_positions (SCM smob, SCM /* posns */)
942 Grob *me = unsmob_grob (smob);
944 int count = normal_stem_count (me);
947 return ly_interval2scm (no_visible_stem_positions (me, pos));
949 vector<Real> x_posns;
950 extract_grob_set (me, "normal-stems", stems);
951 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
952 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
954 Real my_y = me->relative_coordinate (commony, Y_AXIS);
956 Grob *fvs = first_normal_stem (me);
957 Grob *lvs = last_normal_stem (me);
959 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
960 + fvs->relative_coordinate (commony, Y_AXIS) - my_y,
961 Stem::get_stem_info (lvs).ideal_y_
962 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
964 Real x0 = first_normal_stem (me)->relative_coordinate (commonx, X_AXIS);
965 for (vsize i = 0; i < stems.size (); i++)
969 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
970 x_posns.push_back (x);
972 Real dx = last_normal_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
980 Interval chord (Stem::chord_start_y (stems[0]),
981 Stem::chord_start_y (stems.back ()));
983 /* Simple beams (2 stems) on middle line should be allowed to be
986 However, if both stems reach middle line,
987 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
989 For that case, we apply artificial slope */
990 if (!ideal[LEFT] && chord.delta () && count == 2)
993 Direction d = (Direction) (sign (chord.delta ()) * UP);
994 pos[d] = get_beam_thickness (me) / 2;
1001 For broken beams this doesn't work well. In this case, the
1002 slope esp. of the first part of a broken beam should predict
1003 where the second part goes.
1005 ldy = pos[RIGHT] - pos[LEFT];
1009 vector<Offset> ideals;
1010 for (vsize i = 0; i < stems.size (); i++)
1013 ideals.push_back (Offset (x_posns[i],
1014 Stem::get_stem_info (s).ideal_y_
1015 + s->relative_coordinate (commony, Y_AXIS)
1019 minimise_least_squares (&slope, &y, ideals);
1023 set_minimum_dy (me, &dy);
1026 pos = Interval (y, (y + dy));
1030 "position" is relative to the staff.
1032 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1034 me->set_property ("least-squares-dy", scm_from_double (ldy));
1035 return ly_interval2scm (pos);
1039 We can't combine with previous function, since check concave and
1040 slope damping comes first.
1042 TODO: we should use the concaveness to control the amount of damping
1045 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 2);
1047 Beam::shift_region_to_valid (SCM grob, SCM posns)
1049 Grob *me = unsmob_grob (grob);
1053 vector<Real> x_posns;
1054 extract_grob_set (me, "stems", stems);
1055 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
1056 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
1058 Grob *fvs = first_normal_stem (me);
1063 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
1064 for (vsize i = 0; i < stems.size (); i++)
1068 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
1069 x_posns.push_back (x);
1072 Grob *lvs = last_normal_stem (me);
1076 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
1078 Drul_array<Real> pos = ly_scm2interval (posns);
1080 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1082 Real dy = pos[RIGHT] - pos[LEFT];
1084 Real slope = dx ? (dy / dx) : 0.0;
1087 Shift the positions so that we have a chance of finding good
1088 quants (i.e. no short stem failures.)
1090 Interval feasible_left_point;
1091 feasible_left_point.set_full ();
1092 for (vsize i = 0; i < stems.size (); i++)
1095 if (Stem::is_invisible (s))
1098 Direction d = get_grob_direction (s);
1101 = Stem::get_stem_info (s).shortest_y_
1102 - slope * x_posns [i];
1105 left_y is now relative to the stem S. We want relative to
1106 ourselves, so translate:
1109 += + s->relative_coordinate (commony, Y_AXIS)
1110 - me->relative_coordinate (commony, Y_AXIS);
1116 feasible_left_point.intersect (flp);
1119 if (feasible_left_point.is_empty ())
1120 warning (_ ("no viable initial configuration found: may not find good beam slope"));
1121 else if (!feasible_left_point.contains (y))
1123 const int REGION_SIZE = 2; // UGH UGH
1124 if (isinf (feasible_left_point[DOWN]))
1125 y = feasible_left_point[UP] - REGION_SIZE;
1126 else if (isinf (feasible_left_point[UP]))
1127 y = feasible_left_point[DOWN]+ REGION_SIZE;
1129 y = feasible_left_point.center ();
1132 pos = Drul_array<Real> (y, (y + dy));
1133 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1135 return ly_interval2scm (pos);
1138 /* This neat trick is by Werner Lemberg,
1139 damped = tanh (slope)
1140 corresponds with some tables in [Wanske] CHECKME */
1141 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 2);
1143 Beam::slope_damping (SCM smob, SCM posns)
1145 Grob *me = unsmob_grob (smob);
1146 Drul_array<Real> pos = ly_scm2interval (posns);
1148 if (normal_stem_count (me) <= 1)
1151 SCM s = me->get_property ("damping");
1152 Real damping = scm_to_double (s);
1153 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
1154 if (concaveness >= 10000)
1156 pos[LEFT] = pos[RIGHT];
1157 me->set_property ("least-squares-dy", scm_from_double (0));
1163 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1165 Real dy = pos[RIGHT] - pos[LEFT];
1167 Grob *fvs = first_normal_stem (me);
1168 Grob *lvs = last_normal_stem (me);
1170 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1172 Real dx = last_normal_stem (me)->relative_coordinate (commonx, X_AXIS)
1173 - first_normal_stem (me)->relative_coordinate (commonx, X_AXIS);
1175 Real slope = dy && dx ? dy / dx : 0;
1177 slope = 0.6 * tanh (slope) / (damping + concaveness);
1179 Real damped_dy = slope * dx;
1181 set_minimum_dy (me, &damped_dy);
1183 pos[LEFT] += (dy - damped_dy) / 2;
1184 pos[RIGHT] -= (dy - damped_dy) / 2;
1186 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1189 return ly_interval2scm (pos);
1193 MAKE_SCHEME_CALLBACK (Beam, quanting, 2);
1195 Beam::quanting (SCM smob, SCM posns)
1197 Grob *me = unsmob_grob (smob);
1198 Drul_array<Real> ys(0, 0);
1199 ys = robust_scm2drul (posns, ys);
1200 Beam_scoring_problem problem (me, ys);
1202 ys = problem.solve ();
1203 return ly_interval2scm (ys);
1208 Report slice containing the numbers that are both in (car BEAMING)
1212 where_are_the_whole_beams (SCM beaming)
1216 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1218 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1220 l.add_point (scm_to_int (scm_car (s)));
1226 /* Return the Y position of the stem-end, given the Y-left, Y-right
1227 in POS for stem S. This Y position is relative to S. */
1229 Beam::calc_stem_y (Grob *me, Grob *stem, Grob **common,
1230 Real xl, Real xr, Direction feather_dir,
1231 Drul_array<Real> pos, bool french)
1233 Real beam_translation = get_beam_translation (me);
1234 Direction stem_dir = get_grob_direction (stem);
1237 Real relx = dx ? (stem->relative_coordinate (common[X_AXIS], X_AXIS) - xl)/dx : 0;
1238 Real xdir = 2*relx-1;
1240 Real stem_y = linear_combination(pos, xdir);
1242 SCM beaming = stem->get_property ("beaming");
1244 Slice beam_slice (french
1245 ? where_are_the_whole_beams (beaming)
1246 : Stem::beam_multiplicity (stem));
1247 if (beam_slice.is_empty ())
1248 beam_slice = Slice (0,0);
1249 Interval beam_multiplicity(beam_slice[LEFT],
1253 feather dir = 1 , relx 0->1 : factor 0 -> 1
1254 feather dir = 0 , relx 0->1 : factor 1 -> 1
1255 feather dir = -1, relx 0->1 : factor 1 -> 0
1257 Real feather_factor = 1;
1258 if (feather_dir > 0)
1259 feather_factor = relx;
1260 else if (feather_dir < 0)
1261 feather_factor = 1 - relx;
1263 stem_y += feather_factor * beam_translation
1264 * beam_multiplicity[Direction(((french) ? DOWN : UP)*stem_dir)];
1265 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1266 - stem->relative_coordinate (common[Y_AXIS], Y_AXIS);
1272 Hmm. At this time, beam position and slope are determined. Maybe,
1273 stem directions and length should set to relative to the chord's
1274 position of the beam. */
1275 MAKE_SCHEME_CALLBACK (Beam, set_stem_lengths, 1);
1277 Beam::set_stem_lengths (SCM smob)
1279 Grob *me = unsmob_grob (smob);
1281 /* trigger callbacks. */
1282 (void) me->get_property ("direction");
1283 (void) me->get_property ("beaming");
1285 SCM posns = me->get_property ("positions");
1287 extract_grob_set (me, "stems", stems);
1292 for (int a = 2; a--;)
1293 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1295 Drul_array<Real> pos = ly_scm2realdrul (posns);
1296 Real staff_space = Staff_symbol_referencer::staff_space (me);
1297 scale_drul (&pos, staff_space);
1301 if (robust_scm2int (me->get_property ("gap-count"), 0))
1304 thick = get_beam_thickness (me);
1307 Grob *fvs = first_normal_stem (me);
1308 Grob *lvs = last_normal_stem (me);
1310 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1311 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1312 Direction feather_dir = to_dir (me->get_property ("grow-direction"));
1314 for (vsize i = 0; i < stems.size (); i++)
1318 bool french = to_boolean (s->get_property ("french-beaming"));
1319 Real stem_y = calc_stem_y (me, s, common,
1320 xl, xr, feather_dir,
1321 pos, french && s != lvs && s!= fvs);
1324 Make the stems go up to the end of the beam. This doesn't matter
1325 for normal beams, but for tremolo beams it looks silly otherwise.
1328 && !Stem::is_invisible (s))
1329 stem_y += thick * 0.5 * get_grob_direction (s);
1332 Do set_stemend for invisible stems too, so tuplet brackets
1333 have a reference point for sloping
1335 Stem::set_stemend (s, 2 * stem_y / staff_space);
1342 Beam::set_beaming (Grob *me, Beaming_pattern const *beaming)
1344 extract_grob_set (me, "stems", stems);
1347 for (vsize i = 0; i < stems.size (); i++)
1350 Don't overwrite user settings.
1354 Grob *stem = stems[i];
1355 SCM beaming_prop = stem->get_property ("beaming");
1356 if (beaming_prop == SCM_EOL
1357 || index_get_cell (beaming_prop, d) == SCM_EOL)
1359 int count = beaming->beamlet_count (i, d);
1361 && i + 1 < stems.size ()
1362 && Stem::is_invisible (stem))
1363 count = min (count, beaming->beamlet_count (i,-d));
1365 if ( ((i == 0 && d == LEFT)
1366 || (i == stems.size ()-1 && d == RIGHT))
1367 && stems.size () > 1
1368 && to_boolean (me->get_property ("clip-edges")))
1371 Stem::set_beaming (stem, count, d);
1374 while (flip (&d) != LEFT);
1379 Beam::forced_stem_count (Grob *me)
1381 extract_grob_set (me, "normal-stems", stems);
1384 for (vsize i = 0; i < stems.size (); i++)
1388 /* I can imagine counting those boundaries as a half forced stem,
1389 but let's count them full for now. */
1390 Direction defdir = to_dir (s->get_property ("default-direction"));
1392 if (abs (Stem::chord_start_y (s)) > 0.1
1394 && get_grob_direction (s) != defdir)
1401 Beam::normal_stem_count (Grob *me)
1403 extract_grob_set (me, "normal-stems", stems);
1404 return stems.size ();
1408 Beam::first_normal_stem (Grob *me)
1410 extract_grob_set (me, "normal-stems", stems);
1411 return stems.size () ? stems[0] : 0;
1415 Beam::last_normal_stem (Grob *me)
1417 extract_grob_set (me, "normal-stems", stems);
1418 return stems.size () ? stems.back () : 0;
1424 handle rest under beam (do_post: beams are calculated now)
1425 what about combination of collisions and rest under beam.
1429 rest -> stem -> beam -> interpolate_y_position ()
1431 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Beam, rest_collision_callback, 2, 1, "");
1433 Beam::rest_collision_callback (SCM smob, SCM prev_offset)
1435 Grob *rest = unsmob_grob (smob);
1436 if (scm_is_number (rest->get_property ("staff-position")))
1437 return scm_from_int (0);
1439 Real offset = robust_scm2double (prev_offset, 0.0);
1441 Grob *st = unsmob_grob (rest->get_object ("stem"));
1444 return scm_from_double (0.0);
1445 Grob *beam = unsmob_grob (stem->get_object ("beam"));
1447 || !Beam::has_interface (beam)
1448 || !Beam::normal_stem_count (beam))
1449 return scm_from_double (0.0);
1451 Drul_array<Real> pos (robust_scm2drul (beam->get_property ("positions"),
1452 Drul_array<Real> (0,0)));
1454 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1456 scale_drul (&pos, staff_space);
1458 Real dy = pos[RIGHT] - pos[LEFT];
1460 Drul_array<Grob*> visible_stems (first_normal_stem (beam),
1461 last_normal_stem (beam));
1462 extract_grob_set (beam, "stems", stems);
1464 Grob *common = common_refpoint_of_array (stems, beam, X_AXIS);
1466 Real x0 = visible_stems[LEFT]->relative_coordinate (common, X_AXIS);
1467 Real dx = visible_stems[RIGHT]->relative_coordinate (common, X_AXIS) - x0;
1468 Real slope = dy && dx ? dy / dx : 0;
1470 Direction d = get_grob_direction (stem);
1471 Real stem_y = pos[LEFT]
1472 + (stem->relative_coordinate (common, X_AXIS) - x0) * slope;
1474 Real beam_translation = get_beam_translation (beam);
1475 Real beam_thickness = Beam::get_beam_thickness (beam);
1478 TODO: this is not strictly correct for 16th knee beams.
1481 = Stem::beam_multiplicity (stem).length () + 1;
1483 Real height_of_my_beams = beam_thickness / 2
1484 + (beam_count - 1) * beam_translation;
1485 Real beam_y = stem_y - d * height_of_my_beams;
1487 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1489 Interval rest_extent = rest->extent (rest, Y_AXIS);
1490 rest_extent.translate (offset + rest->get_parent (Y_AXIS)->relative_coordinate (common_y, Y_AXIS));
1492 Real rest_dim = rest_extent[d];
1493 Real minimum_distance
1494 = staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1495 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1497 Real shift = d * min (d * (beam_y - d * minimum_distance - rest_dim), 0.0);
1499 shift /= staff_space;
1500 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1502 /* Always move discretely by half spaces */
1503 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1505 /* Inside staff, move by whole spaces*/
1506 if ((rest_extent[d] + staff_space * shift) * d
1508 || (rest_extent[-d] + staff_space * shift) * -d
1510 shift = ceil (fabs (shift)) * sign (shift);
1512 return scm_from_double (offset + staff_space * shift);
1516 Beam::is_knee (Grob *me)
1518 SCM k = me->get_property ("knee");
1519 if (scm_is_bool (k))
1520 return ly_scm2bool (k);
1524 extract_grob_set (me, "stems", stems);
1525 for (vsize i = stems.size (); i--;)
1527 Direction dir = get_grob_direction (stems[i]);
1536 me->set_property ("knee", ly_bool2scm (knee));
1542 Beam::is_cross_staff (Grob *me)
1544 extract_grob_set (me, "stems", stems);
1545 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
1546 for (vsize i = 0; i < stems.size (); i++)
1547 if (Staff_symbol_referencer::get_staff_symbol (stems[i]) != staff_symbol)
1552 MAKE_SCHEME_CALLBACK (Beam, calc_cross_staff, 1)
1554 Beam::calc_cross_staff (SCM smob)
1556 return scm_from_bool (is_cross_staff (unsmob_grob (smob)));
1560 Beam::get_direction_beam_count (Grob *me, Direction d)
1562 extract_grob_set (me, "stems", stems);
1565 for (vsize i = stems.size (); i--;)
1568 Should we take invisible stems into account?
1570 if (get_grob_direction (stems[i]) == d)
1571 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1577 ADD_INTERFACE (Beam,
1580 "The @code{beam-thickness} property is the weight of beams,"
1581 " measured in staffspace. The @code{direction} property is"
1582 " not user-serviceable. Use the @code{direction} property"
1583 " of @code{Stem} instead.\n"
1585 "The following properties may be set in the @code{details}"
1589 "@item stem-length-demerit-factor\n"
1590 "Demerit factor used for inappropriate stem lengths.\n"
1591 "@item secondary-beam-demerit\n"
1592 "Demerit used in quanting calculations for multiple"
1594 "@item region-size\n"
1595 "Size of region for checking quant scores.\n"
1597 "Epsilon for beam quant code to check for presence"
1599 "@item stem-length-limit-penalty\n"
1600 "Penalty for differences in stem lengths on a beam.\n"
1601 "@item damping-direction-penalty\n"
1602 "Demerit penalty applied when beam direction is different"
1603 " from damping direction.\n"
1604 "@item hint-direction-penalty\n"
1605 "Demerit penalty applied when beam direction is different"
1606 " from damping direction, but damping slope is"
1607 " <= @code{round-to-zero-slope}.\n"
1608 "@item musical-direction-factor\n"
1609 "Demerit scaling factor for difference between"
1610 " beam slope and music slope.\n"
1611 "@item ideal-slope-factor\n"
1612 "Demerit scaling factor for difference between"
1613 " beam slope and damping slope.\n"
1614 "@item round-to-zero-slope\n"
1615 "Damping slope which is considered zero for purposes of"
1616 " calculating direction penalties.\n"
1622 "beamed-stem-shorten "
1639 "neutral-direction "
1642 "quantized-positions "