-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add class for efficiently building B-Splines
This adds the IncrementalBSplineBuilder class which can be used to build up a B-Spline from a series of linear points efficiently. The class takes special care to keep the number of control points
- Loading branch information
Showing
11 changed files
with
834 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
#if NET6_0 | ||
using System.Collections.Generic; | ||
using System; | ||
using BenchmarkDotNet.Attributes; | ||
using osu.Framework.Utils; | ||
using osuTK; | ||
using osu.Framework.Graphics.Primitives; | ||
|
||
namespace osu.Framework.Benchmarks | ||
{ | ||
public class BenchmarkPathApproximator : BenchmarkTest | ||
{ | ||
private Vector2[] inputArr = Array.Empty<Vector2>(); | ||
|
||
public override void SetUp() | ||
{ | ||
var input = new List<Vector2>(); | ||
var bounds = new RectangleF(0, 0, 512, 384); | ||
|
||
var current = new Vector2( | ||
Random.Shared.Next((int)bounds.Left, (int)bounds.Right + 1), | ||
Random.Shared.Next((int)bounds.Top, (int)bounds.Bottom + 1) | ||
); | ||
while (input.Count < 25_000) | ||
{ | ||
input.Add(current); | ||
|
||
Vector2 next = current + new Vector2( | ||
(Random.Shared.NextSingle() - 0.5f) * 20f, | ||
(Random.Shared.NextSingle() - 0.5f) * 20f | ||
); | ||
while (!bounds.Contains(next)) | ||
{ | ||
next = current + new Vector2( | ||
(Random.Shared.NextSingle() - 0.5f) * 20f, | ||
(Random.Shared.NextSingle() - 0.5f) * 20f | ||
); | ||
} | ||
current = next; | ||
} | ||
|
||
inputArr = input.ToArray(); | ||
} | ||
|
||
[Benchmark] | ||
public List<Vector2> GetBSpline() | ||
{ | ||
return PathApproximator.PiecewiseLinearToBSpline(inputArr, 3); | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
osu.Framework.Tests/Visual/Drawables/TestSceneInteractivePathDrawing.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using osu.Framework.Graphics; | ||
using osuTK.Graphics; | ||
using osu.Framework.Graphics.Containers; | ||
using osu.Framework.Graphics.Shapes; | ||
using osu.Framework.Graphics.Lines; | ||
using osu.Framework.Input.Events; | ||
using osuTK.Input; | ||
using osu.Framework.Utils; | ||
using osuTK; | ||
|
||
namespace osu.Framework.Tests.Visual.Drawables | ||
{ | ||
[System.ComponentModel.Description("Approximate a hand-drawn path with minimal B-spline control points")] | ||
public partial class TestSceneInteractivePathDrawing : FrameworkTestScene | ||
{ | ||
private readonly Path rawDrawnPath; | ||
private readonly Path approximatedDrawnPath; | ||
private readonly Container controlPointViz; | ||
|
||
private IncrementalBSplineBuilder bSplineBuilder = new IncrementalBSplineBuilder(); | ||
|
||
public TestSceneInteractivePathDrawing() | ||
{ | ||
Child = new Container | ||
{ | ||
RelativeSizeAxes = Axes.Both, | ||
Children = new Drawable[] | ||
{ | ||
rawDrawnPath = new Path | ||
{ | ||
Colour = Color4.DeepPink, | ||
PathRadius = 5, | ||
}, | ||
approximatedDrawnPath = new Path | ||
{ | ||
Colour = Color4.Blue, | ||
PathRadius = 3, | ||
}, | ||
controlPointViz = new Container | ||
{ | ||
RelativeSizeAxes = Axes.Both, | ||
Alpha = 0.5f, | ||
}, | ||
} | ||
}; | ||
|
||
updateViz(); | ||
OnUpdate += _ => updateControlPointsViz(); | ||
|
||
AddStep("Reset path", () => | ||
{ | ||
bSplineBuilder.Clear(); | ||
}); | ||
|
||
AddSliderStep($"{nameof(bSplineBuilder.Degree)}", 1, 5, 3, v => | ||
{ | ||
bSplineBuilder.Degree = v; | ||
}); | ||
AddSliderStep($"{nameof(bSplineBuilder.Tolerance)}", 0f, 1f, 0.1f, v => | ||
{ | ||
bSplineBuilder.Tolerance = v; | ||
}); | ||
} | ||
|
||
private void updateControlPointsViz() | ||
{ | ||
controlPointViz.Clear(); | ||
foreach (var cp in bSplineBuilder.ControlPoints) | ||
{ | ||
controlPointViz.Add(new Box | ||
{ | ||
Origin = Anchor.Centre, | ||
Size = new Vector2(10), | ||
Position = cp, | ||
Colour = Color4.LightGreen, | ||
}); | ||
} | ||
} | ||
|
||
protected override bool OnDragStart(DragStartEvent e) | ||
{ | ||
if (e.Button == MouseButton.Left) | ||
{ | ||
bSplineBuilder.Clear(); | ||
bSplineBuilder.AddLinearPoint(rawDrawnPath.ToLocalSpace(ToScreenSpace(e.MousePosition))); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private void updateViz() | ||
{ | ||
rawDrawnPath.Vertices = bSplineBuilder.InputPath; | ||
approximatedDrawnPath.Vertices = bSplineBuilder.OutputPath; | ||
|
||
updateControlPointsViz(); | ||
} | ||
|
||
protected override void OnDrag(DragEvent e) | ||
{ | ||
bSplineBuilder.AddLinearPoint(rawDrawnPath.ToLocalSpace(ToScreenSpace(e.MousePosition))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.