File: logarithmictickset.h

package info (click to toggle)
aoflagger 3.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,000 kB
  • sloc: cpp: 67,891; python: 497; sh: 242; makefile: 22
file content (239 lines) | stat: -rw-r--r-- 7,541 bytes parent folder | download | duplicates (2)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#ifndef LOGARITHMIC_TICK_SET_H
#define LOGARITHMIC_TICK_SET_H

#include "tickset.h"

class LogarithmicTickSet : public TickSet {
 public:
  LogarithmicTickSet(double min, double max, unsigned sizeRequest)
      : _min(min),
        _minLog10(std::log10(min)),
        _max(max),
        _maxLog10(std::log10(max)),
        _sizeRequest(sizeRequest) {
    if (std::isfinite(min) && std::isfinite(max)) {
      set(sizeRequest);
    }
  }

  std::unique_ptr<TickSet> Clone() const override {
    return std::unique_ptr<LogarithmicTickSet>(new LogarithmicTickSet(*this));
  }

  size_t Size() const final override { return _ticks.size(); }

  Tick GetTick(size_t i) const final override { return _ticks[i]; }

  void Reset() final override {
    _ticks.clear();
    set(_sizeRequest);
  }

  void Set(size_t maxSize) final override {
    _ticks.clear();
    set(maxSize);
  }

  /**
   * Returns a value scaled according to a given axis.
   * Values that are within the min and max will have an axis value of
   * 0 to 1.
   */
  static double UnitToAxis(double unitValue, double unitMin, double unitMax) {
    if (unitMin == 0.0 || unitMax == unitMin)
      return 0.5;
    else
      return std::log(unitValue / unitMin) / std::log(unitMax / unitMin);
  }

  static double AxisToUnit(double axisValue, double unitMin, double unitMax) {
    return std::exp(axisValue * std::log(unitMax / unitMin)) * unitMin;
  }
  double UnitToAxis(double unitValue) const final override {
    return UnitToAxis(unitValue, _min, _max);
  }
  double AxisToUnit(double axisValue) const final override {
    return AxisToUnit(axisValue, _min, _max);
  }

 private:
  static std::string toSuperScript(const std::string& val) {
    const std::string kToSuperTable[10] = {"⁰", "¹", "²", "³", "⁴",
                                           "⁵", "⁶", "⁷", "⁸", "⁹"};
    std::stringstream out;
    for (const char c : val) {
      if (c >= '0' && c <= '9') {
        out << kToSuperTable[c - '0'];
      } else if (c == '-') {
        out << "⁻";
      } else {
        out << c;
      }
    }
    return out.str();
  }

  void set(size_t sizeRequest) {
    std::vector<double> vals;
    bool hasTensOnly = true;
    if (_max == _min) {
      vals.emplace_back(_min);
      hasTensOnly = false;
    } else {
      if (sizeRequest == 0) sizeRequest = 1;
      const double tickStart = roundUpToBase10Number(_min * 0.999),
                   tickEnd = roundDownToBase10Number(_max * 1.001);
      vals.emplace_back(tickStart);
      if (sizeRequest == 1) return;
      // Add all steps with factor of ten
      if (tickEnd > tickStart) {
        const unsigned distance =
            (unsigned)std::round(std::log10(tickEnd / tickStart));
        const unsigned step = (distance + sizeRequest - 1) / sizeRequest;
        const double factor = exp10((double)step);
        double pos = tickStart * factor;
        while (pos <= _max && vals.size() < sizeRequest) {
          vals.emplace_back(pos);
          pos *= factor;
        }
      }
      // can we add two to nine?
      // If there are already 3 steps of 10, skip this so the format can be like
      // "10^3" etc
      bool isFull = vals.size() >= sizeRequest;  // || vals.size() >= 3;
      if (!isFull) {
        std::vector<double> tryTickset(vals);
        double base = tickStart / 10.0;
        do {
          for (double i = 2.0; i < 9.5; ++i) {
            double val = base * i;
            if (val >= _min && val <= _max) tryTickset.push_back(val);
          }
          base *= 10.0;
        } while (base < _max);
        if (tryTickset.size() <= sizeRequest) {
          vals = tryTickset;
          hasTensOnly = false;
          isFull = true;

          // can we add 1.5 ?
          base = tickStart / 10.0;
          do {
            double val = base * 1.5;
            if (val >= _min && val <= _max) tryTickset.push_back(val);
            base *= 10.0;
          } while (base < _max);
          if (tryTickset.size() <= sizeRequest) vals = std::move(tryTickset);
        }
      }
      // can we add two, four, ... eight?
      if (!isFull) {
        std::vector<double> tryTickset(vals);
        double base = tickStart / 10.0;
        do {
          for (double i = 2.0; i < 9.0; i += 2.0) {
            double val = base * i;
            if (val >= _min && val <= _max) tryTickset.push_back(val);
          }
          base *= 10.0;
        } while (base < _max);
        if (tryTickset.size() <= sizeRequest) {
          vals = tryTickset;
          hasTensOnly = false;
          isFull = true;

          // can we add 1.5 ?
          base = tickStart / 10.0;
          do {
            double val = base * 1.5;
            if (val >= _min && val <= _max) tryTickset.push_back(val);
            base *= 10.0;
          } while (base < _max);
          if (tryTickset.size() <= sizeRequest) vals = std::move(tryTickset);
        }
      }
      // can we add two and five?
      if (!isFull) {
        std::vector<double> tryTickset(vals);
        double base = tickStart / 10.0;
        do {
          for (double i = 2.0; i < 6.0; i += 3.0) {
            double val = base * i;
            if (val >= _min && val <= _max) tryTickset.push_back(val);
          }
          base *= 10.0;
        } while (base < _max);
        if (tryTickset.size() <= sizeRequest) {
          vals = std::move(tryTickset);
          hasTensOnly = false;
          isFull = true;
        }
      }
      // can we add fives?
      if (!isFull) {
        std::vector<double> tryTickset(vals);
        double base = tickStart / 10.0;
        do {
          double val = base * 5.0;
          if (val >= _min && val <= _max) tryTickset.push_back(val);
          base *= 10.0;
        } while (base < _max);
        if (tryTickset.size() <= sizeRequest) {
          vals = std::move(tryTickset);
          hasTensOnly = false;
          isFull = true;
        }
      }
      std::sort(vals.begin(), vals.end());
    }

    // Should we use "3e-3" syntax?
    bool preferExp = (_max / _min > 100 || _max >= 1000.0 || _min < 0.01);
    // Can we instead use "10^-5" syntax?
    bool preferTens = preferExp && hasTensOnly;

    for (double v : vals) {
      if (preferTens) {
        int eVal = int(std::round(std::log10(v)));
        _ticks.emplace_back(
            (std::log10(v) - _minLog10) / (_maxLog10 - _minLog10),
            "10" + toSuperScript(std::to_string(eVal)));
      } else if (preferExp) {
        int eVal = int(std::floor(std::log10(v)));
        double mantissa = v / exp10(eVal);
        if (std::fabs(mantissa - 10.0) < 1e-6) {
          eVal++;
          mantissa /= 10.0;
        }
        std::ostringstream str;
        str << (v / exp10(eVal)) << "⋅10"
            << toSuperScript(std::to_string(eVal));
        _ticks.emplace_back(
            (std::log10(v) - _minLog10) / (_maxLog10 - _minLog10), str.str());
      } else {
        std::ostringstream str;
        str << v;
        _ticks.emplace_back(
            (std::log10(v) - _minLog10) / (_maxLog10 - _minLog10), str.str());
      }
    }
  }

  double roundUpToBase10Number(double number) const {
    if (!std::isfinite(number)) return number;
    const double l = log10(number);
    return exp10(ceil(l));
  }

  double roundDownToBase10Number(double number) const {
    if (!std::isfinite(number)) return number;
    const double l = log10(number);
    return exp10(floor(l));
  }

  double _min, _minLog10, _max, _maxLog10;
  size_t _sizeRequest;
  std::vector<Tick> _ticks;
};

#endif