]> git.donarmstrong.com Git - lilypond.git/blob - lily/misc.cc
release: 1.1.29
[lilypond.git] / lily / misc.cc
1 /*
2   misc.cc -- implement various stuff
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--1999, 1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7     Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include <math.h>
11
12 #include "misc.hh"
13
14 #ifndef STANDALONE
15 #include "item.hh"
16 #endif
17
18 int
19 intlog2(int d) {
20   int i=0;
21   while (!(d&1)) 
22     {
23         d/= 2;
24         i++;
25     }
26   assert (!(d/2));
27   return i;
28 }
29
30 double
31 log_2(double x) {
32   return log (x)  /log (2.0);
33 }
34
35 #ifndef STANDALONE
36 Interval
37 itemlist_width (const Array<Item*> &its)
38 {
39   Interval iv ;
40   iv.set_empty();
41    
42   for (int j =0; j < its.size(); j++)
43     {
44         iv.unite (its[j]->extent (X_AXIS));
45
46     }
47   return iv;
48 }
49
50 #endif
51
52
53 /*
54   TODO
55     group in some Array_*
56     make more generic / templatise
57  */
58 int
59 get_lower_bound (Array<Real> const& positions, Real x)
60 {
61   if (x < positions[0])
62     return 0;
63   for (int i = 1; i < positions.size (); i++)
64     if (x < positions[i])
65       return i - 1;
66   return positions.size () - 1;
67 }
68
69 Slice
70 get_bounds_slice (Array<Real> const& positions, Real x)
71 {
72   int l = get_lower_bound (positions, x);
73   int u = positions.size () - 1 <? l + 1;
74   if (x < positions[l])
75     u = l;
76   return Slice (l, u);
77 }
78
79 Interval
80 get_bounds_iv (Array<Real> const& positions, Real x)
81 {
82   Slice slice = get_bounds_slice (positions, x);
83   return Interval (positions[slice.min ()], positions[slice.max ()]);
84 }
85
86 // silly name
87 Interval
88 quantise_iv (Array<Real> const& positions, Real period, Real x)
89 {
90   /*
91     ugh
92     assume that 
93       * positions are sorted, 
94       * positions are nonnegative
95       * period starts at zero
96    */
97
98   int n = (int)(x / period);
99   Real frac = (x / period - n) * period;
100   if (frac < 0)
101     {
102       frac += period;
103       n--;
104     }
105
106   Slice slice = get_bounds_slice (positions, frac);
107   Interval iv(positions[slice.min ()], positions[slice.max ()]);
108
109   if (slice.min () == slice.max ())
110     {
111       if (slice.min () == 0)
112         iv.min () = - period + positions.top ();
113       else
114         iv.max () = period + positions[0];
115     }
116
117   iv += period * n;
118
119   return iv;
120 }