]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/beam-scoring-problem.hh
Beam collision scoring
[lilypond.git] / lily / include / beam-scoring-problem.hh
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1996--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5   Jan Nieuwenhuizen <janneke@gnu.org>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef BEAM_SCORING_PROBLEM_HH
22 #define BEAM_SCORING_PROBLEM_HH
23
24 #include "beam.hh"
25 #include "interval.hh"
26 #include "lily-guile.hh" 
27 #include "lily-proto.hh" 
28 #include "main.hh"  //  DEBUG_BEAM_SCORING
29 #include "std-vector.hh" 
30 #include "stem-info.hh" 
31
32 enum Scorers {
33   // Should be ordered by increasing expensiveness.
34   ORIGINAL_DISTANCE,
35   SLOPE_IDEAL,
36   SLOPE_MUSICAL,
37   SLOPE_DIRECTION,
38   HORIZONTAL_INTER,
39   FORBIDDEN,
40   STEM_LENGTHS,
41   COLLISIONS,
42   NUM_SCORERS,
43 };
44
45 struct Beam_configuration
46 {
47   Interval y;
48   Real demerits;
49 #if DEBUG_BEAM_SCORING
50   string score_card_;
51 #endif
52
53   int next_scorer_todo;
54
55   Beam_configuration ();
56   bool done () const;
57   void add (Real demerit, const string &reason);
58   static Beam_configuration* new_config(Interval start,
59                                         Interval offset);
60 };
61
62 // Comparator for a queue of Beam_configuration*.
63 class Beam_configuration_less
64 {
65 public:
66   bool operator() (Beam_configuration* const& l, Beam_configuration* const& r)
67   {
68     // Invert
69     return l->demerits > r->demerits;
70   }
71 };
72
73
74 struct Beam_quant_parameters
75 {
76   Real SECONDARY_BEAM_DEMERIT;
77   Real STEM_LENGTH_DEMERIT_FACTOR;
78   Real REGION_SIZE;
79
80   /*
81     threshold to combat rounding errors.
82   */
83   Real BEAM_EPS;
84
85   // possibly ridiculous, but too short stems just won't do
86   Real STEM_LENGTH_LIMIT_PENALTY;
87   Real DAMPING_DIRECTION_PENALTY;
88   Real MUSICAL_DIRECTION_FACTOR;
89   Real HINT_DIRECTION_PENALTY;
90   Real IDEAL_SLOPE_FACTOR;
91   Real ROUND_TO_ZERO_SLOPE;
92   Real COLLISION_PENALTY;
93   Real COLLISION_DISTANCE;
94   Real HORIZONTAL_INTER_QUANT_PENALTY;
95   Real STEM_COLLISION_FACTOR;
96   
97   void fill (Grob *him);
98 };
99
100 struct Beam_collision {
101   Real x_;
102   Interval y_;
103   Real base_penalty_;
104
105   // Need to add beam_config->y to get actual offsets.
106   Interval beam_y_;
107 };
108   
109
110 /*
111   Parameters for a single beam.  Precomputed to save time in
112   scoring individual configurations.
113
114   TODO - use trailing _ on data members.
115  
116   */
117 class Beam_scoring_problem
118 {
119 public:
120   Beam_scoring_problem (Grob *me, Drul_array<Real> ys);
121   Drul_array<Real> solve() const;
122
123 private:
124   Grob *beam;
125
126   Interval unquanted_y;
127   
128   Real staff_space;
129   Real beam_thickness;
130   Real line_thickness;
131   Real musical_dy;
132
133   Interval x_span;
134   
135   vector<Stem_info> stem_infos;
136
137   /*
138     Do stem computations.  These depend on YL and YR linearly, so we can
139     precompute for every stem 2 factors.
140
141     We store some info to quickly interpolate.  The stemlengths are
142     affine linear in YL and YR. If YL == YR == 0, then we might have
143     stem_y != 0.0, when we're cross staff.
144   */
145   vector<Real> base_lengths;
146   vector<Real> stem_xpositions;
147   
148   Grob *common[2];
149   bool is_xstaff;
150   bool is_knee;
151
152   Beam_quant_parameters parameters;
153
154   Real staff_radius;
155   Drul_array<int> edge_beam_counts;
156   Drul_array<Direction> edge_dirs;
157
158   // Half-open intervals, representing allowed positions for the beam,
159   // starting from close to the notehead to the direction of the stem
160   // end.  This is used for quickly weeding out invalid
161   // Beam_configurations.
162   Drul_array<Interval> quant_range;
163   Real beam_translation;
164   vector<Beam_collision> collisions_;
165   vector<Beam_segment> segments_;
166   
167   void init_stems ();
168   void init_collisions (vector<Grob*> grobs);
169   void add_collision (Real x, Interval y, Real factor);
170   
171   void one_scorer (Beam_configuration* config) const;
172   Beam_configuration *force_score (SCM inspect_quants,
173                                    const vector<Beam_configuration*> &configs) const;
174   Real y_at (Real x, Beam_configuration const* c) const;
175
176   // Scoring functions:
177   void score_forbidden_quants (Beam_configuration *config) const;
178   void score_horizontal_inter_quants (Beam_configuration *config) const;
179   void score_slope_ideal (Beam_configuration *config) const;
180   void score_slope_direction (Beam_configuration *config) const;
181   void score_slope_musical (Beam_configuration *config) const;
182   void score_stem_lengths (Beam_configuration* config) const;
183   void generate_quants(vector<Beam_configuration*>* scores) const;
184   void score_collisions (Beam_configuration *config) const;
185 };
186
187 #endif /* BEAM_SCORING_PROBLEM_HH */