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]
24 #include "molecule.hh"
25 #include "leastsquares.hh"
27 #include "paper-def.hh"
29 #include "grouping.hh"
30 #include "stem-info.hh"
33 IMPLEMENT_IS_TYPE_B1(Beam, Spanner);
45 s->add_dependency (this);
48 if (!spanned_drul_[LEFT])
54 Beam::brew_molecule_p() const
56 Molecule *mol_p = new Molecule;
58 // Real inter_f = paper()->interbeam_f ();
59 Real inter_f = paper()->internote_f ();
60 Real x0 = stems[0]->hpos_f();
61 for (int j=0; j <stems.size(); j++)
64 Stem * prev = (j > 0)? stems[j-1] : 0;
65 Stem * next = (j < stems.size()-1) ? stems[j+1] :0;
67 Molecule sb = stem_beams (i, next, prev);
68 Real x = i->hpos_f()-x0;
69 sb.translate (Offset (x, (x * slope + left_pos)* inter_f));
72 mol_p->translate (x0 - spanned_drul_[LEFT]->absolute_coordinate(X_AXIS), X_AXIS);
79 Real w=(paper()->note_width () + width ().length ())/2.0;
80 return Offset (w, (left_pos + w* slope)*paper()->internote_f ());
84 Beam::do_pre_processing()
91 Beam::do_print() const
94 DOUT << "slope " <<slope << "left ypos " << left_pos;
100 Beam::do_post_processing()
102 if (stems.size() < 2)
104 warning ("Beam with less than 2 stems");
105 transparent_b_ = true;
113 Beam::do_substitute_dependent (Score_elem*o,Score_elem*n)
115 if (o->is_type_b (Stem::static_name()))
116 stems.substitute ((Stem*)o->item(), n?(Stem*) n->item ():0);
120 Beam::do_width() const
122 return Interval (stems[0]->hpos_f(),
123 stems.top()->hpos_f ());
127 Beam::set_default_dir()
129 int up = 0, down = 0;
130 int up_count = 0, down_count = 0;
132 for (int i=0; i <stems.size(); i++)
135 int cur_down = sl->get_center_distance_from_top();
136 int cur_up = sl->get_center_distance_from_bottom();
153 // the following relation is equal to
154 // up / up_count > down / down_count
155 dir_ = (up * down_count > down * up_count) ? UP : DOWN;
157 for (int i=0; i <stems.size(); i++)
165 should use minimum energy formulation (cf linespacing)
168 the y of the (start) of the beam should be quantisized,
169 so that no stafflines appear just in between two beam-flags
175 Array<Stem_info> sinfo;
176 for (int j=0; j <stems.size(); j++)
180 i->set_default_extents();
181 if (i->invisible_b())
188 slope = left_pos = 0;
189 else if (sinfo.size() == 1)
192 left_pos = sinfo[0].idealy_f_;
197 Real leftx = sinfo[0].x;
199 for (int i=0; i < sinfo.size(); i++)
202 l.input.push (Offset (sinfo[i].x, sinfo[i].idealy_f_));
205 l.minimise (slope, left_pos);
209 for (int i=0; i < sinfo.size(); i++)
211 Real y = sinfo[i].x * slope + left_pos;
212 Real my = sinfo[i].miny_f_;
223 This neat trick is by Werner Lemberg, damped = tanh (slope) corresponds
224 with some tables in [Wanske]
226 slope = 0.6 * tanh (slope);
229 Real sl = slope*paper()->internote_f ();
230 paper()->lookup_l ()->beam (sl, 20 PT);
231 slope = sl /paper()->internote_f ();
237 Real x0 = stems[0]->hpos_f();
238 for (int j=0; j <stems.size(); j++)
242 Real x = s->hpos_f()-x0;
243 s->set_stemend (left_pos + slope * x);
248 Beam::set_grouping (Rhythmic_grouping def, Rhythmic_grouping cur)
252 assert (cur.children.size() == stems.size ());
259 for (int j=0; j <stems.size(); j++)
263 int f = intlog2(abs (s->flag_i_))-2;
268 b= cur.generate_beams (flags, fi);
271 assert (stems.size() == b.size ()/2);
274 for (int j=0, i=0; i < b.size() && j <stems.size (); i+= 2, j++)
277 s->beams_left_i_ = b[i];
278 s->beams_right_i_ = b[i+1];
283 beams to go with one stem.
286 Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
288 assert (!next || next->hpos_f() > here->hpos_f ());
289 assert (!prev || prev->hpos_f() < here->hpos_f ());
290 // Real dy=paper()->internote_f ()*2;
291 Real dy = paper()->interbeam_f ();
292 Real stemdx = paper()->rule_thickness ();
293 Real sl = slope*paper()->internote_f ();
294 paper()->lookup_l ()->beam (sl, 20 PT);
299 /* half beams extending to the left. */
302 int lhalfs= lhalfs = here->beams_left_i_ - prev->beams_right_i_ ;
303 int lwholebeams= here->beams_left_i_ <? prev->beams_right_i_ ;
304 Real w = (here->hpos_f() - prev->hpos_f ())/4;
307 if (lhalfs) // generates warnings if not
308 a = paper()->lookup_l ()->beam (sl, w);
309 a.translate (Offset (-w, -w * sl));
310 for (int j = 0; j < lhalfs; j++)
313 b.translate (-dir_ * dy * (lwholebeams+j), Y_AXIS);
320 int rhalfs = here->beams_right_i_ - next->beams_left_i_;
321 int rwholebeams = here->beams_right_i_ <? next->beams_left_i_;
323 Real w = next->hpos_f() - here->hpos_f ();
324 Atom a = paper()->lookup_l ()->beam (sl, w + stemdx);
327 for (; j < rwholebeams; j++)
330 b.translate (-dir_ * dy * j, Y_AXIS);
336 a = paper()->lookup_l ()->beam (sl, w);
338 for (; j < rwholebeams + rhalfs; j++)
341 b.translate (-dir_ * dy * j, Y_AXIS);
346 leftbeams.add (rightbeams);