comparison base/RangeMapper.h @ 885:12a6140b3ae0

Add unclamped range mapper methods
author Chris Cannam
date Thu, 06 Feb 2014 15:31:16 +0000
parents b4787b595db3
children cc27f35aa75c
comparison
equal deleted inserted replaced
884:633a8fa622c9 885:12a6140b3ae0
23 23
24 class RangeMapper 24 class RangeMapper
25 { 25 {
26 public: 26 public:
27 virtual ~RangeMapper() { } 27 virtual ~RangeMapper() { }
28
29 /**
30 * Return the position that maps to the given value, rounding to
31 * the nearest position and clamping to the minimum and maximum
32 * extents of the mapper's positional range.
33 */
28 virtual int getPositionForValue(float value) const = 0; 34 virtual int getPositionForValue(float value) const = 0;
35
36 /**
37 * Return the position that maps to the given value, rounding to
38 * the nearest position, without clamping. That is, whatever
39 * mapping function is in use will be projected even outside the
40 * minimum and maximum extents of the mapper's positional
41 * range. (The mapping outside that range is not guaranteed to be
42 * exact, except if the mapper is a linear one.)
43 */
44 virtual int getPositionForValueUnclamped(float value) const = 0;
45
46 /**
47 * Return the value mapped from the given position, clamping to
48 * the minimum and maximum extents of the mapper's value range.
49 */
29 virtual float getValueForPosition(int position) const = 0; 50 virtual float getValueForPosition(int position) const = 0;
51
52 /**
53 * Return the value mapped from the given positionq, without
54 * clamping. That is, whatever mapping function is in use will be
55 * projected even outside the minimum and maximum extents of the
56 * mapper's value range. (The mapping outside that range is not
57 * guaranteed to be exact, except if the mapper is a linear one.)
58 */
59 virtual float getValueForPositionUnclamped(int position) const = 0;
60
61 /**
62 * Get the unit of the mapper's value range.
63 */
30 virtual QString getUnit() const { return ""; } 64 virtual QString getUnit() const { return ""; }
31 }; 65 };
32 66
33 67
34 class LinearRangeMapper : public RangeMapper 68 class LinearRangeMapper : public RangeMapper
43 LinearRangeMapper(int minpos, int maxpos, 77 LinearRangeMapper(int minpos, int maxpos,
44 float minval, float maxval, 78 float minval, float maxval,
45 QString unit = "", bool inverted = false); 79 QString unit = "", bool inverted = false);
46 80
47 virtual int getPositionForValue(float value) const; 81 virtual int getPositionForValue(float value) const;
48 virtual float getValueForPosition(int position) const; 82 virtual int getPositionForValueUnclamped(float value) const;
83
84 virtual float getValueForPosition(int position) const;
85 virtual float getValueForPositionUnclamped(int position) const;
49 86
50 virtual QString getUnit() const { return m_unit; } 87 virtual QString getUnit() const { return m_unit; }
51 88
52 protected: 89 protected:
53 int m_minpos; 90 int m_minpos;
62 { 99 {
63 public: 100 public:
64 /** 101 /**
65 * Map values in range minval->maxval into integer range 102 * Map values in range minval->maxval into integer range
66 * minpos->maxpos such that logs of the values are mapped 103 * minpos->maxpos such that logs of the values are mapped
67 * linearly. minval and minpos must be less than maxval and maxpos 104 * linearly. minval must be greater than zero, and minval and
68 * respectively. If inverted is true, the range will be mapped 105 * minpos must be less than maxval and maxpos respectively. If
69 * "backwards" (minval to maxpos and maxval to minpos). 106 * inverted is true, the range will be mapped "backwards" (minval
107 * to maxpos and maxval to minpos).
70 */ 108 */
71 LogRangeMapper(int minpos, int maxpos, 109 LogRangeMapper(int minpos, int maxpos,
72 float minval, float maxval, 110 float minval, float maxval,
73 QString m_unit = "", bool inverted = false); 111 QString m_unit = "", bool inverted = false);
74 112
79 static void convertMinMax(int minpos, int maxpos, 117 static void convertMinMax(int minpos, int maxpos,
80 float minval, float maxval, 118 float minval, float maxval,
81 float &ratio, float &minlog); 119 float &ratio, float &minlog);
82 120
83 virtual int getPositionForValue(float value) const; 121 virtual int getPositionForValue(float value) const;
84 virtual float getValueForPosition(int position) const; 122 virtual int getPositionForValueUnclamped(float value) const;
123
124 virtual float getValueForPosition(int position) const;
125 virtual float getValueForPositionUnclamped(int position) const;
85 126
86 virtual QString getUnit() const { return m_unit; } 127 virtual QString getUnit() const { return m_unit; }
87 128
88 protected: 129 protected:
89 int m_minpos; 130 int m_minpos;
120 */ 161 */
121 InterpolatingRangeMapper(CoordMap pointMappings, 162 InterpolatingRangeMapper(CoordMap pointMappings,
122 QString unit); 163 QString unit);
123 164
124 virtual int getPositionForValue(float value) const; 165 virtual int getPositionForValue(float value) const;
125 virtual float getValueForPosition(int position) const; 166 virtual int getPositionForValueUnclamped(float value) const;
167
168 virtual float getValueForPosition(int position) const;
169 virtual float getValueForPositionUnclamped(int position) const;
126 170
127 virtual QString getUnit() const { return m_unit; } 171 virtual QString getUnit() const { return m_unit; }
128 172
129 protected: 173 protected:
130 CoordMap m_mappings; 174 CoordMap m_mappings;
131 std::map<int, float> m_reverse; 175 std::map<int, float> m_reverse;
132 QString m_unit; 176 QString m_unit;
177
178 template <typename T>
179 float interpolate(T *mapping, float v) const;
133 }; 180 };
134 181
135 class AutoRangeMapper : public RangeMapper 182 class AutoRangeMapper : public RangeMapper
136 { 183 {
137 public: 184 public:
187 * Return the mapping type in use. 234 * Return the mapping type in use.
188 */ 235 */
189 MappingType getType() const { return m_type; } 236 MappingType getType() const { return m_type; }
190 237
191 virtual int getPositionForValue(float value) const; 238 virtual int getPositionForValue(float value) const;
192 virtual float getValueForPosition(int position) const; 239 virtual int getPositionForValueUnclamped(float value) const;
240
241 virtual float getValueForPosition(int position) const;
242 virtual float getValueForPositionUnclamped(int position) const;
193 243
194 virtual QString getUnit() const { return m_unit; } 244 virtual QString getUnit() const { return m_unit; }
195 245
196 protected: 246 protected:
197 MappingType m_type; 247 MappingType m_type;