< Summary - ReFlex - Library

Information
Class: PointCloud.Benchmark.Filter.BoxFilter
Assembly: ReFlex.PointCloud.Benchmark
File(s): D:\a\reflex\reflex\test\Library\Performance\PointCloud.Benchmark\Filter\BoxFilter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 151
Coverable lines: 151
Total lines: 271
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 96
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Radius()100%210%
set_Radius(...)100%210%
get_DefaultValue()100%210%
.ctor(...)100%210%
Filter(...)0%600240%
FilterOptimized(...)0%600240%
FilterOptimized2(...)0%600240%
FilterOptimized3(...)0%600240%
SetRadius(...)100%210%
ComputeIndex(...)100%210%

File(s)

D:\a\reflex\reflex\test\Library\Performance\PointCloud.Benchmark\Filter\BoxFilter.cs

#LineLine coverage
 1using PointCloud.Benchmark.Common;
 2
 3namespace PointCloud.Benchmark.Filter;
 4
 5/// <summary>
 6/// A two-dimensional box-blur with separated kernels
 7/// </summary>
 8public class BoxFilter
 9{
 10    #region fields
 11
 12    private int _radius, _diameter, _width;
 13
 14    #endregion
 15
 16    #region properties
 17
 18    /// <summary>
 19    /// Gets or sets the radius.
 20    /// </summary>
 21    /// <value>
 22    /// The radius.
 23    /// </value>
 24    public int Radius
 25    {
 026        get => _radius;
 027        set => SetRadius(value);
 28    }
 29
 030    public float DefaultValue { get; set; }
 31
 32    #endregion
 33
 34    #region constructor
 35
 36    /// <summary>
 37    /// Initializes a new instance of the <see cref="BoxFilter"/> class.
 38    /// </summary>
 39    /// <param name="radius">The radius.</param>
 040    public BoxFilter(int radius) => SetRadius(radius);
 41
 42    #endregion
 43
 44    #region methods
 45
 46    /// <summary>
 47    /// Filters highly frequented changes in a field of <see cref="ReFlex.Core.Common.Components.Point3"/>.
 48    /// </summary>
 49    public void Filter(PointCloud3 target)
 050    {
 051        var targetRef = target.AsJaggedArray();
 052        var width = target.SizeX;
 053        var height = target.SizeY;
 54
 55        // x direction
 056        for (var y = 0; y < height; ++y)
 057        {
 058            var start = targetRef[0][y];
 059            var sum = DefaultValue * _diameter;
 60
 061            for (var x = 0; x < width; x++)
 062            {
 063                var nrad = x - _radius - 1;
 064                var prad = x + _radius;
 65
 066                var sub = nrad >= 0 ? targetRef[nrad][y] : start;
 067                var add = prad < width ? targetRef[prad][y] : targetRef[width - 1][y];
 68
 069                sum -= sub.IsValid ? sub.Z : DefaultValue;
 070                sum += add.IsValid ? add.Z : DefaultValue;
 71
 072                targetRef[x][y].Z = sum / _diameter;
 073            }
 074        }
 75
 76        // y direction
 077        for (var x = 0; x < width; ++x)
 078        {
 079            var start = targetRef[x][0];
 080            var sum = DefaultValue * _diameter;
 81
 082            for (var y = 0; y < height; y++)
 083            {
 084                var nrad = y - _radius - 1;
 085                var prad = y + _radius;
 86
 087                var sub = nrad >= 0 ? targetRef[x][nrad] : start;
 088                var add = prad < height ? targetRef[x][prad] : targetRef[x][height - 1];
 89
 090                sum -= sub.IsValid ? sub.Z : DefaultValue;
 091                sum += add.IsValid ? add.Z : DefaultValue;
 92
 093                targetRef[x][y].Z = sum / _diameter;
 094            }
 095        }
 096    }
 97
 98    /// <summary>
 99    /// Filters highly frequented changes in a field of <see cref="ReFlex.Core.Common.Components.Point3"/>.
 100    /// </summary>
 101    public void FilterOptimized(PointCloud3 target)
 0102    {
 0103        var targetRef = target.AsSpan();
 0104        _width = target.SizeX;
 0105        var height = target.SizeY;
 106
 107        // x direction
 0108        for (var y = 0; y < height; ++y)
 0109        {
 0110            var start = targetRef[ComputeIndex(0, y)];
 0111            var sum = DefaultValue * _diameter;
 112
 0113            for (var x = 0; x < _width; x++)
 0114            {
 0115                var nrad = x - _radius - 1;
 0116                var prad = x + _radius;
 117
 0118                var sub = nrad >= 0 ? targetRef[ComputeIndex(nrad, y)] : start;
 0119                var add = prad < _width ? targetRef[ComputeIndex(prad, y)] : targetRef[ComputeIndex(_width - 1, y)];
 120
 0121                sum -= sub.IsValid ? sub.Z : DefaultValue;
 0122                sum += add.IsValid ? add.Z : DefaultValue;
 123
 0124                targetRef[ComputeIndex(x, y)].Z = sum / _diameter;
 0125            }
 0126        }
 127
 128        // y direction
 0129        for (var x = 0; x < _width; ++x)
 0130        {
 0131            var start = targetRef[ComputeIndex(x, 0)];
 0132            var sum = DefaultValue * _diameter;
 133
 0134            for (var y = 0; y < height; y++)
 0135            {
 0136                var nrad = y - _radius - 1;
 0137                var prad = y + _radius;
 138
 0139                var sub = nrad >= 0 ? targetRef[ComputeIndex(x, nrad)] : start;
 0140                var add = prad < height ? targetRef[ComputeIndex(x, prad)] : targetRef[ComputeIndex(x, height - 1)];
 141
 0142                sum -= sub.IsValid ? sub.Z : DefaultValue;
 0143                sum += add.IsValid ? add.Z : DefaultValue;
 144
 0145                targetRef[ComputeIndex(x, y)].Z = sum / _diameter;
 0146            }
 0147        }
 0148    }
 149
 150    /// <summary>
 151    /// Filters highly frequented changes in a field of <see cref="ReFlex.Core.Common.Components.Point3"/>.
 152    /// </summary>
 153    public void FilterOptimized2(PointCloud3 target)
 0154    {
 0155        var targetRef = target.AsArray().AsSpan();
 0156        _width = target.SizeX;
 0157        var height = target.SizeY;
 158
 159        // x direction
 0160        for (var y = 0; y < height; ++y)
 0161        {
 0162            var start = targetRef[ComputeIndex(0, y)];
 0163            var sum = DefaultValue * _diameter;
 164
 0165            for (var x = 0; x < _width; x++)
 0166            {
 0167                var nrad = x - _radius - 1;
 0168                var prad = x + _radius;
 169
 0170                var sub = nrad >= 0 ? targetRef[ComputeIndex(nrad, y)] : start;
 0171                var add = prad < _width ? targetRef[ComputeIndex(prad, y)] : targetRef[ComputeIndex(_width - 1, y)];
 172
 0173                sum -= sub.IsValid ? sub.Z : DefaultValue;
 0174                sum += add.IsValid ? add.Z : DefaultValue;
 175
 0176                targetRef[ComputeIndex(x, y)].Z = sum / _diameter;
 0177            }
 0178        }
 179
 180        // y direction
 0181        for (var x = 0; x < _width; ++x)
 0182        {
 0183            var start = targetRef[ComputeIndex(x, 0)];
 0184            var sum = DefaultValue * _diameter;
 185
 0186            for (var y = 0; y < height; y++)
 0187            {
 0188                var nrad = y - _radius - 1;
 0189                var prad = y + _radius;
 190
 0191                var sub = nrad >= 0 ? targetRef[ComputeIndex(x, nrad)] : start;
 0192                var add = prad < height ? targetRef[ComputeIndex(x, prad)] : targetRef[ComputeIndex(x, height - 1)];
 193
 0194                sum -= sub.IsValid ? sub.Z : DefaultValue;
 0195                sum += add.IsValid ? add.Z : DefaultValue;
 196
 0197                targetRef[ComputeIndex(x, y)].Z = sum / _diameter;
 0198            }
 0199        }
 0200    }
 201
 202    /// <summary>
 203    /// Filters highly frequented changes in a field of <see cref="ReFlex.Core.Common.Components.Point3"/>.
 204    /// </summary>
 205    public void FilterOptimized3(PointCloud3 target)
 0206    {
 0207        var targetRef = target.AsArray();
 0208        _width = target.SizeX;
 0209        var height = target.SizeY;
 210
 211        // x direction
 0212        for (var y = 0; y < height; ++y)
 0213        {
 0214            var start = targetRef[ComputeIndex(0, y)];
 0215            var sum = DefaultValue * _diameter;
 216
 0217            for (var x = 0; x < _width; x++)
 0218            {
 0219                var nrad = x - _radius - 1;
 0220                var prad = x + _radius;
 221
 0222                var sub = nrad >= 0 ? targetRef[ComputeIndex(nrad, y)] : start;
 0223                var add = prad < _width ? targetRef[ComputeIndex(prad, y)] : targetRef[ComputeIndex(_width - 1, y)];
 224
 0225                sum -= sub.IsValid ? sub.Z : DefaultValue;
 0226                sum += add.IsValid ? add.Z : DefaultValue;
 227
 0228                targetRef[ComputeIndex(x, y)].Z = sum / _diameter;
 0229            }
 0230        }
 231
 232        // y direction
 0233        for (var x = 0; x < _width; ++x)
 0234        {
 0235            var start = targetRef[ComputeIndex(x, 0)];
 0236            var sum = DefaultValue * _diameter;
 237
 0238            for (var y = 0; y < height; y++)
 0239            {
 0240                var nrad = y - _radius - 1;
 0241                var prad = y + _radius;
 242
 0243                var sub = nrad >= 0 ? targetRef[ComputeIndex(x, nrad)] : start;
 0244                var add = prad < height ? targetRef[ComputeIndex(x, prad)] : targetRef[ComputeIndex(x, height - 1)];
 245
 0246                sum -= sub.IsValid ? sub.Z : DefaultValue;
 0247                sum += add.IsValid ? add.Z : DefaultValue;
 248
 0249                targetRef[ComputeIndex(x, y)].Z = sum / _diameter;
 0250            }
 0251        }
 0252    }
 253
 254
 255    /// <summary>
 256    /// Sets the radius and calculates the diameter.
 257    /// </summary>
 258    /// <param name="radius">The radius.</param>
 259    private void SetRadius(int radius)
 0260    {
 0261        _radius = radius;
 0262        _diameter = radius * 2 + 1;
 0263    }
 264
 265    private int ComputeIndex(int x, int y)
 0266    {
 0267        return x * _width + y;
 0268    }
 269
 270    #endregion
 271}