annotate base/ScaleTickIntervals.h @ 1417:359147a50853 scale-ticks

We don't need to return the tick instruction gubbins
author Chris Cannam
date Thu, 04 May 2017 13:32:42 +0100
parents 12316a9bcc8f
children e7cb4fb2aee4
rev   line source
Chris@1407 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1407 2
Chris@1407 3 /*
Chris@1407 4 Sonic Visualiser
Chris@1407 5 An audio file viewer and annotation editor.
Chris@1407 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1407 7 This file copyright 2006-2017 Chris Cannam and QMUL.
Chris@1407 8
Chris@1407 9 This program is free software; you can redistribute it and/or
Chris@1407 10 modify it under the terms of the GNU General Public License as
Chris@1407 11 published by the Free Software Foundation; either version 2 of the
Chris@1407 12 License, or (at your option) any later version. See the file
Chris@1407 13 COPYING included with this distribution for more information.
Chris@1407 14 */
Chris@1407 15
Chris@1407 16 #ifndef SV_SCALE_TICK_INTERVALS_H
Chris@1407 17 #define SV_SCALE_TICK_INTERVALS_H
Chris@1407 18
Chris@1407 19 #include <string>
Chris@1407 20 #include <vector>
Chris@1407 21 #include <cmath>
Chris@1407 22
Chris@1417 23 #define DEBUG_SCALE_TICK_INTERVALS 1
Chris@1411 24
Chris@1411 25 #ifdef DEBUG_SCALE_TICK_INTERVALS
Chris@1407 26 #include <iostream>
Chris@1411 27 #endif
Chris@1407 28
Chris@1414 29 #include "LogRange.h"
Chris@1414 30
Chris@1407 31 class ScaleTickIntervals
Chris@1407 32 {
Chris@1407 33 public:
Chris@1407 34 struct Range {
Chris@1407 35 double min; // start of value range
Chris@1407 36 double max; // end of value range
Chris@1412 37 int n; // number of divisions (approximate only)
Chris@1407 38 };
Chris@1407 39
Chris@1407 40 struct Tick {
Chris@1407 41 double value; // value this tick represents
Chris@1407 42 std::string label; // value as written
Chris@1407 43 };
Chris@1407 44
Chris@1417 45 typedef std::vector<Tick> Ticks;
Chris@1407 46
Chris@1407 47 static Ticks linear(Range r) {
Chris@1414 48 return linearTicks(r);
Chris@1414 49 }
Chris@1407 50
Chris@1414 51 static Ticks logarithmic(Range r) {
Chris@1414 52 return logTicks(r);
Chris@1414 53 }
Chris@1414 54
Chris@1414 55 private:
Chris@1417 56 struct Instruction {
Chris@1417 57 double initial; // value of first tick
Chris@1417 58 double limit; // max from original range
Chris@1417 59 double spacing; // increment between ticks
Chris@1417 60 double roundTo; // what all displayed values should be rounded to
Chris@1417 61 bool fixed; // whether to use fixed precision (%f rather than %e)
Chris@1417 62 int precision; // number of dp (%f) or sf (%e)
Chris@1417 63 bool logUnmap; // true if values represent logs of display values
Chris@1417 64 };
Chris@1417 65
Chris@1417 66 static Instruction linearInstruction(Range r)
Chris@1414 67 {
Chris@1407 68 if (r.n < 1) {
Chris@1407 69 return {};
Chris@1407 70 }
Chris@1407 71 if (r.max < r.min) {
Chris@1417 72 return linearInstruction({ r.max, r.min, r.n });
Chris@1407 73 }
Chris@1407 74
Chris@1407 75 double inc = (r.max - r.min) / r.n;
Chris@1407 76 if (inc == 0) {
Chris@1411 77 #ifdef DEBUG_SCALE_TICK_INTERVALS
Chris@1411 78 std::cerr << "inc == 0, using trivial range" << std::endl;
Chris@1411 79 #endif
Chris@1411 80 double roundTo = r.min;
Chris@1411 81 if (roundTo <= 0.0) {
Chris@1411 82 roundTo = 1.0;
Chris@1411 83 }
Chris@1417 84 return { r.min, r.max, 1.0, roundTo, true, 1, false };
Chris@1407 85 }
Chris@1408 86
Chris@1408 87 double digInc = log10(inc);
Chris@1408 88 double digMax = log10(fabs(r.max));
Chris@1408 89 double digMin = log10(fabs(r.min));
Chris@1408 90
Chris@1409 91 int precInc = int(trunc(digInc));
Chris@1409 92 if (double(precInc) != digInc) {
Chris@1409 93 precInc -= 1;
Chris@1409 94 }
Chris@1408 95
Chris@1408 96 bool fixed = false;
Chris@1408 97 if (precInc > -4 && precInc < 4) {
Chris@1408 98 fixed = true;
Chris@1408 99 } else if ((digMax >= -3.0 && digMax <= 2.0) &&
Chris@1408 100 (digMin >= -3.0 && digMin <= 3.0)) {
Chris@1408 101 fixed = true;
Chris@1408 102 }
Chris@1408 103
Chris@1408 104 int precRange = int(ceil(digMax - digInc));
Chris@1408 105
Chris@1408 106 int prec = 1;
Chris@1408 107
Chris@1408 108 if (fixed) {
Chris@1415 109 if (digInc < 0) {
Chris@1410 110 prec = -precInc;
Chris@1415 111 } else {
Chris@1410 112 prec = 0;
Chris@1408 113 }
Chris@1408 114 } else {
Chris@1408 115 prec = precRange;
Chris@1408 116 }
Chris@1408 117
Chris@1411 118 double roundTo = pow(10.0, precInc);
Chris@1411 119
Chris@1411 120 #ifdef DEBUG_SCALE_TICK_INTERVALS
Chris@1408 121 std::cerr << "\nmin = " << r.min << ", max = " << r.max << ", n = " << r.n
Chris@1408 122 << ", inc = " << inc << std::endl;
Chris@1408 123 std::cerr << "digMax = " << digMax << ", digInc = " << digInc
Chris@1408 124 << std::endl;
Chris@1408 125 std::cerr << "fixed = " << fixed << ", inc = " << inc
Chris@1408 126 << ", precInc = " << precInc << ", precRange = " << precRange
Chris@1408 127 << ", prec = " << prec << std::endl;
Chris@1408 128 std::cerr << "roundTo = " << roundTo << std::endl;
Chris@1411 129 #endif
Chris@1408 130
Chris@1407 131 inc = round(inc / roundTo) * roundTo;
Chris@1408 132 if (inc < roundTo) inc = roundTo;
Chris@1408 133
Chris@1407 134 double min = ceil(r.min / roundTo) * roundTo;
Chris@1407 135 if (min > r.max) min = r.max;
Chris@1407 136
Chris@1413 137 if (!fixed && min != 0.0) {
Chris@1413 138 double digNewMin = log10(fabs(min));
Chris@1413 139 if (digNewMin < digInc) {
Chris@1413 140 prec = int(ceil(digMax - digNewMin));
Chris@1413 141 #ifdef DEBUG_SCALE_TICK_INTERVALS
Chris@1413 142 std::cerr << "min is smaller than increment, adjusting prec to "
Chris@1413 143 << prec << std::endl;
Chris@1413 144 #endif
Chris@1413 145 }
Chris@1413 146 }
Chris@1413 147
Chris@1417 148 return { min, r.max, inc, roundTo, fixed, prec, false };
Chris@1407 149 }
Chris@1407 150
Chris@1414 151 static Ticks linearTicks(Range r) {
Chris@1417 152 Instruction instruction = linearInstruction(r);
Chris@1417 153 Ticks ticks = explode(instruction);
Chris@1417 154 return ticks;
Chris@1414 155 }
Chris@1414 156
Chris@1414 157 static Ticks logTicks(Range r) {
Chris@1414 158 Range mapped(r);
Chris@1414 159 LogRange::mapRange(mapped.min, mapped.max);
Chris@1417 160 Instruction instruction = linearInstruction(mapped);
Chris@1417 161 instruction.logUnmap = true;
Chris@1414 162 if (fabs(mapped.min - mapped.max) > 3) {
Chris@1417 163 instruction.fixed = false;
Chris@1414 164 }
Chris@1417 165 Ticks ticks = explode(instruction);
Chris@1417 166 return ticks;
Chris@1414 167 }
Chris@1414 168
Chris@1414 169 static Tick makeTick(bool fixed, int precision, double value) {
Chris@1414 170 const int buflen = 40;
Chris@1414 171 char buffer[buflen];
Chris@1414 172 snprintf(buffer, buflen,
Chris@1414 173 fixed ? "%.*f" : "%.*e",
Chris@1414 174 precision, value);
Chris@1414 175 return Tick({ value, std::string(buffer) });
Chris@1414 176 }
Chris@1414 177
Chris@1417 178 static Ticks explode(Instruction instruction) {
Chris@1417 179
Chris@1411 180 #ifdef DEBUG_SCALE_TICK_INTERVALS
Chris@1417 181 std::cerr << "initial = " << instruction.initial
Chris@1417 182 << ", limit = " << instruction.limit
Chris@1417 183 << ", spacing = " << instruction.spacing
Chris@1417 184 << ", roundTo = " << instruction.roundTo
Chris@1417 185 << ", fixed = " << instruction.fixed
Chris@1417 186 << ", precision = " << instruction.precision
Chris@1417 187 << ", logUnmap = " << instruction.logUnmap
Chris@1417 188 << std::endl;
Chris@1411 189 #endif
Chris@1417 190
Chris@1417 191 if (instruction.spacing == 0.0) {
Chris@1417 192 return {};
Chris@1414 193 }
Chris@1417 194
Chris@1411 195 double eps = 1e-7;
Chris@1417 196 if (instruction.spacing < eps * 10.0) {
Chris@1417 197 eps = instruction.spacing / 10.0;
Chris@1411 198 }
Chris@1417 199
Chris@1417 200 double max = instruction.limit;
Chris@1412 201 int n = 0;
Chris@1417 202
Chris@1417 203 Ticks ticks;
Chris@1417 204
Chris@1412 205 while (true) {
Chris@1417 206 double value = instruction.initial + n * instruction.spacing;
Chris@1417 207 value = instruction.roundTo * round(value / instruction.roundTo);
Chris@1414 208 if (value >= max + eps) {
Chris@1412 209 break;
Chris@1412 210 }
Chris@1417 211 if (instruction.logUnmap) {
Chris@1414 212 value = pow(10.0, value);
Chris@1414 213 }
Chris@1417 214 ticks.push_back(makeTick(instruction.fixed,
Chris@1417 215 instruction.precision,
Chris@1417 216 value));
Chris@1412 217 ++n;
Chris@1407 218 }
Chris@1417 219
Chris@1417 220 return ticks;
Chris@1407 221 }
Chris@1407 222 };
Chris@1407 223
Chris@1407 224 #endif