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/accel_classic.cpp | 21 +++++ common/accel_linear.cpp | 21 +++++ common/accel_logarithmic.cpp | 21 +++++ common/accel_natural.cpp | 23 +++++ common/accel_noaccel.cpp | 15 +++ common/accel_power.cpp | 24 +++++ common/accel_sigmoid.cpp | 21 +++++ common/accel_types.hpp | 130 ++++++++++++++++++++++++++ common/common.vcxitems | 10 ++ common/rawaccel.hpp | 212 +++++-------------------------------------- driver/driver.cpp | 8 +- 11 files changed, 314 insertions(+), 192 deletions(-) create mode 100644 common/accel_classic.cpp create mode 100644 common/accel_linear.cpp create mode 100644 common/accel_logarithmic.cpp create mode 100644 common/accel_natural.cpp create mode 100644 common/accel_noaccel.cpp create mode 100644 common/accel_power.cpp create mode 100644 common/accel_sigmoid.cpp create mode 100644 common/accel_types.hpp diff --git a/common/accel_classic.cpp b/common/accel_classic.cpp new file mode 100644 index 0000000..e4e7ab9 --- /dev/null +++ b/common/accel_classic.cpp @@ -0,0 +1,21 @@ +#pragma once + +#define _USE_MATH_DEFINES +#include + +#include "accel_types.hpp" + +namespace rawaccel { + accel_classic::accel_classic(accel_args args) + : accel_implentation(args) {} + + double accel_classic::accelerate(double speed) { + //f(x) = (mx)^k + return pow(curve_constant_one * speed, curve_constant_two); + } + + void accel_classic::verify(accel_args args) { + accel_implentation::verify(args); + if (args.lim_exp <= 1) error("exponent must be greater than 1"); + } +} diff --git a/common/accel_linear.cpp b/common/accel_linear.cpp new file mode 100644 index 0000000..d12e798 --- /dev/null +++ b/common/accel_linear.cpp @@ -0,0 +1,21 @@ +#pragma once + +#define _USE_MATH_DEFINES +#include + +#include "accel_types.hpp" + +namespace rawaccel { + accel_linear::accel_linear(accel_args args) + : accel_implentation(args) {} + + double accel_linear::accelerate(double speed) { + //f(x) = mx + return curve_constant_one * speed; + } + + void accel_linear::verify(accel_args args) { + accel_implentation::verify(args); + if (args.lim_exp <= 1) error("limit must be greater than 1"); + } +} diff --git a/common/accel_logarithmic.cpp b/common/accel_logarithmic.cpp new file mode 100644 index 0000000..c127bcb --- /dev/null +++ b/common/accel_logarithmic.cpp @@ -0,0 +1,21 @@ +#pragma once + +#define _USE_MATH_DEFINES +#include + +#include "accel_types.hpp" + +namespace rawaccel { + accel_logarithmic::accel_logarithmic(accel_args args) + : accel_implentation(args) {} + + double accel_logarithmic::accelerate(double speed) { + //f(x) = log(m*x+1) + return log(speed * curve_constant_one + 1); + } + + void accel_logarithmic::verify(accel_args args) { + accel_implentation::verify(args); + if (args.lim_exp <= 1) error("exponent must be greater than 1"); + } +} diff --git a/common/accel_natural.cpp b/common/accel_natural.cpp new file mode 100644 index 0000000..ba9bc02 --- /dev/null +++ b/common/accel_natural.cpp @@ -0,0 +1,23 @@ + +#pragma once + +#define _USE_MATH_DEFINES +#include + +#include "accel_types.hpp" + +namespace rawaccel { + accel_natural::accel_natural(accel_args args) + : accel_implentation(args) + { curve_constant_one /= curve_constant_two; } + + double accel_natural::accelerate(double speed) { + // f(x) = k(1-e^(-mx)) + return curve_constant_two - (curve_constant_two * exp(-curve_constant_one * speed));; + } + + void accel_natural::verify(accel_args args) { + accel_implentation::verify(args); + if (args.lim_exp <= 1) error("exponent must be greater than 1"); + } +} diff --git a/common/accel_noaccel.cpp b/common/accel_noaccel.cpp new file mode 100644 index 0000000..50506a7 --- /dev/null +++ b/common/accel_noaccel.cpp @@ -0,0 +1,15 @@ +#pragma once + +#define _USE_MATH_DEFINES +#include + +#include "accel_types.hpp" + +namespace rawaccel { + accel_noaccel::accel_noaccel(accel_args args) + : accel_implentation(args) {} + + double accel_noaccel::accelerate(double speed) { return 0; } + + void accel_noaccel::verify(accel_args args) { } +} diff --git a/common/accel_power.cpp b/common/accel_power.cpp new file mode 100644 index 0000000..26f800d --- /dev/null +++ b/common/accel_power.cpp @@ -0,0 +1,24 @@ +#pragma once + +#define _USE_MATH_DEFINES +#include + +#include "accel_types.hpp" + +namespace rawaccel { + accel_power::accel_power(accel_args args) + : accel_implentation(args) + { curve_constant_two++; } + + double accel_power::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 accel_power::verify(accel_args args) { + accel_implentation::verify(args); + if (args.lim_exp <= 0) error("exponent must be greater than 0"); + } +} diff --git a/common/accel_sigmoid.cpp b/common/accel_sigmoid.cpp new file mode 100644 index 0000000..c5280bc --- /dev/null +++ b/common/accel_sigmoid.cpp @@ -0,0 +1,21 @@ +#pragma once + +#define _USE_MATH_DEFINES +#include + +#include "accel_types.hpp" + +namespace rawaccel { + accel_sigmoid::accel_sigmoid(accel_args args) + : accel_implentation(args) {} + + double accel_sigmoid::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 accel_sigmoid::verify(accel_args args) { + accel_implentation::verify(args); + if (args.lim_exp <= 1) error("exponent must be greater than 1"); + } +} diff --git a/common/accel_types.hpp b/common/accel_types.hpp new file mode 100644 index 0000000..c931097 --- /dev/null +++ b/common/accel_types.hpp @@ -0,0 +1,130 @@ +#pragma once + +#define _USE_MATH_DEFINES +#include + +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 + + 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); + double accelerate(double speed); + void verify(accel_args args); + }; + + /// Struct to hold "classic" (linear raised to power) acceleration implementation. + struct accel_classic : accel_implentation { + accel_classic(accel_args args); + double accelerate(double speed); + void verify(accel_args args); + }; + + /// Struct to hold "natural" (vanishing difference) acceleration implementation. + struct accel_natural : accel_implentation { + accel_natural(accel_args args); + double accelerate(double speed); + void verify(accel_args args); + }; + + /// Struct to hold logarithmic acceleration implementation. + struct accel_logarithmic : accel_implentation { + accel_logarithmic(accel_args args); + double accelerate(double speed); + void verify(accel_args args); + }; + + /// Struct to hold sigmoid (s-shaped) acceleration implementation. + struct accel_sigmoid : accel_implentation { + accel_sigmoid(accel_args args); + double accelerate(double speed); + void verify(accel_args args); + }; + + /// Struct to hold power (non-additive) acceleration implementation. + struct accel_power : accel_implentation { + accel_power(accel_args args); + double accelerate(double speed); + void verify(accel_args args); + }; + + /// Struct to hold acceleration implementation which applies no acceleration. + struct accel_noaccel : accel_implentation { + accel_noaccel(accel_args args); + double accelerate(double speed); + void verify(accel_args args); + }; + +} diff --git a/common/common.vcxitems b/common/common.vcxitems index 224792c..f33d8e1 100644 --- a/common/common.vcxitems +++ b/common/common.vcxitems @@ -14,10 +14,20 @@ + + + + + + + + + + \ No newline at end of file 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; diff --git a/driver/driver.cpp b/driver/driver.cpp index 9704b21..1f9cebd 100644 --- a/driver/driver.cpp +++ b/driver/driver.cpp @@ -79,15 +79,15 @@ Arguments: DebugPrint(("RA time < min with %d ticks\n", ticks)); } - input = global.modifier.modify(input, time); + input = global.modifier.modify_with_accel(input, time); } else { - input = global.modifier.modify(input); + input = global.modifier.modify_without_accel(input); } - double result_x = input.x * global.modifier.sensitivity.x + local_carry.x; - double result_y = input.y * global.modifier.sensitivity.y + local_carry.y; + double result_x = input.x + local_carry.x; + double result_y = input.y + local_carry.y; LONG out_x = static_cast(result_x); LONG out_y = static_cast(result_y); -- cgit v1.2.3