changeset 1408:f89365917d02 scale-ticks

These tests now pass, but there's still a lot to be done
author Chris Cannam
date Wed, 03 May 2017 17:02:01 +0100
parents 25ed6dde2ce0
children 21ba60008200
files base/ScaleTickIntervals.h base/test/TestScaleTickIntervals.h
diffstat 2 files changed, 57 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/base/ScaleTickIntervals.h	Wed May 03 13:02:08 2017 +0100
+++ b/base/ScaleTickIntervals.h	Wed May 03 17:02:01 2017 +0100
@@ -61,9 +61,44 @@
 	    explode(r, t);
 	    return t;
 	}
-	
-	double ilg = log10(inc);
-	int prec = int((ilg > 0.0) ? round(ilg) : trunc(ilg)) - 1;
+
+        double digInc = log10(inc);
+        double digMax = log10(fabs(r.max));
+        double digMin = log10(fabs(r.min));
+
+        int precInc = int(trunc(digInc)) - 1;
+
+        bool fixed = false;
+        if (precInc > -4 && precInc < 4) {
+            fixed = true;
+        } else if ((digMax >= -3.0 && digMax <= 2.0) &&
+                   (digMin >= -3.0 && digMin <= 3.0)) {
+            fixed = true;
+        }
+        
+        int precRange = int(ceil(digMax - digInc));
+
+        int prec = 1;
+        
+        if (fixed) {
+            prec = precInc;
+            if (prec < 0) {
+                prec = -prec;
+            }
+        } else {
+            prec = precRange;
+        }
+
+        std::cerr << "\nmin = " << r.min << ", max = " << r.max << ", n = " << r.n
+                  << ", inc = " << inc << std::endl;
+        std::cerr << "digMax = " << digMax << ", digInc = " << digInc
+                  << std::endl;
+        std::cerr << "fixed = " << fixed << ", inc = " << inc
+                  << ", precInc = " << precInc << ", precRange = " << precRange
+                  << ", prec = " << prec << std::endl;
+        
+//	int prec = int((ilg > 0.0) ? round(ilg) : trunc(ilg)) - 1;
+/*
 	int dp = 0, sf = 0;
 	bool fixed = false;
 	if (prec < 0) {
@@ -78,13 +113,22 @@
 	if (prec > -4 && prec < 4) {
 	    fixed = true;
 	}
+*/
+/*        bool fixed = true;
+        int dp = sig;
+        int sf = prec;
+*/      
+	double roundTo = pow(10.0, precInc);
 
-	double roundTo = pow(10.0, prec);
+        std::cerr << "roundTo = " << roundTo << std::endl;
+        
 	inc = round(inc / roundTo) * roundTo;
+        if (inc < roundTo) inc = roundTo;
+        
 	double min = ceil(r.min / roundTo) * roundTo;
 	if (min > r.max) min = r.max;
 
-	Ticks t { min, inc, roundTo, fixed, fixed ? dp : sf, {} };
+	Ticks t { min, inc, roundTo, fixed, prec, {} };
 	explode(r, t);
 	return t;
     }
@@ -102,7 +146,9 @@
 		     t.precision, value);
 	    return Tick({ value, std::string(buffer) });
 	};
-	for (double value = t.initial; value <= r.max; value += t.spacing) {
+	for (double value = t.initial;
+             value < r.max + t.spacing/2;
+             value += t.spacing) {
 	    value = t.roundTo * round(value / t.roundTo);
 	    t.ticks.push_back(makeTick(value));
 	}
--- a/base/test/TestScaleTickIntervals.h	Wed May 03 13:02:08 2017 +0100
+++ b/base/test/TestScaleTickIntervals.h	Wed May 03 17:02:01 2017 +0100
@@ -47,16 +47,20 @@
     void compareTicks(vector<ScaleTickIntervals::Tick> ticks,
 		      vector<ScaleTickIntervals::Tick> expected)
     {
+        double eps = 1e-7;
 	for (int i = 0; i < int(expected.size()); ++i) {
 	    if (i < int(ticks.size())) {
 		if (ticks[i].label != expected[i].label ||
-		    ticks[i].value != expected[i].value) {
+		    fabs(ticks[i].value - expected[i].value) > eps) {
 		    printDiff(ticks, expected);
 		}
 		QCOMPARE(ticks[i].label, expected[i].label);
 		QCOMPARE(ticks[i].value, expected[i].value);
 	    }
 	}
+        if (ticks.size() != expected.size()) {
+            printDiff(ticks, expected);
+        }
 	QCOMPARE(ticks.size(), expected.size());
     }