Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 78 additions & 1 deletion wrapper-tests/SynchronousAccelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,85 @@ public void GivenSpeeds_SynchronousAccelWithNonDefaultSmooth_YieldsCorrectSens()
Assert.AreEqual(expectedOutput, output.Item1, expectedOutput * 0.0001);
}
}
[TestMethod]
public void SynchronousAccel_ThresholdCrossing_IsContinuous()
{
double syncSpeed = 20;
double gamma = 0.5;
double motivity = 1.3;

// smooth = 0.03125 exactly results in sharpness = 16
double[] smoothValues = { 0.03126, 0.03125, 0.03124, 0.0 };

foreach (double smooth in smoothValues)
{
var profile = new Profile();
profile.outputDPI = 1000;
profile.argsX.mode = AccelMode.synchronous;
profile.argsX.gain = false;
profile.argsX.syncSpeed = syncSpeed;
profile.argsX.gamma = gamma;
profile.argsX.motivity = motivity;
profile.argsX.smooth = smooth;

var accel = new ManagedAccel(profile);
var pureSimulator = new UnclampedSynchronousSimulator(syncSpeed, motivity, gamma, smooth);

for (int input = 1; input < 100; input += 5)
{
Tuple<double, double> output = accel.Accelerate(input, 0, 1, 10);

double inputSpeed = (double)input / 10.0;
double expectedOutput = (double)input * pureSimulator.Accelerate(inputSpeed);

double delta = Math.Abs(output.Item1 - expectedOutput);
double relativeError = delta / expectedOutput;

Assert.IsTrue(relativeError < 0.001,
$"Discontinuity found at smooth={smooth}, input={input}. " +
$"Driver: {output.Item1}, Pure Math: {expectedOutput}, Error: {relativeError:P}");
}
}
}
}

/// <summary>
/// A simulator that follows the raw math WITHOUT the sharpness >= 16 optimization.
/// </summary>
public class UnclampedSynchronousSimulator
{
public UnclampedSynchronousSimulator(double syncSpeed, double motivity, double gamma, double smooth)
{
SyncSpeed = syncSpeed;
Motivity = motivity;
Gamma = gamma;
Sharpness = smooth <= 0 ? 100 : 0.5 / smooth;
}

}
public double SyncSpeed { get; }
public double Motivity { get; }
public double Gamma { get; }
public double Sharpness { get; }

public double Accelerate(double inputSpeed)
{
if (inputSpeed == SyncSpeed) return 1.0;

double logSyncSpeed = Math.Log(SyncSpeed);
double logMotivity = Math.Log(Motivity);
double gammaConst = Gamma / logMotivity;

double logX = Math.Log(inputSpeed);
double logDiff = logX - logSyncSpeed;

double logSpace = Math.Abs(gammaConst * logDiff);
double exponent = Math.Pow(Math.Tanh(Math.Pow(logSpace, Sharpness)), 1 / Sharpness);

exponent *= Math.Sign(logDiff);

return Math.Exp(exponent * logMotivity);
}
}

/// <summary>
/// Contains definition of how synchronous accel is expected to accelerate inputs
Expand Down
2 changes: 1 addition & 1 deletion wrapper-tests/wrapper-tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.2.2.7\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.2.2.7\build\net45\MSTest.TestAdapter.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
Expand Down