lilypond-0.1.16
[lilypond.git] / lily / beam.cc
blobd2b5e1e3c620a3d07a7460be9b052f0433c8a50b
1 /*
2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
8 TODO
10 Less hairy code. knee: ([\stem 1; c8 \stem -1; c8]
14 #include <math.h>
16 #include "p-col.hh"
17 #include "varray.hh"
18 #include "proto.hh"
19 #include "dimen.hh"
20 #include "beam.hh"
21 #include "abbreviation-beam.hh"
22 #include "misc.hh"
23 #include "debug.hh"
24 #include "atom.hh"
25 #include "molecule.hh"
26 #include "leastsquares.hh"
27 #include "stem.hh"
28 #include "paper-def.hh"
29 #include "lookup.hh"
30 #include "grouping.hh"
31 #include "stem-info.hh"
34 IMPLEMENT_IS_TYPE_B1(Beam, Spanner);
36 Beam::Beam()
38 slope = 0;
39 left_pos = 0.0;
42 void
43 Beam::add (Stem*s)
45 stems.push (s);
46 s->add_dependency (this);
47 s->beam_l_ = this;
49 if (!spanned_drul_[LEFT])
50 set_bounds (LEFT,s);
51 else
52 set_bounds (RIGHT,s);
55 Molecule*
56 Beam::brew_molecule_p() const
58 Molecule *mol_p = new Molecule;
59 // huh? inter-what
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++)
65 Stem *i = stems[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));
72 mol_p->add (sb);
74 mol_p->translate (x0 - spanned_drul_[LEFT]->absolute_coordinate(X_AXIS), X_AXIS);
75 return mol_p;
78 Offset
79 Beam::center() const
81 Real w=(paper()->note_width () + width ().length ())/2.0;
82 return Offset (w, (left_pos + w* slope)*paper()->internote_f ());
85 void
86 Beam::do_pre_processing()
88 if (!dir_)
89 set_default_dir();
92 void
93 Beam::do_print() const
95 #ifndef NPRINT
96 DOUT << "slope " <<slope << "left ypos " << left_pos;
97 Spanner::do_print();
98 #endif
101 void
102 Beam::do_post_processing()
104 if (stems.size() < 2)
106 warning ("Beam with less than 2 stems");
107 transparent_b_ = true;
108 return ;
110 solve_slope();
111 set_stemlens();
114 void
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);
121 Interval
122 Beam::do_width() const
124 return Interval (stems[0]->hpos_f(),
125 stems.top()->hpos_f ());
128 void
129 Beam::set_default_dir()
131 int up = 0, down = 0;
132 int up_count = 0, down_count = 0;
134 for (int i=0; i <stems.size(); i++)
136 Stem *sl = stems[i];
137 int cur_down = sl->get_center_distance_from_top();
138 int cur_up = sl->get_center_distance_from_bottom();
139 if (cur_down)
141 down += cur_down;
142 down_count++;
144 if (cur_up)
146 up += cur_up;
147 up_count++;
150 if (!down)
151 down_count = 1;
152 if (!up)
153 up_count = 1;
155 // the following relation is equal to
156 // up / up_count > down / down_count
157 dir_ = (up * down_count > down * up_count) ? UP : DOWN;
159 for (int i=0; i <stems.size(); i++)
161 Stem *sl = stems[i];
162 sl->dir_ = dir_;
167 should use minimum energy formulation (cf linespacing)
169 [todo]
170 the y of the (start) of the beam should be quantisized,
171 so that no stafflines appear just in between two beam-flags
174 void
175 Beam::solve_slope()
177 Array<Stem_info> sinfo;
178 for (int j=0; j <stems.size(); j++)
180 Stem *i = stems[j];
182 i->set_default_extents();
183 if (i->invisible_b())
184 continue;
186 Stem_info info (i);
187 sinfo.push (info);
189 if (! sinfo.size())
190 slope = left_pos = 0;
191 else if (sinfo.size() == 1)
193 slope = 0;
194 left_pos = sinfo[0].idealy_f_;
196 else
199 Real leftx = sinfo[0].x;
200 Least_squares l;
201 for (int i=0; i < sinfo.size(); i++)
203 sinfo[i].x -= leftx;
204 l.input.push (Offset (sinfo[i].x, sinfo[i].idealy_f_));
207 l.minimise (slope, left_pos);
210 Real dy = 0.0;
211 for (int i=0; i < sinfo.size(); i++)
213 Real y = sinfo[i].x * slope + left_pos;
214 Real my = sinfo[i].miny_f_;
216 if (my - y > dy)
217 dy = my -y;
219 left_pos += dy;
220 left_pos *= dir_;
222 slope *= dir_;
225 This neat trick is by Werner Lemberg, damped = tanh (slope) corresponds
226 with some tables in [Wanske]
228 slope = 0.6 * tanh (slope);
230 // ugh
231 Real sl = slope*paper()->internote_f ();
232 paper()->lookup_l ()->beam (sl, 20 PT);
233 slope = sl /paper()->internote_f ();
236 void
237 Beam::set_stemlens()
239 Real x0 = stems[0]->hpos_f();
240 for (int j=0; j <stems.size(); j++)
242 Stem *s = stems[j];
244 Real x = s->hpos_f()-x0;
245 s->set_stemend (left_pos + slope * x);
249 void
250 Beam::set_grouping (Rhythmic_grouping def, Rhythmic_grouping cur)
252 def.OK();
253 cur.OK();
254 assert (cur.children.size() == stems.size ());
256 cur.split (def);
258 Array<int> b;
260 Array<int> flags;
261 for (int j=0; j <stems.size(); j++)
263 Stem *s = stems[j];
265 int f = s->flag_i_ - 2;
266 assert (f>0);
267 flags.push (f);
269 int fi =0;
270 b= cur.generate_beams (flags, fi);
271 b.insert (0,0);
272 b.push (0);
273 assert (stems.size() == b.size ()/2);
276 for (int j=0, i=0; i < b.size() && j <stems.size (); i+= 2, j++)
278 Stem *s = stems[j];
279 s->beams_left_i_ = b[i];
280 s->beams_right_i_ = b[i+1];
285 beams to go with one stem.
287 Molecule
288 Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
290 assert (!next || next->hpos_f() > here->hpos_f ());
291 assert (!prev || prev->hpos_f() < here->hpos_f ());
292 // Real dy=paper()->internote_f ()*2;
293 Real dy = paper()->interbeam_f ();
294 Real stemdx = paper()->rule_thickness ();
295 Real sl = slope*paper()->internote_f ();
296 paper()->lookup_l ()->beam (sl, 20 PT);
298 Molecule leftbeams;
299 Molecule rightbeams;
301 /* half beams extending to the left. */
302 if (prev)
304 int lhalfs= lhalfs = here->beams_left_i_ - prev->beams_right_i_ ;
305 int lwholebeams= here->beams_left_i_ <? prev->beams_right_i_ ;
306 Real w = (here->hpos_f () - prev->hpos_f ())/4;
307 Atom a;
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++)
313 Atom b (a);
314 b.translate (-dir_ * dy * (lwholebeams+j), Y_AXIS);
315 leftbeams.add (b);
319 if (next)
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);
327 int j = 0;
328 Real gap_f = 0;
329 if (here->beam_gap_i_)
331 int nogap = rwholebeams - here->beam_gap_i_;
332 for (; j < nogap; j++)
334 Atom b (a);
335 b.translate (-dir_ * dy * j, Y_AXIS);
336 rightbeams.add (b);
338 // TODO: notehead widths differ for different types
339 gap_f = paper()->note_width () / 2;
340 w -= 2 * gap_f;
341 a = paper()->lookup_l ()->beam (sl, w + stemdx);
344 for (; j < rwholebeams; j++)
346 Atom b (a);
347 b.translate (Offset (gap_f, -dir_ * dy * j));
348 rightbeams.add (b);
351 w /= 4;
352 if (rhalfs)
353 a = paper()->lookup_l ()->beam (sl, w);
355 for (; j < rwholebeams + rhalfs; j++)
357 Atom b (a);
358 b.translate (-dir_ * dy * j, Y_AXIS);
359 rightbeams.add (b);
363 leftbeams.add (rightbeams);
364 return leftbeams;