Add further abstraction to Sliders/IHasCurve.

This commit is contained in:
smoogipooo
2017-03-15 12:52:25 +09:00
parent 9fdeff3f9e
commit 6a31219781
12 changed files with 364 additions and 81 deletions

View File

@ -72,7 +72,7 @@ namespace osu.Game.Modes.Osu.Objects.Drawables
AddNested(initialCircle);
var repeatDuration = s.Curve.Length / s.Velocity;
var repeatDuration = s.Curve.Distance / s.Velocity;
foreach (var tick in s.Ticks)
{
var repeatStartTime = s.StartTime + tick.RepeatIndex * repeatDuration;
@ -104,7 +104,7 @@ namespace osu.Game.Modes.Osu.Objects.Drawables
double progress = MathHelper.Clamp((Time.Current - slider.StartTime) / slider.Duration, 0, 1);
int repeat = slider.RepeatAt(progress);
progress = slider.CurveProgressAt(progress);
progress = slider.ProgressAt(progress);
if (repeat > currentRepeat)
{

View File

@ -8,40 +8,30 @@ using osu.Game.Beatmaps.Timing;
using osu.Game.Modes.Objects.Types;
using System;
using System.Collections.Generic;
using osu.Game.Modes.Objects;
namespace osu.Game.Modes.Osu.Objects
{
public class Slider : OsuHitObject, IHasEndTime, IHasCurve, IHasDistance, IHasRepeats
public class Slider : OsuHitObject, IHasCurve
{
public double EndTime => StartTime + RepeatCount * Curve.Length / Velocity;
public IHasCurve CurveObject { get; set; }
public SliderCurve Curve => CurveObject.Curve;
public double EndTime => StartTime + RepeatCount * Curve.Distance / Velocity;
public double Duration => EndTime - StartTime;
public override Vector2 EndPosition => PositionAt(1);
/// <summary>
/// Computes the position on the slider at a given progress that ranges from 0 (beginning of the slider)
/// to 1 (end of the slider). This includes repeat logic.
/// </summary>
/// <param name="progress">Ranges from 0 (beginning of the slider) to 1 (end of the slider).</param>
/// <returns></returns>
public Vector2 PositionAt(double progress) => Curve.PositionAt(CurveProgressAt(progress));
public Vector2 PositionAt(double progress) => CurveObject.PositionAt(progress);
public double ProgressAt(double progress) => CurveObject.ProgressAt(progress);
public int RepeatAt(double progress) => CurveObject.RepeatAt(progress);
/// <summary>
/// Find the current progress along the curve, accounting for repeat logic.
/// </summary>
public double CurveProgressAt(double progress)
{
var p = progress * RepeatCount % 1;
if (RepeatAt(progress) % 2 == 1)
p = 1 - p;
return p;
}
public List<Vector2> ControlPoints => CurveObject.ControlPoints;
public CurveType CurveType => CurveObject.CurveType;
public double Distance => CurveObject.Distance;
/// <summary>
/// Determine which repeat of the slider we are on at a given progress.
/// Range is 0..RepeatCount where 0 is the first run.
/// </summary>
public int RepeatAt(double progress) => (int)(progress * RepeatCount);
public int RepeatCount => CurveObject.RepeatCount;
private int stackHeight;
public override int StackHeight
@ -54,24 +44,6 @@ namespace osu.Game.Modes.Osu.Objects
}
}
public List<Vector2> ControlPoints
{
get { return Curve.ControlPoints; }
set { Curve.ControlPoints = value; }
}
public double Distance
{
get { return Curve.Length; }
set { Curve.Length = value; }
}
public CurveType CurveType
{
get { return Curve.CurveType; }
set { Curve.CurveType = value; }
}
public double Velocity;
public double TickDistance;
@ -90,17 +62,13 @@ namespace osu.Game.Modes.Osu.Objects
TickDistance = baseVelocity / baseDifficulty.SliderTickRate;
}
public int RepeatCount { get; set; } = 1;
internal readonly SliderCurve Curve = new SliderCurve();
public IEnumerable<SliderTick> Ticks
{
get
{
if (TickDistance == 0) yield break;
var length = Curve.Length;
var length = Curve.Distance;
var tickDistance = Math.Min(TickDistance, length);
var repeatDuration = length / Velocity;

View File

@ -1,203 +0,0 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Collections.Generic;
using OpenTK;
using System.Linq;
using osu.Framework.MathUtils;
using osu.Game.Modes.Objects.Types;
namespace osu.Game.Modes.Osu.Objects
{
public class SliderCurve
{
public double Length;
public List<Vector2> ControlPoints;
public CurveType CurveType = CurveType.PerfectCurve;
public Vector2 Offset;
private List<Vector2> calculatedPath = new List<Vector2>();
private List<double> cumulativeLength = new List<double>();
private List<Vector2> calculateSubpath(List<Vector2> subControlPoints)
{
switch (CurveType)
{
case CurveType.Linear:
return subControlPoints;
case CurveType.PerfectCurve:
//we can only use CircularArc iff we have exactly three control points and no dissection.
if (ControlPoints.Count != 3 || subControlPoints.Count != 3)
break;
// Here we have exactly 3 control points. Attempt to fit a circular arc.
List<Vector2> subpath = new CircularArcApproximator(subControlPoints[0], subControlPoints[1], subControlPoints[2]).CreateArc();
// 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)
break;
return subpath;
}
return new BezierApproximator(subControlPoints).CreateBezier();
}
private void calculatePath()
{
calculatedPath.Clear();
// Sliders may consist of various subpaths separated by two consecutive vertices
// with the same position. The following loop parses these subpaths and computes
// their shape independently, consecutively appending them to calculatedPath.
List<Vector2> subControlPoints = new List<Vector2>();
for (int i = 0; i < ControlPoints.Count; ++i)
{
subControlPoints.Add(ControlPoints[i]);
if (i == ControlPoints.Count - 1 || ControlPoints[i] == ControlPoints[i + 1])
{
List<Vector2> subpath = calculateSubpath(subControlPoints);
foreach (Vector2 t in subpath)
if (calculatedPath.Count == 0 || calculatedPath.Last() != t)
calculatedPath.Add(t);
subControlPoints.Clear();
}
}
}
private void calculateCumulativeLengthAndTrimPath()
{
double l = 0;
cumulativeLength.Clear();
cumulativeLength.Add(l);
for (int i = 0; i < calculatedPath.Count - 1; ++i)
{
Vector2 diff = calculatedPath[i + 1] - calculatedPath[i];
double d = diff.Length;
// Shorten slider curves that are too long compared to what's
// in the .osu file.
if (Length - l < d)
{
calculatedPath[i + 1] = calculatedPath[i] + diff * (float)((Length - l) / d);
calculatedPath.RemoveRange(i + 2, calculatedPath.Count - 2 - i);
l = Length;
cumulativeLength.Add(l);
break;
}
l += d;
cumulativeLength.Add(l);
}
//TODO: Figure out if the following code is needed in some cases. Judging by the map
// "Transform" http://osu.ppy.sh/s/484689 it seems like we should _not_ be doing this.
// Lengthen slider curves that are too short compared to what's
// in the .osu file.
/*if (l < Length && calculatedPath.Count > 1)
{
Vector2 diff = calculatedPath[calculatedPath.Count - 1] - calculatedPath[calculatedPath.Count - 2];
double d = diff.Length;
if (d <= 0)
return;
calculatedPath[calculatedPath.Count - 1] += diff * (float)((Length - l) / d);
cumulativeLength[calculatedPath.Count - 1] = Length;
}*/
}
public void Calculate()
{
calculatePath();
calculateCumulativeLengthAndTrimPath();
}
private int indexOfDistance(double d)
{
int i = cumulativeLength.BinarySearch(d);
if (i < 0) i = ~i;
return i;
}
private double progressToDistance(double progress)
{
return MathHelper.Clamp(progress, 0, 1) * Length;
}
private Vector2 interpolateVertices(int i, double d)
{
if (calculatedPath.Count == 0)
return Vector2.Zero;
if (i <= 0)
return calculatedPath.First();
else if (i >= calculatedPath.Count)
return calculatedPath.Last();
Vector2 p0 = calculatedPath[i - 1];
Vector2 p1 = calculatedPath[i];
double d0 = cumulativeLength[i - 1];
double d1 = cumulativeLength[i];
// Avoid division by and almost-zero number in case two points are extremely close to each other.
if (Precision.AlmostEquals(d0, d1))
return p0;
double w = (d - d0) / (d1 - d0);
return p0 + (p1 - p0) * (float)w;
}
/// <summary>
/// Computes the slider curve until a given progress that ranges from 0 (beginning of the slider)
/// to 1 (end of the slider) and stores the generated path in the given list.
/// </summary>
/// <param name="path">The list to be filled with the computed curve.</param>
/// <param name="p0">Start progress. Ranges from 0 (beginning of the slider) to 1 (end of the slider).</param>
/// <param name="p1">End progress. Ranges from 0 (beginning of the slider) to 1 (end of the slider).</param>
public void GetPathToProgress(List<Vector2> path, double p0, double p1)
{
if (calculatedPath.Count == 0 && ControlPoints.Count > 0)
Calculate();
double d0 = progressToDistance(p0);
double d1 = progressToDistance(p1);
path.Clear();
int i = 0;
for (; i < calculatedPath.Count && cumulativeLength[i] < d0; ++i) { }
path.Add(interpolateVertices(i, d0) + Offset);
for (; i < calculatedPath.Count && cumulativeLength[i] <= d1; ++i)
path.Add(calculatedPath[i] + Offset);
path.Add(interpolateVertices(i, d1) + Offset);
}
/// <summary>
/// Computes the position on the slider at a given progress that ranges from 0 (beginning of the curve)
/// to 1 (end of the curve).
/// </summary>
/// <param name="progress">Ranges from 0 (beginning of the curve) to 1 (end of the curve).</param>
/// <returns></returns>
public Vector2 PositionAt(double progress)
{
if (calculatedPath.Count == 0 && ControlPoints.Count > 0)
Calculate();
double d = progressToDistance(progress);
return interpolateVertices(indexOfDistance(d), d) + Offset;
}
}
}