mirror of
https://github.com/osukey/osukey.git
synced 2025-08-05 15:44:04 +09:00
Rework heatmap for more consistent performance
This commit is contained in:
@ -26,10 +26,15 @@ namespace osu.Game.Rulesets.Osu.Statistics
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private const float inner_portion = 0.8f;
|
private const float inner_portion = 0.8f;
|
||||||
|
|
||||||
private const float rotation = 45;
|
/// <summary>
|
||||||
private const float point_size = 4;
|
/// Number of rows/columns of points.
|
||||||
|
/// 4px per point @ 128x128 size (the contents of the <see cref="Heatmap"/> are always square). 1024 total points.
|
||||||
|
/// </summary>
|
||||||
|
private const int points_per_dimension = 32;
|
||||||
|
|
||||||
private Container<HitPoint> allPoints;
|
private const float rotation = 45;
|
||||||
|
|
||||||
|
private GridContainer pointGrid;
|
||||||
|
|
||||||
private readonly BeatmapInfo beatmap;
|
private readonly BeatmapInfo beatmap;
|
||||||
private readonly IReadOnlyList<HitEvent> hitEvents;
|
private readonly IReadOnlyList<HitEvent> hitEvents;
|
||||||
@ -111,52 +116,39 @@ namespace osu.Game.Rulesets.Osu.Statistics
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
allPoints = new Container<HitPoint>
|
pointGrid = new GridContainer
|
||||||
{
|
{
|
||||||
RelativeSizeAxes = Axes.Both
|
RelativeSizeAxes = Axes.Both
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
protected override void Update()
|
Vector2 centre = new Vector2(points_per_dimension) / 2;
|
||||||
|
float innerRadius = centre.X * inner_portion;
|
||||||
|
|
||||||
|
Drawable[][] points = new Drawable[points_per_dimension][];
|
||||||
|
|
||||||
|
for (int r = 0; r < points_per_dimension; r++)
|
||||||
{
|
{
|
||||||
base.Update();
|
points[r] = new Drawable[points_per_dimension];
|
||||||
validateHitPoints();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void validateHitPoints()
|
for (int c = 0; c < points_per_dimension; c++)
|
||||||
{
|
{
|
||||||
if (sizeLayout.IsValid)
|
HitPointType pointType = Vector2.Distance(new Vector2(c, r), centre) <= innerRadius
|
||||||
return;
|
? HitPointType.Hit
|
||||||
|
: HitPointType.Miss;
|
||||||
|
|
||||||
allPoints.Clear();
|
var point = new HitPoint(pointType)
|
||||||
|
|
||||||
// Since the content is fit, both dimensions should have the same size.
|
|
||||||
float size = allPoints.DrawSize.X;
|
|
||||||
|
|
||||||
Vector2 centre = new Vector2(size / 2);
|
|
||||||
int rows = (int)Math.Ceiling(size / point_size);
|
|
||||||
int cols = (int)Math.Ceiling(size / point_size);
|
|
||||||
|
|
||||||
for (int r = 0; r < rows; r++)
|
|
||||||
{
|
{
|
||||||
for (int c = 0; c < cols; c++)
|
|
||||||
{
|
|
||||||
Vector2 pos = new Vector2(c * point_size, r * point_size);
|
|
||||||
HitPointType pointType = HitPointType.Hit;
|
|
||||||
|
|
||||||
if (Vector2.Distance(pos, centre) > size * inner_portion / 2)
|
|
||||||
pointType = HitPointType.Miss;
|
|
||||||
|
|
||||||
allPoints.Add(new HitPoint(pos, pointType)
|
|
||||||
{
|
|
||||||
Size = new Vector2(point_size),
|
|
||||||
Colour = pointType == HitPointType.Hit ? new Color4(102, 255, 204, 255) : new Color4(255, 102, 102, 255)
|
Colour = pointType == HitPointType.Hit ? new Color4(102, 255, 204, 255) : new Color4(255, 102, 102, 255)
|
||||||
});
|
};
|
||||||
|
|
||||||
|
points[r][c] = point;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pointGrid.Content = points;
|
||||||
|
|
||||||
if (hitEvents.Count > 0)
|
if (hitEvents.Count > 0)
|
||||||
{
|
{
|
||||||
// Todo: This should probably not be done like this.
|
// Todo: This should probably not be done like this.
|
||||||
@ -170,41 +162,39 @@ namespace osu.Game.Rulesets.Osu.Statistics
|
|||||||
AddPoint(((OsuHitObject)e.LastHitObject).StackedEndPosition, ((OsuHitObject)e.HitObject).StackedEndPosition, e.CursorPosition.Value, radius);
|
AddPoint(((OsuHitObject)e.LastHitObject).StackedEndPosition, ((OsuHitObject)e.HitObject).StackedEndPosition, e.CursorPosition.Value, radius);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sizeLayout.Validate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void AddPoint(Vector2 start, Vector2 end, Vector2 hitPoint, float radius)
|
protected void AddPoint(Vector2 start, Vector2 end, Vector2 hitPoint, float radius)
|
||||||
{
|
{
|
||||||
if (allPoints.Count == 0)
|
if (pointGrid.Content.Length == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
double angle1 = Math.Atan2(end.Y - hitPoint.Y, hitPoint.X - end.X); // Angle between the end point and the hit point.
|
double angle1 = Math.Atan2(end.Y - hitPoint.Y, hitPoint.X - end.X); // Angle between the end point and the hit point.
|
||||||
double angle2 = Math.Atan2(end.Y - start.Y, start.X - end.X); // Angle between the end point and the start point.
|
double angle2 = Math.Atan2(end.Y - start.Y, start.X - end.X); // Angle between the end point and the start point.
|
||||||
double finalAngle = angle2 - angle1; // Angle between start, end, and hit points.
|
double finalAngle = angle2 - angle1; // Angle between start, end, and hit points.
|
||||||
|
|
||||||
float normalisedDistance = Vector2.Distance(hitPoint, end) / radius;
|
float normalisedDistance = Vector2.Distance(hitPoint, end) / radius;
|
||||||
|
|
||||||
// Since the content is fit, both dimensions should have the same size.
|
// Convert the above into the local search space.
|
||||||
float size = allPoints.DrawSize.X;
|
Vector2 localCentre = new Vector2(points_per_dimension) / 2;
|
||||||
|
float localRadius = localCentre.X * inner_portion * normalisedDistance; // The radius inside the inner portion which of the heatmap which the closest point lies.
|
||||||
|
double localAngle = finalAngle + 3 * Math.PI / 4; // The angle inside the heatmap on which the closest point lies.
|
||||||
|
Vector2 localPoint = localCentre + localRadius * new Vector2((float)Math.Cos(localAngle), (float)Math.Sin(localAngle));
|
||||||
|
|
||||||
// Find the most relevant hit point.
|
// Find the most relevant hit point.
|
||||||
double minDist = double.PositiveInfinity;
|
double minDist = double.PositiveInfinity;
|
||||||
HitPoint point = null;
|
HitPoint point = null;
|
||||||
|
|
||||||
foreach (var p in allPoints)
|
for (int r = 0; r < points_per_dimension; r++)
|
||||||
{
|
{
|
||||||
Vector2 localCentre = new Vector2(size / 2);
|
for (int c = 0; c < points_per_dimension; c++)
|
||||||
float localRadius = localCentre.X * inner_portion * normalisedDistance;
|
{
|
||||||
double localAngle = finalAngle + 3 * Math.PI / 4;
|
float dist = Vector2.Distance(new Vector2(c, r), localPoint);
|
||||||
Vector2 localPoint = localCentre + localRadius * new Vector2((float)Math.Cos(localAngle), (float)Math.Sin(localAngle));
|
|
||||||
|
|
||||||
float dist = Vector2.Distance(p.DrawPosition + p.DrawSize / 2, localPoint);
|
|
||||||
|
|
||||||
if (dist < minDist)
|
if (dist < minDist)
|
||||||
{
|
{
|
||||||
minDist = dist;
|
minDist = dist;
|
||||||
point = p;
|
point = (HitPoint)pointGrid.Content[r][c];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -216,11 +206,11 @@ namespace osu.Game.Rulesets.Osu.Statistics
|
|||||||
{
|
{
|
||||||
private readonly HitPointType pointType;
|
private readonly HitPointType pointType;
|
||||||
|
|
||||||
public HitPoint(Vector2 position, HitPointType pointType)
|
public HitPoint(HitPointType pointType)
|
||||||
{
|
{
|
||||||
this.pointType = pointType;
|
this.pointType = pointType;
|
||||||
|
|
||||||
Position = position;
|
RelativeSizeAxes = Axes.Both;
|
||||||
Alpha = 0;
|
Alpha = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,11 +2,13 @@
|
|||||||
// See the LICENCE file in the repository root for full licence text.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using NUnit.Framework;
|
||||||
using osu.Framework.Extensions.Color4Extensions;
|
using osu.Framework.Extensions.Color4Extensions;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Graphics.Containers;
|
using osu.Framework.Graphics.Containers;
|
||||||
using osu.Framework.Graphics.Shapes;
|
using osu.Framework.Graphics.Shapes;
|
||||||
using osu.Framework.Input.Events;
|
using osu.Framework.Input.Events;
|
||||||
|
using osu.Framework.Threading;
|
||||||
using osu.Framework.Utils;
|
using osu.Framework.Utils;
|
||||||
using osu.Game.Beatmaps;
|
using osu.Game.Beatmaps;
|
||||||
using osu.Game.Rulesets.Osu;
|
using osu.Game.Rulesets.Osu;
|
||||||
@ -20,13 +22,18 @@ namespace osu.Game.Tests.Visual.Ranking
|
|||||||
{
|
{
|
||||||
public class TestSceneAccuracyHeatmap : OsuManualInputManagerTestScene
|
public class TestSceneAccuracyHeatmap : OsuManualInputManagerTestScene
|
||||||
{
|
{
|
||||||
private readonly Box background;
|
private Box background;
|
||||||
private readonly Drawable object1;
|
private Drawable object1;
|
||||||
private readonly Drawable object2;
|
private Drawable object2;
|
||||||
private readonly TestHeatmap heatmap;
|
private TestHeatmap heatmap;
|
||||||
|
private ScheduledDelegate automaticAdditionDelegate;
|
||||||
|
|
||||||
public TestSceneAccuracyHeatmap()
|
[SetUp]
|
||||||
|
public void Setup() => Schedule(() =>
|
||||||
{
|
{
|
||||||
|
automaticAdditionDelegate?.Cancel();
|
||||||
|
automaticAdditionDelegate = null;
|
||||||
|
|
||||||
Children = new[]
|
Children = new[]
|
||||||
{
|
{
|
||||||
background = new Box
|
background = new Box
|
||||||
@ -41,7 +48,7 @@ namespace osu.Game.Tests.Visual.Ranking
|
|||||||
},
|
},
|
||||||
object2 = new BorderCircle
|
object2 = new BorderCircle
|
||||||
{
|
{
|
||||||
Position = new Vector2(500, 300),
|
Position = new Vector2(100, 300),
|
||||||
},
|
},
|
||||||
heatmap = new TestHeatmap(new TestBeatmap(new OsuRuleset().RulesetInfo).BeatmapInfo, new List<HitEvent>())
|
heatmap = new TestHeatmap(new TestBeatmap(new OsuRuleset().RulesetInfo).BeatmapInfo, new List<HitEvent>())
|
||||||
{
|
{
|
||||||
@ -50,13 +57,14 @@ namespace osu.Game.Tests.Visual.Ranking
|
|||||||
Size = new Vector2(130)
|
Size = new Vector2(130)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
});
|
||||||
|
|
||||||
protected override void LoadComplete()
|
[Test]
|
||||||
|
public void TestManyHitPointsAutomatic()
|
||||||
{
|
{
|
||||||
base.LoadComplete();
|
AddStep("add scheduled delegate", () =>
|
||||||
|
{
|
||||||
Scheduler.AddDelayed(() =>
|
automaticAdditionDelegate = Scheduler.AddDelayed(() =>
|
||||||
{
|
{
|
||||||
var randomPos = new Vector2(
|
var randomPos = new Vector2(
|
||||||
RNG.NextSingle(object1.DrawPosition.X - object1.DrawSize.X / 2, object1.DrawPosition.X + object1.DrawSize.X / 2),
|
RNG.NextSingle(object1.DrawPosition.X - object1.DrawSize.X / 2, object1.DrawPosition.X + object1.DrawSize.X / 2),
|
||||||
@ -66,6 +74,15 @@ namespace osu.Game.Tests.Visual.Ranking
|
|||||||
heatmap.AddPoint(object2.Position, object1.Position, randomPos, RNG.NextSingle(10, 500));
|
heatmap.AddPoint(object2.Position, object1.Position, randomPos, RNG.NextSingle(10, 500));
|
||||||
InputManager.MoveMouseTo(background.ToScreenSpace(randomPos));
|
InputManager.MoveMouseTo(background.ToScreenSpace(randomPos));
|
||||||
}, 1, true);
|
}, 1, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
AddWaitStep("wait for some hit points", 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestManualPlacement()
|
||||||
|
{
|
||||||
|
AddStep("return user input", () => InputManager.UseParentInput = true);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool OnMouseDown(MouseDownEvent e)
|
protected override bool OnMouseDown(MouseDownEvent e)
|
||||||
|
Reference in New Issue
Block a user