svcore  1.9
RangeMapper.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 QMUL.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef _RANGE_MAPPER_H_
17 #define _RANGE_MAPPER_H_
18 
19 #include <QString>
20 
21 #include "Debug.h"
22 #include <map>
23 
25 {
26 public:
27  virtual ~RangeMapper() { }
28 
34  virtual int getPositionForValue(float value) const = 0;
35 
44  virtual int getPositionForValueUnclamped(float value) const = 0;
45 
50  virtual float getValueForPosition(int position) const = 0;
51 
59  virtual float getValueForPositionUnclamped(int position) const = 0;
60 
64  virtual QString getUnit() const { return ""; }
65 };
66 
67 
69 {
70 public:
77  LinearRangeMapper(int minpos, int maxpos,
78  float minval, float maxval,
79  QString unit = "", bool inverted = false);
80 
81  virtual int getPositionForValue(float value) const;
82  virtual int getPositionForValueUnclamped(float value) const;
83 
84  virtual float getValueForPosition(int position) const;
85  virtual float getValueForPositionUnclamped(int position) const;
86 
87  virtual QString getUnit() const { return m_unit; }
88 
89 protected:
90  int m_minpos;
91  int m_maxpos;
92  float m_minval;
93  float m_maxval;
94  QString m_unit;
95  bool m_inverted;
96 };
97 
99 {
100 public:
109  LogRangeMapper(int minpos, int maxpos,
110  float minval, float maxval,
111  QString m_unit = "", bool inverted = false);
112 
113  static void convertRatioMinLog(float ratio, float minlog,
114  int minpos, int maxpos,
115  float &minval, float &maxval);
116 
117  static void convertMinMax(int minpos, int maxpos,
118  float minval, float maxval,
119  float &ratio, float &minlog);
120 
121  virtual int getPositionForValue(float value) const;
122  virtual int getPositionForValueUnclamped(float value) const;
123 
124  virtual float getValueForPosition(int position) const;
125  virtual float getValueForPositionUnclamped(int position) const;
126 
127  virtual QString getUnit() const { return m_unit; }
128 
129 protected:
130  int m_minpos;
131  int m_maxpos;
132  float m_ratio;
133  float m_minlog;
134  float m_maxlog;
135  QString m_unit;
137 };
138 
140 {
141 public:
142  typedef std::map<float, int> CoordMap;
143 
162  InterpolatingRangeMapper(CoordMap pointMappings,
163  QString unit);
164 
165  virtual int getPositionForValue(float value) const;
166  virtual int getPositionForValueUnclamped(float value) const;
167 
168  virtual float getValueForPosition(int position) const;
169  virtual float getValueForPositionUnclamped(int position) const;
170 
171  virtual QString getUnit() const { return m_unit; }
172 
173 protected:
175  std::map<int, float> m_reverse;
176  QString m_unit;
177 
178  template <typename T>
179  float interpolate(T *mapping, float v) const;
180 };
181 
183 {
184 public:
185  enum MappingType {
189  };
190 
191  typedef std::map<float, int> CoordMap;
192 
228  AutoRangeMapper(CoordMap pointMappings,
229  QString unit);
230 
232 
236  MappingType getType() const { return m_type; }
237 
238  virtual int getPositionForValue(float value) const;
239  virtual int getPositionForValueUnclamped(float value) const;
240 
241  virtual float getValueForPosition(int position) const;
242  virtual float getValueForPositionUnclamped(int position) const;
243 
244  virtual QString getUnit() const { return m_unit; }
245 
246 protected:
249  QString m_unit;
251 
253 };
254 
255 #endif
LinearRangeMapper(int minpos, int maxpos, float minval, float maxval, QString unit="", bool inverted=false)
Map values in range minval->maxval linearly into integer range minpos->maxpos.
Definition: RangeMapper.cpp:24
virtual float getValueForPosition(int position) const
Return the value mapped from the given position, clamping to the minimum and maximum extents of the m...
static void convertRatioMinLog(float ratio, float minlog, int minpos, int maxpos, float &minval, float &maxval)
MappingType m_type
Definition: RangeMapper.h:247
virtual float getValueForPositionUnclamped(int position) const
Return the value mapped from the given positionq, without clamping.
float interpolate(T *mapping, float v) const
std::map< float, int > CoordMap
Definition: RangeMapper.h:191
virtual int getPositionForValueUnclamped(float value) const
Return the position that maps to the given value, rounding to the nearest position,...
std::map< int, float > m_reverse
Definition: RangeMapper.h:175
virtual float getValueForPosition(int position) const
Return the value mapped from the given position, clamping to the minimum and maximum extents of the m...
MappingType chooseMappingTypeFor(const CoordMap &)
RangeMapper * m_mapper
Definition: RangeMapper.h:250
virtual QString getUnit() const
Get the unit of the mapper's value range.
Definition: RangeMapper.h:127
static void convertMinMax(int minpos, int maxpos, float minval, float maxval, float &ratio, float &minlog)
Definition: RangeMapper.cpp:99
virtual QString getUnit() const
Get the unit of the mapper's value range.
Definition: RangeMapper.h:87
virtual float getValueForPositionUnclamped(int position) const =0
Return the value mapped from the given positionq, without clamping.
virtual int getPositionForValue(float value) const
Return the position that maps to the given value, rounding to the nearest position and clamping to th...
InterpolatingRangeMapper(CoordMap pointMappings, QString unit)
Given a series of (value, position) coordinate mappings, construct a range mapper that maps arbitrary...
virtual float getValueForPosition(int position) const
Return the value mapped from the given position, clamping to the minimum and maximum extents of the m...
Definition: RangeMapper.cpp:58
virtual QString getUnit() const
Get the unit of the mapper's value range.
Definition: RangeMapper.h:64
LogRangeMapper(int minpos, int maxpos, float minval, float maxval, QString m_unit="", bool inverted=false)
Map values in range minval->maxval into integer range minpos->maxpos such that logs of the values are...
Definition: RangeMapper.cpp:76
virtual int getPositionForValue(float value) const
Return the position that maps to the given value, rounding to the nearest position and clamping to th...
virtual int getPositionForValueUnclamped(float value) const =0
Return the position that maps to the given value, rounding to the nearest position,...
virtual ~RangeMapper()
Definition: RangeMapper.h:27
virtual float getValueForPositionUnclamped(int position) const
Return the value mapped from the given positionq, without clamping.
virtual int getPositionForValueUnclamped(float value) const
Return the position that maps to the given value, rounding to the nearest position,...
virtual int getPositionForValueUnclamped(float value) const
Return the position that maps to the given value, rounding to the nearest position,...
Definition: RangeMapper.cpp:48
std::map< float, int > CoordMap
Definition: RangeMapper.h:142
virtual float getValueForPositionUnclamped(int position) const
Return the value mapped from the given positionq, without clamping.
virtual int getPositionForValue(float value) const
Return the position that maps to the given value, rounding to the nearest position and clamping to th...
virtual int getPositionForValue(float value) const
Return the position that maps to the given value, rounding to the nearest position and clamping to th...
Definition: RangeMapper.cpp:39
virtual int getPositionForValueUnclamped(float value) const
Return the position that maps to the given value, rounding to the nearest position,...
virtual int getPositionForValue(float value) const =0
Return the position that maps to the given value, rounding to the nearest position and clamping to th...
virtual float getValueForPosition(int position) const =0
Return the value mapped from the given position, clamping to the minimum and maximum extents of the m...
QString m_unit
Definition: RangeMapper.h:135
virtual QString getUnit() const
Get the unit of the mapper's value range.
Definition: RangeMapper.h:244
virtual float getValueForPositionUnclamped(int position) const
Return the value mapped from the given positionq, without clamping.
Definition: RangeMapper.cpp:67
AutoRangeMapper(CoordMap pointMappings, QString unit)
Given a series of (value, position) coordinate mappings, construct a range mapper that maps arbitrary...
MappingType getType() const
Return the mapping type in use.
Definition: RangeMapper.h:236
CoordMap m_mappings
Definition: RangeMapper.h:248
virtual QString getUnit() const
Get the unit of the mapper's value range.
Definition: RangeMapper.h:171
virtual float getValueForPosition(int position) const
Return the value mapped from the given position, clamping to the minimum and maximum extents of the m...