mirror of
https://github.com/ppy/osu.git
synced 2024-12-15 02:42:54 +08:00
Merge pull request #3644 from smoogipoo/slider-placement
Implement slider placement
This commit is contained in:
commit
54fede4559
19
osu.Game.Rulesets.Osu.Tests/TestCaseSliderPlacementMask.cs
Normal file
19
osu.Game.Rulesets.Osu.Tests/TestCaseSliderPlacementMask.cs
Normal 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 osu.Game.Rulesets.Edit;
|
||||
using osu.Game.Rulesets.Objects;
|
||||
using osu.Game.Rulesets.Objects.Drawables;
|
||||
using osu.Game.Rulesets.Osu.Edit.Masks.SliderMasks;
|
||||
using osu.Game.Rulesets.Osu.Objects;
|
||||
using osu.Game.Rulesets.Osu.Objects.Drawables;
|
||||
using osu.Game.Tests.Visual;
|
||||
|
||||
namespace osu.Game.Rulesets.Osu.Tests
|
||||
{
|
||||
public class TestCaseSliderPlacementMask : HitObjectPlacementMaskTestCase
|
||||
{
|
||||
protected override DrawableHitObject CreateHitObject(HitObject hitObject) => new DrawableSlider((Slider)hitObject);
|
||||
protected override PlacementMask CreateMask() => new SliderPlacementMask();
|
||||
}
|
||||
}
|
@ -1,11 +1,13 @@
|
||||
// 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.Collections.Generic;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Rulesets.Osu.Objects;
|
||||
using osu.Game.Rulesets.Osu.Objects.Drawables.Pieces;
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics;
|
||||
|
||||
namespace osu.Game.Rulesets.Osu.Edit.Masks.SliderMasks.Components
|
||||
@ -13,18 +15,20 @@ namespace osu.Game.Rulesets.Osu.Edit.Masks.SliderMasks.Components
|
||||
public class SliderBodyPiece : CompositeDrawable
|
||||
{
|
||||
private readonly Slider slider;
|
||||
private readonly SnakingSliderBody body;
|
||||
private readonly ManualSliderBody body;
|
||||
|
||||
public SliderBodyPiece(Slider slider)
|
||||
{
|
||||
this.slider = slider;
|
||||
InternalChild = body = new SnakingSliderBody(slider)
|
||||
|
||||
InternalChild = body = new ManualSliderBody
|
||||
{
|
||||
AccentColour = Color4.Transparent,
|
||||
PathWidth = slider.Scale * 64
|
||||
};
|
||||
|
||||
slider.PositionChanged += _ => updatePosition();
|
||||
slider.ScaleChanged += _ => body.PathWidth = slider.Scale * 64;
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
@ -41,12 +45,15 @@ namespace osu.Game.Rulesets.Osu.Edit.Masks.SliderMasks.Components
|
||||
{
|
||||
base.Update();
|
||||
|
||||
slider.Path.Calculate();
|
||||
|
||||
var vertices = new List<Vector2>();
|
||||
slider.Path.GetPathToProgress(vertices, 0, 1);
|
||||
|
||||
body.SetVertices(vertices);
|
||||
|
||||
Size = body.Size;
|
||||
OriginPosition = body.PathOffset;
|
||||
|
||||
// Need to cause one update
|
||||
body.UpdateProgress(0);
|
||||
body.Refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,180 @@
|
||||
// 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 System.Linq;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Extensions.IEnumerableExtensions;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Input.Events;
|
||||
using osu.Framework.MathUtils;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Rulesets.Edit;
|
||||
using osu.Game.Rulesets.Objects.Types;
|
||||
using osu.Game.Rulesets.Osu.Edit.Masks.SliderMasks.Components;
|
||||
using OpenTK;
|
||||
using OpenTK.Input;
|
||||
|
||||
namespace osu.Game.Rulesets.Osu.Edit.Masks.SliderMasks
|
||||
{
|
||||
public class SliderPlacementMask : PlacementMask
|
||||
{
|
||||
public new Objects.Slider HitObject => (Objects.Slider)base.HitObject;
|
||||
|
||||
private readonly List<Segment> segments = new List<Segment>();
|
||||
private Vector2 cursor;
|
||||
|
||||
private PlacementState state;
|
||||
|
||||
public SliderPlacementMask()
|
||||
: base(new Objects.Slider())
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both;
|
||||
segments.Add(new Segment(Vector2.Zero));
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(OsuColour colours)
|
||||
{
|
||||
InternalChildren = new Drawable[]
|
||||
{
|
||||
new SliderBodyPiece(HitObject),
|
||||
new SliderCirclePiece(HitObject, SliderPosition.Start),
|
||||
new SliderCirclePiece(HitObject, SliderPosition.End),
|
||||
new PathControlPointVisualiser(HitObject),
|
||||
};
|
||||
|
||||
setState(PlacementState.Initial);
|
||||
}
|
||||
|
||||
protected override bool OnMouseMove(MouseMoveEvent e)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case PlacementState.Initial:
|
||||
HitObject.Position = e.MousePosition;
|
||||
return true;
|
||||
case PlacementState.Body:
|
||||
cursor = e.MousePosition - HitObject.Position;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected override bool OnClick(ClickEvent e)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case PlacementState.Initial:
|
||||
beginCurve();
|
||||
break;
|
||||
case PlacementState.Body:
|
||||
switch (e.Button)
|
||||
{
|
||||
case MouseButton.Left:
|
||||
segments.Last().ControlPoints.Add(cursor);
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected override bool OnMouseUp(MouseUpEvent e)
|
||||
{
|
||||
if (state == PlacementState.Body && e.Button == MouseButton.Right)
|
||||
endCurve();
|
||||
return base.OnMouseUp(e);
|
||||
}
|
||||
|
||||
protected override bool OnDoubleClick(DoubleClickEvent e)
|
||||
{
|
||||
segments.Add(new Segment(segments[segments.Count - 1].ControlPoints.Last()));
|
||||
return true;
|
||||
}
|
||||
|
||||
private void beginCurve()
|
||||
{
|
||||
BeginPlacement();
|
||||
|
||||
HitObject.StartTime = EditorClock.CurrentTime;
|
||||
setState(PlacementState.Body);
|
||||
}
|
||||
|
||||
private void endCurve()
|
||||
{
|
||||
updateSlider();
|
||||
EndPlacement();
|
||||
}
|
||||
|
||||
protected override void Update()
|
||||
{
|
||||
base.Update();
|
||||
updateSlider();
|
||||
}
|
||||
|
||||
private void updateSlider()
|
||||
{
|
||||
for (int i = 0; i < segments.Count; i++)
|
||||
segments[i].Calculate(i == segments.Count - 1 ? (Vector2?)cursor : null);
|
||||
|
||||
HitObject.ControlPoints = segments.SelectMany(s => s.ControlPoints).Concat(cursor.Yield()).ToArray();
|
||||
HitObject.PathType = HitObject.ControlPoints.Length > 2 ? PathType.Bezier : PathType.Linear;
|
||||
HitObject.Distance = segments.Sum(s => s.Distance);
|
||||
}
|
||||
|
||||
private void setState(PlacementState newState)
|
||||
{
|
||||
state = newState;
|
||||
}
|
||||
|
||||
private enum PlacementState
|
||||
{
|
||||
Initial,
|
||||
Body,
|
||||
}
|
||||
|
||||
private class Segment
|
||||
{
|
||||
public float Distance { get; private set; }
|
||||
|
||||
public readonly List<Vector2> ControlPoints = new List<Vector2>();
|
||||
|
||||
public Segment(Vector2 offset)
|
||||
{
|
||||
ControlPoints.Add(offset);
|
||||
}
|
||||
|
||||
public void Calculate(Vector2? cursor = null)
|
||||
{
|
||||
Span<Vector2> allControlPoints = stackalloc Vector2[ControlPoints.Count + (cursor.HasValue ? 1 : 0)];
|
||||
|
||||
for (int i = 0; i < ControlPoints.Count; i++)
|
||||
allControlPoints[i] = ControlPoints[i];
|
||||
if (cursor.HasValue)
|
||||
allControlPoints[allControlPoints.Length - 1] = cursor.Value;
|
||||
|
||||
List<Vector2> result;
|
||||
|
||||
switch (allControlPoints.Length)
|
||||
{
|
||||
case 1:
|
||||
case 2:
|
||||
result = PathApproximator.ApproximateLinear(allControlPoints);
|
||||
break;
|
||||
default:
|
||||
result = PathApproximator.ApproximateBezier(allControlPoints);
|
||||
break;
|
||||
}
|
||||
|
||||
Distance = 0;
|
||||
for (int i = 0; i < result.Count - 1; i++)
|
||||
Distance += Vector2.Distance(result[i], result[i + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -31,6 +31,7 @@ namespace osu.Game.Rulesets.Osu.Edit
|
||||
protected override IReadOnlyList<HitObjectCompositionTool> CompositionTools => new HitObjectCompositionTool[]
|
||||
{
|
||||
new HitCircleCompositionTool(),
|
||||
new SliderCompositionTool(),
|
||||
new SpinnerCompositionTool()
|
||||
};
|
||||
|
||||
|
20
osu.Game.Rulesets.Osu/Edit/SliderCompositionTool.cs
Normal file
20
osu.Game.Rulesets.Osu/Edit/SliderCompositionTool.cs
Normal file
@ -0,0 +1,20 @@
|
||||
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using osu.Game.Rulesets.Edit;
|
||||
using osu.Game.Rulesets.Edit.Tools;
|
||||
using osu.Game.Rulesets.Osu.Edit.Masks.SliderMasks;
|
||||
using osu.Game.Rulesets.Osu.Objects;
|
||||
|
||||
namespace osu.Game.Rulesets.Osu.Edit
|
||||
{
|
||||
public class SliderCompositionTool : HitObjectCompositionTool
|
||||
{
|
||||
public SliderCompositionTool()
|
||||
: base(nameof(Slider))
|
||||
{
|
||||
}
|
||||
|
||||
public override PlacementMask CreatePlacementMask() => new SliderPlacementMask();
|
||||
}
|
||||
}
|
@ -85,6 +85,11 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables
|
||||
}
|
||||
|
||||
HitObject.PositionChanged += _ => Position = HitObject.StackedPosition;
|
||||
HitObject.ScaleChanged += _ =>
|
||||
{
|
||||
Body.PathWidth = HitObject.Scale * 64;
|
||||
Ball.Scale = new Vector2(HitObject.Scale);
|
||||
};
|
||||
|
||||
slider.ControlPointsChanged += _ => Body.Refresh();
|
||||
}
|
||||
|
@ -26,15 +26,7 @@ namespace osu.Game.Beatmaps
|
||||
Title = "no beatmaps available!"
|
||||
},
|
||||
BeatmapSet = new BeatmapSetInfo(),
|
||||
BaseDifficulty = new BeatmapDifficulty
|
||||
{
|
||||
DrainRate = 0,
|
||||
CircleSize = 0,
|
||||
OverallDifficulty = 0,
|
||||
ApproachRate = 0,
|
||||
SliderMultiplier = 0,
|
||||
SliderTickRate = 0,
|
||||
},
|
||||
BaseDifficulty = new BeatmapDifficulty(),
|
||||
Ruleset = new DummyRulesetInfo()
|
||||
})
|
||||
{
|
||||
|
@ -1,151 +0,0 @@
|
||||
// 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 readonly ref struct BezierApproximator
|
||||
{
|
||||
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_sq = tolerance * tolerance;
|
||||
|
||||
public BezierApproximator(ReadOnlySpan<Vector2> controlPoints)
|
||||
{
|
||||
this.controlPoints = controlPoints;
|
||||
count = controlPoints.Length;
|
||||
|
||||
subdivisionBuffer1 = new Vector2[count];
|
||||
subdivisionBuffer2 = new Vector2[count * 2 - 1];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Make sure the 2nd order derivative (approximated using finite elements) is within tolerable bounds.
|
||||
/// NOTE: The 2nd order derivative of a 2d curve represents its curvature, so intuitively this function
|
||||
/// checks (as the name suggests) whether our approximation is _locally_ "flat". More curvy parts
|
||||
/// need to have a denser approximation to be more "flat".
|
||||
/// </summary>
|
||||
/// <param name="controlPoints">The control points to check for flatness.</param>
|
||||
/// <returns>Whether the control points are flat enough.</returns>
|
||||
private static bool isFlatEnough(Vector2[] controlPoints)
|
||||
{
|
||||
for (int i = 1; i < controlPoints.Length - 1; i++)
|
||||
if ((controlPoints[i - 1] - 2 * controlPoints[i] + controlPoints[i + 1]).LengthSquared > tolerance_sq * 4)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subdivides n control points representing a bezier curve into 2 sets of n control points, each
|
||||
/// describing a bezier curve equivalent to a half of the original curve. Effectively this splits
|
||||
/// the original curve into 2 curves which result in the original curve when pieced back together.
|
||||
/// </summary>
|
||||
/// <param name="controlPoints">The control points to split.</param>
|
||||
/// <param name="l">Output: The control points corresponding to the left half of the curve.</param>
|
||||
/// <param name="r">Output: The control points corresponding to the right half of the curve.</param>
|
||||
private void subdivide(Vector2[] controlPoints, Vector2[] l, Vector2[] r)
|
||||
{
|
||||
Vector2[] midpoints = subdivisionBuffer1;
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
midpoints[i] = controlPoints[i];
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
l[i] = midpoints[0];
|
||||
r[count - i - 1] = midpoints[count - i - 1];
|
||||
|
||||
for (int j = 0; j < count - i - 1; j++)
|
||||
midpoints[j] = (midpoints[j] + midpoints[j + 1]) / 2;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This uses <a href="https://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm">De Casteljau's algorithm</a> to obtain an optimal
|
||||
/// piecewise-linear approximation of the bezier curve with the same amount of points as there are control points.
|
||||
/// </summary>
|
||||
/// <param name="controlPoints">The control points describing the bezier curve to be approximated.</param>
|
||||
/// <param name="output">The points representing the resulting piecewise-linear approximation.</param>
|
||||
private void approximate(Vector2[] controlPoints, List<Vector2> output)
|
||||
{
|
||||
Vector2[] l = subdivisionBuffer2;
|
||||
Vector2[] r = subdivisionBuffer1;
|
||||
|
||||
subdivide(controlPoints, l, r);
|
||||
|
||||
for (int i = 0; i < count - 1; ++i)
|
||||
l[count + i] = r[i + 1];
|
||||
|
||||
output.Add(controlPoints[0]);
|
||||
for (int i = 1; i < count - 1; ++i)
|
||||
{
|
||||
int index = 2 * i;
|
||||
Vector2 p = 0.25f * (l[index - 1] + 2 * l[index] + l[index + 1]);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
// 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 readonly ref struct CatmullApproximator
|
||||
{
|
||||
/// <summary>
|
||||
/// The amount of pieces to calculate for each controlpoint quadruplet.
|
||||
/// </summary>
|
||||
private const int detail = 50;
|
||||
|
||||
private readonly ReadOnlySpan<Vector2> controlPoints;
|
||||
|
||||
public CatmullApproximator(ReadOnlySpan<Vector2> controlPoints)
|
||||
{
|
||||
this.controlPoints = controlPoints;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a piecewise-linear approximation of a Catmull-Rom spline.
|
||||
/// </summary>
|
||||
/// <returns>A list of vectors representing the piecewise-linear approximation.</returns>
|
||||
public List<Vector2> CreateCatmull()
|
||||
{
|
||||
var result = new List<Vector2>((controlPoints.Length - 1) * detail * 2);
|
||||
|
||||
for (int i = 0; i < controlPoints.Length - 1; i++)
|
||||
{
|
||||
var v1 = i > 0 ? controlPoints[i - 1] : controlPoints[i];
|
||||
var v2 = controlPoints[i];
|
||||
var v3 = i < controlPoints.Length - 1 ? controlPoints[i + 1] : v2 + v2 - v1;
|
||||
var v4 = i < controlPoints.Length - 2 ? controlPoints[i + 2] : v3 + v3 - v2;
|
||||
|
||||
for (int c = 0; c < detail; c++)
|
||||
{
|
||||
result.Add(findPoint(ref v1, ref v2, ref v3, ref v4, (float)c / detail));
|
||||
result.Add(findPoint(ref v1, ref v2, ref v3, ref v4, (float)(c + 1) / detail));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds a point on the spline at the position of a parameter.
|
||||
/// </summary>
|
||||
/// <param name="vec1">The first vector.</param>
|
||||
/// <param name="vec2">The second vector.</param>
|
||||
/// <param name="vec3">The third vector.</param>
|
||||
/// <param name="vec4">The fourth vector.</param>
|
||||
/// <param name="t">The parameter at which to find the point on the spline, in the range [0, 1].</param>
|
||||
/// <returns>The point on the spline at <paramref name="t"/>.</returns>
|
||||
private Vector2 findPoint(ref Vector2 vec1, ref Vector2 vec2, ref Vector2 vec3, ref Vector2 vec4, float t)
|
||||
{
|
||||
float t2 = t * t;
|
||||
float t3 = t * t2;
|
||||
|
||||
Vector2 result;
|
||||
result.X = 0.5f * (2f * vec2.X + (-vec1.X + vec3.X) * t + (2f * vec1.X - 5f * vec2.X + 4f * vec3.X - vec4.X) * t2 + (-vec1.X + 3f * vec2.X - 3f * vec3.X + vec4.X) * t3);
|
||||
result.Y = 0.5f * (2f * vec2.Y + (-vec1.Y + vec3.Y) * t + (2f * vec1.Y - 5f * vec2.Y + 4f * vec3.Y - vec4.Y) * t2 + (-vec1.Y + 3f * vec2.Y - 3f * vec3.Y + vec4.Y) * t3);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,97 +0,0 @@
|
||||
// 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 osu.Framework.MathUtils;
|
||||
using OpenTK;
|
||||
|
||||
namespace osu.Game.Rulesets.Objects
|
||||
{
|
||||
public readonly ref struct CircularArcApproximator
|
||||
{
|
||||
private const float tolerance = 0.1f;
|
||||
|
||||
private readonly ReadOnlySpan<Vector2> controlPoints;
|
||||
|
||||
public CircularArcApproximator(ReadOnlySpan<Vector2> controlPoints)
|
||||
{
|
||||
this.controlPoints = controlPoints;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a piecewise-linear approximation of a circular arc curve.
|
||||
/// </summary>
|
||||
/// <returns>A list of vectors representing the piecewise-linear approximation.</returns>
|
||||
public List<Vector2> CreateArc()
|
||||
{
|
||||
Vector2 a = controlPoints[0];
|
||||
Vector2 b = controlPoints[1];
|
||||
Vector2 c = controlPoints[2];
|
||||
|
||||
float aSq = (b - c).LengthSquared;
|
||||
float bSq = (a - c).LengthSquared;
|
||||
float cSq = (a - b).LengthSquared;
|
||||
|
||||
// If we have a degenerate triangle where a side-length is almost zero, then give up and fall
|
||||
// back to a more numerically stable method.
|
||||
if (Precision.AlmostEquals(aSq, 0) || Precision.AlmostEquals(bSq, 0) || Precision.AlmostEquals(cSq, 0))
|
||||
return new List<Vector2>();
|
||||
|
||||
float s = aSq * (bSq + cSq - aSq);
|
||||
float t = bSq * (aSq + cSq - bSq);
|
||||
float u = cSq * (aSq + bSq - cSq);
|
||||
|
||||
float sum = s + t + u;
|
||||
|
||||
// If we have a degenerate triangle with an almost-zero size, then give up and fall
|
||||
// back to a more numerically stable method.
|
||||
if (Precision.AlmostEquals(sum, 0))
|
||||
return new List<Vector2>();
|
||||
|
||||
Vector2 centre = (s * a + t * b + u * c) / sum;
|
||||
Vector2 dA = a - centre;
|
||||
Vector2 dC = c - centre;
|
||||
|
||||
float r = dA.Length;
|
||||
|
||||
double thetaStart = Math.Atan2(dA.Y, dA.X);
|
||||
double thetaEnd = Math.Atan2(dC.Y, dC.X);
|
||||
|
||||
while (thetaEnd < thetaStart)
|
||||
thetaEnd += 2 * Math.PI;
|
||||
|
||||
double dir = 1;
|
||||
double thetaRange = thetaEnd - thetaStart;
|
||||
|
||||
// Decide in which direction to draw the circle, depending on which side of
|
||||
// AC B lies.
|
||||
Vector2 orthoAtoC = c - a;
|
||||
orthoAtoC = new Vector2(orthoAtoC.Y, -orthoAtoC.X);
|
||||
if (Vector2.Dot(orthoAtoC, b - a) < 0)
|
||||
{
|
||||
dir = -dir;
|
||||
thetaRange = 2 * Math.PI - thetaRange;
|
||||
}
|
||||
|
||||
// We select the amount of points for the approximation by requiring the discrete curvature
|
||||
// to be smaller than the provided tolerance. The exact angle required to meet the tolerance
|
||||
// is: 2 * Math.Acos(1 - TOLERANCE / r)
|
||||
// The special case is required for extremely short sliders where the radius is smaller than
|
||||
// the tolerance. This is a pathological rather than a realistic case.
|
||||
int amountPoints = 2 * r <= tolerance ? 2 : Math.Max(2, (int)Math.Ceiling(thetaRange / (2 * Math.Acos(1 - tolerance / r))));
|
||||
|
||||
List<Vector2> output = new List<Vector2>(amountPoints);
|
||||
|
||||
for (int i = 0; i < amountPoints; ++i)
|
||||
{
|
||||
double fract = (double)i / (amountPoints - 1);
|
||||
double theta = thetaStart + dir * fract * thetaRange;
|
||||
Vector2 o = new Vector2((float)Math.Cos(theta), (float)Math.Sin(theta)) * r;
|
||||
output.Add(centre + o);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
@ -14,7 +14,7 @@ namespace osu.Game.Rulesets.Objects
|
||||
{
|
||||
public double Distance;
|
||||
|
||||
public Vector2[] ControlPoints;
|
||||
public Vector2[] ControlPoints = Array.Empty<Vector2>();
|
||||
|
||||
public PathType PathType = PathType.PerfectCurve;
|
||||
|
||||
@ -28,18 +28,14 @@ namespace osu.Game.Rulesets.Objects
|
||||
switch (PathType)
|
||||
{
|
||||
case PathType.Linear:
|
||||
var result = new List<Vector2>(subControlPoints.Length);
|
||||
foreach (var c in subControlPoints)
|
||||
result.Add(c);
|
||||
|
||||
return result;
|
||||
return PathApproximator.ApproximateLinear(subControlPoints);
|
||||
case PathType.PerfectCurve:
|
||||
//we can only use CircularArc iff we have exactly three control points and no dissection.
|
||||
if (ControlPoints.Length != 3 || subControlPoints.Length != 3)
|
||||
break;
|
||||
|
||||
// Here we have exactly 3 control points. Attempt to fit a circular arc.
|
||||
List<Vector2> subpath = new CircularArcApproximator(subControlPoints).CreateArc();
|
||||
List<Vector2> subpath = PathApproximator.ApproximateCircularArc(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 (subpath.Count == 0)
|
||||
@ -47,10 +43,10 @@ namespace osu.Game.Rulesets.Objects
|
||||
|
||||
return subpath;
|
||||
case PathType.Catmull:
|
||||
return new CatmullApproximator(subControlPoints).CreateCatmull();
|
||||
return PathApproximator.ApproximateCatmull(subControlPoints);
|
||||
}
|
||||
|
||||
return new BezierApproximator(subControlPoints).CreateBezier();
|
||||
return PathApproximator.ApproximateBezier(subControlPoints);
|
||||
}
|
||||
|
||||
private void calculatePath()
|
||||
|
@ -18,7 +18,7 @@
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.1.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="2.1.4" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
|
||||
<PackageReference Include="ppy.osu.Framework" Version="2018.1030.0" />
|
||||
<PackageReference Include="ppy.osu.Framework" Version="2018.1102.0" />
|
||||
<PackageReference Include="SharpCompress" Version="0.22.0" />
|
||||
<PackageReference Include="NUnit" Version="3.11.0" />
|
||||
<PackageReference Include="SharpRaven" Version="2.4.0" />
|
||||
|
Loading…
Reference in New Issue
Block a user