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 SV_RANGE_MAPPER_H
17 #define SV_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(double value) const = 0;
35 
44  virtual int getPositionForValueUnclamped(double value) const = 0;
45 
50  virtual double getValueForPosition(int position) const = 0;
51 
59  virtual double getValueForPositionUnclamped(int position) const = 0;
60 
64  virtual QString getUnit() const { return ""; }
65 
74  virtual QString getLabel(int /* position */) const { return ""; }
75 };
76 
77 
79 {
80 public:
87  LinearRangeMapper(int minpos, int maxpos,
88  double minval, double maxval,
89  QString unit = "", bool inverted = false,
90  std::map<int, QString> labels = {});
91 
92  int getPositionForValue(double value) const override;
93  int getPositionForValueUnclamped(double value) const override;
94 
95  double getValueForPosition(int position) const override;
96  double getValueForPositionUnclamped(int position) const override;
97 
98  QString getUnit() const override { return m_unit; }
99  QString getLabel(int position) const override;
100 
101 protected:
102  int m_minpos;
103  int m_maxpos;
104  double m_minval;
105  double m_maxval;
106  QString m_unit;
108  std::map<int, QString> m_labels;
109 };
110 
112 {
113 public:
122  LogRangeMapper(int minpos, int maxpos,
123  double minval, double maxval,
124  QString m_unit = "", bool inverted = false);
125 
126  static void convertRatioMinLog(double ratio, double minlog,
127  int minpos, int maxpos,
128  double &minval, double &maxval);
129 
130  static void convertMinMax(int minpos, int maxpos,
131  double minval, double maxval,
132  double &minlog, double &ratio);
133 
134  int getPositionForValue(double value) const override;
135  int getPositionForValueUnclamped(double value) const override;
136 
137  double getValueForPosition(int position) const override;
138  double getValueForPositionUnclamped(int position) const override;
139 
140  QString getUnit() const override { return m_unit; }
141 
142 protected:
143  int m_minpos;
144  int m_maxpos;
145  double m_ratio;
146  double m_minlog;
147  double m_maxlog;
148  QString m_unit;
150 };
151 
153 {
154 public:
155  typedef std::map<double, int> CoordMap;
156 
175  InterpolatingRangeMapper(CoordMap pointMappings,
176  QString unit);
177 
178  int getPositionForValue(double value) const override;
179  int getPositionForValueUnclamped(double value) const override;
180 
181  double getValueForPosition(int position) const override;
182  double getValueForPositionUnclamped(int position) const override;
183 
184  QString getUnit() const override { return m_unit; }
185 
186 protected:
187  CoordMap m_mappings;
188  std::map<int, double> m_reverse;
189  QString m_unit;
190 
191  template <typename T>
192  double interpolate(T *mapping, double v) const;
193 };
194 
196 {
197 public:
198  enum MappingType {
202  };
203 
204  typedef std::map<double, int> CoordMap;
205 
241  AutoRangeMapper(CoordMap pointMappings,
242  QString unit);
243 
244  ~AutoRangeMapper();
245 
249  MappingType getType() const { return m_type; }
250 
251  int getPositionForValue(double value) const override;
252  int getPositionForValueUnclamped(double value) const override;
253 
254  double getValueForPosition(int position) const override;
255  double getValueForPositionUnclamped(int position) const override;
256 
257  QString getUnit() const override { return m_unit; }
258 
259 protected:
261  CoordMap m_mappings;
262  QString m_unit;
264 
265  MappingType chooseMappingTypeFor(const CoordMap &);
266 };
267 
268 #endif
QString getUnit() const override
Get the unit of the mapper&#39;s value range.
Definition: RangeMapper.h:140
MappingType m_type
Definition: RangeMapper.h:260
QString getUnit() const override
Get the unit of the mapper&#39;s value range.
Definition: RangeMapper.h:184
virtual int getPositionForValue(double value) const =0
Return the position that maps to the given value, rounding to the nearest position and clamping to th...
QString getUnit() const override
Get the unit of the mapper&#39;s value range.
Definition: RangeMapper.h:257
MappingType getType() const
Return the mapping type in use.
Definition: RangeMapper.h:249
std::map< double, int > CoordMap
Definition: RangeMapper.h:155
RangeMapper * m_mapper
Definition: RangeMapper.h:263
virtual double getValueForPositionUnclamped(int position) const =0
Return the value mapped from the given position, without clamping.
virtual double getValueForPosition(int position) const =0
Return the value mapped from the given position, clamping to the minimum and maximum extents of the m...
virtual ~RangeMapper()
Definition: RangeMapper.h:27
std::map< double, int > CoordMap
Definition: RangeMapper.h:204
virtual int getPositionForValueUnclamped(double value) const =0
Return the position that maps to the given value, rounding to the nearest position, without clamping.
virtual QString getLabel(int) const
The mapper may optionally provide special labels for one or more individual positions (such as the mi...
Definition: RangeMapper.h:74
virtual QString getUnit() const
Get the unit of the mapper&#39;s value range.
Definition: RangeMapper.h:64
QString m_unit
Definition: RangeMapper.h:148
std::map< int, QString > m_labels
Definition: RangeMapper.h:108
QString getUnit() const override
Get the unit of the mapper&#39;s value range.
Definition: RangeMapper.h:98
CoordMap m_mappings
Definition: RangeMapper.h:261
std::map< int, double > m_reverse
Definition: RangeMapper.h:188