]> git.donarmstrong.com Git - lilypond.git/blob - lily/misc.cc
patch::: 1.3.11.hwn1
[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 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
36 /*
37   TODO
38     group in some Array_*
39     make more generic / templatise
40  */
41 int
42 get_lower_bound (Array<Real> const& positions, Real x)
43 {
44   if (x < positions[0])
45     return 0;
46   for (int i = 1; i < positions.size (); i++)
47     if (x < positions[i])
48       return i - 1;
49   return positions.size () - 1;
50 }
51
52 Slice
53 get_bounds_slice (Array<Real> const& positions, Real x)
54 {
55   int l = get_lower_bound (positions, x);
56   int u = positions.size () - 1 <? l + 1;
57   if (x < positions[l])
58     u = l;
59   return Slice (l, u);
60 }
61
62 Interval
63 get_bounds_iv (Array<Real> const& positions, Real x)
64 {
65   Slice slice = get_bounds_slice (positions, x);
66   return Interval (positions[slice[SMALLER]], positions[slice[BIGGER]]);
67 }
68
69 // silly name
70 Interval
71 quantise_iv (Array<Real> const& positions, Real period, Real x)
72 {
73   /*
74     ugh
75     assume that 
76       * positions are sorted, 
77       * positions are nonnegative
78       * period starts at zero
79    */
80
81   int n = (int)(x / period);
82   Real frac = (x / period - n) * period;
83   if (frac < 0)
84     {
85       frac += period;
86       n--;
87     }
88
89   Slice slice = get_bounds_slice (positions, frac);
90   Interval iv(positions[slice[SMALLER]], positions[slice[BIGGER]]);
91
92   if (slice[SMALLER] == slice[BIGGER])
93     {
94       if (slice[SMALLER] == 0)
95         iv[SMALLER] = - period + positions.top ();
96       else
97         iv[BIGGER] = period + positions[0];
98     }
99
100   iv += period * n;
101
102   return iv;
103 }