| | | 1 | | using BenchmarkDotNet.Attributes; |
| | | 2 | | using ReFlex.Core.Common.Components; |
| | | 3 | | using ReFlex.Core.Common.Util; |
| | | 4 | | using ReFlex.Core.Interactivity.Interfaces; |
| | | 5 | | using ReFlex.Core.Interactivity.Util; |
| | | 6 | | using LegacyObserver = PointCloud.Benchmark.Interactivity.MultiInteractionObserver; |
| | | 7 | | using CurrentObserver = ReFlex.Core.Interactivity.Components.MultiInteractionObserver; |
| | | 8 | | |
| | | 9 | | namespace PointCloud.Benchmark.Benchmarks.Interactivity; |
| | | 10 | | |
| | | 11 | | // Adapters expose the base-class stages without including the different detectors. |
| | | 12 | | public sealed class LegacyObserverAdapter : LegacyObserver |
| | | 13 | | { |
| | | 14 | | public ExtremumDescription Classify(int x, int y) => ComputeExtremumType(PointCloud.AsJaggedArray(), x, y); |
| | | 15 | | public InteractionFrame Smooth(IList<Interaction> contacts) => ComputeSmoothingValue(contacts); |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | public sealed class CurrentObserverAdapter : CurrentObserver |
| | | 19 | | { |
| | | 20 | | public ExtremumDescription Classify(int x, int y) => ComputeExtremumType(PointCloud.AsJaggedArray(), x, y); |
| | | 21 | | public InteractionFrame Smooth(IList<Interaction> contacts) => ComputeSmoothingValue(contacts); |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | public static class ObserverComparisonSetup |
| | | 25 | | { |
| | | 26 | | public static void Configure(IInteractionObserver observer) |
| | 0 | 27 | | { |
| | 0 | 28 | | var cloud = new PointCloud3(640, 480); |
| | 0 | 29 | | foreach (var point in cloud.AsArray()) point.Z = 1.45f; |
| | 0 | 30 | | for (var i = 0; i < 20; i++) cloud[40 + i * 28, 240].Z = i % 2 == 0 ? 1.3f : 1.6f; |
| | 0 | 31 | | observer.PointCloud = cloud; |
| | 0 | 32 | | observer.Distance = 1.45f; |
| | 0 | 33 | | observer.ExtremumTypeCheckRadius = 12; |
| | 0 | 34 | | observer.ExtremumTypeCheckNumSamples = 16; |
| | 0 | 35 | | observer.ExtremumTypeCheckFittingPercentage = .75f; |
| | 0 | 36 | | observer.InteractionHistorySize = 25; |
| | 0 | 37 | | observer.NumSmoothingFrames = 25; |
| | 0 | 38 | | observer.FilterType = FilterType.MovingAverage; |
| | 0 | 39 | | observer.ContactRetentionSeconds = 1; |
| | 0 | 40 | | observer.TouchMergeDistance2D = 64; |
| | 0 | 41 | | observer.MaxConfidence = 30; |
| | 0 | 42 | | observer.DepthScale = 100; |
| | 0 | 43 | | observer.UseVelocityPrediction = false; |
| | 0 | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | [MemoryDiagnoser] |
| | | 48 | | public class ObserverExtremumComparison |
| | | 49 | | { |
| | | 50 | | private LegacyObserverAdapter _legacy = null!; |
| | | 51 | | private CurrentObserverAdapter _current = null!; |
| | | 52 | | |
| | | 53 | | [Params(ExtremumTypeCheckMethod.FixedRadius, ExtremumTypeCheckMethod.StochasticStatic, |
| | | 54 | | ExtremumTypeCheckMethod.StochasticDynamic)] |
| | | 55 | | public ExtremumTypeCheckMethod Sampling { get; set; } |
| | | 56 | | |
| | | 57 | | [GlobalSetup] |
| | | 58 | | public void Setup() |
| | | 59 | | { |
| | | 60 | | _legacy = new LegacyObserverAdapter(); |
| | | 61 | | _current = new CurrentObserverAdapter(); |
| | | 62 | | ObserverComparisonSetup.Configure(_legacy); |
| | | 63 | | ObserverComparisonSetup.Configure(_current); |
| | | 64 | | _legacy.ExtremumTypeCheckMethod = _current.ExtremumTypeCheckMethod = Sampling; |
| | | 65 | | for (var i = 0; i < 20; i++) |
| | | 66 | | { |
| | | 67 | | var expected = i % 2 == 0 ? ExtremumType.Minimum : ExtremumType.Maximum; |
| | | 68 | | if (_legacy.Classify(40 + i * 28, 240).Type != expected || |
| | | 69 | | _current.Classify(40 + i * 28, 240).Type != expected) |
| | | 70 | | throw new InvalidOperationException("Classifier output differs from the reference scene."); |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | [Benchmark(Baseline = true)] |
| | | 75 | | public int BenchmarkCopy() |
| | | 76 | | { |
| | | 77 | | var count = 0; |
| | | 78 | | for (var i = 0; i < 20; i++) count += _legacy.Classify(40 + i * 28, 240).NumFittingPoints; |
| | | 79 | | return count; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | [Benchmark] |
| | | 83 | | public int Current() |
| | | 84 | | { |
| | | 85 | | var count = 0; |
| | | 86 | | for (var i = 0; i < 20; i++) count += _current.Classify(40 + i * 28, 240).NumFittingPoints; |
| | | 87 | | return count; |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | [MemoryDiagnoser] |
| | | 92 | | public class ObserverSmoothingComparison |
| | | 93 | | { |
| | | 94 | | private LegacyObserverAdapter _legacy = null!; |
| | | 95 | | private CurrentObserverAdapter _current = null!; |
| | | 96 | | private List<Interaction> _contacts = null!; |
| | | 97 | | |
| | | 98 | | [GlobalSetup] |
| | | 99 | | public void Setup() |
| | | 100 | | { |
| | | 101 | | _legacy = new LegacyObserverAdapter(); |
| | | 102 | | _current = new CurrentObserverAdapter(); |
| | | 103 | | ObserverComparisonSetup.Configure(_legacy); |
| | | 104 | | ObserverComparisonSetup.Configure(_current); |
| | | 105 | | _contacts = Enumerable.Range(0, 20).Select(i => new Interaction |
| | | 106 | | { |
| | | 107 | | Position = new Point3(40 + i * 28, 240, -.5f), |
| | | 108 | | Type = InteractionType.Push, |
| | | 109 | | Confidence = 10 |
| | | 110 | | }).ToList(); |
| | | 111 | | for (var i = 0; i < 25; i++) |
| | | 112 | | { |
| | | 113 | | _legacy.Smooth(_contacts); |
| | | 114 | | _current.Smooth(_contacts); |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | [Benchmark(Baseline = true)] |
| | | 119 | | public InteractionFrame BenchmarkCopy() => _legacy.Smooth(_contacts); |
| | | 120 | | |
| | | 121 | | [Benchmark] |
| | | 122 | | public InteractionFrame Current() => _current.Smooth(_contacts); |
| | | 123 | | } |