-
Notifications
You must be signed in to change notification settings - Fork 10
Implement lookup-table-based rectangle approximation. #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b07d340
Implement lookup-table-based rectangle approximation.
Veeno 213bead
Fix formatting.
Veeno 7a047f2
Optimize RectUtil.LessThanGeoMean (x is always between m.a and m.b).
Veeno b0e284d
Optimize RectUtil.LessThanGeoMean (m.b is always greater than m.a).
Veeno d40fa38
Refactor RectUtil value generation strategy.
Veeno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| using System.Collections.Immutable; | ||
|
|
||
| namespace RLBotCS.ManagerTools; | ||
|
|
||
| public static class RectUtil | ||
| { | ||
| /// <summary> | ||
| /// Defines the maximum represented aspect ratio (<tt>Limit</tt>/1) | ||
| /// as well as the granularity of stored ratios.<br/> | ||
| /// Number of entries and the total resulting size in memory | ||
| /// for different <tt>Limit</tt> values are as follows: | ||
| /// <code> | ||
| /// ┌───────┬─────────┬────────────┐ | ||
| /// │ Limit │ entries │ size │ | ||
| /// ├───────┼─────────┼────────────┤ | ||
| /// │ 512 │ 1174 │ 9.17 kiB │ | ||
| /// │ 1024 │ 2563 │ 20.02 kiB │ | ||
| /// │ 2048 │ 5555 │ 43.4 kiB │ | ||
| /// │ 4096 │ 11977 │ 93.57 kiB │ | ||
| /// │ 8192 │ 25673 │ 200.57 kiB │ | ||
| /// │ 16384 │ 54778 │ 427.95 kiB │ | ||
| /// └───────┴─────────┴────────────┘ | ||
| /// </code> | ||
| /// </summary> | ||
| public const ushort Limit = 4096; | ||
|
|
||
| private const float HalfPrecisionRangeHigh = 4096f; | ||
| private const float HalfPrecisionRangeLow = 1.0f / HalfPrecisionRangeHigh; | ||
|
|
||
| private static readonly ImmutableArray<float> ratios; | ||
| private static readonly ImmutableArray<(ushort, ushort)> rects; | ||
|
|
||
| static RectUtil() | ||
| { | ||
| SortedDictionary<float, (ushort, ushort)> dictionary = []; | ||
|
|
||
| static int Gcd(int a, int b) | ||
| { | ||
| // Greatest common divisor by Euclidean algorithm https://stackoverflow.com/a/41766138 | ||
| while (a != 0 && b != 0) | ||
| { | ||
| if (a > b) | ||
| a %= b; | ||
| else | ||
| b %= a; | ||
| } | ||
|
|
||
| return a | b; | ||
| } | ||
|
|
||
| for (ushort a = 1; a <= Limit; ++a) | ||
| for (ushort b = 1; b <= a && a * b <= Limit; ++b) | ||
| if (Gcd(a, b) == 1) | ||
| dictionary.Add((float)a / b, (a, b)); | ||
|
|
||
| ratios = [.. dictionary.Keys]; | ||
| rects = [.. dictionary.Values]; | ||
| } | ||
|
|
||
| private static float GeoMean(float a, float b) | ||
| { | ||
| if ( | ||
| a >= HalfPrecisionRangeHigh | ||
| || b >= HalfPrecisionRangeHigh | ||
| || a <= HalfPrecisionRangeLow | ||
| || b <= HalfPrecisionRangeLow | ||
| ) | ||
| return MathF.Sqrt(a) * MathF.Sqrt(b); | ||
| return MathF.Sqrt(a * b); | ||
| } | ||
|
|
||
| private static bool LessThanGeoMean(float x, (float a, float b) m) | ||
| { | ||
| if (m.b >= HalfPrecisionRangeHigh) | ||
| return x < MathF.Sqrt(m.a) * MathF.Sqrt(m.b); | ||
| return x * x < m.a * m.b; | ||
| } | ||
|
|
||
| private static (ushort, ushort) Find(float value) | ||
| { | ||
| int higherIdx = ratios.BinarySearch(value); | ||
|
|
||
| if (higherIdx >= 0) | ||
| return rects[higherIdx]; | ||
|
|
||
| higherIdx = ~higherIdx; | ||
|
|
||
| // No need to handle this because value >= 1.0 == ratios.First() | ||
| //if (higherIdx == 0) | ||
| // return rects.First(); | ||
|
|
||
| if (higherIdx == ratios.Length) | ||
| return rects.Last(); | ||
|
|
||
| int lowerIdx = higherIdx - 1; | ||
| return rects[ | ||
| LessThanGeoMean(value, (ratios[lowerIdx], ratios[higherIdx])) | ||
| ? lowerIdx | ||
| : higherIdx | ||
| ]; | ||
| } | ||
|
|
||
| private static (ushort cols, ushort rows) Find(float width, float height) | ||
| { | ||
| if (width >= height) | ||
| return Find(width / height); | ||
|
|
||
| (ushort rows, ushort cols) = Find(height / width); | ||
| return (cols, rows); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Approximates the rectangle <tt>width</tt>×<tt>height</tt> with <tt>cols</tt>×<tt>rows</tt> | ||
| /// rectangles with dimensions <tt>elementWidth</tt>×<tt>elementHeight</tt> scaled by <tt>scale</tt>. | ||
| /// </summary> | ||
| public static (ushort cols, ushort rows, float scale) ApproximateRect( | ||
| int width, | ||
| int height, | ||
| int elementWidth, | ||
| int elementHeight | ||
| ) | ||
| { | ||
| float elementsInWidth = (float)width / elementWidth; | ||
| float elementsInHeight = (float)height / elementHeight; | ||
| (ushort cols, ushort rows) = Find(elementsInWidth, elementsInHeight); | ||
|
|
||
| // Ideal horizontal and vertical scale are | ||
| // ((float)width / cols) / elementWidth == ((float)width / elementWidth) / cols == elementsInWidth / cols | ||
| // ((float)height / rows) / elementHeight == ((float)height / elementHeight) / rows == elementsInHeight / rows | ||
| return (cols, rows, GeoMean(elementsInWidth / cols, elementsInHeight / rows)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using RLBotCS.ManagerTools; | ||
|
|
||
| namespace RLBotCSTests.ManagerTools; | ||
|
|
||
| [TestClass] | ||
| public class RectUtilTest | ||
| { | ||
| const int TestUpTo = 64; | ||
|
|
||
| [TestMethod] | ||
| public void ApproximateRectTest() | ||
| { | ||
| for (int i = 1; i <= TestUpTo; ++i) | ||
| { | ||
| for (int j = 1; j <= TestUpTo; ++j) | ||
| { | ||
| (ushort cols, ushort rows, float scale) = RectUtil.ApproximateRect(i, i, j, j); | ||
| Assert.AreEqual(1, cols); | ||
| Assert.AreEqual(1, rows); | ||
| // Slightly iffy, but it passes. | ||
| Assert.AreEqual((float)i / j, scale); | ||
| } | ||
| } | ||
|
|
||
| for (int i = 1; i <= TestUpTo; ++i) | ||
| { | ||
| float iMin = i * 0.95f; | ||
| float iMax = i * 1.05f; | ||
| for (int j = 1; j <= TestUpTo; ++j) | ||
| { | ||
| float jMin = j * 0.95f; | ||
| float jMax = j * 1.05f; | ||
| for (int k = 1; k <= TestUpTo; ++k) | ||
| { | ||
| for (int l = 1; l <= TestUpTo; ++l) | ||
| { | ||
| (ushort cols, ushort rows, float scale) = RectUtil.ApproximateRect( | ||
| i, | ||
| j, | ||
| k, | ||
| l | ||
| ); | ||
| Assert.IsInRange(iMin, iMax, k * scale * cols); | ||
| Assert.IsInRange(jMin, jMax, l * scale * rows); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.