diff base/RangeMapper.cpp @ 384:6f6ab834449d spectrogram-cache-rejig

* Merge from trunk
author Chris Cannam
date Wed, 27 Feb 2008 11:59:42 +0000
parents 2b40f83e7627
children
line wrap: on
line diff
--- a/base/RangeMapper.cpp	Thu Nov 15 14:03:56 2007 +0000
+++ b/base/RangeMapper.cpp	Wed Feb 27 11:59:42 2008 +0000
@@ -60,29 +60,52 @@
 }
 
 LogRangeMapper::LogRangeMapper(int minpos, int maxpos,
-                               float ratio, float minlog,
+                               float minval, float maxval,
                                QString unit) :
     m_minpos(minpos),
     m_maxpos(maxpos),
-    m_ratio(ratio),
-    m_minlog(minlog),
     m_unit(unit)
 {
+    convertMinMax(minpos, maxpos, minval, maxval, m_minlog, m_ratio);
+
+    std::cerr << "LogRangeMapper: minpos " << minpos << ", maxpos "
+              << maxpos << ", minval " << minval << ", maxval "
+              << maxval << ", minlog " << m_minlog << ", ratio " << m_ratio
+              << ", unit " << unit.toStdString() << std::endl;
+
     assert(m_maxpos != m_minpos);
 
     m_maxlog = (m_maxpos - m_minpos) / m_ratio + m_minlog;
 }
 
+void
+LogRangeMapper::convertMinMax(int minpos, int maxpos,
+                              float minval, float maxval,
+                              float &minlog, float &ratio)
+{
+    static float thresh = powf(10, -10);
+    if (minval < thresh) minval = thresh;
+    minlog = log10f(minval);
+    ratio = (maxpos - minpos) / (log10f(maxval) - minlog);
+}
+
+void
+LogRangeMapper::convertRatioMinLog(float ratio, float minlog,
+                                   int minpos, int maxpos,
+                                   float &minval, float &maxval)
+{
+    minval = powf(10, minlog);
+    maxval = powf(10, (maxpos - minpos) / ratio + minlog);
+}
+
 int
 LogRangeMapper::getPositionForValue(float value) const
 {
-    float mapped = m_ratio * log10(value);
-    int position = lrintf(((mapped - m_minlog) / (m_maxlog - m_minlog))
-                          * (m_maxpos - m_minpos));
+    int position = (log10(value) - m_minlog) * m_ratio + m_minpos;
     if (position < m_minpos) position = m_minpos;
     if (position > m_maxpos) position = m_maxpos;
-//    std::cerr << "LogRangeMapper::getPositionForValue: " << value << " -> "
-//              << position << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", ratio " << m_ratio << ", minlog " << m_minlog << ")" << std::endl;
+    std::cerr << "LogRangeMapper::getPositionForValue: " << value << " -> "
+              << position << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", ratio " << m_ratio << ", minlog " << m_minlog << ")" << std::endl;
     return position;
 }
 
@@ -90,8 +113,8 @@
 LogRangeMapper::getValueForPosition(int position) const
 {
     float value = powf(10, (position - m_minpos) / m_ratio + m_minlog);
-//    std::cerr << "LogRangeMapper::getValueForPosition: " << position << " -> "
-//              << value << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", ratio " << m_ratio << ", minlog " << m_minlog << ")" << std::endl;
+    std::cerr << "LogRangeMapper::getValueForPosition: " << position << " -> "
+              << value << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", ratio " << m_ratio << ", minlog " << m_minlog << ")" << std::endl;
     return value;
 }