-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PreemptionComputer interface and it's implementations: SimplePree…
…mptionComputer and StabilizedPreemptionComputer; Conditionally register PreemptionComputerin HotKeyProfileRunner;
- Loading branch information
Showing
6 changed files
with
125 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using CommunityToolkit.Diagnostics; | ||
|
||
namespace SightKeeper.Commons; | ||
|
||
public static class EnumerableExtensions | ||
{ | ||
public static float Median(this IEnumerable<float> values) | ||
{ | ||
var sortedValues = values.Order().ToList(); | ||
Guard.IsNotEmpty(sortedValues); | ||
var middle = sortedValues.Count / 2; | ||
if (sortedValues.Count % 2 == 0) | ||
return (sortedValues[middle - 1] + sortedValues[middle]) / 2; | ||
return sortedValues[middle]; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...eper.Services/Prediction/Handling/MouseMoving/Decorators/Preemption/PreemptionComputer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Numerics; | ||
|
||
namespace SightKeeper.Services.Prediction.Handling.MouseMoving.Decorators.Preemption; | ||
|
||
public interface PreemptionComputer | ||
{ | ||
Vector2 ComputePreemption(Vector2 moveVector, TimeSpan timeDelta); | ||
void Reset(); | ||
} |
34 changes: 34 additions & 0 deletions
34
...ervices/Prediction/Handling/MouseMoving/Decorators/Preemption/SimplePreemptionComputer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Numerics; | ||
using CommunityToolkit.Diagnostics; | ||
using Serilog; | ||
using SightKeeper.Domain.Model; | ||
|
||
namespace SightKeeper.Services.Prediction.Handling.MouseMoving.Decorators.Preemption; | ||
|
||
public sealed class SimplePreemptionComputer : PreemptionComputer | ||
{ | ||
public SimplePreemptionComputer(Profile profile) | ||
{ | ||
Guard.IsNotNull(profile.PreemptionSettings); | ||
_preemptionFactor = new Vector2(profile.PreemptionSettings.HorizontalFactor, profile.PreemptionSettings.VerticalFactor); | ||
} | ||
|
||
public Vector2 ComputePreemption(Vector2 moveVector, TimeSpan timeDelta) | ||
{ | ||
var targetVelocity = moveVector + _previousPreemption; | ||
targetVelocity /= (float)timeDelta.TotalMilliseconds; | ||
var preemption = targetVelocity * BasePreemptionFactor * _preemptionFactor; | ||
Log.ForContext<SimplePreemptionComputer>().Debug("Preemption is {Preemption}", preemption); | ||
_previousPreemption = preemption; | ||
return preemption; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
_previousPreemption = Vector2.Zero; | ||
} | ||
|
||
private const float BasePreemptionFactor = 100; | ||
private readonly Vector2 _preemptionFactor; | ||
private Vector2 _previousPreemption = Vector2.Zero; | ||
} |
53 changes: 53 additions & 0 deletions
53
...ces/Prediction/Handling/MouseMoving/Decorators/Preemption/StabilizedPreemptionComputer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Numerics; | ||
using CommunityToolkit.Diagnostics; | ||
using Serilog; | ||
using SightKeeper.Commons; | ||
using SightKeeper.Domain.Model; | ||
|
||
namespace SightKeeper.Services.Prediction.Handling.MouseMoving.Decorators.Preemption; | ||
|
||
public sealed class StabilizedPreemptionComputer : PreemptionComputer | ||
{ | ||
public StabilizedPreemptionComputer(Profile profile) | ||
{ | ||
Guard.IsNotNull(profile.PreemptionSettings?.StabilizationSettings); | ||
_preemptionFactor = new Vector2(profile.PreemptionSettings.HorizontalFactor, profile.PreemptionSettings.VerticalFactor); | ||
_velocities = new List<Vector2>(profile.PreemptionSettings.StabilizationSettings.BufferSize); | ||
_method = profile.PreemptionSettings.StabilizationSettings.Method switch | ||
{ | ||
PreemptionStabilizationMethod.Median => EnumerableExtensions.Median, | ||
PreemptionStabilizationMethod.Mean => Enumerable.Average, | ||
_ => ThrowHelper.ThrowArgumentOutOfRangeException<Func<IEnumerable<float>, float>>() | ||
}; | ||
} | ||
|
||
public Vector2 ComputePreemption(Vector2 moveVector, TimeSpan timeDelta) | ||
{ | ||
var targetVelocity = moveVector + _previousPreemption; | ||
targetVelocity /= (float)timeDelta.TotalMilliseconds; | ||
|
||
if (_velocities.Count == _velocities.Capacity) | ||
_velocities.RemoveAt(0); | ||
_velocities.Add(targetVelocity); | ||
|
||
Vector2 preemption = new( | ||
_method(_velocities.Select(velocity => velocity.X)), | ||
_method(_velocities.Select(velocity => velocity.Y))); | ||
|
||
preemption *= BasePreemptionFactor * _preemptionFactor; | ||
Log.ForContext<StabilizedPreemptionComputer>().Debug("Preemption is {Preemption}", preemption); | ||
_previousPreemption = preemption; | ||
return preemption; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
_previousPreemption = Vector2.Zero; | ||
} | ||
|
||
private const float BasePreemptionFactor = 100; | ||
private readonly Vector2 _preemptionFactor; | ||
private readonly List<Vector2> _velocities; | ||
private readonly Func<IEnumerable<float>, float> _method; | ||
private Vector2 _previousPreemption = Vector2.Zero; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters