< Summary - ReFlex - Library

Information
Class: ReFlex.Sensor.EmulatorModule.DepthStreamReplayCamera
Assembly: ReFlex.Sensor.EmulatorModule
File(s): D:\a\reflex\reflex\library\src\Sensor\EmulatorModule\DepthStreamReplayCamera.cs
Line coverage
84%
Covered lines: 78
Uncovered lines: 14
Coverable lines: 92
Total lines: 180
Line coverage: 84.7%
Branch coverage
69%
Covered branches: 29
Total branches: 42
Branch coverage: 69%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
.ctor()100%11100%
get_Id()100%210%
get_CameraType()100%11100%
get_ModelDescription()100%210%
get_State()75%44100%
get_StreamParameter()100%11100%
Initialize()100%210%
EnableStream(...)50%2293.75%
GetPossibleConfigurations()100%11100%
StartStream()50%15853.33%
StopStream()75%44100%
Stream()75%1616100%
ConvertImage(...)75%44100%
GetSizePerPixel(...)75%4485.71%

File(s)

D:\a\reflex\reflex\library\src\Sensor\EmulatorModule\DepthStreamReplayCamera.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Runtime.CompilerServices;
 5using System.Threading.Tasks;
 6using NLog;
 7using ReFlex.Core.Common.Components;
 8using ReFlex.Core.Common.Util;
 9using ReFlex.Core.Networking.Util;
 10using ReFlex.Core.Tracking.Interfaces;
 11using ReFlex.Core.Tracking.Util;
 12using SixLabors.ImageSharp;
 13using SixLabors.ImageSharp.PixelFormats;
 14
 15namespace ReFlex.Sensor.EmulatorModule
 16{
 17    public class DepthStreamReplayCamera : IDepthCamera
 18    {
 019        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
 20        private bool _isStarted;
 2221        private int _numConfigs = 0;
 22        private bool _stopRequested;
 23
 24        private EmulatedPointCloud _pointCloud;
 25
 026        public string Id => "ReplayCamera";
 2227        public CameraType CameraType { get; } = CameraType.Emulator;
 28
 029        public string ModelDescription => Id;
 30
 31        public DepthCameraState State =>
 8832            _numConfigs == 0
 8833                ? DepthCameraState.Disconnected
 8834                : _isStarted
 8835                    ? DepthCameraState.Streaming
 8836                    : DepthCameraState.Connected;
 37
 15438        public StreamParameter StreamParameter { get; private set; }
 39
 40        public event EventHandler<DepthCameraState> StateChanged;
 41        public event EventHandler<DepthCameraFrame> FrameReady;
 42        public event EventHandler<ImageByteArray> DepthImageReady;
 43
 44        public void Initialize()
 045        {
 46
 047        }
 48
 49        public void EnableStream(StreamParameter parameter)
 2250        {
 2251            if (_isStarted)
 052                StopStream();
 53
 2254            var param = new EmulatorParameters
 2255            {
 2256                HeightInMeters = 1.6f,
 2257                WidthInMeters = 2.4f,
 2258                MaxDepthInMeter = 2.1f,
 2259                MinDepthInMeter = 0.8f,
 2260                PlaneDistanceInMeter = 1.45f,
 2261                Radius = (int)(parameter.Width * 0.4)
 2262            };
 63
 2264            StreamParameter = parameter;
 65
 2266            _pointCloud = new EmulatedPointCloud(param);
 2267            _pointCloud.InitializePointCloud(parameter);
 2268        }
 69
 70        public IList<StreamParameter> GetPossibleConfigurations()
 2271        {
 2272            var result = RecordingUtils.RetrieveConfigurations().ToList();
 73
 2274            _numConfigs = result.Count;
 75
 2276            return result;
 2277        }
 78
 79        public void StartStream()
 2280        {
 2281            if (StreamParameter == null || State != DepthCameraState.Connected)
 082            {
 083                Logger.Log(LogLevel.Error, $"Cannot start {GetType().Name}: incorrect configuration.");
 084                return;
 85            }
 86
 2287            if (_isStarted)
 088            {
 089                StopStream();
 090                Task.Delay(TimeSpan.FromMilliseconds(500));
 091            }
 92
 2293            var delay = 1000 / StreamParameter.Framerate;
 94
 2295            _isStarted = true;
 96
 2297            StateChanged?.Invoke(this, State);
 98
 4499            Task.Run(() => Stream(RecordingUtils.GetFrames(StreamParameter.Name), delay, StreamParameter.Width, StreamPa
 22100        }
 101
 102        public void StopStream()
 24103        {
 24104            if (!_isStarted)
 2105            {
 2106                StateChanged?.Invoke(this, State);
 2107                return;
 108            }
 109
 22110            _stopRequested = true;
 24111        }
 112
 113        private async Task Stream(IReadOnlyList<string> frames, int delay, int width, int height, DepthImageFormat forma
 22114        {
 22115            var i = 0;
 116
 50117            while (i < frames?.Count && !_stopRequested)
 28118            {
 28119                switch (format)
 120                {
 121                    case DepthImageFormat.Greyccale48bpp:
 16122                        ConvertImage<Rgb48>(frames[i], width, height, format);
 16123                        break;
 124                    case DepthImageFormat.Greyscale8bpp:
 4125                        ConvertImage<L8>(frames[i], width, height, format);
 4126                        break;
 127                    case DepthImageFormat.Rgb24bpp:
 128                    default:
 8129                        ConvertImage<Rgb24>(frames[i], width, height, format);
 8130                        break;
 131                }
 132
 28133                await Task.Delay(TimeSpan.FromMilliseconds(delay));
 134
 28135                i = i < frames?.Count - 1 ? i + 1 : 0;
 136
 28137            }
 138
 22139            _isStarted = false;
 140
 22141            StateChanged?.Invoke(this, State);
 142
 22143            _stopRequested = false;
 22144        }
 145
 28146        private void ConvertImage<T>(string file, int width, int height, DepthImageFormat format) where T : unmanaged, I
 147
 28148            using var img = Image.Load<T>(file);
 149
 28150            var size = GetSizePerPixel(format);
 151
 28152            var data = new byte[img.Width * img.Height * size];
 28153            img.CopyPixelDataTo(data);
 154
 28155            var args = new ImageByteArray(data, width, height, DepthImageFormatTools.BytesPerChannel(format), DepthImage
 156
 28157            _pointCloud.Reset();
 28158            _pointCloud.UpdateFromGreyScaleImage(args);
 159
 28160            DepthImageReady?.Invoke(this, args);
 28161            FrameReady?.Invoke(this, new DepthCameraFrame { Depth = _pointCloud.Points });
 162
 56163        }
 164
 165        private static int GetSizePerPixel(DepthImageFormat format)
 28166        {
 28167            switch (format)
 168            {
 169                case DepthImageFormat.Greyccale48bpp:
 16170                    return Unsafe.SizeOf<Rgb48>();
 171                case DepthImageFormat.Rgb24bpp:
 8172                    return Unsafe.SizeOf<Rgb24>();
 173                case DepthImageFormat.Greyscale8bpp:
 4174                    return Unsafe.SizeOf<L8>();
 175                default:
 0176                    return Unsafe.SizeOf<Rgba32>();
 177            }
 28178        }
 179    }
 180}