Mercurial > hg > svcore
comparison data/model/PowerOfSqrtTwoZoomConstraint.cpp @ 147:3a13b0d4934e
* Reorganising code base. This revision will not compile.
author | Chris Cannam |
---|---|
date | Mon, 31 Jul 2006 11:44:37 +0000 |
parents | |
children | 166c22eff678 |
comparison
equal
deleted
inserted
replaced
146:f90fad823cea | 147:3a13b0d4934e |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Sonic Visualiser | |
5 An audio file viewer and annotation editor. | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 This file copyright 2006 Chris Cannam. | |
8 | |
9 This program is free software; you can redistribute it and/or | |
10 modify it under the terms of the GNU General Public License as | |
11 published by the Free Software Foundation; either version 2 of the | |
12 License, or (at your option) any later version. See the file | |
13 COPYING included with this distribution for more information. | |
14 */ | |
15 | |
16 #include "PowerOfSqrtTwoZoomConstraint.h" | |
17 | |
18 #include <iostream> | |
19 #include <cmath> | |
20 | |
21 | |
22 size_t | |
23 PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(size_t blockSize, | |
24 RoundingDirection dir) const | |
25 { | |
26 int type, power; | |
27 size_t rv = getNearestBlockSize(blockSize, type, power, dir); | |
28 return rv; | |
29 } | |
30 | |
31 size_t | |
32 PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(size_t blockSize, | |
33 int &type, | |
34 int &power, | |
35 RoundingDirection dir) const | |
36 { | |
37 // std::cerr << "given " << blockSize << std::endl; | |
38 | |
39 size_t minCachePower = getMinCachePower(); | |
40 | |
41 if (blockSize < (1U << minCachePower)) { | |
42 type = -1; | |
43 power = 0; | |
44 float val = 1.0, prevVal = 1.0; | |
45 while (val + 0.01 < blockSize) { | |
46 prevVal = val; | |
47 val *= sqrt(2); | |
48 } | |
49 size_t rval; | |
50 if (dir == RoundUp) rval = size_t(val + 0.01); | |
51 else if (dir == RoundDown) rval = size_t(prevVal + 0.01); | |
52 else if (val - blockSize < blockSize - prevVal) rval = size_t(val + 0.01); | |
53 else rval = size_t(prevVal + 0.01); | |
54 // std::cerr << "returning " << rval << std::endl; | |
55 return rval; | |
56 } | |
57 | |
58 unsigned int prevBase = (1 << minCachePower); | |
59 unsigned int prevPower = minCachePower; | |
60 unsigned int prevType = 0; | |
61 | |
62 size_t result = 0; | |
63 | |
64 for (unsigned int i = 0; ; ++i) { | |
65 | |
66 power = minCachePower + i/2; | |
67 type = i % 2; | |
68 | |
69 unsigned int base; | |
70 if (type == 0) { | |
71 base = (1 << power); | |
72 } else { | |
73 base = (((unsigned int)((1 << minCachePower) * sqrt(2) + 0.01)) | |
74 << (power - minCachePower)); | |
75 } | |
76 | |
77 // std::cerr << "Testing base " << base << std::endl; | |
78 if (base >= blockSize) { | |
79 if (dir == RoundNearest) { | |
80 if (base - blockSize < blockSize - prevBase) { | |
81 dir = RoundUp; | |
82 } else { | |
83 dir = RoundDown; | |
84 } | |
85 } | |
86 if (dir == RoundUp) { | |
87 result = base; | |
88 break; | |
89 } else { | |
90 type = prevType; | |
91 power = prevPower; | |
92 result = prevBase; | |
93 break; | |
94 } | |
95 } | |
96 | |
97 prevType = type; | |
98 prevPower = power; | |
99 prevBase = base; | |
100 } | |
101 | |
102 if (result > getMaxZoomLevel()) result = getMaxZoomLevel(); | |
103 return result; | |
104 } |