< Summary - ReFlex - Library

Information
Class: ReFlex.Core.Interactivity.Components.ExtremumConsolidator
Assembly: ReFlex.Core.Interactivity
File(s): D:\a\reflex\reflex\library\src\Core\Interactivity\Components\ExtremumConsolidator.cs
Line coverage
100%
Covered lines: 51
Uncovered lines: 0
Coverable lines: 51
Total lines: 75
Line coverage: 100%
Branch coverage
96%
Covered branches: 31
Total branches: 32
Branch coverage: 96.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ValidateRadius(...)100%66100%
Consolidate(...)96.15%2626100%
Cell()100%11100%

File(s)

D:\a\reflex\reflex\library\src\Core\Interactivity\Components\ExtremumConsolidator.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using ReFlex.Core.Common.Components;
 5using Math = System.Math;
 6
 7namespace ReFlex.Core.Interactivity.Components;
 8
 9/// <summary>Combines nearby, already classified extrema into one pressure measurement before tracking.</summary>
 10public static class ExtremumConsolidator
 11{
 12    public const float DefaultRadius = 3;
 13    public const float MaxRadius = 32;
 14
 33615    public static float ValidateRadius(float radius) => float.IsFinite(radius) && radius >= 0 && radius <= MaxRadius
 33616        ? 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)
 24619    {
 24620        ValidateRadius(radius);
 23821        if (radius == 0 || candidates.Count < 2)
 15822            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.
 32826        var ordered = candidates.OrderByDescending(i => Math.Abs(i.Position.Z))
 24827            .ThenByDescending(i => i.ExtremumDescription?.PercentageFittingPoints ?? 0)
 82428            .ThenBy(i => i.Type).ThenBy(i => i.Position.X).ThenBy(i => i.Position.Y).ToArray();
 40829        (int X, int Y) Cell(Point3 p) => ((int)Math.Floor(p.X / radius), (int)Math.Floor(p.Y / radius));
 57630        var cells = ordered.Select((item, index) => (item, index)).GroupBy(pair => Cell(pair.item.Position))
 68031            .ToDictionary(group => group.Key, group => group.Select(pair => pair.index).ToArray());
 8032        var assigned = new bool[ordered.Length];
 8033        var result = new List<Interaction>();
 65634        for (var index = 0; index < ordered.Length; index++)
 24835        {
 33636            if (assigned[index]) continue;
 16037            var seed = ordered[index];
 16038            var cell = Cell(seed.Position);
 16039            var members = new List<int>();
 128040            for (var x = cell.X - 1; x <= cell.X + 1; x++)
 384041            for (var y = cell.Y - 1; y <= cell.Y + 1; y++)
 144042            {
 269243                if (!cells.TryGetValue((x, y), out var neighbours)) continue;
 108844                foreach (var neighbour in neighbours)
 26245                {
 27446                    if (assigned[neighbour] || ordered[neighbour].Type != seed.Type) continue;
 25047                    var point = ordered[neighbour].Position;
 25048                    var dx = point.X - seed.Position.X;
 25049                    var dy = point.Y - seed.Position.Y;
 50                    // Always relative to the seed: a chain of weak extrema cannot bridge fingers.
 49851                    if (dx * dx + dy * dy <= radius * radius) members.Add(neighbour);
 25052                }
 18853            }
 54
 16055            var measurement = new Interaction(seed);
 48056            double total = 0, sumX = 0, sumY = 0;
 122457            foreach (var member in members.OrderBy(i => i))
 24858            {
 24859                assigned[member] = true;
 24860                var candidate = ordered[member];
 24861                var weight = Math.Max(1e-6, Math.Abs(candidate.Position.Z));
 24862                total += weight;
 24863                sumX += weight * candidate.Position.X;
 24864                sumY += weight * candidate.Position.Y;
 24865                measurement.Confidence = Math.Max(measurement.Confidence, candidate.Confidence);
 24866            }
 67            // Subpixel centre avoids alternating between equal adjacent peaks. Keep the strongest
 68            // measured depth/type/quality; duplicates never multiply confidence or weaken pressure.
 16069            measurement.Position.X = (float)(sumX / total);
 16070            measurement.Position.Y = (float)(sumY / total);
 16071            result.Add(measurement);
 16072        }
 8073        return result;
 23874    }
 75}