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 ++++++++++++++++++++++++++++++++++++++++++++++++++--- grapher/Form1.cs | 4 ++-- wrapper/wrapper.cpp | 2 +- wrapper/wrapper.hpp | 2 +- 4 files changed, 61 insertions(+), 7 deletions(-) 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); diff --git a/grapher/Form1.cs b/grapher/Form1.cs index 1915b01..4165b0f 100644 --- a/grapher/Form1.cs +++ b/grapher/Form1.cs @@ -15,14 +15,14 @@ namespace grapher public RawAcceleration() { InitializeComponent(); - var managedAccel = new ManagedAccel(6, 0, 1, 0.025, 0); + var managedAccel = new ManagedAccel(6, 0, 1.333, 0.05, 0); var orderedPoints = new SortedDictionary(); for (int i = 0; i < 100; i++) { for (int j = 0; j <= i; j++) { - var output = managedAccel.Accelerate(i, j, 1, 6); + var output = managedAccel.Accelerate(i, j, 1); var inMagnitude = Magnitude(i,j); var outMagnitude = Magnitude(output.Item1, output.Item2); diff --git a/wrapper/wrapper.cpp b/wrapper/wrapper.cpp index 2c004e4..1adafba 100644 --- a/wrapper/wrapper.cpp +++ b/wrapper/wrapper.cpp @@ -5,7 +5,7 @@ using namespace rawaccel; using namespace System; -Tuple^ ManagedAccel::Accelerate(int x, int y, double time, double mode) +Tuple^ ManagedAccel::Accelerate(int x, int y, double time) { vec2d input_vec2d = {x, y}; vec2d output = (*accel_instance)(input_vec2d, (milliseconds)time); diff --git a/wrapper/wrapper.hpp b/wrapper/wrapper.hpp index e9bbf2e..0307b4c 100644 --- a/wrapper/wrapper.hpp +++ b/wrapper/wrapper.hpp @@ -48,5 +48,5 @@ public: return accel_instance; } - Tuple^ Accelerate(int x, int y, double time, double mode); + Tuple^ Accelerate(int x, int y, double time); }; \ No newline at end of file -- cgit v1.2.3