File: tickset.h

package info (click to toggle)
aoflagger 3.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,476 kB
  • sloc: cpp: 51,868; python: 152; sh: 25; makefile: 17
file content (617 lines) | stat: -rw-r--r-- 14,594 bytes parent folder | download
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#ifndef TICKSET_H
#define TICKSET_H

#include <string>
#include <vector>
#include <cmath>
#include <sstream>

#include "../structures/date.h"

#ifndef HAVE_EXP10
#define exp10(x) exp( (2.3025850929940456840179914546844) * (x) )
#endif

typedef std::pair<double, std::string> Tick;

class TickSet
{
	public:
		TickSet()
		{ }
		virtual ~TickSet()
		{ }
		
		virtual unsigned Size() const = 0;
		virtual Tick GetTick(unsigned i) const = 0;
		
		virtual void DecreaseTicks()
		{
			if(Size() > 1)
			{
				Set(Size() - 1);
			}
		}
		virtual void Set(unsigned maxSize) = 0;
		virtual void Reset() = 0;
		/**
		 * Returns a value scaled according to the axis.
		 * Values that are within the min and max will have an axis value of
		 * 0 to 1.
		 */
		virtual double UnitToAxis(double unitValue) const = 0;
		virtual double AxisToUnit(double axisValue) const = 0;
	protected:
	private:
		
};

class NumericTickSet : public TickSet
{
	public:
		NumericTickSet(double min, double max, unsigned sizeRequest) :
			_min(min), _max(max), _sizeRequest(sizeRequest)
		{
			set(sizeRequest);
		}
		
		unsigned Size() const final override
		{
			return _ticks.size();
		}
		
		Tick GetTick(unsigned i) const final override
		{
			std::stringstream tickStr;
			tickStr << _ticks[i];
			return Tick((_ticks[i] - _min) / (_max - _min), tickStr.str());
		}
		
		void Reset() final override
		{
			_ticks.clear();
			set(_sizeRequest);
		}
		
		void Set(unsigned maxSize) final override
		{
			_ticks.clear();
			set(maxSize);
		}
		double UnitToAxis(double unitValue) const final override
		{
			return (unitValue - _min) / (_max - _min);
		}
		double AxisToUnit(double axisValue) const final override
		{
			return axisValue * (_max - _min) + _min;
		}
	private:
		void set(unsigned sizeRequest)
		{
			if(std::isfinite(_min) && std::isfinite(_max))
			{
				if(_max == _min)
					_ticks.push_back(_min);
				else
				{
					if(sizeRequest == 0)
						return;
					double tickWidth = roundUpToNiceNumber(fabs(_max - _min) / (double) sizeRequest);
					if(tickWidth == 0.0)
						tickWidth = 1.0;
					if(_min < _max)
					{
						double pos = roundUpToNiceNumber(_min, tickWidth);
						while(pos <= _max)
						{
							if(fabs(pos) < tickWidth/100.0)
								_ticks.push_back(0.0);
							else
								_ticks.push_back(pos);
							pos += tickWidth;
						}
					} else {
						double pos = -roundUpToNiceNumber(-_min, tickWidth);
						while(pos >= _max)
						{
							if(fabs(pos) < tickWidth/100.0)
								_ticks.push_back(0.0);
							else
								_ticks.push_back(pos);
							pos -= tickWidth;
						}
					}
					while(_ticks.size() > sizeRequest)
						_ticks.pop_back();
				}
			}
		}
		
		double roundUpToNiceNumber(double number)
		{
			if(!std::isfinite(number))
				return number;
			double roundedNumber = 1.0;
			if(number <= 0.0)
			{
				if(number == 0.0)
					return 0.0;
				else
				{
					roundedNumber = -1.0;
					number *= -1.0;
				}
			}
			while(number > 10)
			{
				number /= 10;
				roundedNumber *= 10;
			}
			while(number <= 1)
			{
				number *= 10;
				roundedNumber /= 10;
			}
			if(number <= 2) return roundedNumber * 2;
			else if(number <= 5) return roundedNumber * 5;
			else return roundedNumber * 10;
		}
		double roundUpToNiceNumber(double number, double roundUnit)
		{
			return roundUnit * ceil(number / roundUnit);
		}
		
		double _min, _max;
		unsigned _sizeRequest;
		std::vector<double> _ticks;
};

class LogarithmicTickSet : public TickSet
{
	public:
		LogarithmicTickSet(double min, double max, unsigned sizeRequest) :
			_min(min), _minLog10(log10(min)),
			_max(max), _maxLog10(log10(max)),
			_sizeRequest(sizeRequest)
		{
			if(!std::isfinite(min) || !std::isfinite(max))
				throw std::runtime_error("Invalid (non-finite) range in LogarithmicTickSet");
			set(sizeRequest);
		}
		
		unsigned Size() const final override
		{
			return _ticks.size();
		}
		
		Tick GetTick(unsigned i) const final override
		{
			return _ticks[i];
		}
		
		void Reset() final override
		{
			_ticks.clear();
			set(_sizeRequest);
		}
		
		void Set(unsigned 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)
		{
			return log(unitValue / unitMin) / log(unitMax / unitMin);
		}
		
		static double AxisToUnit(double axisValue, double unitMin, double unitMax)
		{
			return exp(axisValue * 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:
		void set(unsigned 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) round(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(round(log10(v)));
					_ticks.emplace_back((log10(v) - _minLog10) / (_maxLog10 - _minLog10), "10^" + std::to_string(eVal));
				}
				else if(preferExp)
				{
					int eVal = int(floor(log10(v)));
					std::ostringstream str;
					str << (v / exp10(eVal)) << "⋅10^" << eVal;
					_ticks.emplace_back((log10(v) - _minLog10) / (_maxLog10 - _minLog10), str.str());
				}
				else {
					std::ostringstream str;
					str << v;
					_ticks.emplace_back((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;
		unsigned _sizeRequest;
		std::vector<Tick> _ticks;
};

class TimeTickSet : public TickSet
{
	public:
		TimeTickSet(double minTime, double maxTime, unsigned sizeRequest) : _min(minTime), _max(maxTime), _sizeRequest(sizeRequest)
		{
			if(!std::isfinite(minTime) || !std::isfinite(maxTime))
				throw std::runtime_error("Invalid (non-finite) range in TimeTickSet");
			set(sizeRequest);
		}
		
		unsigned Size() const final override
		{
			return _ticks.size();
		}
		
		Tick GetTick(unsigned i) const final override
		{
			double val = _ticks[i];
			return Tick((val - _min) / (_max - _min), Date::AipsMJDToTimeString(val));
		}
		
		void Reset() final override
		{
			_ticks.clear();
			set(_sizeRequest);
		}
		
		void Set(unsigned maxSize) final override
		{
			_ticks.clear();
			set(maxSize);
		}
		double UnitToAxis(double) const final override { return 0.0; }
		double AxisToUnit(double) const final override { return 0.0; }
	private:
		void set(unsigned sizeRequest)
		{
			if(_max == _min)
				_ticks.push_back(_min);
			else
			{
				if(sizeRequest == 0)
					return;
				double tickWidth = calculateTickWidth((_max - _min) / (double) sizeRequest);
				if(tickWidth == 0.0 || !std::isfinite(tickWidth))
					tickWidth = 1.0;
				double
					pos = roundUpToNiceNumber(_min, tickWidth);
				while(pos < _max)
				{
					_ticks.push_back(pos);
					pos += tickWidth;
				}
				while(_ticks.size() > sizeRequest)
					_ticks.pop_back();
			}
		}
		
		double calculateTickWidth(double lowerLimit) const
		{
			if(!std::isfinite(lowerLimit))
				return lowerLimit;
			
			// number is in units of seconds
			
			// In days?
			if(lowerLimit >= 60.0*60.0*24.0)
			{
				double width = 60.0*60.0*24.0;
				while(width < lowerLimit)
					width *= 2.0;
				return width;
			}
			// in hours?
			else if(lowerLimit > 60.0*30.0)
			{
				if(lowerLimit <= 60.0*60.0)
					return 60.0*60.0; // hours
				else if(lowerLimit <= 60.0*60.0*2.0)
					return 60.0*60.0*2.0; // two hours
				else if(lowerLimit <= 60.0*60.0*3.0)
					return 60.0*60.0*3.0; // three hours
				else if(lowerLimit <= 60.0*60.0*4.0)
					return 60.0*60.0*4.0; // four hours
				else if(lowerLimit <= 60.0*60.0*6.0)
					return 60.0*60.0*6.0; // six hours
				else
					return 60.0*60.0*12.0; // twelve hours
			}
			// in minutes?
			else if(lowerLimit > 30.0)
			{
				if(lowerLimit <= 60.0)
					return 60.0; // in minutes
				else if(lowerLimit <= 60.0*2.0)
					return 60.0*2.0; // two minutes
				else if(lowerLimit <= 60.0*5.0)
					return 60.0*5.0; // five minutes
				else if(lowerLimit <= 60.0*10.0)
					return 60.0*10.0; // ten minutes
				else if(lowerLimit <= 60.0*15.0)
					return 60.0*15.0; // quarter hours
				else
					return 60.0*30.0; // half hours
			}
			// in seconds?
			else if(lowerLimit > 0.5)
			{
				if(lowerLimit <= 1.0)
					return 1.0; // in seconds
				else if(lowerLimit <= 2.0)
					return 2.0; // two seconds
				else if(lowerLimit <= 5.0)
					return 5.0; // five seconds
				else if(lowerLimit <= 10.0)
					return 10.0; // ten seconds
				else if(lowerLimit <= 15.0)
					return 15.0; // quarter minute
				else
					return 30.0; // half a minute
			}
			else if(lowerLimit == 0.0)
				return 0.0;
			// in 10th of seconds or lower?
			else
			{
				double factor = 1.0;
				while(lowerLimit <= 0.1 && std::isfinite(lowerLimit))
				{
					factor *= 0.1;
					lowerLimit *= 10.0;
				}
				if(lowerLimit <= 0.2)
					return 0.2 * factor;
				else if(lowerLimit <= 0.5)
					return 0.5 * factor;
				else
					return factor;
			}
		}
		
		double roundUpToNiceNumber(double number, double roundUnit)
		{
			return roundUnit * ceil(number / roundUnit);
		}
		
		double _min, _max;
		unsigned _sizeRequest;
		std::vector<double> _ticks;
};

class TextTickSet : public TickSet
{
	public:
		TextTickSet(const std::vector<std::string> &labels, unsigned sizeRequest) : _sizeRequest(sizeRequest), _labels(labels)
		{
			set(sizeRequest);
		}
		
		unsigned Size() const final override
		{
			return _ticks.size();
		}
		
		Tick GetTick(unsigned i) const final override
		{
			const size_t labelIndex = _ticks[i];
			const double val = (_labels.size() == 1) ? 0.5 : (double) labelIndex / (double) (_labels.size() - 1);
			return Tick(val, _labels[labelIndex]);
		}
		
		void Reset() final override
		{
			_ticks.clear();
			set(_sizeRequest);
		}
		
		void Set(unsigned maxSize) final override
		{
			_ticks.clear();
			set(maxSize);
		}
		double UnitToAxis(double) const final override { return 0.0; }
		double AxisToUnit(double) const final override { return 0.0; }
		
	private:
		void set(unsigned sizeRequest)
		{
			if(sizeRequest > _labels.size())
				sizeRequest = _labels.size();
			const unsigned stepSize =
				(unsigned) ceil((double) _labels.size() / (double) sizeRequest);
			
			for(size_t tick=0;tick<_labels.size();tick += stepSize)
				_ticks.push_back(tick);
		}
		
		unsigned _sizeRequest;
		std::vector<std::string> _labels;
		std::vector<size_t> _ticks;
};

#endif