summaryrefslogtreecommitdiff
path: root/common/accel-motivity.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'common/accel-motivity.hpp')
-rw-r--r--common/accel-motivity.hpp117
1 files changed, 36 insertions, 81 deletions
diff --git a/common/accel-motivity.hpp b/common/accel-motivity.hpp
index 246cf37..0efe7ea 100644
--- a/common/accel-motivity.hpp
+++ b/common/accel-motivity.hpp
@@ -1,101 +1,56 @@
#pragma once
-#include <math.h>
-
-#include "accel-base.hpp"
+#include "accel-lookup.hpp"
-#define RA_LOOKUP
+#include <math.h>
namespace rawaccel {
- constexpr size_t LUT_SIZE = 601;
-
- struct si_pair {
- double slope = 0;
- double intercept = 0;
- };
-
- /// <summary> Struct to hold sigmoid (s-shaped) gain implementation. </summary>
- struct motivity_impl {
- double rate;
- double limit;
+ struct sigmoid {
+ double accel;
+ double motivity;
double midpoint;
- double subtractive_constant;
+ double constant;
- motivity_impl(const accel_args& args) :
- rate(pow(10,args.accel)), limit(2*log10(args.limit)), midpoint(log10(args.midpoint))
- {
- subtractive_constant = limit / 2;
- }
+ sigmoid(const accel_args& args) :
+ accel(exp(args.growth_rate)),
+ motivity(2 * log(args.motivity)),
+ midpoint(log(args.midpoint)),
+ constant(-motivity / 2) {}
- inline double operator()(double speed) const {
- double log_speed = log10(speed);
- return pow(10, limit / (exp(-rate * (log_speed - midpoint)) + 1) - subtractive_constant);
-
- }
-
- inline double legacy_offset(double speed) const { return operator()(speed); }
-
- inline double apply(si_pair* lookup, double speed) const
+ double operator()(double x) const
{
- si_pair pair = lookup[map(speed)];
- return pair.slope + pair.intercept / speed;
+ double denom = exp(accel * (midpoint - log(x))) + 1;
+ return exp(motivity / denom + constant);
}
+ };
- inline int map(double speed) const
- {
- int index = speed > 0 ? (int)(100 * log10(speed) + 201) : 0;
-
- if (index < 0) return 0;
- if (index >= LUT_SIZE) return LUT_SIZE - 1;
+ /// <summary> Struct to hold sigmoid (s-shaped) gain implementation. </summary>
+ struct motivity : binlog_lut {
- return index;
- }
+ using binlog_lut::operator();
- inline void fill(si_pair* lookup) const
+ motivity(const accel_args& args) :
+ binlog_lut(args)
{
- double lookup_speed = 0;
- double integral_interval = 0;
- double gain_integral_speed = 0;
- double gain_integral_speed_prev = 0;
- double gain = 0;
- double intercept = 0;
- double output = 0;
- double output_prev = 0;
- double x = -2;
-
- double logarithm_interval = 0.01;
- double integral_intervals_per_speed = 10;
- double integral_interval_factor = pow(10, logarithm_interval) / integral_intervals_per_speed;
-
- lookup[0] = {};
-
- for (size_t i = 1; i < LUT_SIZE; i++)
- {
- x += logarithm_interval;
-
- // Each lookup speed will be 10^0.01 = 2.33% higher than the previous
- // To get 10 integral intervals per speed, set interval to 0.233%
- lookup_speed = pow(10, x);
- integral_interval = lookup_speed * integral_interval_factor;
-
- while (gain_integral_speed < lookup_speed)
- {
- output_prev = output;
- gain_integral_speed_prev = gain_integral_speed;
- gain_integral_speed += integral_interval;
- gain = operator()(gain_integral_speed);
- output += gain * integral_interval;
+ double sum = 0;
+ double a = 0;
+ auto sigmoid_sum = [&, sig = sigmoid(args)](double b) mutable {
+ double interval = (b - a) / args.spaced_args.partitions;
+ for (int i = 1; i <= args.spaced_args.partitions; i++) {
+ sum += sig(a + i * interval) * interval;
}
-
- intercept = output_prev - gain_integral_speed_prev * gain;
-
- lookup[i] = { gain, intercept };
- }
-
+ a = b;
+ return sum;
+ };
+
+ fill([&](double x) {
+ double y = sigmoid_sum(x);
+ if (!this->transfer) y /= x;
+ return y;
+ });
}
- };
- using accel_motivity = nonadditive_accel<motivity_impl>;
+ };
}