1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "RelativelyFineZoomConstraint.h"
#include <vector>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
namespace sv {
ZoomLevel
RelativelyFineZoomConstraint::getNearestZoomLevel(ZoomLevel requested,
RoundingDirection dir) const
{
static vector<int> levels;
int maxLevel = getMaxZoomLevel().level;
if (levels.empty()) {
int level = 1;
while (level <= maxLevel) {
// cerr << level << " ";
levels.push_back(level);
int step = level / 10;
int pwr = 0;
while (step > 0) {
++pwr;
step /= 2;
}
step = (1 << pwr);
level += step;
}
// cerr << endl;
}
RoundingDirection effective = dir;
if (requested.zone == ZoomLevel::PixelsPerFrame) {
if (dir == RoundUp) effective = RoundDown;
else if (dir == RoundDown) effective = RoundUp;
}
// iterator pointing to first level that is >= requested
auto i = lower_bound(levels.begin(), levels.end(), requested.level);
ZoomLevel newLevel(requested);
if (i == levels.end()) {
newLevel.level = maxLevel;
} else if (*i == requested.level) {
newLevel.level = requested.level;
} else if (effective == RoundUp) {
newLevel.level = *i;
} else if (effective == RoundDown) {
if (i != levels.begin()) {
--i;
}
newLevel.level = *i;
} else { // RoundNearest
if (i != levels.begin()) {
auto j = i;
--j;
if (requested.level - *j < *i - requested.level) {
newLevel.level = *j;
} else {
newLevel.level = *i;
}
}
}
// canonicalise
if (newLevel.level == 1) {
newLevel.zone = ZoomLevel::FramesPerPixel;
}
using namespace std::rel_ops;
if (newLevel > getMaxZoomLevel()) {
newLevel = getMaxZoomLevel();
} else if (newLevel < getMinZoomLevel()) {
newLevel = getMinZoomLevel();
}
return newLevel;
}
} // end namespace sv
|