Skip to content

Commit 22758f0

Browse files
committed
Update to v0.4.2 API and switch coding style
1 parent 659701b commit 22758f0

1 file changed

Lines changed: 35 additions & 35 deletions

File tree

HawkuFilters/NoiseReduction.cs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@ namespace OpenTabletDriver.Plugin
99
[PluginName("TabletDriver Noise Reduction")]
1010
public class TabletDriverNoiseReduction : IFilter
1111
{
12-
private LinkedList<Vector2> _buffer = new LinkedList<Vector2>();
13-
private float _distThreshold, _distMax;
14-
private const int _iterations = 10;
15-
private int _samples = 10;
16-
private Vector2 _lastPoint;
12+
private readonly LinkedList<Vector2> buffer = new LinkedList<Vector2>();
13+
private float distThreshold, distMax;
14+
private const int iterations = 10;
15+
private int samples = 10;
16+
private Vector2 lastPoint;
1717

1818
public Vector2 Filter(Vector2 point)
1919
{
2020
SetTarget(point);
2121

22-
if (_buffer.Count <= 1)
22+
if (this.buffer.Count <= 1)
2323
{
2424
return SetOutput(point);
2525
}
2626

2727
// Calculate geometric median from the buffer positions
28-
GetGeometricMedianVector(ref _lastPoint);
28+
this.lastPoint = GetGeometricMedianVector(this.lastPoint);
2929

3030
// Distance between latest position and ring buffer
31-
var distance = Vector2.Distance(point, _lastPoint);
31+
var distance = Vector2.Distance(point, this.lastPoint);
3232

3333
// Distance larger than threshold -> modify the ring buffer
3434
if (distance > DistThreshold)
@@ -39,23 +39,23 @@ public Vector2 Filter(Vector2 point)
3939
// Distance ratio should be between 0.0 and 1.0
4040
// 0.0 -> distance == distanceThreshold
4141
// 1.0 -> distance == distanceMaximum
42-
distanceRatio = (distance - DistThreshold) / (_distMax - DistThreshold);
42+
distanceRatio = (distance - DistThreshold) / (this.distMax - DistThreshold);
4343

4444
if (distanceRatio >= 1f)
4545
{
4646
// Distance larger than maximum -> fill buffer with the latest target position
47-
var bufCount = _buffer.Count;
48-
_buffer.Clear();
47+
var bufCount = this.buffer.Count;
48+
this.buffer.Clear();
4949
for (int i = 0; i < bufCount; i++)
50-
_buffer.AddLast(point);
50+
this.buffer.AddLast(point);
5151
return SetOutput(point);
5252
}
5353
else
5454
{
5555
// Move buffer positions and current position towards the latest target using linear interpolation
5656
// Amount of movement is the distance ratio between threshold and maximum
5757

58-
var bufNode = _buffer.First;
58+
var bufNode = buffer.First;
5959

6060
while (bufNode != null)
6161
{
@@ -66,29 +66,29 @@ public Vector2 Filter(Vector2 point)
6666
bufNode = bufNode.Next;
6767
}
6868

69-
_lastPoint.X += (float)((point.X - _lastPoint.X) * distanceRatio);
70-
_lastPoint.Y += (float)((point.Y - _lastPoint.Y) * distanceRatio);
69+
this.lastPoint.X += (float)((point.X - this.lastPoint.X) * distanceRatio);
70+
this.lastPoint.Y += (float)((point.Y - this.lastPoint.Y) * distanceRatio);
7171

72-
return _lastPoint;
72+
return this.lastPoint;
7373
}
7474
}
7575
return SetOutput(point);
7676
}
7777

7878
private void SetTarget(Vector2 point)
7979
{
80-
_buffer.AddLast(point);
81-
while (_buffer.Count > Samples)
82-
_buffer.RemoveFirst();
80+
buffer.AddLast(point);
81+
while (buffer.Count > Samples)
82+
buffer.RemoveFirst();
8383
}
8484

8585
private Vector2 SetOutput(Vector2 point)
8686
{
87-
_lastPoint = point;
87+
this.lastPoint = point;
8888
return point;
8989
}
9090

91-
private Vector2 GetGeometricMedianVector(ref Vector2 point)
91+
private Vector2 GetGeometricMedianVector(Vector2 point)
9292
{
9393
var candidate = new Vector2();
9494
var next = new Vector2();
@@ -98,15 +98,15 @@ private Vector2 GetGeometricMedianVector(ref Vector2 point)
9898

9999
// Calculate the starting position
100100
if (!GetAverageVector(ref candidate))
101-
return _lastPoint;
101+
return this.lastPoint;
102102

103103
// Iterate
104-
for (int iteration = 0; iteration < _iterations; iteration++)
104+
for (int iteration = 0; iteration < iterations; iteration++)
105105
{
106106
denominator = 0;
107107

108108
// Loop through the buffer and calculate a denominator.
109-
foreach (var bufferPoint in _buffer)
109+
foreach (var bufferPoint in buffer)
110110
{
111111
distance = Vector2.Distance(candidate, bufferPoint);
112112

@@ -121,7 +121,7 @@ private Vector2 GetGeometricMedianVector(ref Vector2 point)
121121
next.Y = 0;
122122

123123
// Loop through the buffer and calculate a weighted average
124-
foreach (var bufferPoint in _buffer)
124+
foreach (var bufferPoint in buffer)
125125
{
126126
distance = Vector2.Distance(candidate, bufferPoint);
127127

@@ -147,20 +147,20 @@ private Vector2 GetGeometricMedianVector(ref Vector2 point)
147147

148148
private bool GetAverageVector(ref Vector2 point)
149149
{
150-
if (_buffer.Count == 0)
150+
if (buffer.Count == 0)
151151
return false;
152152

153153
point.X = 0;
154154
point.Y = 0;
155155

156-
foreach (var bufferPoint in _buffer)
156+
foreach (var bufferPoint in buffer)
157157
{
158158
point.X += bufferPoint.X;
159159
point.Y += bufferPoint.Y;
160160
}
161161

162-
point.X /= _buffer.Count;
163-
point.Y /= _buffer.Count;
162+
point.X /= buffer.Count;
163+
point.Y /= buffer.Count;
164164
return true;
165165
}
166166

@@ -169,20 +169,20 @@ public int Samples
169169
{
170170
set
171171
{
172-
_samples = Math.Clamp(value, 0, 20);
172+
this.samples = Math.Clamp(value, 0, 20);
173173
}
174-
get => _samples;
174+
get => this.samples;
175175
}
176176

177-
[UnitProperty("Distance Threshold", "px")]
177+
[Property("Distance Threshold"), Unit("px")]
178178
public float DistThreshold
179179
{
180180
set
181181
{
182-
_distThreshold = Math.Clamp(value, 0, 10);
183-
_distMax = value * 2;
182+
this.distThreshold = Math.Clamp(value, 0, 10);
183+
distMax = value * 2;
184184
}
185-
get => _distThreshold;
185+
get => this.distThreshold;
186186
}
187187

188188
public FilterStage FilterStage => FilterStage.PostTranspose;

0 commit comments

Comments
 (0)