2018-01-05 19:21:19 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-02-07 12:59:30 +08:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
2016-12-03 20:56:35 +08:00
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
2016-12-03 17:41:03 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using osu.Framework.MathUtils;
|
2017-04-18 15:05:58 +08:00
|
|
|
|
using osu.Game.Rulesets.Objects.Types;
|
2017-03-15 11:52:25 +08:00
|
|
|
|
using OpenTK;
|
2016-11-29 20:29:53 +08:00
|
|
|
|
|
2017-04-18 15:05:58 +08:00
|
|
|
|
namespace osu.Game.Rulesets.Objects
|
2016-11-29 20:29:53 +08:00
|
|
|
|
{
|
|
|
|
|
public class SliderCurve
|
|
|
|
|
{
|
2017-03-15 11:52:25 +08:00
|
|
|
|
public double Distance;
|
2016-11-29 20:29:53 +08:00
|
|
|
|
|
2016-12-10 01:04:02 +08:00
|
|
|
|
public List<Vector2> ControlPoints;
|
2016-11-29 20:29:53 +08:00
|
|
|
|
|
2017-03-13 18:15:25 +08:00
|
|
|
|
public CurveType CurveType = CurveType.PerfectCurve;
|
2016-11-29 20:29:53 +08:00
|
|
|
|
|
2017-02-09 15:29:21 +08:00
|
|
|
|
public Vector2 Offset;
|
|
|
|
|
|
2017-03-23 12:41:50 +08:00
|
|
|
|
private readonly List<Vector2> calculatedPath = new List<Vector2>();
|
|
|
|
|
private readonly List<double> cumulativeLength = new List<double>();
|
2016-11-29 20:29:53 +08:00
|
|
|
|
|
2016-12-10 01:04:02 +08:00
|
|
|
|
private List<Vector2> calculateSubpath(List<Vector2> subControlPoints)
|
2016-11-29 20:29:53 +08:00
|
|
|
|
{
|
|
|
|
|
switch (CurveType)
|
|
|
|
|
{
|
2017-03-13 18:15:25 +08:00
|
|
|
|
case CurveType.Linear:
|
2016-12-10 01:04:02 +08:00
|
|
|
|
return subControlPoints;
|
2017-03-13 18:15:25 +08:00
|
|
|
|
case CurveType.PerfectCurve:
|
2017-02-24 16:34:50 +08:00
|
|
|
|
//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;
|
2016-11-30 05:27:27 +08:00
|
|
|
|
}
|
2017-02-24 16:34:50 +08:00
|
|
|
|
|
|
|
|
|
return new BezierApproximator(subControlPoints).CreateBezier();
|
2016-11-30 05:27:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-03 22:19:23 +08:00
|
|
|
|
private void calculatePath()
|
2016-11-30 05:27:27 +08:00
|
|
|
|
{
|
2016-12-03 17:41:03 +08:00
|
|
|
|
calculatedPath.Clear();
|
2016-11-30 05:27:27 +08:00
|
|
|
|
|
|
|
|
|
// 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.
|
2016-12-10 01:04:02 +08:00
|
|
|
|
List<Vector2> subControlPoints = new List<Vector2>();
|
|
|
|
|
for (int i = 0; i < ControlPoints.Count; ++i)
|
2016-11-30 05:27:27 +08:00
|
|
|
|
{
|
2016-12-10 01:04:02 +08:00
|
|
|
|
subControlPoints.Add(ControlPoints[i]);
|
|
|
|
|
if (i == ControlPoints.Count - 1 || ControlPoints[i] == ControlPoints[i + 1])
|
2016-11-30 05:27:27 +08:00
|
|
|
|
{
|
2016-12-10 01:04:02 +08:00
|
|
|
|
List<Vector2> subpath = calculateSubpath(subControlPoints);
|
2017-03-07 09:59:19 +08:00
|
|
|
|
foreach (Vector2 t in subpath)
|
|
|
|
|
if (calculatedPath.Count == 0 || calculatedPath.Last() != t)
|
|
|
|
|
calculatedPath.Add(t);
|
2016-11-30 05:27:27 +08:00
|
|
|
|
|
2016-12-10 01:04:02 +08:00
|
|
|
|
subControlPoints.Clear();
|
2016-11-30 05:27:27 +08:00
|
|
|
|
}
|
2016-11-29 20:29:53 +08:00
|
|
|
|
}
|
2016-12-03 22:19:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void calculateCumulativeLengthAndTrimPath()
|
|
|
|
|
{
|
|
|
|
|
double l = 0;
|
|
|
|
|
|
2016-12-03 17:41:03 +08:00
|
|
|
|
cumulativeLength.Clear();
|
2016-12-03 22:19:23 +08:00
|
|
|
|
cumulativeLength.Add(l);
|
|
|
|
|
|
2016-12-03 17:41:03 +08:00
|
|
|
|
for (int i = 0; i < calculatedPath.Count - 1; ++i)
|
|
|
|
|
{
|
2016-12-03 22:19:23 +08:00
|
|
|
|
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.
|
2017-03-15 11:52:25 +08:00
|
|
|
|
if (Distance - l < d)
|
2016-12-03 22:19:23 +08:00
|
|
|
|
{
|
2017-03-15 11:52:25 +08:00
|
|
|
|
calculatedPath[i + 1] = calculatedPath[i] + diff * (float)((Distance - l) / d);
|
2016-12-03 22:19:23 +08:00
|
|
|
|
calculatedPath.RemoveRange(i + 2, calculatedPath.Count - 2 - i);
|
|
|
|
|
|
2017-03-15 11:52:25 +08:00
|
|
|
|
l = Distance;
|
2016-12-03 22:19:23 +08:00
|
|
|
|
cumulativeLength.Add(l);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-12-03 17:41:03 +08:00
|
|
|
|
|
2016-12-03 22:19:23 +08:00
|
|
|
|
l += d;
|
|
|
|
|
cumulativeLength.Add(l);
|
2016-12-03 17:41:03 +08:00
|
|
|
|
}
|
2016-12-03 22:19:23 +08:00
|
|
|
|
|
2016-12-17 15:44:58 +08:00
|
|
|
|
//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.
|
2016-12-03 22:19:23 +08:00
|
|
|
|
// Lengthen slider curves that are too short compared to what's
|
|
|
|
|
// in the .osu file.
|
2016-12-17 15:44:58 +08:00
|
|
|
|
/*if (l < Length && calculatedPath.Count > 1)
|
2016-12-03 22:19:23 +08:00
|
|
|
|
{
|
|
|
|
|
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;
|
2016-12-17 15:44:58 +08:00
|
|
|
|
}*/
|
2016-12-03 22:19:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Calculate()
|
|
|
|
|
{
|
|
|
|
|
calculatePath();
|
|
|
|
|
calculateCumulativeLengthAndTrimPath();
|
2016-11-29 20:29:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-03 18:43:56 +08:00
|
|
|
|
private int indexOfDistance(double d)
|
2016-11-29 20:29:53 +08:00
|
|
|
|
{
|
2016-12-03 17:41:03 +08:00
|
|
|
|
int i = cumulativeLength.BinarySearch(d);
|
|
|
|
|
if (i < 0) i = ~i;
|
|
|
|
|
|
2016-12-03 18:43:56 +08:00
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private double progressToDistance(double progress)
|
|
|
|
|
{
|
2017-03-15 11:52:25 +08:00
|
|
|
|
return MathHelper.Clamp(progress, 0, 1) * Distance;
|
2016-12-03 18:43:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Vector2 interpolateVertices(int i, double d)
|
|
|
|
|
{
|
|
|
|
|
if (calculatedPath.Count == 0)
|
|
|
|
|
return Vector2.Zero;
|
2016-12-03 17:41:03 +08:00
|
|
|
|
|
|
|
|
|
if (i <= 0)
|
|
|
|
|
return calculatedPath.First();
|
2016-12-03 18:43:56 +08:00
|
|
|
|
else if (i >= calculatedPath.Count)
|
|
|
|
|
return calculatedPath.Last();
|
2016-12-03 17:41:03 +08:00
|
|
|
|
|
|
|
|
|
Vector2 p0 = calculatedPath[i - 1];
|
|
|
|
|
Vector2 p1 = calculatedPath[i];
|
|
|
|
|
|
|
|
|
|
double d0 = cumulativeLength[i - 1];
|
|
|
|
|
double d1 = cumulativeLength[i];
|
2016-11-29 20:29:53 +08:00
|
|
|
|
|
2016-12-03 17:41:03 +08:00
|
|
|
|
// Avoid division by and almost-zero number in case two points are extremely close to each other.
|
|
|
|
|
if (Precision.AlmostEquals(d0, d1))
|
|
|
|
|
return p0;
|
2016-11-29 20:29:53 +08:00
|
|
|
|
|
2016-12-03 17:41:03 +08:00
|
|
|
|
double w = (d - d0) / (d1 - d0);
|
|
|
|
|
return p0 + (p1 - p0) * (float)w;
|
2016-11-29 20:29:53 +08:00
|
|
|
|
}
|
2016-12-03 18:43:56 +08:00
|
|
|
|
|
2016-12-03 20:25:31 +08:00
|
|
|
|
/// <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>
|
2017-02-09 15:57:10 +08:00
|
|
|
|
/// <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>
|
2016-12-03 20:25:31 +08:00
|
|
|
|
public void GetPathToProgress(List<Vector2> path, double p0, double p1)
|
2016-12-03 18:43:56 +08:00
|
|
|
|
{
|
2017-02-14 17:40:37 +08:00
|
|
|
|
if (calculatedPath.Count == 0 && ControlPoints.Count > 0)
|
|
|
|
|
Calculate();
|
|
|
|
|
|
2016-12-03 20:25:31 +08:00
|
|
|
|
double d0 = progressToDistance(p0);
|
|
|
|
|
double d1 = progressToDistance(p1);
|
|
|
|
|
|
|
|
|
|
path.Clear();
|
2016-12-03 18:43:56 +08:00
|
|
|
|
|
|
|
|
|
int i = 0;
|
2017-03-07 09:59:19 +08:00
|
|
|
|
for (; i < calculatedPath.Count && cumulativeLength[i] < d0; ++i) { }
|
2016-12-03 20:25:31 +08:00
|
|
|
|
|
2017-02-09 15:29:21 +08:00
|
|
|
|
path.Add(interpolateVertices(i, d0) + Offset);
|
2016-12-03 20:25:31 +08:00
|
|
|
|
|
|
|
|
|
for (; i < calculatedPath.Count && cumulativeLength[i] <= d1; ++i)
|
2017-02-09 15:29:21 +08:00
|
|
|
|
path.Add(calculatedPath[i] + Offset);
|
2016-12-03 18:43:56 +08:00
|
|
|
|
|
2017-02-09 15:29:21 +08:00
|
|
|
|
path.Add(interpolateVertices(i, d1) + Offset);
|
2016-12-03 18:43:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-03 20:25:31 +08:00
|
|
|
|
/// <summary>
|
2017-03-06 10:11:29 +08:00
|
|
|
|
/// Computes the position on the slider at a given progress that ranges from 0 (beginning of the curve)
|
|
|
|
|
/// to 1 (end of the curve).
|
2016-12-03 20:25:31 +08:00
|
|
|
|
/// </summary>
|
2017-03-06 10:11:29 +08:00
|
|
|
|
/// <param name="progress">Ranges from 0 (beginning of the curve) to 1 (end of the curve).</param>
|
2016-12-03 20:25:31 +08:00
|
|
|
|
/// <returns></returns>
|
2016-12-03 18:43:56 +08:00
|
|
|
|
public Vector2 PositionAt(double progress)
|
|
|
|
|
{
|
2017-02-14 17:40:37 +08:00
|
|
|
|
if (calculatedPath.Count == 0 && ControlPoints.Count > 0)
|
|
|
|
|
Calculate();
|
|
|
|
|
|
2016-12-03 18:43:56 +08:00
|
|
|
|
double d = progressToDistance(progress);
|
2017-02-09 15:29:21 +08:00
|
|
|
|
return interpolateVertices(indexOfDistance(d), d) + Offset;
|
2016-12-03 18:43:56 +08:00
|
|
|
|
}
|
2016-11-29 20:29:53 +08:00
|
|
|
|
}
|
2017-10-12 21:27:22 +08:00
|
|
|
|
}
|