-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
JuiceStreamPath.cs
321 lines (267 loc) · 13.2 KB
/
JuiceStreamPath.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Utils;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
using osuTK;
namespace osu.Game.Rulesets.Catch.Objects
{
/// <summary>
/// Represents the path of a juice stream.
/// <para>
/// A <see cref="JuiceStream"/> holds a legacy <see cref="SliderPath"/> as the representation of the path.
/// However, the <see cref="SliderPath"/> representation is difficult to work with.
/// This <see cref="JuiceStreamPath"/> represents the path in a more convenient way, a polyline connecting list of <see cref="JuiceStreamPathVertex"/>s.
/// </para>
/// </summary>
public class JuiceStreamPath
{
/// <summary>
/// The height of legacy osu!standard playfield.
/// The sliders converted by <see cref="ConvertToSliderPath"/> are vertically contained in this height.
/// </summary>
internal const float OSU_PLAYFIELD_HEIGHT = 384;
/// <summary>
/// The list of vertices of the path, which is represented as a polyline connecting the vertices.
/// </summary>
public IReadOnlyList<JuiceStreamPathVertex> Vertices => vertices;
/// <summary>
/// The current version number.
/// This starts from <c>1</c> and incremented whenever this <see cref="JuiceStreamPath"/> is modified.
/// </summary>
public int InvalidationID { get; private set; } = 1;
/// <summary>
/// The difference between first vertex's <see cref="JuiceStreamPathVertex.Time"/> and last vertex's <see cref="JuiceStreamPathVertex.Time"/>.
/// </summary>
public double Duration => vertices[^1].Time - vertices[0].Time;
/// <remarks>
/// This list should always be non-empty.
/// </remarks>
private readonly List<JuiceStreamPathVertex> vertices = new List<JuiceStreamPathVertex>
{
new JuiceStreamPathVertex()
};
/// <summary>
/// Compute the x-position of the path at the given <paramref name="time"/>.
/// </summary>
/// <remarks>
/// When the given time is outside of the path, the x position at the corresponding endpoint is returned,
/// </remarks>
public float PositionAtTime(double time)
{
int index = vertexIndexAtTime(time);
return positionAtTime(time, index);
}
/// <summary>
/// Remove all vertices of this path, then add a new vertex <c>(0, 0)</c>.
/// </summary>
public void Clear()
{
vertices.Clear();
vertices.Add(new JuiceStreamPathVertex());
invalidate();
}
/// <summary>
/// Insert a vertex at given <paramref name="time"/>.
/// The <see cref="PositionAtTime"/> is used as the position of the new vertex.
/// Thus, the set of points of the path is not changed (up to floating-point precision).
/// </summary>
/// <returns>The index of the new vertex.</returns>
public int InsertVertex(double time)
{
if (!double.IsFinite(time))
throw new ArgumentOutOfRangeException(nameof(time));
int index = vertexIndexAtTime(time);
float x = positionAtTime(time, index);
vertices.Insert(index, new JuiceStreamPathVertex(time, x));
invalidate();
return index;
}
/// <summary>
/// Move the vertex of given <paramref name="index"/> to the given position <paramref name="newX"/>.
/// </summary>
public void SetVertexPosition(int index, float newX)
{
if (index < 0 || index >= vertices.Count)
throw new ArgumentOutOfRangeException(nameof(index));
if (!float.IsFinite(newX))
throw new ArgumentOutOfRangeException(nameof(newX));
vertices[index] = new JuiceStreamPathVertex(vertices[index].Time, newX);
invalidate();
}
/// <summary>
/// Add a new vertex at given <paramref name="time"/> and position.
/// </summary>
public void Add(double time, float x)
{
int index = InsertVertex(time);
SetVertexPosition(index, x);
}
/// <summary>
/// Remove all vertices that satisfy the given <paramref name="predicate"/>.
/// </summary>
/// <remarks>
/// If all vertices are removed, a new vertex <c>(0, 0)</c> is added.
/// </remarks>
/// <param name="predicate">The predicate to determine whether a vertex should be removed given the vertex and its index in the path.</param>
/// <returns>The number of removed vertices.</returns>
public int RemoveVertices(Func<JuiceStreamPathVertex, int, bool> predicate)
{
int index = 0;
int removeCount = vertices.RemoveAll(vertex => predicate(vertex, index++));
if (vertices.Count == 0)
vertices.Add(new JuiceStreamPathVertex());
if (removeCount != 0)
invalidate();
return removeCount;
}
/// <summary>
/// Recreate this path by using difference set of vertices at given time points.
/// In addition to the given <paramref name="sampleTimes"/>, the first vertex and the last vertex are always added to the new path.
/// New vertices use the positions on the original path. Thus, <see cref="PositionAtTime"/>s at <paramref name="sampleTimes"/> are preserved.
/// </summary>
public void ResampleVertices(IEnumerable<double> sampleTimes)
{
var sampledVertices = new List<JuiceStreamPathVertex>();
foreach (double time in sampleTimes)
{
if (!double.IsFinite(time))
throw new ArgumentOutOfRangeException(nameof(sampleTimes));
double clampedTime = Math.Clamp(time, vertices[0].Time, vertices[^1].Time);
float x = PositionAtTime(clampedTime);
sampledVertices.Add(new JuiceStreamPathVertex(clampedTime, x));
}
sampledVertices.Sort();
// The first vertex and the last vertex are always used in the result.
vertices.RemoveRange(1, vertices.Count - (vertices.Count == 1 ? 1 : 2));
vertices.InsertRange(1, sampledVertices);
invalidate();
}
/// <summary>
/// Convert a <see cref="SliderPath"/> to list of vertices and write the result to this <see cref="JuiceStreamPath"/>.
/// </summary>
/// <remarks>
/// Duplicated vertices are automatically removed.
/// </remarks>
public void ConvertFromSliderPath(SliderPath sliderPath, double velocity)
{
var sliderPathVertices = new List<Vector2>();
sliderPath.GetPathToProgress(sliderPathVertices, 0, 1);
double time = 0;
vertices.Clear();
vertices.Add(new JuiceStreamPathVertex(0, sliderPathVertices.FirstOrDefault().X));
for (int i = 1; i < sliderPathVertices.Count; i++)
{
time += Vector2.Distance(sliderPathVertices[i - 1], sliderPathVertices[i]) / velocity;
if (!Precision.AlmostEquals(vertices[^1].Time, time))
Add(time, sliderPathVertices[i].X);
}
invalidate();
}
/// <summary>
/// Computes the minimum slider velocity required to convert this path to a <see cref="SliderPath"/>.
/// </summary>
public double ComputeRequiredVelocity()
{
double maximumSlope = 0;
for (int i = 1; i < vertices.Count; i++)
{
double xDifference = Math.Abs((double)vertices[i].X - vertices[i - 1].X);
double timeDifference = vertices[i].Time - vertices[i - 1].Time;
// A short segment won't affect the resulting path much anyways so ignore it to avoid divide-by-zero.
if (Precision.AlmostEquals(timeDifference, 0))
continue;
maximumSlope = Math.Max(maximumSlope, xDifference / timeDifference);
}
return maximumSlope;
}
/// <summary>
/// Convert the path of this <see cref="JuiceStreamPath"/> to a <see cref="SliderPath"/> and write the result to <paramref name="sliderPath"/>.
/// The resulting slider is "folded" to make it vertically contained in the playfield `(0..<see cref="OSU_PLAYFIELD_HEIGHT"/>)` assuming the slider start position is <paramref name="sliderStartY"/>.
///
/// The velocity of the converted slider is assumed to be <paramref name="velocity"/>.
/// To preserve the path, <paramref name="velocity"/> should be at least the value returned by <see cref="ComputeRequiredVelocity"/>.
/// </summary>
public void ConvertToSliderPath(SliderPath sliderPath, float sliderStartY, double velocity)
{
const float margin = 1;
// Note: these two variables and `sliderPath` are modified by the local functions.
double currentTime = 0;
Vector2 lastPosition = new Vector2(vertices[0].X, 0);
sliderPath.ControlPoints.Clear();
sliderPath.ControlPoints.Add(new PathControlPoint(lastPosition));
for (int i = 1; i < vertices.Count; i++)
{
sliderPath.ControlPoints[^1].Type = PathType.LINEAR;
float deltaX = vertices[i].X - lastPosition.X;
double length = (vertices[i].Time - currentTime) * velocity;
// Should satisfy `deltaX^2 + deltaY^2 = length^2`.
// The expression inside the `sqrt` is (almost) non-negative if the slider velocity is large enough.
double deltaY = Math.Sqrt(Math.Max(0, length * length - (double)deltaX * deltaX));
// When `deltaY` is small, one segment is always enough.
// This case is handled separately to prevent divide-by-zero.
if (deltaY <= OSU_PLAYFIELD_HEIGHT / 2 - margin)
{
float nextX = vertices[i].X;
float nextY = (float)(lastPosition.Y + getYDirection() * deltaY);
addControlPoint(nextX, nextY);
continue;
}
// When `deltaY` is large or when the slider velocity is fast, the segment must be partitioned to subsegments to stay in bounds.
for (double currentProgress = 0; currentProgress < deltaY;)
{
double nextProgress = Math.Min(currentProgress + getMaxDeltaY(), deltaY);
float nextX = (float)(vertices[i - 1].X + nextProgress / deltaY * deltaX);
float nextY = (float)(lastPosition.Y + getYDirection() * (nextProgress - currentProgress));
addControlPoint(nextX, nextY);
currentProgress = nextProgress;
}
}
int getYDirection()
{
float lastSliderY = sliderStartY + lastPosition.Y;
return lastSliderY < OSU_PLAYFIELD_HEIGHT / 2 ? 1 : -1;
}
float getMaxDeltaY()
{
float lastSliderY = sliderStartY + lastPosition.Y;
return Math.Max(lastSliderY, OSU_PLAYFIELD_HEIGHT - lastSliderY) - margin;
}
void addControlPoint(float nextX, float nextY)
{
Vector2 nextPosition = new Vector2(nextX, nextY);
sliderPath.ControlPoints.Add(new PathControlPoint(nextPosition));
currentTime += Vector2.Distance(lastPosition, nextPosition) / velocity;
lastPosition = nextPosition;
}
}
/// <summary>
/// Find the index at which a new vertex with <paramref name="time"/> can be inserted.
/// </summary>
private int vertexIndexAtTime(double time)
{
// The position of `(time, Infinity)` is uniquely determined because infinite positions are not allowed.
int i = vertices.BinarySearch(new JuiceStreamPathVertex(time, float.PositiveInfinity));
return i < 0 ? ~i : i;
}
/// <summary>
/// Compute the position at the given <paramref name="time"/>, assuming <paramref name="index"/> is the vertex index returned by <see cref="vertexIndexAtTime"/>.
/// </summary>
private float positionAtTime(double time, int index)
{
if (index <= 0)
return vertices[0].X;
if (index >= vertices.Count)
return vertices[^1].X;
double duration = vertices[index].Time - vertices[index - 1].Time;
if (Precision.AlmostEquals(duration, 0))
return vertices[index].X;
float deltaX = vertices[index].X - vertices[index - 1].X;
return (float)(vertices[index - 1].X + deltaX * ((time - vertices[index - 1].Time) / duration));
}
private void invalidate() => InvalidationID++;
}
}