forked from RobTillaart/FastMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastMap.cpp
104 lines (88 loc) · 2.51 KB
/
FastMap.cpp
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
//
// FILE: FastMap.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.1
// PURPOSE: class with fast map function - library for Arduino
// URL: https://github.com/RobTillaart/FastMap
//
// HISTORY:
// 0.3.1 2020-08-31 updated documentation
// 0.3.0 2020-07-04 added fastMapDouble + test sketch.
// 0.2.1 2020-06-10 fix library.json; rename license
// 0.2.0 2020-03-21 #pragma once; readme.md; license.md
//
// 0.1.8 2017-07-27 revert double to float (issue 33)
// 0.1.7 2017-04-28 cleaned up, get examples working again
// 0.1.06 2015-03-08 replaced float by double (support ARM)
// 0.1.05 2014-11-02 stripped of bit mask experimental code
// 0.1.04 add back() - the inverse map
// tested with bit mask for constrain code (Perfomance was killed)
// 0.1.03 proper name
// 0.1.02 sqeezed the code (first public version)
// 0.1.01 refactor
// 0.1.00 initial version
//
#include "FastMap.h"
FastMap::FastMap()
{
init(0, 1, 0, 1);
}
void FastMap::init(float in_min, float in_max, float out_min, float out_max)
{
_in_min = in_min;
_in_max = in_max;
_out_min = out_min;
_out_max = out_max;
_factor = (out_max - out_min)/(in_max - in_min);
_base = out_min - in_min * _factor;
_backfactor = 1/_factor;
_backbase = in_min - out_min * _backfactor;
}
float FastMap::constrainedMap(float value)
{
if (value <= _in_min) return _out_min;
if (value >= _in_max) return _out_max;
return this->map(value);
}
float FastMap::lowerConstrainedMap(float value)
{
if (value <= _in_min) return _out_min;
return this->map(value);
}
float FastMap::upperConstrainedMap(float value)
{
if (value >= _in_max) return _out_max;
return this->map(value);
}
FastMapDouble::FastMapDouble()
{
init(0, 1, 0, 1);
}
void FastMapDouble::init(double in_min, double in_max, double out_min, double out_max)
{
_in_min = in_min;
_in_max = in_max;
_out_min = out_min;
_out_max = out_max;
_factor = (out_max - out_min)/(in_max - in_min);
_base = out_min - in_min * _factor;
_backfactor = 1/_factor;
_backbase = in_min - out_min * _backfactor;
}
double FastMapDouble::constrainedMap(double value)
{
if (value <= _in_min) return _out_min;
if (value >= _in_max) return _out_max;
return this->map(value);
}
double FastMapDouble::lowerConstrainedMap(double value)
{
if (value <= _in_min) return _out_min;
return this->map(value);
}
double FastMapDouble::upperConstrainedMap(double value)
{
if (value >= _in_max) return _out_max;
return this->map(value);
}
// END OF FILE