]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/linespace.hh
release: 0.0.42.pre3
[lilypond.git] / lily / include / linespace.hh
1 #ifndef PROBLEM_HH
2 #define PROBLEM_HH
3
4 #include "glob.hh"
5 #include "plist.hh"
6 #include "varray.hh"
7 #include "vector.hh"
8 #include "interval.hh"
9
10 /// helper struct for #Spacing_problem#
11 struct Colinfo {
12     PCol const *pcol_;
13     Real const * fixpos;
14     Interval width;
15     
16     /* *************** */
17     Colinfo();
18     void operator=(Colinfo const&);
19     Colinfo(Colinfo const&);
20     ~Colinfo();
21     Colinfo(PCol const *,Real const *);
22     void print() const;
23     bool fixed() const { return fixpos;}
24     Real fixed_position()const { return *fixpos; }
25     Real minright() const { return width.right; }
26     Real minleft() const { return -width.left; }
27 };
28
29
30 /** the problem, given by the columns (which include constraints) and
31     intercolumn spacing. The problem is:
32
33     Generate a spacing which
34     \begin{itemize}
35     \item
36     Satisfies spacing constraints (notes can't be printed through each other)
37     \item
38     Looks good, ie tries to conform to  an ideal spacing as much as possible.
39     \end{itemize}
40     This is converted by regarding idealspacing as "springs" attached
41     to columns. The equilibrium of one spring is the ideal
42     distance. The columns have a size, this imposes "hard" constraints
43     on the distances. This transforms the problem into a quadratic
44     programming problem with linear constraints.
45
46     The quality is given by the total potential energy in the
47     springs. The lower the energy, the better the configuration.
48 */
49 class Spacing_problem {
50     Array<Idealspacing const *> ideals;
51     Array<Colinfo> cols;
52
53     /// the index of #c# in #cols#
54     int col_id(PCol const *c) const;
55
56     /// generate an (nonoptimal) solution
57     Vector find_initial_solution() const;
58
59     /// check if problem is too tight
60     bool check_feasible() const;
61
62     /// does #this# contain the column #w#? 
63     bool contains(PCol const *w);
64
65     /// make the energy function
66     void make_matrices(Matrix &quad, Vector &lin,Real&) const;
67
68     /// generate the LP constraints
69     void make_constraints(Mixed_qp& lp) const;
70
71 public:
72     /** solve the spacing problem
73       
74       @return the column positions, and the energy (last element)
75
76       */
77     Array<Real> solve() const;
78
79     
80     /**
81        add a idealspacing to the problem.
82       
83     One pair of columns can have no, one or more idealspacings,
84     since they can be "summed" if the columns to which #i# refers are
85     not in this problem, the spacing is ignored.
86     */
87     void add_ideal(Idealspacing const *i);
88     
89     
90     /** add a col to the problem. columns have to be added left to right. The column contains
91       info on it's minimum width.
92     */
93     void add_column(PCol const *, bool fixed=false, Real fixpos=0.0);
94  
95
96
97     bool check_constraints(Vector v) const;
98
99     Vector try_initial_solution() const;
100     void OK() const;
101     void print() const;
102     void print_ideal(Idealspacing const *)const;
103 };
104
105
106 #endif