]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/beam-scoring-problem.hh
Run grand-replace (issue 3765)
[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--2014 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 {
34   // Should be ordered by increasing expensiveness.
35   ORIGINAL_DISTANCE,
36   SLOPE_IDEAL,
37   SLOPE_MUSICAL,
38   SLOPE_DIRECTION,
39   HORIZONTAL_INTER,
40   FORBIDDEN,
41   STEM_LENGTHS,
42   COLLISIONS,
43   NUM_SCORERS,
44 };
45
46 struct Beam_configuration
47 {
48   Interval y;
49   Real demerits;
50 #if DEBUG_BEAM_SCORING
51   string score_card_;
52 #endif
53
54   int next_scorer_todo;
55
56   Beam_configuration ();
57   bool done () const;
58   void add (Real demerit, const string &reason);
59   static Beam_configuration *new_config (Interval start,
60                                          Interval offset);
61 };
62
63 // Comparator for a queue of Beam_configuration*.
64 class Beam_configuration_less
65 {
66 public:
67   bool operator () (Beam_configuration *const &l, Beam_configuration *const &r)
68   {
69     // Invert
70     return l->demerits > r->demerits;
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_PADDING;
94   Real HORIZONTAL_INTER_QUANT_PENALTY;
95   Real STEM_COLLISION_FACTOR;
96
97   void fill (Grob *him);
98 };
99
100 struct Beam_collision
101 {
102   Real x_;
103   Interval y_;
104   Real base_penalty_;
105
106   // Need to add beam_config->y to get actual offsets.
107   Interval beam_y_;
108 };
109
110 /*
111   Parameters for a single beam.  Precomputed to save time in
112   scoring individual configurations.
113
114   */
115 class Beam_scoring_problem
116 {
117 public:
118   Beam_scoring_problem (Grob *me, Drul_array<Real> ys, bool);
119   Drul_array<Real> solve () const;
120
121 private:
122   Spanner *beam_;
123
124   Interval unquanted_y_;
125   bool align_broken_intos_;
126   bool do_initial_slope_calculations_;
127
128   Real staff_space_;
129   Real beam_thickness_;
130   Real line_thickness_;
131   Real musical_dy_;
132   int normal_stem_count_;
133   Real x_span_;
134
135   /*
136     Do stem computations.  These depend on YL and YR linearly, so we can
137     precompute for every stem 2 factors.
138
139     We store some info to quickly interpolate.  The stemlengths are
140     affine linear in YL and YR. If YL == YR == 0, then we might have
141     stem_y != 0.0, when we're cross staff.
142   */
143   vector<Stem_info> stem_infos_;
144   vector<Real> chord_start_y_;
145   vector<Interval> head_positions_;
146   vector<Slice> beam_multiplicity_;
147   vector<bool> is_normal_;
148   vector<Real> base_lengths_;
149   vector<Real> stem_xpositions_;
150   vector<Real> stem_ypositions_;
151
152   bool is_xstaff_;
153   bool is_knee_;
154
155   Beam_quant_parameters parameters_;
156
157   Real staff_radius_;
158   Drul_array<int> edge_beam_counts_;
159   Drul_array<Direction> edge_dirs_;
160
161   // Half-open intervals, representing allowed positions for the beam,
162   // starting from close to the notehead to the direction of the stem
163   // end.  This is used for quickly weeding out invalid
164   // Beam_configurations.
165   Drul_array<Interval> quant_range_;
166   Real beam_translation_;
167   vector<Beam_collision> collisions_;
168   vector<Beam_segment> segments_;
169
170   vsize first_normal_index ();
171   vsize last_normal_index ();
172
173   void init_instance_variables (Grob *me, Drul_array<Real> ys, bool align_broken_intos);
174   void add_collision (Real x, Interval y, Real factor);
175   void no_visible_stem_positions ();
176   void least_squares_positions ();
177   Real calc_concaveness ();
178   void slope_damping ();
179   void shift_region_to_valid ();
180
181   void one_scorer (Beam_configuration *config) const;
182   Beam_configuration *force_score (SCM inspect_quants,
183                                    const vector<Beam_configuration *> &configs) const;
184   Real y_at (Real x, Beam_configuration const *c) const;
185
186   // Scoring functions:
187   void score_forbidden_quants (Beam_configuration *config) const;
188   void score_horizontal_inter_quants (Beam_configuration *config) const;
189   void score_slope_ideal (Beam_configuration *config) const;
190   void score_slope_direction (Beam_configuration *config) const;
191   void score_slope_musical (Beam_configuration *config) const;
192   void score_stem_lengths (Beam_configuration *config) const;
193   void generate_quants (vector<Beam_configuration *> *scores) const;
194   void score_collisions (Beam_configuration *config) const;
195 };
196
197 #endif /* BEAM_SCORING_PROBLEM_HH */