| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Linq; |
| | | 3 | | using ReFlex.Core.Common.Components; |
| | | 4 | | |
| | | 5 | | namespace ReFlex.Core.Interactivity.Util; |
| | | 6 | | |
| | | 7 | | /// <summary>Assigns measurements to matching positions without mutating contact lifecycle state.</summary> |
| | | 8 | | internal static class GreedyContactAssignment |
| | | 9 | | { |
| | | 10 | | public static int[] Assign(List<Interaction> candidates, IReadOnlyList<(int TouchId, Point3 Position)> targets, |
| | | 11 | | float distanceLimit) |
| | 6 | 12 | | { |
| | | 13 | | // Preserve the previous algorithm, including its gate and deterministic pair ordering. |
| | 26 | 14 | | var pairs = candidates.SelectMany((candidate, index) => targets.Select(track => new |
| | 26 | 15 | | { |
| | 26 | 16 | | CandidateIndex = index, |
| | 26 | 17 | | TouchId = track.TouchId, |
| | 26 | 18 | | Distance = Point3.Squared2DDistance(candidate.Position, track.Position) |
| | 26 | 19 | | })) |
| | 10 | 20 | | .Where(pair => pair.Distance < distanceLimit) |
| | 8 | 21 | | .OrderBy(pair => pair.Distance) |
| | 8 | 22 | | .ThenBy(pair => pair.TouchId) |
| | 8 | 23 | | .ThenBy(pair => candidates[pair.CandidateIndex].Position.X) |
| | 8 | 24 | | .ThenBy(pair => candidates[pair.CandidateIndex].Position.Y) |
| | 14 | 25 | | .ThenBy(pair => pair.CandidateIndex); |
| | 6 | 26 | | var ids = Enumerable.Repeat(-1, candidates.Count).ToArray(); |
| | 6 | 27 | | var assignedTracks = new HashSet<int>(); |
| | 34 | 28 | | foreach (var pair in pairs) |
| | 8 | 29 | | if (ids[pair.CandidateIndex] < 0 && assignedTracks.Add(pair.TouchId)) |
| | 4 | 30 | | ids[pair.CandidateIndex] = pair.TouchId; |
| | 6 | 31 | | return ids; |
| | 6 | 32 | | } |
| | | 33 | | } |