comparison data/model/PowerOfSqrtTwoZoomConstraint.cpp @ 1528:a7485c1bdba5

Tests and a couple of minor fixes for zoom constraints
author Chris Cannam
date Tue, 18 Sep 2018 15:04:46 +0100
parents 48e9f538e6e9
children c1c45c5146bb
comparison
equal deleted inserted replaced
1526:8988b27ebf38 1528:a7485c1bdba5
16 #include "PowerOfSqrtTwoZoomConstraint.h" 16 #include "PowerOfSqrtTwoZoomConstraint.h"
17 17
18 #include <iostream> 18 #include <iostream>
19 #include <cmath> 19 #include <cmath>
20 20
21 #include "base/Debug.h"
22
21 23
22 int 24 int
23 PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(int blockSize, 25 PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(int blockSize,
24 RoundingDirection dir) const 26 RoundingDirection dir) const
25 { 27 {
32 PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(int blockSize, 34 PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(int blockSize,
33 int &type, 35 int &type,
34 int &power, 36 int &power,
35 RoundingDirection dir) const 37 RoundingDirection dir) const
36 { 38 {
37 // cerr << "given " << blockSize << endl; 39 // SVCERR << "given " << blockSize << endl;
38 40
39 int minCachePower = getMinCachePower(); 41 int minCachePower = getMinCachePower();
40 42
41 if (blockSize < (1 << minCachePower)) { 43 if (blockSize < (1 << minCachePower)) {
42 type = -1; 44 type = -1;
44 float val = 1.0, prevVal = 1.0; 46 float val = 1.0, prevVal = 1.0;
45 while (val + 0.01 < blockSize) { 47 while (val + 0.01 < blockSize) {
46 prevVal = val; 48 prevVal = val;
47 val *= sqrtf(2.f); 49 val *= sqrtf(2.f);
48 } 50 }
49 int rval; 51 int rval = int(val + 0.01f);
50 if (dir == RoundUp) rval = int(val + 0.01f); 52 // SVCERR << "got val = " << val << ", rval = " << rval << ", prevVal = " << prevVal << endl;
51 else if (dir == RoundDown) rval = int(prevVal + 0.01f); 53 if (rval != blockSize && dir != RoundUp) {
52 else if (val - float(blockSize) < 54 if (dir == RoundDown) {
53 float(blockSize) - prevVal) rval = int(val + 0.01f); 55 rval = int(prevVal + 0.01f);
54 else rval = int(prevVal + 0.01); 56 } else if (val - float(blockSize) < float(blockSize) - prevVal) {
55 // SVDEBUG << "returning " << rval << endl; 57 rval = int(val + 0.01f);
58 } else {
59 rval = int(prevVal + 0.01);
60 }
61 }
62 // SVCERR << "returning " << rval << endl;
56 return rval; 63 return rval;
57 } 64 }
58 65
59 int prevBase = (1 << minCachePower); 66 int prevBase = (1 << minCachePower);
60 int prevPower = minCachePower; 67 int prevPower = minCachePower;
73 } else { 80 } else {
74 base = (((unsigned int)((1 << minCachePower) * sqrt(2.) + 0.01)) 81 base = (((unsigned int)((1 << minCachePower) * sqrt(2.) + 0.01))
75 << (power - minCachePower)); 82 << (power - minCachePower));
76 } 83 }
77 84
78 // SVDEBUG << "Testing base " << base << endl; 85 // SVCERR << "Testing base " << base << " (i = " << i << ", power = " << power << ", type = " << type << ")" << endl;
79 86
80 if (base == blockSize) { 87 if (base == blockSize) {
81 result = base; 88 result = base;
82 break; 89 break;
83 } 90 }