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])
55 Beam::brew_molecule_p() const
57 Molecule *mol_p = new Molecule;
59 // Real inter_f = paper()->interbeam_f ();
60 Real inter_f = paper()->internote_f ();
61 Real x0 = stems[0]->hpos_f();
62 for (int j=0; j <stems.size(); j++)
65 Stem * prev = (j > 0)? stems[j-1] : 0;
66 Stem * next = (j < stems.size()-1) ? stems[j+1] :0;
68 Molecule sb = stem_beams (i, next, prev);
69 Real x = i->hpos_f()-x0;
70 sb.translate (Offset (x, (x * slope + left_pos)* inter_f));
73 mol_p->translate (x0 - spanned_drul_[LEFT]->absolute_coordinate(X_AXIS), X_AXIS);
80 Real w=(paper()->note_width () + width ().length ())/2.0;
81 return Offset (w, (left_pos + w* slope)*paper()->internote_f ());
85 Beam::do_pre_processing()
92 Beam::do_print() const
95 DOUT << "slope " <<slope << "left ypos " << left_pos;
101 Beam::do_post_processing()
103 if (stems.size() < 2)
105 warning ("Beam with less than 2 stems");
106 transparent_b_ = true;
114 Beam::do_substitute_dependent (Score_elem*o,Score_elem*n)
116 if (o->is_type_b (Stem::static_name()))
117 stems.substitute ((Stem*)o->item(), n?(Stem*) n->item ():0);
121 Beam::do_width() const
123 return Interval (stems[0]->hpos_f(),
124 stems.top()->hpos_f ());
128 Beam::set_default_dir()
130 int up = 0, down = 0;
131 int up_count = 0, down_count = 0;
133 for (int i=0; i <stems.size(); i++)
136 int cur_down = sl->get_center_distance_from_top();
137 int cur_up = sl->get_center_distance_from_bottom();
154 // the following relation is equal to
155 // up / up_count > down / down_count
156 dir_ = (up * down_count > down * up_count) ? UP : DOWN;
158 for (int i=0; i <stems.size(); i++)
166 should use minimum energy formulation (cf linespacing)
169 the y of the (start) of the beam should be quantisized,
170 so that no stafflines appear just in between two beam-flags
176 Array<Stem_info> sinfo;
177 for (int j=0; j <stems.size(); j++)
181 i->set_default_extents();
182 if (i->invisible_b())
189 slope = left_pos = 0;
190 else if (sinfo.size() == 1)
193 left_pos = sinfo[0].idealy_f_;
198 Real leftx = sinfo[0].x;
200 for (int i=0; i < sinfo.size(); i++)
203 l.input.push (Offset (sinfo[i].x, sinfo[i].idealy_f_));
206 l.minimise (slope, left_pos);
210 for (int i=0; i < sinfo.size(); i++)
212 Real y = sinfo[i].x * slope + left_pos;
213 Real my = sinfo[i].miny_f_;
224 This neat trick is by Werner Lemberg, damped = tanh (slope) corresponds
225 with some tables in [Wanske]
227 slope = 0.6 * tanh (slope);
230 Real sl = slope*paper()->internote_f ();
231 paper()->lookup_l ()->beam (sl, 20 PT);
232 slope = sl /paper()->internote_f ();
238 Real x0 = stems[0]->hpos_f();
239 for (int j=0; j <stems.size(); j++)
243 Real x = s->hpos_f()-x0;
244 s->set_stemend (left_pos + slope * x);
249 Beam::set_grouping (Rhythmic_grouping def, Rhythmic_grouping cur)
253 assert (cur.children.size() == stems.size ());
260 for (int j=0; j <stems.size(); j++)
264 int f = intlog2(abs (s->flag_i_))-2;
269 b= cur.generate_beams (flags, fi);
272 assert (stems.size() == b.size ()/2);
275 for (int j=0, i=0; i < b.size() && j <stems.size (); i+= 2, j++)
278 s->beams_left_i_ = b[i];
279 s->beams_right_i_ = b[i+1];
284 beams to go with one stem.
287 Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
289 assert (!next || next->hpos_f() > here->hpos_f ());
290 assert (!prev || prev->hpos_f() < here->hpos_f ());
291 // Real dy=paper()->internote_f ()*2;
292 Real dy = paper()->interbeam_f ();
293 Real stemdx = paper()->rule_thickness ();
294 Real sl = slope*paper()->internote_f ();
295 paper()->lookup_l ()->beam (sl, 20 PT);
300 /* half beams extending to the left. */
303 int lhalfs= lhalfs = here->beams_left_i_ - prev->beams_right_i_ ;
304 int lwholebeams= here->beams_left_i_ <? prev->beams_right_i_ ;
305 Real w = (here->hpos_f() - prev->hpos_f ())/4;
308 if (lhalfs) // generates warnings if not
309 a = paper()->lookup_l ()->beam (sl, w);
310 a.translate (Offset (-w, -w * sl));
311 for (int j = 0; j < lhalfs; j++)
314 b.translate (-dir_ * dy * (lwholebeams+j), Y_AXIS);
321 int rhalfs = here->beams_right_i_ - next->beams_left_i_;
322 int rwholebeams = here->beams_right_i_ <? next->beams_left_i_;
324 Real w = next->hpos_f() - here->hpos_f ();
325 Atom a = paper()->lookup_l ()->beam (sl, w + stemdx);
328 for (; j < rwholebeams; j++)
331 b.translate (-dir_ * dy * j, Y_AXIS);
337 a = paper()->lookup_l ()->beam (sl, w);
339 for (; j < rwholebeams + rhalfs; j++)
342 b.translate (-dir_ * dy * j, Y_AXIS);
347 leftbeams.add (rightbeams);