2 beam.cc -- implement Beam
4 source file of the GNU GNU LilyPond music typesetter
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
10 Less hairy code. Better slope calculations.
11 knee: ([\stem 1; c8 \stem -1; c8]
25 #include "molecule.hh"
26 #include "leastsquares.hh"
28 #include "paper-def.hh"
30 #include "grouping.hh"
42 Stem_info(Stem const *);
45 Stem_info::Stem_info(Stem const *s)
49 idealy = dir * s->stem_end_f();
50 miny = dir * s->stem_start_f() + 2; // ugh
51 assert(miny <= idealy);
62 Real w=(paper()->note_width() + width().length())/2.0;
63 return Offset(w, (left_pos + w* slope)*paper()->internote_f());
77 s->add_dependency(this);
78 s->print_flag_b_ = false;
82 Beam::set_default_dir()
85 int up_count = 0, down_count = 0;
87 for (int i=0; i <stems.size(); i++) {
89 int cur_down = sl->get_center_distance_from_top();
90 int cur_up = sl->get_center_distance_from_bottom();
105 // the following relation is equal to
106 // up / up_count > down / down_count
107 dir_i_ = (up * down_count > down * up_count) ? 1 : -1;
109 for (int i=0; i <stems.size(); i++) {
116 should use minimum energy formulation (cf linespacing)
121 Array<Stem_info> sinfo;
122 for (int j=0; j <stems.size(); j++) {
125 i->set_default_extents();
126 if (i->invisible_b())
132 Real leftx = sinfo[0].x;
134 for (int i=0; i < sinfo.size(); i++) {
136 l.input.push(Offset(sinfo[i].x, sinfo[i].idealy));
139 l.minimise(slope, left_pos);
141 for (int i=0; i < sinfo.size(); i++) {
142 Real y = sinfo[i].x * slope + left_pos;
143 Real my = sinfo[i].miny;
152 slope = 0.6 * tanh(slope); // damping
155 Real sl = slope*paper()->internote_f();
156 paper()->lookup_l()->beam(sl, 20 PT);
157 slope = sl /paper()->internote_f();
163 Real x0 = stems[0]->hpos_f();
164 for (int j=0; j <stems.size(); j++) {
167 Real x = s->hpos_f()-x0;
168 s->set_stemend(left_pos + slope * x);
174 Beam::do_post_processing()
181 Beam::set_grouping(Rhythmic_grouping def, Rhythmic_grouping cur)
185 assert(cur.children.size() == stems.size());
192 for (int j=0; j <stems.size(); j++) {
195 int f = intlog2(abs(s->flag_i_))-2;
200 b= cur.generate_beams(flags, fi);
203 assert(stems.size() == b.size()/2);
206 for (int j=0, i=0; i < b.size() && j <stems.size(); i+= 2, j++) {
208 s->beams_left_i_ = b[i];
209 s->beams_right_i_ = b[i+1];
214 Beam::do_pre_processing()
216 assert(stems.size()>1);
224 Beam::do_width() const
226 return Interval( stems[0]->hpos_f(),
227 stems.top()->hpos_f() );
231 beams to go with one stem.
234 Beam::stem_beams(Stem *here, Stem *next, Stem *prev)const
236 assert( !next || next->hpos_f() > here->hpos_f() );
237 assert( !prev || prev->hpos_f() < here->hpos_f() );
238 Real dy=paper()->internote_f()*2;
239 Real stemdx = paper()->rule_thickness();
240 Real sl = slope*paper()->internote_f();
241 paper()->lookup_l()->beam(sl, 20 PT);
246 /* half beams extending to the left. */
248 int lhalfs= lhalfs = here->beams_left_i_ - prev->beams_right_i_ ;
249 int lwholebeams= here->beams_left_i_ <? prev->beams_right_i_ ;
250 Real w = (here->hpos_f() - prev->hpos_f())/4;
253 if (lhalfs) // generates warnings if not
254 a = paper()->lookup_l()->beam(sl, w);
255 a.translate(Offset (-w, -w * sl));
256 for (int j = 0; j < lhalfs; j++) {
258 b.translate_y( -dir_i_ * dy * (lwholebeams+j));
264 int rhalfs = here->beams_right_i_ - next->beams_left_i_;
265 int rwholebeams = here->beams_right_i_ <? next->beams_left_i_;
267 Real w = next->hpos_f() - here->hpos_f();
268 Atom a = paper()->lookup_l()->beam(sl, w + stemdx);
271 for (; j < rwholebeams; j++) {
273 b.translate_y( -dir_i_ * dy * j);
279 a = paper()->lookup_l()->beam(sl, w);
281 for (; j < rwholebeams + rhalfs; j++) {
283 b.translate_y( -dir_i_ * dy * j);
288 leftbeams.add(rightbeams);
294 Beam::brew_molecule_p() const
297 Real inter=paper()->internote_f();
299 Real x0 = stems[0]->hpos_f();
300 for (int j=0; j <stems.size(); j++) {
302 Stem * prev = (j > 0)? stems[j-1] : 0;
303 Stem * next = (j < stems.size()-1) ? stems[j+1] :0;
305 Molecule sb = stem_beams(i, next, prev);
306 Real x = i->hpos_f()-x0;
307 sb.translate(Offset(x, (x * slope + left_pos)* inter));
310 out->translate_x(x0 - left_col_l_->hpos);
314 IMPLEMENT_STATIC_NAME(Beam);
315 IMPLEMENT_IS_TYPE_B1(Beam, Spanner);
318 Beam::do_print()const
321 mtor << "slope " <<slope << "left ypos " << left_pos;
327 Beam::do_substitute_dependency(Score_elem*o,Score_elem*n)
330 while ((i=stems.find_i((Stem*)o->item())) >=0)
331 if (n) stems[i] = (Stem*) n->item();