comparison data/model/PowerOfSqrtTwoZoomConstraint.cpp @ 1429:48e9f538e6e9

Untabify
author Chris Cannam
date Thu, 01 Mar 2018 18:02:22 +0000
parents cc27f35aa75c
children 710e6250a401 a7485c1bdba5
comparison
equal deleted inserted replaced
1428:87ae75da6527 1429:48e9f538e6e9
19 #include <cmath> 19 #include <cmath>
20 20
21 21
22 int 22 int
23 PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(int blockSize, 23 PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(int blockSize,
24 RoundingDirection dir) const 24 RoundingDirection dir) const
25 { 25 {
26 int type, power; 26 int type, power;
27 int rv = getNearestBlockSize(blockSize, type, power, dir); 27 int rv = getNearestBlockSize(blockSize, type, power, dir);
28 return rv; 28 return rv;
29 } 29 }
30 30
31 int 31 int
32 PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(int blockSize, 32 PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(int blockSize,
33 int &type, 33 int &type,
34 int &power, 34 int &power,
35 RoundingDirection dir) const 35 RoundingDirection dir) const
36 { 36 {
37 // cerr << "given " << blockSize << endl; 37 // cerr << "given " << blockSize << endl;
38 38
39 int minCachePower = getMinCachePower(); 39 int minCachePower = getMinCachePower();
40 40
41 if (blockSize < (1 << minCachePower)) { 41 if (blockSize < (1 << minCachePower)) {
42 type = -1; 42 type = -1;
43 power = 0; 43 power = 0;
44 float val = 1.0, prevVal = 1.0; 44 float val = 1.0, prevVal = 1.0;
45 while (val + 0.01 < blockSize) { 45 while (val + 0.01 < blockSize) {
46 prevVal = val; 46 prevVal = val;
47 val *= sqrtf(2.f); 47 val *= sqrtf(2.f);
48 } 48 }
49 int rval; 49 int rval;
50 if (dir == RoundUp) rval = int(val + 0.01f); 50 if (dir == RoundUp) rval = int(val + 0.01f);
51 else if (dir == RoundDown) rval = int(prevVal + 0.01f); 51 else if (dir == RoundDown) rval = int(prevVal + 0.01f);
52 else if (val - float(blockSize) < 52 else if (val - float(blockSize) <
53 float(blockSize) - prevVal) rval = int(val + 0.01f); 53 float(blockSize) - prevVal) rval = int(val + 0.01f);
54 else rval = int(prevVal + 0.01); 54 else rval = int(prevVal + 0.01);
55 // SVDEBUG << "returning " << rval << endl; 55 // SVDEBUG << "returning " << rval << endl;
56 return rval; 56 return rval;
57 } 57 }
58 58
59 int prevBase = (1 << minCachePower); 59 int prevBase = (1 << minCachePower);
60 int prevPower = minCachePower; 60 int prevPower = minCachePower;
61 int prevType = 0; 61 int prevType = 0;
62 62
63 int result = 0; 63 int result = 0;
64 64
65 for (unsigned int i = 0; ; ++i) { 65 for (unsigned int i = 0; ; ++i) {
66 66
67 power = minCachePower + i/2; 67 power = minCachePower + i/2;
68 type = i % 2; 68 type = i % 2;
69 69
70 int base; 70 int base;
71 if (type == 0) { 71 if (type == 0) {
72 base = (1 << power); 72 base = (1 << power);
73 } else { 73 } else {
74 base = (((unsigned int)((1 << minCachePower) * sqrt(2.) + 0.01)) 74 base = (((unsigned int)((1 << minCachePower) * sqrt(2.) + 0.01))
75 << (power - minCachePower)); 75 << (power - minCachePower));
76 } 76 }
77 77
78 // SVDEBUG << "Testing base " << base << endl; 78 // SVDEBUG << "Testing base " << base << endl;
79 79
80 if (base == blockSize) { 80 if (base == blockSize) {
81 result = base; 81 result = base;
82 break; 82 break;
83 } 83 }
84 84
85 if (base > blockSize) { 85 if (base > blockSize) {
86 if (dir == RoundNearest) { 86 if (dir == RoundNearest) {
87 if (base - blockSize < blockSize - prevBase) { 87 if (base - blockSize < blockSize - prevBase) {
88 dir = RoundUp; 88 dir = RoundUp;
89 } else { 89 } else {
90 dir = RoundDown; 90 dir = RoundDown;
91 } 91 }
92 } 92 }
93 if (dir == RoundUp) { 93 if (dir == RoundUp) {
94 result = base; 94 result = base;
95 break; 95 break;
96 } else { 96 } else {
97 type = prevType; 97 type = prevType;
98 power = prevPower; 98 power = prevPower;
99 result = prevBase; 99 result = prevBase;
100 break; 100 break;
101 } 101 }
102 } 102 }
103 103
104 prevType = type; 104 prevType = type;
105 prevPower = power; 105 prevPower = power;
106 prevBase = base; 106 prevBase = base;
107 } 107 }
108 108
109 if (result > getMaxZoomLevel()) result = getMaxZoomLevel(); 109 if (result > getMaxZoomLevel()) result = getMaxZoomLevel();
110 return result; 110 return result;
111 } 111 }