diff options
| author | Jacob Palecki <[email protected]> | 2021-04-08 22:23:48 -0700 |
|---|---|---|
| committer | Jacob Palecki <[email protected]> | 2021-04-08 22:23:48 -0700 |
| commit | 0bf728ac4f0443cb1093d7eb3d2635bf068182b0 (patch) | |
| tree | 93fb9cc0c96cd1bb162b25a1adf825d389195802 /common | |
| parent | Progress in arbitrary (diff) | |
| parent | make it safe (diff) | |
| download | rawaccel-0bf728ac4f0443cb1093d7eb3d2635bf068182b0.tar.xz rawaccel-0bf728ac4f0443cb1093d7eb3d2635bf068182b0.zip | |
merge and improve
Diffstat (limited to 'common')
| -rw-r--r-- | common/accel-lookup.hpp | 113 | ||||
| -rw-r--r-- | common/accel-natural.hpp | 4 | ||||
| -rw-r--r-- | common/rawaccel-base.hpp | 2 | ||||
| -rw-r--r-- | common/rawaccel-validate.hpp | 2 |
4 files changed, 69 insertions, 52 deletions
diff --git a/common/accel-lookup.hpp b/common/accel-lookup.hpp index be107f8..f70f2b1 100644 --- a/common/accel-lookup.hpp +++ b/common/accel-lookup.hpp @@ -3,6 +3,8 @@ #include "rawaccel-base.hpp" #include "utility.hpp" +#include <math.h> + namespace rawaccel { struct linear_range { @@ -55,7 +57,7 @@ namespace rawaccel { template <typename Lookup> struct lut_base { - enum { capacity = LUT_CAPACITY }; + enum { capacity = SPACED_LUT_CAPACITY }; using value_t = float; template <typename Func> @@ -150,20 +152,22 @@ namespace rawaccel { }; struct si_pair { - double slope = 0; - double intercept = 0; + float slope = 0; + float intercept = 0; }; struct arbitrary_lut_point { - double applicable_speed = 0; + float applicable_speed = 0; si_pair slope_intercept = {}; }; struct arbitrary_lut { + enum { capacity = SPACED_LUT_CAPACITY / 4 }; + fp_rep_range range; - arbitrary_lut_point data[LUT_CAPACITY] = {}; - int log_lookup[LUT_CAPACITY] = {}; - float raw_data_in[LUT_CAPACITY * 2] = {}; +; + arbitrary_lut_point data[capacity] = {}; + int log_lookup[capacity] = {}; double first_point_speed; double last_point_speed; int last_arbitrary_index; @@ -172,27 +176,34 @@ namespace rawaccel { double operator()(double speed) const { int index = 0; + int last_arb_index = last_arbitrary_index; + int last_log_index = last_log_lookup_index; - if (speed < first_point_speed) - { - // Apply from 0 index - } - else if (speed > last_point_speed) - { - index = last_arbitrary_index; - } - else if (speed > range.stop) - { - index = search_from(log_lookup[last_log_lookup_index], speed); - } - else if (speed < range.start) + if (unsigned(last_arb_index) < capacity && + unsigned(last_log_index) < capacity && + speed > first_point_speed) { - index = search_from(0, speed); - } - else - { - int log_lookup = get_log_index(speed); - index = search_from(log_lookup, speed); + if (speed > last_point_speed) + { + index = last_arb_index; + } + else if (speed > range.stop) + { + int last_log = log_lookup[last_log_index]; + if (unsigned(last_log) >= capacity) return 1; + index = search_from(last_log, last_arb_index, speed); + } + else if (speed < range.start) + { + index = search_from(0, last_arb_index, speed); + } + else + { + int log_index = get_log_index(speed); + if (unsigned(log_index) >= capacity) return 1; + index = search_from(log_index, last_arb_index, speed); + } + } return apply(index, speed); @@ -205,7 +216,7 @@ namespace rawaccel { return index; } - int inline search_from(int index, double speed) const + int inline search_from(int index, int last, double speed) const { int prev_index; @@ -214,7 +225,7 @@ namespace rawaccel { prev_index = index; index++; } - while (index <= last_arbitrary_index && data[index].applicable_speed < speed); + while (index <= last && data[index].applicable_speed < speed); index--; @@ -227,10 +238,22 @@ namespace rawaccel { return pair.slope + pair.intercept / speed; } - void fill(float* points, int length) + + void fill(vec2<float>* points, int length) { - vec2d current = {0, 0}; - vec2d next; + first_point_speed = points[0].x; + // -2 because the last index in the arbitrary array is used for slope-intercept only + last_arbitrary_index = length - 2; + last_point_speed = points[last_arbitrary_index].x; + + int start = static_cast<int>(floor(log(first_point_speed))); + int end = static_cast<int>(floor(log(last_point_speed))); + int num = static_cast<int>(capacity / (end - start)); + range = fp_rep_range{ start, end, num }; + last_log_lookup_index = num * (end - start) - 1; + + vec2<float> current = {0, 0}; + vec2<float> next; int log_index = 0; double log_inner_iterator = range.start; double log_inner_slice = 1 / range.num; @@ -238,17 +261,23 @@ namespace rawaccel { for (int i = 0; i < length; i++) { - next = vec2d{ points[i * 2], points[i * 2 + 1] }; + next = points[i]; double slope = (next.y - current.y) / (next.x - current.x); double intercept = next.y - slope * next.x; - si_pair current_si = { slope, intercept }; - arbitrary_lut_point current_lut_point = { next.x, current_si }; + si_pair current_si = { + static_cast<float>(slope), + static_cast<float>(intercept) + }; + arbitrary_lut_point current_lut_point = { + static_cast<float>(next.x), + current_si + }; this->data[i] = current_lut_point; while (log_value < next.x) { - this->log_lookup[log_index] = log_value; + this->log_lookup[log_index] = static_cast<int>(log_value); log_index++; log_inner_iterator += log_inner_slice; log_value = pow(2, log_inner_iterator); @@ -256,20 +285,8 @@ namespace rawaccel { } } - arbitrary_lut(const table_args &args) + arbitrary_lut(const accel_args&) { - first_point_speed = raw_data_in[0]; - // -2 because the last index in the arbitrary array is used for slope-intercept only - last_arbitrary_index = (length - 2)*2; - last_point_speed = raw_data_in[last_arbitrary_index]; - - double start = (int)floor(log(first_point_speed)); - double end = (int)floor(log(last_point_speed)); - double num = (int)floor(LUT_CAPACITY / (end - start)); - range = fp_rep_range{ start, end, num }; - last_log_lookup_index = num * (end - start) - 1; - - fill(raw_data_in, length); } }; } diff --git a/common/accel-natural.hpp b/common/accel-natural.hpp index 8d25351..9f76d1a 100644 --- a/common/accel-natural.hpp +++ b/common/accel-natural.hpp @@ -28,7 +28,7 @@ namespace rawaccel { double offset_x = offset - x; double decay = exp(accel * offset_x); - return limit * (1 - (decay * offset_x + offset) / x) + 1; + return limit * (1 - (offset - decay * offset_x) / x) + 1; } using natural_base::natural_base; @@ -43,7 +43,7 @@ namespace rawaccel { double offset_x = offset - x; double decay = exp(accel * offset_x); - double output = limit * (offset_x + decay / accel) + constant; + double output = limit * (decay / accel - offset_x) + constant; return output / x + 1; } diff --git a/common/rawaccel-base.hpp b/common/rawaccel-base.hpp index dde56f5..2f49ec0 100644 --- a/common/rawaccel-base.hpp +++ b/common/rawaccel-base.hpp @@ -15,7 +15,7 @@ namespace rawaccel { inline constexpr size_t MAX_DEV_ID_LEN = 200; - inline constexpr size_t LUT_CAPACITY = 1025; + inline constexpr size_t SPACED_LUT_CAPACITY = 1025; inline constexpr double MAX_NORM = 16; inline constexpr double PI = 3.14159265358979323846; diff --git a/common/rawaccel-validate.hpp b/common/rawaccel-validate.hpp index 4f7dd9c..ef6f667 100644 --- a/common/rawaccel-validate.hpp +++ b/common/rawaccel-validate.hpp @@ -27,7 +27,7 @@ namespace rawaccel { }; auto check_accel = [&error](const accel_args& args) { - static_assert(LUT_CAPACITY == 1025, "update error msg"); + static_assert(SPACED_LUT_CAPACITY == 1025, "update error msg"); const auto& lut_args = args.lut_args; |