]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/speedquantizer.cpp
Be more permissive when checking route continuity
[r2c2.git] / source / libr2c2 / speedquantizer.cpp
index 710e1041fb1d1f42a74dee61fe717d84394def9c..f2b14b8a2aa8d6f6d585562739f8c111d548d787 100644 (file)
@@ -1,10 +1,3 @@
-/* $Id$
-
-This file is part of R²C²
-Copyright © 2011  Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
 #include "speedquantizer.h"
 
 using namespace std;
@@ -13,33 +6,36 @@ using namespace Msp;
 namespace R2C2 {
 
 SpeedQuantizer::SpeedQuantizer(unsigned n):
-       steps(n+1)
+       steps(n+1),
+       weight_limit(10)
 {
        if(n<1)
-               throw InvalidParameterValue("Must have at leats one speed step");
+               throw invalid_argument("SpeedQuantizer::SpeedQuantizer");
 }
 
 void SpeedQuantizer::learn(unsigned i, float s, float w)
 {
        if(i>=steps.size())
-               throw InvalidParameterValue("Speed step index out of range");
+               throw out_of_range("SpeedQuantizer::learn");
        steps[i].add(s, w);
 }
 
 float SpeedQuantizer::get_speed(unsigned i) const
 {
+       if(i>=steps.size())
+               throw out_of_range("SpeedQuantizer::get_speed");
        if(i==0)
                return 0;
-       if(steps[i].weight)
+       if(steps[i].weight>=weight_limit)
                return steps[i].speed;
 
        unsigned low;
        unsigned high;
        for(low=i; low>0; --low)
-               if(steps[low].weight)
+               if(steps[low].weight>=weight_limit)
                        break;
        for(high=i; high+1<steps.size(); ++high)
-               if(steps[high].weight)
+               if(steps[high].weight>=weight_limit)
                        break;
 
        if(steps[high].weight)
@@ -69,7 +65,7 @@ unsigned SpeedQuantizer::find_speed_step(float speed) const
        unsigned high = 0;
        unsigned last = 0;
        for(unsigned i=0; (!high && i<steps.size()); ++i)
-               if(steps[i].weight)
+               if(steps[i].weight>=weight_limit)
                {
                        last = i;
                        if(steps[i].speed>=speed)
@@ -88,7 +84,7 @@ unsigned SpeedQuantizer::find_speed_step(float speed) const
                        else
                                return 0;
                }
-               return min(min(static_cast<unsigned>(low*speed/steps[low].speed), steps.size()-1), last+limit);
+               return min(min<unsigned>(low*speed/steps[low].speed, steps.size()-1), last+limit);
        }
 
        float f = (speed-steps[low].speed)/(steps[high].speed-steps[low].speed);