]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/beam-scoring-problem.hh
755fb388f2b8d6ba3dac2ce24547861e7a3c2c20
[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 "interval.hh"
25 #include "lily-proto.hh" 
26 #include "lily-guile.hh" 
27 #include "std-vector.hh" 
28 #include "stem-info.hh" 
29 #include "main.hh"  //  DEBUG_BEAM_SCORING
30
31 enum Scorers {
32   // Should be ordered by increasing expensiveness.
33   ORIGINAL_DISTANCE,
34   SLOPES,
35   FORBIDDEN,
36   STEM_LENGTHS,
37   NUM_SCORERS,
38 };
39
40 struct Beam_configuration
41 {
42   Interval y;
43   Real demerits;
44 #if DEBUG_BEAM_SCORING
45   string score_card_;
46 #endif
47
48   int next_scorer_todo;
49
50   Beam_configuration ();
51   bool done () const;
52   void add (Real demerit, const string &reason);
53   static Beam_configuration* new_config(Interval start,
54                                         Interval offset);
55 };
56
57 // Comparator for a queue of Beam_configuration*.
58 class Beam_configuration_less
59 {
60 public:
61   bool operator() (Beam_configuration* const& l, Beam_configuration* const& r)
62   {
63     // Invert
64     return l->demerits > r->demerits;
65   }
66 };
67
68
69 struct Beam_quant_parameters
70 {
71   Real SECONDARY_BEAM_DEMERIT;
72   Real STEM_LENGTH_DEMERIT_FACTOR;
73   Real REGION_SIZE;
74
75   /*
76     threshold to combat rounding errors.
77   */
78   Real BEAM_EPS;
79
80   // possibly ridiculous, but too short stems just won't do
81   Real STEM_LENGTH_LIMIT_PENALTY;
82   Real DAMPING_DIRECTION_PENALTY;
83   Real MUSICAL_DIRECTION_FACTOR;
84   Real HINT_DIRECTION_PENALTY;
85   Real IDEAL_SLOPE_FACTOR;
86   Real ROUND_TO_ZERO_SLOPE;
87
88   void fill (Grob *him);
89 };
90
91
92
93 /*
94   Parameters for a single beam.  Precomputed to save time in
95   scoring individual configurations.
96
97   TODO - use trailing _ on data members.
98  
99   */
100 class Beam_scoring_problem
101 {
102 public:
103   Beam_scoring_problem (Grob *me, Drul_array<Real> ys);
104   Drul_array<Real> solve() const;
105
106 private:
107   Grob *beam;
108
109   Interval unquanted_y;
110   
111   Real staff_space;
112   Real beam_thickness;
113   Real line_thickness;
114   Real musical_dy;
115
116   Interval x_span;
117   
118   vector<Stem_info> stem_infos;
119
120   /*
121     Do stem computations.  These depend on YL and YR linearly, so we can
122     precompute for every stem 2 factors.
123
124     We store some info to quickly interpolate.  The stemlengths are
125     affine linear in YL and YR. If YL == YR == 0, then we might have
126     stem_y != 0.0, when we're cross staff.
127   */
128   vector<Real> base_lengths;
129   vector<Real> stem_xpositions;
130   
131   Grob *common[2];
132   bool is_xstaff;
133   bool is_knee;
134
135   Beam_quant_parameters parameters;
136
137   Real staff_radius;
138   Drul_array<int> edge_beam_counts;
139   Drul_array<Direction> edge_dirs;
140   Real beam_translation;
141
142   void init_stems ();
143
144   void one_scorer (Beam_configuration* config) const;
145   Beam_configuration *force_score (SCM inspect_quants,
146                                    const vector<Beam_configuration*> &configs) const;
147
148   // Scoring functions:
149   void score_forbidden_quants (Beam_configuration *config) const;
150   void score_slopes_dy (Beam_configuration *config) const;
151   void score_stem_lengths (Beam_configuration* config) const;
152   void generate_quants(vector<Beam_configuration*>* scores) const;
153 };
154
155 #endif /* BEAM_SCORING_PROBLEM_HH */