< Summary - ReFlex - Library

Information
Class: ReFlex.Core.Interactivity.Components.MultiInteractionObserver
Assembly: ReFlex.Core.Interactivity
File(s): D:\a\reflex\reflex\library\src\Core\Interactivity\Components\MultiInteractionObserver.cs
Line coverage
29%
Covered lines: 25
Uncovered lines: 59
Coverable lines: 84
Total lines: 169
Line coverage: 29.7%
Branch coverage
28%
Covered branches: 8
Total branches: 28
Branch coverage: 28.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
get_Type()100%210%
get_PointCloud()100%11100%
set_PointCloud(...)100%2283.33%
get_VectorField()100%11100%
set_VectorField(...)50%2266.66%
CheckInitialState()37.5%14854.54%
InitializeConfidenceMatrix()50%4475%
Analyze(...)0%156120%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Diagnostics;
 4using System.Linq;
 5using System.Threading.Tasks;
 6using ReFlex.Core.Common.Components;
 7using ReFlex.Core.Common.Util;
 8
 9namespace ReFlex.Core.Interactivity.Components
 10{
 11    /// <summary>
 12    /// Finds multiple Interactions on a 2d vectorfield.
 13    /// </summary>
 14    public class MultiInteractionObserver : InteractionObserverBase
 15    {
 16        #region Fields
 17
 18        private PointCloud3 _pointCloud;
 19        private VectorField2 _vectorField;
 20        private int[][] _confidenceMat;
 21
 1222        private bool _isProcessing = false;
 23
 1224        private readonly Stopwatch _stopWatch = new();
 25
 26
 27        #endregion
 28
 29        #region Properties
 30
 31        /// <summary>
 32        /// Gets the type.
 33        /// </summary>
 34        /// <value>
 35        /// The type.
 36        /// </value>
 037        public override ObserverType Type => ObserverType.MultiTouch;
 38
 39        /// <summary>
 40        /// Gets or sets the point cloud.
 41        /// </summary>
 42        /// <value>
 43        /// The point cloud.
 44        /// </value>
 45        public override PointCloud3 PointCloud
 46        {
 2447            get => _pointCloud;
 48            set
 1449            {
 1450                if (_pointCloud == value)
 051                    return;
 52
 1453                _pointCloud = value;
 1454                InitializeConfidenceMatrix();
 1455            }
 56        }
 57
 58        /// <summary>
 59        /// Gets or sets the vector field.
 60        /// </summary>
 61        /// <value>
 62        /// The vector field.
 63        /// </value>
 64        public override VectorField2 VectorField
 65        {
 1466            get => _vectorField;
 67            set
 468            {
 469                if (_vectorField == value)
 470                    return;
 71
 072                _vectorField = value;
 073                InitializeConfidenceMatrix();
 474            }
 75        }
 76
 77        #endregion
 78
 79        /// <summary>
 80        /// Initializes a new instance of the <see cref="MultiInteractionObserver"/> class.
 81        /// </summary>
 1282        public MultiInteractionObserver()
 1283        {
 1284        }
 85
 86        protected override Task<ProcessingResult> CheckInitialState()
 287        {
 288          if (PointCloud == null ||
 289              VectorField == null ||
 290              _confidenceMat == null)
 291            return Task.FromResult(new ProcessingResult(ProcessServiceStatus.Error));
 92
 093          var processResult = new ProcessingResult(ProcessServiceStatus.Available);
 94
 095          return Task.FromResult(
 096              _isProcessing
 097                  ? new ProcessingResult(ProcessServiceStatus.Skipped)
 098                  : processResult);
 299        }
 100
 101        /// <summary>
 102        /// Initializes the confidence matrix.
 103        /// </summary>
 104        private void InitializeConfidenceMatrix()
 14105        {
 14106            if (VectorField != null && !_isProcessing)
 0107                ArrayUtils.InitializeArray(out _confidenceMat, VectorField.SizeX, VectorField.SizeY);
 14108        }
 109
 110        protected override Task<Tuple<IEnumerable<Interaction>, ProcessPerformance>> Analyze(ProcessPerformance performa
 0111        {
 0112            performance.Preparation = TimeSpan.Zero;
 113
 0114            var stride = VectorField.Stride;
 0115            var pointCloud = PointCloud.AsJaggedArray();
 0116            var vectorField = VectorField.AsJaggedArray();
 117
 0118            var result = new List<Interaction>();
 119
 0120            _isProcessing = true;
 121
 0122            Parallel.For(1, (VectorField.SizeX - 1) / stride, (i) =>
 0123            {
 0124                var x = i * stride;
 0125
 0126                for (var y = stride; y < VectorField.SizeY - stride; y += stride)
 0127                {
 0128                    var vCenter = vectorField[x][y];
 0129
 0130                    if (!vCenter.IsValid)
 0131                        continue;
 0132
 0133                    var vectorX = vCenter.Add(vectorField[x + stride][y]);
 0134                    var vectorX2 = vCenter.Add(vectorField[x - stride][y]);
 0135                    var vectorY = vCenter.Add(vectorField[x][y + stride]);
 0136                    var vectorY2 = vCenter.Add(vectorField[x][y - stride]);
 0137
 0138                    var angleX = vectorX.AngleBetween(vectorX2);
 0139                    var angleY = vectorY.AngleBetween(vectorY2);
 0140                    var angle = angleX + angleY / 2f;
 0141
 0142                    var confidence = _confidenceMat[x][y];
 0143                    if (float.IsNaN(angle) || angle > MinAngle)
 0144                    {
 0145                        if (confidence > 0)
 0146                            _confidenceMat[x][y]--;
 0147                    }
 0148                    else
 0149                    {
 0150                        lock (result)
 0151                        {
 0152                            result.Add(new Interaction(new Point3(x, y, pointCloud[x][y].Z), InteractionType.None,
 0153                                confidence));
 0154                        }
 0155
 0156                        if (confidence < MaxConfidence)
 0157                            _confidenceMat[x][y]++;
 0158                    }
 0159                }
 0160            });
 161
 0162            _isProcessing = false;
 163
 0164            return Task.FromResult(new Tuple<IEnumerable<Interaction>, ProcessPerformance>(result.ToArray(), performance
 0165        }
 166
 167    }
 168
 169}