]> git.donarmstrong.com Git - lilypond.git/blob - src/grouping.cc
e07de1254acf1e555c59d36ea469b5546521baf3
[lilypond.git] / src / grouping.cc
1 #include "grouping.hh"
2 #include "debug.hh"
3
4 Interval
5 vec_union(svec<Interval> notes)
6 {
7     Interval u;
8     u.set_empty();
9     for (int i =0 ; i < notes.sz() ; i++) {
10         u.unite(notes[i]);
11     }
12     return u;
13 }
14
15 svec<Real>
16 default_bounds(Interval t)
17 {
18     svec<Real> bounds;
19     Real dt = t.length();
20     bounds.add(t.min);
21     bounds.add(t.min + dt/2);    
22     return bounds;
23 }
24
25 svec<Real>
26 Rhythmic_grouping::get_bounds()
27 {
28     svec<Real> bounds;
29     if (children.sz()) {
30         for (int i=0; i < children.sz(); i++) {
31             bounds.add(children[i]->t.min);
32         }
33     } else {
34         default_bounds(t);
35     }
36 //      bounds.add(t.max );
37     return bounds;
38 }
39
40 Real
41 Rhythmic_grouping::last()
42 {
43     return t.max;
44 }
45
46 void
47 Rhythmic_grouping::split_grouping(svec<Real> bounds)
48 {
49     int lasti =0;
50     svec<Rhythmic_grouping*> newgrp;
51     for (int i=0, j = 1; i < children.sz() && j < bounds.sz(); ) {
52         if ( children[i]->t.max < bounds[j]) {
53             i ++;
54             continue;
55         } else if (children[i]->t.max > bounds[j]) {
56             j ++;
57             continue;
58         }
59
60         assert( children[lasti]->t.min == bounds[j-1] );
61         assert( children[i]->t.max == bounds[j] );
62
63         Rhythmic_grouping * n = new Rhythmic_grouping(Interval(
64             bounds[j-1], bounds[j]));
65         for (int k = lasti ; k < i; k++)
66             n->children.add(children[k]);
67         newgrp.add(n);
68
69         i = lasti = i+1;
70     }
71     if (newgrp.sz() <= 1) {
72         newgrp[0]->children.set_size(0);
73         delete newgrp[0];
74         return;
75     }
76     children = newgrp;    
77 }
78
79 void
80 Rhythmic_grouping::split_half()
81 {
82     svec<Real> bounds = default_bounds(t);
83     bounds.add(t.max);
84     split_grouping(bounds);
85     
86     for (int i=0; i < children.sz(); i++) {
87         if (children[i]->children.sz())
88             children[i]->split_half();
89     }
90 }
91
92 Rhythmic_grouping*
93 Rhythmic_grouping::sub_grouping(Interval v)
94 {
95     return 0;                   // todo!
96 }
97 void
98 Rhythmic_grouping::split_grouping(Rhythmic_grouping &initial_grouping)
99 {
100     svec<Rhythmic_grouping*> newgrp;
101     svec<Real> bounds = initial_grouping.get_bounds();
102     bounds.add(initial_grouping.last());
103     split_grouping(bounds);
104     for (int i=0; i < children.sz(); i++) {
105         Interval h = children[i]->t;
106         Rhythmic_grouping*r = initial_grouping.sub_grouping(h);
107         if (children[i]->children.sz()) {
108             if (r)
109                 children[i]->split_grouping(*r);
110             else
111                 children[i]->split_half();
112         }
113     }
114 }
115
116 Rhythmic_grouping::Rhythmic_grouping(Interval i)
117 {
118     t=i;
119 }
120
121 Rhythmic_grouping::Rhythmic_grouping(svec<Interval> notes,
122                                 svec<Real> initial_grouping)
123 {
124     t = vec_union(notes);
125     for (int i=0; i < notes.sz(); i++) {
126         children.add(new Rhythmic_grouping(notes[i]));
127     }
128     split_grouping(initial_grouping);
129 }
130
131 Rhythmic_grouping::~Rhythmic_grouping()
132 {
133     for (int i=0; i < children.sz(); i++) {
134         delete children[i];
135     }
136 }
137
138 void
139 Rhythmic_grouping::print()const    
140 {
141     mtor << "{ " << t << "\n";
142     for (int i=0; i < children.sz(); i++) {
143         children[i]->print();
144     }
145     mtor << "}";
146 }
147
148