11using System ;
22using 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 ;
0 commit comments