Skip to content

Commit

Permalink
优化参数曲线简化算法
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuYunPlayer committed Jun 5, 2024
1 parent 462ce8f commit 7843e88
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
33 changes: 33 additions & 0 deletions TuneLab.Base/Science/MathUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,39 @@ public static double[] MonotonicHermiteInterpolation(this IReadOnlyList<Point> p
return HermiteInterpolation.Calculate(points, xs, mMonotonicHermiteSlopeCalculator);
}

public static List<Point> Simplify(this IReadOnlyList<Point> points, double gap, double tolerance)
{
if (points.Count <= 2)
return points.ToList();

var last = points.ConstFirst();
var result = new List<Point>() { last };
for (int i = 1; i < points.Count - 1; i++)
{
var point = points[i];
var next = points[i + 1];
var kLast = Slope(point, last);
var kNext = Slope(point, next);
if (kLast == 0 && kNext == 0)
continue;

if (point.X - last.X < gap)
{
if (kLast * kNext > 0)
{
if (Math.Abs(Math.Log2(kLast / kNext)) < tolerance)
continue;
}
}

last = point;
result.Add(point);
}
result.Add(points.ConstLast());

return result;
}

public static List<Point> Simplify(this IReadOnlyList<Point> points, double gap)
{
if (points.Count <= 2)
Expand Down
4 changes: 2 additions & 2 deletions TuneLab/Views/AutomationRendererOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public void Move(Avalonia.Point mousePosition)
mAutomation.DiscardTo(mHead);
foreach (var line in mPointLines)
{
mAutomation.AddLine(line.Simplify(5), 5);
mAutomation.AddLine(line.Simplify(5, 2), 5);
}
}

Expand All @@ -320,7 +320,7 @@ public void Up()
mAutomation.DiscardTo(mHead);
foreach (var line in mPointLines)
{
mAutomation.AddLine(line.Simplify(5), 5);
mAutomation.AddLine(line.Simplify(5, 2), 5);
}
AutomationRenderer.Part.EndMergeDirty();
mAutomation.Commit();
Expand Down
4 changes: 2 additions & 2 deletions TuneLab/Views/PianoGridOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ public void Move(Avalonia.Point mousePosition)
PianoGrid.Part.Pitch.DiscardTo(mHead);
foreach (var line in mPointLines)
{
PianoGrid.Part.Pitch.AddLine(line.Simplify(5), 5);
PianoGrid.Part.Pitch.AddLine(line.Simplify(5, 2), 5);
}
}

Expand All @@ -1181,7 +1181,7 @@ public void Up()
PianoGrid.Part.EndMergeDirty();
foreach (var line in mPointLines)
{
PianoGrid.Part.Pitch.AddLine(line.Simplify(5), 5);
PianoGrid.Part.Pitch.AddLine(line.Simplify(5, 2), 5);
}
PianoGrid.Part.Pitch.Commit();
mPointLines.Clear();
Expand Down

0 comments on commit 7843e88

Please sign in to comment.