From 59ab71f786f13e258479769f21065adfcaadd234 Mon Sep 17 00:00:00 2001 From: OliBomby Date: Fri, 20 Sep 2024 01:06:52 +0200 Subject: [PATCH 01/19] Implement minimum enclosing circle --- osu.Game/Utils/GeometryUtils.cs | 133 ++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/osu.Game/Utils/GeometryUtils.cs b/osu.Game/Utils/GeometryUtils.cs index 8572ac66095c..7e6db10a2804 100644 --- a/osu.Game/Utils/GeometryUtils.cs +++ b/osu.Game/Utils/GeometryUtils.cs @@ -6,6 +6,7 @@ using System.Linq; using osu.Framework.Graphics; using osu.Framework.Graphics.Primitives; +using osu.Framework.Utils; using osu.Game.Rulesets.Objects.Types; using osuTK; @@ -218,5 +219,137 @@ private static IEnumerable enumerateStartAndEndPositions(IEnumerable points) + { + // Iterating through all the points to check whether the points lie inside the circle or not + foreach (Vector2 p in points) + { + if (!isInside(c, p)) return false; + } + + return true; + } + + // Function to return the minimum enclosing circle for N <= 3 + private static (Vector2, float) minCircleTrivial(ReadOnlySpan points) + { + switch (points.Length) + { + case 0: + return (new Vector2(0, 0), 0); + + case 1: + return (points[0], 0); + + case 2: + return circleFrom(points[0], points[1]); + } + + // To check if MEC can be determined by 2 points only + for (int i = 0; i < 3; i++) + { + for (int j = i + 1; j < 3; j++) + { + var c = circleFrom(points[i], points[j]); + + if (isValidCircle(c, points)) + return c; + } + } + + return circleFrom(points[0], points[1], points[2]); + } + + // Returns the MEC using Welzl's algorithm + // Takes a set of input points P and a set R + // points on the circle boundary. + // n represents the number of points in P that are not yet processed. + private static (Vector2, float) welzlHelper(List points, ReadOnlySpan r, int n, Random random) + { + // Base case when all points processed or |R| = 3 + if (n == 0 || r.Length == 3) + return minCircleTrivial(r); + + // Pick a random point randomly + int idx = random.Next(n); + Vector2 p = points[idx]; + + // Put the picked point at the end of P since it's more efficient than + // deleting from the middle of the list + (points[idx], points[n - 1]) = (points[n - 1], points[idx]); + + // Get the MEC circle d from the set of points P - {p} + var d = welzlHelper(points, r, n - 1, random); + + // If d contains p, return d + if (isInside(d, p)) + return d; + + // Otherwise, must be on the boundary of the MEC + // Stackalloc to avoid allocations. It's safe to assume that the length of r will be at most 3 + Span r2 = stackalloc Vector2[r.Length + 1]; + r.CopyTo(r2); + r2[r.Length] = p; + + // Return the MEC for P - {p} and R U {p} + return welzlHelper(points, r2, n - 1, random); + } + + #endregion + + /// + /// Function to find the minimum enclosing circle for a collection of points. + /// + /// A tuple containing the circle center and radius. + public static (Vector2, float) MinimumEnclosingCircle(IEnumerable points) + { + // Using Welzl's algorithm to find the minimum enclosing circle + // https://www.geeksforgeeks.org/minimum-enclosing-circle-using-welzls-algorithm/ + List pCopy = points.ToList(); + return welzlHelper(pCopy, Array.Empty(), pCopy.Count, new Random()); + } + + /// + /// Function to find the minimum enclosing circle for a collection of hit objects. + /// + /// A tuple containing the circle center and radius. + public static (Vector2, float) MinimumEnclosingCircle(IEnumerable hitObjects) => + MinimumEnclosingCircle(enumerateStartAndEndPositions(hitObjects)); } } From ee006247516569cb9fdd380d66612f21290d48ee Mon Sep 17 00:00:00 2001 From: OliBomby Date: Fri, 20 Sep 2024 01:07:47 +0200 Subject: [PATCH 02/19] use minimum enclosing circle selection centre in rotation --- .../Edit/OsuSelectionRotationHandler.cs | 9 ++++----- .../SkinEditor/SkinSelectionRotationHandler.cs | 9 ++++----- .../Compose/Components/SelectionBoxRotationHandle.cs | 10 +++++++--- .../Compose/Components/SelectionRotationHandler.cs | 6 ++++++ 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Edit/OsuSelectionRotationHandler.cs b/osu.Game.Rulesets.Osu/Edit/OsuSelectionRotationHandler.cs index 62a39d3702c1..44d1543ae461 100644 --- a/osu.Game.Rulesets.Osu/Edit/OsuSelectionRotationHandler.cs +++ b/osu.Game.Rulesets.Osu/Edit/OsuSelectionRotationHandler.cs @@ -47,7 +47,6 @@ private void updateState() private OsuHitObject[]? objectsInRotation; - private Vector2? defaultOrigin; private Dictionary? originalPositions; private Dictionary? originalPathControlPointPositions; @@ -61,7 +60,7 @@ public override void Begin() changeHandler?.BeginChange(); objectsInRotation = selectedMovableObjects.ToArray(); - defaultOrigin = GeometryUtils.GetSurroundingQuad(objectsInRotation).Centre; + DefaultOrigin = GeometryUtils.MinimumEnclosingCircle(objectsInRotation).Item1; originalPositions = objectsInRotation.ToDictionary(obj => obj, obj => obj.Position); originalPathControlPointPositions = objectsInRotation.OfType().ToDictionary( obj => obj, @@ -73,9 +72,9 @@ public override void Update(float rotation, Vector2? origin = null) if (!OperationInProgress.Value) throw new InvalidOperationException($"Cannot {nameof(Update)} a rotate operation without calling {nameof(Begin)} first!"); - Debug.Assert(objectsInRotation != null && originalPositions != null && originalPathControlPointPositions != null && defaultOrigin != null); + Debug.Assert(objectsInRotation != null && originalPositions != null && originalPathControlPointPositions != null && DefaultOrigin != null); - Vector2 actualOrigin = origin ?? defaultOrigin.Value; + Vector2 actualOrigin = origin ?? DefaultOrigin.Value; foreach (var ho in objectsInRotation) { @@ -103,7 +102,7 @@ public override void Commit() objectsInRotation = null; originalPositions = null; originalPathControlPointPositions = null; - defaultOrigin = null; + DefaultOrigin = null; } private IEnumerable selectedMovableObjects => selectedItems.Cast() diff --git a/osu.Game/Overlays/SkinEditor/SkinSelectionRotationHandler.cs b/osu.Game/Overlays/SkinEditor/SkinSelectionRotationHandler.cs index 36b38543d11c..9fd28a1cad6a 100644 --- a/osu.Game/Overlays/SkinEditor/SkinSelectionRotationHandler.cs +++ b/osu.Game/Overlays/SkinEditor/SkinSelectionRotationHandler.cs @@ -46,7 +46,6 @@ private void updateState() private Drawable[]? objectsInRotation; - private Vector2? defaultOrigin; private Dictionary? originalRotations; private Dictionary? originalPositions; @@ -60,7 +59,7 @@ public override void Begin() objectsInRotation = selectedItems.Cast().ToArray(); originalRotations = objectsInRotation.ToDictionary(d => d, d => d.Rotation); originalPositions = objectsInRotation.ToDictionary(d => d, d => d.ToScreenSpace(d.OriginPosition)); - defaultOrigin = GeometryUtils.GetSurroundingQuad(objectsInRotation.SelectMany(d => d.ScreenSpaceDrawQuad.GetVertices().ToArray())).Centre; + DefaultOrigin = GeometryUtils.GetSurroundingQuad(objectsInRotation.SelectMany(d => d.ScreenSpaceDrawQuad.GetVertices().ToArray())).Centre; base.Begin(); } @@ -70,7 +69,7 @@ public override void Update(float rotation, Vector2? origin = null) if (objectsInRotation == null) throw new InvalidOperationException($"Cannot {nameof(Update)} a rotate operation without calling {nameof(Begin)} first!"); - Debug.Assert(originalRotations != null && originalPositions != null && defaultOrigin != null); + Debug.Assert(originalRotations != null && originalPositions != null && DefaultOrigin != null); if (objectsInRotation.Length == 1 && origin == null) { @@ -79,7 +78,7 @@ public override void Update(float rotation, Vector2? origin = null) return; } - var actualOrigin = origin ?? defaultOrigin.Value; + var actualOrigin = origin ?? DefaultOrigin.Value; foreach (var drawableItem in objectsInRotation) { @@ -100,7 +99,7 @@ public override void Commit() objectsInRotation = null; originalPositions = null; originalRotations = null; - defaultOrigin = null; + DefaultOrigin = null; base.Commit(); } diff --git a/osu.Game/Screens/Edit/Compose/Components/SelectionBoxRotationHandle.cs b/osu.Game/Screens/Edit/Compose/Components/SelectionBoxRotationHandle.cs index c62e0e0d4161..898efc8b5e56 100644 --- a/osu.Game/Screens/Edit/Compose/Components/SelectionBoxRotationHandle.cs +++ b/osu.Game/Screens/Edit/Compose/Components/SelectionBoxRotationHandle.cs @@ -113,9 +113,13 @@ protected override void OnDragEnd(DragEndEvent e) private float convertDragEventToAngleOfRotation(DragEvent e) { - // Adjust coordinate system to the center of SelectionBox - float startAngle = MathF.Atan2(e.LastMousePosition.Y - selectionBox.DrawHeight / 2, e.LastMousePosition.X - selectionBox.DrawWidth / 2); - float endAngle = MathF.Atan2(e.MousePosition.Y - selectionBox.DrawHeight / 2, e.MousePosition.X - selectionBox.DrawWidth / 2); + // Adjust coordinate system to the center of the selection + Vector2 center = rotationHandler?.DefaultOrigin is not null + ? selectionBox.ToLocalSpace(rotationHandler.ToScreenSpace(rotationHandler.DefaultOrigin.Value)) + : selectionBox.DrawSize / 2; + + float startAngle = MathF.Atan2(e.LastMousePosition.Y - center.Y, e.LastMousePosition.X - center.X); + float endAngle = MathF.Atan2(e.MousePosition.Y - center.Y, e.MousePosition.X - center.X); return (endAngle - startAngle) * 180 / MathF.PI; } diff --git a/osu.Game/Screens/Edit/Compose/Components/SelectionRotationHandler.cs b/osu.Game/Screens/Edit/Compose/Components/SelectionRotationHandler.cs index 532daaf7fa03..680acad11488 100644 --- a/osu.Game/Screens/Edit/Compose/Components/SelectionRotationHandler.cs +++ b/osu.Game/Screens/Edit/Compose/Components/SelectionRotationHandler.cs @@ -27,6 +27,12 @@ public partial class SelectionRotationHandler : Component /// public Bindable CanRotateAroundPlayfieldOrigin { get; private set; } = new BindableBool(); + /// + /// Implementation-defined origin point to rotate around when no explicit origin is provided. + /// This field is only assigned during a rotation operation. + /// + public Vector2? DefaultOrigin { get; protected set; } + /// /// Performs a single, instant, atomic rotation operation. /// From 8e11cda41a35919cfddf5b6e601686b4f549b335 Mon Sep 17 00:00:00 2001 From: OliBomby Date: Fri, 20 Sep 2024 01:07:54 +0200 Subject: [PATCH 03/19] use minimum enclosing circle selection centre in scale --- osu.Game.Rulesets.Osu/Edit/OsuSelectionScaleHandler.cs | 2 +- osu.Game/Overlays/SkinEditor/SkinSelectionScaleHandler.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Edit/OsuSelectionScaleHandler.cs b/osu.Game.Rulesets.Osu/Edit/OsuSelectionScaleHandler.cs index 56c3ba9315ed..e9d5b3105a14 100644 --- a/osu.Game.Rulesets.Osu/Edit/OsuSelectionScaleHandler.cs +++ b/osu.Game.Rulesets.Osu/Edit/OsuSelectionScaleHandler.cs @@ -84,10 +84,10 @@ public override void Begin() OriginalSurroundingQuad = objectsInScale.Count == 1 && objectsInScale.First().Key is Slider slider ? GeometryUtils.GetSurroundingQuad(slider.Path.ControlPoints.Select(p => slider.Position + p.Position)) : GeometryUtils.GetSurroundingQuad(objectsInScale.Keys); - defaultOrigin = OriginalSurroundingQuad.Value.Centre; originalConvexHull = objectsInScale.Count == 1 && objectsInScale.First().Key is Slider slider2 ? GeometryUtils.GetConvexHull(slider2.Path.ControlPoints.Select(p => slider2.Position + p.Position)) : GeometryUtils.GetConvexHull(objectsInScale.Keys); + defaultOrigin = GeometryUtils.MinimumEnclosingCircle(originalConvexHull).Item1; } public override void Update(Vector2 scale, Vector2? origin = null, Axes adjustAxis = Axes.Both, float axisRotation = 0) diff --git a/osu.Game/Overlays/SkinEditor/SkinSelectionScaleHandler.cs b/osu.Game/Overlays/SkinEditor/SkinSelectionScaleHandler.cs index 977aaade9952..691576921255 100644 --- a/osu.Game/Overlays/SkinEditor/SkinSelectionScaleHandler.cs +++ b/osu.Game/Overlays/SkinEditor/SkinSelectionScaleHandler.cs @@ -67,7 +67,7 @@ public override void Begin() objectsInScale = selectedItems.Cast().ToDictionary(d => d, d => new OriginalDrawableState(d)); OriginalSurroundingQuad = ToLocalSpace(GeometryUtils.GetSurroundingQuad(objectsInScale.SelectMany(d => d.Key.ScreenSpaceDrawQuad.GetVertices().ToArray()))); - defaultOrigin = OriginalSurroundingQuad.Value.Centre; + defaultOrigin = ToLocalSpace(GeometryUtils.MinimumEnclosingCircle(objectsInScale.SelectMany(d => d.Key.ScreenSpaceDrawQuad.GetVertices().ToArray())).Item1); isFlippedX = false; isFlippedY = false; From 92b5650ff8dab72c298a396960cb5ef51e1a5d3f Mon Sep 17 00:00:00 2001 From: OliBomby Date: Mon, 23 Sep 2024 10:56:03 +0200 Subject: [PATCH 04/19] fix outdated comment --- .../Screens/Edit/Compose/Components/SelectionRotationHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Screens/Edit/Compose/Components/SelectionRotationHandler.cs b/osu.Game/Screens/Edit/Compose/Components/SelectionRotationHandler.cs index 680acad11488..af3b3d64890f 100644 --- a/osu.Game/Screens/Edit/Compose/Components/SelectionRotationHandler.cs +++ b/osu.Game/Screens/Edit/Compose/Components/SelectionRotationHandler.cs @@ -13,7 +13,7 @@ namespace osu.Game.Screens.Edit.Compose.Components public partial class SelectionRotationHandler : Component { /// - /// Whether there is any ongoing scale operation right now. + /// Whether there is any ongoing rotation operation right now. /// public Bindable OperationInProgress { get; private set; } = new BindableBool(); From a9ebfbe431e4616a7a0c3ea49182065839471014 Mon Sep 17 00:00:00 2001 From: OliBomby Date: Mon, 23 Sep 2024 11:37:42 +0200 Subject: [PATCH 05/19] Assert default origin not null in rotation handle --- osu.Game.Tests/Visual/Editing/TestSceneComposeSelectBox.cs | 1 + .../Edit/Compose/Components/SelectionBoxRotationHandle.cs | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/osu.Game.Tests/Visual/Editing/TestSceneComposeSelectBox.cs b/osu.Game.Tests/Visual/Editing/TestSceneComposeSelectBox.cs index 30f397f518aa..2bf07d8e274f 100644 --- a/osu.Game.Tests/Visual/Editing/TestSceneComposeSelectBox.cs +++ b/osu.Game.Tests/Visual/Editing/TestSceneComposeSelectBox.cs @@ -84,6 +84,7 @@ public override void Begin() targetContainer = getTargetContainer(); initialRotation = targetContainer!.Rotation; + DefaultOrigin = ToLocalSpace(targetContainer.ToScreenSpace(Vector2.Zero)); base.Begin(); } diff --git a/osu.Game/Screens/Edit/Compose/Components/SelectionBoxRotationHandle.cs b/osu.Game/Screens/Edit/Compose/Components/SelectionBoxRotationHandle.cs index 898efc8b5e56..03d600bfa26e 100644 --- a/osu.Game/Screens/Edit/Compose/Components/SelectionBoxRotationHandle.cs +++ b/osu.Game/Screens/Edit/Compose/Components/SelectionBoxRotationHandle.cs @@ -77,6 +77,8 @@ protected override void OnDrag(DragEvent e) { base.OnDrag(e); + if (rotationHandler == null || !rotationHandler.OperationInProgress.Value) return; + rawCumulativeRotation += convertDragEventToAngleOfRotation(e); applyRotation(shouldSnap: e.ShiftPressed); @@ -114,9 +116,7 @@ protected override void OnDragEnd(DragEndEvent e) private float convertDragEventToAngleOfRotation(DragEvent e) { // Adjust coordinate system to the center of the selection - Vector2 center = rotationHandler?.DefaultOrigin is not null - ? selectionBox.ToLocalSpace(rotationHandler.ToScreenSpace(rotationHandler.DefaultOrigin.Value)) - : selectionBox.DrawSize / 2; + Vector2 center = selectionBox.ToLocalSpace(rotationHandler!.ToScreenSpace(rotationHandler!.DefaultOrigin!.Value)); float startAngle = MathF.Atan2(e.LastMousePosition.Y - center.Y, e.LastMousePosition.X - center.X); float endAngle = MathF.Atan2(e.MousePosition.Y - center.Y, e.MousePosition.X - center.X); From 0d06b122c1630e277864118b9cde787747902a21 Mon Sep 17 00:00:00 2001 From: OliBomby Date: Mon, 23 Sep 2024 11:39:42 +0200 Subject: [PATCH 06/19] rename region --- osu.Game/Utils/GeometryUtils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Utils/GeometryUtils.cs b/osu.Game/Utils/GeometryUtils.cs index 7e6db10a2804..c933006cc546 100644 --- a/osu.Game/Utils/GeometryUtils.cs +++ b/osu.Game/Utils/GeometryUtils.cs @@ -220,7 +220,7 @@ private static IEnumerable enumerateStartAndEndPositions(IEnumerable Date: Mon, 23 Sep 2024 11:42:02 +0200 Subject: [PATCH 07/19] use named tuple members --- osu.Game/Utils/GeometryUtils.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/osu.Game/Utils/GeometryUtils.cs b/osu.Game/Utils/GeometryUtils.cs index c933006cc546..51777f8ea0d9 100644 --- a/osu.Game/Utils/GeometryUtils.cs +++ b/osu.Game/Utils/GeometryUtils.cs @@ -223,9 +223,9 @@ private static IEnumerable enumerateStartAndEndPositions(IEnumerable points, ReadOnlySpan /// Function to find the minimum enclosing circle for a collection of points. /// - /// A tuple containing the circle center and radius. + /// A tuple containing the circle centre and radius. public static (Vector2, float) MinimumEnclosingCircle(IEnumerable points) { // Using Welzl's algorithm to find the minimum enclosing circle @@ -348,7 +348,7 @@ public static (Vector2, float) MinimumEnclosingCircle(IEnumerable point /// /// Function to find the minimum enclosing circle for a collection of hit objects. /// - /// A tuple containing the circle center and radius. + /// A tuple containing the circle centre and radius. public static (Vector2, float) MinimumEnclosingCircle(IEnumerable hitObjects) => MinimumEnclosingCircle(enumerateStartAndEndPositions(hitObjects)); } From d0f12006a4e8755179fa9cd0faf979dab93ae526 Mon Sep 17 00:00:00 2001 From: OliBomby Date: Mon, 23 Sep 2024 11:42:28 +0200 Subject: [PATCH 08/19] update wikipedia url --- osu.Game/Utils/GeometryUtils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Utils/GeometryUtils.cs b/osu.Game/Utils/GeometryUtils.cs index 51777f8ea0d9..8395c3a09003 100644 --- a/osu.Game/Utils/GeometryUtils.cs +++ b/osu.Game/Utils/GeometryUtils.cs @@ -234,7 +234,7 @@ private static (Vector2, float) circleFrom(Vector2 a, Vector2 b, Vector2 c) if (Precision.AlmostEquals(0, (b.Y - a.Y) * (c.X - a.X) - (b.X - a.X) * (c.Y - a.Y))) return circleFrom(a, b); - // See: https://en.wikipedia.org/wiki/Circumscribed_circle#Cartesian_coordinates_2 + // See: https://en.wikipedia.org/wiki/Circumcircle#Cartesian_coordinates float d = 2 * (a.X * (b - c).Y + b.X * (c - a).Y + c.X * (a - b).Y); float aSq = a.LengthSquared; float bSq = b.LengthSquared; From 40cfaabc53cc310809d91a89baebd0e279894bc0 Mon Sep 17 00:00:00 2001 From: OliBomby Date: Mon, 23 Sep 2024 11:43:36 +0200 Subject: [PATCH 09/19] verify n<=3 in minCircleTrivial --- osu.Game/Utils/GeometryUtils.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/osu.Game/Utils/GeometryUtils.cs b/osu.Game/Utils/GeometryUtils.cs index 8395c3a09003..93991efa22c0 100644 --- a/osu.Game/Utils/GeometryUtils.cs +++ b/osu.Game/Utils/GeometryUtils.cs @@ -269,6 +269,9 @@ private static bool isValidCircle((Vector2, float) c, ReadOnlySpan poin // Function to return the minimum enclosing circle for N <= 3 private static (Vector2, float) minCircleTrivial(ReadOnlySpan points) { + if (points.Length > 3) + throw new ArgumentException("Number of points must be at most 3", nameof(points)); + switch (points.Length) { case 0: From 42549e81aa9c750a6603c2e403ff403040a90c93 Mon Sep 17 00:00:00 2001 From: OliBomby Date: Mon, 23 Sep 2024 11:44:07 +0200 Subject: [PATCH 10/19] use RNG.Next --- osu.Game/Utils/GeometryUtils.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/osu.Game/Utils/GeometryUtils.cs b/osu.Game/Utils/GeometryUtils.cs index 93991efa22c0..d4968749bfa4 100644 --- a/osu.Game/Utils/GeometryUtils.cs +++ b/osu.Game/Utils/GeometryUtils.cs @@ -303,14 +303,14 @@ private static (Vector2, float) minCircleTrivial(ReadOnlySpan points) // Takes a set of input points P and a set R // points on the circle boundary. // n represents the number of points in P that are not yet processed. - private static (Vector2, float) welzlHelper(List points, ReadOnlySpan r, int n, Random random) + private static (Vector2, float) welzlHelper(List points, ReadOnlySpan r, int n) { // Base case when all points processed or |R| = 3 if (n == 0 || r.Length == 3) return minCircleTrivial(r); // Pick a random point randomly - int idx = random.Next(n); + int idx = RNG.Next(n); Vector2 p = points[idx]; // Put the picked point at the end of P since it's more efficient than @@ -318,7 +318,7 @@ private static (Vector2, float) welzlHelper(List points, ReadOnlySpan points, ReadOnlySpan point // Using Welzl's algorithm to find the minimum enclosing circle // https://www.geeksforgeeks.org/minimum-enclosing-circle-using-welzls-algorithm/ List pCopy = points.ToList(); - return welzlHelper(pCopy, Array.Empty(), pCopy.Count, new Random()); + return welzlHelper(pCopy, Array.Empty(), pCopy.Count); } /// From 86817d0cfc9be71adbd9f6ceb7ff369e880becd4 Mon Sep 17 00:00:00 2001 From: OliBomby Date: Mon, 23 Sep 2024 12:15:31 +0200 Subject: [PATCH 11/19] Add benchmark for minimum enclosing circle --- osu.Game.Benchmarks/BenchmarkGeometryUtils.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 osu.Game.Benchmarks/BenchmarkGeometryUtils.cs diff --git a/osu.Game.Benchmarks/BenchmarkGeometryUtils.cs b/osu.Game.Benchmarks/BenchmarkGeometryUtils.cs new file mode 100644 index 000000000000..2ab4d3369a3c --- /dev/null +++ b/osu.Game.Benchmarks/BenchmarkGeometryUtils.cs @@ -0,0 +1,29 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using BenchmarkDotNet.Attributes; +using osu.Framework.Utils; +using osu.Game.Utils; +using osuTK; + +namespace osu.Game.Benchmarks +{ + public class BenchmarkGeometryUtils : BenchmarkTest + { + [Params(100, 1000, 2000, 4000, 8000, 10000)] + public int N; + + private Vector2[] points = null!; + + public override void SetUp() + { + points = new Vector2[N]; + + for (int i = 0; i < points.Length; ++i) + points[i] = new Vector2(RNG.Next(512), RNG.Next(384)); + } + + [Benchmark] + public void MinimumEnclosingCircle() => GeometryUtils.MinimumEnclosingCircle(points); + } +} From 203951780ed50a9ab3338548c9dd9bf32131f14e Mon Sep 17 00:00:00 2001 From: OliBomby Date: Mon, 23 Sep 2024 12:15:42 +0200 Subject: [PATCH 12/19] use collection expression instead of stackalloc --- osu.Game/Utils/GeometryUtils.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/osu.Game/Utils/GeometryUtils.cs b/osu.Game/Utils/GeometryUtils.cs index d4968749bfa4..e365a0086220 100644 --- a/osu.Game/Utils/GeometryUtils.cs +++ b/osu.Game/Utils/GeometryUtils.cs @@ -325,13 +325,8 @@ private static (Vector2, float) welzlHelper(List points, ReadOnlySpan r2 = stackalloc Vector2[r.Length + 1]; - r.CopyTo(r2); - r2[r.Length] = p; - // Return the MEC for P - {p} and R U {p} - return welzlHelper(points, r2, n - 1); + return welzlHelper(points, [..r, p], n - 1); } #endregion From eead6b9eaea9aad7c5ed1b9afe6ef067de0afd3b Mon Sep 17 00:00:00 2001 From: OliBomby Date: Mon, 23 Sep 2024 13:13:33 +0200 Subject: [PATCH 13/19] return to stackalloc because its faster --- osu.Game/Utils/GeometryUtils.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/osu.Game/Utils/GeometryUtils.cs b/osu.Game/Utils/GeometryUtils.cs index e365a0086220..d4968749bfa4 100644 --- a/osu.Game/Utils/GeometryUtils.cs +++ b/osu.Game/Utils/GeometryUtils.cs @@ -325,8 +325,13 @@ private static (Vector2, float) welzlHelper(List points, ReadOnlySpan r2 = stackalloc Vector2[r.Length + 1]; + r.CopyTo(r2); + r2[r.Length] = p; + // Return the MEC for P - {p} and R U {p} - return welzlHelper(points, [..r, p], n - 1); + return welzlHelper(points, r2, n - 1); } #endregion From bf245aa9d61d2fc1f3cffede114f0ecd2a34a7e6 Mon Sep 17 00:00:00 2001 From: OliBomby Date: Mon, 23 Sep 2024 13:16:45 +0200 Subject: [PATCH 14/19] add a max depth to prevent stack overflow --- osu.Game/Utils/GeometryUtils.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/osu.Game/Utils/GeometryUtils.cs b/osu.Game/Utils/GeometryUtils.cs index d4968749bfa4..c4c63903bba0 100644 --- a/osu.Game/Utils/GeometryUtils.cs +++ b/osu.Game/Utils/GeometryUtils.cs @@ -305,8 +305,11 @@ private static (Vector2, float) minCircleTrivial(ReadOnlySpan points) // n represents the number of points in P that are not yet processed. private static (Vector2, float) welzlHelper(List points, ReadOnlySpan r, int n) { + const int max_depth = 4000; + // Base case when all points processed or |R| = 3 - if (n == 0 || r.Length == 3) + // To prevent stack overflow, we stop at a certain depth and give an approximate answer + if (n == 0 || r.Length == 3 || points.Count - n >= max_depth) return minCircleTrivial(r); // Pick a random point randomly From c857de3a9a45f691235a1ac9d4ddd5381e6e5042 Mon Sep 17 00:00:00 2001 From: OliBomby Date: Tue, 24 Sep 2024 11:44:02 +0200 Subject: [PATCH 15/19] Revert "add a max depth to prevent stack overflow" This reverts commit bf245aa9d61d2fc1f3cffede114f0ecd2a34a7e6. --- osu.Game/Utils/GeometryUtils.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/osu.Game/Utils/GeometryUtils.cs b/osu.Game/Utils/GeometryUtils.cs index c4c63903bba0..d4968749bfa4 100644 --- a/osu.Game/Utils/GeometryUtils.cs +++ b/osu.Game/Utils/GeometryUtils.cs @@ -305,11 +305,8 @@ private static (Vector2, float) minCircleTrivial(ReadOnlySpan points) // n represents the number of points in P that are not yet processed. private static (Vector2, float) welzlHelper(List points, ReadOnlySpan r, int n) { - const int max_depth = 4000; - // Base case when all points processed or |R| = 3 - // To prevent stack overflow, we stop at a certain depth and give an approximate answer - if (n == 0 || r.Length == 3 || points.Count - n >= max_depth) + if (n == 0 || r.Length == 3) return minCircleTrivial(r); // Pick a random point randomly From 3031b68552cc9b2caa498a92ca4c72c4711a8871 Mon Sep 17 00:00:00 2001 From: OliBomby Date: Tue, 24 Sep 2024 11:56:04 +0200 Subject: [PATCH 16/19] add TestMinimumEnclosingCircle --- osu.Game.Tests/Utils/GeometryUtilsTest.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/osu.Game.Tests/Utils/GeometryUtilsTest.cs b/osu.Game.Tests/Utils/GeometryUtilsTest.cs index ded4656ac16e..f73175bb5ba5 100644 --- a/osu.Game.Tests/Utils/GeometryUtilsTest.cs +++ b/osu.Game.Tests/Utils/GeometryUtilsTest.cs @@ -29,5 +29,23 @@ public void TestConvexHull(int[] values, int[] expected) Assert.That(hull, Is.EquivalentTo(expectedPoints)); } + + [TestCase(new int[] { }, 0, 0, 0)] + [TestCase(new[] { 0, 0 }, 0, 0, 0)] + [TestCase(new[] { 0, 0, 1, 1, 1, -1, 2, 0 }, 1, 0, 1)] + [TestCase(new[] { 0, 0, 1, 1, 1, -1, 2, 0, 1, 0 }, 1, 0, 1)] + [TestCase(new[] { 0, 0, 1, 1, 2, -1, 2, 0, 1, 0, 4, 10 }, 3, 4.5f, 5.5901699f)] + public void TestMinimumEnclosingCircle(int[] values, float x, float y, float r) + { + var points = new Vector2[values.Length / 2]; + for (int i = 0; i < values.Length; i += 2) + points[i / 2] = new Vector2(values[i], values[i + 1]); + + (var centre, float radius) = GeometryUtils.MinimumEnclosingCircle(points); + + Assert.That(centre.X, Is.EqualTo(x).Within(0.0001)); + Assert.That(centre.Y, Is.EqualTo(y).Within(0.0001)); + Assert.That(radius, Is.EqualTo(r).Within(0.0001)); + } } } From 2d95c0b0bbaf6d97c940414f2e6afc61aa15e656 Mon Sep 17 00:00:00 2001 From: OliBomby Date: Tue, 24 Sep 2024 18:45:52 +0200 Subject: [PATCH 17/19] remove tail recursion form welzl --- osu.Game/Utils/GeometryUtils.cs | 46 ++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/osu.Game/Utils/GeometryUtils.cs b/osu.Game/Utils/GeometryUtils.cs index d4968749bfa4..877f58769b0c 100644 --- a/osu.Game/Utils/GeometryUtils.cs +++ b/osu.Game/Utils/GeometryUtils.cs @@ -305,33 +305,37 @@ private static (Vector2, float) minCircleTrivial(ReadOnlySpan points) // n represents the number of points in P that are not yet processed. private static (Vector2, float) welzlHelper(List points, ReadOnlySpan r, int n) { - // Base case when all points processed or |R| = 3 - if (n == 0 || r.Length == 3) - return minCircleTrivial(r); + Span r2 = stackalloc Vector2[3]; + int rLength = r.Length; + r.CopyTo(r2); - // Pick a random point randomly - int idx = RNG.Next(n); - Vector2 p = points[idx]; + while (true) + { + // Base case when all points processed or |R| = 3 + if (n == 0 || rLength == 3) return minCircleTrivial(r2[..rLength]); - // Put the picked point at the end of P since it's more efficient than - // deleting from the middle of the list - (points[idx], points[n - 1]) = (points[n - 1], points[idx]); + // Pick a random point randomly + int idx = RNG.Next(n); + Vector2 p = points[idx]; - // Get the MEC circle d from the set of points P - {p} - var d = welzlHelper(points, r, n - 1); + // Put the picked point at the end of P since it's more efficient than + // deleting from the middle of the list + (points[idx], points[n - 1]) = (points[n - 1], points[idx]); - // If d contains p, return d - if (isInside(d, p)) - return d; + // Get the MEC circle d from the set of points P - {p} + var d = welzlHelper(points, r2[..rLength], n - 1); - // Otherwise, must be on the boundary of the MEC - // Stackalloc to avoid allocations. It's safe to assume that the length of r will be at most 3 - Span r2 = stackalloc Vector2[r.Length + 1]; - r.CopyTo(r2); - r2[r.Length] = p; + // If d contains p, return d + if (isInside(d, p)) return d; - // Return the MEC for P - {p} and R U {p} - return welzlHelper(points, r2, n - 1); + // Otherwise, must be on the boundary of the MEC + // Stackalloc to avoid allocations. It's safe to assume that the length of r will be at most 3 + r2[rLength] = p; + rLength++; + + // Return the MEC for P - {p} and R U {p} + n--; + } } #endregion From 796fc948e138f839b083826fb69e6130e303c0c2 Mon Sep 17 00:00:00 2001 From: OliBomby Date: Tue, 24 Sep 2024 20:15:03 +0200 Subject: [PATCH 18/19] Rewrite Welzl's algorithm to use no recursion --- osu.Game/Utils/GeometryUtils.cs | 104 ++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 45 deletions(-) diff --git a/osu.Game/Utils/GeometryUtils.cs b/osu.Game/Utils/GeometryUtils.cs index 877f58769b0c..e9e79deb49b4 100644 --- a/osu.Game/Utils/GeometryUtils.cs +++ b/osu.Game/Utils/GeometryUtils.cs @@ -255,7 +255,7 @@ private static (Vector2, float) circleFrom(Vector2 a, Vector2 b) } // Function to check whether a circle encloses the given points - private static bool isValidCircle((Vector2, float) c, ReadOnlySpan points) + private static bool isValidCircle((Vector2, float) c, List points) { // Iterating through all the points to check whether the points lie inside the circle or not foreach (Vector2 p in points) @@ -267,12 +267,12 @@ private static bool isValidCircle((Vector2, float) c, ReadOnlySpan poin } // Function to return the minimum enclosing circle for N <= 3 - private static (Vector2, float) minCircleTrivial(ReadOnlySpan points) + private static (Vector2, float) minCircleTrivial(List points) { - if (points.Length > 3) + if (points.Count > 3) throw new ArgumentException("Number of points must be at most 3", nameof(points)); - switch (points.Length) + switch (points.Count) { case 0: return (new Vector2(0, 0), 0); @@ -299,45 +299,6 @@ private static (Vector2, float) minCircleTrivial(ReadOnlySpan points) return circleFrom(points[0], points[1], points[2]); } - // Returns the MEC using Welzl's algorithm - // Takes a set of input points P and a set R - // points on the circle boundary. - // n represents the number of points in P that are not yet processed. - private static (Vector2, float) welzlHelper(List points, ReadOnlySpan r, int n) - { - Span r2 = stackalloc Vector2[3]; - int rLength = r.Length; - r.CopyTo(r2); - - while (true) - { - // Base case when all points processed or |R| = 3 - if (n == 0 || rLength == 3) return minCircleTrivial(r2[..rLength]); - - // Pick a random point randomly - int idx = RNG.Next(n); - Vector2 p = points[idx]; - - // Put the picked point at the end of P since it's more efficient than - // deleting from the middle of the list - (points[idx], points[n - 1]) = (points[n - 1], points[idx]); - - // Get the MEC circle d from the set of points P - {p} - var d = welzlHelper(points, r2[..rLength], n - 1); - - // If d contains p, return d - if (isInside(d, p)) return d; - - // Otherwise, must be on the boundary of the MEC - // Stackalloc to avoid allocations. It's safe to assume that the length of r will be at most 3 - r2[rLength] = p; - rLength++; - - // Return the MEC for P - {p} and R U {p} - n--; - } - } - #endregion /// @@ -348,8 +309,61 @@ public static (Vector2, float) MinimumEnclosingCircle(IEnumerable point { // Using Welzl's algorithm to find the minimum enclosing circle // https://www.geeksforgeeks.org/minimum-enclosing-circle-using-welzls-algorithm/ - List pCopy = points.ToList(); - return welzlHelper(pCopy, Array.Empty(), pCopy.Count); + List P = points.ToList(); + + var stack = new Stack<(Vector2?, int)>(); + var r = new List(3); + (Vector2, float) d = (Vector2.Zero, 0); + + stack.Push((null, P.Count)); + + while (stack.Count > 0) + { + // n represents the number of points in P that are not yet processed. + // p represents the point that was randomly picked to process. + (Vector2? p, int n) = stack.Pop(); + + if (!p.HasValue) + { + // Base case when all points processed or |R| = 3 + if (n == 0 || r.Count == 3) + { + d = minCircleTrivial(r); + continue; + } + + // Pick a random point randomly + int idx = RNG.Next(n); + p = P[idx]; + + // Put the picked point at the end of P since it's more efficient than + // deleting from the middle of the list + (P[idx], P[n - 1]) = (P[n - 1], P[idx]); + + // Schedule processing of p after we get the MEC circle d from the set of points P - {p} + stack.Push((p, n)); + // Get the MEC circle d from the set of points P - {p} + stack.Push((null, n - 1)); + } + else + { + // If d contains p, return d + if (isInside(d, p.Value)) + continue; + + // Remove points from R that were added in a deeper recursion + // |R| = |P| - |stack| - n + int removeCount = r.Count - (P.Count - stack.Count - n); + r.RemoveRange(r.Count - removeCount, removeCount); + + // Otherwise, must be on the boundary of the MEC + r.Add(p.Value); + // Return the MEC for P - {p} and R U {p} + stack.Push((null, n - 1)); + } + } + + return d; } /// From 21796900e2eeed8e8b9d707b285eca5a4184f6fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Fri, 27 Sep 2024 09:26:08 +0200 Subject: [PATCH 19/19] Fix code quality naming issue --- osu.Game/Utils/GeometryUtils.cs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/osu.Game/Utils/GeometryUtils.cs b/osu.Game/Utils/GeometryUtils.cs index e9e79deb49b4..eac86a9c02a7 100644 --- a/osu.Game/Utils/GeometryUtils.cs +++ b/osu.Game/Utils/GeometryUtils.cs @@ -309,21 +309,21 @@ public static (Vector2, float) MinimumEnclosingCircle(IEnumerable point { // Using Welzl's algorithm to find the minimum enclosing circle // https://www.geeksforgeeks.org/minimum-enclosing-circle-using-welzls-algorithm/ - List P = points.ToList(); + List p = points.ToList(); var stack = new Stack<(Vector2?, int)>(); var r = new List(3); (Vector2, float) d = (Vector2.Zero, 0); - stack.Push((null, P.Count)); + stack.Push((null, p.Count)); while (stack.Count > 0) { - // n represents the number of points in P that are not yet processed. - // p represents the point that was randomly picked to process. - (Vector2? p, int n) = stack.Pop(); + // `n` represents the number of points in P that are not yet processed. + // `point` represents the point that was randomly picked to process. + (Vector2? point, int n) = stack.Pop(); - if (!p.HasValue) + if (!point.HasValue) { // Base case when all points processed or |R| = 3 if (n == 0 || r.Count == 3) @@ -334,30 +334,30 @@ public static (Vector2, float) MinimumEnclosingCircle(IEnumerable point // Pick a random point randomly int idx = RNG.Next(n); - p = P[idx]; + point = p[idx]; // Put the picked point at the end of P since it's more efficient than // deleting from the middle of the list - (P[idx], P[n - 1]) = (P[n - 1], P[idx]); + (p[idx], p[n - 1]) = (p[n - 1], p[idx]); // Schedule processing of p after we get the MEC circle d from the set of points P - {p} - stack.Push((p, n)); + stack.Push((point, n)); // Get the MEC circle d from the set of points P - {p} stack.Push((null, n - 1)); } else { // If d contains p, return d - if (isInside(d, p.Value)) + if (isInside(d, point.Value)) continue; // Remove points from R that were added in a deeper recursion // |R| = |P| - |stack| - n - int removeCount = r.Count - (P.Count - stack.Count - n); + int removeCount = r.Count - (p.Count - stack.Count - n); r.RemoveRange(r.Count - removeCount, removeCount); // Otherwise, must be on the boundary of the MEC - r.Add(p.Value); + r.Add(point.Value); // Return the MEC for P - {p} and R U {p} stack.Push((null, n - 1)); }