1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 18:20:34 +08:00
osu-lazer/osu.Game/Rulesets/Objects/SliderPath.cs

287 lines
10 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2018-10-11 16:44:25 +08:00
using System;
2018-04-13 17:19:50 +08:00
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
2018-04-13 17:19:50 +08:00
using osu.Framework.MathUtils;
using osu.Game.Rulesets.Objects.Types;
using OpenTK;
namespace osu.Game.Rulesets.Objects
{
2018-11-12 15:53:30 +08:00
public struct SliderPath : IEquatable<SliderPath>
2018-04-13 17:19:50 +08:00
{
2018-11-12 13:16:21 +08:00
/// <summary>
/// The user-set distance of the path. If non-null, <see cref="Distance"/> will match this value,
/// and the path will be shortened/lengthened to match this length.
/// </summary>
public readonly double? ExpectedDistance;
2018-04-13 17:19:50 +08:00
2018-11-12 15:38:14 +08:00
/// <summary>
/// The type of path.
/// </summary>
public readonly PathType Type;
[JsonProperty]
private Vector2[] controlPoints;
2018-11-12 15:38:14 +08:00
private List<Vector2> calculatedPath;
private List<double> cumulativeLength;
private bool isInitialised;
2018-11-12 13:08:36 +08:00
2018-11-12 13:16:21 +08:00
/// <summary>
/// Creates a new <see cref="SliderPath"/>.
/// </summary>
/// <param name="type">The type of path.</param>
/// <param name="controlPoints">The control points of the path.</param>
/// <param name="expectedDistance">A user-set distance of the path that may be shorter or longer than the true distance between all
/// <paramref name="controlPoints"/>. The path will be shortened/lengthened to match this length.
/// If null, the path will use the true distance between all <paramref name="controlPoints"/>.</param>
[JsonConstructor]
public SliderPath(PathType type, Vector2[] controlPoints, double? expectedDistance = null)
{
2018-11-12 15:38:14 +08:00
this = default;
this.controlPoints = controlPoints;
2018-11-12 15:38:14 +08:00
Type = type;
ExpectedDistance = expectedDistance;
2018-04-13 17:19:50 +08:00
2018-11-12 15:38:14 +08:00
ensureInitialised();
}
2018-10-26 13:18:48 +08:00
/// <summary>
/// The control points of the path.
/// </summary>
[JsonIgnore]
public ReadOnlySpan<Vector2> ControlPoints
{
get
{
ensureInitialised();
return controlPoints.AsSpan();
}
}
2018-11-12 13:16:21 +08:00
/// <summary>
/// The distance of the path after lengthening/shortening to account for <see cref="ExpectedDistance"/>.
/// </summary>
2018-11-12 15:38:39 +08:00
[JsonIgnore]
2018-11-12 15:38:14 +08:00
public double Distance
{
get
{
ensureInitialised();
return cumulativeLength.Count == 0 ? 0 : cumulativeLength[cumulativeLength.Count - 1];
}
}
2018-11-12 13:08:36 +08:00
/// <summary>
/// Computes the slider path 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 path.</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)
{
2018-11-12 15:38:14 +08:00
ensureInitialised();
2018-11-12 13:08:36 +08:00
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));
for (; i < calculatedPath.Count && cumulativeLength[i] <= d1; ++i)
path.Add(calculatedPath[i]);
path.Add(interpolateVertices(i, d1));
}
/// <summary>
/// Computes the position on the slider at a given progress that ranges from 0 (beginning of the path)
/// to 1 (end of the path).
/// </summary>
/// <param name="progress">Ranges from 0 (beginning of the path) to 1 (end of the path).</param>
/// <returns></returns>
public Vector2 PositionAt(double progress)
{
2018-11-12 15:38:14 +08:00
ensureInitialised();
2018-11-12 13:08:36 +08:00
double d = progressToDistance(progress);
return interpolateVertices(indexOfDistance(d), d);
}
2018-04-13 17:19:50 +08:00
2018-11-12 15:38:14 +08:00
private void ensureInitialised()
{
if (isInitialised)
return;
isInitialised = true;
controlPoints = controlPoints ?? Array.Empty<Vector2>();
2018-11-12 15:38:14 +08:00
calculatedPath = new List<Vector2>();
cumulativeLength = new List<double>();
calculatePath();
calculateCumulativeLength();
}
2018-10-11 16:44:25 +08:00
private List<Vector2> calculateSubpath(ReadOnlySpan<Vector2> subControlPoints)
2018-04-13 17:19:50 +08:00
{
switch (Type)
2018-04-13 17:19:50 +08:00
{
case PathType.Linear:
return PathApproximator.ApproximateLinear(subControlPoints);
case PathType.PerfectCurve:
2018-04-13 17:19:50 +08:00
//we can only use CircularArc iff we have exactly three control points and no dissection.
2018-10-11 16:44:25 +08:00
if (ControlPoints.Length != 3 || subControlPoints.Length != 3)
2018-04-13 17:19:50 +08:00
break;
// Here we have exactly 3 control points. Attempt to fit a circular arc.
List<Vector2> subpath = PathApproximator.ApproximateCircularArc(subControlPoints);
2018-04-13 17:19:50 +08:00
// 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;
case PathType.Catmull:
return PathApproximator.ApproximateCatmull(subControlPoints);
2018-04-13 17:19:50 +08:00
}
return PathApproximator.ApproximateBezier(subControlPoints);
2018-04-13 17:19:50 +08:00
}
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.
2018-10-11 16:44:25 +08:00
int start = 0;
int end = 0;
for (int i = 0; i < ControlPoints.Length; ++i)
2018-04-13 17:19:50 +08:00
{
2018-10-11 16:44:25 +08:00
end++;
2018-04-13 17:19:50 +08:00
if (i == ControlPoints.Length - 1 || ControlPoints[i] == ControlPoints[i + 1])
2018-10-11 16:44:25 +08:00
{
ReadOnlySpan<Vector2> cpSpan = ControlPoints.Slice(start, end - start);
2018-10-11 16:44:25 +08:00
2018-10-16 10:11:04 +08:00
foreach (Vector2 t in calculateSubpath(cpSpan))
if (calculatedPath.Count == 0 || calculatedPath.Last() != t)
calculatedPath.Add(t);
2018-10-11 16:44:25 +08:00
start = end;
2018-04-13 17:19:50 +08:00
}
}
}
private void calculateCumulativeLength()
2018-04-13 17:19:50 +08:00
{
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;
// Shorted slider paths that are too long compared to the expected distance
if (ExpectedDistance.HasValue && ExpectedDistance - l < d)
2018-04-13 17:19:50 +08:00
{
calculatedPath[i + 1] = calculatedPath[i] + diff * (float)((ExpectedDistance - l) / d);
2018-04-13 17:19:50 +08:00
calculatedPath.RemoveRange(i + 2, calculatedPath.Count - 2 - i);
l = ExpectedDistance.Value;
2018-04-13 17:19:50 +08:00
cumulativeLength.Add(l);
break;
}
l += d;
cumulativeLength.Add(l);
}
// Lengthen slider paths that are too short compared to the expected distance
if (ExpectedDistance.HasValue && l < ExpectedDistance && calculatedPath.Count > 1)
2018-04-13 17:19:50 +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)((ExpectedDistance - l) / d);
cumulativeLength[calculatedPath.Count - 1] = ExpectedDistance.Value;
}
2018-04-13 17:19:50 +08:00
}
private int indexOfDistance(double d)
{
int i = cumulativeLength.BinarySearch(d);
if (i < 0) i = ~i;
return i;
}
private double progressToDistance(double progress)
{
2018-11-12 13:07:48 +08:00
return MathHelper.Clamp(progress, 0, 1) * Distance;
2018-04-13 17:19:50 +08:00
}
private Vector2 interpolateVertices(int i, double d)
{
if (calculatedPath.Count == 0)
return Vector2.Zero;
if (i <= 0)
return calculatedPath.First();
if (i >= calculatedPath.Count)
2018-04-13 17:19:50 +08:00
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;
}
2018-11-12 15:53:30 +08:00
public bool Equals(SliderPath other)
{
if (ControlPoints == null && other.ControlPoints != null)
return false;
if (other.ControlPoints == null && ControlPoints != null)
return false;
return ControlPoints.SequenceEqual(other.ControlPoints) && ExpectedDistance.Equals(other.ExpectedDistance) && Type == other.Type;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is SliderPath other && Equals(other);
}
2018-04-13 17:19:50 +08:00
}
}