svcore  1.9
RangeMapper.h
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 QMUL.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #ifndef _RANGE_MAPPER_H_
00017 #define _RANGE_MAPPER_H_
00018 
00019 #include <QString>
00020 
00021 #include "Debug.h"
00022 #include <map>
00023 
00024 class RangeMapper 
00025 {
00026 public:
00027     virtual ~RangeMapper() { }
00028 
00034     virtual int getPositionForValue(float value) const = 0;
00035 
00044     virtual int getPositionForValueUnclamped(float value) const = 0;
00045 
00050     virtual float getValueForPosition(int position) const = 0;
00051 
00059     virtual float getValueForPositionUnclamped(int position) const = 0;
00060 
00064     virtual QString getUnit() const { return ""; }
00065 };
00066 
00067 
00068 class LinearRangeMapper : public RangeMapper
00069 {
00070 public:
00077     LinearRangeMapper(int minpos, int maxpos,
00078                       float minval, float maxval,
00079                       QString unit = "", bool inverted = false);
00080     
00081     virtual int getPositionForValue(float value) const;
00082     virtual int getPositionForValueUnclamped(float value) const;
00083 
00084     virtual float getValueForPosition(int position) const;
00085     virtual float getValueForPositionUnclamped(int position) const;
00086 
00087     virtual QString getUnit() const { return m_unit; }
00088 
00089 protected:
00090     int m_minpos;
00091     int m_maxpos;
00092     float m_minval;
00093     float m_maxval;
00094     QString m_unit;
00095     bool m_inverted;
00096 };
00097 
00098 class LogRangeMapper : public RangeMapper
00099 {
00100 public:
00109     LogRangeMapper(int minpos, int maxpos,
00110                    float minval, float maxval,
00111                    QString m_unit = "", bool inverted = false);
00112 
00113     static void convertRatioMinLog(float ratio, float minlog,
00114                                    int minpos, int maxpos,
00115                                    float &minval, float &maxval);
00116 
00117     static void convertMinMax(int minpos, int maxpos,
00118                               float minval, float maxval,
00119                               float &ratio, float &minlog);
00120 
00121     virtual int getPositionForValue(float value) const;
00122     virtual int getPositionForValueUnclamped(float value) const;
00123 
00124     virtual float getValueForPosition(int position) const;
00125     virtual float getValueForPositionUnclamped(int position) const;
00126 
00127     virtual QString getUnit() const { return m_unit; }
00128 
00129 protected:
00130     int m_minpos;
00131     int m_maxpos;
00132     float m_ratio;
00133     float m_minlog;
00134     float m_maxlog;
00135     QString m_unit;
00136     bool m_inverted;
00137 };
00138 
00139 class InterpolatingRangeMapper : public RangeMapper
00140 {
00141 public:
00142     typedef std::map<float, int> CoordMap;
00143 
00162     InterpolatingRangeMapper(CoordMap pointMappings,
00163                              QString unit);
00164 
00165     virtual int getPositionForValue(float value) const;
00166     virtual int getPositionForValueUnclamped(float value) const;
00167 
00168     virtual float getValueForPosition(int position) const;
00169     virtual float getValueForPositionUnclamped(int position) const;
00170 
00171     virtual QString getUnit() const { return m_unit; }
00172 
00173 protected:
00174     CoordMap m_mappings;
00175     std::map<int, float> m_reverse;
00176     QString m_unit;
00177 
00178     template <typename T>
00179     float interpolate(T *mapping, float v) const;
00180 };
00181 
00182 class AutoRangeMapper : public RangeMapper
00183 {
00184 public:
00185     enum MappingType {
00186         Interpolating,
00187         StraightLine,
00188         Logarithmic,
00189     };
00190 
00191     typedef std::map<float, int> CoordMap;
00192 
00228     AutoRangeMapper(CoordMap pointMappings,
00229                     QString unit);
00230 
00231     ~AutoRangeMapper();
00232 
00236     MappingType getType() const { return m_type; }
00237 
00238     virtual int getPositionForValue(float value) const;
00239     virtual int getPositionForValueUnclamped(float value) const;
00240 
00241     virtual float getValueForPosition(int position) const;
00242     virtual float getValueForPositionUnclamped(int position) const;
00243 
00244     virtual QString getUnit() const { return m_unit; }
00245 
00246 protected:
00247     MappingType m_type;
00248     CoordMap m_mappings;
00249     QString m_unit;
00250     RangeMapper *m_mapper;
00251 
00252     MappingType chooseMappingTypeFor(const CoordMap &);
00253 };
00254 
00255 #endif