comparison base/RangeMapper.cpp @ 879:eb6b6a88faed

Unit-test RangeMapper, fix a couple of bugs
author Chris Cannam
date Fri, 31 Jan 2014 13:39:37 +0000
parents e802e550a1f2
children b4787b595db3
comparison
equal deleted inserted replaced
862:786ee8d1f30e 879:eb6b6a88faed
41 int position = m_minpos + 41 int position = m_minpos +
42 lrintf(((value - m_minval) / (m_maxval - m_minval)) 42 lrintf(((value - m_minval) / (m_maxval - m_minval))
43 * (m_maxpos - m_minpos)); 43 * (m_maxpos - m_minpos));
44 if (position < m_minpos) position = m_minpos; 44 if (position < m_minpos) position = m_minpos;
45 if (position > m_maxpos) position = m_maxpos; 45 if (position > m_maxpos) position = m_maxpos;
46 // SVDEBUG << "LinearRangeMapper::getPositionForValue: " << value << " -> " 46 if (m_inverted) return m_maxpos - (position - m_minpos);
47 // << position << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", minval " << m_minval << ", maxval " << m_maxval << ")" << endl;
48 if (m_inverted) return m_maxpos - position;
49 else return position; 47 else return position;
50 } 48 }
51 49
52 float 50 float
53 LinearRangeMapper::getValueForPosition(int position) const 51 LinearRangeMapper::getValueForPosition(int position) const
54 { 52 {
55 if (m_inverted) position = m_maxpos - position; 53 if (m_inverted) position = m_maxpos - (position - m_minpos);
54 if (position < m_minpos) position = m_minpos;
55 if (position > m_maxpos) position = m_maxpos;
56 float value = m_minval + 56 float value = m_minval +
57 ((float(position - m_minpos) / float(m_maxpos - m_minpos)) 57 ((float(position - m_minpos) / float(m_maxpos - m_minpos))
58 * (m_maxval - m_minval)); 58 * (m_maxval - m_minval));
59 if (value < m_minval) value = m_minval;
60 if (value > m_maxval) value = m_maxval;
61 // SVDEBUG << "LinearRangeMapper::getValueForPosition: " << position << " -> "
62 // << value << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", minval " << m_minval << ", maxval " << m_maxval << ")" << endl;
63 return value; 59 return value;
64 } 60 }
65 61
66 LogRangeMapper::LogRangeMapper(int minpos, int maxpos, 62 LogRangeMapper::LogRangeMapper(int minpos, int maxpos,
67 float minval, float maxval, 63 float minval, float maxval,
71 m_unit(unit), 67 m_unit(unit),
72 m_inverted(inverted) 68 m_inverted(inverted)
73 { 69 {
74 convertMinMax(minpos, maxpos, minval, maxval, m_minlog, m_ratio); 70 convertMinMax(minpos, maxpos, minval, maxval, m_minlog, m_ratio);
75 71
76 cerr << "LogRangeMapper: minpos " << minpos << ", maxpos " 72 // cerr << "LogRangeMapper: minpos " << minpos << ", maxpos "
77 << maxpos << ", minval " << minval << ", maxval " 73 // << maxpos << ", minval " << minval << ", maxval "
78 << maxval << ", minlog " << m_minlog << ", ratio " << m_ratio 74 // << maxval << ", minlog " << m_minlog << ", ratio " << m_ratio
79 << ", unit " << unit << endl; 75 // << ", unit " << unit << endl;
80 76
81 assert(m_maxpos != m_minpos); 77 assert(m_maxpos != m_minpos);
82 78
83 m_maxlog = (m_maxpos - m_minpos) / m_ratio + m_minlog; 79 m_maxlog = (m_maxpos - m_minpos) / m_ratio + m_minlog;
80
81 // cerr << "LogRangeMapper: maxlog = " << m_maxlog << endl;
84 } 82 }
85 83
86 void 84 void
87 LogRangeMapper::convertMinMax(int minpos, int maxpos, 85 LogRangeMapper::convertMinMax(int minpos, int maxpos,
88 float minval, float maxval, 86 float minval, float maxval,
104 } 102 }
105 103
106 int 104 int
107 LogRangeMapper::getPositionForValue(float value) const 105 LogRangeMapper::getPositionForValue(float value) const
108 { 106 {
109 int position = (log10(value) - m_minlog) * m_ratio + m_minpos; 107 int position = lrintf((log10(value) - m_minlog) * m_ratio) + m_minpos;
110 if (position < m_minpos) position = m_minpos; 108 if (position < m_minpos) position = m_minpos;
111 if (position > m_maxpos) position = m_maxpos; 109 if (position > m_maxpos) position = m_maxpos;
112 // SVDEBUG << "LogRangeMapper::getPositionForValue: " << value << " -> " 110 // cerr << "LogRangeMapper::getPositionForValue: " << value << " -> "
113 // << position << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", ratio " << m_ratio << ", minlog " << m_minlog << ")" << endl; 111 // << position << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", ratio " << m_ratio << ", minlog " << m_minlog << ")" << endl;
114 if (m_inverted) return m_maxpos - position; 112 if (m_inverted) return m_maxpos - (position - m_minpos);
115 else return position; 113 else return position;
116 } 114 }
117 115
118 float 116 float
119 LogRangeMapper::getValueForPosition(int position) const 117 LogRangeMapper::getValueForPosition(int position) const
120 { 118 {
121 if (m_inverted) position = m_maxpos - position; 119 if (m_inverted) position = m_maxpos - (position - m_minpos);
120 if (position < m_minpos) position = m_minpos;
121 if (position > m_maxpos) position = m_maxpos;
122 float value = powf(10, (position - m_minpos) / m_ratio + m_minlog); 122 float value = powf(10, (position - m_minpos) / m_ratio + m_minlog);
123 // SVDEBUG << "LogRangeMapper::getValueForPosition: " << position << " -> " 123 // cerr << "LogRangeMapper::getValueForPosition: " << position << " -> "
124 // << value << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", ratio " << m_ratio << ", minlog " << m_minlog << ")" << endl; 124 // << value << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", ratio " << m_ratio << ", minlog " << m_minlog << ")" << endl;
125 return value; 125 return value;
126 } 126 }
127 127