comparison base/ScaleTickIntervals.h @ 1429:48e9f538e6e9

Untabify
author Chris Cannam
date Thu, 01 Mar 2018 18:02:22 +0000
parents 04ce84f21af3
children 3a128665fa6f
comparison
equal deleted inserted replaced
1428:87ae75da6527 1429:48e9f538e6e9
28 28
29 class ScaleTickIntervals 29 class ScaleTickIntervals
30 { 30 {
31 public: 31 public:
32 struct Range { 32 struct Range {
33 double min; // start of value range 33 double min; // start of value range
34 double max; // end of value range 34 double max; // end of value range
35 int n; // number of divisions (approximate only) 35 int n; // number of divisions (approximate only)
36 }; 36 };
37 37
38 struct Tick { 38 struct Tick {
39 double value; // value this tick represents 39 double value; // value this tick represents
40 std::string label; // value as written 40 std::string label; // value as written
41 }; 41 };
42 42
43 typedef std::vector<Tick> Ticks; 43 typedef std::vector<Tick> Ticks;
44 44
45 /** 45 /**
77 Scientific, 77 Scientific,
78 Auto 78 Auto
79 }; 79 };
80 80
81 struct Instruction { 81 struct Instruction {
82 double initial; // value of first tick 82 double initial; // value of first tick
83 double limit; // max from original range 83 double limit; // max from original range
84 double spacing; // increment between ticks 84 double spacing; // increment between ticks
85 double roundTo; // what all displayed values should be rounded to 85 double roundTo; // what all displayed values should be rounded to
86 Display display; // whether to use fixed precision (%e, %f, or %g) 86 Display display; // whether to use fixed precision (%e, %f, or %g)
87 int precision; // number of dp (%f) or sf (%e) 87 int precision; // number of dp (%f) or sf (%e)
88 bool logUnmap; // true if values represent logs of display values 88 bool logUnmap; // true if values represent logs of display values
89 }; 89 };
90 90
91 static Instruction linearInstruction(Range r) 91 static Instruction linearInstruction(Range r)
92 { 92 {
93 Display display = Auto; 93 Display display = Auto;
94 94
95 if (r.max < r.min) { 95 if (r.max < r.min) {
96 return linearInstruction({ r.max, r.min, r.n }); 96 return linearInstruction({ r.max, r.min, r.n });
97 } 97 }
98 if (r.n < 1 || r.max == r.min) { 98 if (r.n < 1 || r.max == r.min) {
99 return { r.min, r.min, 1.0, r.min, display, 1, false }; 99 return { r.min, r.min, 1.0, r.min, display, 1, false };
100 } 100 }
101 101
102 double inc = (r.max - r.min) / r.n; 102 double inc = (r.max - r.min) / r.n;
103 103
104 double digInc = log10(inc); 104 double digInc = log10(inc);
105 double digMax = log10(fabs(r.max)); 105 double digMax = log10(fabs(r.max));
106 double digMin = log10(fabs(r.min)); 106 double digMin = log10(fabs(r.min));
107 107
108 int precInc = int(floor(digInc)); 108 int precInc = int(floor(digInc));
109 double roundTo = pow(10.0, precInc); 109 double roundTo = pow(10.0, precInc);
110 110
111 if (precInc > -4 && precInc < 4) { 111 if (precInc > -4 && precInc < 4) {
112 display = Fixed; 112 display = Fixed;
113 } else if ((digMax >= -2.0 && digMax <= 3.0) && 113 } else if ((digMax >= -2.0 && digMax <= 3.0) &&
114 (digMin >= -3.0 && digMin <= 3.0)) { 114 (digMin >= -3.0 && digMin <= 3.0)) {
180 180
181 static Instruction logInstruction(Range r) 181 static Instruction logInstruction(Range r)
182 { 182 {
183 Display display = Auto; 183 Display display = Auto;
184 184
185 if (r.n < 1) { 185 if (r.n < 1) {
186 return {}; 186 return {};
187 } 187 }
188 if (r.max < r.min) { 188 if (r.max < r.min) {
189 return logInstruction({ r.max, r.min, r.n }); 189 return logInstruction({ r.max, r.min, r.n });
190 } 190 }
191 if (r.max == r.min) { 191 if (r.max == r.min) {
192 return { r.min, r.max, 1.0, r.min, display, 1, true }; 192 return { r.min, r.max, 1.0, r.min, display, 1, true };
193 } 193 }
194 194
195 double inc = (r.max - r.min) / r.n; 195 double inc = (r.max - r.min) / r.n;
196 196
197 double digInc = log10(inc); 197 double digInc = log10(inc);
198 int precInc = int(floor(digInc)); 198 int precInc = int(floor(digInc));
199 double roundTo = pow(10.0, precInc); 199 double roundTo = pow(10.0, precInc);
200 200
201 if (roundTo != 0.0) { 201 if (roundTo != 0.0) {
202 inc = round(inc / roundTo) * roundTo; 202 inc = round(inc / roundTo) * roundTo;
203 if (inc < roundTo) inc = roundTo; 203 if (inc < roundTo) inc = roundTo;
204 } 204 }
235 << ", inc = " << inc << ", precInc = " << precInc 235 << ", inc = " << inc << ", precInc = " << precInc
236 << ", prec = " << prec << endl; 236 << ", prec = " << prec << endl;
237 SVDEBUG << "ScaleTickIntervals: roundTo = " << roundTo << endl; 237 SVDEBUG << "ScaleTickIntervals: roundTo = " << roundTo << endl;
238 #endif 238 #endif
239 239
240 double min = r.min; 240 double min = r.min;
241 if (inc != 0.0) { 241 if (inc != 0.0) {
242 min = ceil(r.min / inc) * inc; 242 min = ceil(r.min / inc) * inc;
243 if (min > r.max) min = r.max; 243 if (min > r.max) min = r.max;
244 } 244 }
245 245
273 } 273 }
274 274
275 static Ticks explode(Instruction instruction) { 275 static Ticks explode(Instruction instruction) {
276 276
277 #ifdef DEBUG_SCALE_TICK_INTERVALS 277 #ifdef DEBUG_SCALE_TICK_INTERVALS
278 SVDEBUG << "ScaleTickIntervals::explode:" << endl 278 SVDEBUG << "ScaleTickIntervals::explode:" << endl
279 << "initial = " << instruction.initial 279 << "initial = " << instruction.initial
280 << ", limit = " << instruction.limit 280 << ", limit = " << instruction.limit
281 << ", spacing = " << instruction.spacing 281 << ", spacing = " << instruction.spacing
282 << ", roundTo = " << instruction.roundTo 282 << ", roundTo = " << instruction.roundTo
283 << ", display = " << instruction.display 283 << ", display = " << instruction.display
309 value = pow(10.0, value); 309 value = pow(10.0, value);
310 } 310 }
311 if (instruction.roundTo != 0.0) { 311 if (instruction.roundTo != 0.0) {
312 value = instruction.roundTo * round(value / instruction.roundTo); 312 value = instruction.roundTo * round(value / instruction.roundTo);
313 } 313 }
314 ticks.push_back(makeTick(instruction.display, 314 ticks.push_back(makeTick(instruction.display,
315 instruction.precision, 315 instruction.precision,
316 value)); 316 value));
317 ++n; 317 ++n;
318 } 318 }
319 319
320 return ticks; 320 return ticks;
321 } 321 }
322 }; 322 };
323 323