< Summary - ReFlex - Library

Information
Class: PointCloud.Benchmark.Interactivity.InteractionObserverBase
Assembly: ReFlex.PointCloud.Benchmark
File(s): D:\a\reflex\reflex\test\Library\Performance\PointCloud.Benchmark\Interactivity\InteractionObserverBase.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 252
Coverable lines: 252
Total lines: 512
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 79
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%210%
get_ExtremumMergeRadius()100%210%
get_ObservationTimeoutSeconds()100%210%
get_Distance()100%210%
get_MinDistance()100%210%
get_MaxDistance()100%210%
get_ExtremumTypeCheckNumSamples()100%210%
set_ExtremumTypeCheckNumSamples(...)0%620%
get_ExtremumTypeCheckRadius()100%210%
set_ExtremumTypeCheckRadius(...)0%620%
get_ExtremumTypeCheckFittingPercentage()100%210%
get_ExtremumTypeCheckMethod()100%210%
get_PredictionHorizonSeconds()100%210%
set_PredictionHorizonSeconds(...)100%210%
get_UseVelocityPrediction()100%210%
set_UseVelocityPrediction(...)100%210%
get_NumFramesForPrediction()100%210%
set_NumFramesForPrediction(...)100%210%
get_UseSecondDerivation()100%210%
set_UseSecondDerivation(...)100%210%
get_SecondDerivationMagnitude()100%210%
set_SecondDerivationMagnitude(...)100%210%
get_InteractionHistorySize()100%210%
set_InteractionHistorySize(...)0%620%
get_NumSmoothingFrames()100%210%
set_NumSmoothingFrames(...)0%2040%
get_UseMinimumCostAssignment()100%210%
get_AssignmentMissCost()100%210%
get_AssignmentBirthCost()100%210%
get_ConfidenceGainPerSecond()100%210%
set_ConfidenceGainPerSecond(...)100%210%
get_ConfidenceDecayPerSecond()100%210%
set_ConfidenceDecayPerSecond(...)100%210%
get_ContactRetentionSeconds()100%210%
set_ContactRetentionSeconds(...)0%620%
get_TouchMergeDistance2D()100%210%
set_TouchMergeDistance2D(...)0%620%
get_DepthScale()100%210%
set_DepthScale(...)0%620%
get_FilterType()100%210%
set_FilterType(...)0%620%
get_MinAngle()100%210%
get_MinConfidence()100%210%
get_MaxConfidence()100%210%
set_MaxConfidence(...)0%620%
get_MeasurePerformance()100%210%
get_FrameId()100%210%
UpdateFrameId(...)100%210%
ComputeSmoothingValue(...)0%2040%
ComputeExtremumType(...)0%650250%
ConvertDepthValue(...)0%210140%
ApplyConfidenceFilter(...)100%210%
ComputeExtremumType(...)100%210%
UpdatePerformanceMetrics(...)0%620%
CalculateAverageDistance()0%110100%
GetSample(...)100%210%
GenerateSamples()0%620%
UpdateSamples()0%620%

File(s)

D:\a\reflex\reflex\test\Library\Performance\PointCloud.Benchmark\Interactivity\InteractionObserverBase.cs

#LineLine coverage
 1using ReFlex.Core.Common.Components;
 2using ReFlex.Core.Common.Interfaces;
 3using ReFlex.Core.Common.Util;
 4using ReFlex.Core.Interactivity.Components;
 5using ReFlex.Core.Interactivity.Interfaces;
 6using ReFlex.Core.Interactivity.Util;
 7using Math = System.Math;
 8
 9namespace PointCloud.Benchmark.Interactivity
 10{
 11    public abstract class InteractionObserverBase : IInteractionObserver, IPerformanceReporter
 12    {
 13        #region Fields
 14
 15        private readonly InteractionSmoothingBehaviour _smoothingBehaviour;
 016        private int _numSmoothingFrames = 5;
 017        private int _historySize = 25;
 018        private double _contactRetentionSeconds = .1;
 019        private float _touchMergeDistance2d = 64f;
 020        private float _depthScale = 100.0f;
 21        private FilterType _filterType;
 22        private float _maxConfidence;
 23        private int _extremumTypeCheckRadius;
 24        private int _extremumTypeCheckNumSamples;
 25
 026        protected Tuple<int, int>[] _fixedSamples = Array.Empty<Tuple<int, int>>();
 027        protected Tuple<int, int>[] _stochasticSamples = Array.Empty<Tuple<int, int>>();
 28
 29        #endregion
 30
 31        #region Properties
 32
 033        public float ExtremumMergeRadius { get; set; } = ExtremumConsolidator.DefaultRadius;
 034        public double ObservationTimeoutSeconds { get; set; } = 1;
 35
 36        /// <summary>
 37        /// Gets or sets the distance.
 38        /// </summary>
 39        /// <value>
 40        /// The distance.
 41        /// </value>
 042        public float Distance { get; set; }
 43
 44        /// <summary>
 45        /// Gets or sets the minimum depth.
 46        /// </summary>
 47        /// <value>
 48        /// The maximum depth.
 49        /// </value>
 050        public float MinDistance { get; set; }
 51
 52        /// <summary>
 53        /// Gets or sets the maximum depth.
 54        /// </summary>
 55        /// <value>
 56        /// The maximum depth.
 57        /// </value>
 058        public float MaxDistance { get; set; }
 59
 60        /// <summary>
 61        /// defines how many points should be checked to determine the type of the extremum
 62        /// </summary>
 63        public int ExtremumTypeCheckNumSamples
 64        {
 065            get => _extremumTypeCheckNumSamples;
 66            set
 067            {
 068                if (_extremumTypeCheckNumSamples == value)
 069                    return;
 070                _extremumTypeCheckNumSamples = value;
 071                UpdateSamples();
 072            }
 73        }
 74
 75        /// <summary>
 76        /// The pixel radius in which the points are sampled for deriving the type of the local extremum
 77        /// </summary>
 78        public int ExtremumTypeCheckRadius
 79        {
 080            get => _extremumTypeCheckRadius;
 81            set
 082            {
 083                if (_extremumTypeCheckRadius == value)
 084                    return;
 085                _extremumTypeCheckRadius = value;
 086                UpdateSamples();
 087            }
 88        }
 89
 90        /// <summary>
 91        /// Which ratio of "correctly aligned" points should match to distinguish between Undefined or Minimum/Maximum ?
 92        /// </summary>
 093        public float ExtremumTypeCheckFittingPercentage { get; set; }
 94
 95        /// <summary>
 96        /// Defines how neighboring the points are sampled for determining the type of extremum.
 97        /// </summary>
 098        public ExtremumTypeCheckMethod ExtremumTypeCheckMethod { get; set; }
 99
 100        public double PredictionHorizonSeconds
 101        {
 0102            get => _smoothingBehaviour.PredictionHorizonSeconds;
 0103            set => _smoothingBehaviour.PredictionHorizonSeconds = value;
 104        }
 105
 106        public bool UseVelocityPrediction
 107        {
 0108            get => _smoothingBehaviour.UseVelocityForMapping;
 0109            set => _smoothingBehaviour.UseVelocityForMapping = value;
 110        }
 111        public int NumFramesForPrediction
 112        {
 0113            get => _smoothingBehaviour.NumFramesForPrediction;
 0114            set => _smoothingBehaviour.NumFramesForPrediction = value;
 115        }
 116        public bool UseSecondDerivation
 117        {
 0118            get => _smoothingBehaviour.UseSecondDerivation;
 0119            set => _smoothingBehaviour.UseSecondDerivation = value;
 120        }
 121        public float SecondDerivationMagnitude
 122        {
 0123            get => _smoothingBehaviour.SecondDerivationMagnitude;
 0124            set => _smoothingBehaviour.SecondDerivationMagnitude = value;
 125        }
 126
 127        public int InteractionHistorySize
 128        {
 0129            get => _historySize;
 130            set
 0131            {
 0132                _historySize = value;
 0133                if (_smoothingBehaviour != null)
 0134                {
 0135                    _smoothingBehaviour.NumFramesHistory = value;
 136                    // update depending smoothing/filter values
 0137                    NumSmoothingFrames = _numSmoothingFrames;
 0138                    ContactRetentionSeconds = _contactRetentionSeconds;
 0139                }
 0140            }
 141        }
 142
 143        public int NumSmoothingFrames
 144        {
 0145            get => _numSmoothingFrames;
 146            set
 0147            {
 0148                _numSmoothingFrames = value;
 0149                if (_smoothingBehaviour != null)
 0150                    _smoothingBehaviour.NumFramesSmoothing = value < _smoothingBehaviour.NumFramesHistory
 0151                        ? _smoothingBehaviour.NumFramesHistory
 0152                        : value;
 0153            }
 154        }
 155
 156        // TODO: write tests for negative (!) values and values larger than history
 157        // Shared-contract compatibility; this benchmark retains its historical tracker implementation.
 0158        public bool UseMinimumCostAssignment { get; set; } = true;
 0159        public float AssignmentMissCost { get; set; } = .5f;
 0160        public float AssignmentBirthCost { get; set; } = .5f;
 161
 162        public float ConfidenceGainPerSecond
 163        {
 0164            get => _smoothingBehaviour.ConfidenceGainPerSecond;
 0165            set => _smoothingBehaviour.ConfidenceGainPerSecond = value;
 166        }
 167        public float ConfidenceDecayPerSecond
 168        {
 0169            get => _smoothingBehaviour.ConfidenceDecayPerSecond;
 0170            set => _smoothingBehaviour.ConfidenceDecayPerSecond = value;
 171        }
 172
 173        public double ContactRetentionSeconds
 174        {
 0175            get => _contactRetentionSeconds;
 176            set
 0177            {
 0178                _contactRetentionSeconds = value;
 0179                if (_smoothingBehaviour != null)
 0180                    _smoothingBehaviour.ContactRetentionSeconds = value;
 0181            }
 182        }
 183
 184        public float TouchMergeDistance2D
 185        {
 0186            get => _touchMergeDistance2d;
 187            set
 0188            {
 0189                _touchMergeDistance2d = value;
 0190                if (_smoothingBehaviour != null)
 0191                    _smoothingBehaviour.TouchMergeDistance2D = value;
 0192            }
 193        }
 194
 195        public float DepthScale
 196        {
 0197            get => _depthScale;
 198            set
 0199            {
 0200                _depthScale = value;
 0201                if (_smoothingBehaviour != null)
 0202                    _smoothingBehaviour.DepthScale = value;
 0203            }
 204        }
 205
 206        public FilterType FilterType
 207        {
 0208            get => _filterType;
 209            set
 0210            {
 0211                _filterType = value;
 0212                if (_smoothingBehaviour != null)
 0213                    _smoothingBehaviour.UpdateFilterType(_filterType);
 0214            }
 215        }
 216
 217
 218        /// <summary>
 219        /// Gets or sets the minimum angle.
 220        /// </summary>
 221        /// <value>
 222        /// The minimum angle.
 223        /// </value>
 0224        public virtual float MinAngle { get; set; }
 225
 226        /// <summary>
 227        /// Gets or sets the minimum confidence.
 228        /// </summary>
 229        /// <value>
 230        /// The minimum confidence.
 231        /// </value>
 0232        public virtual float MinConfidence { get; set; }
 233
 234        /// <summary>
 235        /// Gets or sets the maximum confidence.
 236        /// </summary>
 237        /// <value>
 238        /// The maximum confidence.
 239        /// </value>
 240        public virtual float MaxConfidence
 241        {
 0242            get => _maxConfidence;
 243            set
 0244            {
 0245                _maxConfidence = value;
 0246                if (_smoothingBehaviour != null)
 0247                    _smoothingBehaviour.MaxConfidence = (int) MaxConfidence;
 0248            }
 249        }
 250
 0251        public bool MeasurePerformance { get; set; }
 252
 0253        protected int FrameId { get; private set; }
 254
 255        #region Abstract Members
 256
 257        public abstract ObserverType Type { get; }
 258        public abstract PointCloud3 PointCloud { get; set; }
 259        public abstract VectorField2 VectorField { get; set; }
 260
 261        public abstract event EventHandler<IList<Interaction>> NewInteractions;
 262        public event EventHandler<IList<InteractionVelocity>> NewVelocities;
 263
 264        public event EventHandler<PerformanceDataItem> PerformanceDataUpdated;
 265
 266        public event EventHandler<IList<InteractionFrame>> InteractionHistoryUpdated;
 267
 268        public abstract Task<ProcessingResult> Update();
 269
 270        #endregion
 271
 272        #region Constructor
 273
 0274        protected InteractionObserverBase()
 0275        {
 0276            _smoothingBehaviour = new InteractionSmoothingBehaviour(InteractionHistorySize);
 0277        }
 278
 279        #endregion
 280
 281        public void UpdateFrameId(int frameId)
 0282        {
 0283            FrameId = frameId;
 0284        }
 285
 286        #endregion
 287
 288        protected InteractionFrame ComputeSmoothingValue(IList<Interaction> rawInteractions)
 0289        {
 0290            var result = _smoothingBehaviour.Update(rawInteractions);
 0291            NewVelocities?.Invoke(this, _smoothingBehaviour.GetVelocities(result));
 0292            InteractionHistoryUpdated?.Invoke(this, _smoothingBehaviour.InteractionsFramesCache);
 293
 0294            return result;
 0295        }
 296
 297        protected virtual ExtremumDescription ComputeExtremumType(Point3[][] pointsArray, int xPos, int yPos)
 0298        {
 0299            if (ExtremumTypeCheckMethod == ExtremumTypeCheckMethod.Global)
 0300                return new ExtremumDescription
 0301                    { Type = ExtremumType.Maximum, NumFittingPoints = ExtremumTypeCheckNumSamples, PercentageFittingPoin
 302
 0303            var xMax = pointsArray.Length - 1;
 0304            var yMax = xMax > 0 ? pointsArray[0].Length - 1 : 0;
 305
 306            // refZ is the absolute distance from sensor
 0307            var refZ = Math.Abs(pointsArray[xPos][yPos].Z);
 308
 0309            var numCloser = 0;
 0310            var numFarther = 0;
 311
 312            Tuple<int, int>[] samples;
 0313            switch (ExtremumTypeCheckMethod)
 314            {
 315                case ExtremumTypeCheckMethod.StochasticStatic:
 0316                    samples = _stochasticSamples;
 0317                    break;
 318                case ExtremumTypeCheckMethod.StochasticDynamic:
 0319                    samples = GenerateSamples();
 0320                    break;
 321                case ExtremumTypeCheckMethod.Global:
 0322                    throw new ArgumentException("invalid Method 'Global' at this point.");
 323                case ExtremumTypeCheckMethod.FixedRadius:
 324                default:
 0325                    samples = _fixedSamples;
 0326                    break;
 327            }
 328
 0329            for (var i = 0; i < samples.Length; i++)
 0330            {
 0331                var sample = GetSample(pointsArray, xPos, yPos, samples, i, xMax, yMax);
 0332                if (Math.Abs(sample.Z) > refZ)
 0333                    numFarther++;
 334                else
 0335                    numCloser++;
 0336            }
 337
 338
 0339            var ratioMax = ExtremumTypeCheckNumSamples > 0 ? (float)numCloser / ExtremumTypeCheckNumSamples : 0;
 0340            var ratioMin = ExtremumTypeCheckNumSamples > 0 ? (float)numFarther / ExtremumTypeCheckNumSamples : 0;
 341
 0342            if (numCloser > numFarther && ratioMax > ExtremumTypeCheckFittingPercentage)
 0343            {
 0344                return new ExtremumDescription
 0345                {
 0346                    Type = ExtremumType.Maximum,
 0347                    NumFittingPoints = numCloser,
 0348                    PercentageFittingPoints = ratioMax
 0349                };
 350            }
 351
 0352            if (numFarther > numCloser && ratioMin > ExtremumTypeCheckFittingPercentage)
 0353            {
 0354                return new ExtremumDescription
 0355                {
 0356                    Type = ExtremumType.Minimum,
 0357                    NumFittingPoints = numFarther,
 0358                    PercentageFittingPoints = ratioMin
 0359                };
 360            }
 361
 0362            return new ExtremumDescription
 0363            {
 0364                Type = ExtremumType.Undefined,
 0365                NumFittingPoints = Math.Max(numCloser, numFarther),
 0366                PercentageFittingPoints = Math.Max(ratioMin, ratioMax)
 0367            };
 368
 0369        }
 370
 371        protected virtual List<Interaction> ConvertDepthValue(List<Interaction> interactions)
 0372        {
 0373            var result = new List<Interaction>();
 374
 0375            foreach (var item in interactions)
 0376            {
 0377                if (item == null)
 0378                    continue;
 379
 0380                if (item.Position.Z < Distance)
 0381                {
 0382                    var depth = (Distance - item.Position.Z - MinDistance) / (MinDistance - MaxDistance);
 383
 0384                    if (depth > 0)
 0385                        continue;
 386
 0387                    if (depth < -1)
 0388                        depth = -1;
 389
 0390                    item.Position.Z = depth;
 0391                }
 392                else
 0393                {
 0394                    var depth = (item.Position.Z - MinDistance - Distance) / (MaxDistance - MinDistance);
 395
 0396                    if (depth < 0)
 0397                        continue;
 398
 0399                    if (depth > 1)
 0400                        depth = 1;
 401
 0402                    item.Position.Z = depth;
 0403                }
 404
 0405                result.Add(item);
 0406            }
 407
 0408            return result;
 0409        }
 410
 411        protected virtual IEnumerable<Interaction> ApplyConfidenceFilter(IEnumerable<Interaction> interactions)
 0412        {
 0413            return interactions.Where(item => item.Confidence > MinConfidence);
 0414        }
 415
 416        protected virtual List<Interaction> ComputeExtremumType(List<Interaction> interactions, Point3[][] pointsArray)
 0417        {
 0418            interactions.ForEach(interaction =>
 0419                interaction.ExtremumDescription = ComputeExtremumType(pointsArray, (int)interaction.Position.X, (int)int
 420
 0421            return interactions;
 0422        }
 423
 424        protected virtual void UpdatePerformanceMetrics(ProcessPerformance perfItem)
 0425        {
 0426            var pData = new PerformanceDataItem
 0427            {
 0428                FrameId = Convert.ToInt32(FrameId),
 0429                FrameStart = DateTime.Now.Ticks,
 0430                Process = perfItem,
 0431                Stage = PerformanceDataStage.ProcessingData
 0432            };
 433
 0434            PerformanceDataUpdated?.Invoke(this, pData);
 0435        }
 436
 437        /// <summary>
 438        /// Calculates the average distance for the currently stored PointCloud.
 439        /// </summary>
 440        /// <returns></returns>
 441        public virtual float CalculateAverageDistance()
 0442        {
 0443            var sum = 0f;
 0444            var points = PointCloud?.AsArray();
 445
 0446            if (points == null)
 0447                return sum;
 448
 0449            var numValidSamples = 0;
 450
 0451            for (var i = 0; i < points.Length; ++i)
 0452            {
 0453                if (!points[i].IsValid || points[i].IsFiltered)
 0454                    continue;
 455
 0456                numValidSamples++;
 0457                sum += points[i].Z;
 0458            }
 459
 0460            return (float) decimal.Round((decimal) (sum / numValidSamples), 2);
 0461        }
 462
 463        private static Point3 GetSample(Point3[][] pointsArray, int xPos, int yPos, Tuple<int, int>[] samples, int i, in
 0464        {
 0465            var xIdx = xPos + samples[i].Item1;
 0466            xIdx = Math.Min(Math.Max(xIdx, 0), xMax);
 467
 0468            var yIdx = yPos + samples[i].Item2;
 0469            yIdx = Math.Min(Math.Max(yIdx, 0), yMax);
 0470            var sample = pointsArray[xIdx][yIdx];
 471
 0472            return sample;
 0473        }
 474
 475        private Tuple<int, int>[] GenerateSamples()
 0476        {
 0477            var result = new List<Tuple<int, int>>();
 478
 0479            var rnd = new Random();
 480
 0481            var min = (int)Math.Floor(0.5f * ExtremumTypeCheckRadius);
 0482            var max = ExtremumTypeCheckRadius;
 483
 0484            for (var i = 0; i < ExtremumTypeCheckNumSamples; i++)
 0485            {
 0486                var xStochastic = rnd.Next(min, max);
 0487                var yStochastic = rnd.Next(min, max);
 488
 0489                result.Add(new Tuple<int, int>(xStochastic, yStochastic));
 0490            }
 491
 0492            return result.ToArray();
 0493        }
 494
 495        private void UpdateSamples()
 0496        {
 0497            var samplesFixed = new List<Tuple<int, int>>();
 498
 0499            for (var i = 0; i < ExtremumTypeCheckNumSamples; i++)
 0500            {
 0501                var p = (float)i / ExtremumTypeCheckRadius;
 0502                var xFixed = (int) Math.Floor(ExtremumTypeCheckRadius * Math.Cos(p * 2 * Math.PI));
 0503                var yFixed = (int) Math.Floor(ExtremumTypeCheckRadius * Math.Sin(p * 2 * Math.PI));
 504
 0505                samplesFixed.Add(new Tuple<int, int>(xFixed, yFixed));
 0506            }
 0507            _fixedSamples = samplesFixed.ToArray();
 508
 0509            _stochasticSamples = GenerateSamples();
 0510        }
 511    }
 512}

Methods/Properties

.ctor()
get_ExtremumMergeRadius()
get_ObservationTimeoutSeconds()
get_Distance()
get_MinDistance()
get_MaxDistance()
get_ExtremumTypeCheckNumSamples()
set_ExtremumTypeCheckNumSamples(System.Int32)
get_ExtremumTypeCheckRadius()
set_ExtremumTypeCheckRadius(System.Int32)
get_ExtremumTypeCheckFittingPercentage()
get_ExtremumTypeCheckMethod()
get_PredictionHorizonSeconds()
set_PredictionHorizonSeconds(System.Double)
get_UseVelocityPrediction()
set_UseVelocityPrediction(System.Boolean)
get_NumFramesForPrediction()
set_NumFramesForPrediction(System.Int32)
get_UseSecondDerivation()
set_UseSecondDerivation(System.Boolean)
get_SecondDerivationMagnitude()
set_SecondDerivationMagnitude(System.Single)
get_InteractionHistorySize()
set_InteractionHistorySize(System.Int32)
get_NumSmoothingFrames()
set_NumSmoothingFrames(System.Int32)
get_UseMinimumCostAssignment()
get_AssignmentMissCost()
get_AssignmentBirthCost()
get_ConfidenceGainPerSecond()
set_ConfidenceGainPerSecond(System.Single)
get_ConfidenceDecayPerSecond()
set_ConfidenceDecayPerSecond(System.Single)
get_ContactRetentionSeconds()
set_ContactRetentionSeconds(System.Double)
get_TouchMergeDistance2D()
set_TouchMergeDistance2D(System.Single)
get_DepthScale()
set_DepthScale(System.Single)
get_FilterType()
set_FilterType(ReFlex.Core.Interactivity.Util.FilterType)
get_MinAngle()
get_MinConfidence()
get_MaxConfidence()
set_MaxConfidence(System.Single)
get_MeasurePerformance()
get_FrameId()
UpdateFrameId(System.Int32)
ComputeSmoothingValue(System.Collections.Generic.IList`1<ReFlex.Core.Common.Components.Interaction>)
ComputeExtremumType(ReFlex.Core.Common.Components.Point3[][],System.Int32,System.Int32)
ConvertDepthValue(System.Collections.Generic.List`1<ReFlex.Core.Common.Components.Interaction>)
ApplyConfidenceFilter(System.Collections.Generic.IEnumerable`1<ReFlex.Core.Common.Components.Interaction>)
ComputeExtremumType(System.Collections.Generic.List`1<ReFlex.Core.Common.Components.Interaction>,ReFlex.Core.Common.Components.Point3[][])
UpdatePerformanceMetrics(ReFlex.Core.Common.Util.ProcessPerformance)
CalculateAverageDistance()
GetSample(ReFlex.Core.Common.Components.Point3[][],System.Int32,System.Int32,System.Tuple`2<System.Int32,System.Int32>[],System.Int32,System.Int32,System.Int32)
GenerateSamples()
UpdateSamples()