| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using ReFlex.Core.Common.Components; |
| | | 5 | | using Math = System.Math; |
| | | 6 | | |
| | | 7 | | namespace ReFlex.Core.Interactivity.Components; |
| | | 8 | | |
| | | 9 | | /// <summary>Combines nearby, already classified extrema into one pressure measurement before tracking.</summary> |
| | | 10 | | public static class ExtremumConsolidator |
| | | 11 | | { |
| | | 12 | | public const float DefaultRadius = 3; |
| | | 13 | | public const float MaxRadius = 32; |
| | | 14 | | |
| | 336 | 15 | | public static float ValidateRadius(float radius) => float.IsFinite(radius) && radius >= 0 && radius <= MaxRadius |
| | 336 | 16 | | ? radius : throw new ArgumentOutOfRangeException(nameof(radius), "Extremum merge radius must be between 0 and 32 |
| | | 17 | | |
| | | 18 | | public static IList<Interaction> Consolidate(IList<Interaction> candidates, float radius) |
| | 246 | 19 | | { |
| | 246 | 20 | | ValidateRadius(radius); |
| | 238 | 21 | | if (radius == 0 || candidates.Count < 2) |
| | 158 | 22 | | return candidates; |
| | | 23 | | |
| | | 24 | | // Strongest pressure seeds first, independent of Parallel.For's arrival order. Buckets keep |
| | | 25 | | // neighbourhood searches local even before the tracker's candidate limit is applied. |
| | 328 | 26 | | var ordered = candidates.OrderByDescending(i => Math.Abs(i.Position.Z)) |
| | 248 | 27 | | .ThenByDescending(i => i.ExtremumDescription?.PercentageFittingPoints ?? 0) |
| | 824 | 28 | | .ThenBy(i => i.Type).ThenBy(i => i.Position.X).ThenBy(i => i.Position.Y).ToArray(); |
| | 408 | 29 | | (int X, int Y) Cell(Point3 p) => ((int)Math.Floor(p.X / radius), (int)Math.Floor(p.Y / radius)); |
| | 576 | 30 | | var cells = ordered.Select((item, index) => (item, index)).GroupBy(pair => Cell(pair.item.Position)) |
| | 680 | 31 | | .ToDictionary(group => group.Key, group => group.Select(pair => pair.index).ToArray()); |
| | 80 | 32 | | var assigned = new bool[ordered.Length]; |
| | 80 | 33 | | var result = new List<Interaction>(); |
| | 656 | 34 | | for (var index = 0; index < ordered.Length; index++) |
| | 248 | 35 | | { |
| | 336 | 36 | | if (assigned[index]) continue; |
| | 160 | 37 | | var seed = ordered[index]; |
| | 160 | 38 | | var cell = Cell(seed.Position); |
| | 160 | 39 | | var members = new List<int>(); |
| | 1280 | 40 | | for (var x = cell.X - 1; x <= cell.X + 1; x++) |
| | 3840 | 41 | | for (var y = cell.Y - 1; y <= cell.Y + 1; y++) |
| | 1440 | 42 | | { |
| | 2692 | 43 | | if (!cells.TryGetValue((x, y), out var neighbours)) continue; |
| | 1088 | 44 | | foreach (var neighbour in neighbours) |
| | 262 | 45 | | { |
| | 274 | 46 | | if (assigned[neighbour] || ordered[neighbour].Type != seed.Type) continue; |
| | 250 | 47 | | var point = ordered[neighbour].Position; |
| | 250 | 48 | | var dx = point.X - seed.Position.X; |
| | 250 | 49 | | var dy = point.Y - seed.Position.Y; |
| | | 50 | | // Always relative to the seed: a chain of weak extrema cannot bridge fingers. |
| | 498 | 51 | | if (dx * dx + dy * dy <= radius * radius) members.Add(neighbour); |
| | 250 | 52 | | } |
| | 188 | 53 | | } |
| | | 54 | | |
| | 160 | 55 | | var measurement = new Interaction(seed); |
| | 480 | 56 | | double total = 0, sumX = 0, sumY = 0; |
| | 1224 | 57 | | foreach (var member in members.OrderBy(i => i)) |
| | 248 | 58 | | { |
| | 248 | 59 | | assigned[member] = true; |
| | 248 | 60 | | var candidate = ordered[member]; |
| | 248 | 61 | | var weight = Math.Max(1e-6, Math.Abs(candidate.Position.Z)); |
| | 248 | 62 | | total += weight; |
| | 248 | 63 | | sumX += weight * candidate.Position.X; |
| | 248 | 64 | | sumY += weight * candidate.Position.Y; |
| | 248 | 65 | | measurement.Confidence = Math.Max(measurement.Confidence, candidate.Confidence); |
| | 248 | 66 | | } |
| | | 67 | | // Subpixel centre avoids alternating between equal adjacent peaks. Keep the strongest |
| | | 68 | | // measured depth/type/quality; duplicates never multiply confidence or weaken pressure. |
| | 160 | 69 | | measurement.Position.X = (float)(sumX / total); |
| | 160 | 70 | | measurement.Position.Y = (float)(sumY / total); |
| | 160 | 71 | | result.Add(measurement); |
| | 160 | 72 | | } |
| | 80 | 73 | | return result; |
| | 238 | 74 | | } |
| | | 75 | | } |