Skip to content

Commit cafdf72

Browse files
committed
Migrate to Interpolator and make use of SIMD
1 parent 40c141c commit cafdf72

3 files changed

Lines changed: 77 additions & 67 deletions

File tree

.modules/OpenTabletDriver

Submodule OpenTabletDriver updated 47 files

DevocubFilters/AntiChatter.cs

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,70 @@
22
using System.Numerics;
33
using OpenTabletDriver.Plugin.Attributes;
44
using OpenTabletDriver.Plugin.Tablet;
5+
using OpenTabletDriver.Plugin.Tablet.Interpolator;
6+
using OpenTabletDriver.Plugin.Timers;
57

68
namespace TabletDriverFilters.Devocub
79
{
810
using static MathF;
911

1012
[PluginName("TabletDriver AntiChatter Filter")]
11-
public class AntiChatter : IFilter
13+
public class AntiChatter : Interpolator
1214
{
13-
private Vector2 _lastPos;
14-
private float _timerInterval;
15-
private const float _threshold = 0.9f;
16-
private float _latency = 2.0f;
15+
public AntiChatter(ITimer scheduler) : base(scheduler) { }
16+
17+
private Vector2 lastPos;
18+
private Vector2 targetPos;
19+
private SyntheticTabletReport report;
20+
private const float threshold = 0.9f;
21+
private float latency = 2.0f;
22+
23+
public override void UpdateState(SyntheticTabletReport report)
24+
{
25+
this.targetPos = report.Position;
26+
this.report = report;
27+
}
28+
29+
public override SyntheticTabletReport Interpolate()
30+
{
31+
this.report.Position = Filter(this.targetPos);
32+
return this.report;
33+
}
1734

1835
public Vector2 Filter(Vector2 point)
1936
{
2037
Vector2 calcTarget = new Vector2();
21-
float deltaX, deltaY, distance, weightModifier, predictionModifier;
22-
23-
if (_lastPos == null)
24-
{
25-
_lastPos = point;
26-
return point;
27-
}
38+
Vector2 delta;
39+
float distance, weightModifier, predictionModifier;
2840

2941
if (PredictionEnabled)
3042
{
3143
// Calculate predicted position onNewPacket
32-
if (_lastPos.X != point.X || _lastPos.Y != point.Y)
44+
if (this.lastPos.X != point.X || this.lastPos.Y != point.Y)
3345
{
3446
// Calculate distance between last 2 packets and prediction
35-
deltaX = point.X - _lastPos.X;
36-
deltaY = point.Y - _lastPos.Y;
37-
distance = Sqrt(deltaX * deltaX + deltaY * deltaY);
47+
delta = point - lastPos;
48+
distance = Vector2.Distance(lastPos, point);
3849
predictionModifier = 1 / Cosh((distance - PredictionOffsetX) * PredictionSharpness) * PredictionStrength + PredictionOffsetY;
3950

4051
// Apply prediction
41-
deltaX *= predictionModifier;
42-
deltaY *= predictionModifier;
52+
delta *= predictionModifier;
4353

4454
// Update predicted position
45-
calcTarget.X = (float)(point.X + deltaX);
46-
calcTarget.Y = (float)(point.Y + deltaY);
55+
calcTarget = point + delta;
4756

4857
// Update old position for further prediction
49-
_lastPos.X = point.X;
50-
_lastPos.Y = point.Y;
58+
this.lastPos = point;
5159
}
5260
}
5361
else
54-
{
55-
calcTarget.X = point.X;
56-
calcTarget.Y = point.Y;
57-
}
62+
calcTarget = point;
5863

59-
deltaX = calcTarget.X - _lastPos.X;
60-
deltaY = calcTarget.Y - _lastPos.Y;
61-
distance = Sqrt(deltaX * deltaX + deltaY * deltaY);
64+
delta = calcTarget - lastPos;
65+
distance = Vector2.Distance(lastPos, calcTarget);
6266

6367
float stepCount = Latency / TimerInterval;
64-
float target = 1 - _threshold;
68+
float target = 1 - threshold;
6569
float weight = (float)(1.0 - (1.0 / Pow((float)(1.0 / target), (float)(1.0 / stepCount))));
6670

6771
// Devocub smoothing
@@ -76,26 +80,23 @@ public Vector2 Filter(Vector2 point)
7680

7781
weightModifier = weight / weightModifier;
7882
weightModifier = Math.Clamp(weightModifier, 0, 1);
79-
_lastPos.X += (float)(deltaX * weightModifier);
80-
_lastPos.Y += (float)(deltaY * weightModifier);
83+
this.lastPos += delta * weightModifier;
8184

82-
return _lastPos;
85+
return lastPos;
8386
}
8487

85-
public FilterStage FilterStage => FilterStage.PostTranspose;
88+
public static FilterStage FilterStage => FilterStage.PostTranspose;
8689

8790
[SliderProperty("Latency", 0f, 5f, 2f)]
8891
public float Latency
8992
{
90-
set => _latency = Math.Clamp(value, 0, 1000);
91-
get => _latency;
93+
set => this.latency = Math.Clamp(value, 0, 1000);
94+
get => this.latency;
9295
}
9396

94-
[Property("Timer Interval"), Unit("hz")]
9597
public float TimerInterval
9698
{
97-
set => _timerInterval = 1000f / value;
98-
get => _timerInterval;
99+
get => 1000 / Hertz;
99100
}
100101

101102
[Property("Antichatter Strength")]

HawkuFilters/Smoothing.cs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,70 @@
22
using System.Numerics;
33
using OpenTabletDriver.Plugin.Attributes;
44
using OpenTabletDriver.Plugin.Tablet;
5+
using OpenTabletDriver.Plugin.Tablet.Interpolator;
6+
using OpenTabletDriver.Plugin.Timers;
57

68
namespace TabletDriverFilters.Hawku
79
{
810
using static Math;
911

1012
[PluginName("TabletDriver Smoothing Filter")]
11-
public class Smoothing : IFilter
13+
public class Smoothing : Interpolator
1214
{
13-
private DateTime? _lastFilterTime;
14-
private Vector2 _lastPos;
15-
private float _timerInterval;
16-
private const float _threshold = 0.63f;
15+
public Smoothing(ITimer scheduler) : base(scheduler) { }
1716

18-
public Vector2 Filter(Vector2 point)
17+
private DateTime? lastFilterTime;
18+
private Vector3 targetPos;
19+
private Vector3 lastPos;
20+
private SyntheticTabletReport report;
21+
private const float threshold = 0.63f;
22+
23+
public override void UpdateState(SyntheticTabletReport report)
24+
{
25+
this.targetPos = new Vector3(report.Position, report.Pressure);
26+
this.report = report;
27+
}
28+
29+
public override SyntheticTabletReport Interpolate()
30+
{
31+
var newPoint = Filter(this.targetPos);
32+
report.Position = new Vector2(newPoint.X, newPoint.Y);
33+
report.Pressure = (uint)newPoint.Z;
34+
return report;
35+
}
36+
37+
public Vector3 Filter(Vector3 point)
1938
{
20-
var timeDelta = DateTime.Now - _lastFilterTime;
39+
var timeDelta = DateTime.Now - this.lastFilterTime;
2140
// If a time difference hasn't been established or it has been 100 milliseconds since the last filter
22-
if (timeDelta == null || timeDelta.Value.TotalMilliseconds > 100 || _lastPos == null)
41+
if (timeDelta == null || timeDelta.Value.TotalMilliseconds > 100)
2342
{
24-
SetPreviousState(point);
43+
this.lastPos = point;
44+
this.lastFilterTime = DateTime.Now;
2545
return point;
2646
}
2747
else
2848
{
29-
Vector2 pos = new Vector2(_lastPos.X, _lastPos.Y);
30-
float deltaX = point.X - _lastPos.X;
31-
float deltaY = point.Y - _lastPos.Y;
49+
Vector3 delta = point - this.lastPos;
3250

3351
double stepCount = Latency / TimerInterval;
34-
double target = 1 - _threshold;
52+
double target = 1 - threshold;
3553
double weight = 1.0 - (1.0 / Pow(1.0 / target, 1.0 / stepCount));
3654

37-
pos.X += (float)(deltaX * weight);
38-
pos.Y += (float)(deltaY * weight);
39-
SetPreviousState(pos);
40-
return pos;
55+
this.lastPos += delta * (float)weight;
56+
this.lastFilterTime = DateTime.Now;
57+
return this.lastPos;
4158
}
4259
}
4360

44-
private void SetPreviousState(Vector2 lastPosition)
45-
{
46-
_lastPos = lastPosition;
47-
_lastFilterTime = DateTime.Now;
48-
}
49-
50-
public FilterStage FilterStage => FilterStage.PostTranspose;
61+
public static FilterStage FilterStage => FilterStage.PostTranspose;
5162

5263
[SliderProperty("Latency", 0f, 5f, 2f)]
5364
public float Latency { set; get; }
5465

55-
[Property("Timer Interval"), Unit("hz")]
5666
public float TimerInterval
5767
{
58-
set => _timerInterval = 1000f / value;
59-
get => _timerInterval;
68+
get => 1000 / Hertz;
6069
}
6170
}
6271
}

0 commit comments

Comments
 (0)