From 3226272a2d49bcbaec72ec735c22b4e0cc2a5c57 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Mon, 27 Jul 2020 19:31:09 -0700 Subject: skeleton --- common/rawaccel.hpp | 192 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 137 insertions(+), 55 deletions(-) (limited to 'common/rawaccel.hpp') diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp index 095af76..f7f9df7 100644 --- a/common/rawaccel.hpp +++ b/common/rawaccel.hpp @@ -5,6 +5,7 @@ #include "vec2.h" #include "x64-util.hpp" +#include "external/tagged-union-single.h" namespace rawaccel { @@ -55,8 +56,119 @@ struct accel_scale_clamp { void error(const char*); +using milliseconds = double; + +struct args_t { + mode accel_mode = mode::noaccel; + milliseconds time_min = 0.4; + double offset = 0; + double accel = 0; + double lim_exp = 2; + double midpoint = 0; + vec2d weight = { 1, 1 }; + vec2d cap = { 0, 0 }; +}; + +struct accel_implentation { + double b = 0; + double k = 0; + double m = 0; + double offset = 0; + + accel_implentation(args_t args) + { + b = args.accel; + k = args.lim_exp; + m = args.midpoint; + offset = args.offset; + } + + double virtual accelerate(double speed) { return 0; } + void virtual verify(args_t args) { + if (args.accel < 0) error("accel can not be negative, use a negative weight to compensate"); + if (args.time_min <= 0) error("min time must be positive"); + } + + accel_implentation() = default; +}; + +struct accel_linear : accel_implentation { + accel_linear(args_t args) + : accel_implentation(args) {} + + double virtual accelerate(double speed) override { + return b * speed; + } + + void virtual verify(args_t args) override { + accel_implentation::verify(args); + if (args.lim_exp <= 1) error("limit must be greater than 1"); + } +}; + +struct accel_classic : accel_implentation { + accel_classic(args_t args) + : accel_implentation(args) {} + + double virtual accelerate(double speed) override { + return pow(b * speed, k); + + } + + void virtual verify(args_t args) override { + accel_implentation::verify(args); + if (args.lim_exp <= 1) error("exponent must be greater than 1"); + } +}; + +struct accel_logarithmic : accel_implentation { + accel_logarithmic(args_t args) + : accel_implentation(args) {} + + double virtual accelerate(double speed) override { + return log(speed * b + 1); + } + + void virtual verify(args_t args) override { + accel_implentation::verify(args); + if (args.lim_exp <= 1) error("exponent must be greater than 1"); + } +}; + +struct accel_sigmoid : accel_implentation { + accel_sigmoid(args_t args) + : accel_implentation(args) {} + + double virtual accelerate(double speed) override { + return k / (exp(-b * (speed - m)) + 1); + + } + + void virtual verify(args_t args) override { + accel_implentation::verify(args); + if (args.lim_exp <= 1) error("exponent must be greater than 1"); + } +}; + +struct accel_power : accel_implentation { + accel_power(args_t args) + : accel_implentation(args) {} + + double virtual accelerate(double speed) override { + return (offset > 0 && speed < 1) ? 0 : pow(speed*b, k) - 1; + + + } + + void virtual verify(args_t args) override { + accel_implentation::verify(args); + if (args.lim_exp <= 1) error("exponent must be greater than 1"); + } +}; + +using accel_implementation_t = tagged_union; + struct accel_function { - using milliseconds = double; /* This value is ideally a few microseconds lower than @@ -77,63 +189,13 @@ struct accel_function { // or the exponent for classic and power modes double k = 1; + accel_implementation_t accel = accel_implentation{}; + vec2d weight = { 1, 1 }; vec2 clamp; - inline vec2d operator()(const vec2d& input, milliseconds time, mode accel_mode) const { - double mag = sqrtsd(input.x * input.x + input.y * input.y); - double time_clamped = clampsd(time, time_min, 100); - double speed = maxsd(mag / time_clamped - speed_offset, 0); - - double accel_val = 0; - - switch (accel_mode) { - case mode::linear: accel_val = b * speed; - break; - case mode::classic: accel_val = pow(b * speed, k); - break; - case mode::natural: accel_val = k - (k * exp(-b * speed)); - break; - case mode::logarithmic: accel_val = log(speed * b + 1); - break; - case mode::sigmoid: accel_val = k / (exp(-b * (speed - m)) + 1); - break; - case mode::power: accel_val = (speed_offset > 0 && speed < 1) ? 0 : pow(speed*b, k) - 1; - break; - default: - break; - } - - double scale_x = weight.x * accel_val + 1; - double scale_y = weight.y * accel_val + 1; - - return { - input.x * clamp.x(scale_x), - input.y * clamp.y(scale_y) - }; - } - - struct args_t { - mode accel_mode = mode::noaccel; - milliseconds time_min = 0.4; - double offset = 0; - double accel = 0; - double lim_exp = 2; - double midpoint = 0; - vec2d weight = { 1, 1 }; - vec2d cap = { 0, 0 }; - }; - accel_function(args_t args) { - // Preconditions to guard against division by zero and - // ensure the C math functions can not return NaN or -Inf. - if (args.accel < 0) error("accel can not be negative, use a negative weight to compensate"); - if (args.time_min <= 0) error("min time must be positive"); - if (args.lim_exp <= 1) { - if (args.accel_mode == mode::classic) error("exponent must be greater than 1"); - if (args.accel_mode == mode::power) { if (args.lim_exp <=0 ) error("exponent must be greater than 0"); } - else error("limit must be greater than 1"); - } + accel = accel_linear(args); time_min = args.time_min; m = args.midpoint; @@ -148,6 +210,26 @@ struct accel_function { clamp.y = accel_scale_clamp(args.cap.y); } + double apply(double speed) const { + return accel.visit([=](auto accel_t) { return accel_t.accelerate(speed); }); + } + + inline vec2d operator()(const vec2d& input, milliseconds time, mode accel_mode) const { + double mag = sqrtsd(input.x * input.x + input.y * input.y); + double time_clamped = clampsd(time, time_min, 100); + double speed = maxsd(mag / time_clamped - speed_offset, 0); + + double accel_val = apply(speed); + + double scale_x = weight.x * accel_val + 1; + double scale_y = weight.y * accel_val + 1; + + return { + input.x * clamp.x(scale_x), + input.y * clamp.y(scale_y) + }; + } + accel_function() = default; }; @@ -159,7 +241,7 @@ struct variables { accel_function accel_fn; vec2d sensitivity = { 1, 1 }; - variables(double degrees, vec2d sens, accel_function::args_t accel_args) + variables(double degrees, vec2d sens, args_t accel_args) : accel_fn(accel_args) { apply_rotate = degrees != 0; -- cgit v1.2.3 From 894a13926f79e209e3b619012cd2bb6961d959e2 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Tue, 28 Jul 2020 01:53:32 -0700 Subject: Further refactoring and comments --- common/rawaccel.hpp | 546 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 311 insertions(+), 235 deletions(-) (limited to 'common/rawaccel.hpp') diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp index f7f9df7..3380e4c 100644 --- a/common/rawaccel.hpp +++ b/common/rawaccel.hpp @@ -7,256 +7,332 @@ #include "x64-util.hpp" #include "external/tagged-union-single.h" -namespace rawaccel { +namespace rawaccel { -enum class mode { noaccel, linear, classic, natural, logarithmic, sigmoid, power }; + enum class mode { noaccel, linear, classic, natural, logarithmic, sigmoid, power }; -struct rotator { - vec2d rot_vec = { 1, 0 }; + struct rotator { + vec2d rot_vec = { 1, 0 }; - inline vec2d operator()(const vec2d& input) const { - return { - input.x * rot_vec.x - input.y * rot_vec.y, - input.x * rot_vec.y + input.y * rot_vec.x - }; - } + inline vec2d operator()(const vec2d& input) const { + return { + input.x * rot_vec.x - input.y * rot_vec.y, + input.x * rot_vec.y + input.y * rot_vec.x + }; + } - rotator(double degrees) { - double rads = degrees * M_PI / 180; - rot_vec = { cos(rads), sin(rads) }; - } + rotator(double degrees) { + double rads = degrees * M_PI / 180; + rot_vec = { cos(rads), sin(rads) }; + } - rotator() = default; -}; + rotator() = default; + }; -struct accel_scale_clamp { - double lo = 0; - double hi = 9; + struct accel_scale_clamp { + double lo = 0; + double hi = 9; - inline double operator()(double scale) const { - return clampsd(scale, lo, hi); - } + inline double operator()(double scale) const { + return clampsd(scale, lo, hi); + } - accel_scale_clamp(double cap) : accel_scale_clamp() { - if (cap <= 0) { - // use default, effectively uncapped accel - return; + accel_scale_clamp(double cap) : accel_scale_clamp() { + if (cap <= 0) { + // use default, effectively uncapped accel + return; + } + + if (cap < 1) { + // assume negative accel + lo = cap; + hi = 1; + } + else hi = cap; } - if (cap < 1) { - // assume negative accel - lo = cap; - hi = 1; + accel_scale_clamp() = default; + }; + +#ifdef _KERNEL_MODE + void error(const char*) {} +#else + void error(const char* s); +#endif + + using milliseconds = double; + + struct args_t { + mode accel_mode = mode::noaccel; + milliseconds time_min = 0.4; + double offset = 0; + double accel = 0; + double lim_exp = 2; + double midpoint = 0; + vec2d weight = { 1, 1 }; + vec2d cap = { 0, 0 }; + }; + + /// + /// Struct to hold acceleration implementation details. + /// + /// Type of acceleration. + template + struct accel_implentation { + + /// The acceleration ramp rate. + double b = 0; + + /// The limit or exponent for various modes. + double k = 0; + + /// The midpoint in sigmoid mode. + double m = 0; + + /// The offset past which acceleration is applied. Used in power mode. + double offset = 0; + + /// + /// Initializes a new instance of the struct. + /// + /// + /// + accel_implentation(args_t args) + { + b = args.accel; + k = args.lim_exp - 1; + m = args.midpoint; + offset = args.offset; + } + + /// + /// Returns accelerated value of speed as a ratio of magnitude. + /// + /// Mouse speed at which to calculate acceleration. + /// Ratio of accelerated movement magnitude to input movement magnitude. + double accelerate(double speed) { return 0; } + + /// + /// Verifies arguments as valid. Errors if not. + /// + /// Arguments to verified. + void verify(args_t args) { + if (args.accel < 0) error("accel can not be negative, use a negative weight to compensate"); + if (args.time_min <= 0) error("min time must be positive"); } - else hi = cap; - } - - accel_scale_clamp() = default; -}; - -void error(const char*); - -using milliseconds = double; - -struct args_t { - mode accel_mode = mode::noaccel; - milliseconds time_min = 0.4; - double offset = 0; - double accel = 0; - double lim_exp = 2; - double midpoint = 0; - vec2d weight = { 1, 1 }; - vec2d cap = { 0, 0 }; -}; - -struct accel_implentation { - double b = 0; - double k = 0; - double m = 0; - double offset = 0; - - accel_implentation(args_t args) - { - b = args.accel; - k = args.lim_exp; - m = args.midpoint; - offset = args.offset; - } - - double virtual accelerate(double speed) { return 0; } - void virtual verify(args_t args) { - if (args.accel < 0) error("accel can not be negative, use a negative weight to compensate"); - if (args.time_min <= 0) error("min time must be positive"); - } - - accel_implentation() = default; -}; - -struct accel_linear : accel_implentation { - accel_linear(args_t args) - : accel_implentation(args) {} - - double virtual accelerate(double speed) override { - return b * speed; - } - - void virtual verify(args_t args) override { - accel_implentation::verify(args); - if (args.lim_exp <= 1) error("limit must be greater than 1"); - } -}; - -struct accel_classic : accel_implentation { - accel_classic(args_t args) - : accel_implentation(args) {} - - double virtual accelerate(double speed) override { - return pow(b * speed, k); - - } - - void virtual verify(args_t args) override { - accel_implentation::verify(args); - if (args.lim_exp <= 1) error("exponent must be greater than 1"); - } -}; - -struct accel_logarithmic : accel_implentation { - accel_logarithmic(args_t args) - : accel_implentation(args) {} - - double virtual accelerate(double speed) override { - return log(speed * b + 1); - } - - void virtual verify(args_t args) override { - accel_implentation::verify(args); - if (args.lim_exp <= 1) error("exponent must be greater than 1"); - } -}; - -struct accel_sigmoid : accel_implentation { - accel_sigmoid(args_t args) - : accel_implentation(args) {} - - double virtual accelerate(double speed) override { - return k / (exp(-b * (speed - m)) + 1); - - } - - void virtual verify(args_t args) override { - accel_implentation::verify(args); - if (args.lim_exp <= 1) error("exponent must be greater than 1"); - } -}; - -struct accel_power : accel_implentation { - accel_power(args_t args) - : accel_implentation(args) {} - - double virtual accelerate(double speed) override { - return (offset > 0 && speed < 1) ? 0 : pow(speed*b, k) - 1; - - - } - - void virtual verify(args_t args) override { - accel_implentation::verify(args); - if (args.lim_exp <= 1) error("exponent must be greater than 1"); - } -}; - -using accel_implementation_t = tagged_union; - -struct accel_function { - - /* - This value is ideally a few microseconds lower than - the user's mouse polling interval, though it should - not matter if the system is stable. - */ - milliseconds time_min = 0.4; - - double speed_offset = 0; - // speed midpoint in sigmoid mode - double m = 0; + /// + /// + /// + /// + accel_implentation() = default; + }; + + /// Struct to hold linear acceleration implementation. + struct accel_linear : accel_implentation { + + accel_linear(args_t args) + : accel_implentation(args) {} + + double accelerate(double speed) { + //f(x) = mx + return b * speed; + } + + void verify(args_t args) { + accel_implentation::verify(args); + if (args.lim_exp <= 1) error("limit must be greater than 1"); + } + }; - // accel ramp rate - double b = 0; + /// Struct to hold "classic" (linear raised to power) acceleration implementation. + struct accel_classic : accel_implentation { + accel_classic(args_t args) + : accel_implentation(args) {} + + double accelerate(double speed) { + //f(x) = (mx)^k + return pow(b * speed, k); + } + + void verify(args_t args) { + accel_implentation::verify(args); + if (args.lim_exp <= 1) error("exponent must be greater than 1"); + } + }; + + /// Struct to hold "natural" (vanishing difference) acceleration implementation. + struct accel_natural : accel_implentation { + accel_natural(args_t args) + : accel_implentation(args) + { b /= k; } + + double accelerate(double speed) { + // f(x) = k(1-e^(-mx)) + return k - (k * exp(-b * speed));; + } + + void verify(args_t args) { + accel_implentation::verify(args); + if (args.lim_exp <= 1) error("exponent must be greater than 1"); + } + }; + + /// Struct to hold logarithmic acceleration implementation. + struct accel_logarithmic : accel_implentation { + accel_logarithmic(args_t args) + : accel_implentation(args) {} + + double accelerate(double speed) { + return log(speed * b + 1); + } - // the limit for natural and sigmoid modes - // or the exponent for classic and power modes - double k = 1; + void verify(args_t args) { + accel_implentation::verify(args); + if (args.lim_exp <= 1) error("exponent must be greater than 1"); + } + }; + + /// Struct to hold sigmoid (s-shaped) acceleration implementation. + struct accel_sigmoid : accel_implentation { + accel_sigmoid(args_t args) + : accel_implentation(args) {} + + double accelerate(double speed) { + //f(x) = k/(1+e^(-m(c-x))) + return k / (exp(-b * (speed - m)) + 1); + } + + void verify(args_t args) { + accel_implentation::verify(args); + if (args.lim_exp <= 1) error("exponent must be greater than 1"); + } + }; + + /// Struct to hold power (non-additive) acceleration implementation. + struct accel_power : accel_implentation { + accel_power(args_t args) + : accel_implentation(args) + { k++; } + + double accelerate(double speed) { + //f(x) = (mx)^k - 1 + // The subtraction of 1 is with later addition of 1 in mind, + // so that the input vector is directly multiplied by (mx)^k (if unweighted) + return (offset > 0 && speed < 1) ? 0 : pow(speed * b, k) - 1; + } + + void verify(args_t args) { + accel_implentation::verify(args); + if (args.lim_exp <= 0) error("exponent must be greater than 0"); + } + }; + + /// Struct to hold acceleration implementation which applies no acceleration. + struct accel_noaccel : accel_implentation { + accel_noaccel(args_t args) + : accel_implentation(args) {} + + double accelerate(double speed) { return 0; } + + void verify(args_t args) {} + }; - accel_implementation_t accel = accel_implentation{}; + using accel_implementation_t = tagged_union; + + struct accel_function { + + /* + This value is ideally a few microseconds lower than + the user's mouse polling interval, though it should + not matter if the system is stable. + */ + milliseconds time_min = 0.4; + + /// The offset past which acceleration is applied. + double speed_offset = 0; + + accel_implementation_t accel; + + vec2d weight = { 1, 1 }; + vec2 clamp; + + accel_function(args_t args) { + switch (args.accel_mode) + { + case mode::linear: accel = accel_linear(args); + break; + case mode::classic: accel = accel_classic(args); + break; + case mode::natural: accel = accel_natural(args); + break; + case mode::logarithmic: accel = accel_logarithmic(args); + break; + case mode::sigmoid: accel = accel_sigmoid(args); + break; + case mode::power: accel = accel_power(args); + } + + verify(args); + + time_min = args.time_min; + speed_offset = args.offset; + weight = args.weight; + clamp.x = accel_scale_clamp(args.cap.x); + clamp.y = accel_scale_clamp(args.cap.y); + } + + double apply(double speed) const { + return accel.visit([=](auto accel_t) { return accel_t.accelerate(speed); }); + } - vec2d weight = { 1, 1 }; - vec2 clamp; + void verify(args_t args) const { + return accel.visit([=](auto accel_t) { accel_t.verify(args); }); + } - accel_function(args_t args) { - accel = accel_linear(args); + inline vec2d operator()(const vec2d& input, milliseconds time, mode accel_mode) const { + double mag = sqrtsd(input.x * input.x + input.y * input.y); + double time_clamped = clampsd(time, time_min, 100); + double speed = maxsd(mag / time_clamped - speed_offset, 0); - time_min = args.time_min; - m = args.midpoint; - b = args.accel; - k = args.lim_exp - 1; - if (args.accel_mode == mode::natural) b /= k; - if (args.accel_mode == mode::power) k++; - - speed_offset = args.offset; - weight = args.weight; - clamp.x = accel_scale_clamp(args.cap.x); - clamp.y = accel_scale_clamp(args.cap.y); - } - - double apply(double speed) const { - return accel.visit([=](auto accel_t) { return accel_t.accelerate(speed); }); - } - - inline vec2d operator()(const vec2d& input, milliseconds time, mode accel_mode) const { - double mag = sqrtsd(input.x * input.x + input.y * input.y); - double time_clamped = clampsd(time, time_min, 100); - double speed = maxsd(mag / time_clamped - speed_offset, 0); - - double accel_val = apply(speed); - - double scale_x = weight.x * accel_val + 1; - double scale_y = weight.y * accel_val + 1; - - return { - input.x * clamp.x(scale_x), - input.y * clamp.y(scale_y) - }; - } - - accel_function() = default; -}; - -struct variables { - bool apply_rotate = false; - bool apply_accel = false; - mode accel_mode = mode::noaccel; - rotator rotate; - accel_function accel_fn; - vec2d sensitivity = { 1, 1 }; - - variables(double degrees, vec2d sens, args_t accel_args) - : accel_fn(accel_args) - { - apply_rotate = degrees != 0; - if (apply_rotate) rotate = rotator(degrees); - else rotate = rotator(); - - apply_accel = accel_args.accel_mode != mode::noaccel; - accel_mode = accel_args.accel_mode; - - if (sens.x == 0) sens.x = 1; - if (sens.y == 0) sens.y = 1; - sensitivity = sens; - } - - variables() = default; -}; - -} // rawaccel + double accel_val = apply(speed); + + double scale_x = weight.x * accel_val + 1; + double scale_y = weight.y * accel_val + 1; + + return { + input.x * clamp.x(scale_x), + input.y * clamp.y(scale_y) + }; + } + + accel_function() = default; + }; + + struct variables { + bool apply_rotate = false; + bool apply_accel = false; + mode accel_mode = mode::noaccel; + rotator rotate; + accel_function accel_fn; + vec2d sensitivity = { 1, 1 }; + + variables(double degrees, vec2d sens, args_t accel_args) + : accel_fn(accel_args) + { + apply_rotate = degrees != 0; + if (apply_rotate) rotate = rotator(degrees); + else rotate = rotator(); + + apply_accel = accel_args.accel_mode != mode::noaccel; + accel_mode = accel_args.accel_mode; + + if (sens.x == 0) sens.x = 1; + if (sens.y == 0) sens.y = 1; + sensitivity = sens; + } + + variables() = default; + }; + +} // rawaccel \ No newline at end of file -- cgit v1.2.3 From f7a1883e4d26ebb2904518236656d53d8b5645b7 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Tue, 28 Jul 2020 02:09:21 -0700 Subject: Remove extra mode from variables --- common/rawaccel.hpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'common/rawaccel.hpp') diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp index 3380e4c..28d9ee2 100644 --- a/common/rawaccel.hpp +++ b/common/rawaccel.hpp @@ -290,7 +290,7 @@ namespace rawaccel { return accel.visit([=](auto accel_t) { accel_t.verify(args); }); } - inline vec2d operator()(const vec2d& input, milliseconds time, mode accel_mode) const { + inline vec2d operator()(const vec2d& input, milliseconds time) const { double mag = sqrtsd(input.x * input.x + input.y * input.y); double time_clamped = clampsd(time, time_min, 100); double speed = maxsd(mag / time_clamped - speed_offset, 0); @@ -312,7 +312,6 @@ namespace rawaccel { struct variables { bool apply_rotate = false; bool apply_accel = false; - mode accel_mode = mode::noaccel; rotator rotate; accel_function accel_fn; vec2d sensitivity = { 1, 1 }; @@ -325,7 +324,6 @@ namespace rawaccel { else rotate = rotator(); apply_accel = accel_args.accel_mode != mode::noaccel; - accel_mode = accel_args.accel_mode; if (sens.x == 0) sens.x = 1; if (sens.y == 0) sens.y = 1; -- cgit v1.2.3 From b1ef35050600978318581586691f5a8f119abf5b Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Tue, 28 Jul 2020 02:44:09 -0700 Subject: Rename variables and add modify functions --- common/rawaccel.hpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'common/rawaccel.hpp') diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp index 28d9ee2..9b35568 100644 --- a/common/rawaccel.hpp +++ b/common/rawaccel.hpp @@ -309,14 +309,14 @@ namespace rawaccel { accel_function() = default; }; - struct variables { + struct mouse_modifier { bool apply_rotate = false; bool apply_accel = false; rotator rotate; accel_function accel_fn; vec2d sensitivity = { 1, 1 }; - variables(double degrees, vec2d sens, args_t accel_args) + mouse_modifier(double degrees, vec2d sens, args_t accel_args) : accel_fn(accel_args) { apply_rotate = degrees != 0; @@ -330,7 +330,20 @@ namespace rawaccel { sensitivity = sens; } - variables() = default; + vec2d modify(vec2d input) + { + if (apply_rotate) + { + return rotate(input); + } + } + + vec2d modify(vec2d input, milliseconds time) + { + return accel_fn(modify(input), time); + } + + mouse_modifier() = default; }; } // rawaccel \ No newline at end of file -- cgit v1.2.3 From 41a2448dc9a76b7d39e86cc5ee3b1d6a1583ba72 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Tue, 28 Jul 2020 13:25:53 -0700 Subject: Fix not all paths returning value --- common/rawaccel.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'common/rawaccel.hpp') diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp index 9b35568..9d4b5e3 100644 --- a/common/rawaccel.hpp +++ b/common/rawaccel.hpp @@ -330,15 +330,17 @@ namespace rawaccel { sensitivity = sens; } - vec2d modify(vec2d input) + inline vec2d modify(vec2d input) { if (apply_rotate) { return rotate(input); } + + return input; } - vec2d modify(vec2d input, milliseconds time) + inline vec2d modify(vec2d input, milliseconds time) { return accel_fn(modify(input), time); } -- cgit v1.2.3 From 9e55978ad8b8da90bb30407047b399e9c430c070 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Tue, 28 Jul 2020 14:54:31 -0700 Subject: More comments and light refactoring --- common/rawaccel.hpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) (limited to 'common/rawaccel.hpp') diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp index 9d4b5e3..2b45c06 100644 --- a/common/rawaccel.hpp +++ b/common/rawaccel.hpp @@ -9,11 +9,20 @@ namespace rawaccel { + /// Enum to hold acceleration implementation types (i.e. types of curves.) enum class mode { noaccel, linear, classic, natural, logarithmic, sigmoid, power }; + /// Struct to hold vector rotation details. struct rotator { + + /// Rotational vector, which points in the direction of the post-rotation positive y axis. vec2d rot_vec = { 1, 0 }; + /// + /// Rotates given input vector according to struct's rotational vector. + /// + /// Input vector to be rotated + /// 2d vector of rotated input. inline vec2d operator()(const vec2d& input) const { return { input.x * rot_vec.x - input.y * rot_vec.y, @@ -29,10 +38,16 @@ namespace rawaccel { rotator() = default; }; + /// Struct to hold clamp (min and max) details for acceleration application struct accel_scale_clamp { double lo = 0; double hi = 9; + /// + /// Clamps given input to min at lo, max at hi. + /// + /// Double to be clamped + /// Clamped input as double inline double operator()(double scale) const { return clampsd(scale, lo, hi); } @@ -54,6 +69,7 @@ namespace rawaccel { accel_scale_clamp() = default; }; +// Error throwing calls std libraries which are unavailable in kernel mode. #ifdef _KERNEL_MODE void error(const char*) {} #else @@ -62,6 +78,7 @@ namespace rawaccel { using milliseconds = double; + /// Struct to hold arguments for an acceleration function. struct args_t { mode accel_mode = mode::noaccel; milliseconds time_min = 0.4; @@ -74,7 +91,7 @@ namespace rawaccel { }; /// - /// Struct to hold acceleration implementation details. + /// Struct to hold acceleration curve implementation details. /// /// Type of acceleration. template @@ -216,8 +233,8 @@ namespace rawaccel { { k++; } double accelerate(double speed) { - //f(x) = (mx)^k - 1 - // The subtraction of 1 is with later addition of 1 in mind, + // f(x) = (mx)^k - 1 + // The subtraction of 1 occurs with later addition of 1 in mind, // so that the input vector is directly multiplied by (mx)^k (if unweighted) return (offset > 0 && speed < 1) ? 0 : pow(speed * b, k) - 1; } @@ -238,8 +255,10 @@ namespace rawaccel { void verify(args_t args) {} }; + /// Tagged union to hold all accel implementations and allow "polymorphism" via a visitor call. using accel_implementation_t = tagged_union; + /// Struct for holding acceleration application details. struct accel_function { /* @@ -247,14 +266,19 @@ namespace rawaccel { the user's mouse polling interval, though it should not matter if the system is stable. */ + /// The minimum time period for one mouse movement. milliseconds time_min = 0.4; /// The offset past which acceleration is applied. double speed_offset = 0; + /// The acceleration implementation (i.e. curve) accel_implementation_t accel; + /// The weight of acceleration applied in {x, y} dimensions. vec2d weight = { 1, 1 }; + + /// The object which sets a min and max for the acceleration scale. vec2 clamp; accel_function(args_t args) { @@ -273,6 +297,8 @@ namespace rawaccel { case mode::power: accel = accel_power(args); } + // Verification is performed by the accel_implementation object + // and therefore must occur after the object has been instantiated verify(args); time_min = args.time_min; @@ -282,14 +308,29 @@ namespace rawaccel { clamp.y = accel_scale_clamp(args.cap.y); } + /// + /// Applies mouse acceleration to a given speed, via visitor function to accel_implementation_t + /// + /// Speed from which to determine acceleration + /// Acceleration as a ratio magnitudes, as a double double apply(double speed) const { return accel.visit([=](auto accel_t) { return accel_t.accelerate(speed); }); } + /// + /// Verifies acceleration arguments, via visitor function to accel_implementation_t + /// + /// Arguments to be verified void verify(args_t args) const { return accel.visit([=](auto accel_t) { accel_t.verify(args); }); } + /// + /// Applies weighted acceleration to given input for given time period. + /// + /// 2d vector of {x, y} mouse movement to be accelerated + /// Time period over which input movement was accumulated + /// inline vec2d operator()(const vec2d& input, milliseconds time) const { double mag = sqrtsd(input.x * input.x + input.y * input.y); double time_clamped = clampsd(time, time_min, 100); @@ -309,6 +350,7 @@ namespace rawaccel { accel_function() = default; }; + /// Struct to hold variables and methods for modifying mouse input struct mouse_modifier { bool apply_rotate = false; bool apply_accel = false; @@ -330,6 +372,12 @@ namespace rawaccel { sensitivity = sens; } + /// + /// Applies modification without acceleration. Rotation is the only + /// modification currently implemented. + /// + /// Input to be modified. + /// 2d vector of modified input. inline vec2d modify(vec2d input) { if (apply_rotate) @@ -340,6 +388,12 @@ namespace rawaccel { return input; } + /// + /// Applies modification, including acceleration. + /// + /// Input to be modified + /// Time period for determining acceleration. + /// 2d vector with modified input. inline vec2d modify(vec2d input, milliseconds time) { return accel_fn(modify(input), time); -- cgit v1.2.3 From 06d6d8d3e3cb5186daab029f2d7c7fa3ea0438d2 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Tue, 28 Jul 2020 15:25:41 -0700 Subject: Rename acceleration constants, arguments --- common/rawaccel.hpp | 74 ++++++++++++++++++++++++++--------------------------- 1 file changed, 37 insertions(+), 37 deletions(-) (limited to 'common/rawaccel.hpp') diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp index 2b45c06..e904350 100644 --- a/common/rawaccel.hpp +++ b/common/rawaccel.hpp @@ -79,7 +79,7 @@ namespace rawaccel { using milliseconds = double; /// Struct to hold arguments for an acceleration function. - struct args_t { + struct accel_args { mode accel_mode = mode::noaccel; milliseconds time_min = 0.4; double offset = 0; @@ -97,14 +97,14 @@ namespace rawaccel { template struct accel_implentation { - /// The acceleration ramp rate. - double b = 0; + /// First constant for use in acceleration curves. Generally, the acceleration ramp rate. + double curve_constant_one = 0; - /// The limit or exponent for various modes. - double k = 0; + /// Second constant for use in acceleration curves. Generally, the limit or exponent in the curve. + double curve_constant_two = 0; - /// The midpoint in sigmoid mode. - double m = 0; + /// Third constant for use in acceleration curves. The midpoint in sigmoid mode. + double curve_constant_three = 0; /// The offset past which acceleration is applied. Used in power mode. double offset = 0; @@ -114,11 +114,11 @@ namespace rawaccel { /// /// /// - accel_implentation(args_t args) + accel_implentation(accel_args args) { - b = args.accel; - k = args.lim_exp - 1; - m = args.midpoint; + curve_constant_one = args.accel; + curve_constant_two = args.lim_exp - 1; + curve_constant_three = args.midpoint; offset = args.offset; } @@ -133,7 +133,7 @@ namespace rawaccel { /// Verifies arguments as valid. Errors if not. /// /// Arguments to verified. - void verify(args_t args) { + void verify(accel_args args) { if (args.accel < 0) error("accel can not be negative, use a negative weight to compensate"); if (args.time_min <= 0) error("min time must be positive"); } @@ -148,15 +148,15 @@ namespace rawaccel { /// Struct to hold linear acceleration implementation. struct accel_linear : accel_implentation { - accel_linear(args_t args) + accel_linear(accel_args args) : accel_implentation(args) {} double accelerate(double speed) { //f(x) = mx - return b * speed; + return curve_constant_one * speed; } - void verify(args_t args) { + void verify(accel_args args) { accel_implentation::verify(args); if (args.lim_exp <= 1) error("limit must be greater than 1"); } @@ -164,15 +164,15 @@ namespace rawaccel { /// Struct to hold "classic" (linear raised to power) acceleration implementation. struct accel_classic : accel_implentation { - accel_classic(args_t args) + accel_classic(accel_args args) : accel_implentation(args) {} double accelerate(double speed) { //f(x) = (mx)^k - return pow(b * speed, k); + return pow(curve_constant_one * speed, curve_constant_two); } - void verify(args_t args) { + void verify(accel_args args) { accel_implentation::verify(args); if (args.lim_exp <= 1) error("exponent must be greater than 1"); } @@ -180,16 +180,16 @@ namespace rawaccel { /// Struct to hold "natural" (vanishing difference) acceleration implementation. struct accel_natural : accel_implentation { - accel_natural(args_t args) + accel_natural(accel_args args) : accel_implentation(args) - { b /= k; } + { curve_constant_one /= curve_constant_two; } double accelerate(double speed) { // f(x) = k(1-e^(-mx)) - return k - (k * exp(-b * speed));; + return curve_constant_two - (curve_constant_two * exp(-curve_constant_one * speed));; } - void verify(args_t args) { + void verify(accel_args args) { accel_implentation::verify(args); if (args.lim_exp <= 1) error("exponent must be greater than 1"); } @@ -197,14 +197,14 @@ namespace rawaccel { /// Struct to hold logarithmic acceleration implementation. struct accel_logarithmic : accel_implentation { - accel_logarithmic(args_t args) + accel_logarithmic(accel_args args) : accel_implentation(args) {} double accelerate(double speed) { - return log(speed * b + 1); + return log(speed * curve_constant_one + 1); } - void verify(args_t args) { + void verify(accel_args args) { accel_implentation::verify(args); if (args.lim_exp <= 1) error("exponent must be greater than 1"); } @@ -212,15 +212,15 @@ namespace rawaccel { /// Struct to hold sigmoid (s-shaped) acceleration implementation. struct accel_sigmoid : accel_implentation { - accel_sigmoid(args_t args) + accel_sigmoid(accel_args args) : accel_implentation(args) {} double accelerate(double speed) { //f(x) = k/(1+e^(-m(c-x))) - return k / (exp(-b * (speed - m)) + 1); + return curve_constant_two / (exp(-curve_constant_one * (speed - curve_constant_three)) + 1); } - void verify(args_t args) { + void verify(accel_args args) { accel_implentation::verify(args); if (args.lim_exp <= 1) error("exponent must be greater than 1"); } @@ -228,18 +228,18 @@ namespace rawaccel { /// Struct to hold power (non-additive) acceleration implementation. struct accel_power : accel_implentation { - accel_power(args_t args) + accel_power(accel_args args) : accel_implentation(args) - { k++; } + { curve_constant_two++; } double accelerate(double speed) { // f(x) = (mx)^k - 1 // The subtraction of 1 occurs with later addition of 1 in mind, // so that the input vector is directly multiplied by (mx)^k (if unweighted) - return (offset > 0 && speed < 1) ? 0 : pow(speed * b, k) - 1; + return (offset > 0 && speed < 1) ? 0 : pow(speed * curve_constant_one, curve_constant_two) - 1; } - void verify(args_t args) { + void verify(accel_args args) { accel_implentation::verify(args); if (args.lim_exp <= 0) error("exponent must be greater than 0"); } @@ -247,12 +247,12 @@ namespace rawaccel { /// Struct to hold acceleration implementation which applies no acceleration. struct accel_noaccel : accel_implentation { - accel_noaccel(args_t args) + accel_noaccel(accel_args args) : accel_implentation(args) {} double accelerate(double speed) { return 0; } - void verify(args_t args) {} + void verify(accel_args args) {} }; /// Tagged union to hold all accel implementations and allow "polymorphism" via a visitor call. @@ -281,7 +281,7 @@ namespace rawaccel { /// The object which sets a min and max for the acceleration scale. vec2 clamp; - accel_function(args_t args) { + accel_function(accel_args args) { switch (args.accel_mode) { case mode::linear: accel = accel_linear(args); @@ -321,7 +321,7 @@ namespace rawaccel { /// Verifies acceleration arguments, via visitor function to accel_implementation_t /// /// Arguments to be verified - void verify(args_t args) const { + void verify(accel_args args) const { return accel.visit([=](auto accel_t) { accel_t.verify(args); }); } @@ -358,7 +358,7 @@ namespace rawaccel { accel_function accel_fn; vec2d sensitivity = { 1, 1 }; - mouse_modifier(double degrees, vec2d sens, args_t accel_args) + mouse_modifier(double degrees, vec2d sens, accel_args accel_args) : accel_fn(accel_args) { apply_rotate = degrees != 0; -- cgit v1.2.3 From cdd82efdfdd7c2e9b4c2ed9777792f9921eedb9e Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Tue, 28 Jul 2020 17:21:39 -0700 Subject: Get rid of enum and use types\tags directly --- common/rawaccel.hpp | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) (limited to 'common/rawaccel.hpp') diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp index e904350..08829dd 100644 --- a/common/rawaccel.hpp +++ b/common/rawaccel.hpp @@ -9,9 +9,6 @@ namespace rawaccel { - /// Enum to hold acceleration implementation types (i.e. types of curves.) - enum class mode { noaccel, linear, classic, natural, logarithmic, sigmoid, power }; - /// Struct to hold vector rotation details. struct rotator { @@ -80,7 +77,7 @@ namespace rawaccel { /// Struct to hold arguments for an acceleration function. struct accel_args { - mode accel_mode = mode::noaccel; + int accel_mode = 0; milliseconds time_min = 0.4; double offset = 0; double accel = 0; @@ -282,20 +279,8 @@ namespace rawaccel { vec2 clamp; accel_function(accel_args args) { - switch (args.accel_mode) - { - case mode::linear: accel = accel_linear(args); - break; - case mode::classic: accel = accel_classic(args); - break; - case mode::natural: accel = accel_natural(args); - break; - case mode::logarithmic: accel = accel_logarithmic(args); - break; - case mode::sigmoid: accel = accel_sigmoid(args); - break; - case mode::power: accel = accel_power(args); - } + accel.tag = args.accel_mode; + accel.visit([&](auto& a){ a = {args}; }); // Verification is performed by the accel_implementation object // and therefore must occur after the object has been instantiated @@ -365,7 +350,8 @@ namespace rawaccel { if (apply_rotate) rotate = rotator(degrees); else rotate = rotator(); - apply_accel = accel_args.accel_mode != mode::noaccel; + apply_accel = (accel_args.accel_mode != 0 && + accel_args.accel_mode != accel_implementation_t::id); if (sens.x == 0) sens.x = 1; if (sens.y == 0) sens.y = 1; -- cgit v1.2.3 From 344f759dad28c8b3866d7942bd8927ef1caaf6ab Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Tue, 28 Jul 2020 18:16:12 -0700 Subject: add per-class args structs --- common/rawaccel.hpp | 58 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 23 deletions(-) (limited to 'common/rawaccel.hpp') diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp index 08829dd..9cfa5e7 100644 --- a/common/rawaccel.hpp +++ b/common/rawaccel.hpp @@ -9,6 +9,13 @@ namespace rawaccel { +// Error throwing calls std libraries which are unavailable in kernel mode. +#ifdef _KERNEL_MODE + void error(const char*) {} +#else + void error(const char* s); +#endif + /// Struct to hold vector rotation details. struct rotator { @@ -66,25 +73,15 @@ namespace rawaccel { accel_scale_clamp() = default; }; -// Error throwing calls std libraries which are unavailable in kernel mode. -#ifdef _KERNEL_MODE - void error(const char*) {} -#else - void error(const char* s); -#endif - using milliseconds = double; /// Struct to hold arguments for an acceleration function. struct accel_args { - int accel_mode = 0; milliseconds time_min = 0.4; double offset = 0; double accel = 0; double lim_exp = 2; double midpoint = 0; - vec2d weight = { 1, 1 }; - vec2d cap = { 0, 0 }; }; /// @@ -255,6 +252,14 @@ namespace rawaccel { /// Tagged union to hold all accel implementations and allow "polymorphism" via a visitor call. using accel_implementation_t = tagged_union; + struct accel_fn_args { + accel_args acc_args = accel_args{}; + int accel_mode = 0; + milliseconds time_min = 0.4; + vec2d weight = { 1, 1 }; + vec2d cap = { 0, 0 }; + }; + /// Struct for holding acceleration application details. struct accel_function { @@ -278,16 +283,16 @@ namespace rawaccel { /// The object which sets a min and max for the acceleration scale. vec2 clamp; - accel_function(accel_args args) { + accel_function(accel_fn_args args) { accel.tag = args.accel_mode; - accel.visit([&](auto& a){ a = {args}; }); + accel.visit([&](auto& a){ a = {args.acc_args}; }); // Verification is performed by the accel_implementation object // and therefore must occur after the object has been instantiated - verify(args); + verify(args.acc_args); time_min = args.time_min; - speed_offset = args.offset; + speed_offset = args.acc_args.offset; weight = args.weight; clamp.x = accel_scale_clamp(args.cap.x); clamp.y = accel_scale_clamp(args.cap.y); @@ -335,6 +340,13 @@ namespace rawaccel { accel_function() = default; }; + struct modifier_args + { + double degrees = 0; + vec2d sens = { 1, 1 }; + accel_fn_args acc_fn_args = accel_fn_args{}; + }; + /// Struct to hold variables and methods for modifying mouse input struct mouse_modifier { bool apply_rotate = false; @@ -343,19 +355,19 @@ namespace rawaccel { accel_function accel_fn; vec2d sensitivity = { 1, 1 }; - mouse_modifier(double degrees, vec2d sens, accel_args accel_args) - : accel_fn(accel_args) + mouse_modifier(modifier_args args) + : accel_fn(args.acc_fn_args) { - apply_rotate = degrees != 0; - if (apply_rotate) rotate = rotator(degrees); + apply_rotate = args.degrees != 0; + if (apply_rotate) rotate = rotator(args.degrees); else rotate = rotator(); - apply_accel = (accel_args.accel_mode != 0 && - accel_args.accel_mode != accel_implementation_t::id); + apply_accel = (args.acc_fn_args.accel_mode != 0 && + args.acc_fn_args.accel_mode != accel_implementation_t::id); - if (sens.x == 0) sens.x = 1; - if (sens.y == 0) sens.y = 1; - sensitivity = sens; + if (args.sens.x == 0) args.sens.x = 1; + if (args.sens.y == 0) args.sens.y = 1; + sensitivity = args.sens; } /// -- cgit v1.2.3 From 33317e79489848ae537ac78b9c9e70372857aee8 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Wed, 29 Jul 2020 00:35:39 -0700 Subject: Separate accel implementations into files --- common/rawaccel.hpp | 212 ++++++---------------------------------------------- 1 file changed, 24 insertions(+), 188 deletions(-) (limited to 'common/rawaccel.hpp') diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp index 9cfa5e7..d0c1b66 100644 --- a/common/rawaccel.hpp +++ b/common/rawaccel.hpp @@ -7,14 +7,15 @@ #include "x64-util.hpp" #include "external/tagged-union-single.h" -namespace rawaccel { +#include "accel_linear.cpp" +#include "accel_classic.cpp" +#include "accel_natural.cpp" +#include "accel_logarithmic.cpp" +#include "accel_sigmoid.cpp" +#include "accel_power.cpp" +#include "accel_noaccel.cpp" -// Error throwing calls std libraries which are unavailable in kernel mode. -#ifdef _KERNEL_MODE - void error(const char*) {} -#else - void error(const char* s); -#endif +namespace rawaccel { /// Struct to hold vector rotation details. struct rotator { @@ -73,182 +74,6 @@ namespace rawaccel { accel_scale_clamp() = default; }; - using milliseconds = double; - - /// Struct to hold arguments for an acceleration function. - struct accel_args { - milliseconds time_min = 0.4; - double offset = 0; - double accel = 0; - double lim_exp = 2; - double midpoint = 0; - }; - - /// - /// Struct to hold acceleration curve implementation details. - /// - /// Type of acceleration. - template - struct accel_implentation { - - /// First constant for use in acceleration curves. Generally, the acceleration ramp rate. - double curve_constant_one = 0; - - /// Second constant for use in acceleration curves. Generally, the limit or exponent in the curve. - double curve_constant_two = 0; - - /// Third constant for use in acceleration curves. The midpoint in sigmoid mode. - double curve_constant_three = 0; - - /// The offset past which acceleration is applied. Used in power mode. - double offset = 0; - - /// - /// Initializes a new instance of the struct. - /// - /// - /// - accel_implentation(accel_args args) - { - curve_constant_one = args.accel; - curve_constant_two = args.lim_exp - 1; - curve_constant_three = args.midpoint; - offset = args.offset; - } - - /// - /// Returns accelerated value of speed as a ratio of magnitude. - /// - /// Mouse speed at which to calculate acceleration. - /// Ratio of accelerated movement magnitude to input movement magnitude. - double accelerate(double speed) { return 0; } - - /// - /// Verifies arguments as valid. Errors if not. - /// - /// Arguments to verified. - void verify(accel_args args) { - if (args.accel < 0) error("accel can not be negative, use a negative weight to compensate"); - if (args.time_min <= 0) error("min time must be positive"); - } - - /// - /// - /// - /// - accel_implentation() = default; - }; - - /// Struct to hold linear acceleration implementation. - struct accel_linear : accel_implentation { - - accel_linear(accel_args args) - : accel_implentation(args) {} - - double accelerate(double speed) { - //f(x) = mx - return curve_constant_one * speed; - } - - void verify(accel_args args) { - accel_implentation::verify(args); - if (args.lim_exp <= 1) error("limit must be greater than 1"); - } - }; - - /// Struct to hold "classic" (linear raised to power) acceleration implementation. - struct accel_classic : accel_implentation { - accel_classic(accel_args args) - : accel_implentation(args) {} - - double accelerate(double speed) { - //f(x) = (mx)^k - return pow(curve_constant_one * speed, curve_constant_two); - } - - void verify(accel_args args) { - accel_implentation::verify(args); - if (args.lim_exp <= 1) error("exponent must be greater than 1"); - } - }; - - /// Struct to hold "natural" (vanishing difference) acceleration implementation. - struct accel_natural : accel_implentation { - accel_natural(accel_args args) - : accel_implentation(args) - { curve_constant_one /= curve_constant_two; } - - double accelerate(double speed) { - // f(x) = k(1-e^(-mx)) - return curve_constant_two - (curve_constant_two * exp(-curve_constant_one * speed));; - } - - void verify(accel_args args) { - accel_implentation::verify(args); - if (args.lim_exp <= 1) error("exponent must be greater than 1"); - } - }; - - /// Struct to hold logarithmic acceleration implementation. - struct accel_logarithmic : accel_implentation { - accel_logarithmic(accel_args args) - : accel_implentation(args) {} - - double accelerate(double speed) { - return log(speed * curve_constant_one + 1); - } - - void verify(accel_args args) { - accel_implentation::verify(args); - if (args.lim_exp <= 1) error("exponent must be greater than 1"); - } - }; - - /// Struct to hold sigmoid (s-shaped) acceleration implementation. - struct accel_sigmoid : accel_implentation { - accel_sigmoid(accel_args args) - : accel_implentation(args) {} - - double accelerate(double speed) { - //f(x) = k/(1+e^(-m(c-x))) - return curve_constant_two / (exp(-curve_constant_one * (speed - curve_constant_three)) + 1); - } - - void verify(accel_args args) { - accel_implentation::verify(args); - if (args.lim_exp <= 1) error("exponent must be greater than 1"); - } - }; - - /// Struct to hold power (non-additive) acceleration implementation. - struct accel_power : accel_implentation { - accel_power(accel_args args) - : accel_implentation(args) - { curve_constant_two++; } - - double accelerate(double speed) { - // f(x) = (mx)^k - 1 - // The subtraction of 1 occurs with later addition of 1 in mind, - // so that the input vector is directly multiplied by (mx)^k (if unweighted) - return (offset > 0 && speed < 1) ? 0 : pow(speed * curve_constant_one, curve_constant_two) - 1; - } - - void verify(accel_args args) { - accel_implentation::verify(args); - if (args.lim_exp <= 0) error("exponent must be greater than 0"); - } - }; - - /// Struct to hold acceleration implementation which applies no acceleration. - struct accel_noaccel : accel_implentation { - accel_noaccel(accel_args args) - : accel_implentation(args) {} - - double accelerate(double speed) { return 0; } - - void verify(accel_args args) {} - }; - /// Tagged union to hold all accel implementations and allow "polymorphism" via a visitor call. using accel_implementation_t = tagged_union; @@ -371,18 +196,20 @@ namespace rawaccel { } /// - /// Applies modification without acceleration. Rotation is the only - /// modification currently implemented. + /// Applies modification without acceleration. /// /// Input to be modified. /// 2d vector of modified input. - inline vec2d modify(vec2d input) + inline vec2d modify_without_accel(vec2d input) { if (apply_rotate) { return rotate(input); } + input.x *= sensitivity.x; + input.y *= sensitivity.y; + return input; } @@ -392,9 +219,18 @@ namespace rawaccel { /// Input to be modified /// Time period for determining acceleration. /// 2d vector with modified input. - inline vec2d modify(vec2d input, milliseconds time) + inline vec2d modify_with_accel(vec2d input, milliseconds time) { - return accel_fn(modify(input), time); + if (apply_rotate) + { + return rotate(input); + } + + input = accel_fn(input, time); + input.x *= sensitivity.x; + input.y *= sensitivity.y; + + return input; } mouse_modifier() = default; -- cgit v1.2.3 From 7963edee802d5a7b51f1867a5133857c403c8ef6 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Wed, 29 Jul 2020 01:10:29 -0700 Subject: Inline methods for linking, and fix sens application bug --- common/rawaccel.hpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'common/rawaccel.hpp') diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp index d0c1b66..6f737da 100644 --- a/common/rawaccel.hpp +++ b/common/rawaccel.hpp @@ -204,7 +204,7 @@ namespace rawaccel { { if (apply_rotate) { - return rotate(input); + input = rotate(input); } input.x *= sensitivity.x; @@ -223,10 +223,14 @@ namespace rawaccel { { if (apply_rotate) { - return rotate(input); + input = rotate(input); + } + + if (apply_accel) + { + input = accel_fn(input, time); } - input = accel_fn(input, time); input.x *= sensitivity.x; input.y *= sensitivity.y; -- cgit v1.2.3 From 6bb5b03e2afb0dd52204dfb1924dfd096387de3a Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Wed, 29 Jul 2020 11:52:36 -0700 Subject: Use modifier object in wrapper --- common/rawaccel.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'common/rawaccel.hpp') diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp index 6f737da..b480e87 100644 --- a/common/rawaccel.hpp +++ b/common/rawaccel.hpp @@ -20,7 +20,7 @@ namespace rawaccel { /// Struct to hold vector rotation details. struct rotator { - /// Rotational vector, which points in the direction of the post-rotation positive y axis. + /// Rotational vector, which points in the direction of the post-rotation positive x axis. vec2d rot_vec = { 1, 0 }; /// -- cgit v1.2.3