Skip to content

Commit a50a4d0

Browse files
committed
Merge branch 'smoothingfilter' into noisereductionfilter
2 parents 809f02c + 198b144 commit a50a4d0

6 files changed

Lines changed: 100 additions & 22 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule ".modules/OpenTabletDriver"]
2+
path = .modules/OpenTabletDriver
3+
url = https://github.com/InfinityGhost/OpenTabletDriver

.modules/OpenTabletDriver

Submodule OpenTabletDriver added at 01e068b

TabletDriverFilters/TabletDriverFilters.csproj renamed to HawkuFilters/HawkuFilters.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<TargetFramework>netcoreapp3.1</TargetFramework>
55
</PropertyGroup>
66

7-
<ItemGroup>
8-
<PackageReference Include="TabletDriverPlugin" Version="0.3.0" />
7+
<ItemGroup>
8+
<ProjectReference Include="../.modules/OpenTabletDriver/OpenTabletDriver.Plugin/OpenTabletDriver.Plugin.csproj" />
99
</ItemGroup>
1010

1111
</Project>

TabletDriverFilters/TabletDriverNoiseReduction.cs renamed to HawkuFilters/NoiseReduction.cs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
using System;
22
using System.Collections.Generic;
3-
using TabletDriverPlugin.Attributes;
4-
using TabletDriverPlugin.Tablet;
3+
using System.Numerics;
4+
using OpenTabletDriver.Plugin.Attributes;
5+
using OpenTabletDriver.Plugin.Tablet;
56

6-
namespace TabletDriverPlugin
7+
namespace OpenTabletDriver.Plugin
78
{
89
[PluginName("TabletDriver Noise Reduction")]
910
public class TabletDriverNoiseReduction : IFilter
1011
{
11-
private LinkedList<Point> _buffer = new LinkedList<Point>();
12+
private LinkedList<Vector2> _buffer = new LinkedList<Vector2>();
1213
private float _distThreshold, _distMax;
1314
private const int _iterations = 10;
1415
private int _samples = 10;
15-
private Point _lastPoint;
16+
private Vector2 _lastPoint;
1617

17-
public Point Filter(Point point)
18+
public Vector2 Filter(Vector2 point)
1819
{
1920
SetTarget(point);
2021

@@ -27,7 +28,7 @@ public Point Filter(Point point)
2728
GetGeometricMedianVector(ref _lastPoint);
2829

2930
// Distance between latest position and ring buffer
30-
var distance = point.DistanceFrom(_lastPoint);
31+
var distance = Vector2.Distance(point, _lastPoint);
3132

3233
// Distance larger than threshold -> modify the ring buffer
3334
if (distance > DistThreshold)
@@ -53,13 +54,24 @@ public Point Filter(Point point)
5354
{
5455
// Move buffer positions and current position towards the latest target using linear interpolation
5556
// Amount of movement is the distance ratio between threshold and maximum
56-
var bufEnum = _buffer.GetEnumerator();
57+
// var bufEnum = _buffer.GetEnumerator();
5758

5859
// buffer.LerpAdd()
59-
while (bufEnum.MoveNext())
60+
// while (bufEnum.MoveNext())
61+
// {
62+
// bufEnum.Current.X += (float)((point.X - bufEnum.Current.X) * distanceRatio);
63+
// bufEnum.Current.Y += (float)((point.Y - bufEnum.Current.Y) * distanceRatio);
64+
// }
65+
66+
var bufNode = _buffer.First;
67+
68+
while (bufNode != null)
6069
{
61-
bufEnum.Current.X += (float)((point.X - bufEnum.Current.X) * distanceRatio);
62-
bufEnum.Current.Y += (float)((point.Y - bufEnum.Current.Y) * distanceRatio);
70+
var bufPoint = bufNode.Value;
71+
bufPoint.X = (float)((point.X - bufPoint.X) * distanceRatio);
72+
bufPoint.Y = (float)((point.Y - bufPoint.Y) * distanceRatio);
73+
bufNode.Value = bufPoint;
74+
bufNode = bufNode.Next;
6375
}
6476

6577
// outputPosition.LerpAdd()
@@ -74,23 +86,23 @@ public Point Filter(Point point)
7486
return SetOutput(point);
7587
}
7688

77-
private void SetTarget(Point point)
89+
private void SetTarget(Vector2 point)
7890
{
7991
_buffer.AddLast(point);
8092
while (_buffer.Count > Samples)
8193
_buffer.RemoveFirst();
8294
}
8395

84-
private Point SetOutput(Point point)
96+
private Vector2 SetOutput(Vector2 point)
8597
{
8698
_lastPoint = point;
8799
return point;
88100
}
89101

90-
private Point GetGeometricMedianVector(ref Point point)
102+
private Vector2 GetGeometricMedianVector(ref Vector2 point)
91103
{
92-
var candidate = new Point();
93-
var next = new Point();
104+
var candidate = new Vector2();
105+
var next = new Vector2();
94106
var minimumDistance = 0.001;
95107

96108
double denominator, weight, distance;
@@ -107,7 +119,7 @@ private Point GetGeometricMedianVector(ref Point point)
107119
// Loop through the buffer and calculate a denominator.
108120
foreach (var bufferPoint in _buffer)
109121
{
110-
distance = candidate.DistanceFrom(bufferPoint);
122+
distance = Vector2.Distance(candidate, bufferPoint);
111123

112124
if (distance > minimumDistance)
113125
denominator += 1.0 / distance;
@@ -122,7 +134,7 @@ private Point GetGeometricMedianVector(ref Point point)
122134
// Loop through the buffer and calculate a weighted average
123135
foreach (var bufferPoint in _buffer)
124136
{
125-
distance = candidate.DistanceFrom(bufferPoint);
137+
distance = Vector2.Distance(candidate, bufferPoint);
126138

127139
if (distance > minimumDistance)
128140
weight = 1.0 / distance;
@@ -144,7 +156,7 @@ private Point GetGeometricMedianVector(ref Point point)
144156
return point;
145157
}
146158

147-
private bool GetAverageVector(ref Point point)
159+
private bool GetAverageVector(ref Vector2 point)
148160
{
149161
if (_buffer.Count == 0)
150162
return false;

HawkuFilters/Smoothing.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Numerics;
3+
using OpenTabletDriver.Plugin.Attributes;
4+
using OpenTabletDriver.Plugin.Tablet;
5+
6+
namespace TabletDriverFilters.Hawku
7+
{
8+
using static Math;
9+
10+
[PluginName("TabletDriver Smoothing Filter")]
11+
public class Smoothing : IFilter
12+
{
13+
private DateTime? _lastFilterTime;
14+
private Vector2 _lastPos;
15+
private float _timerInterval;
16+
private const float _threshold = 0.63f;
17+
18+
public Vector2 Filter(Vector2 point)
19+
{
20+
var timeDelta = DateTime.Now - _lastFilterTime;
21+
// 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)
23+
{
24+
SetPreviousState(point);
25+
return point;
26+
}
27+
else
28+
{
29+
Vector2 pos = new Vector2(_lastPos.X, _lastPos.Y);
30+
float deltaX = point.X - _lastPos.X;
31+
float deltaY = point.Y - _lastPos.Y;
32+
33+
double stepCount = Latency / TimerInterval;
34+
double target = 1 - _threshold;
35+
double weight = 1.0 - (1.0 / Pow(1.0 / target, 1.0 / stepCount));
36+
37+
pos.X += (float)(deltaX * weight);
38+
pos.Y += (float)(deltaY * weight);
39+
SetPreviousState(pos);
40+
return pos;
41+
}
42+
}
43+
44+
private void SetPreviousState(Vector2 lastPosition)
45+
{
46+
_lastPos = lastPosition;
47+
_lastFilterTime = DateTime.Now;
48+
}
49+
50+
public FilterStage FilterStage => FilterStage.PostTranspose;
51+
52+
[SliderProperty("Latency", 0f, 5f, 2f)]
53+
public float Latency { set; get; }
54+
55+
[UnitProperty("Timer Interval", "hz")]
56+
public float TimerInterval
57+
{
58+
set => _timerInterval = 1000f / value;
59+
get => _timerInterval;
60+
}
61+
}
62+
}

TabletDriverFilters.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
44
VisualStudioVersion = 15.0.26124.0
55
MinimumVisualStudioVersion = 15.0.26124.0
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TabletDriverFilters", "TabletDriverFilters\TabletDriverFilters.csproj", "{EBD5C9CA-5B65-40B9-8923-696819BEF243}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HawkuFilters", "HawkuFilters\HawkuFilters.csproj", "{EBD5C9CA-5B65-40B9-8923-696819BEF243}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution

0 commit comments

Comments
 (0)