-
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.
Merge pull request #6044 from default0/path-approximation
Add class for efficiently building B-Splines
- Loading branch information
Showing
8 changed files
with
357 additions
and
45 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
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
109 changes: 109 additions & 0 deletions
109
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,109 @@ | ||
// 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 readonly 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 += _ => updateViz(); | ||
|
||
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.GetControlPoints()) | ||
{ | ||
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.GetInputPath(); | ||
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
Oops, something went wrong.