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"
34 IMPLEMENT_IS_TYPE_B1(Beam, Spanner);
46 s->add_dependency (this);
49 if (!spanned_drul_[LEFT])
56 Beam::brew_molecule_p() const
58 Molecule *mol_p = new Molecule;
60 // Real inter_f = paper()->interbeam_f ();
61 Real inter_f = paper()->internote_f ();
62 Real x0 = stems[0]->hpos_f();
63 for (int j=0; j <stems.size(); j++)
66 Stem * prev = (j > 0)? stems[j-1] : 0;
67 Stem * next = (j < stems.size()-1) ? stems[j+1] :0;
69 Molecule sb = stem_beams (i, next, prev);
70 Real x = i->hpos_f()-x0;
71 sb.translate (Offset (x, (x * slope + left_pos)* inter_f));
74 mol_p->translate_axis (x0 - spanned_drul_[LEFT]->absolute_coordinate(X_AXIS), X_AXIS);
81 Real w=(paper()->note_width () + width ().length ())/2.0;
82 return Offset (w, (left_pos + w* slope)*paper()->internote_f ());
86 Beam::do_pre_processing()
93 Beam::do_print() const
96 DOUT << "slope " <<slope << "left ypos " << left_pos;
102 Beam::do_post_processing()
104 if (stems.size() < 2)
106 warning (_("Beam with less than 2 stems"));
107 transparent_b_ = true;
115 Beam::do_substitute_dependent (Score_elem*o,Score_elem*n)
117 if (o->is_type_b (Stem::static_name()))
118 stems.substitute ((Stem*)o->item(), n?(Stem*) n->item ():0);
122 Beam::do_width() const
124 return Interval (stems[0]->hpos_f(),
125 stems.top()->hpos_f ());
129 Beam::set_default_dir()
131 Drul_array<int> total;
132 total[UP] = total[DOWN] = 0;
133 Drul_array<int> count;
134 count[UP] = count[DOWN] = 0;
137 for (int i=0; i <stems.size(); i++)
140 int current = s->dir_
141 ? (1 + d * s->dir_)/2
142 : s->get_center_distance(Direction(-d));
150 } while ((d *= -1) != DOWN);
155 } while ((d *= -1) != DOWN);
157 /* the following relation is equal to
158 up / up_count > down / down_count
160 dir_ = (total[UP] * count[DOWN] > total[DOWN] * count[UP]) ? UP : DOWN;
162 for (int i=0; i <stems.size(); i++)
170 should use minimum energy formulation (cf linespacing)
173 the y of the (start) of the beam should be quantisized,
174 so that no stafflines appear just in between two beam-flags
180 Array<Stem_info> sinfo;
181 for (int j=0; j <stems.size(); j++)
185 i->set_default_extents();
186 if (i->invisible_b())
193 slope = left_pos = 0;
194 else if (sinfo.size() == 1)
197 left_pos = sinfo[0].idealy_f_;
202 Real leftx = sinfo[0].x;
204 for (int i=0; i < sinfo.size(); i++)
207 l.input.push (Offset (sinfo[i].x, sinfo[i].idealy_f_));
210 l.minimise (slope, left_pos);
214 for (int i=0; i < sinfo.size(); i++)
216 Real y = sinfo[i].x * slope + left_pos;
217 Real my = sinfo[i].miny_f_;
228 This neat trick is by Werner Lemberg, damped = tanh (slope) corresponds
229 with some tables in [Wanske]
231 slope = 0.6 * tanh (slope);
234 Real sl = slope*paper()->internote_f ();
235 paper()->lookup_l ()->beam (sl, 20 PT);
236 slope = sl /paper()->internote_f ();
243 should check for visibility of stem..
245 Real x0 = stems[0]->hpos_f();
246 for (int j=0; j <stems.size(); j++)
250 Real x = s->hpos_f()-x0;
251 s->set_stemend (left_pos + slope * x);
256 Beam::set_grouping (Rhythmic_grouping def, Rhythmic_grouping cur)
260 assert (cur.children.size() == stems.size ());
267 for (int j=0; j <stems.size(); j++)
271 int f = s->flag_i_ - 2;
276 b= cur.generate_beams (flags, fi);
279 assert (stems.size() == b.size ()/2);
282 for (int j=0, i=0; i < b.size() && j <stems.size (); i+= 2, j++)
285 s->beams_left_i_ = b[i];
286 s->beams_right_i_ = b[i+1];
291 beams to go with one stem.
294 Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
296 assert (!next || next->hpos_f() > here->hpos_f ());
297 assert (!prev || prev->hpos_f() < here->hpos_f ());
298 // Real dy=paper()->internote_f ()*2;
299 Real dy = paper()->interbeam_f ();
300 Real stemdx = paper()->rule_thickness ();
301 Real sl = slope*paper()->internote_f ();
302 paper()->lookup_l ()->beam (sl, 20 PT);
307 /* half beams extending to the left. */
310 int lhalfs= lhalfs = here->beams_left_i_ - prev->beams_right_i_ ;
311 int lwholebeams= here->beams_left_i_ <? prev->beams_right_i_ ;
312 Real w = (here->hpos_f () - prev->hpos_f ())/4;
314 if (lhalfs) // generates warnings if not
315 a = paper()->lookup_l ()->beam (sl, w);
316 a.translate (Offset (-w, -w * sl));
317 for (int j = 0; j < lhalfs; j++)
320 b.translate_axis (-dir_ * dy * (lwholebeams+j), Y_AXIS);
327 int rhalfs = here->beams_right_i_ - next->beams_left_i_;
328 int rwholebeams = here->beams_right_i_ <? next->beams_left_i_;
330 Real w = next->hpos_f() - here->hpos_f ();
331 Atom a = paper()->lookup_l ()->beam (sl, w + stemdx);
335 if (here->beam_gap_i_)
337 int nogap = rwholebeams - here->beam_gap_i_;
338 for (; j < nogap; j++)
341 b.translate_axis (-dir_ * dy * j, Y_AXIS);
344 // TODO: notehead widths differ for different types
345 gap_f = paper()->note_width () / 2;
347 a = paper()->lookup_l ()->beam (sl, w + stemdx);
350 for (; j < rwholebeams; j++)
353 b.translate (Offset (gap_f, -dir_ * dy * j));
359 a = paper()->lookup_l ()->beam (sl, w);
361 for (; j < rwholebeams + rhalfs; j++)
364 b.translate_axis (-dir_ * dy * j, Y_AXIS);
369 leftbeams.add (rightbeams);