Skip to content

Commit

Permalink
Refactor: Remove interp from smooth version
Browse files Browse the repository at this point in the history
Previously interp was required to get raw tablet coordinates which the smooth version uses. Now, any filter can get raw coords.
Removing interp has been tested to change coords by AT MOST 50 lines (nearly always far below this) and only when the distance hit the flat part of the funcion at Y = 15.
(50 lines is around 0.5-0.25mm)
  • Loading branch information
Kuuuube committed Oct 20, 2022
1 parent c1baecf commit ae17ff4
Showing 1 changed file with 30 additions and 60 deletions.
90 changes: 30 additions & 60 deletions Kuuube's_Chatter_Exterminator/Chatter_Exterminator_Smooth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,86 +6,56 @@

namespace Kuuube_s_Chatter_Exterminator
{
using static MathF;

[PluginName("Kuuube's CHATTER EXTERMINATOR SMOOTH")]
public class Kuuube_s_CHATTER_EXTERMINATOR_SMOOTH : AsyncPositionedPipelineElement<IDeviceReport>
public class Kuuube_s_CHATTER_EXTERMINATOR_SMOOTH : IPositionedPipelineElement<IDeviceReport>
{
public Kuuube_s_CHATTER_EXTERMINATOR_SMOOTH() { }

private bool isReady;
private Vector2 position;
private Vector2 targetPos, calcTarget;
private readonly float threshold = 0.9f;
private readonly float _AntichatterOffsetY = 15;
private readonly float _Xoffset = 1.96f;
private readonly float _FuncStretch = 1.7f;
private uint pressure;
private Vector2 LastPos;
private readonly float weight = 0.9f;
private readonly float Y_Offset = 15;
private readonly float Stretch = 1.96f;
private readonly float X_Offset = 1.7f;

public Vector2 Filter(Vector2 calcTarget)
public Vector2 Filter(Vector2 input)
{
if (!this.isReady)
{
this.position = calcTarget;
this.isReady = true;
return calcTarget;
}

var delta = calcTarget - this.position;
var distance = Vector2.Distance(this.position, calcTarget);

float target = 1 - threshold;
float weight = (float)(1.0 - (1.0 / Pow((float)(1.0 / target), (float)1 / 1000)));
Vector2 Delta = new Vector2();
{
Delta.X = input.X - LastPos.X;
Delta.Y = input.Y - LastPos.Y;
}

var weightModifier = (float)Pow(((distance * -1 + (Chatter_Extermination_Strength - _FuncStretch)) / _Xoffset), 19) + _AntichatterOffsetY;
float distance = (float)MathF.Sqrt(MathF.Pow(Delta.X, 2) + MathF.Pow(Delta.Y, 2));

// Limit minimum
if (weightModifier + _AntichatterOffsetY < 0)
weightModifier = 0;
else
weightModifier += _AntichatterOffsetY;
float function = MathF.Pow(((distance * -1 + (Chatter_Extermination_Strength - X_Offset)) / Stretch), 19) + Y_Offset;

weightModifier = weight / weightModifier;
weightModifier = Math.Clamp(weightModifier, 0, 1);
this.position += delta * weightModifier;
float weightModifier = Math.Clamp(function + Y_Offset, 0, float.MaxValue);

return this.position;
}
weightModifier = Math.Clamp(weight / weightModifier, 0, 1);

LastPos.X += (Delta.X * weightModifier);
LastPos.Y += (Delta.Y * weightModifier);

protected override void ConsumeState()
{
if (State is ITabletReport report)
{
this.targetPos = report.Position;
this.pressure = report.Pressure;
calcTarget = targetPos;
}
else
calcTarget = targetPos;
return LastPos;
}

protected override void UpdateState()
public event Action<IDeviceReport> Emit;

public void Consume(IDeviceReport value)
{
if (State is ITabletReport report)
if (value is ITabletReport report)
{
report.Position = Filter(calcTarget);
report.Pressure = this.pressure;
State = report;
report.Position = Filter(report.Position);
value = report;
}

if (PenIsInRange() || State is not ITabletReport)
{
OnEmit();
}
Emit?.Invoke(value);
}

public override PipelinePosition Position => PipelinePosition.PreTransform;
public PipelinePosition Position => PipelinePosition.PreTransform;

[Property("Chatter Extermination Strength"), DefaultPropertyValue(6f), ToolTip
("Kuuube's CHATTER EXTERMINATOR SMOOTH:\n\n" +
"Recommended settings are 6-7 for dragging and 15-16 for hovering.\n" +
"However, any value above zero is accepted.\n" +
"Frequency is unused and only present due to restrictions on OTD's plugin system.")]
public float Chatter_Extermination_Strength { set; get; } = 3;
"However, any value above zero is accepted.")]
public float Chatter_Extermination_Strength { set; get; }
}
}

0 comments on commit ae17ff4

Please sign in to comment.