| | | 1 | | using System; |
| | | 2 | | using System.Diagnostics; |
| | | 3 | | using System.Threading; |
| | | 4 | | using Implementation.Interfaces; |
| | | 5 | | using NLog; |
| | | 6 | | using ReFlex.Core.Common.Components; |
| | | 7 | | using ReFlex.Core.Tracking.Util; |
| | | 8 | | |
| | | 9 | | namespace Implementation.Components |
| | | 10 | | { |
| | | 11 | | public class DepthImageManager : IDepthImageManager |
| | | 12 | | { |
| | 0 | 13 | | private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); |
| | | 14 | | |
| | | 15 | | private readonly IFilterManager _filterManager; |
| | | 16 | | |
| | 58 | 17 | | public PointCloud3 PointCloud { get; private set; } |
| | | 18 | | |
| | 52 | 19 | | public VectorField2 VectorField { get; private set; } |
| | | 20 | | |
| | | 21 | | public event EventHandler<PointCloud3> PointcloudFiltered; |
| | | 22 | | public event EventHandler<VectorField2> VectorfieldChanged; |
| | | 23 | | public event EventHandler<ImageByteArray> DepthImageChanged; |
| | | 24 | | public event EventHandler<DepthCameraFrame> RawDepthFrameReceived; |
| | | 25 | | |
| | 10 | 26 | | public int ImageWidth { get; private set; } |
| | | 27 | | |
| | 10 | 28 | | public int ImageHeight { get; private set; } |
| | | 29 | | |
| | 8 | 30 | | private readonly object _rasterSync = new(); |
| | | 31 | | private int _isUpdating; |
| | | 32 | | private long _sequence; |
| | 8 | 33 | | internal volatile bool UseObservationSnapshots = true; |
| | 8 | 34 | | internal long RasterSequence => _sequence; |
| | | 35 | | |
| | | 36 | | // Live readers share the producer gate and keep it until their async analysis finishes. |
| | 40 | 37 | | internal bool TryReserveRasters() => Interlocked.CompareExchange(ref _isUpdating, 1, 0) == 0; |
| | 38 | 38 | | internal void ReleaseRasters() => Volatile.Write(ref _isUpdating, 0); |
| | | 39 | | internal T ReadRasters<T>(Func<T> read) |
| | 4 | 40 | | { |
| | 8 | 41 | | lock (_rasterSync) return read(); |
| | 4 | 42 | | } |
| | | 43 | | |
| | | 44 | | // Internal consumers may copy now or reserve the live rasters before reading them later. |
| | | 45 | | internal event Action<long, double, PointCloud3, VectorField2> SnapshotSourceReady; |
| | | 46 | | public event EventHandler<TrackingObservation> ObservationReady; |
| | | 47 | | |
| | 8 | 48 | | public DepthImageManager(IFilterManager filterManager) |
| | 8 | 49 | | { |
| | 8 | 50 | | _filterManager = filterManager; |
| | 8 | 51 | | } |
| | | 52 | | |
| | | 53 | | public void Initialize(int sizeX, int sizeY) |
| | 10 | 54 | | { |
| | 10 | 55 | | var cloud = new PointCloud3(sizeX, sizeY); |
| | 10 | 56 | | var vectors = new VectorField2(sizeX, sizeY); |
| | 10 | 57 | | lock (_rasterSync) |
| | 10 | 58 | | { |
| | 10 | 59 | | PointCloud = cloud; |
| | 10 | 60 | | VectorField = vectors; |
| | 10 | 61 | | ImageWidth = sizeX; |
| | 10 | 62 | | ImageHeight = sizeY; |
| | 10 | 63 | | } |
| | 10 | 64 | | OnPointcloudFiltered(this, cloud); |
| | 10 | 65 | | OnVectorfieldChanged(this, vectors); |
| | 10 | 66 | | } |
| | | 67 | | |
| | | 68 | | public void Update(DepthCameraFrame frame) |
| | 32 | 69 | | { |
| | 34 | 70 | | if (!TryReserveRasters()) return; |
| | 30 | 71 | | var sequence = ++_sequence; |
| | 30 | 72 | | var timestamp = (double)Stopwatch.GetTimestamp() / Stopwatch.Frequency; |
| | | 73 | | try |
| | 30 | 74 | | { |
| | 30 | 75 | | RawDepthFrameReceived?.Invoke(this, frame); |
| | | 76 | | PointCloud3 cloud; |
| | | 77 | | VectorField2 vectors; |
| | 30 | 78 | | lock (_rasterSync) |
| | 30 | 79 | | { |
| | 30 | 80 | | cloud = PointCloud; |
| | 30 | 81 | | vectors = VectorField; |
| | 30 | 82 | | _filterManager.FilterAndUpdate(frame.Depth, cloud); |
| | 30 | 83 | | vectors.Populate(cloud); |
| | 30 | 84 | | } |
| | 30 | 85 | | SnapshotSourceReady?.Invoke(sequence, timestamp, cloud, vectors); |
| | 30 | 86 | | var handlers = ObservationReady; |
| | 30 | 87 | | if (UseObservationSnapshots && handlers != null) |
| | 16 | 88 | | handlers(this, new TrackingObservation(sequence, timestamp, cloud, vectors)); |
| | 30 | 89 | | OnPointcloudFiltered(this, cloud); |
| | 30 | 90 | | OnVectorfieldChanged(this, vectors); |
| | 30 | 91 | | } |
| | 0 | 92 | | catch (Exception exc) |
| | 0 | 93 | | { |
| | 0 | 94 | | Logger.Error(exc); |
| | 0 | 95 | | } |
| | 90 | 96 | | finally { ReleaseRasters(); } |
| | 32 | 97 | | } |
| | | 98 | | |
| | | 99 | | public void Update(ImageByteArray depthImage) |
| | 0 | 100 | | { |
| | | 101 | | //@ todo: maybe convert into 8-bit image ? |
| | 0 | 102 | | DepthImageChanged?.Invoke(this, depthImage); |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | protected virtual void OnPointcloudFiltered(object sender, PointCloud3 pointcloud) => |
| | 40 | 106 | | PointcloudFiltered?.Invoke(sender, pointcloud); |
| | | 107 | | |
| | | 108 | | protected virtual void OnVectorfieldChanged(object sender, VectorField2 vectorfield) => |
| | 40 | 109 | | VectorfieldChanged?.Invoke(sender, vectorfield); |
| | | 110 | | |
| | | 111 | | } |
| | | 112 | | } |