2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
10 Less hairy code. knee: ([\stem 1; c8 \stem -1; c8]
21 #include "abbreviation-beam.hh"
25 #include "molecule.hh"
26 #include "leastsquares.hh"
28 #include "paper-def.hh"
30 #include "grouping.hh"
31 #include "stem-info.hh"
32 #include "main.hh" // experimental features
35 IMPLEMENT_IS_TYPE_B1 (Beam, Spanner);
37 const int MINIMUM_STEMLEN = 5;
49 s->add_dependency (this);
52 if (!spanned_drul_[LEFT])
59 Beam::brew_molecule_p () const
61 Molecule *mol_p = new Molecule;
62 Real inter_f = paper ()->internote_f ();
63 Real x0 = stems[0]->hpos_f ();
64 for (int j=0; j <stems.size (); j++)
67 Stem * prev = (j > 0)? stems[j-1] : 0;
68 Stem * next = (j < stems.size ()-1) ? stems[j+1] :0;
70 Molecule sb = stem_beams (i, next, prev);
71 Real x = i->hpos_f ()-x0;
72 sb.translate (Offset (x, (x * slope_f + left_y)* inter_f));
75 mol_p->translate_axis (x0 - spanned_drul_[LEFT]->absolute_coordinate (X_AXIS), X_AXIS);
82 Real w= (paper ()->note_width () + width ().length ())/2.0;
83 return Offset (w, (left_y + w* slope_f)*paper ()->internote_f ());
87 Beam::do_pre_processing ()
94 Beam::do_print () const
97 DOUT << "slope_f " <<slope_f << "left ypos " << left_y;
103 Beam::do_post_processing ()
105 if (stems.size () < 2)
107 warning (_ ("Beam with less than 2 stems"));
108 transparent_b_ = true;
116 Beam::do_substitute_dependent (Score_elem*o,Score_elem*n)
118 if (o->is_type_b (Stem::static_name ()))
119 stems.substitute ((Stem*)o->item (), n? (Stem*) n->item ():0);
123 Beam::do_width () const
125 return Interval (stems[0]->hpos_f (),
126 stems.top ()->hpos_f ());
130 Beam::set_default_dir ()
132 Drul_array<int> total;
133 total[UP] = total[DOWN] = 0;
134 Drul_array<int> count;
135 count[UP] = count[DOWN] = 0;
138 for (int i=0; i <stems.size (); i++)
141 int current = s->dir_
142 ? (1 + d * s->dir_)/2
143 : s->get_center_distance (Direction (-d));
151 } while ((d *= -1) != DOWN);
156 } while ((d *= -1) != DOWN);
158 /* the following relation is equal to
159 up / up_count > down / down_count
161 dir_ = (total[UP] * count[DOWN] > total[DOWN] * count[UP]) ? UP : DOWN;
163 for (int i=0; i <stems.size (); i++)
171 should use minimum energy formulation (cf linespacing)
177 Array<Stem_info> sinfo;
178 for (int j=0; j <stems.size (); j++)
182 i->set_default_extents ();
183 if (i->invisible_b ())
190 slope_f = left_y = 0;
191 else if (sinfo.size () == 1)
194 left_y = sinfo[0].idealy_f_;
199 Real leftx = sinfo[0].x;
201 for (int i=0; i < sinfo.size (); i++)
204 l.input.push (Offset (sinfo[i].x, sinfo[i].idealy_f_));
207 l.minimise (slope_f, left_y);
211 for (int i=0; i < sinfo.size (); i++)
213 Real y = sinfo[i].x * slope_f + left_y;
214 Real my = sinfo[i].miny_f_;
225 This neat trick is by Werner Lemberg, damped = tanh (slope_f) corresponds
226 with some tables in [Wanske]
228 slope_f = 0.6 * tanh (slope_f);
232 // y-values traditionally use internote dimension: therefore slope = (y/in)/x
233 // but mf and beam-lookup use PT dimension for y (as used for x-values)
234 // ugh --- there goes our simplified but careful quantisation
235 Real sl = slope_f * paper ()->internote_f ();
236 paper ()->lookup_l ()->beam (sl, 20 PT);
237 slope_f = sl / paper ()->internote_f ();
241 Beam::quantise_yspan ()
244 [Ross] (simplification of)
245 Try to set slope_f complying with y-span of:
247 - beam_thickness / 2 + staffline_thickness / 2
248 - beam_thickness + staffline_thickness
251 Real interline_f = paper ()->interline_f ();
252 Real internote_f = interline_f / 2;
253 Real staffline_thickness = paper ()->rule_thickness ();
254 Real beam_thickness = 0.48 * (interline_f - staffline_thickness);
256 const int QUANTS = 3;
259 beam_thickness / 2 + staffline_thickness / 2,
260 beam_thickness + staffline_thickness
263 Real xspan_f = stems.top ()->hpos_f () - stems[0]->hpos_f ();
264 // y-values traditionally use internote dimension: therefore slope = (y/in)/x
265 Real yspan_f = xspan_f * abs (slope_f * internote_f);
266 int yspan_i = (int)(yspan_f / interline_f);
267 Real q = (yspan_f / interline_f - yspan_i) * interline_f;
269 for (; i < QUANTS - 1; i++)
270 if ((q >= qdy[i]) && (q <= qdy[i + 1]))
272 if (q - qdy[i] < qdy[i + 1] - q)
282 yspan_f = (Real)yspan_i * interline_f + q;
283 // y-values traditionally use internote dimension: therefore slope = (y/in)/x
284 slope_f = yspan_f / xspan_f / internote_f * sign (slope_f);
288 Beam::quantise_left_y (Beam::Pos pos, bool extend_b)
291 quantising left y should suffice, as slope is quantised too
292 if extend then stems must not get shorter
295 Real interline_f = paper ()->interline_f ();
296 Real internote_f = interline_f / 2;
297 Real staffline_thickness = paper ()->rule_thickness ();
298 Real beam_thickness = 0.48 * (interline_f - staffline_thickness);
300 const int QUANTS = 6;
302 -staffline_thickness,
304 beam_thickness + staffline_thickness / 2,
305 interline_f / 2 + beam_thickness / 2 + staffline_thickness / 2,
306 interline_f - staffline_thickness,
307 interline_f + beam_thickness / 2,
310 ugh, using i triggers gcc 2.7.2.1 internal compiler error (far down):
311 for (int i = 0; i < QUANTS; i++)
313 for (int ii = 0; ii < QUANTS; ii++)
314 qy[ii] -= beam_thickness / 2;
324 // y-values traditionally use internote dimension
325 Real y = left_y * internote_f;
326 int y_i = (int)floor(y / interline_f);
327 y = (y / interline_f - y_i) * interline_f;
330 for (int ii = 0; ii < QUANTS; ii++)
331 qy[ii] -= interline_f;
335 for (; i < QUANTS; i++)
339 // found if lower_i is allowed, and nearer (from below) y than new pos
340 if ((pos & qpos[lower_i]) && (y - qy[lower_i] < y - qy[i]))
342 // if new pos is allowed or old pos isn't: assign new pos
343 if ((pos & qpos[i]) || !(pos & qpos[lower_i]))
347 int upper_i = QUANTS - 1;
348 for (i = QUANTS - 1; i >= 0; i--)
352 // found if upper_i is allowed, and nearer (from above) y than new pos
353 if ((pos & qpos[upper_i]) && (qy[upper_i] - y < qy[i] - y))
355 // if new pos is allowed or old pos isn't: assign new pos
356 if ((pos & qpos[i]) || !(pos & qpos[upper_i]))
360 // y-values traditionally use internote dimension
361 Real upper_y = (qy[upper_i] + interline_f * y_i) / internote_f;
362 Real lower_y = (qy[lower_i] + interline_f * y_i) / internote_f;
365 left_y = (dir_ > 0 ? upper_y : lower_y);
367 left_y = (upper_y - left_y < y - lower_y ? upper_y : lower_y);
371 Beam::set_stemlens ()
373 Real x0 = stems[0]->hpos_f ();
376 Real interline_f = paper ()->interline_f ();
377 Real internote_f = interline_f / 2;
378 Real staffline_thickness = paper ()->rule_thickness ();
379 Real beam_thickness = 0.48 * (interline_f - staffline_thickness);
380 Real xspan_f = stems.top ()->hpos_f () - stems[0]->hpos_f ();
382 ugh, y values are in "internote" dimension
384 Real yspan_f = xspan_f * abs (slope_f * internote_f);
385 int yspan_i = (int)(yspan_f / interline_f);
389 if (yspan_f < staffline_thickness / 2)
390 left_pos = (Pos)(STRADDLE | SIT | HANG);
392 left_pos = (Pos) (sign (slope_f) > 0 ? STRADDLE | HANG
396 ugh, slope currently mangled by availability mf chars...
397 be more generous regarding beam position between stafflines
399 Real q = (yspan_f / interline_f - yspan_i) * interline_f;
400 if (q < interline_f / 3 - beam_thickness / 2)
401 left_pos = (Pos) (left_pos | INTER);
403 if (stems[0]->beams_right_i_ > 1)
404 left_pos = (Pos)(left_pos & (STRADDLE | INTER));
406 // ugh, rounding problems!
407 const Real EPSILON = interline_f / 10;
411 quantise_left_y (left_pos, dy);
413 for (int j=0; j < stems.size (); j++)
417 Real x = s->hpos_f () - x0;
418 s->set_stemend (left_y + slope_f * x);
419 Real y = s->stem_length_f ();
420 if (y < MINIMUM_STEMLEN)
421 dy = dy >? (MINIMUM_STEMLEN - y);
423 } while (abs (dy) > EPSILON)
427 Beam::set_grouping (Rhythmic_grouping def, Rhythmic_grouping cur)
431 assert (cur.children.size () == stems.size ());
438 for (int j=0; j <stems.size (); j++)
442 int f = s->flag_i_ - 2;
447 b= cur.generate_beams (flags, fi);
450 assert (stems.size () == b.size ()/2);
453 for (int j=0, i=0; i < b.size () && j <stems.size (); i+= 2, j++)
456 s->beams_left_i_ = b[i];
457 s->beams_right_i_ = b[i+1];
462 beams to go with one stem.
465 Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
467 assert (!next || next->hpos_f () > here->hpos_f ());
468 assert (!prev || prev->hpos_f () < here->hpos_f ());
469 // Real dy=paper ()->internote_f ()*2;
470 Real dy = paper ()->interbeam_f ();
471 Real stemdx = paper ()->rule_thickness ();
472 Real sl = slope_f*paper ()->internote_f ();
473 paper ()->lookup_l ()->beam (sl, 20 PT);
478 /* half beams extending to the left. */
481 int lhalfs= lhalfs = here->beams_left_i_ - prev->beams_right_i_ ;
482 int lwholebeams= here->beams_left_i_ <? prev->beams_right_i_ ;
483 Real w = (here->hpos_f () - prev->hpos_f ())/4;
485 if (lhalfs) // generates warnings if not
486 a = paper ()->lookup_l ()->beam (sl, w);
487 a.translate (Offset (-w, -w * sl));
488 for (int j = 0; j < lhalfs; j++)
491 b.translate_axis (-dir_ * dy * (lwholebeams+j), Y_AXIS);
498 int rhalfs = here->beams_right_i_ - next->beams_left_i_;
499 int rwholebeams = here->beams_right_i_ <? next->beams_left_i_;
501 Real w = next->hpos_f () - here->hpos_f ();
502 Atom a = paper ()->lookup_l ()->beam (sl, w + stemdx);
506 if (here->beam_gap_i_)
508 int nogap = rwholebeams - here->beam_gap_i_;
509 for (; j < nogap; j++)
512 b.translate_axis (-dir_ * dy * j, Y_AXIS);
515 // TODO: notehead widths differ for different types
516 gap_f = paper ()->note_width () / 2;
518 a = paper ()->lookup_l ()->beam (sl, w + stemdx);
521 for (; j < rwholebeams; j++)
524 b.translate (Offset (gap_f, -dir_ * dy * j));
530 a = paper ()->lookup_l ()->beam (sl, w);
532 for (; j < rwholebeams + rhalfs; j++)
535 b.translate_axis (-dir_ * dy * j, Y_AXIS);
540 leftbeams.add (rightbeams);