1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-06 08:22:56 +08:00

Make approximators share an interface

This commit is contained in:
smoogipoo 2018-11-01 19:00:14 +09:00
parent c1304eca1b
commit 1aae123ff5
7 changed files with 88 additions and 96 deletions

View File

@ -163,10 +163,10 @@ namespace osu.Game.Rulesets.Osu.Edit.Masks.SliderMasks
{ {
case 1: case 1:
case 2: case 2:
result = new LinearApproximator(allControlPoints).CreateLinear(); result = new LinearApproximator().Approximate(allControlPoints);
break; break;
default: default:
result = new BezierApproximator(allControlPoints).CreateBezier(); result = new BezierApproximator().Approximate(allControlPoints);
break; break;
} }

View File

@ -7,23 +7,72 @@ using OpenTK;
namespace osu.Game.Rulesets.Objects namespace osu.Game.Rulesets.Objects
{ {
public readonly ref struct BezierApproximator public struct BezierApproximator : IApproximator
{ {
private readonly int count;
private readonly ReadOnlySpan<Vector2> controlPoints;
private readonly Vector2[] subdivisionBuffer1;
private readonly Vector2[] subdivisionBuffer2;
private const float tolerance = 0.25f; private const float tolerance = 0.25f;
private const float tolerance_sq = tolerance * tolerance; private const float tolerance_sq = tolerance * tolerance;
public BezierApproximator(ReadOnlySpan<Vector2> controlPoints) private int count;
private Vector2[] subdivisionBuffer1;
private Vector2[] subdivisionBuffer2;
/// <summary>
/// Creates a piecewise-linear approximation of a bezier curve, by adaptively repeatedly subdividing
/// the control points until their approximation error vanishes below a given threshold.
/// </summary>
/// <returns>A list of vectors representing the piecewise-linear approximation.</returns>
public List<Vector2> Approximate(ReadOnlySpan<Vector2> controlPoints)
{ {
this.controlPoints = controlPoints; List<Vector2> output = new List<Vector2>();
count = controlPoints.Length; count = controlPoints.Length;
if (count == 0)
return output;
subdivisionBuffer1 = new Vector2[count]; subdivisionBuffer1 = new Vector2[count];
subdivisionBuffer2 = new Vector2[count * 2 - 1]; subdivisionBuffer2 = new Vector2[count * 2 - 1];
Stack<Vector2[]> toFlatten = new Stack<Vector2[]>();
Stack<Vector2[]> freeBuffers = new Stack<Vector2[]>();
// "toFlatten" contains all the curves which are not yet approximated well enough.
// We use a stack to emulate recursion without the risk of running into a stack overflow.
// (More specifically, we iteratively and adaptively refine our curve with a
// <a href="https://en.wikipedia.org/wiki/Depth-first_search">Depth-first search</a>
// over the tree resulting from the subdivisions we make.)
toFlatten.Push(controlPoints.ToArray());
Vector2[] leftChild = subdivisionBuffer2;
while (toFlatten.Count > 0)
{
Vector2[] parent = toFlatten.Pop();
if (isFlatEnough(parent))
{
// If the control points we currently operate on are sufficiently "flat", we use
// an extension to De Casteljau's algorithm to obtain a piecewise-linear approximation
// of the bezier curve represented by our control points, consisting of the same amount
// of points as there are control points.
approximate(parent, output);
freeBuffers.Push(parent);
continue;
}
// If we do not yet have a sufficiently "flat" (in other words, detailed) approximation we keep
// subdividing the curve we are currently operating on.
Vector2[] rightChild = freeBuffers.Count > 0 ? freeBuffers.Pop() : new Vector2[count];
subdivide(parent, leftChild, rightChild);
// We re-use the buffer of the parent for one of the children, so that we save one allocation per iteration.
for (int i = 0; i < count; ++i)
parent[i] = leftChild[i];
toFlatten.Push(rightChild);
toFlatten.Push(parent);
}
output.Add(controlPoints[count - 1]);
return output;
} }
/// <summary> /// <summary>
@ -92,60 +141,5 @@ namespace osu.Game.Rulesets.Objects
output.Add(p); output.Add(p);
} }
} }
/// <summary>
/// Creates a piecewise-linear approximation of a bezier curve, by adaptively repeatedly subdividing
/// the control points until their approximation error vanishes below a given threshold.
/// </summary>
/// <returns>A list of vectors representing the piecewise-linear approximation.</returns>
public List<Vector2> CreateBezier()
{
List<Vector2> output = new List<Vector2>();
if (count == 0)
return output;
Stack<Vector2[]> toFlatten = new Stack<Vector2[]>();
Stack<Vector2[]> freeBuffers = new Stack<Vector2[]>();
// "toFlatten" contains all the curves which are not yet approximated well enough.
// We use a stack to emulate recursion without the risk of running into a stack overflow.
// (More specifically, we iteratively and adaptively refine our curve with a
// <a href="https://en.wikipedia.org/wiki/Depth-first_search">Depth-first search</a>
// over the tree resulting from the subdivisions we make.)
toFlatten.Push(controlPoints.ToArray());
Vector2[] leftChild = subdivisionBuffer2;
while (toFlatten.Count > 0)
{
Vector2[] parent = toFlatten.Pop();
if (isFlatEnough(parent))
{
// If the control points we currently operate on are sufficiently "flat", we use
// an extension to De Casteljau's algorithm to obtain a piecewise-linear approximation
// of the bezier curve represented by our control points, consisting of the same amount
// of points as there are control points.
approximate(parent, output);
freeBuffers.Push(parent);
continue;
}
// If we do not yet have a sufficiently "flat" (in other words, detailed) approximation we keep
// subdividing the curve we are currently operating on.
Vector2[] rightChild = freeBuffers.Count > 0 ? freeBuffers.Pop() : new Vector2[count];
subdivide(parent, leftChild, rightChild);
// We re-use the buffer of the parent for one of the children, so that we save one allocation per iteration.
for (int i = 0; i < count; ++i)
parent[i] = leftChild[i];
toFlatten.Push(rightChild);
toFlatten.Push(parent);
}
output.Add(controlPoints[count - 1]);
return output;
}
} }
} }

View File

@ -7,25 +7,18 @@ using OpenTK;
namespace osu.Game.Rulesets.Objects namespace osu.Game.Rulesets.Objects
{ {
public readonly ref struct CatmullApproximator public readonly struct CatmullApproximator : IApproximator
{ {
/// <summary> /// <summary>
/// The amount of pieces to calculate for each controlpoint quadruplet. /// The amount of pieces to calculate for each controlpoint quadruplet.
/// </summary> /// </summary>
private const int detail = 50; private const int detail = 50;
private readonly ReadOnlySpan<Vector2> controlPoints;
public CatmullApproximator(ReadOnlySpan<Vector2> controlPoints)
{
this.controlPoints = controlPoints;
}
/// <summary> /// <summary>
/// Creates a piecewise-linear approximation of a Catmull-Rom spline. /// Creates a piecewise-linear approximation of a Catmull-Rom spline.
/// </summary> /// </summary>
/// <returns>A list of vectors representing the piecewise-linear approximation.</returns> /// <returns>A list of vectors representing the piecewise-linear approximation.</returns>
public List<Vector2> CreateCatmull() public List<Vector2> Approximate(ReadOnlySpan<Vector2> controlPoints)
{ {
var result = new List<Vector2>((controlPoints.Length - 1) * detail * 2); var result = new List<Vector2>((controlPoints.Length - 1) * detail * 2);

View File

@ -8,22 +8,15 @@ using OpenTK;
namespace osu.Game.Rulesets.Objects namespace osu.Game.Rulesets.Objects
{ {
public readonly ref struct CircularArcApproximator public readonly struct CircularArcApproximator : IApproximator
{ {
private const float tolerance = 0.1f; private const float tolerance = 0.1f;
private readonly ReadOnlySpan<Vector2> controlPoints;
public CircularArcApproximator(ReadOnlySpan<Vector2> controlPoints)
{
this.controlPoints = controlPoints;
}
/// <summary> /// <summary>
/// Creates a piecewise-linear approximation of a circular arc curve. /// Creates a piecewise-linear approximation of a circular arc curve.
/// </summary> /// </summary>
/// <returns>A list of vectors representing the piecewise-linear approximation.</returns> /// <returns>A list of vectors representing the piecewise-linear approximation.</returns>
public List<Vector2> CreateArc() public List<Vector2> Approximate(ReadOnlySpan<Vector2> controlPoints)
{ {
Vector2 a = controlPoints[0]; Vector2 a = controlPoints[0];
Vector2 b = controlPoints[1]; Vector2 b = controlPoints[1];

View File

@ -0,0 +1,19 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using OpenTK;
namespace osu.Game.Rulesets.Objects
{
public interface IApproximator
{
/// <summary>
/// Approximates a path by interpolating a sequence of control points.
/// </summary>
/// <param name="controlPoints">The control points of the path.</param>
/// <returns>A set of points that lie on the path.</returns>
List<Vector2> Approximate(ReadOnlySpan<Vector2> controlPoints);
}
}

View File

@ -7,16 +7,9 @@ using OpenTK;
namespace osu.Game.Rulesets.Objects namespace osu.Game.Rulesets.Objects
{ {
public readonly ref struct LinearApproximator public readonly struct LinearApproximator : IApproximator
{ {
private readonly ReadOnlySpan<Vector2> controlPoints; public List<Vector2> Approximate(ReadOnlySpan<Vector2> controlPoints)
public LinearApproximator(ReadOnlySpan<Vector2> controlPoints)
{
this.controlPoints = controlPoints;
}
public List<Vector2> CreateLinear()
{ {
var result = new List<Vector2>(controlPoints.Length); var result = new List<Vector2>(controlPoints.Length);

View File

@ -28,14 +28,14 @@ namespace osu.Game.Rulesets.Objects
switch (PathType) switch (PathType)
{ {
case PathType.Linear: case PathType.Linear:
return new LinearApproximator(subControlPoints).CreateLinear(); return new LinearApproximator().Approximate(subControlPoints);
case PathType.PerfectCurve: case PathType.PerfectCurve:
//we can only use CircularArc iff we have exactly three control points and no dissection. //we can only use CircularArc iff we have exactly three control points and no dissection.
if (ControlPoints.Length != 3 || subControlPoints.Length != 3) if (ControlPoints.Length != 3 || subControlPoints.Length != 3)
break; break;
// Here we have exactly 3 control points. Attempt to fit a circular arc. // Here we have exactly 3 control points. Attempt to fit a circular arc.
List<Vector2> subpath = new CircularArcApproximator(subControlPoints).CreateArc(); List<Vector2> subpath = new CircularArcApproximator().Approximate(subControlPoints);
// If for some reason a circular arc could not be fit to the 3 given points, fall back to a numerically stable bezier approximation. // If for some reason a circular arc could not be fit to the 3 given points, fall back to a numerically stable bezier approximation.
if (subpath.Count == 0) if (subpath.Count == 0)
@ -43,10 +43,10 @@ namespace osu.Game.Rulesets.Objects
return subpath; return subpath;
case PathType.Catmull: case PathType.Catmull:
return new CatmullApproximator(subControlPoints).CreateCatmull(); return new CatmullApproximator().Approximate(subControlPoints);
} }
return new BezierApproximator(subControlPoints).CreateBezier(); return new BezierApproximator().Approximate(subControlPoints);
} }
private void calculatePath() private void calculatePath()