Chris@147: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@147: Chris@147: /* Chris@147: Sonic Visualiser Chris@147: An audio file viewer and annotation editor. Chris@147: Centre for Digital Music, Queen Mary, University of London. Chris@147: This file copyright 2006 Chris Cannam. Chris@147: Chris@147: This program is free software; you can redistribute it and/or Chris@147: modify it under the terms of the GNU General Public License as Chris@147: published by the Free Software Foundation; either version 2 of the Chris@147: License, or (at your option) any later version. See the file Chris@147: COPYING included with this distribution for more information. Chris@147: */ Chris@147: Chris@147: #include "PowerOfSqrtTwoZoomConstraint.h" Chris@147: Chris@147: #include Chris@147: #include Chris@147: Chris@1528: #include "base/Debug.h" Chris@1528: Chris@147: Chris@1324: ZoomLevel Chris@1324: PowerOfSqrtTwoZoomConstraint::getNearestZoomLevel(ZoomLevel requested, Chris@1429: RoundingDirection dir) const Chris@147: { Chris@147: int type, power; Chris@1324: int blockSize; Chris@1324: Chris@1324: if (requested.zone == ZoomLevel::FramesPerPixel) { Chris@1324: blockSize = getNearestBlockSize(requested.level, type, power, dir); Chris@1324: return { requested.zone, blockSize }; Chris@1324: } else { Chris@1324: RoundingDirection opposite = dir; Chris@1324: if (dir == RoundUp) opposite = RoundDown; Chris@1324: else if (dir == RoundDown) opposite = RoundUp; Chris@1324: blockSize = getNearestBlockSize(requested.level, type, power, opposite); Chris@1324: if (blockSize > getMinZoomLevel().level) { Chris@1324: blockSize = getMinZoomLevel().level; Chris@1324: } Chris@1324: if (blockSize == 1) { Chris@1324: return { ZoomLevel::FramesPerPixel, 1 }; Chris@1324: } else { Chris@1324: return { requested.zone, blockSize }; Chris@1324: } Chris@1324: } Chris@147: } Chris@147: Chris@929: int Chris@929: PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(int blockSize, Chris@1429: int &type, Chris@1429: int &power, Chris@1429: RoundingDirection dir) const Chris@147: { Chris@1528: // SVCERR << "given " << blockSize << endl; Chris@147: Chris@929: int minCachePower = getMinCachePower(); Chris@147: Chris@929: if (blockSize < (1 << minCachePower)) { Chris@1429: type = -1; Chris@1429: power = 0; Chris@1429: float val = 1.0, prevVal = 1.0; Chris@1429: while (val + 0.01 < blockSize) { Chris@1429: prevVal = val; Chris@1429: val *= sqrtf(2.f); Chris@1429: } Chris@1528: int rval = int(val + 0.01f); Chris@1528: // SVCERR << "got val = " << val << ", rval = " << rval << ", prevVal = " << prevVal << endl; Chris@1528: if (rval != blockSize && dir != RoundUp) { Chris@1528: if (dir == RoundDown) { Chris@1528: rval = int(prevVal + 0.01f); Chris@1528: } else if (val - float(blockSize) < float(blockSize) - prevVal) { Chris@1528: rval = int(val + 0.01f); Chris@1528: } else { Chris@1528: rval = int(prevVal + 0.01); Chris@1528: } Chris@1528: } Chris@1528: // SVCERR << "returning " << rval << endl; Chris@1429: return rval; Chris@147: } Chris@147: Chris@929: int prevBase = (1 << minCachePower); Chris@929: int prevPower = minCachePower; Chris@929: int prevType = 0; Chris@147: Chris@929: int result = 0; Chris@147: Chris@147: for (unsigned int i = 0; ; ++i) { Chris@147: Chris@1429: power = minCachePower + i/2; Chris@1429: type = i % 2; Chris@147: Chris@1429: int base; Chris@1429: if (type == 0) { Chris@1429: base = (1 << power); Chris@1429: } else { Chris@1429: base = (((unsigned int)((1 << minCachePower) * sqrt(2.) + 0.01)) Chris@1429: << (power - minCachePower)); Chris@1429: } Chris@147: Chris@1528: // SVCERR << "Testing base " << base << " (i = " << i << ", power = " << power << ", type = " << type << ")" << endl; Chris@377: Chris@377: if (base == blockSize) { Chris@377: result = base; Chris@1552: // SVCERR << "Equal, accepting" << endl; Chris@377: break; Chris@377: } Chris@377: Chris@1429: if (base > blockSize) { Chris@1429: if (dir == RoundNearest) { Chris@1429: if (base - blockSize < blockSize - prevBase) { Chris@1429: dir = RoundUp; Chris@1552: // SVCERR << "Closer to " << base << " than " << prevBase Chris@1552: // << ", rounding up" << endl; Chris@1429: } else { Chris@1429: dir = RoundDown; Chris@1552: // SVCERR << "Closer to " << prevBase << " than " << base Chris@1552: // << ", rounding down" << endl; Chris@1429: } Chris@1429: } Chris@1429: if (dir == RoundUp) { Chris@1429: result = base; Chris@1429: break; Chris@1429: } else { Chris@1429: type = prevType; Chris@1429: power = prevPower; Chris@1429: result = prevBase; Chris@1429: break; Chris@1429: } Chris@1429: } Chris@147: Chris@1429: prevType = type; Chris@1429: prevPower = power; Chris@1429: prevBase = base; Chris@147: } Chris@147: Chris@1324: if (result > getMaxZoomLevel().level) { Chris@1324: result = getMaxZoomLevel().level; Chris@1324: } Chris@1324: Chris@1552: // SVCERR << "Returning result " << result << endl; Chris@1552: Chris@147: return result; Chris@147: }